From 77399ff13057c9f776c18cdcc671164084a96fa5 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 28 Mar 2024 10:08:17 +0800 Subject: [PATCH] fix(cli): wasi Node.js worker template (#2015) --- cli/src/api/templates/wasi-worker-template.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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,