Skip to content

Commit ecb9d9e

Browse files
authored
[minor] Improve JSDoc-inferred types (#1912)
Refs: #1910
1 parent 0ad1f9d commit ecb9d9e

File tree

5 files changed

+145
-36
lines changed

5 files changed

+145
-36
lines changed

lib/receiver.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const INFLATING = 5;
2222
/**
2323
* HyBi Receiver implementation.
2424
*
25-
* @extends stream.Writable
25+
* @extends Writable
2626
*/
2727
class Receiver extends Writable {
2828
/**
@@ -586,7 +586,7 @@ module.exports = Receiver;
586586
/**
587587
* Builds an error object.
588588
*
589-
* @param {(Error|RangeError)} ErrorCtor The error constructor
589+
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
590590
* @param {String} message The error message
591591
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
592592
* `message`

lib/sender.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */
2+
13
'use strict';
24

5+
const net = require('net');
6+
const tls = require('tls');
37
const { randomFillSync } = require('crypto');
48

59
const PerMessageDeflate = require('./permessage-deflate');
@@ -16,7 +20,7 @@ class Sender {
1620
/**
1721
* Creates a Sender instance.
1822
*
19-
* @param {net.Socket} socket The connection socket
23+
* @param {(net.Socket|tls.Socket)} socket The connection socket
2024
* @param {Object} [extensions] An object containing the negotiated extensions
2125
*/
2226
constructor(socket, extensions) {

lib/stream.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { Duplex } = require('stream');
55
/**
66
* Emits the `'close'` event on a stream.
77
*
8-
* @param {stream.Duplex} The stream.
8+
* @param {Duplex} stream The stream.
99
* @private
1010
*/
1111
function emitClose(stream) {
@@ -43,7 +43,7 @@ function duplexOnError(err) {
4343
*
4444
* @param {WebSocket} ws The `WebSocket` to wrap
4545
* @param {Object} [options] The options for the `Duplex` constructor
46-
* @return {stream.Duplex} The duplex stream
46+
* @return {Duplex} The duplex stream
4747
* @public
4848
*/
4949
function createWebSocketStream(ws, options) {

lib/websocket-server.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
2+
13
'use strict';
24

35
const EventEmitter = require('events');
6+
const http = require('http');
7+
const https = require('https');
8+
const net = require('net');
9+
const tls = require('tls');
410
const { createHash } = require('crypto');
5-
const { createServer, STATUS_CODES } = require('http');
611

712
const PerMessageDeflate = require('./permessage-deflate');
813
const WebSocket = require('./websocket');
@@ -34,7 +39,8 @@ class WebSocketServer extends EventEmitter {
3439
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
3540
* permessage-deflate
3641
* @param {Number} [options.port] The port where to bind the server
37-
* @param {http.Server} [options.server] A pre-created HTTP/S server to use
42+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
43+
* server to use
3844
* @param {Function} [options.verifyClient] A hook to reject connections
3945
* @param {Function} [callback] A listener for the `listening` event
4046
*/
@@ -63,8 +69,8 @@ class WebSocketServer extends EventEmitter {
6369
}
6470

6571
if (options.port != null) {
66-
this._server = createServer((req, res) => {
67-
const body = STATUS_CODES[426];
72+
this._server = http.createServer((req, res) => {
73+
const body = http.STATUS_CODES[426];
6874

6975
res.writeHead(426, {
7076
'Content-Length': body.length,
@@ -173,7 +179,8 @@ class WebSocketServer extends EventEmitter {
173179
* Handle a HTTP Upgrade request.
174180
*
175181
* @param {http.IncomingMessage} req The request object
176-
* @param {net.Socket} socket The network socket between the server and client
182+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
183+
* server and client
177184
* @param {Buffer} head The first packet of the upgraded stream
178185
* @param {Function} cb Callback
179186
* @public
@@ -252,7 +259,8 @@ class WebSocketServer extends EventEmitter {
252259
* @param {String} key The value of the `Sec-WebSocket-Key` header
253260
* @param {Object} extensions The accepted extensions
254261
* @param {http.IncomingMessage} req The request object
255-
* @param {net.Socket} socket The network socket between the server and client
262+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
263+
* server and client
256264
* @param {Buffer} head The first packet of the upgraded stream
257265
* @param {Function} cb Callback
258266
* @throws {Error} If called more than once with the same socket
@@ -375,15 +383,15 @@ function socketOnError() {
375383
/**
376384
* Close the connection when preconditions are not fulfilled.
377385
*
378-
* @param {net.Socket} socket The socket of the upgrade request
386+
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
379387
* @param {Number} code The HTTP response status code
380388
* @param {String} [message] The HTTP response body
381389
* @param {Object} [headers] Additional HTTP response headers
382390
* @private
383391
*/
384392
function abortHandshake(socket, code, message, headers) {
385393
if (socket.writable) {
386-
message = message || STATUS_CODES[code];
394+
message = message || http.STATUS_CODES[code];
387395
headers = {
388396
Connection: 'close',
389397
'Content-Type': 'text/html',
@@ -392,7 +400,7 @@ function abortHandshake(socket, code, message, headers) {
392400
};
393401

394402
socket.write(
395-
`HTTP/1.1 ${code} ${STATUS_CODES[code]}\r\n` +
403+
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
396404
Object.keys(headers)
397405
.map((h) => `${h}: ${headers[h]}`)
398406
.join('\r\n') +

lib/websocket.js

+119-22
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class WebSocket extends EventEmitter {
3636
/**
3737
* Create a new `WebSocket`.
3838
*
39-
* @param {(String|url.URL)} address The URL to which to connect
39+
* @param {(String|URL)} address The URL to which to connect
4040
* @param {(String|String[])} [protocols] The subprotocols
4141
* @param {Object} [options] Connection options
4242
*/
@@ -112,6 +112,50 @@ class WebSocket extends EventEmitter {
112112
return Object.keys(this._extensions).join();
113113
}
114114

115+
/**
116+
* @type {Function}
117+
*/
118+
/* istanbul ignore next */
119+
get onclose() {
120+
return undefined;
121+
}
122+
123+
/* istanbul ignore next */
124+
set onclose(listener) {}
125+
126+
/**
127+
* @type {Function}
128+
*/
129+
/* istanbul ignore next */
130+
get onerror() {
131+
return null;
132+
}
133+
134+
/* istanbul ignore next */
135+
set onerror(listener) {}
136+
137+
/**
138+
* @type {Function}
139+
*/
140+
/* istanbul ignore next */
141+
get onopen() {
142+
return undefined;
143+
}
144+
145+
/* istanbul ignore next */
146+
set onopen(listener) {}
147+
148+
/**
149+
* @type {Function}
150+
*/
151+
/* istanbul ignore next */
152+
get onmessage() {
153+
return undefined;
154+
}
155+
156+
/* istanbul ignore next */
157+
set onmessage(listener) {}
158+
115159
/**
116160
* @type {String}
117161
*/
@@ -136,7 +180,8 @@ class WebSocket extends EventEmitter {
136180
/**
137181
* Set up the socket and the internal resources.
138182
*
139-
* @param {net.Socket} socket The network socket between the server and client
183+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
184+
* server and client
140185
* @param {Buffer} head The first packet of the upgraded stream
141186
* @param {Number} [maxPayload=0] The maximum allowed message size
142187
* @private
@@ -392,11 +437,76 @@ class WebSocket extends EventEmitter {
392437
}
393438
}
394439

395-
readyStates.forEach((readyState, i) => {
396-
const descriptor = { enumerable: true, value: i };
440+
/**
441+
* @constant {Number} CONNECTING
442+
* @memberof WebSocket
443+
*/
444+
Object.defineProperty(WebSocket, 'CONNECTING', {
445+
enumerable: true,
446+
value: readyStates.indexOf('CONNECTING')
447+
});
397448

398-
Object.defineProperty(WebSocket.prototype, readyState, descriptor);
399-
Object.defineProperty(WebSocket, readyState, descriptor);
449+
/**
450+
* @constant {Number} CONNECTING
451+
* @memberof WebSocket.prototype
452+
*/
453+
Object.defineProperty(WebSocket.prototype, 'CONNECTING', {
454+
enumerable: true,
455+
value: readyStates.indexOf('CONNECTING')
456+
});
457+
458+
/**
459+
* @constant {Number} OPEN
460+
* @memberof WebSocket
461+
*/
462+
Object.defineProperty(WebSocket, 'OPEN', {
463+
enumerable: true,
464+
value: readyStates.indexOf('OPEN')
465+
});
466+
467+
/**
468+
* @constant {Number} OPEN
469+
* @memberof WebSocket.prototype
470+
*/
471+
Object.defineProperty(WebSocket.prototype, 'OPEN', {
472+
enumerable: true,
473+
value: readyStates.indexOf('OPEN')
474+
});
475+
476+
/**
477+
* @constant {Number} CLOSING
478+
* @memberof WebSocket
479+
*/
480+
Object.defineProperty(WebSocket, 'CLOSING', {
481+
enumerable: true,
482+
value: readyStates.indexOf('CLOSING')
483+
});
484+
485+
/**
486+
* @constant {Number} CLOSING
487+
* @memberof WebSocket.prototype
488+
*/
489+
Object.defineProperty(WebSocket.prototype, 'CLOSING', {
490+
enumerable: true,
491+
value: readyStates.indexOf('CLOSING')
492+
});
493+
494+
/**
495+
* @constant {Number} CLOSED
496+
* @memberof WebSocket
497+
*/
498+
Object.defineProperty(WebSocket, 'CLOSED', {
499+
enumerable: true,
500+
value: readyStates.indexOf('CLOSED')
501+
});
502+
503+
/**
504+
* @constant {Number} CLOSED
505+
* @memberof WebSocket.prototype
506+
*/
507+
Object.defineProperty(WebSocket.prototype, 'CLOSED', {
508+
enumerable: true,
509+
value: readyStates.indexOf('CLOSED')
400510
});
401511

402512
[
@@ -416,14 +526,7 @@ readyStates.forEach((readyState, i) => {
416526
//
417527
['open', 'error', 'close', 'message'].forEach((method) => {
418528
Object.defineProperty(WebSocket.prototype, `on${method}`, {
419-
configurable: true,
420529
enumerable: true,
421-
/**
422-
* Return the listener of the event.
423-
*
424-
* @return {(Function|undefined)} The event listener or `undefined`
425-
* @public
426-
*/
427530
get() {
428531
const listeners = this.listeners(method);
429532
for (let i = 0; i < listeners.length; i++) {
@@ -432,12 +535,6 @@ readyStates.forEach((readyState, i) => {
432535

433536
return undefined;
434537
},
435-
/**
436-
* Add a listener for the event.
437-
*
438-
* @param {Function} listener The listener to add
439-
* @public
440-
*/
441538
set(listener) {
442539
const listeners = this.listeners(method);
443540
for (let i = 0; i < listeners.length; i++) {
@@ -460,7 +557,7 @@ module.exports = WebSocket;
460557
* Initialize a WebSocket client.
461558
*
462559
* @param {WebSocket} websocket The client to initialize
463-
* @param {(String|url.URL)} address The URL to which to connect
560+
* @param {(String|URL)} address The URL to which to connect
464561
* @param {String} [protocols] The subprotocols
465562
* @param {Object} [options] Connection options
466563
* @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
@@ -744,8 +841,8 @@ function tlsConnect(options) {
744841
* Abort the handshake and emit an error.
745842
*
746843
* @param {WebSocket} websocket The WebSocket instance
747-
* @param {(http.ClientRequest|net.Socket)} stream The request to abort or the
748-
* socket to destroy
844+
* @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
845+
* abort or the socket to destroy
749846
* @param {String} message The error message
750847
* @private
751848
*/

0 commit comments

Comments
 (0)