@@ -24,7 +24,6 @@ var cluster;
24
24
const errnoException = util . _errnoException ;
25
25
const exceptionWithHostPort = util . _exceptionWithHostPort ;
26
26
const isLegalPort = internalNet . isLegalPort ;
27
- const assertPort = internalNet . assertPort ;
28
27
29
28
function noop ( ) { }
30
29
@@ -1363,14 +1362,6 @@ Server.prototype.listen = function() {
1363
1362
if ( hasCallback ) {
1364
1363
this . once ( 'listening' , cb ) ;
1365
1364
}
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
-
1374
1365
const backlogFromArgs =
1375
1366
// (handle, backlog) or (path, backlog) or (port, backlog)
1376
1367
toNumber ( args . length > 1 && args [ 1 ] ) ||
@@ -1381,38 +1372,51 @@ Server.prototype.listen = function() {
1381
1372
if ( options instanceof TCP ) {
1382
1373
this . _handle = options ;
1383
1374
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 ) {
1386
1379
listen ( this , null , null , null , backlogFromArgs , options . fd ) ;
1387
- } else {
1388
- const backlog = options . backlog || backlogFromArgs ;
1380
+ return this ;
1381
+ }
1389
1382
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 ) ;
1412
1406
}
1407
+ return this ;
1413
1408
}
1414
1409
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 ) ;
1416
1420
} ;
1417
1421
1418
1422
function lookupAndListen ( self , port , address , backlog , exclusive ) {
0 commit comments