Skip to content

Commit 8018d0d

Browse files
committed
fix(*): wasi browser compatible
1 parent 4928afe commit 8018d0d

40 files changed

+291
-245
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ jobs:
390390
strategy:
391391
fail-fast: false
392392
matrix:
393-
node: ['18', '20']
393+
node: ['20', '22']
394394
runs-on: ubuntu-latest
395395

396396
steps:
@@ -539,7 +539,7 @@ jobs:
539539
strategy:
540540
fail-fast: false
541541
matrix:
542-
node: ['18', '20']
542+
node: ['18', '20', '22']
543543
runs-on: ubuntu-latest
544544
steps:
545545
- uses: actions/checkout@v4

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
"postinstall": "husky install"
2525
},
2626
"devDependencies": {
27-
"@emnapi/core": "^0.45.0",
28-
"@emnapi/runtime": "^0.45.0",
29-
"@napi-rs/cli": "^3.0.0-alpha.34",
27+
"@napi-rs/cli": "^3.0.0-alpha.36",
28+
"@napi-rs/wasm-runtime": "^0.1.1",
3029
"@swc-node/core": "^1.10.6",
3130
"@swc-node/register": "^1.6.8",
3231
"@swc/core": "^1.3.101",

packages/argon2/argon2.wasi-browser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core'
2-
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
3-
import { WASI as __WASI } from '@tybys/wasm-util'
4-
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from 'memfs-browser'
1+
import {
2+
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
3+
getDefaultContext as __emnapiGetDefaultContext,
4+
WASI as __WASI,
5+
} from '@napi-rs/wasm-runtime'
6+
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
57

68
import __wasmUrl from './argon2.wasm32-wasi.wasm?url'
79

packages/argon2/argon2.wasi.cjs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ const __nodePath = require('node:path')
88
const { WASI: __nodeWASI } = require('node:wasi')
99
const { Worker } = require('node:worker_threads')
1010

11-
const { instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync } = require('@emnapi/core')
12-
const { getDefaultContext: __emnapiGetDefaultContext } = require('@emnapi/runtime')
11+
const {
12+
instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
13+
getDefaultContext: __emnapiGetDefaultContext,
14+
} = require('@napi-rs/wasm-runtime')
1315

1416
const __wasi = new __nodeWASI({
1517
version: 'preview1',
1618
env: process.env,
1719
preopens: {
18-
'/': '/',
19-
},
20+
'/': '/'
21+
}
2022
})
2123

2224
const __emnapiContext = __emnapiGetDefaultContext()
@@ -33,19 +35,13 @@ if (!__nodeFs.existsSync(__wasmFilePath)) {
3335
try {
3436
__wasmFilePath = __nodePath.resolve('@node-rs/argon2-wasm32-wasi')
3537
} catch {
36-
throw new Error(
37-
'Cannot find argon2.wasm32-wasi.wasm file, and @node-rs/argon2-wasm32-wasi package is not installed.',
38-
)
38+
throw new Error('Cannot find argon2.wasm32-wasi.wasm file, and @node-rs/argon2-wasm32-wasi package is not installed.')
3939
}
4040
}
4141

42-
const {
43-
instance: __napiInstance,
44-
module: __wasiModule,
45-
napiModule: __napiModule,
46-
} = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
42+
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
4743
context: __emnapiContext,
48-
asyncWorkPoolSize: (function () {
44+
asyncWorkPoolSize: (function() {
4945
const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
5046
// NaN > 0 is false
5147
if (threadsSizeFromEnv > 0) {
@@ -72,7 +68,7 @@ const {
7268
},
7369
beforeInit({ instance }) {
7470
__napi_rs_initialize_modules(instance)
75-
},
71+
}
7672
})
7773

7874
function __napi_rs_initialize_modules(__napiInstance) {

packages/argon2/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ switch (platform) {
298298
}
299299
}
300300
break
301+
case 's390x':
302+
localFileExisted = existsSync(
303+
join(__dirname, 'argon2.linux-s390x-gnu.node')
304+
)
305+
try {
306+
if (localFileExisted) {
307+
nativeBinding = require('./argon2.linux-s390x-gnu.node')
308+
} else {
309+
nativeBinding = require('@node-rs/argon2-linux-s390x-gnu')
310+
}
311+
} catch (e) {
312+
loadError = e
313+
}
314+
break
301315
default:
302316
throw new Error(`Unsupported architecture on Linux: ${arch}`)
303317
}

packages/argon2/npm/wasm32-wasi/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
},
4040
"browser": "argon2.wasi-browser.js",
4141
"dependencies": {
42-
"@emnapi/core": "^0.45.0",
43-
"@emnapi/runtime": "^0.45.0",
44-
"@tybys/wasm-util": "^0.8.1",
45-
"memfs-browser": "^4.6.0"
42+
"@napi-rs/wasm-runtime": "^0.1.1"
4643
}
4744
}

packages/argon2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"version": "napi version"
6363
},
6464
"devDependencies": {
65-
"@napi-rs/cli": "^3.0.0-alpha.34",
65+
"@napi-rs/cli": "^3.0.0-alpha.36",
6666
"argon2": "^0.31.2",
6767
"cross-env": "^7.0.3"
6868
}

packages/argon2/wasi-worker-browser.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
2-
import { WASI } from '@tybys/wasm-util'
3-
import { Volume, createFsFromVolume } from 'memfs-browser'
1+
import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
2+
import { Volume, createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
43

54
const fs = createFsFromVolume(
65
Volume.fromJSON({
@@ -16,7 +15,7 @@ const handler = new MessageHandler({
1615
// eslint-disable-next-line no-console
1716
console.log.apply(console, arguments)
1817
},
19-
printErr: function () {
18+
printErr: function() {
2019
// eslint-disable-next-line no-console
2120
console.error.apply(console, arguments)
2221
},

packages/argon2/wasi-worker.mjs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
import fs from 'node:fs'
2-
import { createRequire } from 'node:module'
3-
import { parentPort, Worker } from 'node:worker_threads'
1+
import fs from "node:fs";
2+
import { createRequire } from "node:module";
3+
import { parentPort, Worker } from "node:worker_threads";
44

5-
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
6-
import { WASI } from '@tybys/wasm-util'
5+
import { instantiateNapiModuleSync, MessageHandler, WASI } from "@napi-rs/wasm-runtime";
76

8-
const require = createRequire(import.meta.url)
7+
const require = createRequire(import.meta.url);
98

109
if (parentPort) {
11-
parentPort.on('message', (data) => {
12-
globalThis.onmessage({ data })
13-
})
10+
parentPort.on("message", (data) => {
11+
globalThis.onmessage({ data });
12+
});
1413
}
1514

1615
Object.assign(globalThis, {
1716
self: globalThis,
1817
require,
1918
Worker,
2019
importScripts: function (f) {
21-
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
20+
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
2221
},
2322
postMessage: function (msg) {
2423
if (parentPort) {
25-
parentPort.postMessage(msg)
24+
parentPort.postMessage(msg);
2625
}
2726
},
28-
})
27+
});
2928

3029
const handler = new MessageHandler({
3130
onLoad({ wasmModule, wasmMemory }) {
32-
const wasi = new WASI({ fs })
31+
const wasi = new WASI({ fs });
3332

3433
return instantiateNapiModuleSync(wasmModule, {
3534
childThread: true,
@@ -39,13 +38,13 @@ const handler = new MessageHandler({
3938
...importObject.env,
4039
...importObject.napi,
4140
...importObject.emnapi,
42-
memory: wasmMemory,
43-
}
41+
memory: wasmMemory
42+
};
4443
},
45-
})
44+
});
4645
},
47-
})
46+
});
4847

4948
globalThis.onmessage = function (e) {
50-
handler.handle(e)
51-
}
49+
handler.handle(e);
50+
};

packages/bcrypt/bcrypt.wasi-browser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core'
2-
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
3-
import { WASI as __WASI } from '@tybys/wasm-util'
4-
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from 'memfs-browser'
1+
import {
2+
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
3+
getDefaultContext as __emnapiGetDefaultContext,
4+
WASI as __WASI,
5+
} from '@napi-rs/wasm-runtime'
6+
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
57

68
import __wasmUrl from './bcrypt.wasm32-wasi.wasm?url'
79

0 commit comments

Comments
 (0)