@@ -881,64 +881,77 @@ Socket.prototype.connect = function(options, cb) {
881881 connect ( self , options . path ) ;
882882
883883 } else {
884- const dns = require ( 'dns' ) ;
885- var host = options . host || 'localhost' ;
886- var port = 0 ;
887- var localAddress = options . localAddress ;
888- var localPort = options . localPort ;
889- var dnsopts = {
890- family : options . family ,
891- hints : 0
892- } ;
893-
894- if ( localAddress && ! exports . isIP ( localAddress ) )
895- throw new TypeError ( 'localAddress must be a valid IP: ' + localAddress ) ;
896-
897- if ( localPort && typeof localPort !== 'number' )
898- throw new TypeError ( 'localPort should be a number: ' + localPort ) ;
899-
900- port = options . port ;
901- if ( typeof port !== 'undefined' ) {
902- if ( typeof port !== 'number' && typeof port !== 'string' )
903- throw new TypeError ( 'port should be a number or string: ' + port ) ;
904- if ( ! isLegalPort ( port ) )
905- throw new RangeError ( 'port should be >= 0 and < 65536: ' + port ) ;
906- }
907- port |= 0 ;
884+ lookupAndConnect ( self , options ) ;
885+ }
886+ return self ;
887+ } ;
908888
909- if ( dnsopts . family !== 4 && dnsopts . family !== 6 )
910- dnsopts . hints = dns . ADDRCONFIG | dns . V4MAPPED ;
911889
912- debug ( 'connect: find host ' + host ) ;
913- debug ( 'connect: dns options ' + dnsopts ) ;
914- self . _host = host ;
915- dns . lookup ( host , dnsopts , function ( err , ip , addressType ) {
916- self . emit ( 'lookup' , err , ip , addressType ) ;
890+ function lookupAndConnect ( self , options ) {
891+ const dns = require ( 'dns' ) ;
892+ var host = options . host || 'localhost' ;
893+ var port = options . port ;
894+ var localAddress = options . localAddress ;
895+ var localPort = options . localPort ;
917896
918- // It's possible we were destroyed while looking this up.
919- // XXX it would be great if we could cancel the promise returned by
920- // the look up.
921- if ( ! self . _connecting ) return ;
897+ if ( localAddress && ! exports . isIP ( localAddress ) )
898+ throw new TypeError ( 'localAddress must be a valid IP: ' + localAddress ) ;
922899
923- if ( err ) {
924- // net.createConnection() creates a net.Socket object and
925- // immediately calls net.Socket.connect() on it (that's us).
926- // There are no event listeners registered yet so defer the
927- // error event to the next tick.
928- process . nextTick ( connectErrorNT , self , err , options ) ;
929- } else {
930- self . _unrefTimer ( ) ;
931- connect ( self ,
932- ip ,
933- port ,
934- addressType ,
935- localAddress ,
936- localPort ) ;
937- }
938- } ) ;
900+ if ( localPort && typeof localPort !== 'number' )
901+ throw new TypeError ( 'localPort should be a number: ' + localPort ) ;
902+
903+ if ( typeof port !== 'undefined' ) {
904+ if ( typeof port !== 'number' && typeof port !== 'string' )
905+ throw new TypeError ( 'port should be a number or string: ' + port ) ;
906+ if ( ! isLegalPort ( port ) )
907+ throw new RangeError ( 'port should be >= 0 and < 65536: ' + port ) ;
939908 }
940- return self ;
941- } ;
909+ port |= 0 ;
910+
911+ // If host is an IP, skip performing a lookup
912+ // TODO(evanlucas) should we hot path this for localhost?
913+ var addressType = exports . isIP ( host ) ;
914+ if ( addressType ) {
915+ connect ( self , host , port , addressType , localAddress , localPort ) ;
916+ return ;
917+ }
918+
919+ var dnsopts = {
920+ family : options . family ,
921+ hints : 0
922+ } ;
923+
924+ if ( dnsopts . family !== 4 && dnsopts . family !== 6 )
925+ dnsopts . hints = dns . ADDRCONFIG | dns . V4MAPPED ;
926+
927+ debug ( 'connect: find host ' + host ) ;
928+ debug ( 'connect: dns options ' + dnsopts ) ;
929+ self . _host = host ;
930+ dns . lookup ( host , dnsopts , function ( err , ip , addressType ) {
931+ self . emit ( 'lookup' , err , ip , addressType ) ;
932+
933+ // It's possible we were destroyed while looking this up.
934+ // XXX it would be great if we could cancel the promise returned by
935+ // the look up.
936+ if ( ! self . _connecting ) return ;
937+
938+ if ( err ) {
939+ // net.createConnection() creates a net.Socket object and
940+ // immediately calls net.Socket.connect() on it (that's us).
941+ // There are no event listeners registered yet so defer the
942+ // error event to the next tick.
943+ process . nextTick ( connectErrorNT , self , err , options ) ;
944+ } else {
945+ self . _unrefTimer ( ) ;
946+ connect ( self ,
947+ ip ,
948+ port ,
949+ addressType ,
950+ localAddress ,
951+ localPort ) ;
952+ }
953+ } ) ;
954+ }
942955
943956
944957function connectErrorNT ( self , err , options ) {
0 commit comments