mirror of
https://github.com/napi-rs/node-rs.git
synced 2026-10-28 11:04:33 +08:00
feat: support wasi target on browser (#757)
This commit is contained in:
1 parent
ed4e9aa062
commit
f39eec00c7
37 files changed
+827
-183
No files matched your search
@@ -4,6 +4,9 @@ parserOptions:
|
||||
ecmaVersion: latest
|
||||
sourceType: module
|
||||
|
||||
globals:
|
||||
globalThis: true
|
||||
|
||||
env:
|
||||
browser: true
|
||||
es6: true
|
||||
|
||||
+3
-1
@@ -26,11 +26,12 @@
|
||||
"devDependencies": {
|
||||
"@emnapi/core": "^0.45.0",
|
||||
"@emnapi/runtime": "^0.45.0",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"@swc-node/core": "^1.10.6",
|
||||
"@swc-node/register": "^1.6.8",
|
||||
"@swc/core": "^1.3.101",
|
||||
"@taplo/cli": "^0.5.2",
|
||||
"@tybys/wasm-util": "^0.8.1",
|
||||
"@types/node": "^20.10.5",
|
||||
"@typescript-eslint/eslint-plugin": "^6.16.0",
|
||||
"@typescript-eslint/parser": "^6.16.0",
|
||||
@@ -45,6 +46,7 @@
|
||||
"husky": "^8.0.3",
|
||||
"lerna": "^8.0.1",
|
||||
"lint-staged": "^15.2.0",
|
||||
"memfs-browser": "^3.4.13000",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^3.1.1",
|
||||
"ts-node": "^10.9.2",
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
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 __wasmUrl from './argon2.wasm32-wasi.wasm?url'
|
||||
|
||||
const __fs = __createFsFromVolume(
|
||||
__Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const __wasi = new __WASI({
|
||||
version: 'preview1',
|
||||
fs: __fs,
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
|
||||
const __sharedMemory = new WebAssembly.Memory({
|
||||
initial: 1024,
|
||||
maximum: 10240,
|
||||
shared: true,
|
||||
})
|
||||
|
||||
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
||||
|
||||
const {
|
||||
instance: __napiInstance,
|
||||
module: __wasiModule,
|
||||
napiModule: __napiModule,
|
||||
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
||||
context: __emnapiContext,
|
||||
asyncWorkPoolSize: 4,
|
||||
wasi: __wasi,
|
||||
onCreateWorker() {
|
||||
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
|
||||
type: 'module',
|
||||
})
|
||||
},
|
||||
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']?.()
|
||||
}
|
||||
export const Algorithm = __napiModule.exports.Algorithm
|
||||
export const hash = __napiModule.exports.hash
|
||||
export const hashRaw = __napiModule.exports.hashRaw
|
||||
export const hashRawSync = __napiModule.exports.hashRawSync
|
||||
export const hashSync = __napiModule.exports.hashSync
|
||||
export const verify = __napiModule.exports.verify
|
||||
export const verifySync = __napiModule.exports.verifySync
|
||||
export const Version = __napiModule.exports.Version
|
||||
@@ -15,8 +15,8 @@ const __wasi = new __nodeWASI({
|
||||
version: 'preview1',
|
||||
env: process.env,
|
||||
preopens: {
|
||||
'/': '/'
|
||||
}
|
||||
'/': '/',
|
||||
},
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
@@ -33,13 +33,19 @@ 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) {
|
||||
@@ -66,7 +72,7 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
|
||||
},
|
||||
beforeInit({ instance }) {
|
||||
__napi_rs_initialize_modules(instance)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
function __napi_rs_initialize_modules(__napiInstance) {
|
||||
@@ -83,11 +89,11 @@ function __napi_rs_initialize_modules(__napiInstance) {
|
||||
__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.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
|
||||
@@ -8,7 +8,9 @@
|
||||
"files": [
|
||||
"argon2.wasm32-wasi.wasm",
|
||||
"argon2.wasi.cjs",
|
||||
"wasi-worker.mjs"
|
||||
"argon2.wasi-browser.js",
|
||||
"wasi-worker.mjs",
|
||||
"wasi-worker-browser.mjs"
|
||||
],
|
||||
"description": "RustCrypto: Argon2 binding for Node.js",
|
||||
"keywords": [
|
||||
@@ -35,8 +37,11 @@
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/napi-rs/node-rs.git"
|
||||
},
|
||||
"browser": "argon2.wasi-browser.js",
|
||||
"dependencies": {
|
||||
"@emnapi/core": "^0.45.0",
|
||||
"@emnapi/runtime": "^0.45.0"
|
||||
"@emnapi/runtime": "^0.45.0",
|
||||
"@tybys/wasm-util": "^0.8.1",
|
||||
"memfs-browser": "^3.4.13000"
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,7 @@
|
||||
"version": "napi version"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"argon2": "^0.31.2",
|
||||
"cross-env": "^7.0.3"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
import { Volume, createFsFromVolume } from 'memfs-browser'
|
||||
|
||||
const fs = createFsFromVolume(
|
||||
Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({
|
||||
fs,
|
||||
print: function () {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log.apply(console, arguments)
|
||||
},
|
||||
})
|
||||
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)
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
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 } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
|
||||
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, {
|
||||
@@ -18,18 +18,18 @@ Object.assign(globalThis, {
|
||||
require,
|
||||
Worker,
|
||||
importScripts: function (f) {
|
||||
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
|
||||
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
||||
},
|
||||
postMessage: function (msg) {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(msg);
|
||||
parentPort.postMessage(msg)
|
||||
}
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({ fs });
|
||||
const wasi = new WASI({ fs })
|
||||
|
||||
return instantiateNapiModuleSync(wasmModule, {
|
||||
childThread: true,
|
||||
@@ -39,13 +39,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)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
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 __wasmUrl from './bcrypt.wasm32-wasi.wasm?url'
|
||||
|
||||
const __fs = __createFsFromVolume(
|
||||
__Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const __wasi = new __WASI({
|
||||
version: 'preview1',
|
||||
fs: __fs,
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
|
||||
const __sharedMemory = new WebAssembly.Memory({
|
||||
initial: 1024,
|
||||
maximum: 10240,
|
||||
shared: true,
|
||||
})
|
||||
|
||||
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
||||
|
||||
const {
|
||||
instance: __napiInstance,
|
||||
module: __wasiModule,
|
||||
napiModule: __napiModule,
|
||||
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
||||
context: __emnapiContext,
|
||||
asyncWorkPoolSize: 4,
|
||||
wasi: __wasi,
|
||||
onCreateWorker() {
|
||||
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
|
||||
type: 'module',
|
||||
})
|
||||
},
|
||||
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']?.()
|
||||
}
|
||||
export const DEFAULT_COST = __napiModule.exports.DEFAULT_COST
|
||||
export const genSalt = __napiModule.exports.genSalt
|
||||
export const genSaltSync = __napiModule.exports.genSaltSync
|
||||
export const hash = __napiModule.exports.hash
|
||||
export const hashSync = __napiModule.exports.hashSync
|
||||
export const verify = __napiModule.exports.verify
|
||||
export const verifySync = __napiModule.exports.verifySync
|
||||
@@ -15,8 +15,8 @@ const __wasi = new __nodeWASI({
|
||||
version: 'preview1',
|
||||
env: process.env,
|
||||
preopens: {
|
||||
'/': '/'
|
||||
}
|
||||
'/': '/',
|
||||
},
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
@@ -33,13 +33,19 @@ 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) {
|
||||
@@ -66,7 +72,7 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
|
||||
},
|
||||
beforeInit({ instance }) {
|
||||
__napi_rs_initialize_modules(instance)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
function __napi_rs_initialize_modules(__napiInstance) {
|
||||
@@ -81,10 +87,10 @@ function __napi_rs_initialize_modules(__napiInstance) {
|
||||
__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.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
|
||||
@@ -8,7 +8,9 @@
|
||||
"files": [
|
||||
"bcrypt.wasm32-wasi.wasm",
|
||||
"bcrypt.wasi.cjs",
|
||||
"wasi-worker.mjs"
|
||||
"bcrypt.wasi-browser.js",
|
||||
"wasi-worker.mjs",
|
||||
"wasi-worker-browser.mjs"
|
||||
],
|
||||
"description": "Rust bcrypt binding",
|
||||
"keywords": [
|
||||
@@ -39,8 +41,11 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"browser": "bcrypt.wasi-browser.js",
|
||||
"dependencies": {
|
||||
"@emnapi/core": "^0.45.0",
|
||||
"@emnapi/runtime": "^0.45.0"
|
||||
"@emnapi/runtime": "^0.45.0",
|
||||
"@tybys/wasm-util": "^0.8.1",
|
||||
"memfs-browser": "^3.4.13000"
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"@types/bcrypt": "^5.0.2",
|
||||
"bcrypt": "^5.1.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
import { Volume, createFsFromVolume } from 'memfs-browser'
|
||||
|
||||
const fs = createFsFromVolume(
|
||||
Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({
|
||||
fs,
|
||||
print: function () {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log.apply(console, arguments)
|
||||
},
|
||||
})
|
||||
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)
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
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 } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
|
||||
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, {
|
||||
@@ -18,18 +18,18 @@ Object.assign(globalThis, {
|
||||
require,
|
||||
Worker,
|
||||
importScripts: function (f) {
|
||||
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
|
||||
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
||||
},
|
||||
postMessage: function (msg) {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(msg);
|
||||
parentPort.postMessage(msg)
|
||||
}
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({ fs });
|
||||
const wasi = new WASI({ fs })
|
||||
|
||||
return instantiateNapiModuleSync(wasmModule, {
|
||||
childThread: true,
|
||||
@@ -39,13 +39,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)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
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 __wasmUrl from './crc32.wasm32-wasi.wasm?url'
|
||||
|
||||
const __fs = __createFsFromVolume(
|
||||
__Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const __wasi = new __WASI({
|
||||
version: 'preview1',
|
||||
fs: __fs,
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
|
||||
const __sharedMemory = new WebAssembly.Memory({
|
||||
initial: 1024,
|
||||
maximum: 10240,
|
||||
shared: true,
|
||||
})
|
||||
|
||||
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
||||
|
||||
const {
|
||||
instance: __napiInstance,
|
||||
module: __wasiModule,
|
||||
napiModule: __napiModule,
|
||||
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
||||
context: __emnapiContext,
|
||||
asyncWorkPoolSize: 4,
|
||||
wasi: __wasi,
|
||||
onCreateWorker() {
|
||||
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
|
||||
type: 'module',
|
||||
})
|
||||
},
|
||||
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']?.()
|
||||
}
|
||||
export const crc32 = __napiModule.exports.crc32
|
||||
export const crc32c = __napiModule.exports.crc32c
|
||||
@@ -15,8 +15,8 @@ const __wasi = new __nodeWASI({
|
||||
version: 'preview1',
|
||||
env: process.env,
|
||||
preopens: {
|
||||
'/': '/'
|
||||
}
|
||||
'/': '/',
|
||||
},
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
@@ -37,9 +37,13 @@ if (!__nodeFs.existsSync(__wasmFilePath)) {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -66,12 +70,12 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
|
||||
},
|
||||
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.crc32 = __napiModule.exports.crc32
|
||||
module.exports.crc32c = __napiModule.exports.crc32c
|
||||
@@ -8,7 +8,9 @@
|
||||
"files": [
|
||||
"crc32.wasm32-wasi.wasm",
|
||||
"crc32.wasi.cjs",
|
||||
"wasi-worker.mjs"
|
||||
"crc32.wasi-browser.js",
|
||||
"wasi-worker.mjs",
|
||||
"wasi-worker-browser.mjs"
|
||||
],
|
||||
"description": "SIMD crc32",
|
||||
"keywords": [
|
||||
@@ -36,8 +38,11 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"browser": "crc32.wasi-browser.js",
|
||||
"dependencies": {
|
||||
"@emnapi/core": "^0.45.0",
|
||||
"@emnapi/runtime": "^0.45.0"
|
||||
"@emnapi/runtime": "^0.45.0",
|
||||
"@tybys/wasm-util": "^0.8.1",
|
||||
"memfs-browser": "^3.4.13000"
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"@types/crc": "^3.8.3",
|
||||
"buffer": "^6.0.3",
|
||||
"crc": "^4.3.2",
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
import { Volume, createFsFromVolume } from 'memfs-browser'
|
||||
|
||||
const fs = createFsFromVolume(
|
||||
Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({
|
||||
fs,
|
||||
print: function () {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log.apply(console, arguments)
|
||||
},
|
||||
})
|
||||
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)
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
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 } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
|
||||
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, {
|
||||
@@ -18,18 +18,18 @@ Object.assign(globalThis, {
|
||||
require,
|
||||
Worker,
|
||||
importScripts: function (f) {
|
||||
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
|
||||
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
||||
},
|
||||
postMessage: function (msg) {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(msg);
|
||||
parentPort.postMessage(msg)
|
||||
}
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({ fs });
|
||||
const wasi = new WASI({ fs })
|
||||
|
||||
return instantiateNapiModuleSync(wasmModule, {
|
||||
childThread: true,
|
||||
@@ -39,13 +39,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)
|
||||
}
|
||||
@@ -312,6 +312,13 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
if (!nativeBinding) {
|
||||
try {
|
||||
nativeBinding = require('@node-rs/deno-lint-wasm32-wasi')
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!nativeBinding) {
|
||||
|
||||
@@ -73,7 +73,7 @@
|
||||
"typanion": "^3.14.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"@types/webpack": "^5.28.5"
|
||||
},
|
||||
"funding": {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
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 __wasmUrl from './jieba.wasm32-wasi.wasm?url'
|
||||
|
||||
const __fs = __createFsFromVolume(
|
||||
__Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const __wasi = new __WASI({
|
||||
version: 'preview1',
|
||||
fs: __fs,
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
|
||||
const __sharedMemory = new WebAssembly.Memory({
|
||||
initial: 1024,
|
||||
maximum: 10240,
|
||||
shared: true,
|
||||
})
|
||||
|
||||
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
||||
|
||||
const {
|
||||
instance: __napiInstance,
|
||||
module: __wasiModule,
|
||||
napiModule: __napiModule,
|
||||
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
||||
context: __emnapiContext,
|
||||
asyncWorkPoolSize: 4,
|
||||
wasi: __wasi,
|
||||
onCreateWorker() {
|
||||
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
|
||||
type: 'module',
|
||||
})
|
||||
},
|
||||
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']?.()
|
||||
}
|
||||
export const cut = __napiModule.exports.cut
|
||||
export const cutAll = __napiModule.exports.cutAll
|
||||
export const cutForSearch = __napiModule.exports.cutForSearch
|
||||
export const extract = __napiModule.exports.extract
|
||||
export const load = __napiModule.exports.load
|
||||
export const loadDict = __napiModule.exports.loadDict
|
||||
export const loadTFIDFDict = __napiModule.exports.loadTFIDFDict
|
||||
export const tag = __napiModule.exports.tag
|
||||
@@ -15,8 +15,8 @@ const __wasi = new __nodeWASI({
|
||||
version: 'preview1',
|
||||
env: process.env,
|
||||
preopens: {
|
||||
'/': '/'
|
||||
}
|
||||
'/': '/',
|
||||
},
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
@@ -37,9 +37,13 @@ if (!__nodeFs.existsSync(__wasmFilePath)) {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -66,7 +70,7 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
|
||||
},
|
||||
beforeInit({ instance }) {
|
||||
__napi_rs_initialize_modules(instance)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
function __napi_rs_initialize_modules(__napiInstance) {
|
||||
@@ -81,11 +85,11 @@ function __napi_rs_initialize_modules(__napiInstance) {
|
||||
__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.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
|
||||
@@ -8,7 +8,9 @@
|
||||
"files": [
|
||||
"jieba.wasm32-wasi.wasm",
|
||||
"jieba.wasi.cjs",
|
||||
"wasi-worker.mjs"
|
||||
"jieba.wasi-browser.js",
|
||||
"wasi-worker.mjs",
|
||||
"wasi-worker-browser.mjs"
|
||||
],
|
||||
"description": "Fastest Chinese word segmentation in Node.js",
|
||||
"keywords": [
|
||||
@@ -36,8 +38,11 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"browser": "jieba.wasi-browser.js",
|
||||
"dependencies": {
|
||||
"@emnapi/core": "^0.45.0",
|
||||
"@emnapi/runtime": "^0.45.0"
|
||||
"@emnapi/runtime": "^0.45.0",
|
||||
"@tybys/wasm-util": "^0.8.1",
|
||||
"memfs-browser": "^3.4.13000"
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"nodejieba": "^2.6.0"
|
||||
},
|
||||
"funding": {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
import { Volume, createFsFromVolume } from 'memfs-browser'
|
||||
|
||||
const fs = createFsFromVolume(
|
||||
Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({
|
||||
fs,
|
||||
print: function () {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log.apply(console, arguments)
|
||||
},
|
||||
})
|
||||
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)
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
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 } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
|
||||
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, {
|
||||
@@ -18,18 +18,18 @@ Object.assign(globalThis, {
|
||||
require,
|
||||
Worker,
|
||||
importScripts: function (f) {
|
||||
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
|
||||
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
||||
},
|
||||
postMessage: function (msg) {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(msg);
|
||||
parentPort.postMessage(msg)
|
||||
}
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({ fs });
|
||||
const wasi = new WASI({ fs })
|
||||
|
||||
return instantiateNapiModuleSync(wasmModule, {
|
||||
childThread: true,
|
||||
@@ -39,13 +39,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)
|
||||
}
|
||||
@@ -312,6 +312,13 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
if (!nativeBinding) {
|
||||
try {
|
||||
nativeBinding = require('@node-rs/jsonwebtoken-wasm32-wasi')
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!nativeBinding) {
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
"node": ">= 10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"@types/jsonwebtoken": "^9.0.5",
|
||||
"jsonwebtoken": "^9.0.2"
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
"files": [
|
||||
"xxhash.wasm32-wasi.wasm",
|
||||
"xxhash.wasi.cjs",
|
||||
"wasi-worker.mjs"
|
||||
"xxhash.wasi-browser.js",
|
||||
"wasi-worker.mjs",
|
||||
"wasi-worker-browser.mjs"
|
||||
],
|
||||
"description": "Fastest xxhash implementation in Node.js",
|
||||
"keywords": [
|
||||
@@ -39,8 +41,11 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"browser": "xxhash.wasi-browser.js",
|
||||
"dependencies": {
|
||||
"@emnapi/core": "^0.45.0",
|
||||
"@emnapi/runtime": "^0.45.0"
|
||||
"@emnapi/runtime": "^0.45.0",
|
||||
"@tybys/wasm-util": "^0.8.1",
|
||||
"memfs-browser": "^3.4.13000"
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@
|
||||
"url": "https://github.com/napi-rs/node-rs/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^3.0.0-alpha.30",
|
||||
"@napi-rs/cli": "^3.0.0-alpha.33",
|
||||
"@types/xxhashjs": "^0.2.4",
|
||||
"webpack": "^5.89.0",
|
||||
"xxhash": "^0.3.0",
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { instantiateNapiModuleSync, MessageHandler } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
import { Volume, createFsFromVolume } from 'memfs-browser'
|
||||
|
||||
const fs = createFsFromVolume(
|
||||
Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({
|
||||
fs,
|
||||
print: function () {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log.apply(console, arguments)
|
||||
},
|
||||
})
|
||||
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)
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
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 } from '@emnapi/core'
|
||||
import { WASI } from '@tybys/wasm-util'
|
||||
|
||||
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, {
|
||||
@@ -18,18 +18,18 @@ Object.assign(globalThis, {
|
||||
require,
|
||||
Worker,
|
||||
importScripts: function (f) {
|
||||
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
|
||||
;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
|
||||
},
|
||||
postMessage: function (msg) {
|
||||
if (parentPort) {
|
||||
parentPort.postMessage(msg);
|
||||
parentPort.postMessage(msg)
|
||||
}
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
const handler = new MessageHandler({
|
||||
onLoad({ wasmModule, wasmMemory }) {
|
||||
const wasi = new WASI({ fs });
|
||||
const wasi = new WASI({ fs })
|
||||
|
||||
return instantiateNapiModuleSync(wasmModule, {
|
||||
childThread: true,
|
||||
@@ -39,13 +39,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)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
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 __wasmUrl from './xxhash.wasm32-wasi.wasm?url'
|
||||
|
||||
const __fs = __createFsFromVolume(
|
||||
__Volume.fromJSON({
|
||||
'/': null,
|
||||
}),
|
||||
)
|
||||
|
||||
const __wasi = new __WASI({
|
||||
version: 'preview1',
|
||||
fs: __fs,
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
|
||||
const __sharedMemory = new WebAssembly.Memory({
|
||||
initial: 1024,
|
||||
maximum: 10240,
|
||||
shared: true,
|
||||
})
|
||||
|
||||
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
||||
|
||||
const {
|
||||
instance: __napiInstance,
|
||||
module: __wasiModule,
|
||||
napiModule: __napiModule,
|
||||
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
||||
context: __emnapiContext,
|
||||
asyncWorkPoolSize: 4,
|
||||
wasi: __wasi,
|
||||
onCreateWorker() {
|
||||
return new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
|
||||
type: 'module',
|
||||
})
|
||||
},
|
||||
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']?.()
|
||||
}
|
||||
export const Xxh32 = __napiModule.exports.Xxh32
|
||||
export const Xxh64 = __napiModule.exports.Xxh64
|
||||
export const xxh32 = __napiModule.exports.xxh32
|
||||
export const xxh64 = __napiModule.exports.xxh64
|
||||
export const xxh3 = __napiModule.exports.xxh3
|
||||
@@ -15,8 +15,8 @@ const __wasi = new __nodeWASI({
|
||||
version: 'preview1',
|
||||
env: process.env,
|
||||
preopens: {
|
||||
'/': '/'
|
||||
}
|
||||
'/': '/',
|
||||
},
|
||||
})
|
||||
|
||||
const __emnapiContext = __emnapiGetDefaultContext()
|
||||
@@ -33,13 +33,19 @@ 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.')
|
||||
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), {
|
||||
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) {
|
||||
@@ -66,7 +72,7 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
|
||||
},
|
||||
beforeInit({ instance }) {
|
||||
__napi_rs_initialize_modules(instance)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
function __napi_rs_initialize_modules(__napiInstance) {
|
||||
@@ -83,8 +89,8 @@ function __napi_rs_initialize_modules(__napiInstance) {
|
||||
__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.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
|
||||
@@ -326,13 +326,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@napi-rs/cli@npm:^3.0.0-alpha.30":
|
||||
version: 3.0.0-alpha.30
|
||||
resolution: "@napi-rs/cli@npm:3.0.0-alpha.30"
|
||||
"@napi-rs/cli@npm:^3.0.0-alpha.33":
|
||||
version: 3.0.0-alpha.33
|
||||
resolution: "@napi-rs/cli@npm:3.0.0-alpha.33"
|
||||
dependencies:
|
||||
"@napi-rs/cross-toolchain": "npm:^0.0.14"
|
||||
"@octokit/rest": "npm:^20.0.2"
|
||||
"@tybys/wasm-util": "npm:0.8.0"
|
||||
"@tybys/wasm-util": "npm:0.8.1"
|
||||
clipanion: "npm:^3.2.1"
|
||||
colorette: "npm:^2.0.20"
|
||||
debug: "npm:^4.3.4"
|
||||
@@ -357,7 +357,7 @@ __metadata:
|
||||
bin:
|
||||
napi: dist/cli.js
|
||||
napi-raw: cli.mjs
|
||||
checksum: e06b7273977a1bb48ae1b45c52370a2e3f34577db2161c9886ed846430aca0c1a53098f76c7d0c89c38a0d44dbc7d56c64e678f39d043fbe27d6906775b72457
|
||||
checksum: 7cd93b36f820f2157fa22a0300f148370782190530f53ce40129fe909acd2cd7cc4e49a85d73efb1946947dd5921c4e2a995f349c90f2a8d424da43f15226ef0
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -681,7 +681,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@node-rs/argon2@workspace:packages/argon2"
|
||||
dependencies:
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
argon2: "npm:^0.31.2"
|
||||
cross-env: "npm:^7.0.3"
|
||||
languageName: unknown
|
||||
@@ -691,7 +691,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@node-rs/bcrypt@workspace:packages/bcrypt"
|
||||
dependencies:
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
"@types/bcrypt": "npm:^5.0.2"
|
||||
bcrypt: "npm:^5.1.1"
|
||||
bcryptjs: "npm:^2.4.3"
|
||||
@@ -703,7 +703,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@node-rs/crc32@workspace:packages/crc32"
|
||||
dependencies:
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
"@types/crc": "npm:^3.8.3"
|
||||
buffer: "npm:^6.0.3"
|
||||
crc: "npm:^4.3.2"
|
||||
@@ -715,7 +715,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.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
"@types/webpack": "npm:^5.28.5"
|
||||
clipanion: "npm:^3.2.1"
|
||||
typanion: "npm:^3.14.0"
|
||||
@@ -736,7 +736,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@node-rs/jieba@workspace:packages/jieba"
|
||||
dependencies:
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
nodejieba: "npm:^2.6.0"
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
@@ -745,7 +745,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@node-rs/jsonwebtoken@workspace:packages/jsonwebtoken"
|
||||
dependencies:
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
"@types/jsonwebtoken": "npm:^9.0.5"
|
||||
jsonwebtoken: "npm:^9.0.2"
|
||||
languageName: unknown
|
||||
@@ -755,7 +755,7 @@ __metadata:
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@node-rs/xxhash@workspace:packages/xxhash"
|
||||
dependencies:
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
"@types/xxhashjs": "npm:^0.2.4"
|
||||
webpack: "npm:^5.89.0"
|
||||
xxhash: "npm:^0.3.0"
|
||||
@@ -1617,12 +1617,12 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@tybys/wasm-util@npm:0.8.0":
|
||||
version: 0.8.0
|
||||
resolution: "@tybys/wasm-util@npm:0.8.0"
|
||||
"@tybys/wasm-util@npm:0.8.1, @tybys/wasm-util@npm:^0.8.1":
|
||||
version: 0.8.1
|
||||
resolution: "@tybys/wasm-util@npm:0.8.1"
|
||||
dependencies:
|
||||
tslib: "npm:^2.4.0"
|
||||
checksum: e3f0c03a83d668deeed07cc8cb7e0f3deec0363402e83fa30b2690671eb5f72db8feec2bab1d04e1f4998bb94b051f546e23ea306657cfc39cbe3229014b9223
|
||||
checksum: 4e0975ee0ed8202e4af0be45a379ac11fa98e8c17cb5a038bc1d29f6e1bd37eb84be991d725d214d0f257d4aaccd67e5ac6e68c3c81cdd3c7f3f678d05d52cb7
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -4462,6 +4462,13 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"fs-monkey@npm:^1.0.3":
|
||||
version: 1.0.5
|
||||
resolution: "fs-monkey@npm:1.0.5"
|
||||
checksum: 815025e75549fb1ac6c403413b82fd631eded862ae27694a515c0f666069e95874ab34e79c33d1b3b8c87d1e54350d5e4262090d0aa5bd7130143cbc627537e4
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"fs.realpath@npm:^1.0.0":
|
||||
version: 1.0.0
|
||||
resolution: "fs.realpath@npm:1.0.0"
|
||||
@@ -6357,6 +6364,25 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"memfs-browser@npm:^3.4.13000":
|
||||
version: 3.4.13000
|
||||
resolution: "memfs-browser@npm:3.4.13000"
|
||||
dependencies:
|
||||
buffer: "npm:^6.0.3"
|
||||
memfs: "npm:3.4.13"
|
||||
checksum: 580fa76a69638b4f1e3e9f36db4ece7de3689427cc9519c567e5e6413e2106390892a63acf19675cc0908eafbb7f991ebf5425fede19ebd8b1fe0880e8b16cba
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"memfs@npm:3.4.13":
|
||||
version: 3.4.13
|
||||
resolution: "memfs@npm:3.4.13"
|
||||
dependencies:
|
||||
fs-monkey: "npm:^1.0.3"
|
||||
checksum: f14ab3ff938eacf688577d1b0f7bf77ca3a05d4df9c335b024ed6790e6b224b569cc4b61c1de604c0420a0fac6b3fbf3f283c72fd2be9ce395534539599ac63b
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"memoize@npm:^10.0.0":
|
||||
version: 10.0.0
|
||||
resolution: "memoize@npm:10.0.0"
|
||||
@@ -6842,11 +6868,12 @@ __metadata:
|
||||
dependencies:
|
||||
"@emnapi/core": "npm:^0.45.0"
|
||||
"@emnapi/runtime": "npm:^0.45.0"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.30"
|
||||
"@napi-rs/cli": "npm:^3.0.0-alpha.33"
|
||||
"@swc-node/core": "npm:^1.10.6"
|
||||
"@swc-node/register": "npm:^1.6.8"
|
||||
"@swc/core": "npm:^1.3.101"
|
||||
"@taplo/cli": "npm:^0.5.2"
|
||||
"@tybys/wasm-util": "npm:^0.8.1"
|
||||
"@types/node": "npm:^20.10.5"
|
||||
"@typescript-eslint/eslint-plugin": "npm:^6.16.0"
|
||||
"@typescript-eslint/parser": "npm:^6.16.0"
|
||||
@@ -6861,6 +6888,7 @@ __metadata:
|
||||
husky: "npm:^8.0.3"
|
||||
lerna: "npm:^8.0.1"
|
||||
lint-staged: "npm:^15.2.0"
|
||||
memfs-browser: "npm:^3.4.13000"
|
||||
npm-run-all: "npm:^4.1.5"
|
||||
prettier: "npm:^3.1.1"
|
||||
ts-node: "npm:^10.9.2"
|
||||
|
||||
Reference in new issue
Block a user