diff --git a/cli/esbuild.mjs b/cli/esbuild.mjs index 8ae92293..b068a438 100644 --- a/cli/esbuild.mjs +++ b/cli/esbuild.mjs @@ -1,4 +1,4 @@ -import { build} from 'esbuild' +import { build } from 'esbuild' import { pull } from 'lodash-es' import packageJson from './package.json' with { type: 'json' } @@ -9,7 +9,11 @@ await build({ bundle: true, format: 'cjs', platform: 'node', - external: pull(Object.keys(packageJson.dependencies), '@octokit/rest', 'lodash-es'), + external: pull( + Object.keys(packageJson.dependencies), + '@octokit/rest', + 'lodash-es', + ), define: { 'import.meta.url': '__filename', }, diff --git a/cli/src/api/templates/js-binding.ts b/cli/src/api/templates/js-binding.ts index 9ac04f6c..569b122f 100644 --- a/cli/src/api/templates/js-binding.ts +++ b/cli/src/api/templates/js-binding.ts @@ -211,11 +211,12 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { if (!nativeBinding) { if (loadErrors.length > 0) { - // TODO Link to documentation with potential fixes - // - The package owner could build/publish bindings for this arch - // - The user may need to bundle the correct files - // - The user may need to re-install node_modules to get new packages - throw new Error('Failed to load native binding', { cause: loadErrors }) + throw new Error( + \`Cannot find native binding. \` + + \`npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). \` + + 'Please try \`npm i\` again after removing both package-lock.json and node_modules directory.', + { cause: loadErrors } + ) } throw new Error(\`Failed to load native binding\`) } diff --git a/cli/src/utils/log.ts b/cli/src/utils/log.ts index f55629a3..d75e2c19 100644 --- a/cli/src/utils/log.ts +++ b/cli/src/utils/log.ts @@ -25,7 +25,7 @@ export const debugFactory = (namespace: string) => { console.error( colors.white(colors.bgRed(' ERROR ')), ...args.map((arg) => - arg instanceof Error ? arg.stack ?? arg.message : arg, + arg instanceof Error ? (arg.stack ?? arg.message) : arg, ), ) diff --git a/examples/napi-compat-mode/__tests__/napi6/bigint.spec.ts b/examples/napi-compat-mode/__tests__/napi6/bigint.spec.ts index a997ba9e..e65f426a 100644 --- a/examples/napi-compat-mode/__tests__/napi6/bigint.spec.ts +++ b/examples/napi-compat-mode/__tests__/napi6/bigint.spec.ts @@ -18,10 +18,7 @@ test('should create bigints', (t) => { bindings.testCreateBigintFromMinI128(), BigInt('-170141183460469231731687303715884105728'), ) - t.is( - bindings.testCreateBigintFromNegativeI128(), - BigInt('-10'), - ) + t.is(bindings.testCreateBigintFromNegativeI128(), BigInt('-10')) t.is( bindings.testCreateBigintFromU128(), BigInt('340282366920938463463374607431768211455'), diff --git a/examples/napi-compat-mode/index.wasi.cjs b/examples/napi-compat-mode/index.wasi.cjs index 562227a6..531be7b1 100644 --- a/examples/napi-compat-mode/index.wasi.cjs +++ b/examples/napi-compat-mode/index.wasi.cjs @@ -21,7 +21,7 @@ const __wasi = new __nodeWASI({ env: process.env, preopens: { [__rootDir]: __rootDir, - } + }, }) const __emnapiContext = __emnapiGetDefaultContext() @@ -41,14 +41,23 @@ if (__nodeFs.existsSync(__wasmDebugFilePath)) { try { __wasmFilePath = __nodePath.resolve('@examples/compat-mode-wasm32-wasi') } catch { - throw new Error('Cannot find index.wasm file, and @examples/compat-mode-wasm32-wasi package is not installed.') + throw new Error( + 'Cannot find index.wasm file, and @examples/compat-mode-wasm32-wasi package is not installed.', + ) } } -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), { +const { + instance: __napiInstance, + module: __wasiModule, + napiModule: __napiModule, +} = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), { context: __emnapiContext, - asyncWorkPoolSize: (function() { - const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE) + asyncWorkPoolSize: (function () { + const threadsSizeFromEnv = Number( + process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? + process.env.UV_THREADPOOL_SIZE, + ) // NaN > 0 is false if (threadsSizeFromEnv > 0) { return threadsSizeFromEnv @@ -85,4 +94,3 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule }, }) module.exports = __napiModule.exports - diff --git a/examples/napi-compat-mode/wasi-worker-browser.mjs b/examples/napi-compat-mode/wasi-worker-browser.mjs index 8b1b1722..37244bef 100644 --- a/examples/napi-compat-mode/wasi-worker-browser.mjs +++ b/examples/napi-compat-mode/wasi-worker-browser.mjs @@ -1,4 +1,8 @@ -import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime' +import { + instantiateNapiModuleSync, + MessageHandler, + WASI, +} from '@napi-rs/wasm-runtime' const handler = new MessageHandler({ onLoad({ wasmModule, wasmMemory }) { @@ -7,7 +11,7 @@ const handler = new MessageHandler({ // eslint-disable-next-line no-console console.log.apply(console, arguments) }, - printErr: function() { + printErr: function () { // eslint-disable-next-line no-console console.error.apply(console, arguments) }, diff --git a/examples/napi-compat-mode/wasi-worker.mjs b/examples/napi-compat-mode/wasi-worker.mjs index 84b448fc..359897ee 100644 --- a/examples/napi-compat-mode/wasi-worker.mjs +++ b/examples/napi-compat-mode/wasi-worker.mjs @@ -1,17 +1,21 @@ -import fs from "node:fs"; -import { createRequire } from "node:module"; -import { parse } from "node:path"; -import { WASI } from "node:wasi"; -import { parentPort, Worker } from "node:worker_threads"; +import fs from 'node:fs' +import { createRequire } from 'node:module' +import { parse } from 'node:path' +import { WASI } from 'node:wasi' +import { parentPort, Worker } from 'node:worker_threads' -const require = createRequire(import.meta.url); +const require = createRequire(import.meta.url) -const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime"); +const { + instantiateNapiModuleSync, + MessageHandler, + getDefaultContext, +} = require('@napi-rs/wasm-runtime') if (parentPort) { - parentPort.on("message", (data) => { - globalThis.onmessage({ data }); - }); + parentPort.on('message', (data) => { + globalThis.onmessage({ data }) + }) } Object.assign(globalThis, { @@ -19,18 +23,18 @@ Object.assign(globalThis, { require, Worker, importScripts: function (f) { - ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + ;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f) }, postMessage: function (msg) { if (parentPort) { - parentPort.postMessage(msg); + parentPort.postMessage(msg) } }, -}); +}) -const emnapiContext = getDefaultContext(); +const emnapiContext = getDefaultContext() -const __rootDir = parse(process.cwd()).root; +const __rootDir = parse(process.cwd()).root const handler = new MessageHandler({ onLoad({ wasmModule, wasmMemory }) { @@ -40,7 +44,7 @@ const handler = new MessageHandler({ preopens: { [__rootDir]: __rootDir, }, - }); + }) return instantiateNapiModuleSync(wasmModule, { childThread: true, @@ -51,13 +55,13 @@ const handler = new MessageHandler({ ...importObject.env, ...importObject.napi, ...importObject.emnapi, - memory: wasmMemory - }; + memory: wasmMemory, + } }, - }); + }) }, -}); +}) globalThis.onmessage = function (e) { - handler.handle(e); -}; + handler.handle(e) +} diff --git a/examples/napi/index.cjs b/examples/napi/index.cjs index 7a48d8db..0d35d555 100644 --- a/examples/napi/index.cjs +++ b/examples/napi/index.cjs @@ -365,11 +365,12 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { if (!nativeBinding) { if (loadErrors.length > 0) { - // TODO Link to documentation with potential fixes - // - The package owner could build/publish bindings for this arch - // - The user may need to bundle the correct files - // - The user may need to re-install node_modules to get new packages - throw new Error('Failed to load native binding', { cause: loadErrors }) + throw new Error( + `Cannot find native binding. ` + + `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` + + 'Please try `npm i` again after removing both package-lock.json and node_modules directory.', + { cause: loadErrors } + ) } throw new Error(`Failed to load native binding`) }