Skip to content

Commit

Permalink
[minor] Improve JSDoc-inferred types
Browse files Browse the repository at this point in the history
Refs: #1910
  • Loading branch information
jimmywarting authored and lpinca committed Jul 7, 2021
1 parent 0ad1f9d commit a8cdb46
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/receiver.js
Expand Up @@ -22,7 +22,7 @@ const INFLATING = 5;
/**
* HyBi Receiver implementation.
*
* @extends stream.Writable
* @extends Writable
*/
class Receiver extends Writable {
/**
Expand Down Expand Up @@ -586,7 +586,7 @@ module.exports = Receiver;
/**
* Builds an error object.
*
* @param {(Error|RangeError)} ErrorCtor The error constructor
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
* @param {String} message The error message
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
* `message`
Expand Down
6 changes: 5 additions & 1 deletion lib/sender.js
@@ -1,5 +1,9 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */

'use strict';

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

const PerMessageDeflate = require('./permessage-deflate');
Expand All @@ -16,7 +20,7 @@ class Sender {
/**
* Creates a Sender instance.
*
* @param {net.Socket} socket The connection socket
* @param {(net.Socket|tls.Socket)} socket The connection socket
* @param {Object} [extensions] An object containing the negotiated extensions
*/
constructor(socket, extensions) {
Expand Down
4 changes: 2 additions & 2 deletions lib/stream.js
Expand Up @@ -5,7 +5,7 @@ const { Duplex } = require('stream');
/**
* Emits the `'close'` event on a stream.
*
* @param {stream.Duplex} The stream.
* @param {Duplex} stream The stream.
* @private
*/
function emitClose(stream) {
Expand Down Expand Up @@ -43,7 +43,7 @@ function duplexOnError(err) {
*
* @param {WebSocket} ws The `WebSocket` to wrap
* @param {Object} [options] The options for the `Duplex` constructor
* @return {stream.Duplex} The duplex stream
* @return {Duplex} The duplex stream
* @public
*/
function createWebSocketStream(ws, options) {
Expand Down
26 changes: 17 additions & 9 deletions lib/websocket-server.js
@@ -1,8 +1,13 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */

'use strict';

const EventEmitter = require('events');
const http = require('http');
const https = require('https');
const net = require('net');
const tls = require('tls');
const { createHash } = require('crypto');
const { createServer, STATUS_CODES } = require('http');

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

if (options.port != null) {
this._server = createServer((req, res) => {
const body = STATUS_CODES[426];
this._server = http.createServer((req, res) => {
const body = http.STATUS_CODES[426];

res.writeHead(426, {
'Content-Length': body.length,
Expand Down Expand Up @@ -173,7 +179,8 @@ class WebSocketServer extends EventEmitter {
* Handle a HTTP Upgrade request.
*
* @param {http.IncomingMessage} req The request object
* @param {net.Socket} socket The network socket between the server and client
* @param {(net.Socket|tls.Socket)} 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 @@ -252,7 +259,8 @@ class WebSocketServer extends EventEmitter {
* @param {String} key The value of the `Sec-WebSocket-Key` header
* @param {Object} extensions The accepted extensions
* @param {http.IncomingMessage} req The request object
* @param {net.Socket} socket The network socket between the server and client
* @param {(net.Socket|tls.Socket)} 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 @@ -375,15 +383,15 @@ function socketOnError() {
/**
* Close the connection when preconditions are not fulfilled.
*
* @param {net.Socket} socket The socket of the upgrade request
* @param {(net.Socket|tls.Socket)} 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
* @private
*/
function abortHandshake(socket, code, message, headers) {
if (socket.writable) {
message = message || STATUS_CODES[code];
message = message || http.STATUS_CODES[code];
headers = {
Connection: 'close',
'Content-Type': 'text/html',
Expand All @@ -392,7 +400,7 @@ function abortHandshake(socket, code, message, headers) {
};

socket.write(
`HTTP/1.1 ${code} ${STATUS_CODES[code]}\r\n` +
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
Object.keys(headers)
.map((h) => `${h}: ${headers[h]}`)
.join('\r\n') +
Expand Down
15 changes: 10 additions & 5 deletions lib/websocket.js
Expand Up @@ -30,13 +30,17 @@ const closeTimeout = 30 * 1000;
/**
* Class representing a WebSocket.
*
* @property {Number} CONNECTING The connection is not yet open
* @property {Number} OPEN The connection is open and ready to communicate
* @property {Number} CLOSING The connection is in the process of closing
* @property {Number} CLOSED The connection is closed
* @extends EventEmitter
*/
class WebSocket extends EventEmitter {
/**
* Create a new `WebSocket`.
*
* @param {(String|url.URL)} address The URL to which to connect
* @param {(String|URL)} address The URL to which to connect
* @param {(String|String[])} [protocols] The subprotocols
* @param {Object} [options] Connection options
*/
Expand Down Expand Up @@ -136,7 +140,8 @@ class WebSocket extends EventEmitter {
/**
* Set up the socket and the internal resources.
*
* @param {net.Socket} socket The network socket between the server and client
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Number} [maxPayload=0] The maximum allowed message size
* @private
Expand Down Expand Up @@ -460,7 +465,7 @@ module.exports = WebSocket;
* Initialize a WebSocket client.
*
* @param {WebSocket} websocket The client to initialize
* @param {(String|url.URL)} address The URL to which to connect
* @param {(String|URL)} address The URL to which to connect
* @param {String} [protocols] The subprotocols
* @param {Object} [options] Connection options
* @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
Expand Down Expand Up @@ -744,8 +749,8 @@ function tlsConnect(options) {
* Abort the handshake and emit an error.
*
* @param {WebSocket} websocket The WebSocket instance
* @param {(http.ClientRequest|net.Socket)} stream The request to abort or the
* socket to destroy
* @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
* abort or the socket to destroy
* @param {String} message The error message
* @private
*/
Expand Down

0 comments on commit a8cdb46

Please sign in to comment.