From 874e336728322c49d602be74cb7f2aa693902cfb Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 30 Dec 2023 11:50:20 +0800 Subject: [PATCH] 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 | 22 ++++++- packages/deno-lint/index.js | 9 +-- .../deno-lint/npm/wasm32-wasi/package.json | 10 ++- packages/deno-lint/package.json | 2 +- 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 | 22 ++++++- .../jsonwebtoken/npm/wasm32-wasi/README.md | 3 - .../jsonwebtoken/npm/wasm32-wasi/package.json | 39 ------------ packages/jsonwebtoken/package.json | 5 +- 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 | 60 +++++++++--------- 36 files changed, 290 insertions(+), 167 deletions(-) delete mode 100644 packages/jsonwebtoken/npm/wasm32-wasi/README.md delete mode 100644 packages/jsonwebtoken/npm/wasm32-wasi/package.json diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bf90a23a..922d5a17 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 + - 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 . + - uses: actions/setup-node@v4 + with: + node-version: ${{ matrix.node }} + cache: yarn + - name: 'Install dependencies' + run: yarn install --immutable --mode=skip-build + - 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..d9883e62 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.28", "@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..5fbd41aa 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.28", "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..68de25a4 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.28", "@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..b762a24d 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.28", "@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 index b93d6d36..ae33d40f 100644 --- a/packages/deno-lint/deno-lint.wasi.cjs +++ b/packages/deno-lint/deno-lint.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, 'deno-lint.wasm32-wasi.wasm')), { +let __wasmFilePath = __nodePath.join(__dirname, 'deno-lint.wasm32-wasi.wasm') + +if (!__nodeFs.existsSync(__wasmFilePath)) { + try { + __wasmFilePath = __nodePath.resolve('@node-rs/deno-lint-wasm32-wasi') + } catch { + throw new Error('Cannot find deno-lint.wasm32-wasi.wasm file, and @node-rs/deno-lint-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/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..6850be70 100644 --- a/packages/deno-lint/package.json +++ b/packages/deno-lint/package.json @@ -74,7 +74,7 @@ "typanion": "^3.14.0" }, "devDependencies": { - "@napi-rs/cli": "^3.0.0-alpha.25", + "@napi-rs/cli": "^3.0.0-alpha.28", "@types/webpack": "^5.28.5" }, "funding": { 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..401515e4 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.28", "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 index 64b09b2f..2fa0f414 100644 --- a/packages/jsonwebtoken/jsonwebtoken.wasi.cjs +++ b/packages/jsonwebtoken/jsonwebtoken.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, 'jsonwebtoken.wasm32-wasi.wasm')), { +let __wasmFilePath = __nodePath.join(__dirname, 'jsonwebtoken.wasm32-wasi.wasm') + +if (!__nodeFs.existsSync(__wasmFilePath)) { + try { + __wasmFilePath = __nodePath.resolve('@node-rs/jsonwebtoken-wasm32-wasi') + } catch { + throw new Error('Cannot find jsonwebtoken.wasm32-wasi.wasm file, and @node-rs/jsonwebtoken-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/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..83d21977 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.28", "@types/jsonwebtoken": "^9.0.5", "jsonwebtoken": "^9.0.2" } 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..e5159bee 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.28", "@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 7665e6d8..d45ecb69 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.25 - resolution: "@napi-rs/cli@npm:3.0.0-alpha.25" +"@napi-rs/cli@npm:^3.0.0-alpha.28": + version: 3.0.0-alpha.28 + resolution: "@napi-rs/cli@npm:3.0.0-alpha.28" dependencies: "@napi-rs/cross-toolchain": "npm:^0.0.14" "@octokit/rest": "npm:^20.0.2" @@ -336,16 +336,16 @@ __metadata: clipanion: "npm:^3.2.1" colorette: "npm:^2.0.20" debug: "npm:^4.3.4" - emnapi: "npm:0.44.0" + emnapi: "npm:0.45.0" inquirer: "npm:^9.2.12" js-yaml: "npm:^4.1.0" lodash-es: "npm:^4.17.21" toml: "npm:^3.0.0" typanion: "npm:^3.14.0" peerDependencies: - "@emnapi/runtime": 0.44.0 - "@tybys/wasm-util": 0.8.0 - emnapi: 0.44.0 + "@emnapi/runtime": ^0.45.0 + "@tybys/wasm-util": ^0.8.0 + emnapi: ^0.45.0 peerDependenciesMeta: "@emnapi/runtime": optional: true @@ -356,7 +356,7 @@ __metadata: bin: napi: dist/cli.js napi-raw: cli.mjs - checksum: 742f8900616099ddc65cf76e54b039637eb869100e8eee6b10c563bd17e1ecdfbcf41cc0cb9c11fa46e96d6374e6e62cbf5b3b08dbb678e32552aad56459f8fb + checksum: 45e1a58793120104268520d84f67372cbb2a8ee65e27bf9737eac214d4f63c9c0c05531e11690a03e537b13704a295a7698c70e0b48c579a2226701af728c27b 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.28" 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.28" "@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.28" "@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.28" "@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.28" 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.28" "@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.28" "@types/xxhashjs": "npm:^0.2.4" webpack: "npm:^5.89.0" xxhash: "npm:^0.3.0" @@ -3683,15 +3683,15 @@ __metadata: languageName: node linkType: hard -"emnapi@npm:0.44.0": - version: 0.44.0 - resolution: "emnapi@npm:0.44.0" +"emnapi@npm:0.45.0": + version: 0.45.0 + resolution: "emnapi@npm:0.45.0" peerDependencies: node-addon-api: ">= 6.1.0" peerDependenciesMeta: node-addon-api: optional: true - checksum: 26e6391479c80e3eb5234feb944370c963d7f851f1d3c7a140dea514809b690549ba110d09211b57763ad5f7e308180a3bcd745484faf5363eeb12b45079542b + checksum: 076411fc611c0e3f1c08a9260babbeac8094474d8a4174e6b9f27dc5ba569b51fd42c2513d17d4056680a680a575fb8c81f18259d7208327b53195bf3f58483b languageName: node linkType: hard @@ -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.28" "@swc-node/core": "npm:^1.10.6" "@swc-node/register": "npm:^1.6.8" "@swc/core": "npm:^1.3.101"