Skip to content

Node.js implemented WebAssembly.instantiateStreaming in v18.1.0 #16913

Closed

Description

@aduh95

Version of emscripten/emsdk:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.10 (c3fe57a)
clang version 15.0.0 (https://github.com/llvm/llvm-project 8bc29d14273b05b05d5a56e34c07948dc2c770d3)
Target: wasm32-unknown-emscripten
Thread model: posix

Failing command line in full:

$ emcc -s ENVIRONMENT=node hello_world.c
$ node a.out.js 
(node:50298) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
…/a.out.js:146
      throw ex;
      ^

TypeError: Failed to parse URL from …/a.out.wasm
    at new Request (node:internal/deps/undici/undici:4816:19)
    at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
    ... 4 lines matching cause stack trace ...
    at Object.<anonymous> (…/a.out.js:1914:11)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32) {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:377:5)
      at URL.onParseError (node:internal/url:563:9)
      at new URL (node:internal/url:643:5)
      at new Request (node:internal/deps/undici/undici:4814:25)
      at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
      at Object.fetch (node:internal/deps/undici/undici:6372:20)
      at fetch (node:internal/bootstrap/pre_execution:199:25)
      at instantiateAsync (…/a.out.js:1670:14)
      at createWasm (…/a.out.js:1707:3)
      at Object.<anonymous> (…/a.out.js:1914:11) {
    input: '…/a.out.wasm',
    code: 'ERR_INVALID_URL'
  }
}

Node.js v18.1.0

Note that disabling fetch does the trick:

$ node --no-experimental-fetch a.out.js 
hello, world!

Ideally instantiateStreaming should be called only on HTTP URIs.

Activity

  1. added a commit that references this issue on May 8, 2022
    3cf104e
  2. simnalamburt commented on May 8, 2022

    @simnalamburt
    Contributor

    I've opened a PR to fix this issue: #16917

  3. kripken commented on May 9, 2022

    @kripken
    Member

    Ideally instantiateStreaming should be called only on HTTP URIs.

    Hmm, on the web fetch("page.wasm", ..) works (without http[s]://). So I think it would be best overall if node.js implemented it the same as on the web. Maybe worth looking if there is already a node bug for that, and if not, filing one.

  4. aduh95 commented on May 9, 2022

    @aduh95
    ContributorAuthor

    Ideally instantiateStreaming should be called only on HTTP URIs.

    Hmm, on the web fetch("page.wasm", ..) works (without http[s]://). So I think it would be best overall if node.js implemented it the same as on the web. Maybe worth looking if there is already a node bug for that, and if not, filing one.

    There are at two tricky problems that would need to be addressed in Emscripten:

    1. Emscripten is passing a path rather than an actual URL when ENVIRONMENT_IS_NODE is true.
    2. Emscripten only support CJS for Node.js, and CJS doesn't really have a URL to resolve a relative URL (on ESM context, we could use import.meta.url)

    FWIW I've tried to see if it would work using from an HTTP context using ESM over HTTP, but it looks like Emscripten doesn't produce a valid JS file (it expect a global require to be present instead of creating it):

    $ emcc -s ENVIRONMENT=node -o hello.mjs hello.c
    $ node --input-type=module -e 'import hello from "./hello.mjs";await hello()'                    
    file://…/hello.mjs:118
      if (!(typeof process == 'object' && typeof require == 'function')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
                                                                               ^
    
    Error: not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)
        at file://…/hello.mjs:118:76
        at file://…/[eval1]:1:52
        at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
        at async ESMLoader.eval (node:internal/modules/esm/loader:319:24)
        at async loadESM (node:internal/process/esm_loader:83:5)
        at async handleMainPromise (node:internal/modules/run_main:65:12)
    
    Node.js v18.1.0
    $ python3 -m http.server &
    $ node --experimental-network-imports --input-type=module -e 'import hello from "http://localhost:8000/hello.mjs";await hello()' 
    (node:61848) ExperimentalWarning: Network Imports is an experimental feature. This feature could change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    http://localhost:8000/hello.mjs:118
      if (!(typeof process == 'object' && typeof require == 'function')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)');
                                                                               ^
    
    Error: not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)
        at http://localhost:8000/hello.mjs:118:76
        at file://…/[eval1]:1:59
        at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async ESMLoader.eval (node:internal/modules/esm/loader:319:24)
        at async loadESM (node:internal/process/esm_loader:83:5)
        at async handleMainPromise (node:internal/modules/run_main:65:12)
    
    Node.js v18.1.0

    If I try to patch the resulted file so it can run on Node.js, it still fails (with a warning saying that file: URIs should not be trusted), and it also tries to use the URL as a path which of course doesn't work:

    $ cat - hello.mjs > hello2.mjs <<EOF
    import {fileURLToPath}from'node:url';
    import {createRequire}from'node:module';
    const require=createRequire(import.meta.url);
    const __dirname = fileURLToPath(new URL('./', import.meta.url));
    EOF
    $ node --input-type=module -e 'import hello from "./hello2.mjs";await hello()'
    (node:61983) ExperimentalWarning: Network Imports is an experimental feature. This feature could change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm')
    failed to asynchronously prepare wasm: RuntimeError: Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm')
    warning: Loading from a file URI (file://…/hello.wasm) is not supported in most browsers. See https://emscripten.org/docs/getting_started/FAQ.html#how-do-i-run-a-local-webserver-for-testing-why-does-my-program-stall-in-downloading-or-preparing
    Aborted(RuntimeError: Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm'))
    file://…/hello.mjs:180
          throw ex;
          ^
    
    RuntimeError: Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm')
        at abort (file://…/hello.mjs:1512:11)
        at getBinary (file://…/hello.mjs:1604:5)
        at file://…/hello.mjs:1638:53
    
    Node.js v18.1.0
  5. added a commit that references this issue on May 12, 2022
    85f4a80
  6. added a commit that references this issue on May 26, 2022
    eab8755
  7. added a commit that references this issue on Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions