Skip to content

Commit

Permalink
fix(*): wasi browser compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jan 17, 2024
1 parent 4928afe commit 3ec8863
Show file tree
Hide file tree
Showing 40 changed files with 290 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node: ['18', '20']
node: ['20']
runs-on: ubuntu-latest

steps:
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
"postinstall": "husky install"
},
"devDependencies": {
"@emnapi/core": "^0.45.0",
"@emnapi/runtime": "^0.45.0",
"@napi-rs/cli": "^3.0.0-alpha.34",
"@napi-rs/cli": "^3.0.0-alpha.36",
"@napi-rs/wasm-runtime": "^0.1.1",
"@swc-node/core": "^1.10.6",
"@swc-node/register": "^1.6.8",
"@swc/core": "^1.3.101",
Expand Down
10 changes: 6 additions & 4 deletions packages/argon2/argon2.wasi-browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core'
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
import { WASI as __WASI } from '@tybys/wasm-util'
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from 'memfs-browser'
import {
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
getDefaultContext as __emnapiGetDefaultContext,
WASI as __WASI,
} from '@napi-rs/wasm-runtime'
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from '@napi-rs/wasm-runtime/fs'

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

Expand Down
24 changes: 10 additions & 14 deletions packages/argon2/argon2.wasi.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ 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 {
instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
getDefaultContext: __emnapiGetDefaultContext,
} = require('@napi-rs/wasm-runtime')

const __wasi = new __nodeWASI({
version: 'preview1',
env: process.env,
preopens: {
'/': '/',
},
'/': '/'
}
})

const __emnapiContext = __emnapiGetDefaultContext()
Expand All @@ -33,19 +35,13 @@ 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.',
)
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), {
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
context: __emnapiContext,
asyncWorkPoolSize: (function () {
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) {
Expand All @@ -72,7 +68,7 @@ const {
},
beforeInit({ instance }) {
__napi_rs_initialize_modules(instance)
},
}
})

function __napi_rs_initialize_modules(__napiInstance) {
Expand Down
14 changes: 14 additions & 0 deletions packages/argon2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ switch (platform) {
}
}
break
case 's390x':
localFileExisted = existsSync(
join(__dirname, 'argon2.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./argon2.linux-s390x-gnu.node')
} else {
nativeBinding = require('@node-rs/argon2-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
Expand Down
5 changes: 1 addition & 4 deletions packages/argon2/npm/wasm32-wasi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
},
"browser": "argon2.wasi-browser.js",
"dependencies": {
"@emnapi/core": "^0.45.0",
"@emnapi/runtime": "^0.45.0",
"@tybys/wasm-util": "^0.8.1",
"memfs-browser": "^4.6.0"
"@napi-rs/wasm-runtime": "^0.1.1"
}
}
2 changes: 1 addition & 1 deletion packages/argon2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"version": "napi version"
},
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.34",
"@napi-rs/cli": "^3.0.0-alpha.36",
"argon2": "^0.31.2",
"cross-env": "^7.0.3"
}
Expand Down
7 changes: 3 additions & 4 deletions packages/argon2/wasi-worker-browser.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
import { WASI } from '@tybys/wasm-util'
import { Volume, createFsFromVolume } from 'memfs-browser'
import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
import { Volume, createFsFromVolume } from '@napi-rs/wasm-runtime/fs'

const fs = createFsFromVolume(
Volume.fromJSON({
Expand All @@ -16,7 +15,7 @@ const handler = new MessageHandler({
// eslint-disable-next-line no-console
console.log.apply(console, arguments)
},
printErr: function () {
printErr: function() {
// eslint-disable-next-line no-console
console.error.apply(console, arguments)
},
Expand Down
37 changes: 18 additions & 19 deletions packages/argon2/wasi-worker.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import fs from 'node:fs'
import { createRequire } from 'node:module'
import { parentPort, Worker } from 'node:worker_threads'
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'
import { instantiateNapiModuleSync, MessageHandler, WASI } from "@napi-rs/wasm-runtime";

const require = createRequire(import.meta.url)
const require = createRequire(import.meta.url);

if (parentPort) {
parentPort.on('message', (data) => {
globalThis.onmessage({ data })
})
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)
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
},
postMessage: function (msg) {
if (parentPort) {
parentPort.postMessage(msg)
parentPort.postMessage(msg);
}
},
})
});

const handler = new MessageHandler({
onLoad({ wasmModule, wasmMemory }) {
const wasi = new WASI({ fs })
const wasi = new WASI({ fs });

return instantiateNapiModuleSync(wasmModule, {
childThread: true,
Expand All @@ -39,13 +38,13 @@ const handler = new MessageHandler({
...importObject.env,
...importObject.napi,
...importObject.emnapi,
memory: wasmMemory,
}
memory: wasmMemory
};
},
})
});
},
})
});

globalThis.onmessage = function (e) {
handler.handle(e)
}
handler.handle(e);
};
10 changes: 6 additions & 4 deletions packages/bcrypt/bcrypt.wasi-browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync } from '@emnapi/core'
import { getDefaultContext as __emnapiGetDefaultContext } from '@emnapi/runtime'
import { WASI as __WASI } from '@tybys/wasm-util'
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from 'memfs-browser'
import {
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
getDefaultContext as __emnapiGetDefaultContext,
WASI as __WASI,
} from '@napi-rs/wasm-runtime'
import { Volume as __Volume, createFsFromVolume as __createFsFromVolume } from '@napi-rs/wasm-runtime/fs'

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

Expand Down
24 changes: 10 additions & 14 deletions packages/bcrypt/bcrypt.wasi.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ 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 {
instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
getDefaultContext: __emnapiGetDefaultContext,
} = require('@napi-rs/wasm-runtime')

const __wasi = new __nodeWASI({
version: 'preview1',
env: process.env,
preopens: {
'/': '/',
},
'/': '/'
}
})

const __emnapiContext = __emnapiGetDefaultContext()
Expand All @@ -33,19 +35,13 @@ 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.',
)
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), {
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
context: __emnapiContext,
asyncWorkPoolSize: (function () {
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) {
Expand All @@ -72,7 +68,7 @@ const {
},
beforeInit({ instance }) {
__napi_rs_initialize_modules(instance)
},
}
})

function __napi_rs_initialize_modules(__napiInstance) {
Expand Down
12 changes: 12 additions & 0 deletions packages/bcrypt/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ switch (platform) {
}
}
break
case 's390x':
localFileExisted = existsSync(join(__dirname, 'bcrypt.linux-s390x-gnu.node'))
try {
if (localFileExisted) {
nativeBinding = require('./bcrypt.linux-s390x-gnu.node')
} else {
nativeBinding = require('@node-rs/bcrypt-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
Expand Down
5 changes: 1 addition & 4 deletions packages/bcrypt/npm/wasm32-wasi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
},
"browser": "bcrypt.wasi-browser.js",
"dependencies": {
"@emnapi/core": "^0.45.0",
"@emnapi/runtime": "^0.45.0",
"@tybys/wasm-util": "^0.8.1",
"memfs-browser": "^4.6.0"
"@napi-rs/wasm-runtime": "^0.1.1"
}
}
2 changes: 1 addition & 1 deletion packages/bcrypt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"url": "https://github.com/napi-rs/node-rs/issues"
},
"devDependencies": {
"@napi-rs/cli": "^3.0.0-alpha.34",
"@napi-rs/cli": "^3.0.0-alpha.36",
"@types/bcrypt": "^5.0.2",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
Expand Down
7 changes: 3 additions & 4 deletions packages/bcrypt/wasi-worker-browser.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
import { WASI } from '@tybys/wasm-util'
import { Volume, createFsFromVolume } from 'memfs-browser'
import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
import { Volume, createFsFromVolume } from '@napi-rs/wasm-runtime/fs'

const fs = createFsFromVolume(
Volume.fromJSON({
Expand All @@ -16,7 +15,7 @@ const handler = new MessageHandler({
// eslint-disable-next-line no-console
console.log.apply(console, arguments)
},
printErr: function () {
printErr: function() {
// eslint-disable-next-line no-console
console.error.apply(console, arguments)
},
Expand Down

0 comments on commit 3ec8863

Please sign in to comment.