Skip to content

Commit 792783a

Browse files
authored
Cleanup event handler registration in libsockfs.js. NFC (#26683)
- Elide node-specific code when not targeting node. - Prefer arrow functions
1 parent 6dfd5d9 commit 792783a

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

src/lib/libsockfs.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ addToLibrary({
274274
handlePeerEvents(sock, peer) {
275275
var first = true;
276276

277-
var handleOpen = function () {
277+
function handleOpen() {
278278
#if SOCKET_DEBUG
279279
dbg('websocket: handle open');
280280
#endif
@@ -296,7 +296,7 @@ addToLibrary({
296296
// lied and said this data was sent. shut it down.
297297
peer.socket.close();
298298
}
299-
};
299+
}
300300

301301
function handleMessage(data) {
302302
if (typeof data == 'string') {
@@ -336,43 +336,39 @@ addToLibrary({
336336

337337
sock.recv_queue.push({ addr: peer.addr, port: peer.port, data: data });
338338
SOCKFS.emit('message', sock.stream.fd);
339-
};
339+
}
340340

341+
#if ENVIRONMENT_MAY_BE_NODE
341342
if (ENVIRONMENT_IS_NODE) {
343+
// EventEmitter-style events use by ws library objects in Node.js).
342344
peer.socket.on('open', handleOpen);
343-
peer.socket.on('message', function(data, isBinary) {
345+
peer.socket.on('message', (data, isBinary) => {
344346
if (!isBinary) {
345347
return;
346348
}
347349
handleMessage((new Uint8Array(data)).buffer); // copy from node Buffer -> ArrayBuffer
348350
});
349-
peer.socket.on('close', function() {
350-
SOCKFS.emit('close', sock.stream.fd);
351-
});
352-
peer.socket.on('error', function(error) {
351+
peer.socket.on('close', () => SOCKFS.emit('close', sock.stream.fd));
352+
peer.socket.on('error', (error) =>{
353353
// Although the ws library may pass errors that may be more descriptive than
354354
// ECONNREFUSED they are not necessarily the expected error code e.g.
355355
// ENOTFOUND on getaddrinfo seems to be node.js specific, so using ECONNREFUSED
356356
// is still probably the most useful thing to do.
357357
sock.error = {{{ cDefs.ECONNREFUSED }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
358358
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
359-
// don't throw
360359
});
361-
} else {
362-
peer.socket.onopen = handleOpen;
363-
peer.socket.onclose = function() {
364-
SOCKFS.emit('close', sock.stream.fd);
365-
};
366-
peer.socket.onmessage = function peer_socket_onmessage(event) {
367-
handleMessage(event.data);
368-
};
369-
peer.socket.onerror = function(error) {
370-
// The WebSocket spec only allows a 'simple event' to be thrown on error,
371-
// so we only really know as much as ECONNREFUSED.
372-
sock.error = {{{ cDefs.ECONNREFUSED }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
373-
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
374-
};
360+
return;
375361
}
362+
#endif
363+
peer.socket.onopen = handleOpen;
364+
peer.socket.onclose = () => SOCKFS.emit('close', sock.stream.fd);
365+
peer.socket.onmessage = (event) => handleMessage(event.data);
366+
peer.socket.onerror = (error) => {
367+
// The WebSocket spec only allows a 'simple event' to be thrown on error,
368+
// so we only really know as much as ECONNREFUSED.
369+
sock.error = {{{ cDefs.ECONNREFUSED }}}; // Used in getsockopt for SOL_SOCKET/SO_ERROR test.
370+
SOCKFS.emit('error', [sock.stream.fd, sock.error, 'ECONNREFUSED: Connection refused']);
371+
};
376372
},
377373

378374
//
@@ -534,7 +530,7 @@ addToLibrary({
534530
});
535531
SOCKFS.emit('listen', sock.stream.fd); // Send Event with listen fd.
536532

537-
sock.server.on('connection', function(ws) {
533+
sock.server.on('connection', (ws) => {
538534
#if SOCKET_DEBUG
539535
dbg(`websocket: received connection from: ${ws._socket.remoteAddress}:${ws._socket.remotePort}`);
540536
#endif
@@ -557,11 +553,11 @@ addToLibrary({
557553
SOCKFS.emit('connection', sock.stream.fd);
558554
}
559555
});
560-
sock.server.on('close', function() {
556+
sock.server.on('close', () => {
561557
SOCKFS.emit('close', sock.stream.fd);
562558
sock.server = null;
563559
});
564-
sock.server.on('error', function(error) {
560+
sock.server.on('error', (error) => {
565561
// Although the ws library may pass errors that may be more descriptive than
566562
// ECONNREFUSED they are not necessarily the expected error code e.g.
567563
// ENOTFOUND on getaddrinfo seems to be node.js specific, so using EHOSTUNREACH

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": 244278,
33
"a.out.nodebug.wasm": 577478,
4-
"total": 821821,
4+
"total": 821756,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)