Skip to content

Commit dc4486e

Browse files
committed
Remove use of Module['websocket']. NFC
We can still receive the initial value for websocket from the Module object but there is not need to continue to access it via the Module object.
1 parent d097f6d commit dc4486e

File tree

1 file changed

+38
-51
lines changed

1 file changed

+38
-51
lines changed

src/library_sockfs.js

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,33 @@ addToLibrary({
1010
},
1111
$SOCKFS__deps: ['$FS'],
1212
$SOCKFS: {
13+
websocket: {},
14+
callbacks: {},
15+
on(event, callback) {
16+
SOCKFS.callbacks[event] = callback;
17+
},
18+
emit(event, param) {
19+
SOCKFS.callbacks[event]?.(param);
20+
},
1321
mount(mount) {
14-
// If Module['websocket'] has already been defined (e.g. for configuring
15-
// the subprotocol/url) use that, if not initialise it to a new object.
16-
Module['websocket'] = (Module['websocket'] &&
17-
('object' === typeof Module['websocket'])) ? Module['websocket'] : {};
18-
22+
#if expectToReceiveOnModule('websocket')
23+
// The incomming Module['websocket'] can be used for configuring
24+
// configuring subprotocol/url, etc
25+
SOCKFS.websocket = {{{ makeModuleReceiveExpr('websocket', '{}') }}};
1926
// Add the Event registration mechanism to the exported websocket configuration
2027
// object so we can register network callbacks from native JavaScript too.
2128
// For more documentation see system/include/emscripten/emscripten.h
22-
Module['websocket']._callbacks = {};
23-
Module['websocket']['on'] = /** @this{Object} */ function(event, callback) {
24-
if ('function' === typeof callback) {
25-
this._callbacks[event] = callback;
26-
}
27-
return this;
28-
};
29-
30-
Module['websocket'].emit = /** @this{Object} */ function(event, param) {
31-
if ('function' === typeof this._callbacks[event]) {
32-
this._callbacks[event].call(this, param);
33-
}
34-
};
29+
(Module['websocket'] ??= {})['on'] = SOCKFS.on;
30+
#endif
3531

36-
// If debug is enabled register simple default logging callbacks for each Event.
3732
#if SOCKET_DEBUG
38-
Module['websocket']['on']('error', (error) => dbg('websocket: error ' + error));
39-
Module['websocket']['on']('open', (fd) => dbg('websocket: open fd = ' + fd));
40-
Module['websocket']['on']('listen', (fd) => dbg('websocket: listen fd = ' + fd));
41-
Module['websocket']['on']('connection', (fd) => dbg('websocket: connection fd = ' + fd));
42-
Module['websocket']['on']('message', (fd) => dbg('websocket: message fd = ' + fd));
43-
Module['websocket']['on']('close', (fd) => dbg('websocket: close fd = ' + fd));
33+
// If debug is enabled register simple default logging callbacks for each Event.
34+
SOCKFS.on('error', (error) => dbg('websocket: error ' + error));
35+
SOCKFS.on('open', (fd) => dbg('websocket: open fd = ' + fd));
36+
SOCKFS.on('listen', (fd) => dbg('websocket: listen fd = ' + fd));
37+
SOCKFS.on('connection', (fd) => dbg('websocket: connection fd = ' + fd));
38+
SOCKFS.on('message', (fd) => dbg('websocket: message fd = ' + fd));
39+
SOCKFS.on('close', (fd) => dbg('websocket: close fd = ' + fd));
4440
#endif
4541

4642
return FS.createNode(null, '/', {{{ cDefs.S_IFDIR }}} | 511 /* 0777 */, 0);
@@ -169,17 +165,11 @@ addToLibrary({
169165
} else {
170166
// create the actual websocket object and connect
171167
try {
172-
// runtimeConfig gets set to true if WebSocket runtime configuration is available.
173-
var runtimeConfig = (Module['websocket'] && ('object' === typeof Module['websocket']));
174-
175168
// The default value is 'ws://' the replace is needed because the compiler replaces '//' comments with '#'
176169
// comments without checking context, so we'd end up with ws:#, the replace swaps the '#' for '//' again.
177170
var url = '{{{ WEBSOCKET_URL }}}'.replace('#', '//');
178-
179-
if (runtimeConfig) {
180-
if ('string' === typeof Module['websocket']['url']) {
181-
url = Module['websocket']['url']; // Fetch runtime WebSocket URL config.
182-
}
171+
if (SOCKFS.websocket['url']) {
172+
url = SOCKFS.websocket['url']; // Fetch runtime WebSocket URL config.
183173
}
184174

185175
if (url === 'ws://' || url === 'wss://') { // Is the supplied URL config just a prefix, if so complete it.
@@ -189,11 +179,8 @@ addToLibrary({
189179

190180
// Make the WebSocket subprotocol (Sec-WebSocket-Protocol) default to binary if no configuration is set.
191181
var subProtocols = '{{{ WEBSOCKET_SUBPROTOCOL }}}'; // The default value is 'binary'
192-
193-
if (runtimeConfig) {
194-
if ('string' === typeof Module['websocket']['subprotocol']) {
195-
subProtocols = Module['websocket']['subprotocol']; // Fetch runtime WebSocket subprotocol config.
196-
}
182+
if (SOCKFS.websocket['subprotocol']) {
183+
subProtocols = SOCKFS.websocket['subprotocol']; // Fetch runtime WebSocket subprotocol config.
197184
}
198185

199186
// The default WebSocket options
@@ -208,7 +195,7 @@ addToLibrary({
208195
}
209196

210197
// some webservers (azure) does not support subprotocol header
211-
if (runtimeConfig && null === Module['websocket']['subprotocol']) {
198+
if (SOCKFS.websocket['subprotocol'] === null) {
212199
subProtocols = 'null';
213200
opts = undefined;
214201
}
@@ -280,8 +267,8 @@ addToLibrary({
280267
dbg('websocket: handle open');
281268
#endif
282269

283-
Module['websocket'].emit('open', sock.stream.fd);
284270
sock.connecting = false;
271+
SOCKFS.emit('open', sock.stream.fd);
285272

286273
try {
287274
var queued = peer.msg_send_queue.shift();
@@ -334,7 +321,7 @@ addToLibrary({
334321
}
335322

336323
sock.recv_queue.push({ addr: peer.addr, port: peer.port, data: data });
337-
Module['websocket'].emit('message', sock.stream.fd);
324+
SOCKFS.emit('message', sock.stream.fd);
338325
};
339326

340327
if (ENVIRONMENT_IS_NODE) {
@@ -346,21 +333,21 @@ addToLibrary({
346333
handleMessage((new Uint8Array(data)).buffer); // copy from node Buffer -> ArrayBuffer
347334
});
348335
peer.socket.on('close', function() {
349-
Module['websocket'].emit('close', sock.stream.fd);
336+
SOCKFS.emit('close', sock.stream.fd);
350337
});
351338
peer.socket.on('error', function(error) {
352339
// Although the ws library may pass errors that may be more descriptive than
353340
// ECONNREFUSED they are not necessarily the expected error code e.g.
354341
// ENOTFOUND on getaddrinfo seems to be node.js specific, so using ECONNREFUSED
355342
// is still probably the most useful thing to do.
356343
sock.error = {{{ cDefs.ECONNREFUSED }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
357-
Module['websocket'].emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
344+
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
358345
// don't throw
359346
});
360347
} else {
361348
peer.socket.onopen = handleOpen;
362349
peer.socket.onclose = function() {
363-
Module['websocket'].emit('close', sock.stream.fd);
350+
SOCKFS.emit('close', sock.stream.fd);
364351
};
365352
peer.socket.onmessage = function peer_socket_onmessage(event) {
366353
handleMessage(event.data);
@@ -369,7 +356,7 @@ addToLibrary({
369356
// The WebSocket spec only allows a 'simple event' to be thrown on error,
370357
// so we only really know as much as ECONNREFUSED.
371358
sock.error = {{{ cDefs.ECONNREFUSED }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
372-
Module['websocket'].emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
359+
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
373360
};
374361
}
375362
},
@@ -525,7 +512,7 @@ addToLibrary({
525512
port: sock.sport
526513
// TODO support backlog
527514
});
528-
Module['websocket'].emit('listen', sock.stream.fd); // Send Event with listen fd.
515+
SOCKFS.emit('listen', sock.stream.fd); // Send Event with listen fd.
529516

530517
sock.server.on('connection', function(ws) {
531518
#if SOCKET_DEBUG
@@ -541,17 +528,17 @@ addToLibrary({
541528

542529
// push to queue for accept to pick up
543530
sock.pending.push(newsock);
544-
Module['websocket'].emit('connection', newsock.stream.fd);
531+
SOCKFS.emit('connection', newsock.stream.fd);
545532
} else {
546533
// create a peer on the listen socket so calling sendto
547534
// with the listen socket and an address will resolve
548535
// to the correct client
549536
SOCKFS.websocket_sock_ops.createPeer(sock, ws);
550-
Module['websocket'].emit('connection', sock.stream.fd);
537+
SOCKFS.emit('connection', sock.stream.fd);
551538
}
552539
});
553540
sock.server.on('close', function() {
554-
Module['websocket'].emit('close', sock.stream.fd);
541+
SOCKFS.emit('close', sock.stream.fd);
555542
sock.server = null;
556543
});
557544
sock.server.on('error', function(error) {
@@ -562,7 +549,7 @@ addToLibrary({
562549
// occur in a well written app as errors should get trapped in the compiled
563550
// app's own getaddrinfo call.
564551
sock.error = {{{ cDefs.EHOSTUNREACH }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
565-
Module['websocket'].emit('error', [sock.stream.fd, sock.error, 'EHOSTUNREACH: Host is unreachable']);
552+
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'EHOSTUNREACH: Host is unreachable']);
566553
// don't throw
567554
});
568555
#endif // ENVIRONMENT_MAY_BE_NODE
@@ -763,7 +750,7 @@ addToLibrary({
763750
// FIXME(sbc): This has no corresponding Pop so will currently keep the
764751
// runtime alive indefinitely.
765752
{{{ runtimeKeepalivePush() }}}
766-
Module['websocket']['on'](event, callback ? _callback : null);
753+
SOCKFS.on(event, callback ? _callback : null);
767754
},
768755
emscripten_set_socket_error_callback__deps: ['$_setNetworkCallback'],
769756
emscripten_set_socket_error_callback: (userData, callback) => {

0 commit comments

Comments
 (0)