fix(cli): non-fs polyfilled browser worker binding (#2067)

This commit is contained in:
LongYinan 2024-04-23 14:26:27 +08:00 committed by GitHub
parent da31e367e1
commit 3e44ae3c16
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -65,16 +65,13 @@ globalThis.onmessage = function (e) {
export const createWasiBrowserWorkerBinding = (fs: boolean) => { export const createWasiBrowserWorkerBinding = (fs: boolean) => {
const fsImport = fs const fsImport = fs
? `import { memfsExported as __memfsExported } from '@napi-rs/wasm-runtime/fs' ? `import { instantiateNapiModuleSync, MessageHandler, WASI, createFsProxy } from '@napi-rs/wasm-runtime'
import { memfsExported as __memfsExported } from '@napi-rs/wasm-runtime/fs'
const fs = createFsProxy(__memfsExported)` const fs = createFsProxy(__memfsExported)`
: '\nconst fs = null' : `import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'`
return `import { instantiateNapiModuleSync, MessageHandler, WASI, createFsProxy } from '@napi-rs/wasm-runtime' const wasiCreation = fs
${fsImport} ? `const wasi = new WASI({
const handler = new MessageHandler({
onLoad({ wasmModule, wasmMemory }) {
const wasi = new WASI({
fs, fs,
preopens: { preopens: {
'/': '/', '/': '/',
@ -87,7 +84,22 @@ const handler = new MessageHandler({
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error.apply(console, arguments) console.error.apply(console, arguments)
}, },
}) })`
: `const wasi = new WASI({
print: function () {
// eslint-disable-next-line no-console
console.log.apply(console, arguments)
},
printErr: function() {
// eslint-disable-next-line no-console
console.error.apply(console, arguments)
},
})`
return `${fsImport}
const handler = new MessageHandler({
onLoad({ wasmModule, wasmMemory }) {
${wasiCreation}
return instantiateNapiModuleSync(wasmModule, { return instantiateNapiModuleSync(wasmModule, {
childThread: true, childThread: true,
wasi, wasi,