diff --git a/cli/src/api/templates/wasi-worker-template.ts b/cli/src/api/templates/wasi-worker-template.ts index a69d26d8..2ff41b44 100644 --- a/cli/src/api/templates/wasi-worker-template.ts +++ b/cli/src/api/templates/wasi-worker-template.ts @@ -1,11 +1,12 @@ export const WASI_WORKER_TEMPLATE = `import fs from "node:fs"; import { createRequire } from "node:module"; +import { WASI } from "node:wasi"; import { parentPort, Worker } from "node:worker_threads"; -import { instantiateNapiModuleSync, MessageHandler, WASI } from "@napi-rs/wasm-runtime"; - const require = createRequire(import.meta.url); +const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime"); + if (parentPort) { parentPort.on("message", (data) => { globalThis.onmessage({ data }); @@ -26,13 +27,22 @@ Object.assign(globalThis, { }, }); +const emnapiContext = getDefaultContext() + const handler = new MessageHandler({ onLoad({ wasmModule, wasmMemory }) { - const wasi = new WASI({ fs }); + const wasi = new WASI({ + version: 'preview1', + env: process.env, + preopens: { + '/': '/', + }, + }); return instantiateNapiModuleSync(wasmModule, { childThread: true, wasi, + context: emnapiContext, overwriteImports(importObject) { importObject.env = { ...importObject.env,