Skip to content

Commit d877393

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 0bf241c commit d877393

File tree

1 file changed

+37
-50
lines changed

1 file changed

+37
-50
lines changed

src/library_sockfs.js

Lines changed: 37 additions & 50 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) {
22+
#if expectToReceiveOnModule('websocket')
1423
// 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-
24+
// the subprotocol/url) use that.
25+
Module['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,7 +267,7 @@ addToLibrary({
280267
dbg('websocket: handle open');
281268
#endif
282269

283-
Module['websocket'].emit('open', sock.stream.fd);
270+
SOCKFS.emit('open', sock.stream.fd);
284271

285272
try {
286273
var queued = peer.msg_send_queue.shift();
@@ -333,7 +320,7 @@ addToLibrary({
333320
}
334321

335322
sock.recv_queue.push({ addr: peer.addr, port: peer.port, data: data });
336-
Module['websocket'].emit('message', sock.stream.fd);
323+
SOCKFS.emit('message', sock.stream.fd);
337324
};
338325

339326
if (ENVIRONMENT_IS_NODE) {
@@ -345,21 +332,21 @@ addToLibrary({
345332
handleMessage((new Uint8Array(data)).buffer); // copy from node Buffer -> ArrayBuffer
346333
});
347334
peer.socket.on('close', function() {
348-
Module['websocket'].emit('close', sock.stream.fd);
335+
SOCKFS.emit('close', sock.stream.fd);
349336
});
350337
peer.socket.on('error', function(error) {
351338
// Although the ws library may pass errors that may be more descriptive than
352339
// ECONNREFUSED they are not necessarily the expected error code e.g.
353340
// ENOTFOUND on getaddrinfo seems to be node.js specific, so using ECONNREFUSED
354341
// is still probably the most useful thing to do.
355342
sock.error = {{{ cDefs.ECONNREFUSED }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
356-
Module['websocket'].emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
343+
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
357344
// don't throw
358345
});
359346
} else {
360347
peer.socket.onopen = handleOpen;
361348
peer.socket.onclose = function() {
362-
Module['websocket'].emit('close', sock.stream.fd);
349+
SOCKFS.emit('close', sock.stream.fd);
363350
};
364351
peer.socket.onmessage = function peer_socket_onmessage(event) {
365352
handleMessage(event.data);
@@ -368,7 +355,7 @@ addToLibrary({
368355
// The WebSocket spec only allows a 'simple event' to be thrown on error,
369356
// so we only really know as much as ECONNREFUSED.
370357
sock.error = {{{ cDefs.ECONNREFUSED }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
371-
Module['websocket'].emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
358+
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
372359
};
373360
}
374361
},
@@ -515,7 +502,7 @@ addToLibrary({
515502
port: sock.sport
516503
// TODO support backlog
517504
});
518-
Module['websocket'].emit('listen', sock.stream.fd); // Send Event with listen fd.
505+
SOCKFS.emit('listen', sock.stream.fd); // Send Event with listen fd.
519506

520507
sock.server.on('connection', function(ws) {
521508
#if SOCKET_DEBUG
@@ -531,17 +518,17 @@ addToLibrary({
531518

532519
// push to queue for accept to pick up
533520
sock.pending.push(newsock);
534-
Module['websocket'].emit('connection', newsock.stream.fd);
521+
SOCKFS.emit('connection', newsock.stream.fd);
535522
} else {
536523
// create a peer on the listen socket so calling sendto
537524
// with the listen socket and an address will resolve
538525
// to the correct client
539526
SOCKFS.websocket_sock_ops.createPeer(sock, ws);
540-
Module['websocket'].emit('connection', sock.stream.fd);
527+
SOCKFS.emit('connection', sock.stream.fd);
541528
}
542529
});
543530
sock.server.on('close', function() {
544-
Module['websocket'].emit('close', sock.stream.fd);
531+
SOCKFS.emit('close', sock.stream.fd);
545532
sock.server = null;
546533
});
547534
sock.server.on('error', function(error) {
@@ -552,7 +539,7 @@ addToLibrary({
552539
// occur in a well written app as errors should get trapped in the compiled
553540
// app's own getaddrinfo call.
554541
sock.error = {{{ cDefs.EHOSTUNREACH }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
555-
Module['websocket'].emit('error', [sock.stream.fd, sock.error, 'EHOSTUNREACH: Host is unreachable']);
542+
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'EHOSTUNREACH: Host is unreachable']);
556543
// don't throw
557544
});
558545
#endif // ENVIRONMENT_MAY_BE_NODE
@@ -753,7 +740,7 @@ addToLibrary({
753740
// FIXME(sbc): This has no corresponding Pop so will currently keep the
754741
// runtime alive indefinitely.
755742
{{{ runtimeKeepalivePush() }}}
756-
Module['websocket']['on'](event, callback ? _callback : null);
743+
SOCKFS.on(event, callback ? _callback : null);
757744
},
758745
emscripten_set_socket_error_callback__deps: ['$_setNetworkCallback'],
759746
emscripten_set_socket_error_callback: (userData, callback) => {

0 commit comments

Comments
 (0)