Skip to content

Commit f2d29f0

Browse files
Bartek Wronavogel76
authored andcommitted
Replace require() with library symbols in EXPORT_ES6 library files
Library functions run in synchronous context where await is unavailable. Define top-level library symbols that use await import() at module init time, then reference them via __deps from synchronous functions. - Add libnode_imports.js with shared $nodeOs symbol, register in modules.mjs when EXPORT_ES6 is enabled. - libatomic.js, libwasm_worker.js: Use $nodeOs for os.cpus().length instead of require('node:os'). - libwasi.js: Define $nodeCrypto for crypto.randomFillSync in $initRandomFill. Combine conditional __deps to avoid override. - libcore.js: Define $nodeChildProcess for _emscripten_system. - libnodepath.js: Switch $nodePath initializer to await import(). - libsockfs.js: Define $nodeWs ((await import('ws')).default) for WebSocket constructor in connect() and Server in listen().
1 parent 8781522 commit f2d29f0

6 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/lib/libatomic.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ addToLibrary({
154154

155155
emscripten_has_threading_support: () => !!globalThis.SharedArrayBuffer,
156156

157+
#if ENVIRONMENT_MAY_BE_NODE
158+
emscripten_num_logical_cores__deps: ['$nodeOs'],
159+
#endif
157160
emscripten_num_logical_cores: () =>
158161
#if ENVIRONMENT_MAY_BE_NODE
159-
ENVIRONMENT_IS_NODE ? require('node:os').cpus().length :
162+
ENVIRONMENT_IS_NODE ? nodeOs.cpus().length :
160163
#endif
161164
navigator['hardwareConcurrency'],
162165

src/lib/libcore.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ addToLibrary({
372372
},
373373
#endif
374374

375+
#if ENVIRONMENT_MAY_BE_NODE
376+
$nodeOs: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:os') }}} : undefined",
377+
$nodeChildProcess: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:child_process') }}} : undefined",
378+
_emscripten_system__deps: ['$nodeChildProcess'],
379+
#endif
375380
_emscripten_system: (command) => {
376381
#if ENVIRONMENT_MAY_BE_NODE
377382
if (ENVIRONMENT_IS_NODE) {

src/lib/libnodepath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// operations. Hence, using `nodePath` should be safe here.
1313

1414
addToLibrary({
15-
$nodePath: "require('node:path')",
15+
$nodePath: "{{{ makeNodeImport('node:path') }}}",
1616
$PATH__deps: ['$nodePath'],
1717
$PATH: `{
1818
isAbs: nodePath.isAbsolute,

src/lib/libsockfs.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
*/
66

77
addToLibrary({
8+
$SOCKFS__deps: ['$FS'],
89
$SOCKFS__postset: () => {
10+
#if ENVIRONMENT_MAY_BE_NODE && EXPORT_ES6
11+
// In ESM mode, pre-load 'ws' at init time since require() is not available.
12+
// This runs lazily (only when SOCKFS is used, not at module load time).
13+
addAtInit(`if (ENVIRONMENT_IS_NODE) {
14+
SOCKFS.websocketModule = (await import('ws')).default;
15+
}`);
16+
#endif
917
addAtInit('SOCKFS.root = FS.mount(SOCKFS, {}, null);');
1018
},
11-
$SOCKFS__deps: ['$FS'],
1219
$SOCKFS: {
1320
#if expectToReceiveOnModule('websocket')
1421
websocketArgs: {},
@@ -216,7 +223,11 @@ addToLibrary({
216223
var WebSocketConstructor;
217224
#if ENVIRONMENT_MAY_BE_NODE
218225
if (ENVIRONMENT_IS_NODE) {
226+
#if EXPORT_ES6
227+
WebSocketConstructor = /** @type{(typeof WebSocket)} */(SOCKFS.websocketModule);
228+
#else
219229
WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('ws'));
230+
#endif
220231
} else
221232
#endif // ENVIRONMENT_MAY_BE_NODE
222233
{
@@ -522,7 +533,11 @@ addToLibrary({
522533
if (sock.server) {
523534
throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); // already listening
524535
}
536+
#if EXPORT_ES6
537+
var WebSocketServer = SOCKFS.websocketModule.Server;
538+
#else
525539
var WebSocketServer = require('ws').Server;
540+
#endif
526541
var host = sock.saddr;
527542
#if SOCKET_DEBUG
528543
dbg(`websocket: listen: ${host}:${sock.sport}`);

src/lib/libwasi.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,21 @@ var WasiLibrary = {
569569

570570
// random.h
571571

572-
#if ENVIRONMENT_MAY_BE_SHELL
572+
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
573+
$nodeCrypto: "ENVIRONMENT_IS_NODE ? {{{ makeNodeImport('node:crypto') }}} : undefined",
574+
#endif
575+
576+
#if ENVIRONMENT_MAY_BE_SHELL && ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
577+
$initRandomFill__deps: ['$base64Decode', '$nodeCrypto'],
578+
#elif ENVIRONMENT_MAY_BE_SHELL
573579
$initRandomFill__deps: ['$base64Decode'],
580+
#elif ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
581+
$initRandomFill__deps: ['$nodeCrypto'],
574582
#endif
575583
$initRandomFill: () => {
576584
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
577585
// This block is not needed on v19+ since crypto.getRandomValues is builtin
578586
if (ENVIRONMENT_IS_NODE) {
579-
var nodeCrypto = require('node:crypto');
580587
return (view) => nodeCrypto.randomFillSync(view);
581588
}
582589
#endif // ENVIRONMENT_MAY_BE_NODE

src/lib/libwasm_worker.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,12 @@ if (ENVIRONMENT_IS_WASM_WORKER
288288
_wasmWorkers[id].postMessage({'_wsc': funcPtr, 'x': readEmAsmArgs(sigPtr, varargs) });
289289
},
290290

291+
#if ENVIRONMENT_MAY_BE_NODE
292+
emscripten_navigator_hardware_concurrency__deps: ['$nodeOs'],
293+
#endif
291294
emscripten_navigator_hardware_concurrency: () => {
292295
#if ENVIRONMENT_MAY_BE_NODE
293-
if (ENVIRONMENT_IS_NODE) return require('node:os').cpus().length;
296+
if (ENVIRONMENT_IS_NODE) return nodeOs.cpus().length;
294297
#endif
295298
return navigator['hardwareConcurrency'];
296299
},

0 commit comments

Comments
 (0)