From d5452a661eda06914cfd4477dd2bfaba131d6ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Fri, 12 Aug 2016 18:22:22 +0200 Subject: [PATCH 01/15] Move listenAfterLookup out of listen * move `listenAfterLookup` out of `Server.prototype.listen` * rename `listenAfterLookup` to `lookupAndListen` for consistency with `lookupAndConnect` * self -> this --- lib/net.js | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/net.js b/lib/net.js index ad799567b094c1..a91d6c48b4fe01 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1326,11 +1326,9 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) { Server.prototype.listen = function() { - var self = this; - var lastArg = arguments[arguments.length - 1]; if (typeof lastArg === 'function') { - self.once('listening', lastArg); + this.once('listening', lastArg); } var port = toNumber(arguments[0]); @@ -1341,16 +1339,16 @@ Server.prototype.listen = function() { if (arguments.length === 0 || typeof arguments[0] === 'function') { // Bind to a random port. - listen(self, null, 0, null, backlog); + listen(this, null, 0, null, backlog); } else if (arguments[0] !== null && typeof arguments[0] === 'object') { var h = arguments[0]; h = h._handle || h.handle || h; if (h instanceof TCP) { - self._handle = h; - listen(self, null, -1, -1, backlog); + this._handle = h; + listen(this, null, -1, -1, backlog); } else if (typeof h.fd === 'number' && h.fd >= 0) { - listen(self, null, null, null, backlog, h.fd); + listen(this, null, null, null, backlog, h.fd); } else { // The first argument is a configuration object if (h.backlog) @@ -1362,48 +1360,48 @@ Server.prototype.listen = function() { // with net.connect(). assertPort(h.port); if (h.host) - listenAfterLookup(h.port | 0, h.host, backlog, h.exclusive); + lookupAndListen(this, h.port | 0, h.host, backlog, h.exclusive); else - listen(self, null, h.port | 0, 4, backlog, undefined, h.exclusive); + listen(this, null, h.port | 0, 4, backlog, undefined, h.exclusive); } else if (h.path && isPipeName(h.path)) { - const pipeName = self._pipeName = h.path; - listen(self, pipeName, -1, -1, backlog, undefined, h.exclusive); + const pipeName = this._pipeName = h.path; + listen(this, pipeName, -1, -1, backlog, undefined, h.exclusive); } else { throw new Error('Invalid listen argument: ' + h); } } } else if (isPipeName(arguments[0])) { // UNIX socket or Windows pipe. - const pipeName = self._pipeName = arguments[0]; - listen(self, pipeName, -1, -1, backlog); + const pipeName = this._pipeName = arguments[0]; + listen(this, pipeName, -1, -1, backlog); } else if (arguments[1] === undefined || typeof arguments[1] === 'function' || typeof arguments[1] === 'number') { // The first argument is the port, no IP given. assertPort(port); - listen(self, null, port, 4, backlog); + listen(this, null, port, 4, backlog); } else { // The first argument is the port, the second an IP. assertPort(port); - listenAfterLookup(port, arguments[1], backlog); - } - - function listenAfterLookup(port, address, backlog, exclusive) { - require('dns').lookup(address, function(err, ip, addressType) { - if (err) { - self.emit('error', err); - } else { - addressType = ip ? addressType : 4; - listen(self, ip, port, addressType, backlog, undefined, exclusive); - } - }); + lookupAndListen(this, port, arguments[1], backlog); } - return self; + return this; }; +function lookupAndListen(self, port, address, backlog, exclusive) { + require('dns').lookup(address, function(err, ip, addressType) { + if (err) { + self.emit('error', err); + } else { + addressType = ip ? addressType : 4; + listen(self, ip, port, addressType, backlog, undefined, exclusive); + } + }); +} + Object.defineProperty(Server.prototype, 'listening', { get: function() { return !!this._handle; From 88c44c1ffa1b54ae6741b81f7a0c789753652fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Sat, 13 Aug 2016 15:52:19 +0200 Subject: [PATCH 02/15] Use normalizeConnectArgs in listen This is mostly preparation, `options` will be used later. --- lib/net.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index a91d6c48b4fe01..f9db615329e59a 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1326,9 +1326,20 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) { Server.prototype.listen = function() { - var lastArg = arguments[arguments.length - 1]; - if (typeof lastArg === 'function') { - this.once('listening', lastArg); + const argsLen = arguments.length; + var args = new Array(argsLen); + for (var i = 0; i < argsLen; i++) + args[i] = arguments[i]; + args = normalizeConnectArgs(args); + + if (args[1]) { + this.once('listening', args[1]); + } + + var options = args[0]; + if (arguments.length === 0 || typeof arguments[0] === 'function') { + // Bind to a random port. + options.port = 0; } var port = toNumber(arguments[0]); @@ -1337,6 +1348,8 @@ Server.prototype.listen = function() { // When the ip is omitted it can be the second argument. var backlog = toNumber(arguments[1]) || toNumber(arguments[2]); + options = options._handle || options.handle || options; + if (arguments.length === 0 || typeof arguments[0] === 'function') { // Bind to a random port. listen(this, null, 0, null, backlog); From df8d8c851de7cc182038b87032860a05c12f1878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Sat, 13 Aug 2016 15:53:27 +0200 Subject: [PATCH 03/15] Replace h with options --- lib/net.js | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/net.js b/lib/net.js index f9db615329e59a..ef7c364ac1e84e 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1354,33 +1354,30 @@ Server.prototype.listen = function() { // Bind to a random port. listen(this, null, 0, null, backlog); } else if (arguments[0] !== null && typeof arguments[0] === 'object') { - var h = arguments[0]; - h = h._handle || h.handle || h; - - if (h instanceof TCP) { - this._handle = h; + if (options instanceof TCP) { + this._handle = options; listen(this, null, -1, -1, backlog); - } else if (typeof h.fd === 'number' && h.fd >= 0) { - listen(this, null, null, null, backlog, h.fd); + } else if (typeof options.fd === 'number' && options.fd >= 0) { + listen(this, null, null, null, backlog, options.fd); } else { // The first argument is a configuration object - if (h.backlog) - backlog = h.backlog; + if (options.backlog) + backlog = options.backlog; - if (typeof h.port === 'number' || typeof h.port === 'string' || - (typeof h.port === 'undefined' && 'port' in h)) { + if (typeof options.port === 'number' || typeof options.port === 'string' || + (typeof options.port === 'undefined' && 'port' in options)) { // Undefined is interpreted as zero (random port) for consistency // with net.connect(). - assertPort(h.port); - if (h.host) - lookupAndListen(this, h.port | 0, h.host, backlog, h.exclusive); + assertPort(options.port); + if (options.host) + lookupAndListen(this, options.port | 0, options.host, backlog, options.exclusive); else - listen(this, null, h.port | 0, 4, backlog, undefined, h.exclusive); - } else if (h.path && isPipeName(h.path)) { - const pipeName = this._pipeName = h.path; - listen(this, pipeName, -1, -1, backlog, undefined, h.exclusive); + listen(this, null, options.port | 0, 4, backlog, undefined, options.exclusive); + } else if (options.path && isPipeName(options.path)) { + const pipeName = this._pipeName = options.path; + listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive); } else { - throw new Error('Invalid listen argument: ' + h); + throw new Error('Invalid listen argument: ' + options); } } } else if (isPipeName(arguments[0])) { From 959582c75047976d8349413d994e35838b96dd69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Sat, 13 Aug 2016 16:25:42 +0200 Subject: [PATCH 04/15] Only deal with options object This removes the ifs directly dealing with arguments. --- lib/net.js | 69 ++++++++++++++++++------------------------------------ 1 file changed, 23 insertions(+), 46 deletions(-) diff --git a/lib/net.js b/lib/net.js index ef7c364ac1e84e..d3af202974fea7 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1342,60 +1342,37 @@ Server.prototype.listen = function() { options.port = 0; } - var port = toNumber(arguments[0]); - // The third optional argument is the backlog size. // When the ip is omitted it can be the second argument. var backlog = toNumber(arguments[1]) || toNumber(arguments[2]); options = options._handle || options.handle || options; - if (arguments.length === 0 || typeof arguments[0] === 'function') { - // Bind to a random port. - listen(this, null, 0, null, backlog); - } else if (arguments[0] !== null && typeof arguments[0] === 'object') { - if (options instanceof TCP) { - this._handle = options; - listen(this, null, -1, -1, backlog); - } else if (typeof options.fd === 'number' && options.fd >= 0) { - listen(this, null, null, null, backlog, options.fd); + if (options instanceof TCP) { + this._handle = options; + listen(this, null, -1, -1, backlog); + } else if (typeof options.fd === 'number' && options.fd >= 0) { + listen(this, null, null, null, backlog, options.fd); + } else { + if (options.backlog) + backlog = options.backlog; + + if (typeof options.port === 'number' || typeof options.port === 'string' || + (typeof options.port === 'undefined' && 'port' in options)) { + // Undefined is interpreted as zero (random port) for consistency + // with net.connect(). + assertPort(options.port); + if (options.host) + lookupAndListen(this, options.port | 0, options.host, backlog, options.exclusive); + else + listen(this, null, options.port | 0, 4, backlog, undefined, options.exclusive); + } else if (options.path && isPipeName(options.path)) { + // UNIX socket or Windows pipe. + const pipeName = this._pipeName = options.path; + listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive); } else { - // The first argument is a configuration object - if (options.backlog) - backlog = options.backlog; - - if (typeof options.port === 'number' || typeof options.port === 'string' || - (typeof options.port === 'undefined' && 'port' in options)) { - // Undefined is interpreted as zero (random port) for consistency - // with net.connect(). - assertPort(options.port); - if (options.host) - lookupAndListen(this, options.port | 0, options.host, backlog, options.exclusive); - else - listen(this, null, options.port | 0, 4, backlog, undefined, options.exclusive); - } else if (options.path && isPipeName(options.path)) { - const pipeName = this._pipeName = options.path; - listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive); - } else { - throw new Error('Invalid listen argument: ' + options); - } + throw new Error('Invalid listen argument: ' + options); } - } else if (isPipeName(arguments[0])) { - // UNIX socket or Windows pipe. - const pipeName = this._pipeName = arguments[0]; - listen(this, pipeName, -1, -1, backlog); - - } else if (arguments[1] === undefined || - typeof arguments[1] === 'function' || - typeof arguments[1] === 'number') { - // The first argument is the port, no IP given. - assertPort(port); - listen(this, null, port, 4, backlog); - - } else { - // The first argument is the port, the second an IP. - assertPort(port); - lookupAndListen(this, port, arguments[1], backlog); } return this; From d1c6f7ba49694dba2e7acec520c16ea48a03ee20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Sat, 13 Aug 2016 17:41:01 +0200 Subject: [PATCH 05/15] Fix linter errors --- lib/net.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/net.js b/lib/net.js index d3af202974fea7..920e5945fb42ba 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1363,9 +1363,11 @@ Server.prototype.listen = function() { // with net.connect(). assertPort(options.port); if (options.host) - lookupAndListen(this, options.port | 0, options.host, backlog, options.exclusive); + lookupAndListen(this, options.port | 0, options.host, backlog, + options.exclusive); else - listen(this, null, options.port | 0, 4, backlog, undefined, options.exclusive); + listen(this, null, options.port | 0, 4, backlog, undefined, + options.exclusive); } else if (options.path && isPipeName(options.path)) { // UNIX socket or Windows pipe. const pipeName = this._pipeName = options.path; From 708e1b40f708541407d23983358dd058250f4a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Sat, 13 Aug 2016 19:14:22 +0200 Subject: [PATCH 06/15] Test the new behavior --- test/parallel/test-net-listen-port-option.js | 61 +++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/test/parallel/test-net-listen-port-option.js b/test/parallel/test-net-listen-port-option.js index 77c9cb42f0a930..e6a6f8d538a6f3 100644 --- a/test/parallel/test-net-listen-port-option.js +++ b/test/parallel/test-net-listen-port-option.js @@ -1,28 +1,45 @@ 'use strict'; -var common = require('../common'); -var assert = require('assert'); -var net = require('net'); +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); function close() { this.close(); } -net.Server().listen({ port: undefined }, close); -net.Server().listen({ port: '' + common.PORT }, close); -[ 'nan', - -1, - 123.456, - 0x10000, - 1 / 0, - -1 / 0, - '+Infinity', - '-Infinity' -].forEach(function(port) { - assert.throws(function() { - net.Server().listen({ port: port }, common.fail); - }, /"port" argument must be >= 0 and < 65536/i); -}); +// From lib/net.js +function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; } + +function isPipeName(s) { + return typeof s === 'string' && toNumber(s) === false; +} + +const listenVariants = [ + (port, cb) => net.Server().listen({port}, cb), + (port, cb) => net.Server().listen(port, cb) +]; + +listenVariants.forEach((listenVariant, i) => { + listenVariant(undefined, common.mustCall(close)); + listenVariant('' + (common.PORT + i), common.mustCall(close)); + + [ + 'nan', + -1, + 123.456, + 0x10000, + 1 / 0, + -1 / 0, + '+Infinity', + '-Infinity' + ].forEach((port) => { + if (i === 1 && isPipeName(port)) { + // skip this, because listen(port) can also be listen(path) + return; + } + assert.throws(() => listenVariant(port, common.fail), + /"port" argument must be >= 0 and < 65536/i); + }); -[null, true, false].forEach(function(port) { - assert.throws(function() { - net.Server().listen({ port: port }, common.fail); - }, /invalid listen argument/i); + [null, true, false].forEach((port) => + assert.throws(() => listenVariant(port, common.fail), + /invalid listen argument/i)); }); From 150723dede375f7c9a318f3f8c37dc534b2f3a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Mon, 15 Aug 2016 19:04:26 +0200 Subject: [PATCH 07/15] Use argument.length directly as requested by @mscdex --- lib/net.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/net.js b/lib/net.js index 920e5945fb42ba..ecbb2c479767d4 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1326,9 +1326,8 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) { Server.prototype.listen = function() { - const argsLen = arguments.length; - var args = new Array(argsLen); - for (var i = 0; i < argsLen; i++) + var args = new Array(arguments.length); + for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]; args = normalizeConnectArgs(args); From 5ab4028071413f2e3a340109873c3f7aec13b6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Tue, 16 Aug 2016 18:26:43 +0200 Subject: [PATCH 08/15] Rename normalizeConnectArgs to normalizeArgs --- lib/_tls_wrap.js | 2 +- lib/net.js | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index c2ecd07a4b77bc..e1e4ab8af64ca6 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -952,7 +952,7 @@ function SNICallback(servername, callback) { // // function normalizeConnectArgs(listArgs) { - var args = net._normalizeConnectArgs(listArgs); + var args = net._normalizeArgs(listArgs); var options = args[0]; var cb = args[1]; diff --git a/lib/net.js b/lib/net.js index ecbb2c479767d4..5c5ac8ab2315d2 100644 --- a/lib/net.js +++ b/lib/net.js @@ -64,7 +64,7 @@ exports.connect = exports.createConnection = function() { var args = new Array(argsLen); for (var i = 0; i < argsLen; i++) args[i] = arguments[i]; - args = normalizeConnectArgs(args); + args = normalizeArgs(args); debug('createConnection', args); var s = new Socket(args[0]); return Socket.prototype.connect.apply(s, args); @@ -72,7 +72,8 @@ exports.connect = exports.createConnection = function() { // Returns an array [options] or [options, cb] // It is the same as the argument of Socket.prototype.connect(). -function normalizeConnectArgs(args) { +// This is also used by Server.prototype.listen(). +function normalizeArgs(args) { var options = {}; if (args[0] !== null && typeof args[0] === 'object') { @@ -92,7 +93,7 @@ function normalizeConnectArgs(args) { var cb = args[args.length - 1]; return typeof cb === 'function' ? [options, cb] : [options]; } -exports._normalizeConnectArgs = normalizeConnectArgs; +exports._normalizeArgs = normalizeArgs; // called when creating new Socket, or when re-using a closed Socket @@ -889,7 +890,7 @@ Socket.prototype.connect = function(options, cb) { var args = new Array(argsLen); for (var i = 0; i < argsLen; i++) args[i] = arguments[i]; - args = normalizeConnectArgs(args); + args = normalizeArgs(args); return Socket.prototype.connect.apply(this, args); } @@ -1329,7 +1330,7 @@ Server.prototype.listen = function() { var args = new Array(arguments.length); for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]; - args = normalizeConnectArgs(args); + args = normalizeArgs(args); if (args[1]) { this.once('listening', args[1]); From 0bffc03931a5c134221b45a6c7d401faf9087e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Tue, 16 Aug 2016 18:31:10 +0200 Subject: [PATCH 09/15] Use string interpolation --- test/parallel/test-net-listen-port-option.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-net-listen-port-option.js b/test/parallel/test-net-listen-port-option.js index e6a6f8d538a6f3..ec9effea6d0086 100644 --- a/test/parallel/test-net-listen-port-option.js +++ b/test/parallel/test-net-listen-port-option.js @@ -19,7 +19,7 @@ const listenVariants = [ listenVariants.forEach((listenVariant, i) => { listenVariant(undefined, common.mustCall(close)); - listenVariant('' + (common.PORT + i), common.mustCall(close)); + listenVariant(`${common.PORT + i}`, common.mustCall(close)); [ 'nan', From db798ea7c72123ffbb35a897ca908ede4a122643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Tue, 16 Aug 2016 18:35:19 +0200 Subject: [PATCH 10/15] Style: Use brackets around if --- lib/net.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/net.js b/lib/net.js index 5c5ac8ab2315d2..ca6b86789f8236 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1362,12 +1362,13 @@ Server.prototype.listen = function() { // Undefined is interpreted as zero (random port) for consistency // with net.connect(). assertPort(options.port); - if (options.host) + if (options.host) { lookupAndListen(this, options.port | 0, options.host, backlog, options.exclusive); - else + } else { listen(this, null, options.port | 0, 4, backlog, undefined, options.exclusive); + } } else if (options.path && isPipeName(options.path)) { // UNIX socket or Windows pipe. const pipeName = this._pipeName = options.path; From e772fa301f8f6e88cfa02740f8e26a352110c82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Tue, 16 Aug 2016 18:56:35 +0200 Subject: [PATCH 11/15] Test: Just use '0' for the port --- test/parallel/test-net-listen-port-option.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-net-listen-port-option.js b/test/parallel/test-net-listen-port-option.js index ec9effea6d0086..028ecdde784440 100644 --- a/test/parallel/test-net-listen-port-option.js +++ b/test/parallel/test-net-listen-port-option.js @@ -19,7 +19,7 @@ const listenVariants = [ listenVariants.forEach((listenVariant, i) => { listenVariant(undefined, common.mustCall(close)); - listenVariant(`${common.PORT + i}`, common.mustCall(close)); + listenVariant('0', common.mustCall(close)); [ 'nan', From 3fb143576e2705c8822c7bb8be253f01536862d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Tue, 16 Aug 2016 19:03:49 +0200 Subject: [PATCH 12/15] Optimize argument handling --- lib/net.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/net.js b/lib/net.js index ca6b86789f8236..e1f11eb966e400 100644 --- a/lib/net.js +++ b/lib/net.js @@ -91,7 +91,9 @@ function normalizeArgs(args) { } var cb = args[args.length - 1]; - return typeof cb === 'function' ? [options, cb] : [options]; + if (typeof cb !== 'function') + cb = null; + return [options, cb]; } exports._normalizeArgs = normalizeArgs; @@ -1330,21 +1332,20 @@ Server.prototype.listen = function() { var args = new Array(arguments.length); for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]; - args = normalizeArgs(args); + var [options, cb] = normalizeArgs(args); - if (args[1]) { - this.once('listening', args[1]); + if (typeof cb === 'function') { + this.once('listening', cb); } - var options = args[0]; - if (arguments.length === 0 || typeof arguments[0] === 'function') { + if (args.length === 0 || typeof args[0] === 'function') { // Bind to a random port. options.port = 0; } // The third optional argument is the backlog size. // When the ip is omitted it can be the second argument. - var backlog = toNumber(arguments[1]) || toNumber(arguments[2]); + var backlog = toNumber(args[1]) || toNumber(args[2]); options = options._handle || options.handle || options; From 4a654495198b374b20e48edc67fe603c277c13b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Tue, 16 Aug 2016 19:45:29 +0200 Subject: [PATCH 13/15] Update normalizeArgs comment --- lib/net.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index e1f11eb966e400..25646d2314f00c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -70,7 +70,7 @@ exports.connect = exports.createConnection = function() { return Socket.prototype.connect.apply(s, args); }; -// Returns an array [options] or [options, cb] +// Returns an array [options, cb], where cb can be null. // It is the same as the argument of Socket.prototype.connect(). // This is also used by Server.prototype.listen(). function normalizeArgs(args) { From 3848eaee4e40547a25e587b6c6bedcd2e888cd10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Thu, 18 Aug 2016 17:58:17 +0200 Subject: [PATCH 14/15] Or expression instead of `if` --- lib/net.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/net.js b/lib/net.js index 25646d2314f00c..d08a92e9329e5d 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1355,8 +1355,7 @@ Server.prototype.listen = function() { } else if (typeof options.fd === 'number' && options.fd >= 0) { listen(this, null, null, null, backlog, options.fd); } else { - if (options.backlog) - backlog = options.backlog; + backlog = options.backlog || backlog; if (typeof options.port === 'number' || typeof options.port === 'string' || (typeof options.port === 'undefined' && 'port' in options)) { From 4bb5770ddd73ac9c06aa90a452b3acf74f187c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4r?= Date: Fri, 9 Sep 2016 17:01:24 +0200 Subject: [PATCH 15/15] length checking before array access Requested by @mscdex --- lib/net.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net.js b/lib/net.js index d08a92e9329e5d..35fde4f57601a1 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1345,7 +1345,7 @@ Server.prototype.listen = function() { // The third optional argument is the backlog size. // When the ip is omitted it can be the second argument. - var backlog = toNumber(args[1]) || toNumber(args[2]); + var backlog = toNumber(args.length > 1 && args[1]) || toNumber(args.length > 2 && args[2]); options = options._handle || options.handle || options;