@@ -785,34 +785,18 @@ function connect(self, address, port, addressType, localAddress, localPort) {
785
785
assert . ok ( self . _connecting ) ;
786
786
787
787
var err ;
788
- if ( localAddress || localPort ) {
789
- if ( localAddress && ! exports . isIP ( localAddress ) )
790
- err = new TypeError (
791
- 'localAddress should be a valid IP: ' + localAddress ) ;
792
-
793
- if ( localPort && ! util . isNumber ( localPort ) )
794
- err = new TypeError ( 'localPort should be a number: ' + localPort ) ;
795
788
789
+ if ( localAddress || localPort ) {
796
790
var bind ;
797
791
798
- switch ( addressType ) {
799
- case 4 :
800
- if ( ! localAddress )
801
- localAddress = '0.0.0.0' ;
802
- bind = self . _handle . bind ;
803
- break ;
804
- case 6 :
805
- if ( ! localAddress )
806
- localAddress = '::' ;
807
- bind = self . _handle . bind6 ;
808
- break ;
809
- default :
810
- err = new TypeError ( 'Invalid addressType: ' + addressType ) ;
811
- break ;
812
- }
813
-
814
- if ( err ) {
815
- self . _destroy ( err ) ;
792
+ if ( addressType === 4 ) {
793
+ localAddress = localAddress || '0.0.0.0' ;
794
+ bind = self . _handle . bind ;
795
+ } else if ( addressType === 6 ) {
796
+ localAddress = localAddress || '::' ;
797
+ bind = self . _handle . bind6 ;
798
+ } else {
799
+ self . _destroy ( new TypeError ( 'Invalid addressType: ' + addressType ) ) ;
816
800
return ;
817
801
}
818
802
@@ -832,15 +816,12 @@ function connect(self, address, port, addressType, localAddress, localPort) {
832
816
if ( addressType === 6 || addressType === 4 ) {
833
817
var req = new TCPConnectWrap ( ) ;
834
818
req . oncomplete = afterConnect ;
835
- port = port | 0 ;
836
- if ( port <= 0 || port > 65535 )
837
- throw new RangeError ( 'Port should be > 0 and < 65536' ) ;
838
819
839
- if ( addressType === 6 ) {
840
- err = self . _handle . connect6 ( req , address , port ) ;
841
- } else if ( addressType === 4 ) {
820
+ if ( addressType === 4 )
842
821
err = self . _handle . connect ( req , address , port ) ;
843
- }
822
+ else
823
+ err = self . _handle . connect6 ( req , address , port ) ;
824
+
844
825
} else {
845
826
var req = new PipeConnectWrap ( ) ;
846
827
req . oncomplete = afterConnect ;
@@ -898,19 +879,26 @@ Socket.prototype.connect = function(options, cb) {
898
879
if ( pipe ) {
899
880
connect ( self , options . path ) ;
900
881
901
- } else if ( ! options . host ) {
902
- debug ( 'connect: missing host' ) ;
903
- self . _host = '127.0.0.1' ;
904
- connect ( self , self . _host , options . port , 4 ) ;
905
-
906
882
} else {
907
883
var dns = require ( 'dns' ) ;
908
- var host = options . host ;
884
+ var host = options . host || 'localhost' ;
885
+ var port = options . port | 0 ;
886
+ var localAddress = options . localAddress ;
887
+ var localPort = options . localPort ;
909
888
var dnsopts = {
910
889
family : options . family ,
911
890
hints : 0
912
891
} ;
913
892
893
+ if ( localAddress && ! exports . isIP ( localAddress ) )
894
+ throw new TypeError ( 'localAddress must be a valid IP: ' + localAddress ) ;
895
+
896
+ if ( localPort && ! util . isNumber ( localPort ) )
897
+ throw new TypeError ( 'localPort should be a number: ' + localPort ) ;
898
+
899
+ if ( port <= 0 || port > 65535 )
900
+ throw new RangeError ( 'port should be > 0 and < 65536: ' + port ) ;
901
+
914
902
if ( dnsopts . family !== 4 && dnsopts . family !== 6 )
915
903
dnsopts . hints = dns . ADDRCONFIG | dns . V4MAPPED ;
916
904
@@ -936,19 +924,12 @@ Socket.prototype.connect = function(options, cb) {
936
924
} ) ;
937
925
} else {
938
926
timers . _unrefActive ( self ) ;
939
-
940
- addressType = addressType || 4 ;
941
-
942
- // node_net.cc handles null host names graciously but user land
943
- // expects remoteAddress to have a meaningful value
944
- ip = ip || ( addressType === 4 ? '127.0.0.1' : '0:0:0:0:0:0:0:1' ) ;
945
-
946
927
connect ( self ,
947
928
ip ,
948
- options . port ,
929
+ port ,
949
930
addressType ,
950
- options . localAddress ,
951
- options . localPort ) ;
931
+ localAddress ,
932
+ localPort ) ;
952
933
}
953
934
} ) ;
954
935
}
0 commit comments