Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ function _free() {
#endif // free
#endif // ASSERTIONS

/**
* Indicates whether filename is delivered via URL.createObjectURL()
* @noinline
*/
var isBlobURI = (filename) => filename.startsWith('blob:');

/**
* Indicates whether filename is delivered via file protocol (as opposed to http/https)
* @noinline
Expand Down Expand Up @@ -685,13 +691,18 @@ async function instantiateAsync(binary, binaryFile, imports) {
&& !isFileURI(binaryFile)
#endif
#if ENVIRONMENT_MAY_BE_NODE
// Avoid instantiateStreaming() on Node.js environment for now, as while
// Node.js v18.1.0 implements it, it does not have a full fetch()
// implementation yet.
//
// Reference:
// https://github.com/emscripten-core/emscripten/pull/16917
&& !ENVIRONMENT_IS_NODE
&& (
// Avoid instantiateStreaming() on Node.js environment for now, as while
// Node.js v18.1.0 implements it, it does not have a full fetch()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Currently looks like MINIMUM_NODE_VERSION defaults to 18.3, we could do a conditional to check if MINIMUM_NODE_VERSION is greater than or equal to 20 and in that case drop it. Both node 18 and 20 are already end of life right?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh but I see this comment was already there.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we should probably update the comment to focus on the "does not have a full fetch" part. Specifically node's fetch does not work with file:// URLs, which makes it mostly useless for loading wasm modules that exist locally.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh yeah that's disappointing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

it works with blobs though, which I believe are stored as temporary files because it doesn't need a server spinning to work without issues so it's extremely weird WASM files cannot be fetched ... should we escalate upstream? Right now this is blocking one of my utilities in NodeJS so we could also do a step after the other as this issue is focused to one caveat, the other one is bigger and involves upstream support/changes.

// implementation yet.
//
// Reference:
// https://github.com/emscripten-core/emscripten/pull/16917
!ENVIRONMENT_IS_NODE
// On browsers this check won't be reached but on Node.js it is the only way to avoid
// throwing when URL.createObjectURL(blob) has been passed as the binaryFile.
|| isBlobURI(binaryFile)
)
#endif
#if ENVIRONMENT_MAY_BE_SHELL
// Shell environments don't have fetch.
Expand Down
7 changes: 6 additions & 1 deletion test/codesize/test_codesize_file_preload.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ var ABORT = false;
// but only when noExitRuntime is false.
var EXITSTATUS;

/**
* Indicates whether filename is delivered via URL.createObjectURL()
* @noinline
*/ var isBlobURI = (filename) => filename.startsWith('blob:');

/**
* Indicates whether filename is delivered via file protocol (as opposed to http/https)
* @noinline
Expand Down Expand Up @@ -415,7 +420,7 @@ async function instantiateArrayBuffer(binaryFile, imports) {
}

async function instantiateAsync(binary, binaryFile, imports) {
if (!binary && !isFileURI(binaryFile) && !ENVIRONMENT_IS_NODE) {
if (!binary && !isFileURI(binaryFile) && (!ENVIRONMENT_IS_NODE || isBlobURI(binaryFile))) {
try {
var response = fetch(binaryFile, {
credentials: "same-origin"
Expand Down
25 changes: 18 additions & 7 deletions test/codesize/test_codesize_minimal_O0.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ function _free() {
abort('free() called but not included in the build - add `_free` to EXPORTED_FUNCTIONS');
}

/**
* Indicates whether filename is delivered via URL.createObjectURL()
* @noinline
*/
var isBlobURI = (filename) => filename.startsWith('blob:');

/**
* Indicates whether filename is delivered via file protocol (as opposed to http/https)
* @noinline
Expand Down Expand Up @@ -651,13 +657,18 @@ async function instantiateAsync(binary, binaryFile, imports) {
if (!binary
// Don't use streaming for file:// delivered objects in a webview, fetch them synchronously.
&& !isFileURI(binaryFile)
// Avoid instantiateStreaming() on Node.js environment for now, as while
// Node.js v18.1.0 implements it, it does not have a full fetch()
// implementation yet.
//
// Reference:
// https://github.com/emscripten-core/emscripten/pull/16917
&& !ENVIRONMENT_IS_NODE
&& (
// Avoid instantiateStreaming() on Node.js environment for now, as while
// Node.js v18.1.0 implements it, it does not have a full fetch()
// implementation yet.
//
// Reference:
// https://github.com/emscripten-core/emscripten/pull/16917
!ENVIRONMENT_IS_NODE
// On browsers this check won't be reached but on Node.js it is the only way to avoid
// throwing when URL.createObjectURL(blob) has been passed as the binaryFile.
|| isBlobURI(binaryFile)
)
) {
try {
var response = fetch(binaryFile, { credentials: 'same-origin' });
Expand Down