Skip to content

Commit be9f95b

Browse files
authored
[WasmFS] Make the Node backend optional for web builds (#26608)
Previously, the WasmFS Node backend assumed Node.js was always available. This change allows building binaries that include Node backend stubs, so they can run in web environments. Split out from #24733.
1 parent 07d59b6 commit be9f95b

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/lib/libwasmfs_node.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
addToLibrary({
8-
$wasmfsNodeIsWindows: !!process.platform.match(/^win/),
7+
var wasmFSNodeLibrary = {
8+
$wasmfsNodeIsWindows: "!!process.platform.match(/^win/)",
99

1010
$wasmfsNodeConvertNodeCode__deps: ['$ERRNO_CODES'],
1111
$wasmfsNodeConvertNodeCode: (e) => {
@@ -29,8 +29,8 @@ addToLibrary({
2929
$wasmfsNodeFixStat__deps: ['$wasmfsNodeIsWindows'],
3030
$wasmfsNodeFixStat: (stat) => {
3131
if (wasmfsNodeIsWindows) {
32-
// Node.js on Windows never represents permission bit 'x', so
33-
// propagate read bits to execute bits
32+
// Windows does not report the 'x' permission bit, so propagate read
33+
// bits to execute bits.
3434
stat.mode |= (stat.mode & {{{ cDefs.S_IRUGO }}}) >> 2;
3535
}
3636
return stat;
@@ -222,5 +222,30 @@ addToLibrary({
222222
// implicitly return 0
223223
});
224224
},
225+
};
226+
227+
#if !ENVIRONMENT_MAY_BE_NODE
228+
function makeStub(x, library) {
229+
if (isJsOnlySymbol(x) || isDecorator(x)) {
230+
return;
231+
}
232+
233+
var t = library[x];
234+
if (typeof t == 'string') return;
235+
t = t.toString();
236+
237+
delete library[x + '__i53abi'];
238+
delete library[x + '__deps'];
239+
library[x] = modifyJSFunction(t, (args, body) => {
240+
return `(${args}) => {\n` +
241+
(ASSERTIONS ? "abort('attempt to call Node.js backend function without ENVIRONMENT_MAY_BE_NODE');\n" : '') +
242+
'}';
243+
});
244+
}
245+
246+
for (const name of Object.keys(wasmFSNodeLibrary)) {
247+
makeStub(name, wasmFSNodeLibrary);
248+
}
249+
#endif
225250

226-
});
251+
addToLibrary(wasmFSNodeLibrary);

0 commit comments

Comments
 (0)