Skip to content

Commit 62521f2

Browse files
committed
[minor] Fix nits
1 parent 31da417 commit 62521f2

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

lib/sender.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */
1+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */
22

33
'use strict';
44

5-
const net = require('net');
6-
const tls = require('tls');
5+
const { Duplex } = require('stream');
76
const { randomFillSync } = require('crypto');
87

98
const PerMessageDeflate = require('./permessage-deflate');
@@ -21,7 +20,7 @@ class Sender {
2120
/**
2221
* Creates a Sender instance.
2322
*
24-
* @param {(net.Socket|tls.Socket)} socket The connection socket
23+
* @param {Duplex} socket The connection socket
2524
* @param {Object} [extensions] An object containing the negotiated extensions
2625
* @param {Function} [generateMask] The function used to generate the masking
2726
* key

lib/websocket-server.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
1+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex$" }] */
22

33
'use strict';
44

55
const EventEmitter = require('events');
66
const http = require('http');
7-
const https = require('https');
8-
const net = require('net');
9-
const tls = require('tls');
7+
const { Duplex } = require('stream');
108
const { createHash } = require('crypto');
119

1210
const extension = require('./extension');
@@ -221,8 +219,7 @@ class WebSocketServer extends EventEmitter {
221219
* Handle a HTTP Upgrade request.
222220
*
223221
* @param {http.IncomingMessage} req The request object
224-
* @param {(net.Socket|tls.Socket)} socket The network socket between the
225-
* server and client
222+
* @param {Duplex} socket The network socket between the server and client
226223
* @param {Buffer} head The first packet of the upgraded stream
227224
* @param {Function} cb Callback
228225
* @public
@@ -346,8 +343,7 @@ class WebSocketServer extends EventEmitter {
346343
* @param {String} key The value of the `Sec-WebSocket-Key` header
347344
* @param {Set} protocols The subprotocols
348345
* @param {http.IncomingMessage} req The request object
349-
* @param {(net.Socket|tls.Socket)} socket The network socket between the
350-
* server and client
346+
* @param {Duplex} socket The network socket between the server and client
351347
* @param {Buffer} head The first packet of the upgraded stream
352348
* @param {Function} cb Callback
353349
* @throws {Error} If called more than once with the same socket
@@ -477,7 +473,7 @@ function socketOnError() {
477473
/**
478474
* Close the connection when preconditions are not fulfilled.
479475
*
480-
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
476+
* @param {Duplex} socket The socket of the upgrade request
481477
* @param {Number} code The HTTP response status code
482478
* @param {String} [message] The HTTP response body
483479
* @param {Object} [headers] Additional HTTP response headers
@@ -518,7 +514,7 @@ function abortHandshake(socket, code, message, headers) {
518514
*
519515
* @param {WebSocketServer} server The WebSocket server
520516
* @param {http.IncomingMessage} req The request object
521-
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
517+
* @param {Duplex} socket The socket of the upgrade request
522518
* @param {Number} code The HTTP response status code
523519
* @param {String} message The HTTP response body
524520
* @private

lib/websocket.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Readable$" }] */
1+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex|Readable$" }] */
22

33
'use strict';
44

@@ -8,7 +8,7 @@ const http = require('http');
88
const net = require('net');
99
const tls = require('tls');
1010
const { randomBytes, createHash } = require('crypto');
11-
const { Readable } = require('stream');
11+
const { Duplex, Readable } = require('stream');
1212
const { URL } = require('url');
1313

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

226-
// These methods may not be available if `socket` is actually just a stream:
227-
if (socket.setTimeout) {
228-
socket.setTimeout(0);
229-
}
230-
if (socket.setNoDelay) {
231-
socket.setNoDelay();
232-
}
225+
//
226+
// These methods may not be available if `socket` is just a `Duplex`.
227+
//
228+
if (socket.setTimeout) socket.setTimeout(0);
229+
if (socket.setNoDelay) socket.setNoDelay();
233230

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

@@ -1229,7 +1226,7 @@ function resume(stream) {
12291226
}
12301227

12311228
/**
1232-
* The listener of the `net.Socket` `'close'` event.
1229+
* The listener of the socket `'close'` event.
12331230
*
12341231
* @private
12351232
*/
@@ -1280,7 +1277,7 @@ function socketOnClose() {
12801277
}
12811278

12821279
/**
1283-
* The listener of the `net.Socket` `'data'` event.
1280+
* The listener of the socket `'data'` event.
12841281
*
12851282
* @param {Buffer} chunk A chunk of data
12861283
* @private
@@ -1292,7 +1289,7 @@ function socketOnData(chunk) {
12921289
}
12931290

12941291
/**
1295-
* The listener of the `net.Socket` `'end'` event.
1292+
* The listener of the socket `'end'` event.
12961293
*
12971294
* @private
12981295
*/
@@ -1305,7 +1302,7 @@ function socketOnEnd() {
13051302
}
13061303

13071304
/**
1308-
* The listener of the `net.Socket` `'error'` event.
1305+
* The listener of the socket `'error'` event.
13091306
*
13101307
* @private
13111308
*/

test/websocket-server.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,19 +516,24 @@ describe('WebSocketServer', () => {
516516
});
517517
});
518518

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

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

525525
server.on('upgrade', (req, socket, head) => {
526-
// Put a stream between the raw socket and our websocket processing:
526+
//
527+
// Put a stream between the raw socket and our websocket processing.
528+
//
527529
const { socket1: stream1, socket2: stream2 } = new DuplexPair();
530+
528531
socket.pipe(stream1);
529532
stream1.pipe(socket);
530533

531-
// Pass the other side of the stream as the socket to upgrade:
534+
//
535+
// Pass the other side of the stream as the socket to upgrade.
536+
//
532537
wss.handleUpgrade(req, stream2, head, (ws) => {
533538
ws.send('hello');
534539
ws.close();

0 commit comments

Comments
 (0)