Skip to content

Commit 78963c5

Browse files
authored
fix: detect execution environment when loading wasm module (#2855)
1 parent be49610 commit 78963c5

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

src/bindings/js.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,9 @@ export class JSBuilder extends ExportsWalker {
976976
}
977977
sb.push(`} = await (async url => instantiate(
978978
await (async () => {
979-
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
980-
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
979+
const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null);
980+
if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
981+
else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
981982
})(), {
982983
`);
983984
let needsMaybeDefault = false;

tests/compiler/bindings/esm.debug.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,9 @@ export const {
553553
fn,
554554
} = await (async url => instantiate(
555555
await (async () => {
556-
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
557-
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
556+
const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null);
557+
if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
558+
else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
558559
})(), {
559560
}
560561
))(new URL("esm.debug.wasm", import.meta.url));

tests/compiler/bindings/esm.release.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,9 @@ export const {
553553
fn,
554554
} = await (async url => instantiate(
555555
await (async () => {
556-
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
557-
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
556+
const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null);
557+
if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
558+
else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
558559
})(), {
559560
}
560561
))(new URL("esm.release.wasm", import.meta.url));

tests/compiler/bindings/noExportRuntime.debug.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ export const {
162162
takesFunction,
163163
} = await (async url => instantiate(
164164
await (async () => {
165-
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
166-
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
165+
const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null);
166+
if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
167+
else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
167168
})(), {
168169
}
169170
))(new URL("noExportRuntime.debug.wasm", import.meta.url));

tests/compiler/bindings/noExportRuntime.release.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ export const {
162162
takesFunction,
163163
} = await (async url => instantiate(
164164
await (async () => {
165-
try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
166-
catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
165+
const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null);
166+
if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
167+
else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
167168
})(), {
168169
}
169170
))(new URL("noExportRuntime.release.wasm", import.meta.url));

0 commit comments

Comments
 (0)