fix(cli): wasi Node.js worker template (#2015)
This commit is contained in:
parent
f2e5094345
commit
77399ff130
1 changed files with 13 additions and 3 deletions
|
@ -1,11 +1,12 @@
|
||||||
export const WASI_WORKER_TEMPLATE = `import fs from "node:fs";
|
export const WASI_WORKER_TEMPLATE = `import fs from "node:fs";
|
||||||
import { createRequire } from "node:module";
|
import { createRequire } from "node:module";
|
||||||
|
import { WASI } from "node:wasi";
|
||||||
import { parentPort, Worker } from "node:worker_threads";
|
import { parentPort, Worker } from "node:worker_threads";
|
||||||
|
|
||||||
import { instantiateNapiModuleSync, MessageHandler, WASI } from "@napi-rs/wasm-runtime";
|
|
||||||
|
|
||||||
const require = createRequire(import.meta.url);
|
const require = createRequire(import.meta.url);
|
||||||
|
|
||||||
|
const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime");
|
||||||
|
|
||||||
if (parentPort) {
|
if (parentPort) {
|
||||||
parentPort.on("message", (data) => {
|
parentPort.on("message", (data) => {
|
||||||
globalThis.onmessage({ data });
|
globalThis.onmessage({ data });
|
||||||
|
@ -26,13 +27,22 @@ Object.assign(globalThis, {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const emnapiContext = getDefaultContext()
|
||||||
|
|
||||||
const handler = new MessageHandler({
|
const handler = new MessageHandler({
|
||||||
onLoad({ wasmModule, wasmMemory }) {
|
onLoad({ wasmModule, wasmMemory }) {
|
||||||
const wasi = new WASI({ fs });
|
const wasi = new WASI({
|
||||||
|
version: 'preview1',
|
||||||
|
env: process.env,
|
||||||
|
preopens: {
|
||||||
|
'/': '/',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return instantiateNapiModuleSync(wasmModule, {
|
return instantiateNapiModuleSync(wasmModule, {
|
||||||
childThread: true,
|
childThread: true,
|
||||||
wasi,
|
wasi,
|
||||||
|
context: emnapiContext,
|
||||||
overwriteImports(importObject) {
|
overwriteImports(importObject) {
|
||||||
importObject.env = {
|
importObject.env = {
|
||||||
...importObject.env,
|
...importObject.env,
|
||||||
|
|
Loading…
Reference in a new issue