Skip to content

Commit 6c9f58d

Browse files
committed
Allow use of builtin node WebSocket when available. NFS
Node v21 and up have native WebSocket support. This change makes the import of the external `ws` module conditional so it will only run on older versions of node. See #26676
1 parent e42e640 commit 6c9f58d

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

src/lib/libsockfs.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,13 @@ addToLibrary({
212212
#if SOCKET_DEBUG
213213
dbg(`websocket: connect: ${url}, ${subProtocols.toString()}`);
214214
#endif
215-
// If node we use the ws library.
216-
var WebSocketConstructor;
217215
#if ENVIRONMENT_MAY_BE_NODE
218-
if (ENVIRONMENT_IS_NODE) {
219-
WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('ws'));
220-
} else
221-
#endif // ENVIRONMENT_MAY_BE_NODE
222-
{
223-
WebSocketConstructor = WebSocket;
216+
// If node we use the ws library.
217+
if (ENVIRONMENT_IS_NODE && !globalThis.WebSocket) {
218+
globalThis.WebSocket = /** @type{(typeof WebSocket)} */(require('ws'));
224219
}
225-
ws = new WebSocketConstructor(url, opts);
220+
#endif // ENVIRONMENT_MAY_BE_NODE
221+
ws = new WebSocket(url, opts);
226222
ws.binaryType = 'arraybuffer';
227223
} catch (e) {
228224
#if SOCKET_DEBUG
@@ -515,10 +511,12 @@ addToLibrary({
515511
sock.connecting = true;
516512
},
517513
listen(sock, backlog) {
514+
#if !ENVIRONMENT_MAY_BE_NODE
515+
throw new FS.ErrnoError({{{ cDefs.EOPNOTSUPP }}});
516+
#else
518517
if (!ENVIRONMENT_IS_NODE) {
519518
throw new FS.ErrnoError({{{ cDefs.EOPNOTSUPP }}});
520519
}
521-
#if ENVIRONMENT_MAY_BE_NODE
522520
if (sock.server) {
523521
throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); // already listening
524522
}

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 244343,
2+
"a.out.js": 244388,
33
"a.out.nodebug.wasm": 577478,
4-
"total": 821821,
4+
"total": 821866,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)