Skip to content

Commit bcd4ff7

Browse files
committed
net: refactor server.listen flow control
1 parent ce3fc05 commit bcd4ff7

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

lib/net.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var cluster;
2424
const errnoException = util._errnoException;
2525
const exceptionWithHostPort = util._exceptionWithHostPort;
2626
const isLegalPort = internalNet.isLegalPort;
27-
const assertPort = internalNet.assertPort;
2827

2928
function noop() {}
3029

@@ -1363,14 +1362,6 @@ Server.prototype.listen = function() {
13631362
if (hasCallback) {
13641363
this.once('listening', cb);
13651364
}
1366-
1367-
// ([port][, host][, backlog][, cb]) where port is omitted,
1368-
// that is, listen() or listen(cb),
1369-
if (args.length === 0 || typeof args[0] === 'function') {
1370-
// Bind to a random port.
1371-
options.port = 0;
1372-
}
1373-
13741365
const backlogFromArgs =
13751366
// (handle, backlog) or (path, backlog) or (port, backlog)
13761367
toNumber(args.length > 1 && args[1]) ||
@@ -1381,38 +1372,51 @@ Server.prototype.listen = function() {
13811372
if (options instanceof TCP) {
13821373
this._handle = options;
13831374
listen(this, null, -1, -1, backlogFromArgs);
1384-
} else if (typeof options.fd === 'number' && options.fd >= 0) {
1385-
// (handle[, backlog][, cb]) where handle is an object with a fd
1375+
return this;
1376+
}
1377+
// (handle[, backlog][, cb]) where handle is an object with a fd
1378+
if (typeof options.fd === 'number' && options.fd >= 0) {
13861379
listen(this, null, null, null, backlogFromArgs, options.fd);
1387-
} else {
1388-
const backlog = options.backlog || backlogFromArgs;
1380+
return this;
1381+
}
13891382

1390-
// ([port][, host][, backlog][, cb]) where port is specified
1391-
// or (options[, cb]) where options.port is specified
1392-
if (typeof options.port === 'number' || typeof options.port === 'string' ||
1393-
(typeof options.port === 'undefined' && 'port' in options)) {
1394-
// if (options[, cb]) where options.port is explicitly set as undefined,
1395-
// bind to an arbitrary unused port
1396-
assertPort(options.port);
1397-
// start TCP server listening on host:port
1398-
if (options.host) {
1399-
lookupAndListen(this, options.port | 0, options.host, backlog,
1400-
options.exclusive);
1401-
} else { // Undefined host, listens on unspecified IPv4 address
1402-
listen(this, null, options.port | 0, 4, backlog, undefined,
1403-
options.exclusive);
1404-
}
1405-
} else if (options.path && isPipeName(options.path)) {
1406-
// (path[, backlog][, cb]) or (options[, cb])
1407-
// where path or options.path is a UNIX domain socket or Windows pipe
1408-
const pipeName = this._pipeName = options.path;
1409-
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive);
1410-
} else {
1411-
throw new Error('Invalid listen argument: ' + options);
1383+
// ([port][, host][, backlog][, cb]) where port is omitted,
1384+
// that is, listen() or listen(cb),
1385+
// or (options[, cb]) where options.port is explicitly set as undefined,
1386+
// bind to an arbitrary unused port
1387+
if (args.length === 0 || typeof args[0] === 'function' ||
1388+
(typeof options.port === 'undefined' && 'port' in options)) {
1389+
options.port = 0;
1390+
}
1391+
// ([port][, host][, backlog][, cb]) where port is specified
1392+
// or (options[, cb]) where options.port is specified
1393+
// or if options.port is normalized as 0 before
1394+
if (typeof options.port === 'number' || typeof options.port === 'string') {
1395+
if (!isLegalPort(options.port)) {
1396+
throw new RangeError('"port" argument must be >= 0 and < 65536');
1397+
}
1398+
const backlog = options.backlog || backlogFromArgs;
1399+
// start TCP server listening on host:port
1400+
if (options.host) {
1401+
lookupAndListen(this, options.port | 0, options.host, backlog,
1402+
options.exclusive);
1403+
} else { // Undefined host, listens on unspecified address
1404+
listen(this, null, options.port | 0, 4, // addressType will be ignored
1405+
backlog, undefined, options.exclusive);
14121406
}
1407+
return this;
14131408
}
14141409

1415-
return this;
1410+
// (path[, backlog][, cb]) or (options[, cb])
1411+
// where path or options.path is a UNIX domain socket or Windows pipe
1412+
if (options.path && isPipeName(options.path)) {
1413+
const pipeName = this._pipeName = options.path;
1414+
const backlog = options.backlog || backlogFromArgs;
1415+
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive);
1416+
return this;
1417+
}
1418+
1419+
throw new Error('Invalid listen argument: ' + options);
14161420
};
14171421

14181422
function lookupAndListen(self, port, address, backlog, exclusive) {

0 commit comments

Comments
 (0)