From cd21401f5d049ea18f5d7bb577b55c2d600236d8 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 29 Dec 2023 18:41:50 +0800 Subject: [PATCH 1/3] feat(*): support wasi-wasm32 target --- .eslintignore | 4 + .gitignore | 3 +- .prettierignore | 3 + crates/alloc/Cargo.toml | 2 +- crates/alloc/src/lib.rs | 1 + package.json | 2 + packages/argon2/argon2.wasi.cjs | 75 +++++++++ packages/argon2/index.d.ts | 30 +--- packages/argon2/index.js | 49 ++++-- .../argon2/npm/android-arm-eabi/package.json | 8 +- .../argon2/npm/android-arm64/package.json | 8 +- packages/argon2/npm/darwin-arm64/package.json | 8 +- packages/argon2/npm/darwin-x64/package.json | 8 +- packages/argon2/npm/freebsd-x64/package.json | 8 +- .../npm/linux-arm-gnueabihf/package.json | 8 +- .../argon2/npm/linux-arm64-gnu/package.json | 6 +- .../argon2/npm/linux-arm64-musl/package.json | 6 +- .../argon2/npm/linux-x64-gnu/package.json | 6 +- .../argon2/npm/linux-x64-musl/package.json | 6 +- packages/argon2/npm/wasm32-wasi/README.md | 3 + packages/argon2/npm/wasm32-wasi/package.json | 36 +++++ .../argon2/npm/win32-arm64-msvc/package.json | 8 +- .../argon2/npm/win32-ia32-msvc/package.json | 8 +- .../argon2/npm/win32-x64-msvc/package.json | 8 +- packages/argon2/package.json | 3 +- packages/argon2/wasi-worker.mjs | 51 ++++++ packages/bcrypt/bcrypt.wasi.cjs | 72 +++++++++ .../bcrypt/npm/android-arm-eabi/package.json | 8 +- .../bcrypt/npm/android-arm64/package.json | 8 +- packages/bcrypt/npm/darwin-arm64/package.json | 8 +- packages/bcrypt/npm/darwin-x64/package.json | 8 +- packages/bcrypt/npm/freebsd-x64/package.json | 8 +- .../npm/linux-arm-gnueabihf/package.json | 8 +- .../bcrypt/npm/linux-arm64-gnu/package.json | 6 +- .../bcrypt/npm/linux-arm64-musl/package.json | 6 +- .../bcrypt/npm/linux-x64-gnu/package.json | 6 +- .../bcrypt/npm/linux-x64-musl/package.json | 6 +- packages/bcrypt/npm/wasm32-wasi/README.md | 3 + packages/bcrypt/npm/wasm32-wasi/package.json | 40 +++++ .../bcrypt/npm/win32-arm64-msvc/package.json | 8 +- .../bcrypt/npm/win32-ia32-msvc/package.json | 8 +- .../bcrypt/npm/win32-x64-msvc/package.json | 8 +- packages/bcrypt/package.json | 3 +- packages/bcrypt/wasi-worker.mjs | 51 ++++++ packages/crc32/Cargo.toml | 2 +- packages/crc32/crc32.wasi.cjs | 59 +++++++ packages/crc32/index.d.ts | 7 +- packages/crc32/index.js | 152 +++++++++++++++--- .../crc32/npm/android-arm-eabi/package.json | 8 +- packages/crc32/npm/android-arm64/package.json | 8 +- packages/crc32/npm/darwin-arm64/package.json | 8 +- packages/crc32/npm/darwin-x64/package.json | 8 +- packages/crc32/npm/freebsd-x64/package.json | 8 +- .../npm/linux-arm-gnueabihf/package.json | 8 +- .../crc32/npm/linux-arm64-gnu/package.json | 6 +- .../crc32/npm/linux-arm64-musl/package.json | 6 +- packages/crc32/npm/linux-x64-gnu/package.json | 6 +- .../crc32/npm/linux-x64-musl/package.json | 6 +- packages/crc32/npm/wasm32-wasi/README.md | 3 + packages/crc32/npm/wasm32-wasi/package.json | 37 +++++ .../crc32/npm/win32-arm64-msvc/package.json | 8 +- .../crc32/npm/win32-ia32-msvc/package.json | 8 +- .../crc32/npm/win32-x64-msvc/package.json | 8 +- packages/crc32/package.json | 3 +- packages/crc32/wasi-worker.mjs | 51 ++++++ packages/deno-lint/deno-lint.wasi.cjs | 59 +++++++ packages/deno-lint/index.d.ts | 7 +- packages/deno-lint/index.js | 49 ++++-- .../npm/android-arm-eabi/package.json | 8 +- .../deno-lint/npm/android-arm64/package.json | 8 +- .../deno-lint/npm/darwin-arm64/package.json | 8 +- .../deno-lint/npm/darwin-x64/package.json | 8 +- .../deno-lint/npm/freebsd-x64/package.json | 8 +- .../npm/linux-arm-gnueabihf/package.json | 8 +- .../npm/linux-arm64-gnu/package.json | 6 +- .../npm/linux-arm64-musl/package.json | 6 +- .../deno-lint/npm/linux-x64-gnu/package.json | 6 +- .../deno-lint/npm/linux-x64-musl/package.json | 6 +- packages/deno-lint/npm/wasm32-wasi/README.md | 3 + .../deno-lint/npm/wasm32-wasi/package.json | 39 +++++ .../npm/win32-arm64-msvc/package.json | 8 +- .../npm/win32-ia32-msvc/package.json | 8 +- .../deno-lint/npm/win32-x64-msvc/package.json | 8 +- packages/deno-lint/package.json | 3 +- packages/deno-lint/wasi-worker.mjs | 51 ++++++ packages/jieba/index.d.ts | 1 + packages/jieba/index.js | 49 ++++-- packages/jieba/jieba.wasi.cjs | 73 +++++++++ .../jieba/npm/android-arm-eabi/package.json | 8 +- packages/jieba/npm/android-arm64/package.json | 8 +- packages/jieba/npm/darwin-arm64/package.json | 8 +- packages/jieba/npm/darwin-x64/package.json | 8 +- packages/jieba/npm/freebsd-x64/package.json | 8 +- .../npm/linux-arm-gnueabihf/package.json | 8 +- .../jieba/npm/linux-arm64-gnu/package.json | 6 +- .../jieba/npm/linux-arm64-musl/package.json | 6 +- packages/jieba/npm/linux-x64-gnu/package.json | 6 +- .../jieba/npm/linux-x64-musl/package.json | 6 +- packages/jieba/npm/wasm32-wasi/README.md | 3 + packages/jieba/npm/wasm32-wasi/package.json | 37 +++++ .../jieba/npm/win32-arm64-msvc/package.json | 8 +- .../jieba/npm/win32-ia32-msvc/package.json | 8 +- .../jieba/npm/win32-x64-msvc/package.json | 8 +- packages/jieba/package.json | 3 +- packages/jieba/wasi-worker.mjs | 51 ++++++ packages/jsonwebtoken/index.d.ts | 17 +- packages/jsonwebtoken/index.js | 50 ++++-- packages/jsonwebtoken/jsonwebtoken.wasi.cjs | 70 ++++++++ .../npm/android-arm-eabi/package.json | 8 +- .../npm/android-arm64/package.json | 8 +- .../npm/darwin-arm64/package.json | 8 +- .../jsonwebtoken/npm/darwin-x64/package.json | 8 +- .../jsonwebtoken/npm/freebsd-x64/package.json | 8 +- .../npm/linux-arm-gnueabihf/package.json | 8 +- .../npm/linux-arm64-gnu/package.json | 6 +- .../npm/linux-arm64-musl/package.json | 6 +- .../npm/linux-x64-gnu/package.json | 6 +- .../npm/linux-x64-musl/package.json | 6 +- .../jsonwebtoken/npm/wasm32-wasi/README.md | 3 + .../jsonwebtoken/npm/wasm32-wasi/package.json | 39 +++++ .../npm/win32-arm64-msvc/package.json | 8 +- .../npm/win32-ia32-msvc/package.json | 8 +- .../npm/win32-x64-msvc/package.json | 8 +- packages/jsonwebtoken/package.json | 3 +- packages/jsonwebtoken/wasi-worker.mjs | 51 ++++++ packages/xxhash/index.d.ts | 21 +-- packages/xxhash/index.js | 49 ++++-- .../xxhash/npm/android-arm-eabi/package.json | 8 +- .../xxhash/npm/android-arm64/package.json | 8 +- packages/xxhash/npm/darwin-arm64/package.json | 8 +- packages/xxhash/npm/darwin-x64/package.json | 8 +- packages/xxhash/npm/freebsd-x64/package.json | 8 +- .../npm/linux-arm-gnueabihf/package.json | 8 +- .../xxhash/npm/linux-arm64-gnu/package.json | 6 +- .../xxhash/npm/linux-arm64-musl/package.json | 6 +- .../xxhash/npm/linux-x64-gnu/package.json | 6 +- .../xxhash/npm/linux-x64-musl/package.json | 6 +- packages/xxhash/npm/wasm32-wasi/README.md | 3 + packages/xxhash/npm/wasm32-wasi/package.json | 40 +++++ .../xxhash/npm/win32-arm64-msvc/package.json | 8 +- .../xxhash/npm/win32-ia32-msvc/package.json | 8 +- .../xxhash/npm/win32-x64-msvc/package.json | 8 +- packages/xxhash/package.json | 3 +- packages/xxhash/src/lib.rs | 20 +-- packages/xxhash/wasi-worker.mjs | 51 ++++++ packages/xxhash/xxhash.wasi.cjs | 72 +++++++++ yarn.lock | 20 +++ 147 files changed, 1861 insertions(+), 496 deletions(-) create mode 100644 packages/argon2/argon2.wasi.cjs create mode 100644 packages/argon2/npm/wasm32-wasi/README.md create mode 100644 packages/argon2/npm/wasm32-wasi/package.json create mode 100644 packages/argon2/wasi-worker.mjs create mode 100644 packages/bcrypt/bcrypt.wasi.cjs create mode 100644 packages/bcrypt/npm/wasm32-wasi/README.md create mode 100644 packages/bcrypt/npm/wasm32-wasi/package.json create mode 100644 packages/bcrypt/wasi-worker.mjs create mode 100644 packages/crc32/crc32.wasi.cjs create mode 100644 packages/crc32/npm/wasm32-wasi/README.md create mode 100644 packages/crc32/npm/wasm32-wasi/package.json create mode 100644 packages/crc32/wasi-worker.mjs create mode 100644 packages/deno-lint/deno-lint.wasi.cjs create mode 100644 packages/deno-lint/npm/wasm32-wasi/README.md create mode 100644 packages/deno-lint/npm/wasm32-wasi/package.json create mode 100644 packages/deno-lint/wasi-worker.mjs create mode 100644 packages/jieba/jieba.wasi.cjs create mode 100644 packages/jieba/npm/wasm32-wasi/README.md create mode 100644 packages/jieba/npm/wasm32-wasi/package.json create mode 100644 packages/jieba/wasi-worker.mjs create mode 100644 packages/jsonwebtoken/jsonwebtoken.wasi.cjs create mode 100644 packages/jsonwebtoken/npm/wasm32-wasi/README.md create mode 100644 packages/jsonwebtoken/npm/wasm32-wasi/package.json create mode 100644 packages/jsonwebtoken/wasi-worker.mjs create mode 100644 packages/xxhash/npm/wasm32-wasi/README.md create mode 100644 packages/xxhash/npm/wasm32-wasi/package.json create mode 100644 packages/xxhash/wasi-worker.mjs create mode 100644 packages/xxhash/xxhash.wasi.cjs diff --git a/.eslintignore b/.eslintignore index 3e3d6c68..d1dd98c5 100644 --- a/.eslintignore +++ b/.eslintignore @@ -9,3 +9,7 @@ coverage target packages/deno-lint/cli.js packages/deno-lint/cli.d.ts +packages/*/*.wasi.cjs +packages/*/wasi-worker.mjs +packages/*/index.js +packages/*/index.d.ts diff --git a/.gitignore b/.gitignore index 9765246f..fcfccedb 100644 --- a/.gitignore +++ b/.gitignore @@ -177,6 +177,7 @@ temp/ # End of https://www.gitignore.io/api/node *.node +*.wasm lib artifacts @@ -186,4 +187,4 @@ artifacts !.yarn/plugins !.yarn/releases !.yarn/sdks -!.yarn/versions \ No newline at end of file +!.yarn/versions diff --git a/.prettierignore b/.prettierignore index 1da3ec1b..1002ac18 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,3 +3,6 @@ node_modules lib .yarn yarn.lock +packages/*/*.wasi.js +packages/*/index.js +packages/*/index.d.ts diff --git a/crates/alloc/Cargo.toml b/crates/alloc/Cargo.toml index 767655a1..ec723ae0 100644 --- a/crates/alloc/Cargo.toml +++ b/crates/alloc/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[target.'cfg(not(target_os = "linux"))'.dependencies] +[target.'cfg(all(not(target_os = "linux"), not(target_family = "wasm")))'.dependencies] mimalloc = { version = "0.1" } [target.'cfg(target_os = "linux")'.dependencies] diff --git a/crates/alloc/src/lib.rs b/crates/alloc/src/lib.rs index dfa36066..16a932e9 100644 --- a/crates/alloc/src/lib.rs +++ b/crates/alloc/src/lib.rs @@ -1,2 +1,3 @@ +#[cfg(not(target_family = "wasm"))] #[global_allocator] static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc; diff --git a/package.json b/package.json index 95956ee6..ab7522e0 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,8 @@ "postinstall": "husky install" }, "devDependencies": { + "@emnapi/core": "^0.44.0", + "@emnapi/runtime": "^0.44.0", "@napi-rs/cli": "^3.0.0-alpha.25", "@swc-node/core": "^1.10.6", "@swc-node/register": "^1.6.8", diff --git a/packages/argon2/argon2.wasi.cjs b/packages/argon2/argon2.wasi.cjs new file mode 100644 index 00000000..a86ff8d7 --- /dev/null +++ b/packages/argon2/argon2.wasi.cjs @@ -0,0 +1,75 @@ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const __nodeFs= require('node:fs') +const __nodePath = require('node:path') +const { WASI: __nodeWASI } = require('node:wasi') +const { Worker } = require('node:worker_threads') + +const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') +const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') + +const __wasi = new __nodeWASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/' + } +}) + +const __emnapiContext = __emnapiGetDefaultContext() + +const __sharedMemory = new WebAssembly.Memory({ + initial: 1024, + maximum: 10240, + shared: true, +}) + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'argon2.wasm32-wasi.wasm')), { + context: __emnapiContext, + asyncWorkPoolSize: 4, + wasi: __wasi, + onCreateWorker() { + return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { + env: process.env, + execArgv: ['--experimental-wasi-unstable-preview1'], + }) + }, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: __sharedMemory, + } + return importObject + }, + beforeInit({ instance }) { + __napi_rs_initialize_modules(instance) + } +}) + +function __napi_rs_initialize_modules(__napiInstance) { + __napiInstance.exports['__napi_register__Algorithm_0']?.() + __napiInstance.exports['__napi_register__Version_1']?.() + __napiInstance.exports['__napi_register__Options_struct_2']?.() + __napiInstance.exports['__napi_register__HashTask_impl_3']?.() + __napiInstance.exports['__napi_register__hash_4']?.() + __napiInstance.exports['__napi_register__hash_sync_5']?.() + __napiInstance.exports['__napi_register__RawHashTask_impl_6']?.() + __napiInstance.exports['__napi_register__hash_raw_7']?.() + __napiInstance.exports['__napi_register__hash_raw_sync_8']?.() + __napiInstance.exports['__napi_register__VerifyTask_impl_9']?.() + __napiInstance.exports['__napi_register__verify_10']?.() + __napiInstance.exports['__napi_register__verify_sync_11']?.() +} +module.exports.Algorithm = __napiModule.exports.Algorithm, +module.exports.hash = __napiModule.exports.hash, +module.exports.hashRaw = __napiModule.exports.hashRaw, +module.exports.hashRawSync = __napiModule.exports.hashRawSync, +module.exports.hashSync = __napiModule.exports.hashSync, +module.exports.verify = __napiModule.exports.verify, +module.exports.verifySync = __napiModule.exports.verifySync, +module.exports.Version = __napiModule.exports.Version diff --git a/packages/argon2/index.d.ts b/packages/argon2/index.d.ts index 236332d2..8d521c17 100644 --- a/packages/argon2/index.d.ts +++ b/packages/argon2/index.d.ts @@ -17,20 +17,12 @@ export const enum Algorithm { * Hybrid that mixes Argon2i and Argon2d passes. * Uses the Argon2i approach for the first half pass over memory and Argon2d approach for subsequent passes. This effectively places it in the “middle” between the other two: it doesn’t provide as good TMTO/GPU cracking resistance as Argon2d, nor as good of side-channel resistance as Argon2i, but overall provides the most well-rounded approach to both classes of attacks. */ - Argon2id = 2, + Argon2id = 2 } -export function hash( - password: string | Buffer, - options?: Options | undefined | null, - abortSignal?: AbortSignal | undefined | null, -): Promise +export function hash(password: string | Buffer, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise -export function hashRaw( - password: string | Buffer, - options?: Options | undefined | null, - abortSignal?: AbortSignal | undefined | null, -): Promise +export function hashRaw(password: string | Buffer, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise export function hashRawSync(password: string | Buffer, options?: Options | undefined | null): Buffer @@ -73,18 +65,9 @@ export interface Options { salt?: Buffer } -export function verify( - hashed: string | Buffer, - password: string | Buffer, - options?: Options | undefined | null, - abortSignal?: AbortSignal | undefined | null, -): Promise +export function verify(hashed: string | Buffer, password: string | Buffer, options?: Options | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise -export function verifySync( - hashed: string | Buffer, - password: string | Buffer, - options?: Options | undefined | null, -): boolean +export function verifySync(hashed: string | Buffer, password: string | Buffer, options?: Options | undefined | null): boolean export const enum Version { /** Version 16 (0x10 in hex) */ @@ -93,5 +76,6 @@ export const enum Version { * Default value * Version 19 (0x13 in hex) */ - V0x13 = 1, + V0x13 = 1 } + diff --git a/packages/argon2/index.js b/packages/argon2/index.js index 9d55a93f..55f879bb 100644 --- a/packages/argon2/index.js +++ b/packages/argon2/index.js @@ -94,7 +94,9 @@ switch (platform) { case 'win32': switch (arch) { case 'x64': - localFileExisted = existsSync(join(__dirname, 'argon2.win32-x64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.win32-x64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.win32-x64-msvc.node') @@ -106,7 +108,9 @@ switch (platform) { } break case 'ia32': - localFileExisted = existsSync(join(__dirname, 'argon2.win32-ia32-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.win32-ia32-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.win32-ia32-msvc.node') @@ -118,7 +122,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'argon2.win32-arm64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.win32-arm64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.win32-arm64-msvc.node') @@ -157,7 +163,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'argon2.darwin-arm64.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.darwin-arm64.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.darwin-arm64.node') @@ -191,7 +199,9 @@ switch (platform) { switch (arch) { case 'x64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'argon2.linux-x64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.linux-x64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.linux-x64-musl.node') @@ -202,7 +212,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'argon2.linux-x64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.linux-x64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.linux-x64-gnu.node') @@ -216,7 +228,9 @@ switch (platform) { break case 'arm64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'argon2.linux-arm64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.linux-arm64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.linux-arm64-musl.node') @@ -227,7 +241,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'argon2.linux-arm64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.linux-arm64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.linux-arm64-gnu.node') @@ -240,7 +256,9 @@ switch (platform) { } break case 'arm': - localFileExisted = existsSync(join(__dirname, 'argon2.linux-arm-gnueabihf.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.linux-arm-gnueabihf.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.linux-arm-gnueabihf.node') @@ -253,7 +271,9 @@ switch (platform) { break case 'riscv64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'argon2.linux-riscv64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.linux-riscv64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.linux-riscv64-musl.node') @@ -264,7 +284,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'argon2.linux-riscv64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'argon2.linux-riscv64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./argon2.linux-riscv64-gnu.node') @@ -286,8 +308,9 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = - existsSync(join(__dirname, 'argon2.wasm32-wasi.wasm')) && existsSync(join(__dirname, 'argon2.wasi.cjs')) + localFileExisted = existsSync( + join(__dirname, 'argon2.wasm32-wasi.wasm') + ) && existsSync(join(__dirname, 'argon2.wasi.cjs')) if (localFileExisted) { nativeBinding = require('./argon2.wasi.cjs') } else { diff --git a/packages/argon2/npm/android-arm-eabi/package.json b/packages/argon2/npm/android-arm-eabi/package.json index cbcaa0c0..82c595cb 100644 --- a/packages/argon2/npm/android-arm-eabi/package.json +++ b/packages/argon2/npm/android-arm-eabi/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-android-arm-eabi", "version": "1.5.2", - "os": [ - "android" - ], "cpu": [ "arm" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "android" + ] } diff --git a/packages/argon2/npm/android-arm64/package.json b/packages/argon2/npm/android-arm64/package.json index 57d5ba2e..5f2b8387 100644 --- a/packages/argon2/npm/android-arm64/package.json +++ b/packages/argon2/npm/android-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-android-arm64", "version": "1.5.2", - "os": [ - "android" - ], "cpu": [ "arm64" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "android" + ] } diff --git a/packages/argon2/npm/darwin-arm64/package.json b/packages/argon2/npm/darwin-arm64/package.json index 8d825ae1..c5c3adad 100644 --- a/packages/argon2/npm/darwin-arm64/package.json +++ b/packages/argon2/npm/darwin-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-darwin-arm64", "version": "1.5.2", - "os": [ - "darwin" - ], "cpu": [ "arm64" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/argon2/npm/darwin-x64/package.json b/packages/argon2/npm/darwin-x64/package.json index f2d56891..2bd88f6c 100644 --- a/packages/argon2/npm/darwin-x64/package.json +++ b/packages/argon2/npm/darwin-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-darwin-x64", "version": "1.5.2", - "os": [ - "darwin" - ], "cpu": [ "x64" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/argon2/npm/freebsd-x64/package.json b/packages/argon2/npm/freebsd-x64/package.json index 9dd046db..02842250 100644 --- a/packages/argon2/npm/freebsd-x64/package.json +++ b/packages/argon2/npm/freebsd-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-freebsd-x64", "version": "1.5.2", - "os": [ - "freebsd" - ], "cpu": [ "x64" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "freebsd" + ] } diff --git a/packages/argon2/npm/linux-arm-gnueabihf/package.json b/packages/argon2/npm/linux-arm-gnueabihf/package.json index 26134f3a..6260b38b 100644 --- a/packages/argon2/npm/linux-arm-gnueabihf/package.json +++ b/packages/argon2/npm/linux-arm-gnueabihf/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-linux-arm-gnueabihf", "version": "1.5.2", - "os": [ - "linux" - ], "cpu": [ "arm" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "linux" + ] } diff --git a/packages/argon2/npm/linux-arm64-gnu/package.json b/packages/argon2/npm/linux-arm64-gnu/package.json index 21b59fed..fa7208ea 100644 --- a/packages/argon2/npm/linux-arm64-gnu/package.json +++ b/packages/argon2/npm/linux-arm64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-linux-arm64-gnu", "version": "1.5.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -36,6 +33,9 @@ "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/argon2/npm/linux-arm64-musl/package.json b/packages/argon2/npm/linux-arm64-musl/package.json index b2445bc5..16597c4f 100644 --- a/packages/argon2/npm/linux-arm64-musl/package.json +++ b/packages/argon2/npm/linux-arm64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-linux-arm64-musl", "version": "1.5.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -36,6 +33,9 @@ "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/argon2/npm/linux-x64-gnu/package.json b/packages/argon2/npm/linux-x64-gnu/package.json index d5f749f4..ced38519 100644 --- a/packages/argon2/npm/linux-x64-gnu/package.json +++ b/packages/argon2/npm/linux-x64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-linux-x64-gnu", "version": "1.5.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -36,6 +33,9 @@ "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/argon2/npm/linux-x64-musl/package.json b/packages/argon2/npm/linux-x64-musl/package.json index 97a46679..05fc913e 100644 --- a/packages/argon2/npm/linux-x64-musl/package.json +++ b/packages/argon2/npm/linux-x64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-linux-x64-musl", "version": "1.5.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -36,6 +33,9 @@ "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/argon2/npm/wasm32-wasi/README.md b/packages/argon2/npm/wasm32-wasi/README.md new file mode 100644 index 00000000..1cd4015c --- /dev/null +++ b/packages/argon2/npm/wasm32-wasi/README.md @@ -0,0 +1,3 @@ +# `@node-rs/argon2-wasm32-wasi` + +This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/argon2` diff --git a/packages/argon2/npm/wasm32-wasi/package.json b/packages/argon2/npm/wasm32-wasi/package.json new file mode 100644 index 00000000..634caac4 --- /dev/null +++ b/packages/argon2/npm/wasm32-wasi/package.json @@ -0,0 +1,36 @@ +{ + "name": "@node-rs/argon2-wasm32-wasi", + "version": "1.5.2", + "cpu": [ + "wasm32" + ], + "main": "argon2.wasm32-wasi.wasm", + "files": [ + "argon2.wasm32-wasi.wasm" + ], + "description": "RustCrypto: Argon2 binding for Node.js", + "keywords": [ + "argon2", + "argon2-password", + "crypto", + "password", + "encryption", + "hashing", + "hash", + "secure", + "verify" + ], + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 10" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + } +} diff --git a/packages/argon2/npm/win32-arm64-msvc/package.json b/packages/argon2/npm/win32-arm64-msvc/package.json index d940188e..745ea0c8 100644 --- a/packages/argon2/npm/win32-arm64-msvc/package.json +++ b/packages/argon2/npm/win32-arm64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-win32-arm64-msvc", "version": "1.5.2", - "os": [ - "win32" - ], "cpu": [ "arm64" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/argon2/npm/win32-ia32-msvc/package.json b/packages/argon2/npm/win32-ia32-msvc/package.json index b2ada510..d37ed0d9 100644 --- a/packages/argon2/npm/win32-ia32-msvc/package.json +++ b/packages/argon2/npm/win32-ia32-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-win32-ia32-msvc", "version": "1.5.2", - "os": [ - "win32" - ], "cpu": [ "ia32" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/argon2/npm/win32-x64-msvc/package.json b/packages/argon2/npm/win32-x64-msvc/package.json index b93b7e4e..3f0ba476 100644 --- a/packages/argon2/npm/win32-x64-msvc/package.json +++ b/packages/argon2/npm/win32-x64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/argon2-win32-x64-msvc", "version": "1.5.2", - "os": [ - "win32" - ], "cpu": [ "x64" ], @@ -35,5 +32,8 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/argon2/package.json b/packages/argon2/package.json index 4abd2234..8d51cb12 100644 --- a/packages/argon2/package.json +++ b/packages/argon2/package.json @@ -31,7 +31,8 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi" + "armv7-linux-androideabi", + "wasm32-wasi-preview1-threads" ] }, "publishConfig": { diff --git a/packages/argon2/wasi-worker.mjs b/packages/argon2/wasi-worker.mjs new file mode 100644 index 00000000..d23c9501 --- /dev/null +++ b/packages/argon2/wasi-worker.mjs @@ -0,0 +1,51 @@ +import fs from "node:fs"; +import { createRequire } from "node:module"; +import { parentPort, Worker } from "node:worker_threads"; + +import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; +import { WASI } from "@tybys/wasm-util"; + +const require = createRequire(import.meta.url); + +if (parentPort) { + parentPort.on("message", (data) => { + globalThis.onmessage({ data }); + }); +} + +Object.assign(globalThis, { + self: globalThis, + require, + Worker, + importScripts: function (f) { + ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + }, + postMessage: function (msg) { + if (parentPort) { + parentPort.postMessage(msg); + } + }, +}); + +const handler = new MessageHandler({ + onLoad({ wasmModule, wasmMemory }) { + const wasi = new WASI({ fs }); + + return instantiateNapiModuleSync(wasmModule, { + childThread: true, + wasi, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: wasmMemory + }; + }, + }); + }, +}); + +globalThis.onmessage = function (e) { + handler.handle(e); +}; diff --git a/packages/bcrypt/bcrypt.wasi.cjs b/packages/bcrypt/bcrypt.wasi.cjs new file mode 100644 index 00000000..678828e6 --- /dev/null +++ b/packages/bcrypt/bcrypt.wasi.cjs @@ -0,0 +1,72 @@ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const __nodeFs= require('node:fs') +const __nodePath = require('node:path') +const { WASI: __nodeWASI } = require('node:wasi') +const { Worker } = require('node:worker_threads') + +const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') +const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') + +const __wasi = new __nodeWASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/' + } +}) + +const __emnapiContext = __emnapiGetDefaultContext() + +const __sharedMemory = new WebAssembly.Memory({ + initial: 1024, + maximum: 10240, + shared: true, +}) + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'bcrypt.wasm32-wasi.wasm')), { + context: __emnapiContext, + asyncWorkPoolSize: 4, + wasi: __wasi, + onCreateWorker() { + return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { + env: process.env, + execArgv: ['--experimental-wasi-unstable-preview1'], + }) + }, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: __sharedMemory, + } + return importObject + }, + beforeInit({ instance }) { + __napi_rs_initialize_modules(instance) + } +}) + +function __napi_rs_initialize_modules(__napiInstance) { + __napiInstance.exports['__napi_register__HashTask_impl_0']?.() + __napiInstance.exports['__napi_register__SaltTask_impl_1']?.() + __napiInstance.exports['__napi_register__VerifyTask_impl_2']?.() + __napiInstance.exports['__napi_register__DEFAULT_COST_3']?.() + __napiInstance.exports['__napi_register__gen_salt_sync_4']?.() + __napiInstance.exports['__napi_register__gen_salt_js_5']?.() + __napiInstance.exports['__napi_register__hash_sync_6']?.() + __napiInstance.exports['__napi_register__hash_7']?.() + __napiInstance.exports['__napi_register__verify_sync_8']?.() + __napiInstance.exports['__napi_register__verify_9']?.() +} +module.exports.DEFAULT_COST = __napiModule.exports.DEFAULT_COST, +module.exports.genSalt = __napiModule.exports.genSalt, +module.exports.genSaltSync = __napiModule.exports.genSaltSync, +module.exports.hash = __napiModule.exports.hash, +module.exports.hashSync = __napiModule.exports.hashSync, +module.exports.verify = __napiModule.exports.verify, +module.exports.verifySync = __napiModule.exports.verifySync diff --git a/packages/bcrypt/npm/android-arm-eabi/package.json b/packages/bcrypt/npm/android-arm-eabi/package.json index 04d5b986..89a9a657 100644 --- a/packages/bcrypt/npm/android-arm-eabi/package.json +++ b/packages/bcrypt/npm/android-arm-eabi/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-android-arm-eabi", "version": "1.7.3", - "os": [ - "android" - ], "cpu": [ "arm" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/bcrypt/npm/android-arm64/package.json b/packages/bcrypt/npm/android-arm64/package.json index f524e5a2..c263b180 100644 --- a/packages/bcrypt/npm/android-arm64/package.json +++ b/packages/bcrypt/npm/android-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-android-arm64", "version": "1.7.3", - "os": [ - "android" - ], "cpu": [ "arm64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/bcrypt/npm/darwin-arm64/package.json b/packages/bcrypt/npm/darwin-arm64/package.json index 79ecbf2c..f358ecc5 100644 --- a/packages/bcrypt/npm/darwin-arm64/package.json +++ b/packages/bcrypt/npm/darwin-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-darwin-arm64", "version": "1.7.3", - "os": [ - "darwin" - ], "cpu": [ "arm64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/bcrypt/npm/darwin-x64/package.json b/packages/bcrypt/npm/darwin-x64/package.json index 1689f6c4..bc99926b 100644 --- a/packages/bcrypt/npm/darwin-x64/package.json +++ b/packages/bcrypt/npm/darwin-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-darwin-x64", "version": "1.7.3", - "os": [ - "darwin" - ], "cpu": [ "x64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/bcrypt/npm/freebsd-x64/package.json b/packages/bcrypt/npm/freebsd-x64/package.json index fd53a442..ce3feba4 100644 --- a/packages/bcrypt/npm/freebsd-x64/package.json +++ b/packages/bcrypt/npm/freebsd-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-freebsd-x64", "version": "1.7.3", - "os": [ - "freebsd" - ], "cpu": [ "x64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "freebsd" + ] } diff --git a/packages/bcrypt/npm/linux-arm-gnueabihf/package.json b/packages/bcrypt/npm/linux-arm-gnueabihf/package.json index ebfcaba9..80f3a47c 100644 --- a/packages/bcrypt/npm/linux-arm-gnueabihf/package.json +++ b/packages/bcrypt/npm/linux-arm-gnueabihf/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-linux-arm-gnueabihf", "version": "1.7.3", - "os": [ - "linux" - ], "cpu": [ "arm" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "linux" + ] } diff --git a/packages/bcrypt/npm/linux-arm64-gnu/package.json b/packages/bcrypt/npm/linux-arm64-gnu/package.json index 523ca85b..ab6f6ac5 100644 --- a/packages/bcrypt/npm/linux-arm64-gnu/package.json +++ b/packages/bcrypt/npm/linux-arm64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-linux-arm64-gnu", "version": "1.7.3", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/bcrypt/npm/linux-arm64-musl/package.json b/packages/bcrypt/npm/linux-arm64-musl/package.json index ae18c80b..c0d7cae5 100644 --- a/packages/bcrypt/npm/linux-arm64-musl/package.json +++ b/packages/bcrypt/npm/linux-arm64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-linux-arm64-musl", "version": "1.7.3", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/bcrypt/npm/linux-x64-gnu/package.json b/packages/bcrypt/npm/linux-x64-gnu/package.json index 961e212b..4471dd41 100644 --- a/packages/bcrypt/npm/linux-x64-gnu/package.json +++ b/packages/bcrypt/npm/linux-x64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-linux-x64-gnu", "version": "1.7.3", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/bcrypt/npm/linux-x64-musl/package.json b/packages/bcrypt/npm/linux-x64-musl/package.json index 03a9b9fb..1cf1e471 100644 --- a/packages/bcrypt/npm/linux-x64-musl/package.json +++ b/packages/bcrypt/npm/linux-x64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-linux-x64-musl", "version": "1.7.3", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/bcrypt/npm/wasm32-wasi/README.md b/packages/bcrypt/npm/wasm32-wasi/README.md new file mode 100644 index 00000000..30fe5432 --- /dev/null +++ b/packages/bcrypt/npm/wasm32-wasi/README.md @@ -0,0 +1,3 @@ +# `@node-rs/bcrypt-wasm32-wasi` + +This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/bcrypt` diff --git a/packages/bcrypt/npm/wasm32-wasi/package.json b/packages/bcrypt/npm/wasm32-wasi/package.json new file mode 100644 index 00000000..98af27cd --- /dev/null +++ b/packages/bcrypt/npm/wasm32-wasi/package.json @@ -0,0 +1,40 @@ +{ + "name": "@node-rs/bcrypt-wasm32-wasi", + "version": "1.7.3", + "cpu": [ + "wasm32" + ], + "main": "bcrypt.wasm32-wasi.wasm", + "files": [ + "bcrypt.wasm32-wasi.wasm" + ], + "description": "Rust bcrypt binding", + "keywords": [ + "bcrypt", + "auth", + "password", + "authentication", + "encryption", + "crypto", + "N-API", + "napi-rs", + "node-rs" + ], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 10" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/bcrypt/npm/win32-arm64-msvc/package.json b/packages/bcrypt/npm/win32-arm64-msvc/package.json index a5e7cdba..abc111ba 100644 --- a/packages/bcrypt/npm/win32-arm64-msvc/package.json +++ b/packages/bcrypt/npm/win32-arm64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-win32-arm64-msvc", "version": "1.7.3", - "os": [ - "win32" - ], "cpu": [ "arm64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/bcrypt/npm/win32-ia32-msvc/package.json b/packages/bcrypt/npm/win32-ia32-msvc/package.json index 60a25f75..5cd7e097 100644 --- a/packages/bcrypt/npm/win32-ia32-msvc/package.json +++ b/packages/bcrypt/npm/win32-ia32-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-win32-ia32-msvc", "version": "1.7.3", - "os": [ - "win32" - ], "cpu": [ "ia32" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/bcrypt/npm/win32-x64-msvc/package.json b/packages/bcrypt/npm/win32-x64-msvc/package.json index 6592e3f9..0a21f93e 100644 --- a/packages/bcrypt/npm/win32-x64-msvc/package.json +++ b/packages/bcrypt/npm/win32-x64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/bcrypt-win32-x64-msvc", "version": "1.7.3", - "os": [ - "win32" - ], "cpu": [ "x64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/bcrypt/package.json b/packages/bcrypt/package.json index b1f2c489..0c5bdaf3 100644 --- a/packages/bcrypt/package.json +++ b/packages/bcrypt/package.json @@ -41,7 +41,8 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi" + "armv7-linux-androideabi", + "wasm32-wasi-preview1-threads" ] }, "engines": { diff --git a/packages/bcrypt/wasi-worker.mjs b/packages/bcrypt/wasi-worker.mjs new file mode 100644 index 00000000..d23c9501 --- /dev/null +++ b/packages/bcrypt/wasi-worker.mjs @@ -0,0 +1,51 @@ +import fs from "node:fs"; +import { createRequire } from "node:module"; +import { parentPort, Worker } from "node:worker_threads"; + +import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; +import { WASI } from "@tybys/wasm-util"; + +const require = createRequire(import.meta.url); + +if (parentPort) { + parentPort.on("message", (data) => { + globalThis.onmessage({ data }); + }); +} + +Object.assign(globalThis, { + self: globalThis, + require, + Worker, + importScripts: function (f) { + ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + }, + postMessage: function (msg) { + if (parentPort) { + parentPort.postMessage(msg); + } + }, +}); + +const handler = new MessageHandler({ + onLoad({ wasmModule, wasmMemory }) { + const wasi = new WASI({ fs }); + + return instantiateNapiModuleSync(wasmModule, { + childThread: true, + wasi, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: wasmMemory + }; + }, + }); + }, +}); + +globalThis.onmessage = function (e) { + handler.handle(e); +}; diff --git a/packages/crc32/Cargo.toml b/packages/crc32/Cargo.toml index 5b342f52..1c3e649d 100644 --- a/packages/crc32/Cargo.toml +++ b/packages/crc32/Cargo.toml @@ -12,7 +12,7 @@ crc32c = { version = "0.6" } crc32fast = { version = "1.3", features = ["nightly"] } global_alloc = { path = "../../crates/alloc" } napi = { version = "2", default-features = false, features = ["napi3"] } -napi-derive = { version = "2", default-features = false } +napi-derive = { version = "2" } [build-dependencies] napi-build = "2" diff --git a/packages/crc32/crc32.wasi.cjs b/packages/crc32/crc32.wasi.cjs new file mode 100644 index 00000000..28ce9d8e --- /dev/null +++ b/packages/crc32/crc32.wasi.cjs @@ -0,0 +1,59 @@ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const __nodeFs= require('node:fs') +const __nodePath = require('node:path') +const { WASI: __nodeWASI } = require('node:wasi') +const { Worker } = require('node:worker_threads') + +const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') +const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') + +const __wasi = new __nodeWASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/' + } +}) + +const __emnapiContext = __emnapiGetDefaultContext() + +const __sharedMemory = new WebAssembly.Memory({ + initial: 1024, + maximum: 10240, + shared: true, +}) + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'crc32.wasm32-wasi.wasm')), { + context: __emnapiContext, + asyncWorkPoolSize: 4, + wasi: __wasi, + onCreateWorker() { + return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { + env: process.env, + execArgv: ['--experimental-wasi-unstable-preview1'], + }) + }, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: __sharedMemory, + } + return importObject + }, + beforeInit({ instance }) { + __napi_rs_initialize_modules(instance) + } +}) + +function __napi_rs_initialize_modules(__napiInstance) { + __napiInstance.exports['__napi_register__crc32c_0']?.() + __napiInstance.exports['__napi_register__crc32_1']?.() +} +module.exports.crc32 = __napiModule.exports.crc32, +module.exports.crc32c = __napiModule.exports.crc32c diff --git a/packages/crc32/index.d.ts b/packages/crc32/index.d.ts index 45f2a945..090ada9e 100644 --- a/packages/crc32/index.d.ts +++ b/packages/crc32/index.d.ts @@ -1,2 +1,7 @@ -export function crc32c(input: string | Buffer, initialState?: number | undefined | null): number +/* auto-generated by NAPI-RS */ +/* eslint-disable */ + export function crc32(input: string | Buffer, initialState?: number | undefined | null): number + +export function crc32c(input: string | Buffer, initialState?: number | undefined | null): number + diff --git a/packages/crc32/index.js b/packages/crc32/index.js index 3948fef1..505a2901 100644 --- a/packages/crc32/index.js +++ b/packages/crc32/index.js @@ -1,3 +1,7 @@ +// prettier-ignore +/* eslint-disable */ +/* auto-generated by NAPI-RS */ + const { existsSync, readFileSync } = require('fs') const { join } = require('path') @@ -7,17 +11,52 @@ let nativeBinding = null let localFileExisted = false let loadError = null -function isMusl() { - // For Node 10 - if (!process.report || typeof process.report.getReport !== 'function') { - try { - return readFileSync('/usr/bin/ldd', 'utf8').includes('musl') - } catch (e) { +const isMusl = () => { + let musl = false + if (process.platform === 'linux') { + musl = isMuslFromFilesystem() + if (musl === null) { + musl = isMuslFromReport() + } + if (musl === null) { + musl = isMuslFromChildProcess() + } + } + return musl +} + +const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-') + +const isMuslFromFilesystem = () => { + try { + return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl') + } catch { + return null + } +} + +const isMuslFromReport = () => { + const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null + if (!report) { + return null + } + if (report.header && report.header.glibcVersionRuntime) { + return false + } + if (Array.isArray(report.sharedObjects)) { + if (report.sharedObjects.some(isFileMusl)) { return true } - } else { - const { glibcVersionRuntime } = process.report.getReport().header - return !glibcVersionRuntime + } + return false +} + +const isMuslFromChildProcess = () => { + try { + return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl') + } catch (e) { + // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false + return false } } @@ -55,7 +94,9 @@ switch (platform) { case 'win32': switch (arch) { case 'x64': - localFileExisted = existsSync(join(__dirname, 'crc32.win32-x64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.win32-x64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.win32-x64-msvc.node') @@ -67,7 +108,9 @@ switch (platform) { } break case 'ia32': - localFileExisted = existsSync(join(__dirname, 'crc32.win32-ia32-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.win32-ia32-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.win32-ia32-msvc.node') @@ -79,7 +122,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'crc32.win32-arm64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.win32-arm64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.win32-arm64-msvc.node') @@ -95,6 +140,15 @@ switch (platform) { } break case 'darwin': + localFileExisted = existsSync(join(__dirname, 'crc32.darwin-universal.node')) + try { + if (localFileExisted) { + nativeBinding = require('./crc32.darwin-universal.node') + } else { + nativeBinding = require('@node-rs/crc32-darwin-universal') + } + break + } catch {} switch (arch) { case 'x64': localFileExisted = existsSync(join(__dirname, 'crc32.darwin-x64.node')) @@ -109,7 +163,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'crc32.darwin-arm64.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.darwin-arm64.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.darwin-arm64.node') @@ -143,7 +199,9 @@ switch (platform) { switch (arch) { case 'x64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'crc32.linux-x64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.linux-x64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.linux-x64-musl.node') @@ -154,7 +212,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'crc32.linux-x64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.linux-x64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.linux-x64-gnu.node') @@ -168,7 +228,9 @@ switch (platform) { break case 'arm64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'crc32.linux-arm64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.linux-arm64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.linux-arm64-musl.node') @@ -179,7 +241,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'crc32.linux-arm64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.linux-arm64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.linux-arm64-gnu.node') @@ -192,7 +256,9 @@ switch (platform) { } break case 'arm': - localFileExisted = existsSync(join(__dirname, 'crc32.linux-arm-gnueabihf.node')) + localFileExisted = existsSync( + join(__dirname, 'crc32.linux-arm-gnueabihf.node') + ) try { if (localFileExisted) { nativeBinding = require('./crc32.linux-arm-gnueabihf.node') @@ -203,6 +269,35 @@ switch (platform) { loadError = e } break + case 'riscv64': + if (isMusl()) { + localFileExisted = existsSync( + join(__dirname, 'crc32.linux-riscv64-musl.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./crc32.linux-riscv64-musl.node') + } else { + nativeBinding = require('@node-rs/crc32-linux-riscv64-musl') + } + } catch (e) { + loadError = e + } + } else { + localFileExisted = existsSync( + join(__dirname, 'crc32.linux-riscv64-gnu.node') + ) + try { + if (localFileExisted) { + nativeBinding = require('./crc32.linux-riscv64-gnu.node') + } else { + nativeBinding = require('@node-rs/crc32-linux-riscv64-gnu') + } + } catch (e) { + loadError = e + } + } + break default: throw new Error(`Unsupported architecture on Linux: ${arch}`) } @@ -211,6 +306,21 @@ switch (platform) { throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`) } +if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { + try { + localFileExisted = existsSync( + join(__dirname, 'crc32.wasm32-wasi.wasm') + ) && existsSync(join(__dirname, 'crc32.wasi.cjs')) + if (localFileExisted) { + nativeBinding = require('./crc32.wasi.cjs') + } else { + nativeBinding = require('@node-rs/crc32-wasm32-wasi') + } + } catch { + // ignore + } +} + if (!nativeBinding) { if (loadError) { throw loadError @@ -218,7 +328,5 @@ if (!nativeBinding) { throw new Error(`Failed to load native binding`) } -const { crc32c, crc32 } = nativeBinding - -module.exports.crc32c = crc32c -module.exports.crc32 = crc32 +module.exports.crc32 = nativeBinding.crc32 +module.exports.crc32c = nativeBinding.crc32c diff --git a/packages/crc32/npm/android-arm-eabi/package.json b/packages/crc32/npm/android-arm-eabi/package.json index 6a3c254a..f27cd620 100644 --- a/packages/crc32/npm/android-arm-eabi/package.json +++ b/packages/crc32/npm/android-arm-eabi/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-android-arm-eabi", "version": "1.7.2", - "os": [ - "android" - ], "cpu": [ "arm" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/crc32/npm/android-arm64/package.json b/packages/crc32/npm/android-arm64/package.json index 6220e0a8..bd816dd5 100644 --- a/packages/crc32/npm/android-arm64/package.json +++ b/packages/crc32/npm/android-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-android-arm64", "version": "1.7.2", - "os": [ - "android" - ], "cpu": [ "arm64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/crc32/npm/darwin-arm64/package.json b/packages/crc32/npm/darwin-arm64/package.json index affb4cd0..845f07b5 100644 --- a/packages/crc32/npm/darwin-arm64/package.json +++ b/packages/crc32/npm/darwin-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-darwin-arm64", "version": "1.7.2", - "os": [ - "darwin" - ], "cpu": [ "arm64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/crc32/npm/darwin-x64/package.json b/packages/crc32/npm/darwin-x64/package.json index ce193e43..55323bf3 100644 --- a/packages/crc32/npm/darwin-x64/package.json +++ b/packages/crc32/npm/darwin-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-darwin-x64", "version": "1.7.2", - "os": [ - "darwin" - ], "cpu": [ "x64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/crc32/npm/freebsd-x64/package.json b/packages/crc32/npm/freebsd-x64/package.json index 6f4f5337..93300151 100644 --- a/packages/crc32/npm/freebsd-x64/package.json +++ b/packages/crc32/npm/freebsd-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-freebsd-x64", "version": "1.7.2", - "os": [ - "freebsd" - ], "cpu": [ "x64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "freebsd" + ] } diff --git a/packages/crc32/npm/linux-arm-gnueabihf/package.json b/packages/crc32/npm/linux-arm-gnueabihf/package.json index 7a6e12f2..d0faaa73 100644 --- a/packages/crc32/npm/linux-arm-gnueabihf/package.json +++ b/packages/crc32/npm/linux-arm-gnueabihf/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-linux-arm-gnueabihf", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "arm" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "linux" + ] } diff --git a/packages/crc32/npm/linux-arm64-gnu/package.json b/packages/crc32/npm/linux-arm64-gnu/package.json index fbdf8806..504f7e02 100644 --- a/packages/crc32/npm/linux-arm64-gnu/package.json +++ b/packages/crc32/npm/linux-arm64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-linux-arm64-gnu", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/crc32/npm/linux-arm64-musl/package.json b/packages/crc32/npm/linux-arm64-musl/package.json index 865a4c0d..a40c8ff0 100644 --- a/packages/crc32/npm/linux-arm64-musl/package.json +++ b/packages/crc32/npm/linux-arm64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-linux-arm64-musl", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/crc32/npm/linux-x64-gnu/package.json b/packages/crc32/npm/linux-x64-gnu/package.json index 3321941c..4df10a8b 100644 --- a/packages/crc32/npm/linux-x64-gnu/package.json +++ b/packages/crc32/npm/linux-x64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-linux-x64-gnu", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/crc32/npm/linux-x64-musl/package.json b/packages/crc32/npm/linux-x64-musl/package.json index 2f981223..82e0189b 100644 --- a/packages/crc32/npm/linux-x64-musl/package.json +++ b/packages/crc32/npm/linux-x64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-linux-x64-musl", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/crc32/npm/wasm32-wasi/README.md b/packages/crc32/npm/wasm32-wasi/README.md new file mode 100644 index 00000000..04a96872 --- /dev/null +++ b/packages/crc32/npm/wasm32-wasi/README.md @@ -0,0 +1,3 @@ +# `@node-rs/crc32-wasm32-wasi` + +This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/crc32` diff --git a/packages/crc32/npm/wasm32-wasi/package.json b/packages/crc32/npm/wasm32-wasi/package.json new file mode 100644 index 00000000..780567c0 --- /dev/null +++ b/packages/crc32/npm/wasm32-wasi/package.json @@ -0,0 +1,37 @@ +{ + "name": "@node-rs/crc32-wasm32-wasi", + "version": "1.7.2", + "cpu": [ + "wasm32" + ], + "main": "crc32.wasm32-wasi.wasm", + "files": [ + "crc32.wasm32-wasi.wasm" + ], + "description": "SIMD crc32", + "keywords": [ + "SIMD", + "NAPI", + "napi-rs", + "node-rs", + "crc32", + "crc32c" + ], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 10" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/crc32/npm/win32-arm64-msvc/package.json b/packages/crc32/npm/win32-arm64-msvc/package.json index e79d7a9a..a6177291 100644 --- a/packages/crc32/npm/win32-arm64-msvc/package.json +++ b/packages/crc32/npm/win32-arm64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-win32-arm64-msvc", "version": "1.7.2", - "os": [ - "win32" - ], "cpu": [ "arm64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/crc32/npm/win32-ia32-msvc/package.json b/packages/crc32/npm/win32-ia32-msvc/package.json index ae98c2ba..bb69df2f 100644 --- a/packages/crc32/npm/win32-ia32-msvc/package.json +++ b/packages/crc32/npm/win32-ia32-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-win32-ia32-msvc", "version": "1.7.2", - "os": [ - "win32" - ], "cpu": [ "ia32" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/crc32/npm/win32-x64-msvc/package.json b/packages/crc32/npm/win32-x64-msvc/package.json index 38533f77..effc10f1 100644 --- a/packages/crc32/npm/win32-x64-msvc/package.json +++ b/packages/crc32/npm/win32-x64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/crc32-win32-x64-msvc", "version": "1.7.2", - "os": [ - "win32" - ], "cpu": [ "x64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/crc32/package.json b/packages/crc32/package.json index db94129a..a9014ee2 100644 --- a/packages/crc32/package.json +++ b/packages/crc32/package.json @@ -35,7 +35,8 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi" + "armv7-linux-androideabi", + "wasm32-wasi-preview1-threads" ] }, "engines": { diff --git a/packages/crc32/wasi-worker.mjs b/packages/crc32/wasi-worker.mjs new file mode 100644 index 00000000..d23c9501 --- /dev/null +++ b/packages/crc32/wasi-worker.mjs @@ -0,0 +1,51 @@ +import fs from "node:fs"; +import { createRequire } from "node:module"; +import { parentPort, Worker } from "node:worker_threads"; + +import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; +import { WASI } from "@tybys/wasm-util"; + +const require = createRequire(import.meta.url); + +if (parentPort) { + parentPort.on("message", (data) => { + globalThis.onmessage({ data }); + }); +} + +Object.assign(globalThis, { + self: globalThis, + require, + Worker, + importScripts: function (f) { + ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + }, + postMessage: function (msg) { + if (parentPort) { + parentPort.postMessage(msg); + } + }, +}); + +const handler = new MessageHandler({ + onLoad({ wasmModule, wasmMemory }) { + const wasi = new WASI({ fs }); + + return instantiateNapiModuleSync(wasmModule, { + childThread: true, + wasi, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: wasmMemory + }; + }, + }); + }, +}); + +globalThis.onmessage = function (e) { + handler.handle(e); +}; diff --git a/packages/deno-lint/deno-lint.wasi.cjs b/packages/deno-lint/deno-lint.wasi.cjs new file mode 100644 index 00000000..b93d6d36 --- /dev/null +++ b/packages/deno-lint/deno-lint.wasi.cjs @@ -0,0 +1,59 @@ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const __nodeFs= require('node:fs') +const __nodePath = require('node:path') +const { WASI: __nodeWASI } = require('node:wasi') +const { Worker } = require('node:worker_threads') + +const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') +const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') + +const __wasi = new __nodeWASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/' + } +}) + +const __emnapiContext = __emnapiGetDefaultContext() + +const __sharedMemory = new WebAssembly.Memory({ + initial: 1024, + maximum: 10240, + shared: true, +}) + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'deno-lint.wasm32-wasi.wasm')), { + context: __emnapiContext, + asyncWorkPoolSize: 4, + wasi: __wasi, + onCreateWorker() { + return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { + env: process.env, + execArgv: ['--experimental-wasi-unstable-preview1'], + }) + }, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: __sharedMemory, + } + return importObject + }, + beforeInit({ instance }) { + __napi_rs_initialize_modules(instance) + } +}) + +function __napi_rs_initialize_modules(__napiInstance) { + __napiInstance.exports['__napi_register__lint_0']?.() + __napiInstance.exports['__napi_register__denolint_1']?.() +} +module.exports.denolint = __napiModule.exports.denolint, +module.exports.lint = __napiModule.exports.lint diff --git a/packages/deno-lint/index.d.ts b/packages/deno-lint/index.d.ts index cefecd30..c0cf8407 100644 --- a/packages/deno-lint/index.d.ts +++ b/packages/deno-lint/index.d.ts @@ -3,8 +3,5 @@ export function denolint(dirname: string, configPath: string): boolean -export function lint( - fileName: string, - sourceCode: string | Buffer, - allRules?: boolean | string | undefined | null, -): Array +export function lint(fileName: string, sourceCode: string | Buffer, allRules?: boolean | string | undefined | null): Array + diff --git a/packages/deno-lint/index.js b/packages/deno-lint/index.js index 5f8aebcb..cfd4d3fe 100644 --- a/packages/deno-lint/index.js +++ b/packages/deno-lint/index.js @@ -94,7 +94,9 @@ switch (platform) { case 'win32': switch (arch) { case 'x64': - localFileExisted = existsSync(join(__dirname, 'deno-lint.win32-x64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.win32-x64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.win32-x64-msvc.node') @@ -106,7 +108,9 @@ switch (platform) { } break case 'ia32': - localFileExisted = existsSync(join(__dirname, 'deno-lint.win32-ia32-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.win32-ia32-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.win32-ia32-msvc.node') @@ -118,7 +122,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'deno-lint.win32-arm64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.win32-arm64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.win32-arm64-msvc.node') @@ -157,7 +163,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'deno-lint.darwin-arm64.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.darwin-arm64.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.darwin-arm64.node') @@ -191,7 +199,9 @@ switch (platform) { switch (arch) { case 'x64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'deno-lint.linux-x64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.linux-x64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.linux-x64-musl.node') @@ -202,7 +212,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'deno-lint.linux-x64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.linux-x64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.linux-x64-gnu.node') @@ -216,7 +228,9 @@ switch (platform) { break case 'arm64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'deno-lint.linux-arm64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.linux-arm64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.linux-arm64-musl.node') @@ -227,7 +241,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'deno-lint.linux-arm64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.linux-arm64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.linux-arm64-gnu.node') @@ -240,7 +256,9 @@ switch (platform) { } break case 'arm': - localFileExisted = existsSync(join(__dirname, 'deno-lint.linux-arm-gnueabihf.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.linux-arm-gnueabihf.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.linux-arm-gnueabihf.node') @@ -253,7 +271,9 @@ switch (platform) { break case 'riscv64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'deno-lint.linux-riscv64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.linux-riscv64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.linux-riscv64-musl.node') @@ -264,7 +284,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'deno-lint.linux-riscv64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.linux-riscv64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./deno-lint.linux-riscv64-gnu.node') @@ -286,8 +308,9 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = - existsSync(join(__dirname, 'deno-lint.wasm32-wasi.wasm')) && existsSync(join(__dirname, 'deno-lint.wasi.cjs')) + localFileExisted = existsSync( + join(__dirname, 'deno-lint.wasm32-wasi.wasm') + ) && existsSync(join(__dirname, 'deno-lint.wasi.cjs')) if (localFileExisted) { nativeBinding = require('./deno-lint.wasi.cjs') } else { diff --git a/packages/deno-lint/npm/android-arm-eabi/package.json b/packages/deno-lint/npm/android-arm-eabi/package.json index 4039c510..33522740 100644 --- a/packages/deno-lint/npm/android-arm-eabi/package.json +++ b/packages/deno-lint/npm/android-arm-eabi/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-android-arm-eabi", "version": "1.17.3", - "os": [ - "android" - ], "cpu": [ "arm" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/deno-lint/npm/android-arm64/package.json b/packages/deno-lint/npm/android-arm64/package.json index 198d3532..6109a90b 100644 --- a/packages/deno-lint/npm/android-arm64/package.json +++ b/packages/deno-lint/npm/android-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-android-arm64", "version": "1.17.3", - "os": [ - "android" - ], "cpu": [ "arm64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/deno-lint/npm/darwin-arm64/package.json b/packages/deno-lint/npm/darwin-arm64/package.json index 9f092397..279dfb52 100644 --- a/packages/deno-lint/npm/darwin-arm64/package.json +++ b/packages/deno-lint/npm/darwin-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-darwin-arm64", "version": "1.17.3", - "os": [ - "darwin" - ], "cpu": [ "arm64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/deno-lint/npm/darwin-x64/package.json b/packages/deno-lint/npm/darwin-x64/package.json index a7b8037c..9a507057 100644 --- a/packages/deno-lint/npm/darwin-x64/package.json +++ b/packages/deno-lint/npm/darwin-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-darwin-x64", "version": "1.17.3", - "os": [ - "darwin" - ], "cpu": [ "x64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/deno-lint/npm/freebsd-x64/package.json b/packages/deno-lint/npm/freebsd-x64/package.json index 5db5bc00..7d645737 100644 --- a/packages/deno-lint/npm/freebsd-x64/package.json +++ b/packages/deno-lint/npm/freebsd-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-freebsd-x64", "version": "1.17.3", - "os": [ - "freebsd" - ], "cpu": [ "x64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "freebsd" + ] } diff --git a/packages/deno-lint/npm/linux-arm-gnueabihf/package.json b/packages/deno-lint/npm/linux-arm-gnueabihf/package.json index 427cbdcc..1b883bfb 100644 --- a/packages/deno-lint/npm/linux-arm-gnueabihf/package.json +++ b/packages/deno-lint/npm/linux-arm-gnueabihf/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-linux-arm-gnueabihf", "version": "1.17.3", - "os": [ - "linux" - ], "cpu": [ "arm" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "linux" + ] } diff --git a/packages/deno-lint/npm/linux-arm64-gnu/package.json b/packages/deno-lint/npm/linux-arm64-gnu/package.json index 911590bd..3fbbb5a2 100644 --- a/packages/deno-lint/npm/linux-arm64-gnu/package.json +++ b/packages/deno-lint/npm/linux-arm64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-linux-arm64-gnu", "version": "1.17.3", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/deno-lint/npm/linux-arm64-musl/package.json b/packages/deno-lint/npm/linux-arm64-musl/package.json index 160bff05..05e16c54 100644 --- a/packages/deno-lint/npm/linux-arm64-musl/package.json +++ b/packages/deno-lint/npm/linux-arm64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-linux-arm64-musl", "version": "1.17.3", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/deno-lint/npm/linux-x64-gnu/package.json b/packages/deno-lint/npm/linux-x64-gnu/package.json index 9e86baad..1582a10a 100644 --- a/packages/deno-lint/npm/linux-x64-gnu/package.json +++ b/packages/deno-lint/npm/linux-x64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-linux-x64-gnu", "version": "1.17.3", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/deno-lint/npm/linux-x64-musl/package.json b/packages/deno-lint/npm/linux-x64-musl/package.json index 04cac777..8c4e8875 100644 --- a/packages/deno-lint/npm/linux-x64-musl/package.json +++ b/packages/deno-lint/npm/linux-x64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-linux-x64-musl", "version": "1.17.3", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/deno-lint/npm/wasm32-wasi/README.md b/packages/deno-lint/npm/wasm32-wasi/README.md new file mode 100644 index 00000000..6c36d268 --- /dev/null +++ b/packages/deno-lint/npm/wasm32-wasi/README.md @@ -0,0 +1,3 @@ +# `@node-rs/deno-lint-wasm32-wasi` + +This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/deno-lint` diff --git a/packages/deno-lint/npm/wasm32-wasi/package.json b/packages/deno-lint/npm/wasm32-wasi/package.json new file mode 100644 index 00000000..93528aa9 --- /dev/null +++ b/packages/deno-lint/npm/wasm32-wasi/package.json @@ -0,0 +1,39 @@ +{ + "name": "@node-rs/deno-lint-wasm32-wasi", + "version": "1.17.3", + "cpu": [ + "wasm32" + ], + "main": "deno-lint.wasm32-wasi.wasm", + "files": [ + "deno-lint.wasm32-wasi.wasm" + ], + "description": "Deno lint binding for Node.js", + "keywords": [ + "Deno", + "Lint", + "ESLint", + "node-rs", + "napi", + "N-API", + "Rust", + "napi-rs" + ], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 10" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/deno-lint/npm/win32-arm64-msvc/package.json b/packages/deno-lint/npm/win32-arm64-msvc/package.json index fd1c75a2..76cd8b9c 100644 --- a/packages/deno-lint/npm/win32-arm64-msvc/package.json +++ b/packages/deno-lint/npm/win32-arm64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-win32-arm64-msvc", "version": "1.17.3", - "os": [ - "win32" - ], "cpu": [ "arm64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/deno-lint/npm/win32-ia32-msvc/package.json b/packages/deno-lint/npm/win32-ia32-msvc/package.json index eb9e1a0b..c5d6b53b 100644 --- a/packages/deno-lint/npm/win32-ia32-msvc/package.json +++ b/packages/deno-lint/npm/win32-ia32-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-win32-ia32-msvc", "version": "1.17.3", - "os": [ - "win32" - ], "cpu": [ "ia32" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/deno-lint/npm/win32-x64-msvc/package.json b/packages/deno-lint/npm/win32-x64-msvc/package.json index 619c8708..e4212afd 100644 --- a/packages/deno-lint/npm/win32-x64-msvc/package.json +++ b/packages/deno-lint/npm/win32-x64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/deno-lint-win32-x64-msvc", "version": "1.17.3", - "os": [ - "win32" - ], "cpu": [ "x64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/deno-lint/package.json b/packages/deno-lint/package.json index 136bcbd7..0416a574 100644 --- a/packages/deno-lint/package.json +++ b/packages/deno-lint/package.json @@ -43,7 +43,8 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi" + "armv7-linux-androideabi", + "wasm32-wasi-preview1-threads" ] }, "engines": { diff --git a/packages/deno-lint/wasi-worker.mjs b/packages/deno-lint/wasi-worker.mjs new file mode 100644 index 00000000..d23c9501 --- /dev/null +++ b/packages/deno-lint/wasi-worker.mjs @@ -0,0 +1,51 @@ +import fs from "node:fs"; +import { createRequire } from "node:module"; +import { parentPort, Worker } from "node:worker_threads"; + +import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; +import { WASI } from "@tybys/wasm-util"; + +const require = createRequire(import.meta.url); + +if (parentPort) { + parentPort.on("message", (data) => { + globalThis.onmessage({ data }); + }); +} + +Object.assign(globalThis, { + self: globalThis, + require, + Worker, + importScripts: function (f) { + ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + }, + postMessage: function (msg) { + if (parentPort) { + parentPort.postMessage(msg); + } + }, +}); + +const handler = new MessageHandler({ + onLoad({ wasmModule, wasmMemory }) { + const wasi = new WASI({ fs }); + + return instantiateNapiModuleSync(wasmModule, { + childThread: true, + wasi, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: wasmMemory + }; + }, + }); + }, +}); + +globalThis.onmessage = function (e) { + handler.handle(e); +}; diff --git a/packages/jieba/index.d.ts b/packages/jieba/index.d.ts index 7da0c283..5a279744 100644 --- a/packages/jieba/index.d.ts +++ b/packages/jieba/index.d.ts @@ -26,3 +26,4 @@ export interface TaggedWord { tag: string word: string } + diff --git a/packages/jieba/index.js b/packages/jieba/index.js index b6f91a75..a93c3aef 100644 --- a/packages/jieba/index.js +++ b/packages/jieba/index.js @@ -94,7 +94,9 @@ switch (platform) { case 'win32': switch (arch) { case 'x64': - localFileExisted = existsSync(join(__dirname, 'jieba.win32-x64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.win32-x64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.win32-x64-msvc.node') @@ -106,7 +108,9 @@ switch (platform) { } break case 'ia32': - localFileExisted = existsSync(join(__dirname, 'jieba.win32-ia32-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.win32-ia32-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.win32-ia32-msvc.node') @@ -118,7 +122,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'jieba.win32-arm64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.win32-arm64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.win32-arm64-msvc.node') @@ -157,7 +163,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'jieba.darwin-arm64.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.darwin-arm64.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.darwin-arm64.node') @@ -191,7 +199,9 @@ switch (platform) { switch (arch) { case 'x64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'jieba.linux-x64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.linux-x64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.linux-x64-musl.node') @@ -202,7 +212,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'jieba.linux-x64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.linux-x64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.linux-x64-gnu.node') @@ -216,7 +228,9 @@ switch (platform) { break case 'arm64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'jieba.linux-arm64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.linux-arm64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.linux-arm64-musl.node') @@ -227,7 +241,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'jieba.linux-arm64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.linux-arm64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.linux-arm64-gnu.node') @@ -240,7 +256,9 @@ switch (platform) { } break case 'arm': - localFileExisted = existsSync(join(__dirname, 'jieba.linux-arm-gnueabihf.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.linux-arm-gnueabihf.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.linux-arm-gnueabihf.node') @@ -253,7 +271,9 @@ switch (platform) { break case 'riscv64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'jieba.linux-riscv64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.linux-riscv64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.linux-riscv64-musl.node') @@ -264,7 +284,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'jieba.linux-riscv64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'jieba.linux-riscv64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./jieba.linux-riscv64-gnu.node') @@ -286,8 +308,9 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = - existsSync(join(__dirname, 'jieba.wasm32-wasi.wasm')) && existsSync(join(__dirname, 'jieba.wasi.cjs')) + localFileExisted = existsSync( + join(__dirname, 'jieba.wasm32-wasi.wasm') + ) && existsSync(join(__dirname, 'jieba.wasi.cjs')) if (localFileExisted) { nativeBinding = require('./jieba.wasi.cjs') } else { diff --git a/packages/jieba/jieba.wasi.cjs b/packages/jieba/jieba.wasi.cjs new file mode 100644 index 00000000..e585c42c --- /dev/null +++ b/packages/jieba/jieba.wasi.cjs @@ -0,0 +1,73 @@ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const __nodeFs= require('node:fs') +const __nodePath = require('node:path') +const { WASI: __nodeWASI } = require('node:wasi') +const { Worker } = require('node:worker_threads') + +const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') +const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') + +const __wasi = new __nodeWASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/' + } +}) + +const __emnapiContext = __emnapiGetDefaultContext() + +const __sharedMemory = new WebAssembly.Memory({ + initial: 1024, + maximum: 10240, + shared: true, +}) + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'jieba.wasm32-wasi.wasm')), { + context: __emnapiContext, + asyncWorkPoolSize: 4, + wasi: __wasi, + onCreateWorker() { + return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { + env: process.env, + execArgv: ['--experimental-wasi-unstable-preview1'], + }) + }, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: __sharedMemory, + } + return importObject + }, + beforeInit({ instance }) { + __napi_rs_initialize_modules(instance) + } +}) + +function __napi_rs_initialize_modules(__napiInstance) { + __napiInstance.exports['__napi_register__load_0']?.() + __napiInstance.exports['__napi_register__load_dict_1']?.() + __napiInstance.exports['__napi_register__cut_2']?.() + __napiInstance.exports['__napi_register__cut_all_3']?.() + __napiInstance.exports['__napi_register__cut_for_search_4']?.() + __napiInstance.exports['__napi_register__TaggedWord_struct_5']?.() + __napiInstance.exports['__napi_register__tag_6']?.() + __napiInstance.exports['__napi_register__Keyword_struct_7']?.() + __napiInstance.exports['__napi_register__extract_8']?.() + __napiInstance.exports['__napi_register__load_tfidf_dict_9']?.() +} +module.exports.cut = __napiModule.exports.cut, +module.exports.cutAll = __napiModule.exports.cutAll, +module.exports.cutForSearch = __napiModule.exports.cutForSearch, +module.exports.extract = __napiModule.exports.extract, +module.exports.load = __napiModule.exports.load, +module.exports.loadDict = __napiModule.exports.loadDict, +module.exports.loadTFIDFDict = __napiModule.exports.loadTFIDFDict, +module.exports.tag = __napiModule.exports.tag diff --git a/packages/jieba/npm/android-arm-eabi/package.json b/packages/jieba/npm/android-arm-eabi/package.json index e156b040..81606e09 100644 --- a/packages/jieba/npm/android-arm-eabi/package.json +++ b/packages/jieba/npm/android-arm-eabi/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-android-arm-eabi", "version": "1.7.2", - "os": [ - "android" - ], "cpu": [ "arm" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/jieba/npm/android-arm64/package.json b/packages/jieba/npm/android-arm64/package.json index ecd338c8..cbbdf2d7 100644 --- a/packages/jieba/npm/android-arm64/package.json +++ b/packages/jieba/npm/android-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-android-arm64", "version": "1.7.2", - "os": [ - "android" - ], "cpu": [ "arm64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/jieba/npm/darwin-arm64/package.json b/packages/jieba/npm/darwin-arm64/package.json index fe564a22..76cc97ab 100644 --- a/packages/jieba/npm/darwin-arm64/package.json +++ b/packages/jieba/npm/darwin-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-darwin-arm64", "version": "1.7.2", - "os": [ - "darwin" - ], "cpu": [ "arm64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/jieba/npm/darwin-x64/package.json b/packages/jieba/npm/darwin-x64/package.json index ee3a3ed1..2504cf7e 100644 --- a/packages/jieba/npm/darwin-x64/package.json +++ b/packages/jieba/npm/darwin-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-darwin-x64", "version": "1.7.2", - "os": [ - "darwin" - ], "cpu": [ "x64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/jieba/npm/freebsd-x64/package.json b/packages/jieba/npm/freebsd-x64/package.json index 3524dffe..9422c448 100644 --- a/packages/jieba/npm/freebsd-x64/package.json +++ b/packages/jieba/npm/freebsd-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-freebsd-x64", "version": "1.7.2", - "os": [ - "freebsd" - ], "cpu": [ "x64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "freebsd" + ] } diff --git a/packages/jieba/npm/linux-arm-gnueabihf/package.json b/packages/jieba/npm/linux-arm-gnueabihf/package.json index a8c15455..2422833b 100644 --- a/packages/jieba/npm/linux-arm-gnueabihf/package.json +++ b/packages/jieba/npm/linux-arm-gnueabihf/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-linux-arm-gnueabihf", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "arm" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "linux" + ] } diff --git a/packages/jieba/npm/linux-arm64-gnu/package.json b/packages/jieba/npm/linux-arm64-gnu/package.json index 9120a956..ef543cee 100644 --- a/packages/jieba/npm/linux-arm64-gnu/package.json +++ b/packages/jieba/npm/linux-arm64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-linux-arm64-gnu", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/jieba/npm/linux-arm64-musl/package.json b/packages/jieba/npm/linux-arm64-musl/package.json index a88d03ee..4bfda7f5 100644 --- a/packages/jieba/npm/linux-arm64-musl/package.json +++ b/packages/jieba/npm/linux-arm64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-linux-arm64-musl", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/jieba/npm/linux-x64-gnu/package.json b/packages/jieba/npm/linux-x64-gnu/package.json index 7d7a2667..3a561065 100644 --- a/packages/jieba/npm/linux-x64-gnu/package.json +++ b/packages/jieba/npm/linux-x64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-linux-x64-gnu", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/jieba/npm/linux-x64-musl/package.json b/packages/jieba/npm/linux-x64-musl/package.json index 0d32d6af..196ab068 100644 --- a/packages/jieba/npm/linux-x64-musl/package.json +++ b/packages/jieba/npm/linux-x64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-linux-x64-musl", "version": "1.7.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -37,6 +34,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/jieba/npm/wasm32-wasi/README.md b/packages/jieba/npm/wasm32-wasi/README.md new file mode 100644 index 00000000..d04a0e77 --- /dev/null +++ b/packages/jieba/npm/wasm32-wasi/README.md @@ -0,0 +1,3 @@ +# `@node-rs/jieba-wasm32-wasi` + +This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/jieba` diff --git a/packages/jieba/npm/wasm32-wasi/package.json b/packages/jieba/npm/wasm32-wasi/package.json new file mode 100644 index 00000000..f4e3f683 --- /dev/null +++ b/packages/jieba/npm/wasm32-wasi/package.json @@ -0,0 +1,37 @@ +{ + "name": "@node-rs/jieba-wasm32-wasi", + "version": "1.7.2", + "cpu": [ + "wasm32" + ], + "main": "jieba.wasm32-wasi.wasm", + "files": [ + "jieba.wasm32-wasi.wasm" + ], + "description": "Fastest Chinese word segmentation in Node.js", + "keywords": [ + "Jieba", + "Rust", + "node-rs", + "napi", + "napi-rs", + "N-API" + ], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 10" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/jieba/npm/win32-arm64-msvc/package.json b/packages/jieba/npm/win32-arm64-msvc/package.json index 82b53d8f..3bacb66c 100644 --- a/packages/jieba/npm/win32-arm64-msvc/package.json +++ b/packages/jieba/npm/win32-arm64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-win32-arm64-msvc", "version": "1.7.2", - "os": [ - "win32" - ], "cpu": [ "arm64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/jieba/npm/win32-ia32-msvc/package.json b/packages/jieba/npm/win32-ia32-msvc/package.json index 42ce294b..789487f6 100644 --- a/packages/jieba/npm/win32-ia32-msvc/package.json +++ b/packages/jieba/npm/win32-ia32-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-win32-ia32-msvc", "version": "1.7.2", - "os": [ - "win32" - ], "cpu": [ "ia32" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/jieba/npm/win32-x64-msvc/package.json b/packages/jieba/npm/win32-x64-msvc/package.json index 0a2fd545..81cbe8cf 100644 --- a/packages/jieba/npm/win32-x64-msvc/package.json +++ b/packages/jieba/npm/win32-x64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jieba-win32-x64-msvc", "version": "1.7.2", - "os": [ - "win32" - ], "cpu": [ "x64" ], @@ -36,5 +33,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/jieba/package.json b/packages/jieba/package.json index 4a5207f8..1d744b45 100644 --- a/packages/jieba/package.json +++ b/packages/jieba/package.json @@ -35,7 +35,8 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi" + "armv7-linux-androideabi", + "wasm32-wasi-preview1-threads" ] }, "engines": { diff --git a/packages/jieba/wasi-worker.mjs b/packages/jieba/wasi-worker.mjs new file mode 100644 index 00000000..d23c9501 --- /dev/null +++ b/packages/jieba/wasi-worker.mjs @@ -0,0 +1,51 @@ +import fs from "node:fs"; +import { createRequire } from "node:module"; +import { parentPort, Worker } from "node:worker_threads"; + +import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; +import { WASI } from "@tybys/wasm-util"; + +const require = createRequire(import.meta.url); + +if (parentPort) { + parentPort.on("message", (data) => { + globalThis.onmessage({ data }); + }); +} + +Object.assign(globalThis, { + self: globalThis, + require, + Worker, + importScripts: function (f) { + ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + }, + postMessage: function (msg) { + if (parentPort) { + parentPort.postMessage(msg); + } + }, +}); + +const handler = new MessageHandler({ + onLoad({ wasmModule, wasmMemory }) { + const wasi = new WASI({ fs }); + + return instantiateNapiModuleSync(wasmModule, { + childThread: true, + wasi, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: wasmMemory + }; + }, + }); + }, +}); + +globalThis.onmessage = function (e) { + handler.handle(e); +}; diff --git a/packages/jsonwebtoken/index.d.ts b/packages/jsonwebtoken/index.d.ts index 7d0253e7..b8c39ae5 100644 --- a/packages/jsonwebtoken/index.d.ts +++ b/packages/jsonwebtoken/index.d.ts @@ -25,7 +25,7 @@ export const enum Algorithm { /** RSASSA-PSS using SHA-512 */ PS512 = 10, /** Edwards-curve Digital Signature Algorithm (EdDSA) */ - EdDSA = 11, + EdDSA = 11 } export interface Claims { @@ -96,12 +96,7 @@ export interface Header { x5TS256CertThumbprint?: string } -export function sign( - claims: Claims, - key: string | Buffer, - header?: Header | undefined | null, - abortSignal?: AbortSignal | undefined | null, -): Promise +export function sign(claims: Claims, key: string | Buffer, header?: Header | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise export function signSync(claims: Claims, key: string | Buffer, header?: Header | undefined | null): string @@ -172,11 +167,7 @@ export interface Validation { validateSignature?: boolean } -export function verify( - token: string, - key: string | Buffer, - validation?: Validation | undefined | null, - abortSignal?: AbortSignal | undefined | null, -): Promise +export function verify(token: string, key: string | Buffer, validation?: Validation | undefined | null, abortSignal?: AbortSignal | undefined | null): Promise export function verifySync(token: string, key: string | Buffer, validation?: Validation | undefined | null): Claims + diff --git a/packages/jsonwebtoken/index.js b/packages/jsonwebtoken/index.js index 5eebedf5..da551f8e 100644 --- a/packages/jsonwebtoken/index.js +++ b/packages/jsonwebtoken/index.js @@ -94,7 +94,9 @@ switch (platform) { case 'win32': switch (arch) { case 'x64': - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.win32-x64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.win32-x64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.win32-x64-msvc.node') @@ -106,7 +108,9 @@ switch (platform) { } break case 'ia32': - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.win32-ia32-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.win32-ia32-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.win32-ia32-msvc.node') @@ -118,7 +122,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.win32-arm64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.win32-arm64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.win32-arm64-msvc.node') @@ -157,7 +163,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.darwin-arm64.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.darwin-arm64.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.darwin-arm64.node') @@ -191,7 +199,9 @@ switch (platform) { switch (arch) { case 'x64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.linux-x64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.linux-x64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.linux-x64-musl.node') @@ -202,7 +212,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.linux-x64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.linux-x64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.linux-x64-gnu.node') @@ -216,7 +228,9 @@ switch (platform) { break case 'arm64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.linux-arm64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.linux-arm64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.linux-arm64-musl.node') @@ -227,7 +241,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.linux-arm64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.linux-arm64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.linux-arm64-gnu.node') @@ -240,7 +256,9 @@ switch (platform) { } break case 'arm': - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.linux-arm-gnueabihf.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.linux-arm-gnueabihf.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.linux-arm-gnueabihf.node') @@ -253,7 +271,9 @@ switch (platform) { break case 'riscv64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.linux-riscv64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.linux-riscv64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.linux-riscv64-musl.node') @@ -264,7 +284,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'jsonwebtoken.linux-riscv64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.linux-riscv64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./jsonwebtoken.linux-riscv64-gnu.node') @@ -286,9 +308,9 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = - existsSync(join(__dirname, 'jsonwebtoken.wasm32-wasi.wasm')) && - existsSync(join(__dirname, 'jsonwebtoken.wasi.cjs')) + localFileExisted = existsSync( + join(__dirname, 'jsonwebtoken.wasm32-wasi.wasm') + ) && existsSync(join(__dirname, 'jsonwebtoken.wasi.cjs')) if (localFileExisted) { nativeBinding = require('./jsonwebtoken.wasi.cjs') } else { diff --git a/packages/jsonwebtoken/jsonwebtoken.wasi.cjs b/packages/jsonwebtoken/jsonwebtoken.wasi.cjs new file mode 100644 index 00000000..64b09b2f --- /dev/null +++ b/packages/jsonwebtoken/jsonwebtoken.wasi.cjs @@ -0,0 +1,70 @@ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const __nodeFs= require('node:fs') +const __nodePath = require('node:path') +const { WASI: __nodeWASI } = require('node:wasi') +const { Worker } = require('node:worker_threads') + +const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') +const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') + +const __wasi = new __nodeWASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/' + } +}) + +const __emnapiContext = __emnapiGetDefaultContext() + +const __sharedMemory = new WebAssembly.Memory({ + initial: 1024, + maximum: 10240, + shared: true, +}) + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'jsonwebtoken.wasm32-wasi.wasm')), { + context: __emnapiContext, + asyncWorkPoolSize: 4, + wasi: __wasi, + onCreateWorker() { + return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { + env: process.env, + execArgv: ['--experimental-wasi-unstable-preview1'], + }) + }, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: __sharedMemory, + } + return importObject + }, + beforeInit({ instance }) { + __napi_rs_initialize_modules(instance) + } +}) + +function __napi_rs_initialize_modules(__napiInstance) { + __napiInstance.exports['__napi_register__Algorithm_0']?.() + __napiInstance.exports['__napi_register__Claims_struct_1']?.() + __napiInstance.exports['__napi_register__Header_struct_2']?.() + __napiInstance.exports['__napi_register__SignTask_impl_3']?.() + __napiInstance.exports['__napi_register__sign_4']?.() + __napiInstance.exports['__napi_register__sign_sync_5']?.() + __napiInstance.exports['__napi_register__Validation_struct_6']?.() + __napiInstance.exports['__napi_register__VerifyTask_impl_7']?.() + __napiInstance.exports['__napi_register__verify_8']?.() + __napiInstance.exports['__napi_register__verify_sync_9']?.() +} +module.exports.Algorithm = __napiModule.exports.Algorithm, +module.exports.sign = __napiModule.exports.sign, +module.exports.signSync = __napiModule.exports.signSync, +module.exports.verify = __napiModule.exports.verify, +module.exports.verifySync = __napiModule.exports.verifySync diff --git a/packages/jsonwebtoken/npm/android-arm-eabi/package.json b/packages/jsonwebtoken/npm/android-arm-eabi/package.json index d2ca7a06..7d67ffad 100644 --- a/packages/jsonwebtoken/npm/android-arm-eabi/package.json +++ b/packages/jsonwebtoken/npm/android-arm-eabi/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-android-arm-eabi", "version": "0.2.3", - "os": [ - "android" - ], "cpu": [ "arm" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/jsonwebtoken/npm/android-arm64/package.json b/packages/jsonwebtoken/npm/android-arm64/package.json index 55f428ee..1259541d 100644 --- a/packages/jsonwebtoken/npm/android-arm64/package.json +++ b/packages/jsonwebtoken/npm/android-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-android-arm64", "version": "0.2.3", - "os": [ - "android" - ], "cpu": [ "arm64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/jsonwebtoken/npm/darwin-arm64/package.json b/packages/jsonwebtoken/npm/darwin-arm64/package.json index a5fc7183..bbd10f3a 100644 --- a/packages/jsonwebtoken/npm/darwin-arm64/package.json +++ b/packages/jsonwebtoken/npm/darwin-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-darwin-arm64", "version": "0.2.3", - "os": [ - "darwin" - ], "cpu": [ "arm64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/jsonwebtoken/npm/darwin-x64/package.json b/packages/jsonwebtoken/npm/darwin-x64/package.json index d45754c0..8060706e 100644 --- a/packages/jsonwebtoken/npm/darwin-x64/package.json +++ b/packages/jsonwebtoken/npm/darwin-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-darwin-x64", "version": "0.2.3", - "os": [ - "darwin" - ], "cpu": [ "x64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/jsonwebtoken/npm/freebsd-x64/package.json b/packages/jsonwebtoken/npm/freebsd-x64/package.json index 7a493947..e2fea826 100644 --- a/packages/jsonwebtoken/npm/freebsd-x64/package.json +++ b/packages/jsonwebtoken/npm/freebsd-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-freebsd-x64", "version": "0.2.3", - "os": [ - "freebsd" - ], "cpu": [ "x64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "freebsd" + ] } diff --git a/packages/jsonwebtoken/npm/linux-arm-gnueabihf/package.json b/packages/jsonwebtoken/npm/linux-arm-gnueabihf/package.json index e1f3dcd4..be87c77c 100644 --- a/packages/jsonwebtoken/npm/linux-arm-gnueabihf/package.json +++ b/packages/jsonwebtoken/npm/linux-arm-gnueabihf/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-linux-arm-gnueabihf", "version": "0.2.3", - "os": [ - "linux" - ], "cpu": [ "arm" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "linux" + ] } diff --git a/packages/jsonwebtoken/npm/linux-arm64-gnu/package.json b/packages/jsonwebtoken/npm/linux-arm64-gnu/package.json index d0e44b34..6a662ab7 100644 --- a/packages/jsonwebtoken/npm/linux-arm64-gnu/package.json +++ b/packages/jsonwebtoken/npm/linux-arm64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-linux-arm64-gnu", "version": "0.2.3", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/jsonwebtoken/npm/linux-arm64-musl/package.json b/packages/jsonwebtoken/npm/linux-arm64-musl/package.json index 52ad13f8..6d767d57 100644 --- a/packages/jsonwebtoken/npm/linux-arm64-musl/package.json +++ b/packages/jsonwebtoken/npm/linux-arm64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-linux-arm64-musl", "version": "0.2.3", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/jsonwebtoken/npm/linux-x64-gnu/package.json b/packages/jsonwebtoken/npm/linux-x64-gnu/package.json index 37e05938..6cbd959d 100644 --- a/packages/jsonwebtoken/npm/linux-x64-gnu/package.json +++ b/packages/jsonwebtoken/npm/linux-x64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-linux-x64-gnu", "version": "0.2.3", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/jsonwebtoken/npm/linux-x64-musl/package.json b/packages/jsonwebtoken/npm/linux-x64-musl/package.json index 411658c3..b45420d9 100644 --- a/packages/jsonwebtoken/npm/linux-x64-musl/package.json +++ b/packages/jsonwebtoken/npm/linux-x64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-linux-x64-musl", "version": "0.2.3", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -39,6 +36,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/jsonwebtoken/npm/wasm32-wasi/README.md b/packages/jsonwebtoken/npm/wasm32-wasi/README.md new file mode 100644 index 00000000..de0f9fac --- /dev/null +++ b/packages/jsonwebtoken/npm/wasm32-wasi/README.md @@ -0,0 +1,3 @@ +# `@node-rs/jsonwebtoken-wasm32-wasi` + +This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/jsonwebtoken` diff --git a/packages/jsonwebtoken/npm/wasm32-wasi/package.json b/packages/jsonwebtoken/npm/wasm32-wasi/package.json new file mode 100644 index 00000000..37d2a2bd --- /dev/null +++ b/packages/jsonwebtoken/npm/wasm32-wasi/package.json @@ -0,0 +1,39 @@ +{ + "name": "@node-rs/jsonwebtoken-wasm32-wasi", + "version": "0.2.3", + "cpu": [ + "wasm32" + ], + "main": "jsonwebtoken.wasm32-wasi.wasm", + "files": [ + "jsonwebtoken.wasm32-wasi.wasm" + ], + "description": "Rust jsonwebtoken binding for Node.js", + "keywords": [ + "jsonwebtoken", + "jwt", + "napi-rs", + "node-rs" + ], + "author": { + "name": "Francesco Benedetto", + "url": "https://github.com/nebarf", + "email": "francesco.benedetto263@gmail.com" + }, + "homepage": "https://github.com/napi-rs/node-rs#readme", + "license": "MIT", + "engines": { + "node": ">= 10" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/jsonwebtoken/npm/win32-arm64-msvc/package.json b/packages/jsonwebtoken/npm/win32-arm64-msvc/package.json index c9267059..d3809718 100644 --- a/packages/jsonwebtoken/npm/win32-arm64-msvc/package.json +++ b/packages/jsonwebtoken/npm/win32-arm64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-win32-arm64-msvc", "version": "0.2.3", - "os": [ - "win32" - ], "cpu": [ "arm64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/jsonwebtoken/npm/win32-ia32-msvc/package.json b/packages/jsonwebtoken/npm/win32-ia32-msvc/package.json index a2533efb..8b4c4419 100644 --- a/packages/jsonwebtoken/npm/win32-ia32-msvc/package.json +++ b/packages/jsonwebtoken/npm/win32-ia32-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-win32-ia32-msvc", "version": "0.2.3", - "os": [ - "win32" - ], "cpu": [ "ia32" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/jsonwebtoken/npm/win32-x64-msvc/package.json b/packages/jsonwebtoken/npm/win32-x64-msvc/package.json index e31c8717..f50a36b3 100644 --- a/packages/jsonwebtoken/npm/win32-x64-msvc/package.json +++ b/packages/jsonwebtoken/npm/win32-x64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/jsonwebtoken-win32-x64-msvc", "version": "0.2.3", - "os": [ - "win32" - ], "cpu": [ "x64" ], @@ -38,5 +35,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/jsonwebtoken/package.json b/packages/jsonwebtoken/package.json index 96f5d17f..d66d1625 100644 --- a/packages/jsonwebtoken/package.json +++ b/packages/jsonwebtoken/package.json @@ -31,7 +31,8 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi" + "armv7-linux-androideabi", + "wasm32-wasi-preview1-threads" ] }, "repository": { diff --git a/packages/jsonwebtoken/wasi-worker.mjs b/packages/jsonwebtoken/wasi-worker.mjs new file mode 100644 index 00000000..d23c9501 --- /dev/null +++ b/packages/jsonwebtoken/wasi-worker.mjs @@ -0,0 +1,51 @@ +import fs from "node:fs"; +import { createRequire } from "node:module"; +import { parentPort, Worker } from "node:worker_threads"; + +import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; +import { WASI } from "@tybys/wasm-util"; + +const require = createRequire(import.meta.url); + +if (parentPort) { + parentPort.on("message", (data) => { + globalThis.onmessage({ data }); + }); +} + +Object.assign(globalThis, { + self: globalThis, + require, + Worker, + importScripts: function (f) { + ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + }, + postMessage: function (msg) { + if (parentPort) { + parentPort.postMessage(msg); + } + }, +}); + +const handler = new MessageHandler({ + onLoad({ wasmModule, wasmMemory }) { + const wasi = new WASI({ fs }); + + return instantiateNapiModuleSync(wasmModule, { + childThread: true, + wasi, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: wasmMemory + }; + }, + }); + }, +}); + +globalThis.onmessage = function (e) { + handler.handle(e); +}; diff --git a/packages/xxhash/index.d.ts b/packages/xxhash/index.d.ts index 625a7f4b..d7c1b2de 100644 --- a/packages/xxhash/index.d.ts +++ b/packages/xxhash/index.d.ts @@ -3,32 +3,33 @@ export class Xxh32 { constructor(seed?: number | undefined | null) - update(input: string | Buffer): this + update(input: string | Uint8Array): this digest(): number reset(newState?: number | undefined | null): void } export class Xxh64 { constructor(seed?: bigint | undefined | null) - update(input: string | Buffer): this + update(input: string | Uint8Array): this digest(): bigint reset(newState?: bigint | undefined | null): void } -export function xxh32(input: string | Buffer, seed?: number | undefined | null): number +export function xxh32(input: string | Uint8Array, seed?: number | undefined | null): number -export function xxh64(input: string | Buffer, seed?: bigint | undefined | null): bigint +export function xxh64(input: string | Uint8Array, seed?: bigint | undefined | null): bigint export namespace xxh3 { export class Xxh3 { static withSeed(seed?: bigint | undefined | null): Xxh3 - static withSecret(secret: Buffer): Xxh3 - update(input: string | Buffer): this + static withSecret(secret: Uint8Array): Xxh3 + update(input: string | Uint8Array): this digest(): bigint reset(): void } - export function xxh128(input: string | Buffer, seed?: bigint | undefined | null): bigint - export function xxh128WithSecret(input: string | Buffer, secret: Buffer): bigint - export function xxh64(input: string | Buffer, seed?: bigint | undefined | null): bigint - export function xxh64WithSecret(input: string | Buffer, secret: Buffer): bigint + export function xxh128(input: string | Uint8Array, seed?: bigint | undefined | null): bigint + export function xxh128WithSecret(input: string | Uint8Array, secret: Uint8Array): bigint + export function xxh64(input: string | Uint8Array, seed?: bigint | undefined | null): bigint + export function xxh64WithSecret(input: string | Uint8Array, secret: Uint8Array): bigint } + diff --git a/packages/xxhash/index.js b/packages/xxhash/index.js index 4f9e609d..1eb6ee4a 100644 --- a/packages/xxhash/index.js +++ b/packages/xxhash/index.js @@ -94,7 +94,9 @@ switch (platform) { case 'win32': switch (arch) { case 'x64': - localFileExisted = existsSync(join(__dirname, 'xxhash.win32-x64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.win32-x64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.win32-x64-msvc.node') @@ -106,7 +108,9 @@ switch (platform) { } break case 'ia32': - localFileExisted = existsSync(join(__dirname, 'xxhash.win32-ia32-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.win32-ia32-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.win32-ia32-msvc.node') @@ -118,7 +122,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'xxhash.win32-arm64-msvc.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.win32-arm64-msvc.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.win32-arm64-msvc.node') @@ -157,7 +163,9 @@ switch (platform) { } break case 'arm64': - localFileExisted = existsSync(join(__dirname, 'xxhash.darwin-arm64.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.darwin-arm64.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.darwin-arm64.node') @@ -191,7 +199,9 @@ switch (platform) { switch (arch) { case 'x64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'xxhash.linux-x64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.linux-x64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.linux-x64-musl.node') @@ -202,7 +212,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'xxhash.linux-x64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.linux-x64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.linux-x64-gnu.node') @@ -216,7 +228,9 @@ switch (platform) { break case 'arm64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'xxhash.linux-arm64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.linux-arm64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.linux-arm64-musl.node') @@ -227,7 +241,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'xxhash.linux-arm64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.linux-arm64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.linux-arm64-gnu.node') @@ -240,7 +256,9 @@ switch (platform) { } break case 'arm': - localFileExisted = existsSync(join(__dirname, 'xxhash.linux-arm-gnueabihf.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.linux-arm-gnueabihf.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.linux-arm-gnueabihf.node') @@ -253,7 +271,9 @@ switch (platform) { break case 'riscv64': if (isMusl()) { - localFileExisted = existsSync(join(__dirname, 'xxhash.linux-riscv64-musl.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.linux-riscv64-musl.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.linux-riscv64-musl.node') @@ -264,7 +284,9 @@ switch (platform) { loadError = e } } else { - localFileExisted = existsSync(join(__dirname, 'xxhash.linux-riscv64-gnu.node')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.linux-riscv64-gnu.node') + ) try { if (localFileExisted) { nativeBinding = require('./xxhash.linux-riscv64-gnu.node') @@ -286,8 +308,9 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = - existsSync(join(__dirname, 'xxhash.wasm32-wasi.wasm')) && existsSync(join(__dirname, 'xxhash.wasi.cjs')) + localFileExisted = existsSync( + join(__dirname, 'xxhash.wasm32-wasi.wasm') + ) && existsSync(join(__dirname, 'xxhash.wasi.cjs')) if (localFileExisted) { nativeBinding = require('./xxhash.wasi.cjs') } else { diff --git a/packages/xxhash/npm/android-arm-eabi/package.json b/packages/xxhash/npm/android-arm-eabi/package.json index 8e736ba4..5f34d99c 100644 --- a/packages/xxhash/npm/android-arm-eabi/package.json +++ b/packages/xxhash/npm/android-arm-eabi/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-android-arm-eabi", "version": "1.4.2", - "os": [ - "android" - ], "cpu": [ "arm" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/xxhash/npm/android-arm64/package.json b/packages/xxhash/npm/android-arm64/package.json index 28793d86..edaae6f8 100644 --- a/packages/xxhash/npm/android-arm64/package.json +++ b/packages/xxhash/npm/android-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-android-arm64", "version": "1.4.2", - "os": [ - "android" - ], "cpu": [ "arm64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "android" + ] } diff --git a/packages/xxhash/npm/darwin-arm64/package.json b/packages/xxhash/npm/darwin-arm64/package.json index 1a6dde07..2641befd 100644 --- a/packages/xxhash/npm/darwin-arm64/package.json +++ b/packages/xxhash/npm/darwin-arm64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-darwin-arm64", "version": "1.4.2", - "os": [ - "darwin" - ], "cpu": [ "arm64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/xxhash/npm/darwin-x64/package.json b/packages/xxhash/npm/darwin-x64/package.json index d5608440..ca0c28f2 100644 --- a/packages/xxhash/npm/darwin-x64/package.json +++ b/packages/xxhash/npm/darwin-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-darwin-x64", "version": "1.4.2", - "os": [ - "darwin" - ], "cpu": [ "x64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "darwin" + ] } diff --git a/packages/xxhash/npm/freebsd-x64/package.json b/packages/xxhash/npm/freebsd-x64/package.json index ff698f71..83360432 100644 --- a/packages/xxhash/npm/freebsd-x64/package.json +++ b/packages/xxhash/npm/freebsd-x64/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-freebsd-x64", "version": "1.4.2", - "os": [ - "freebsd" - ], "cpu": [ "x64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "freebsd" + ] } diff --git a/packages/xxhash/npm/linux-arm-gnueabihf/package.json b/packages/xxhash/npm/linux-arm-gnueabihf/package.json index be4b0a9b..a52623d1 100644 --- a/packages/xxhash/npm/linux-arm-gnueabihf/package.json +++ b/packages/xxhash/npm/linux-arm-gnueabihf/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-linux-arm-gnueabihf", "version": "1.4.2", - "os": [ - "linux" - ], "cpu": [ "arm" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "linux" + ] } diff --git a/packages/xxhash/npm/linux-arm64-gnu/package.json b/packages/xxhash/npm/linux-arm64-gnu/package.json index 00432408..8b15af62 100644 --- a/packages/xxhash/npm/linux-arm64-gnu/package.json +++ b/packages/xxhash/npm/linux-arm64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-linux-arm64-gnu", "version": "1.4.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/xxhash/npm/linux-arm64-musl/package.json b/packages/xxhash/npm/linux-arm64-musl/package.json index 35407667..35d7f847 100644 --- a/packages/xxhash/npm/linux-arm64-musl/package.json +++ b/packages/xxhash/npm/linux-arm64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-linux-arm64-musl", "version": "1.4.2", - "os": [ - "linux" - ], "cpu": [ "arm64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/xxhash/npm/linux-x64-gnu/package.json b/packages/xxhash/npm/linux-x64-gnu/package.json index fe9eb94e..219a3426 100644 --- a/packages/xxhash/npm/linux-x64-gnu/package.json +++ b/packages/xxhash/npm/linux-x64-gnu/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-linux-x64-gnu", "version": "1.4.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "glibc" ] diff --git a/packages/xxhash/npm/linux-x64-musl/package.json b/packages/xxhash/npm/linux-x64-musl/package.json index adbfdd3f..67cc490c 100644 --- a/packages/xxhash/npm/linux-x64-musl/package.json +++ b/packages/xxhash/npm/linux-x64-musl/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-linux-x64-musl", "version": "1.4.2", - "os": [ - "linux" - ], "cpu": [ "x64" ], @@ -40,6 +37,9 @@ "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" }, + "os": [ + "linux" + ], "libc": [ "musl" ] diff --git a/packages/xxhash/npm/wasm32-wasi/README.md b/packages/xxhash/npm/wasm32-wasi/README.md new file mode 100644 index 00000000..78faa706 --- /dev/null +++ b/packages/xxhash/npm/wasm32-wasi/README.md @@ -0,0 +1,3 @@ +# `@node-rs/xxhash-wasm32-wasi` + +This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/xxhash` diff --git a/packages/xxhash/npm/wasm32-wasi/package.json b/packages/xxhash/npm/wasm32-wasi/package.json new file mode 100644 index 00000000..49d1b8b8 --- /dev/null +++ b/packages/xxhash/npm/wasm32-wasi/package.json @@ -0,0 +1,40 @@ +{ + "name": "@node-rs/xxhash-wasm32-wasi", + "version": "1.4.2", + "cpu": [ + "wasm32" + ], + "main": "xxhash.wasm32-wasi.wasm", + "files": [ + "xxhash.wasm32-wasi.wasm" + ], + "description": "Fastest xxhash implementation in Node.js", + "keywords": [ + "hash", + "xxhash", + "xxhashjs", + "Rust", + "node-rs", + "napi", + "napi-rs", + "N-API", + "Node-API" + ], + "author": "LongYinan ", + "homepage": "https://github.com/napi-rs/node-rs", + "license": "MIT", + "engines": { + "node": ">= 12" + }, + "publishConfig": { + "registry": "https://registry.npmjs.org/", + "access": "public" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "bugs": { + "url": "https://github.com/napi-rs/node-rs/issues" + } +} diff --git a/packages/xxhash/npm/win32-arm64-msvc/package.json b/packages/xxhash/npm/win32-arm64-msvc/package.json index f80f9d5d..dfe405ef 100644 --- a/packages/xxhash/npm/win32-arm64-msvc/package.json +++ b/packages/xxhash/npm/win32-arm64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-win32-arm64-msvc", "version": "1.4.2", - "os": [ - "win32" - ], "cpu": [ "arm64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/xxhash/npm/win32-ia32-msvc/package.json b/packages/xxhash/npm/win32-ia32-msvc/package.json index 94bf36d0..d7d44772 100644 --- a/packages/xxhash/npm/win32-ia32-msvc/package.json +++ b/packages/xxhash/npm/win32-ia32-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-win32-ia32-msvc", "version": "1.4.2", - "os": [ - "win32" - ], "cpu": [ "ia32" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/xxhash/npm/win32-x64-msvc/package.json b/packages/xxhash/npm/win32-x64-msvc/package.json index 07f03783..450271ec 100644 --- a/packages/xxhash/npm/win32-x64-msvc/package.json +++ b/packages/xxhash/npm/win32-x64-msvc/package.json @@ -1,9 +1,6 @@ { "name": "@node-rs/xxhash-win32-x64-msvc", "version": "1.4.2", - "os": [ - "win32" - ], "cpu": [ "x64" ], @@ -39,5 +36,8 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" - } + }, + "os": [ + "win32" + ] } diff --git a/packages/xxhash/package.json b/packages/xxhash/package.json index 6c3ac5c5..373fffa1 100644 --- a/packages/xxhash/package.json +++ b/packages/xxhash/package.json @@ -37,7 +37,8 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi" + "armv7-linux-androideabi", + "wasm32-wasi-preview1-threads" ] }, "engines": { diff --git a/packages/xxhash/src/lib.rs b/packages/xxhash/src/lib.rs index 342bb32e..191f9365 100644 --- a/packages/xxhash/src/lib.rs +++ b/packages/xxhash/src/lib.rs @@ -9,7 +9,7 @@ use napi_derive::napi; use xxhash_rust::{xxh32, xxh64}; #[napi] -fn xxh32(input: Either, seed: Option) -> u32 { +fn xxh32(input: Either, seed: Option) -> u32 { let seed = seed.unwrap_or(0); xxhash_rust::xxh32::xxh32( match &input { @@ -35,7 +35,7 @@ impl Xxh32 { } #[napi] - pub fn update(&mut self, input: Either) -> &Self { + pub fn update(&mut self, input: Either) -> &Self { match input { Either::A(s) => self.inner.update(s.as_bytes()), Either::B(b) => self.inner.update(b.as_ref()), @@ -55,7 +55,7 @@ impl Xxh32 { } #[napi] -fn xxh64(input: Either, seed: Option) -> u64 { +fn xxh64(input: Either, seed: Option) -> u64 { let seed = seed.map(|b| b.get_u64().1).unwrap_or(0); xxhash_rust::xxh64::xxh64( match &input { @@ -81,7 +81,7 @@ impl Xxh64 { } #[napi] - pub fn update(&mut self, input: Either) -> &Self { + pub fn update(&mut self, input: Either) -> &Self { match input { Either::A(s) => self.inner.update(s.as_bytes()), Either::B(b) => self.inner.update(b.as_ref()), @@ -108,7 +108,7 @@ mod xxh3_js { use xxhash_rust::xxh3; #[napi] - pub fn xxh64(input: Either, seed: Option) -> u64 { + pub fn xxh64(input: Either, seed: Option) -> u64 { let seed = seed.map(|b| b.get_u64().1).unwrap_or(0); match input { Either::A(s) => xxh3::xxh3_64_with_seed(s.as_bytes(), seed), @@ -117,7 +117,7 @@ mod xxh3_js { } #[napi] - pub fn xxh64_with_secret(input: Either, secret: Buffer) -> u64 { + pub fn xxh64_with_secret(input: Either, secret: Uint8Array) -> u64 { match input { Either::A(s) => xxh3::xxh3_64_with_secret(s.as_bytes(), secret.as_ref()), Either::B(b) => xxh3::xxh3_64_with_secret(b.as_ref(), secret.as_ref()), @@ -125,7 +125,7 @@ mod xxh3_js { } #[napi] - pub fn xxh128(input: Either, seed: Option) -> u128 { + pub fn xxh128(input: Either, seed: Option) -> u128 { let seed = seed.map(|b| b.get_u64().1).unwrap_or(0); xxh3::xxh3_128_with_seed( match &input { @@ -137,7 +137,7 @@ mod xxh3_js { } #[napi] - pub fn xxh128_with_secret(input: Either, secret: Buffer) -> u128 { + pub fn xxh128_with_secret(input: Either, secret: Uint8Array) -> u128 { xxh3::xxh3_128_with_secret( match &input { Either::A(s) => s.as_bytes(), @@ -162,7 +162,7 @@ mod xxh3_js { } #[napi(factory)] - pub fn with_secret(secret: Buffer) -> Self { + pub fn with_secret(secret: Uint8Array) -> Self { let mut sec = [0u8; 192]; sec.copy_from_slice(secret.as_ref()); Self { @@ -171,7 +171,7 @@ mod xxh3_js { } #[napi] - pub fn update(&mut self, input: Either) -> &Self { + pub fn update(&mut self, input: Either) -> &Self { self.inner.update(match &input { Either::A(s) => s.as_bytes(), Either::B(b) => b.as_ref(), diff --git a/packages/xxhash/wasi-worker.mjs b/packages/xxhash/wasi-worker.mjs new file mode 100644 index 00000000..d23c9501 --- /dev/null +++ b/packages/xxhash/wasi-worker.mjs @@ -0,0 +1,51 @@ +import fs from "node:fs"; +import { createRequire } from "node:module"; +import { parentPort, Worker } from "node:worker_threads"; + +import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; +import { WASI } from "@tybys/wasm-util"; + +const require = createRequire(import.meta.url); + +if (parentPort) { + parentPort.on("message", (data) => { + globalThis.onmessage({ data }); + }); +} + +Object.assign(globalThis, { + self: globalThis, + require, + Worker, + importScripts: function (f) { + ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); + }, + postMessage: function (msg) { + if (parentPort) { + parentPort.postMessage(msg); + } + }, +}); + +const handler = new MessageHandler({ + onLoad({ wasmModule, wasmMemory }) { + const wasi = new WASI({ fs }); + + return instantiateNapiModuleSync(wasmModule, { + childThread: true, + wasi, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: wasmMemory + }; + }, + }); + }, +}); + +globalThis.onmessage = function (e) { + handler.handle(e); +}; diff --git a/packages/xxhash/xxhash.wasi.cjs b/packages/xxhash/xxhash.wasi.cjs new file mode 100644 index 00000000..8b1598e7 --- /dev/null +++ b/packages/xxhash/xxhash.wasi.cjs @@ -0,0 +1,72 @@ +/* eslint-disable */ +/* prettier-ignore */ + +/* auto-generated by NAPI-RS */ + +const __nodeFs= require('node:fs') +const __nodePath = require('node:path') +const { WASI: __nodeWASI } = require('node:wasi') +const { Worker } = require('node:worker_threads') + +const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') +const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') + +const __wasi = new __nodeWASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/' + } +}) + +const __emnapiContext = __emnapiGetDefaultContext() + +const __sharedMemory = new WebAssembly.Memory({ + initial: 1024, + maximum: 10240, + shared: true, +}) + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'xxhash.wasm32-wasi.wasm')), { + context: __emnapiContext, + asyncWorkPoolSize: 4, + wasi: __wasi, + onCreateWorker() { + return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { + env: process.env, + execArgv: ['--experimental-wasi-unstable-preview1'], + }) + }, + overwriteImports(importObject) { + importObject.env = { + ...importObject.env, + ...importObject.napi, + ...importObject.emnapi, + memory: __sharedMemory, + } + return importObject + }, + beforeInit({ instance }) { + __napi_rs_initialize_modules(instance) + } +}) + +function __napi_rs_initialize_modules(__napiInstance) { + __napiInstance.exports['__napi_register__xxh32_0']?.() + __napiInstance.exports['__napi_register__Xxh32_struct_1']?.() + __napiInstance.exports['__napi_register__Xxh32_impl_6']?.() + __napiInstance.exports['__napi_register__xxh64_7']?.() + __napiInstance.exports['__napi_register__Xxh64_struct_8']?.() + __napiInstance.exports['__napi_register__Xxh64_impl_13']?.() + __napiInstance.exports['__napi_register__xxh64_14']?.() + __napiInstance.exports['__napi_register__xxh64_with_secret_15']?.() + __napiInstance.exports['__napi_register__xxh128_16']?.() + __napiInstance.exports['__napi_register__xxh128_with_secret_17']?.() + __napiInstance.exports['__napi_register__Xxh3_struct_18']?.() + __napiInstance.exports['__napi_register__Xxh3_impl_24']?.() +} +module.exports.Xxh32 = __napiModule.exports.Xxh32, +module.exports.Xxh64 = __napiModule.exports.Xxh64, +module.exports.xxh32 = __napiModule.exports.xxh32, +module.exports.xxh64 = __napiModule.exports.xxh64, +module.exports.xxh3 = __napiModule.exports.xxh3 diff --git a/yarn.lock b/yarn.lock index ef221b97..389a75ff 100644 --- a/yarn.lock +++ b/yarn.lock @@ -49,6 +49,24 @@ __metadata: languageName: node linkType: hard +"@emnapi/core@npm:^0.44.0": + version: 0.44.0 + resolution: "@emnapi/core@npm:0.44.0" + dependencies: + tslib: "npm:^2.4.0" + checksum: 554ad897ae5d6e49471f1922113e60f74ecdc7617ea83ba070b022e7878d7f35160b26d42da98f692f915ba89ccc8900a57ee1afeafbe9fc225c0b92db5772f4 + languageName: node + linkType: hard + +"@emnapi/runtime@npm:^0.44.0": + version: 0.44.0 + resolution: "@emnapi/runtime@npm:0.44.0" + dependencies: + tslib: "npm:^2.4.0" + checksum: 68133f288a5f413787610232c90194ab0b692b859a130866f4869483217d94f71ac3fd23aacfb428e0eb438e4882d0e04874690a1f06386b7b35ebae330e698a + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -6821,6 +6839,8 @@ __metadata: version: 0.0.0-use.local resolution: "node-rs@workspace:." dependencies: + "@emnapi/core": "npm:^0.44.0" + "@emnapi/runtime": "npm:^0.44.0" "@napi-rs/cli": "npm:^3.0.0-alpha.25" "@swc-node/core": "npm:^1.10.6" "@swc-node/register": "npm:^1.6.8" From a5c7203e76ffe668c4cfd34317449f762b10a555 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 30 Dec 2023 11:50:20 +0800 Subject: [PATCH 2/3] patch --- .github/workflows/ci.yaml | 46 ++++++++++++ package.json | 6 +- packages/argon2/argon2.wasi.cjs | 22 +++++- packages/argon2/index.js | 9 +-- packages/argon2/npm/wasm32-wasi/package.json | 10 ++- packages/argon2/package.json | 2 +- packages/bcrypt/bcrypt.wasi.cjs | 22 +++++- packages/bcrypt/binding.js | 8 +- packages/bcrypt/npm/wasm32-wasi/package.json | 10 ++- packages/bcrypt/package.json | 2 +- .../__tests__/__snapshots__/index.spec.ts.md | 6 ++ .../__snapshots__/index.spec.ts.snap | Bin 204 -> 213 bytes packages/crc32/__tests__/index.spec.ts | 2 +- packages/crc32/crc32.wasi.cjs | 22 +++++- packages/crc32/index.js | 9 +-- packages/crc32/npm/wasm32-wasi/package.json | 10 ++- packages/crc32/package.json | 2 +- packages/deno-lint/deno-lint.wasi.cjs | 59 --------------- packages/deno-lint/index.js | 9 +-- .../deno-lint/npm/wasm32-wasi/package.json | 10 ++- packages/deno-lint/package.json | 5 +- packages/deno-lint/wasi-worker.mjs | 51 ------------- packages/jieba/index.js | 9 +-- packages/jieba/jieba.wasi.cjs | 22 +++++- packages/jieba/npm/wasm32-wasi/package.json | 10 ++- packages/jieba/package.json | 2 +- packages/jsonwebtoken/Cargo.toml | 2 +- packages/jsonwebtoken/index.js | 9 +-- packages/jsonwebtoken/jsonwebtoken.wasi.cjs | 70 ------------------ .../jsonwebtoken/npm/wasm32-wasi/README.md | 3 - .../jsonwebtoken/npm/wasm32-wasi/package.json | 39 ---------- packages/jsonwebtoken/package.json | 5 +- packages/jsonwebtoken/wasi-worker.mjs | 51 ------------- packages/xxhash/index.js | 9 +-- packages/xxhash/npm/wasm32-wasi/package.json | 10 ++- packages/xxhash/package.json | 2 +- packages/xxhash/xxhash.wasi.cjs | 22 +++++- yarn.lock | 44 +++++------ 38 files changed, 243 insertions(+), 388 deletions(-) delete mode 100644 packages/deno-lint/deno-lint.wasi.cjs delete mode 100644 packages/deno-lint/wasi-worker.mjs delete mode 100644 packages/jsonwebtoken/jsonwebtoken.wasi.cjs delete mode 100644 packages/jsonwebtoken/npm/wasm32-wasi/README.md delete mode 100644 packages/jsonwebtoken/npm/wasm32-wasi/package.json delete mode 100644 packages/jsonwebtoken/wasi-worker.mjs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bf90a23a..e841dfa3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -69,6 +69,9 @@ jobs: - host: windows-latest target: 'aarch64-pc-windows-msvc' build: yarn build --target aarch64-pc-windows-msvc + - host: ubuntu-latest + target: 'wasm32-wasi-preview1-threads' + build: yarn workspaces foreach -A --no-private -j 1 --exclude @node-rs/jsonwebtoken --exclude @node-rs/denolint run build --target wasm32-wasi-preview1-threads name: stable - ${{ matrix.settings.target }} - node@20 runs-on: ${{ matrix.settings.host }} @@ -133,11 +136,20 @@ jobs: - name: Upload artifact uses: actions/upload-artifact@v4 + if: ${{ matrix.settings.target != 'wasm32-wasi-preview1-threads' }} with: name: bindings-${{ matrix.settings.target }} path: packages/*/*.node if-no-files-found: error + - name: Upload artifact + uses: actions/upload-artifact@v4 + if: ${{ matrix.settings.target == 'wasm32-wasi-preview1-threads' }} + with: + name: bindings-${{ matrix.settings.target }} + path: packages/*/*.wasm + if-no-files-found: error + build-freebsd: runs-on: macos-12 name: Build FreeBSD @@ -520,6 +532,40 @@ jobs: yarn test -s ls -la + test-wasi-nodejs: + name: Test bindings on wasi - node@${{ matrix.node }} + needs: + - build + strategy: + fail-fast: false + matrix: + node: ['18', '20'] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + cache: yarn + - name: 'Install dependencies' + run: yarn install --immutable --mode=skip-build + - name: Download artifacts + uses: actions/download-artifact@v4 + with: + name: bindings-wasm32-wasi-preview1-threads + path: artifacts + - name: Move artifacts + run: yarn artifacts + shell: bash + - name: List packages + run: ls -R . + - name: Test + run: | + yarn build:ts + yarn test -s + env: + NAPI_RS_FORCE_WASI: 'true' + publish: name: Publish runs-on: ubuntu-latest diff --git a/package.json b/package.json index ab7522e0..b5330461 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,9 @@ "postinstall": "husky install" }, "devDependencies": { - "@emnapi/core": "^0.44.0", - "@emnapi/runtime": "^0.44.0", - "@napi-rs/cli": "^3.0.0-alpha.25", + "@emnapi/core": "^0.45.0", + "@emnapi/runtime": "^0.45.0", + "@napi-rs/cli": "^3.0.0-alpha.29", "@swc-node/core": "^1.10.6", "@swc-node/register": "^1.6.8", "@swc/core": "^1.3.101", diff --git a/packages/argon2/argon2.wasi.cjs b/packages/argon2/argon2.wasi.cjs index a86ff8d7..eb626780 100644 --- a/packages/argon2/argon2.wasi.cjs +++ b/packages/argon2/argon2.wasi.cjs @@ -27,9 +27,27 @@ const __sharedMemory = new WebAssembly.Memory({ shared: true, }) -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'argon2.wasm32-wasi.wasm')), { +let __wasmFilePath = __nodePath.join(__dirname, 'argon2.wasm32-wasi.wasm') + +if (!__nodeFs.existsSync(__wasmFilePath)) { + try { + __wasmFilePath = __nodePath.resolve('@node-rs/argon2-wasm32-wasi') + } catch { + throw new Error('Cannot find argon2.wasm32-wasi.wasm file, and @node-rs/argon2-wasm32-wasi package is not installed.') + } +} + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), { context: __emnapiContext, - asyncWorkPoolSize: 4, + 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 + } else { + return 4 + } + })(), wasi: __wasi, onCreateWorker() { return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { diff --git a/packages/argon2/index.js b/packages/argon2/index.js index 55f879bb..32a7c172 100644 --- a/packages/argon2/index.js +++ b/packages/argon2/index.js @@ -308,14 +308,7 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = existsSync( - join(__dirname, 'argon2.wasm32-wasi.wasm') - ) && existsSync(join(__dirname, 'argon2.wasi.cjs')) - if (localFileExisted) { - nativeBinding = require('./argon2.wasi.cjs') - } else { - nativeBinding = require('@node-rs/argon2-wasm32-wasi') - } + nativeBinding = require('./argon2.wasi.cjs') } catch { // ignore } diff --git a/packages/argon2/npm/wasm32-wasi/package.json b/packages/argon2/npm/wasm32-wasi/package.json index 634caac4..5953e4c4 100644 --- a/packages/argon2/npm/wasm32-wasi/package.json +++ b/packages/argon2/npm/wasm32-wasi/package.json @@ -4,9 +4,11 @@ "cpu": [ "wasm32" ], - "main": "argon2.wasm32-wasi.wasm", + "main": "argon2.wasi.cjs", "files": [ - "argon2.wasm32-wasi.wasm" + "argon2.wasm32-wasi.wasm", + "argon2.wasi.cjs", + "wasi-worker.mjs" ], "description": "RustCrypto: Argon2 binding for Node.js", "keywords": [ @@ -32,5 +34,9 @@ "repository": { "type": "git", "url": "git+https://github.com/napi-rs/node-rs.git" + }, + "dependencies": { + "@emnapi/core": "^0.45.0", + "@emnapi/runtime": "^0.45.0" } } diff --git a/packages/argon2/package.json b/packages/argon2/package.json index 8d51cb12..92d2cb6f 100644 --- a/packages/argon2/package.json +++ b/packages/argon2/package.json @@ -56,7 +56,7 @@ "version": "napi version" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.29", "argon2": "^0.31.2", "cross-env": "^7.0.3" } diff --git a/packages/bcrypt/bcrypt.wasi.cjs b/packages/bcrypt/bcrypt.wasi.cjs index 678828e6..f2f6b977 100644 --- a/packages/bcrypt/bcrypt.wasi.cjs +++ b/packages/bcrypt/bcrypt.wasi.cjs @@ -27,9 +27,27 @@ const __sharedMemory = new WebAssembly.Memory({ shared: true, }) -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'bcrypt.wasm32-wasi.wasm')), { +let __wasmFilePath = __nodePath.join(__dirname, 'bcrypt.wasm32-wasi.wasm') + +if (!__nodeFs.existsSync(__wasmFilePath)) { + try { + __wasmFilePath = __nodePath.resolve('@node-rs/bcrypt-wasm32-wasi') + } catch { + throw new Error('Cannot find bcrypt.wasm32-wasi.wasm file, and @node-rs/bcrypt-wasm32-wasi package is not installed.') + } +} + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), { context: __emnapiContext, - asyncWorkPoolSize: 4, + 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 + } else { + return 4 + } + })(), wasi: __wasi, onCreateWorker() { return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { diff --git a/packages/bcrypt/binding.js b/packages/bcrypt/binding.js index 737ea3d8..4f65da56 100644 --- a/packages/bcrypt/binding.js +++ b/packages/bcrypt/binding.js @@ -286,13 +286,7 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = - existsSync(join(__dirname, 'bcrypt.wasm32-wasi.wasm')) && existsSync(join(__dirname, 'bcrypt.wasi.cjs')) - if (localFileExisted) { - nativeBinding = require('./bcrypt.wasi.cjs') - } else { - nativeBinding = require('@node-rs/bcrypt-wasm32-wasi') - } + nativeBinding = require('./bcrypt.wasi.cjs') } catch { // ignore } diff --git a/packages/bcrypt/npm/wasm32-wasi/package.json b/packages/bcrypt/npm/wasm32-wasi/package.json index 98af27cd..5dc4aaf7 100644 --- a/packages/bcrypt/npm/wasm32-wasi/package.json +++ b/packages/bcrypt/npm/wasm32-wasi/package.json @@ -4,9 +4,11 @@ "cpu": [ "wasm32" ], - "main": "bcrypt.wasm32-wasi.wasm", + "main": "bcrypt.wasi.cjs", "files": [ - "bcrypt.wasm32-wasi.wasm" + "bcrypt.wasm32-wasi.wasm", + "bcrypt.wasi.cjs", + "wasi-worker.mjs" ], "description": "Rust bcrypt binding", "keywords": [ @@ -36,5 +38,9 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" + }, + "dependencies": { + "@emnapi/core": "^0.45.0", + "@emnapi/runtime": "^0.45.0" } } diff --git a/packages/bcrypt/package.json b/packages/bcrypt/package.json index 0c5bdaf3..2c8430dd 100644 --- a/packages/bcrypt/package.json +++ b/packages/bcrypt/package.json @@ -68,7 +68,7 @@ "url": "https://github.com/napi-rs/node-rs/issues" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.29", "@types/bcrypt": "^5.0.2", "bcrypt": "^5.1.1", "bcryptjs": "^2.4.3", diff --git a/packages/crc32/__tests__/__snapshots__/index.spec.ts.md b/packages/crc32/__tests__/__snapshots__/index.spec.ts.md index 750b74a5..8d6e3fd1 100644 --- a/packages/crc32/__tests__/__snapshots__/index.spec.ts.md +++ b/packages/crc32/__tests__/__snapshots__/index.spec.ts.md @@ -10,6 +10,12 @@ Generated by [AVA](https://avajs.dev). 2608757237 +## crc32c result should be equal with sse4_crc32 when calculate with initial crc + +> Snapshot 1 + + 614479218 + ## crc32c result should be equal with sse4_crc32 when caclulate with initial crc > Snapshot 1 diff --git a/packages/crc32/__tests__/__snapshots__/index.spec.ts.snap b/packages/crc32/__tests__/__snapshots__/index.spec.ts.snap index 37c8e3f66b6a0b3c26436bd486a370608acf1414..36f6d90dd1ef957072fa4a6be4a9826f4f3b01af 100644 GIT binary patch literal 213 zcmV;`04o1MRzVajKoAAs6(e3_3R@e0jv<&6$Ye6Ii$kJ2vqT#~Jcf<& zj`pG=2v+KA-{XDlvXGiqdc3d&6W$If(s)D=S(6y3ualHeFz_a!KoiV^Do+-VUC;{G z>TAv;CnF*KelAQVc1#?Lxb!2$|Uqz`!h24``49{lH$ P{OaBSOd|)#QUL$}i|}7; literal 204 zcmV;-05ktVRzV { t.snapshot(crc32c(fx)) }) -test('crc32c result should be equal with sse4_crc32 when caclulate with initial crc', (t) => { +test('crc32c result should be equal with sse4_crc32 when calculate with initial crc', (t) => { const initialCrc = crc32c(fx) t.snapshot(crc32c(fx, initialCrc)) }) diff --git a/packages/crc32/crc32.wasi.cjs b/packages/crc32/crc32.wasi.cjs index 28ce9d8e..3dda8a98 100644 --- a/packages/crc32/crc32.wasi.cjs +++ b/packages/crc32/crc32.wasi.cjs @@ -27,9 +27,27 @@ const __sharedMemory = new WebAssembly.Memory({ shared: true, }) -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'crc32.wasm32-wasi.wasm')), { +let __wasmFilePath = __nodePath.join(__dirname, 'crc32.wasm32-wasi.wasm') + +if (!__nodeFs.existsSync(__wasmFilePath)) { + try { + __wasmFilePath = __nodePath.resolve('@node-rs/crc32-wasm32-wasi') + } catch { + throw new Error('Cannot find crc32.wasm32-wasi.wasm file, and @node-rs/crc32-wasm32-wasi package is not installed.') + } +} + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), { context: __emnapiContext, - asyncWorkPoolSize: 4, + 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 + } else { + return 4 + } + })(), wasi: __wasi, onCreateWorker() { return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { diff --git a/packages/crc32/index.js b/packages/crc32/index.js index 505a2901..2c9909d4 100644 --- a/packages/crc32/index.js +++ b/packages/crc32/index.js @@ -308,14 +308,7 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = existsSync( - join(__dirname, 'crc32.wasm32-wasi.wasm') - ) && existsSync(join(__dirname, 'crc32.wasi.cjs')) - if (localFileExisted) { - nativeBinding = require('./crc32.wasi.cjs') - } else { - nativeBinding = require('@node-rs/crc32-wasm32-wasi') - } + nativeBinding = require('./crc32.wasi.cjs') } catch { // ignore } diff --git a/packages/crc32/npm/wasm32-wasi/package.json b/packages/crc32/npm/wasm32-wasi/package.json index 780567c0..3571adc3 100644 --- a/packages/crc32/npm/wasm32-wasi/package.json +++ b/packages/crc32/npm/wasm32-wasi/package.json @@ -4,9 +4,11 @@ "cpu": [ "wasm32" ], - "main": "crc32.wasm32-wasi.wasm", + "main": "crc32.wasi.cjs", "files": [ - "crc32.wasm32-wasi.wasm" + "crc32.wasm32-wasi.wasm", + "crc32.wasi.cjs", + "wasi-worker.mjs" ], "description": "SIMD crc32", "keywords": [ @@ -33,5 +35,9 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" + }, + "dependencies": { + "@emnapi/core": "^0.45.0", + "@emnapi/runtime": "^0.45.0" } } diff --git a/packages/crc32/package.json b/packages/crc32/package.json index a9014ee2..dc707330 100644 --- a/packages/crc32/package.json +++ b/packages/crc32/package.json @@ -62,7 +62,7 @@ "url": "https://github.com/napi-rs/node-rs/issues" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.29", "@types/crc": "^3.8.3", "buffer": "^6.0.3", "crc": "^4.3.2", diff --git a/packages/deno-lint/deno-lint.wasi.cjs b/packages/deno-lint/deno-lint.wasi.cjs deleted file mode 100644 index b93d6d36..00000000 --- a/packages/deno-lint/deno-lint.wasi.cjs +++ /dev/null @@ -1,59 +0,0 @@ -/* eslint-disable */ -/* prettier-ignore */ - -/* auto-generated by NAPI-RS */ - -const __nodeFs= require('node:fs') -const __nodePath = require('node:path') -const { WASI: __nodeWASI } = require('node:wasi') -const { Worker } = require('node:worker_threads') - -const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') -const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') - -const __wasi = new __nodeWASI({ - version: 'preview1', - env: process.env, - preopens: { - '/': '/' - } -}) - -const __emnapiContext = __emnapiGetDefaultContext() - -const __sharedMemory = new WebAssembly.Memory({ - initial: 1024, - maximum: 10240, - shared: true, -}) - -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'deno-lint.wasm32-wasi.wasm')), { - context: __emnapiContext, - asyncWorkPoolSize: 4, - wasi: __wasi, - onCreateWorker() { - return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { - env: process.env, - execArgv: ['--experimental-wasi-unstable-preview1'], - }) - }, - overwriteImports(importObject) { - importObject.env = { - ...importObject.env, - ...importObject.napi, - ...importObject.emnapi, - memory: __sharedMemory, - } - return importObject - }, - beforeInit({ instance }) { - __napi_rs_initialize_modules(instance) - } -}) - -function __napi_rs_initialize_modules(__napiInstance) { - __napiInstance.exports['__napi_register__lint_0']?.() - __napiInstance.exports['__napi_register__denolint_1']?.() -} -module.exports.denolint = __napiModule.exports.denolint, -module.exports.lint = __napiModule.exports.lint diff --git a/packages/deno-lint/index.js b/packages/deno-lint/index.js index cfd4d3fe..14e889ea 100644 --- a/packages/deno-lint/index.js +++ b/packages/deno-lint/index.js @@ -308,14 +308,7 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = existsSync( - join(__dirname, 'deno-lint.wasm32-wasi.wasm') - ) && existsSync(join(__dirname, 'deno-lint.wasi.cjs')) - if (localFileExisted) { - nativeBinding = require('./deno-lint.wasi.cjs') - } else { - nativeBinding = require('@node-rs/deno-lint-wasm32-wasi') - } + nativeBinding = require('./deno-lint.wasi.cjs') } catch { // ignore } diff --git a/packages/deno-lint/npm/wasm32-wasi/package.json b/packages/deno-lint/npm/wasm32-wasi/package.json index 93528aa9..6d90a602 100644 --- a/packages/deno-lint/npm/wasm32-wasi/package.json +++ b/packages/deno-lint/npm/wasm32-wasi/package.json @@ -4,9 +4,11 @@ "cpu": [ "wasm32" ], - "main": "deno-lint.wasm32-wasi.wasm", + "main": "deno-lint.wasi.cjs", "files": [ - "deno-lint.wasm32-wasi.wasm" + "deno-lint.wasm32-wasi.wasm", + "deno-lint.wasi.cjs", + "wasi-worker.mjs" ], "description": "Deno lint binding for Node.js", "keywords": [ @@ -35,5 +37,9 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" + }, + "dependencies": { + "@emnapi/core": "^0.45.0", + "@emnapi/runtime": "^0.45.0" } } diff --git a/packages/deno-lint/package.json b/packages/deno-lint/package.json index 0416a574..8d11e065 100644 --- a/packages/deno-lint/package.json +++ b/packages/deno-lint/package.json @@ -43,8 +43,7 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi", - "wasm32-wasi-preview1-threads" + "armv7-linux-androideabi" ] }, "engines": { @@ -74,7 +73,7 @@ "typanion": "^3.14.0" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.29", "@types/webpack": "^5.28.5" }, "funding": { diff --git a/packages/deno-lint/wasi-worker.mjs b/packages/deno-lint/wasi-worker.mjs deleted file mode 100644 index d23c9501..00000000 --- a/packages/deno-lint/wasi-worker.mjs +++ /dev/null @@ -1,51 +0,0 @@ -import fs from "node:fs"; -import { createRequire } from "node:module"; -import { parentPort, Worker } from "node:worker_threads"; - -import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; -import { WASI } from "@tybys/wasm-util"; - -const require = createRequire(import.meta.url); - -if (parentPort) { - parentPort.on("message", (data) => { - globalThis.onmessage({ data }); - }); -} - -Object.assign(globalThis, { - self: globalThis, - require, - Worker, - importScripts: function (f) { - ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); - }, - postMessage: function (msg) { - if (parentPort) { - parentPort.postMessage(msg); - } - }, -}); - -const handler = new MessageHandler({ - onLoad({ wasmModule, wasmMemory }) { - const wasi = new WASI({ fs }); - - return instantiateNapiModuleSync(wasmModule, { - childThread: true, - wasi, - overwriteImports(importObject) { - importObject.env = { - ...importObject.env, - ...importObject.napi, - ...importObject.emnapi, - memory: wasmMemory - }; - }, - }); - }, -}); - -globalThis.onmessage = function (e) { - handler.handle(e); -}; diff --git a/packages/jieba/index.js b/packages/jieba/index.js index a93c3aef..f0c9ac7a 100644 --- a/packages/jieba/index.js +++ b/packages/jieba/index.js @@ -308,14 +308,7 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = existsSync( - join(__dirname, 'jieba.wasm32-wasi.wasm') - ) && existsSync(join(__dirname, 'jieba.wasi.cjs')) - if (localFileExisted) { - nativeBinding = require('./jieba.wasi.cjs') - } else { - nativeBinding = require('@node-rs/jieba-wasm32-wasi') - } + nativeBinding = require('./jieba.wasi.cjs') } catch { // ignore } diff --git a/packages/jieba/jieba.wasi.cjs b/packages/jieba/jieba.wasi.cjs index e585c42c..d22b0adc 100644 --- a/packages/jieba/jieba.wasi.cjs +++ b/packages/jieba/jieba.wasi.cjs @@ -27,9 +27,27 @@ const __sharedMemory = new WebAssembly.Memory({ shared: true, }) -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'jieba.wasm32-wasi.wasm')), { +let __wasmFilePath = __nodePath.join(__dirname, 'jieba.wasm32-wasi.wasm') + +if (!__nodeFs.existsSync(__wasmFilePath)) { + try { + __wasmFilePath = __nodePath.resolve('@node-rs/jieba-wasm32-wasi') + } catch { + throw new Error('Cannot find jieba.wasm32-wasi.wasm file, and @node-rs/jieba-wasm32-wasi package is not installed.') + } +} + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), { context: __emnapiContext, - asyncWorkPoolSize: 4, + 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 + } else { + return 4 + } + })(), wasi: __wasi, onCreateWorker() { return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { diff --git a/packages/jieba/npm/wasm32-wasi/package.json b/packages/jieba/npm/wasm32-wasi/package.json index f4e3f683..af928b3a 100644 --- a/packages/jieba/npm/wasm32-wasi/package.json +++ b/packages/jieba/npm/wasm32-wasi/package.json @@ -4,9 +4,11 @@ "cpu": [ "wasm32" ], - "main": "jieba.wasm32-wasi.wasm", + "main": "jieba.wasi.cjs", "files": [ - "jieba.wasm32-wasi.wasm" + "jieba.wasm32-wasi.wasm", + "jieba.wasi.cjs", + "wasi-worker.mjs" ], "description": "Fastest Chinese word segmentation in Node.js", "keywords": [ @@ -33,5 +35,9 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" + }, + "dependencies": { + "@emnapi/core": "^0.45.0", + "@emnapi/runtime": "^0.45.0" } } diff --git a/packages/jieba/package.json b/packages/jieba/package.json index 1d744b45..e93bf87b 100644 --- a/packages/jieba/package.json +++ b/packages/jieba/package.json @@ -62,7 +62,7 @@ "url": "https://github.com/napi-rs/node-rs/issues" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.29", "nodejieba": "^2.6.0" }, "funding": { diff --git a/packages/jsonwebtoken/Cargo.toml b/packages/jsonwebtoken/Cargo.toml index 7ece0564..aa905f05 100644 --- a/packages/jsonwebtoken/Cargo.toml +++ b/packages/jsonwebtoken/Cargo.toml @@ -20,4 +20,4 @@ serde = "1.0" serde_json = "1.0" [build-dependencies] -napi-build = "2" +napi-build = { version = "2" } diff --git a/packages/jsonwebtoken/index.js b/packages/jsonwebtoken/index.js index da551f8e..eb6be6a3 100644 --- a/packages/jsonwebtoken/index.js +++ b/packages/jsonwebtoken/index.js @@ -308,14 +308,7 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = existsSync( - join(__dirname, 'jsonwebtoken.wasm32-wasi.wasm') - ) && existsSync(join(__dirname, 'jsonwebtoken.wasi.cjs')) - if (localFileExisted) { - nativeBinding = require('./jsonwebtoken.wasi.cjs') - } else { - nativeBinding = require('@node-rs/jsonwebtoken-wasm32-wasi') - } + nativeBinding = require('./jsonwebtoken.wasi.cjs') } catch { // ignore } diff --git a/packages/jsonwebtoken/jsonwebtoken.wasi.cjs b/packages/jsonwebtoken/jsonwebtoken.wasi.cjs deleted file mode 100644 index 64b09b2f..00000000 --- a/packages/jsonwebtoken/jsonwebtoken.wasi.cjs +++ /dev/null @@ -1,70 +0,0 @@ -/* eslint-disable */ -/* prettier-ignore */ - -/* auto-generated by NAPI-RS */ - -const __nodeFs= require('node:fs') -const __nodePath = require('node:path') -const { WASI: __nodeWASI } = require('node:wasi') -const { Worker } = require('node:worker_threads') - -const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core') -const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime') - -const __wasi = new __nodeWASI({ - version: 'preview1', - env: process.env, - preopens: { - '/': '/' - } -}) - -const __emnapiContext = __emnapiGetDefaultContext() - -const __sharedMemory = new WebAssembly.Memory({ - initial: 1024, - maximum: 10240, - shared: true, -}) - -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'jsonwebtoken.wasm32-wasi.wasm')), { - context: __emnapiContext, - asyncWorkPoolSize: 4, - wasi: __wasi, - onCreateWorker() { - return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { - env: process.env, - execArgv: ['--experimental-wasi-unstable-preview1'], - }) - }, - overwriteImports(importObject) { - importObject.env = { - ...importObject.env, - ...importObject.napi, - ...importObject.emnapi, - memory: __sharedMemory, - } - return importObject - }, - beforeInit({ instance }) { - __napi_rs_initialize_modules(instance) - } -}) - -function __napi_rs_initialize_modules(__napiInstance) { - __napiInstance.exports['__napi_register__Algorithm_0']?.() - __napiInstance.exports['__napi_register__Claims_struct_1']?.() - __napiInstance.exports['__napi_register__Header_struct_2']?.() - __napiInstance.exports['__napi_register__SignTask_impl_3']?.() - __napiInstance.exports['__napi_register__sign_4']?.() - __napiInstance.exports['__napi_register__sign_sync_5']?.() - __napiInstance.exports['__napi_register__Validation_struct_6']?.() - __napiInstance.exports['__napi_register__VerifyTask_impl_7']?.() - __napiInstance.exports['__napi_register__verify_8']?.() - __napiInstance.exports['__napi_register__verify_sync_9']?.() -} -module.exports.Algorithm = __napiModule.exports.Algorithm, -module.exports.sign = __napiModule.exports.sign, -module.exports.signSync = __napiModule.exports.signSync, -module.exports.verify = __napiModule.exports.verify, -module.exports.verifySync = __napiModule.exports.verifySync diff --git a/packages/jsonwebtoken/npm/wasm32-wasi/README.md b/packages/jsonwebtoken/npm/wasm32-wasi/README.md deleted file mode 100644 index de0f9fac..00000000 --- a/packages/jsonwebtoken/npm/wasm32-wasi/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# `@node-rs/jsonwebtoken-wasm32-wasi` - -This is the **wasm32-wasi-preview1-threads** binary for `@node-rs/jsonwebtoken` diff --git a/packages/jsonwebtoken/npm/wasm32-wasi/package.json b/packages/jsonwebtoken/npm/wasm32-wasi/package.json deleted file mode 100644 index 37d2a2bd..00000000 --- a/packages/jsonwebtoken/npm/wasm32-wasi/package.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "@node-rs/jsonwebtoken-wasm32-wasi", - "version": "0.2.3", - "cpu": [ - "wasm32" - ], - "main": "jsonwebtoken.wasm32-wasi.wasm", - "files": [ - "jsonwebtoken.wasm32-wasi.wasm" - ], - "description": "Rust jsonwebtoken binding for Node.js", - "keywords": [ - "jsonwebtoken", - "jwt", - "napi-rs", - "node-rs" - ], - "author": { - "name": "Francesco Benedetto", - "url": "https://github.com/nebarf", - "email": "francesco.benedetto263@gmail.com" - }, - "homepage": "https://github.com/napi-rs/node-rs#readme", - "license": "MIT", - "engines": { - "node": ">= 10" - }, - "publishConfig": { - "registry": "https://registry.npmjs.org/", - "access": "public" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/napi-rs/node-rs.git" - }, - "bugs": { - "url": "https://github.com/napi-rs/node-rs/issues" - } -} diff --git a/packages/jsonwebtoken/package.json b/packages/jsonwebtoken/package.json index d66d1625..7dc42f67 100644 --- a/packages/jsonwebtoken/package.json +++ b/packages/jsonwebtoken/package.json @@ -31,8 +31,7 @@ "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", - "armv7-linux-androideabi", - "wasm32-wasi-preview1-threads" + "armv7-linux-androideabi" ] }, "repository": { @@ -63,7 +62,7 @@ "node": ">= 10" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.29", "@types/jsonwebtoken": "^9.0.5", "jsonwebtoken": "^9.0.2" } diff --git a/packages/jsonwebtoken/wasi-worker.mjs b/packages/jsonwebtoken/wasi-worker.mjs deleted file mode 100644 index d23c9501..00000000 --- a/packages/jsonwebtoken/wasi-worker.mjs +++ /dev/null @@ -1,51 +0,0 @@ -import fs from "node:fs"; -import { createRequire } from "node:module"; -import { parentPort, Worker } from "node:worker_threads"; - -import { instantiateNapiModuleSync, MessageHandler } from "@emnapi/core"; -import { WASI } from "@tybys/wasm-util"; - -const require = createRequire(import.meta.url); - -if (parentPort) { - parentPort.on("message", (data) => { - globalThis.onmessage({ data }); - }); -} - -Object.assign(globalThis, { - self: globalThis, - require, - Worker, - importScripts: function (f) { - ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f); - }, - postMessage: function (msg) { - if (parentPort) { - parentPort.postMessage(msg); - } - }, -}); - -const handler = new MessageHandler({ - onLoad({ wasmModule, wasmMemory }) { - const wasi = new WASI({ fs }); - - return instantiateNapiModuleSync(wasmModule, { - childThread: true, - wasi, - overwriteImports(importObject) { - importObject.env = { - ...importObject.env, - ...importObject.napi, - ...importObject.emnapi, - memory: wasmMemory - }; - }, - }); - }, -}); - -globalThis.onmessage = function (e) { - handler.handle(e); -}; diff --git a/packages/xxhash/index.js b/packages/xxhash/index.js index 1eb6ee4a..9aad7904 100644 --- a/packages/xxhash/index.js +++ b/packages/xxhash/index.js @@ -308,14 +308,7 @@ switch (platform) { if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { try { - localFileExisted = existsSync( - join(__dirname, 'xxhash.wasm32-wasi.wasm') - ) && existsSync(join(__dirname, 'xxhash.wasi.cjs')) - if (localFileExisted) { - nativeBinding = require('./xxhash.wasi.cjs') - } else { - nativeBinding = require('@node-rs/xxhash-wasm32-wasi') - } + nativeBinding = require('./xxhash.wasi.cjs') } catch { // ignore } diff --git a/packages/xxhash/npm/wasm32-wasi/package.json b/packages/xxhash/npm/wasm32-wasi/package.json index 49d1b8b8..3480347b 100644 --- a/packages/xxhash/npm/wasm32-wasi/package.json +++ b/packages/xxhash/npm/wasm32-wasi/package.json @@ -4,9 +4,11 @@ "cpu": [ "wasm32" ], - "main": "xxhash.wasm32-wasi.wasm", + "main": "xxhash.wasi.cjs", "files": [ - "xxhash.wasm32-wasi.wasm" + "xxhash.wasm32-wasi.wasm", + "xxhash.wasi.cjs", + "wasi-worker.mjs" ], "description": "Fastest xxhash implementation in Node.js", "keywords": [ @@ -36,5 +38,9 @@ }, "bugs": { "url": "https://github.com/napi-rs/node-rs/issues" + }, + "dependencies": { + "@emnapi/core": "^0.45.0", + "@emnapi/runtime": "^0.45.0" } } diff --git a/packages/xxhash/package.json b/packages/xxhash/package.json index 373fffa1..25e68001 100644 --- a/packages/xxhash/package.json +++ b/packages/xxhash/package.json @@ -64,7 +64,7 @@ "url": "https://github.com/napi-rs/node-rs/issues" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.29", "@types/xxhashjs": "^0.2.4", "webpack": "^5.89.0", "xxhash": "^0.3.0", diff --git a/packages/xxhash/xxhash.wasi.cjs b/packages/xxhash/xxhash.wasi.cjs index 8b1598e7..f595bb40 100644 --- a/packages/xxhash/xxhash.wasi.cjs +++ b/packages/xxhash/xxhash.wasi.cjs @@ -27,9 +27,27 @@ const __sharedMemory = new WebAssembly.Memory({ shared: true, }) -const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, 'xxhash.wasm32-wasi.wasm')), { +let __wasmFilePath = __nodePath.join(__dirname, 'xxhash.wasm32-wasi.wasm') + +if (!__nodeFs.existsSync(__wasmFilePath)) { + try { + __wasmFilePath = __nodePath.resolve('@node-rs/xxhash-wasm32-wasi') + } catch { + throw new Error('Cannot find xxhash.wasm32-wasi.wasm file, and @node-rs/xxhash-wasm32-wasi package is not installed.') + } +} + +const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), { context: __emnapiContext, - asyncWorkPoolSize: 4, + 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 + } else { + return 4 + } + })(), wasi: __wasi, onCreateWorker() { return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), { diff --git a/yarn.lock b/yarn.lock index 389a75ff..c24fefbb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -49,21 +49,21 @@ __metadata: languageName: node linkType: hard -"@emnapi/core@npm:^0.44.0": - version: 0.44.0 - resolution: "@emnapi/core@npm:0.44.0" +"@emnapi/core@npm:^0.45.0": + version: 0.45.0 + resolution: "@emnapi/core@npm:0.45.0" dependencies: tslib: "npm:^2.4.0" - checksum: 554ad897ae5d6e49471f1922113e60f74ecdc7617ea83ba070b022e7878d7f35160b26d42da98f692f915ba89ccc8900a57ee1afeafbe9fc225c0b92db5772f4 + checksum: 26071cfe32a5a15068483ed36a152c03add319166c30e6cb81f0d6eb2614e69f836daa2d69af5af2e00fc2b8335f172e5ce2ee91f1819b3e689c908a7883433f languageName: node linkType: hard -"@emnapi/runtime@npm:^0.44.0": - version: 0.44.0 - resolution: "@emnapi/runtime@npm:0.44.0" +"@emnapi/runtime@npm:^0.45.0": + version: 0.45.0 + resolution: "@emnapi/runtime@npm:0.45.0" dependencies: tslib: "npm:^2.4.0" - checksum: 68133f288a5f413787610232c90194ab0b692b859a130866f4869483217d94f71ac3fd23aacfb428e0eb438e4882d0e04874690a1f06386b7b35ebae330e698a + checksum: c83052b05efb7147c256bfbb69214c9642fef1dce8d7d901e0314a7b2d2dcd14e1cd75502c6565004847e552658e9913a7e58889c7dca92e240173032f1db5d5 languageName: node linkType: hard @@ -326,9 +326,9 @@ __metadata: languageName: node linkType: hard -"@napi-rs/cli@npm:^3.0.0-alpha.25": - version: 3.0.0-alpha.28 - resolution: "@napi-rs/cli@npm:3.0.0-alpha.28" +"@napi-rs/cli@npm:^3.0.0-alpha.29": + version: 3.0.0-alpha.29 + resolution: "@napi-rs/cli@npm:3.0.0-alpha.29" dependencies: "@napi-rs/cross-toolchain": "npm:^0.0.14" "@octokit/rest": "npm:^20.0.2" @@ -356,7 +356,7 @@ __metadata: bin: napi: dist/cli.js napi-raw: cli.mjs - checksum: 45e1a58793120104268520d84f67372cbb2a8ee65e27bf9737eac214d4f63c9c0c05531e11690a03e537b13704a295a7698c70e0b48c579a2226701af728c27b + checksum: 25ed4464d150d6a18dbbdf1ca25fd9351510f212597e9fc613dc5f74509e618935b3fa453edd3bc4c4d8c13319d5f3d5740671bca228599c1b9608d0a219ee44 languageName: node linkType: hard @@ -680,7 +680,7 @@ __metadata: version: 0.0.0-use.local resolution: "@node-rs/argon2@workspace:packages/argon2" dependencies: - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" argon2: "npm:^0.31.2" cross-env: "npm:^7.0.3" languageName: unknown @@ -690,7 +690,7 @@ __metadata: version: 0.0.0-use.local resolution: "@node-rs/bcrypt@workspace:packages/bcrypt" dependencies: - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" "@types/bcrypt": "npm:^5.0.2" bcrypt: "npm:^5.1.1" bcryptjs: "npm:^2.4.3" @@ -702,7 +702,7 @@ __metadata: version: 0.0.0-use.local resolution: "@node-rs/crc32@workspace:packages/crc32" dependencies: - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" "@types/crc": "npm:^3.8.3" buffer: "npm:^6.0.3" crc: "npm:^4.3.2" @@ -714,7 +714,7 @@ __metadata: version: 0.0.0-use.local resolution: "@node-rs/deno-lint@workspace:packages/deno-lint" dependencies: - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" "@types/webpack": "npm:^5.28.5" clipanion: "npm:^3.2.1" typanion: "npm:^3.14.0" @@ -735,7 +735,7 @@ __metadata: version: 0.0.0-use.local resolution: "@node-rs/jieba@workspace:packages/jieba" dependencies: - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" nodejieba: "npm:^2.6.0" languageName: unknown linkType: soft @@ -744,7 +744,7 @@ __metadata: version: 0.0.0-use.local resolution: "@node-rs/jsonwebtoken@workspace:packages/jsonwebtoken" dependencies: - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" "@types/jsonwebtoken": "npm:^9.0.5" jsonwebtoken: "npm:^9.0.2" languageName: unknown @@ -754,7 +754,7 @@ __metadata: version: 0.0.0-use.local resolution: "@node-rs/xxhash@workspace:packages/xxhash" dependencies: - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" "@types/xxhashjs": "npm:^0.2.4" webpack: "npm:^5.89.0" xxhash: "npm:^0.3.0" @@ -6839,9 +6839,9 @@ __metadata: version: 0.0.0-use.local resolution: "node-rs@workspace:." dependencies: - "@emnapi/core": "npm:^0.44.0" - "@emnapi/runtime": "npm:^0.44.0" - "@napi-rs/cli": "npm:^3.0.0-alpha.25" + "@emnapi/core": "npm:^0.45.0" + "@emnapi/runtime": "npm:^0.45.0" + "@napi-rs/cli": "npm:^3.0.0-alpha.29" "@swc-node/core": "npm:^1.10.6" "@swc-node/register": "npm:^1.6.8" "@swc/core": "npm:^1.3.101" From ff9e3d15c68e04a7a225ff948d6f0a3367b8c446 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 2 Jan 2024 15:25:56 +0800 Subject: [PATCH 3/3] fix filter --- .github/workflows/ci.yaml | 3 ++- ava.config.mjs | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e841dfa3..a8c59f61 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -71,7 +71,7 @@ jobs: build: yarn build --target aarch64-pc-windows-msvc - host: ubuntu-latest target: 'wasm32-wasi-preview1-threads' - build: yarn workspaces foreach -A --no-private -j 1 --exclude @node-rs/jsonwebtoken --exclude @node-rs/denolint run build --target wasm32-wasi-preview1-threads + build: yarn workspaces foreach -A --no-private -j 1 --exclude "@node-rs/{jsonwebtoken,deno-lint}" run build --target wasm32-wasi-preview1-threads name: stable - ${{ matrix.settings.target }} - node@20 runs-on: ${{ matrix.settings.host }} @@ -578,6 +578,7 @@ jobs: - test-linux-aarch64-musl-binding - test-linux-arm-gnueabihf-binding - test-macOS-windows-binding + - test-wasi-nodejs steps: - uses: actions/checkout@v4 diff --git a/ava.config.mjs b/ava.config.mjs index bf75f5d9..70df9caf 100644 --- a/ava.config.mjs +++ b/ava.config.mjs @@ -1,4 +1,4 @@ -export default { +const avaConfig = { extensions: ['ts'], workerThreads: false, cache: false, @@ -9,3 +9,9 @@ export default { TS_NODE_PROJECT: './tsconfig.test.json', }, } + +if (process.env.NAPI_RS_FORCE_WASI) { + avaConfig.files.push(`!packages/jsonwebtoken/**/*.spec.ts`) +} + +export default avaConfig