-
-
Notifications
You must be signed in to change notification settings - Fork 670
fix: use typeof self === object && self.constructor
to identifier execution environment
#2855
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
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
@@ -976,8 +976,8 @@ export class JSBuilder extends ExportsWalker { | |||
} | |||
sb.push(`} = await (async url => instantiate( | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
@@ -976,8 +976,9 @@ export class JSBuilder extends ExportsWalker { | |||
} | |||
sb.push(`} = await (async url => instantiate( | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree, let us wait for the new features in nodejs.
Use a better way to identifier exection environemt becasue
try
andcatch
will cause misleaded error message