fix(cli): wasi Node.js worker template (#2015)

This commit is contained in:
LongYinan 2024-03-28 10:08:17 +08:00 committed by GitHub
parent f2e5094345
commit 77399ff130
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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,