chore(cli): link npm cli issues in native binding load errors (#2707)

* chore(cli): link npm cli issues in native binding load errors

* run fmt
This commit is contained in:
LongYinan 2025-06-12 03:02:19 +08:00 committed by GitHub
parent 6f74589a25
commit e9835c4e75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 66 additions and 47 deletions

View File

@ -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',
},

View File

@ -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\`)
}

View File

@ -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,
),
)

View File

@ -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'),

View File

@ -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

View File

@ -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)
},

View File

@ -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)
}

View File

@ -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`)
}