fix: use typeof self === object && self.constructor to identifier execution environment#2855
Conversation
…xecution environment Use a better way to identifier exection environemt becasue `try` and `catch` will cause misleaded error message
src/bindings/js.ts
Outdated
| await (async () => { | ||
| try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } | ||
| catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } | ||
| if (typeof self === "object" && self.constructor) { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } |
There was a problem hiding this comment.
I'm wondering if this could go wrong, say, in a Worker on a server-side runtime like Cloudflare Workers, Deno, or Bun.
Do you think there's a single solution that would work with most runtimes and bundlers?
There was a problem hiding this comment.
I found a repo to do this thing. https://github.com/flexdinesh/browser-or-node/blob/master/src/index.ts
Maybe we can get inspired from it. But I don't have so many test environment.
There was a problem hiding this comment.
I have checked in the runtime. Everything is fine for current version.
|
By the way, I think github's macos runner is upgraded to aarch64 architecture. |
|
runtime check status: |
| await (async () => { | ||
| try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } | ||
| catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } | ||
| const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); |
There was a problem hiding this comment.
| const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); | |
| const isNodeOrBun = typeof process != "undefined" && process.versions && (process.versions.node || process.versions.bun); |
Also, at some point in the future, I think the readFile method can be removed, because Node.js in recent versions has fetch built-in.
There was a problem hiding this comment.
agree, let us wait for the new features in nodejs.
Use a better way to identifier exection environemt becasue
tryandcatchwill cause misleaded error message