@@ -1351,49 +1351,60 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
13511351
13521352
13531353Server . prototype . listen = function ( ) {
1354- var args = new Array ( arguments . length ) ;
1354+ const args = new Array ( arguments . length ) ;
13551355 for ( var i = 0 ; i < arguments . length ; i ++ )
13561356 args [ i ] = arguments [ i ] ;
1357- var [ options , cb ] = normalizeArgs ( args ) ;
1357+ // TODO(joyeecheung): use destructuring when V8 is fast enough
1358+ const normalized = normalizeArgs ( args ) ;
1359+ var options = normalized [ 0 ] ;
1360+ const cb = normalized [ 1 ] ;
13581361
1359- if ( typeof cb === 'function' ) {
1362+ var hasCallback = ( cb !== null ) ;
1363+ if ( hasCallback ) {
13601364 this . once ( 'listening' , cb ) ;
13611365 }
13621366
1367+ // ([port][, host][, backlog][, cb]) where port is omitted,
1368+ // that is, listen() or listen(cb),
13631369 if ( args . length === 0 || typeof args [ 0 ] === 'function' ) {
13641370 // Bind to a random port.
13651371 options . port = 0 ;
13661372 }
13671373
1368- // The third optional argument is the backlog size.
1369- // When the ip is omitted it can be the second argument.
1370- var backlog = toNumber ( args . length > 1 && args [ 1 ] ) ||
1371- toNumber ( args . length > 2 && args [ 2 ] ) ;
1374+ const backlogFromArgs =
1375+ // (handle, backlog) or (path, backlog) or (port, backlog)
1376+ toNumber ( args . length > 1 && args [ 1 ] ) ||
1377+ toNumber ( args . length > 2 && args [ 2 ] ) ; // (port, host, backlog)
13721378
13731379 options = options . _handle || options . handle || options ;
1374-
1380+ // (handle[, backlog][, cb]) where handle is an object with a handle
13751381 if ( options instanceof TCP ) {
13761382 this . _handle = options ;
1377- listen ( this , null , - 1 , - 1 , backlog ) ;
1383+ listen ( this , null , - 1 , - 1 , backlogFromArgs ) ;
13781384 } else if ( typeof options . fd === 'number' && options . fd >= 0 ) {
1379- listen ( this , null , null , null , backlog , options . fd ) ;
1385+ // (handle[, backlog][, cb]) where handle is an object with a fd
1386+ listen ( this , null , null , null , backlogFromArgs , options . fd ) ;
13801387 } else {
1381- backlog = options . backlog || backlog ;
1388+ const backlog = options . backlog || backlogFromArgs ;
13821389
1390+ // ([port][, host][, backlog][, cb]) where port is specified
1391+ // or (options[, cb]) where options.port is specified
13831392 if ( typeof options . port === 'number' || typeof options . port === 'string' ||
13841393 ( typeof options . port === 'undefined' && 'port' in options ) ) {
1385- // Undefined is interpreted as zero (random port) for consistency
1386- // with net.connect().
1394+ // if (options[, cb]) where options.port is explicitly set as undefined,
1395+ // bind to an arbitrary unused port
13871396 assertPort ( options . port ) ;
1397+ // start TCP server listening on host:port
13881398 if ( options . host ) {
13891399 lookupAndListen ( this , options . port | 0 , options . host , backlog ,
13901400 options . exclusive ) ;
1391- } else {
1401+ } else { // Undefined host, listens on unspecified IPv4 address
13921402 listen ( this , null , options . port | 0 , 4 , backlog , undefined ,
13931403 options . exclusive ) ;
13941404 }
13951405 } else if ( options . path && isPipeName ( options . path ) ) {
1396- // UNIX socket or Windows pipe.
1406+ // (path[, backlog][, cb]) or (options[, cb])
1407+ // where path or options.path is a UNIX domain socket or Windows pipe
13971408 const pipeName = this . _pipeName = options . path ;
13981409 listen ( this , pipeName , - 1 , - 1 , backlog , undefined , options . exclusive ) ;
13991410 } else {
0 commit comments