Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #16913: Don't use fetch() in Node.js environment #16917

Merged
merged 1 commit into from
May 26, 2022

Conversation

simnalamburt
Copy link
Contributor

Node.js v18.1.0 has implemented WebAssembly.instantiateStreaming API, and it broked all emscriptened js codes since emscripten is using WebAssembly.instantiateStreaming as a criterion to distinguish whether it is Node.js environment or a browser environment.

The issue will be fixed if we use ENVIRONMENT_IS_NODE as a proper criterion to distinguish Node.js environment.

Fixes #16913, antelle/argon2-browser#81.

References

@kripken
Copy link
Member

kripken commented May 9, 2022

Interesting, thanks for filing...

I'd hope we can actually use instantiateStreaming if node implemented it, but looks like in #16913 that the blocker is that node's fetch does not work yet. Sounds good to avoid that path for now.

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please add a comment, as we'd like to remove this workaround some day. Perhaps something like this:

// Avoid instantiateStreaming() on node for now, as while newer node implements it,
// it does not have a full fetch() implementation yet.
// [..link to emscripten and/or node bug..]

src/preamble.js Show resolved Hide resolved
@simnalamburt
Copy link
Contributor Author

Done!

@juj
Copy link
Collaborator

juj commented May 12, 2022

What will be the path towards removing this check?

It looks like there is no way to feature test what the bad Node.js versions are/will be?

Does node.js have some kind of synchronous version test that could be used, e.g. "if node.js version >= x.y.z.w (regression first introduced) and node.js version < X.Y.Z.W (bugfix first appeared in), then disable streaming instantiation"?

If not, it will be really hard to evolve out of this check since users of older Node.js versions might break again if this check was ever removed (after latest Node.js fixes things up)?

@simnalamburt
Copy link
Contributor Author

We have several options.

  1. In my opinion, there's no big point with using WebAssembly.instantiateStreaming in Node.js environment, so we might keep this forever.
  2. We can remove this check and wrap fetch() call with try { ... } catch() clause. This will add small overhead to old Node.js codes.
  3. We can check process.version only in ENVIRONMENT_IS_NODE.
  4. etc?

@juj
Copy link
Collaborator

juj commented May 12, 2022

Does node.js have some kind of synchronous version test that could be used, e.g. "if node.js version >= x.y.z.w (regression first introduced) and node.js version < X.Y.Z.W (bugfix first appeared in), then disable streaming instantiation"?

E.g. something like

if (!ENVIRONMENT_IS_NODE || (
    versionGreaterOrEqualTo(process.versions.node, [18,1,0]) &&
    versionLessThan(process.versions.node, [999,999,999]
    /* TODO: When Node.js lands a fix, update version range to match accordingly*/)))
   /* enable streaming instantiation */
else
   /* disable streaming instantiation */

@simnalamburt
Copy link
Contributor Author

There is no built-in function to compare versions in Node.js, so we have to modify the code a bit much to get the behavior you want. Since applying this patch does not cause problems in future Node.js versions, why don't we discuss the issue of removing this patch again in another PR or another issue?

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me to land this, and consider improvements later.

@kripken kripken enabled auto-merge (squash) May 26, 2022 17:28
kripken added a commit that referenced this pull request May 26, 2022
@kripken kripken disabled auto-merge May 26, 2022 19:20
@kripken kripken merged commit eab8755 into emscripten-core:main May 26, 2022
@simnalamburt simnalamburt deleted the issue-16913 branch May 27, 2022 09:45
mizar added a commit to mizar/YaneuraOu.wasm that referenced this pull request Jun 9, 2022
  emscripten/emsdk 3.1.13 (2022-06-04) にて修正済
  cf. emscripten-core/emscripten#16917
mizar added a commit to mizar/YaneuraOu.wasm that referenced this pull request Jun 20, 2022
  emscripten/emsdk 3.1.13 (2022-06-04) にて修正済
  cf. emscripten-core/emscripten#16917
mizar added a commit to mizar/YaneuraOu.wasm that referenced this pull request Jun 20, 2022
  emscripten/emsdk 3.1.13 (2022-06-04) にて修正済
  cf. emscripten-core/emscripten#16917
@RReverser
Copy link
Collaborator

I'd hope we can actually use instantiateStreaming if node implemented it

FWIW, out of curiosity, I tried to use streaming compilation with manual conversion from Node.js file streams, and it does work, but is ~2x slower than the buffer-based variant: https://twitter.com/RReverser/status/1593330290491068416

It's said to be due to the current implementation of Web streams in Node being experimental and not optimised yet, so it might change in the future, but, at least, for now there is no advantage in using streaming compilation.

@simnalamburt
Copy link
Contributor Author

Thanks for the research @RReverser! Looks like we'd be better to sustain this patch for a long time in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Node.js implemented WebAssembly.instantiateStreaming in v18.1.0
4 participants