@@ -60,7 +60,8 @@ const {
60
60
kClose,
61
61
kDestroy,
62
62
kDispatch,
63
- kInterceptors
63
+ kInterceptors,
64
+ kLocalAddress
64
65
} = require ( './core/symbols' )
65
66
66
67
const kClosedResolve = Symbol ( 'kClosedResolve' )
@@ -102,7 +103,8 @@ class Client extends DispatcherBase {
102
103
maxCachedSessions,
103
104
maxRedirections,
104
105
connect,
105
- maxRequestsPerClient
106
+ maxRequestsPerClient,
107
+ localAddress
106
108
} = { } ) {
107
109
super ( )
108
110
@@ -170,6 +172,10 @@ class Client extends DispatcherBase {
170
172
throw new InvalidArgumentError ( 'maxRequestsPerClient must be a positive number' )
171
173
}
172
174
175
+ if ( localAddress != null && ( typeof localAddress !== 'string' || net . isIP ( localAddress ) === 0 ) ) {
176
+ throw new InvalidArgumentError ( 'localAddress must be valid string IP address' )
177
+ }
178
+
173
179
if ( typeof connect !== 'function' ) {
174
180
connect = buildConnector ( {
175
181
...tls ,
@@ -193,6 +199,7 @@ class Client extends DispatcherBase {
193
199
this [ kKeepAliveTimeoutThreshold ] = keepAliveTimeoutThreshold == null ? 1e3 : keepAliveTimeoutThreshold
194
200
this [ kKeepAliveTimeoutValue ] = this [ kKeepAliveDefaultTimeout ]
195
201
this [ kServerName ] = null
202
+ this [ kLocalAddress ] = localAddress != null ? localAddress : null
196
203
this [ kResuming ] = 0 // 0, idle, 1, scheduled, 2 resuming
197
204
this [ kNeedDrain ] = 0 // 0, idle, 1, scheduled, 2 resuming
198
205
this [ kHostHeader ] = `host: ${ this [ kUrl ] . hostname } ${ this [ kUrl ] . port ? `:${ this [ kUrl ] . port } ` : '' } \r\n`
@@ -1020,7 +1027,8 @@ async function connect (client) {
1020
1027
hostname,
1021
1028
protocol,
1022
1029
port,
1023
- servername : client [ kServerName ]
1030
+ servername : client [ kServerName ] ,
1031
+ localAddress : client [ kLocalAddress ]
1024
1032
} ,
1025
1033
connector : client [ kConnector ]
1026
1034
} )
@@ -1033,7 +1041,8 @@ async function connect (client) {
1033
1041
hostname,
1034
1042
protocol,
1035
1043
port,
1036
- servername : client [ kServerName ]
1044
+ servername : client [ kServerName ] ,
1045
+ localAddress : client [ kLocalAddress ]
1037
1046
} , ( err , socket ) => {
1038
1047
if ( err ) {
1039
1048
reject ( err )
@@ -1076,7 +1085,8 @@ async function connect (client) {
1076
1085
hostname,
1077
1086
protocol,
1078
1087
port,
1079
- servername : client [ kServerName ]
1088
+ servername : client [ kServerName ] ,
1089
+ localAddress : client [ kLocalAddress ]
1080
1090
} ,
1081
1091
connector : client [ kConnector ] ,
1082
1092
socket
@@ -1093,7 +1103,8 @@ async function connect (client) {
1093
1103
hostname,
1094
1104
protocol,
1095
1105
port,
1096
- servername : client [ kServerName ]
1106
+ servername : client [ kServerName ] ,
1107
+ localAddress : client [ kLocalAddress ]
1097
1108
} ,
1098
1109
connector : client [ kConnector ] ,
1099
1110
error : err
0 commit comments