Skip to content

Commit

Permalink
[minor] Fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Aug 31, 2023
1 parent 31da417 commit 62521f2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
7 changes: 3 additions & 4 deletions lib/sender.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */

'use strict';

const net = require('net');
const tls = require('tls');
const { Duplex } = require('stream');
const { randomFillSync } = require('crypto');

const PerMessageDeflate = require('./permessage-deflate');
Expand All @@ -21,7 +20,7 @@ class Sender {
/**
* Creates a Sender instance.
*
* @param {(net.Socket|tls.Socket)} socket The connection socket
* @param {Duplex} socket The connection socket
* @param {Object} [extensions] An object containing the negotiated extensions
* @param {Function} [generateMask] The function used to generate the masking
* key
Expand Down
16 changes: 6 additions & 10 deletions lib/websocket-server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex$" }] */

'use strict';

const EventEmitter = require('events');
const http = require('http');
const https = require('https');
const net = require('net');
const tls = require('tls');
const { Duplex } = require('stream');
const { createHash } = require('crypto');

const extension = require('./extension');
Expand Down Expand Up @@ -221,8 +219,7 @@ class WebSocketServer extends EventEmitter {
* Handle a HTTP Upgrade request.
*
* @param {http.IncomingMessage} req The request object
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Duplex} socket The network socket between the server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Function} cb Callback
* @public
Expand Down Expand Up @@ -346,8 +343,7 @@ class WebSocketServer extends EventEmitter {
* @param {String} key The value of the `Sec-WebSocket-Key` header
* @param {Set} protocols The subprotocols
* @param {http.IncomingMessage} req The request object
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Duplex} socket The network socket between the server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Function} cb Callback
* @throws {Error} If called more than once with the same socket
Expand Down Expand Up @@ -477,7 +473,7 @@ function socketOnError() {
/**
* Close the connection when preconditions are not fulfilled.
*
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
* @param {Duplex} socket The socket of the upgrade request
* @param {Number} code The HTTP response status code
* @param {String} [message] The HTTP response body
* @param {Object} [headers] Additional HTTP response headers
Expand Down Expand Up @@ -518,7 +514,7 @@ function abortHandshake(socket, code, message, headers) {
*
* @param {WebSocketServer} server The WebSocket server
* @param {http.IncomingMessage} req The request object
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
* @param {Duplex} socket The socket of the upgrade request
* @param {Number} code The HTTP response status code
* @param {String} message The HTTP response body
* @private
Expand Down
27 changes: 12 additions & 15 deletions lib/websocket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Readable$" }] */
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex|Readable$" }] */

'use strict';

Expand All @@ -8,7 +8,7 @@ const http = require('http');
const net = require('net');
const tls = require('tls');
const { randomBytes, createHash } = require('crypto');
const { Readable } = require('stream');
const { Duplex, Readable } = require('stream');
const { URL } = require('url');

const PerMessageDeflate = require('./permessage-deflate');
Expand Down Expand Up @@ -189,8 +189,7 @@ class WebSocket extends EventEmitter {
/**
* Set up the socket and the internal resources.
*
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Duplex} socket The network socket between the server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Object} options Options object
* @param {Function} [options.generateMask] The function used to generate the
Expand Down Expand Up @@ -223,13 +222,11 @@ class WebSocket extends EventEmitter {
receiver.on('ping', receiverOnPing);
receiver.on('pong', receiverOnPong);

// These methods may not be available if `socket` is actually just a stream:
if (socket.setTimeout) {
socket.setTimeout(0);
}
if (socket.setNoDelay) {
socket.setNoDelay();
}
//
// These methods may not be available if `socket` is just a `Duplex`.
//
if (socket.setTimeout) socket.setTimeout(0);
if (socket.setNoDelay) socket.setNoDelay();

if (head.length > 0) socket.unshift(head);

Expand Down Expand Up @@ -1229,7 +1226,7 @@ function resume(stream) {
}

/**
* The listener of the `net.Socket` `'close'` event.
* The listener of the socket `'close'` event.
*
* @private
*/
Expand Down Expand Up @@ -1280,7 +1277,7 @@ function socketOnClose() {
}

/**
* The listener of the `net.Socket` `'data'` event.
* The listener of the socket `'data'` event.
*
* @param {Buffer} chunk A chunk of data
* @private
Expand All @@ -1292,7 +1289,7 @@ function socketOnData(chunk) {
}

/**
* The listener of the `net.Socket` `'end'` event.
* The listener of the socket `'end'` event.
*
* @private
*/
Expand All @@ -1305,7 +1302,7 @@ function socketOnEnd() {
}

/**
* The listener of the `net.Socket` `'error'` event.
* The listener of the socket `'error'` event.
*
* @private
*/
Expand Down
11 changes: 8 additions & 3 deletions test/websocket-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,19 +516,24 @@ describe('WebSocketServer', () => {
});
});

it('can complete a WebSocket upgrade over any duplex stream', (done) => {
it('completes a WebSocket upgrade over any duplex stream', (done) => {
const server = http.createServer();

server.listen(0, () => {
const wss = new WebSocket.Server({ noServer: true });

server.on('upgrade', (req, socket, head) => {
// Put a stream between the raw socket and our websocket processing:
//
// Put a stream between the raw socket and our websocket processing.
//
const { socket1: stream1, socket2: stream2 } = new DuplexPair();

socket.pipe(stream1);
stream1.pipe(socket);

// Pass the other side of the stream as the socket to upgrade:
//
// Pass the other side of the stream as the socket to upgrade.
//
wss.handleUpgrade(req, stream2, head, (ws) => {
ws.send('hello');
ws.close();
Expand Down

0 comments on commit 62521f2

Please sign in to comment.