Skip to content
Draft
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
8 changes: 8 additions & 0 deletions src/closure-externs/node-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ fs.Stats.prototype.ctimeMs;
*/
fs.Stats.prototype.blksize;

/**
* @param {string|!Buffer|!URL} path
* @param {{type: (string|undefined)}=} options
* @return {!Promise<!Blob>}
* @nosideeffects
*/
fs.openAsBlob = function (path, options) {};

/**
* @param {string} p
* @return {boolean}
Expand Down
24 changes: 16 additions & 8 deletions src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function run() {
#else
// Run a persistent (never-exiting) application starting at main().
_main({{{ argc_argv() }}});
#endif
#endif

#if STACK_OVERFLOW_CHECK
checkStackCookie();
Expand Down Expand Up @@ -189,10 +189,13 @@ var imports = {
{{{
#if EXPORT_ES6 && !ENVIRONMENT_MAY_BE_AUDIO_WORKLET
const moduleUrl = `new URL('${TARGET_BASENAME}.wasm', import.meta.url)`;
const nodeWasmPath = `require('node:url').fileURLToPath(${moduleUrl})`;
#elif !EXPORT_ES6 || AUDIO_WORKLET
const moduleUrl = `'${TARGET_BASENAME}.wasm'`;
const nodeWasmPath = `__dirname + '/${TARGET_BASENAME}.wasm'`;
#else
const moduleUrl = `ENVIRONMENT_IS_AUDIO_WORKLET ? '${TARGET_BASENAME}.wasm' : new URL('${TARGET_BASENAME}.wasm', import.meta.url)`;
const nodeWasmPath = `__dirname + '/${TARGET_BASENAME}.wasm'`;
#endif
}}}
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
Expand All @@ -206,13 +209,18 @@ assert(WebAssembly.instantiateStreaming || Module['wasm'], 'Must load WebAssembl
instantiatePromise =
#endif
(WebAssembly.instantiateStreaming
#if ENVIRONMENT_MAY_BE_NODE
// Avoid using instantiateStreaming() on Node.js since the `fetch()` API
// does not support `file://` URLs.
// See: https://github.com/emscripten-core/emscripten/pull/16917
&& !ENVIRONMENT_IS_NODE
#endif
? WebAssembly.instantiateStreaming(fetch({{{ moduleUrl }}}), imports)
? (ENVIRONMENT_IS_NODE
// Avoid using `fetch()` API on Node.js since it does not support `file://`
// URLs. Instead, provide a Response that wraps a blob with the correct
// MIME type.
// See: https://github.com/emscripten-core/emscripten/pull/16917
? WebAssembly.instantiateStreaming(
require('node:fs')
.openAsBlob({{{ nodeWasmPath }}})
.then((blob) => new Response(blob, { headers: { 'Content-Type': 'application/wasm' } })),
imports,
)
: WebAssembly.instantiateStreaming(fetch({{{ moduleUrl }}}), imports))
: WebAssembly.instantiate(Module['wasm'], imports)).then((output) => {
#else
#if MODULARIZE || AUDIO_WORKLET
Expand Down
29 changes: 26 additions & 3 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,8 @@ async function instantiateAsync(binary, binaryFile, imports) {
&& !isFileURI(binaryFile)
#endif
#if ENVIRONMENT_MAY_BE_NODE
// Avoid using instantiateStreaming() on Node.js since the `fetch()` API
// does not support `file://` URLs.
// See: https://github.com/emscripten-core/emscripten/pull/16917
// Node.js is handled in its own branch below, since it can't use fetch()
// to stream a `file://` URL
&& !ENVIRONMENT_IS_NODE
#endif
#if ENVIRONMENT_MAY_BE_SHELL
Expand All @@ -730,6 +729,30 @@ async function instantiateAsync(binary, binaryFile, imports) {
// fall back of instantiateArrayBuffer below
};
}
#if ENVIRONMENT_MAY_BE_NODE
else if (!binary && ENVIRONMENT_IS_NODE) {
// Avoid using `fetch()` API on Node.js since it does not support `file://`
// URLs. Instead, provide a Response that wraps a fs read stream with the
// correct MIME type.
// See: https://github.com/emscripten-core/emscripten/pull/16917
try {
var url = require('node:url');
var nodeBinaryFile = isFileURI(binaryFile) ? url.fileURLToPath(binaryFile) : binaryFile;
var response = fs.openAsBlob(nodeBinaryFile).then(
(blob) =>
new Response(blob, {
headers: { 'Content-Type': 'application/wasm' },
}),
);
var instantiationResult = await WebAssembly.instantiateStreaming(response, imports);
return instantiationResult;
} catch (reason) {
err(`wasm streaming compile failed: ${reason}`);
err('falling back to ArrayBuffer instantiation');
// fall back of instantiateArrayBuffer below
};
}
#endif
#endif // !SINGLE_FILE
return instantiateArrayBuffer(binaryFile, imports);
}
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_ctors1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19224,
"a.out.js.gz": 8124,
"a.out.js": 19537,
"a.out.js.gz": 8228,
"a.out.nodebug.wasm": 133971,
"a.out.nodebug.wasm.gz": 51271,
"total": 153195,
"total_gz": 59395,
"total": 153508,
"total_gz": 59499,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_ctors2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19201,
"a.out.js.gz": 8107,
"a.out.js": 19514,
"a.out.js.gz": 8211,
"a.out.nodebug.wasm": 133403,
"a.out.nodebug.wasm.gz": 50959,
"total": 152604,
"total_gz": 59066,
"total": 152917,
"total_gz": 59170,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22917,
"a.out.js.gz": 9082,
"a.out.js": 23229,
"a.out.js.gz": 9190,
"a.out.nodebug.wasm": 176383,
"a.out.nodebug.wasm.gz": 58776,
"total": 199300,
"total_gz": 67858,
"total": 199612,
"total_gz": 67966,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19023,
"a.out.js.gz": 8039,
"a.out.js": 19336,
"a.out.js.gz": 8147,
"a.out.nodebug.wasm": 149640,
"a.out.nodebug.wasm.gz": 56311,
"total": 168663,
"total_gz": 64350,
"total": 168976,
"total_gz": 64458,
"sent": [
"_abort_js",
"_tzset_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm_legacy.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19101,
"a.out.js.gz": 8065,
"a.out.js": 19414,
"a.out.js.gz": 8171,
"a.out.nodebug.wasm": 147422,
"a.out.nodebug.wasm.gz": 55982,
"total": 166523,
"total_gz": 64047,
"total": 166836,
"total_gz": 64153,
"sent": [
"_abort_js",
"_tzset_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_lto.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 18568,
"a.out.js.gz": 7818,
"a.out.js": 18881,
"a.out.js.gz": 7926,
"a.out.nodebug.wasm": 101064,
"a.out.nodebug.wasm.gz": 38292,
"total": 119632,
"total_gz": 46110,
"total": 119945,
"total_gz": 46218,
"sent": [
"a (emscripten_resize_heap)",
"b (_setitimer_js)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_mangle.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22967,
"a.out.js.gz": 9103,
"a.out.js": 23279,
"a.out.js.gz": 9210,
"a.out.nodebug.wasm": 242663,
"a.out.nodebug.wasm.gz": 81001,
"total": 265630,
"total_gz": 90104,
"total": 265942,
"total_gz": 90211,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_noexcept.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19224,
"a.out.js.gz": 8124,
"a.out.js": 19537,
"a.out.js.gz": 8228,
"a.out.nodebug.wasm": 135882,
"a.out.nodebug.wasm.gz": 51885,
"total": 155106,
"total_gz": 60009,
"total": 155419,
"total_gz": 60113,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_cxx_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 6604,
"a.out.js.gz": 3154,
"a.out.js": 6917,
"a.out.js.gz": 3266,
"a.out.nodebug.wasm": 173607,
"a.out.nodebug.wasm.gz": 64636,
"total": 180211,
"total_gz": 67790,
"total": 180524,
"total_gz": 67902,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
19 changes: 19 additions & 0 deletions test/codesize/test_codesize_file_preload.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,25 @@ async function instantiateAsync(binary, binaryFile, imports) {
err(`wasm streaming compile failed: ${reason}`);
err("falling back to ArrayBuffer instantiation");
}
} else if (!binary && ENVIRONMENT_IS_NODE) {
// Avoid using `fetch()` API on Node.js since it does not support `file://`
// URLs. Instead, provide a Response that wraps a fs read stream with the
// correct MIME type.
// See: https://github.com/emscripten-core/emscripten/pull/16917
try {
var url = require("node:url");
var nodeBinaryFile = isFileURI(binaryFile) ? url.fileURLToPath(binaryFile) : binaryFile;
var response = fs.openAsBlob(nodeBinaryFile).then(blob => new Response(blob, {
headers: {
"Content-Type": "application/wasm"
}
}));
var instantiationResult = await WebAssembly.instantiateStreaming(response, imports);
return instantiationResult;
} catch (reason) {
err(`wasm streaming compile failed: ${reason}`);
err("falling back to ArrayBuffer instantiation");
}
}
return instantiateArrayBuffer(binaryFile, imports);
}
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_file_preload.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22203,
"a.out.js.gz": 9267,
"a.out.js": 22516,
"a.out.js.gz": 9367,
"a.out.nodebug.wasm": 1198,
"a.out.nodebug.wasm.gz": 730,
"total": 23401,
"total_gz": 9997,
"total": 23714,
"total_gz": 10097,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_files_js_fs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 17871,
"a.out.js.gz": 7460,
"a.out.js": 18184,
"a.out.js.gz": 7571,
"a.out.nodebug.wasm": 381,
"a.out.nodebug.wasm.gz": 258,
"total": 18252,
"total_gz": 7718,
"total": 18565,
"total_gz": 7829,
"sent": [
"a (fd_write)",
"b (fd_read)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_files_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 4979,
"a.out.js.gz": 2409,
"a.out.js": 5292,
"a.out.js.gz": 2518,
"a.out.nodebug.wasm": 58252,
"a.out.nodebug.wasm.gz": 18292,
"total": 63231,
"total_gz": 20701,
"total": 63544,
"total_gz": 20810,
"sent": [
"a (emscripten_date_now)",
"b (emscripten_err)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O0.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23596,
"a.out.js.gz": 8582,
"a.out.js": 23986,
"a.out.js.gz": 8693,
"a.out.nodebug.wasm": 14279,
"a.out.nodebug.wasm.gz": 7101,
"total": 37875,
"total_gz": 15683,
"total": 38265,
"total_gz": 15794,
"sent": [
"fd_write"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 5516,
"a.out.js.gz": 2242,
"a.out.js": 5906,
"a.out.js.gz": 2362,
"a.out.nodebug.wasm": 2050,
"a.out.nodebug.wasm.gz": 1231,
"total": 7566,
"total_gz": 3473,
"total": 7956,
"total_gz": 3593,
"sent": [
"fd_write"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3855,
"a.out.js.gz": 1966,
"a.out.js": 4167,
"a.out.js.gz": 2074,
"a.out.nodebug.wasm": 1444,
"a.out.nodebug.wasm.gz": 922,
"total": 5299,
"total_gz": 2888,
"total": 5611,
"total_gz": 2996,
"sent": [
"fd_write"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O3.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3797,
"a.out.js.gz": 1925,
"a.out.js": 4109,
"a.out.js.gz": 2032,
"a.out.nodebug.wasm": 1198,
"a.out.nodebug.wasm.gz": 730,
"total": 4995,
"total_gz": 2655,
"total": 5307,
"total_gz": 2762,
"sent": [
"a (fd_write)"
],
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_Os.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 3797,
"a.out.js.gz": 1925,
"a.out.js": 4109,
"a.out.js.gz": 2032,
"a.out.nodebug.wasm": 1188,
"a.out.nodebug.wasm.gz": 732,
"total": 4985,
"total_gz": 2657,
"total": 5297,
"total_gz": 2764,
"sent": [
"a (fd_write)"
],
Expand Down
Loading
Loading