@@ -60,7 +60,8 @@ const {
6060 kClose,
6161 kDestroy,
6262 kDispatch,
63- kInterceptors
63+ kInterceptors,
64+ kLocalAddress
6465} = require ( './core/symbols' )
6566
6667const kClosedResolve = Symbol ( 'kClosedResolve' )
@@ -102,7 +103,8 @@ class Client extends DispatcherBase {
102103 maxCachedSessions,
103104 maxRedirections,
104105 connect,
105- maxRequestsPerClient
106+ maxRequestsPerClient,
107+ localAddress
106108 } = { } ) {
107109 super ( )
108110
@@ -170,6 +172,10 @@ class Client extends DispatcherBase {
170172 throw new InvalidArgumentError ( 'maxRequestsPerClient must be a positive number' )
171173 }
172174
175+ if ( localAddress != null && ( typeof localAddress !== 'string' || net . isIP ( localAddress ) === 0 ) ) {
176+ throw new InvalidArgumentError ( 'localAddress must be valid string IP address' )
177+ }
178+
173179 if ( typeof connect !== 'function' ) {
174180 connect = buildConnector ( {
175181 ...tls ,
@@ -193,6 +199,7 @@ class Client extends DispatcherBase {
193199 this [ kKeepAliveTimeoutThreshold ] = keepAliveTimeoutThreshold == null ? 1e3 : keepAliveTimeoutThreshold
194200 this [ kKeepAliveTimeoutValue ] = this [ kKeepAliveDefaultTimeout ]
195201 this [ kServerName ] = null
202+ this [ kLocalAddress ] = localAddress != null ? localAddress : null
196203 this [ kResuming ] = 0 // 0, idle, 1, scheduled, 2 resuming
197204 this [ kNeedDrain ] = 0 // 0, idle, 1, scheduled, 2 resuming
198205 this [ kHostHeader ] = `host: ${ this [ kUrl ] . hostname } ${ this [ kUrl ] . port ? `:${ this [ kUrl ] . port } ` : '' } \r\n`
@@ -1020,7 +1027,8 @@ async function connect (client) {
10201027 hostname,
10211028 protocol,
10221029 port,
1023- servername : client [ kServerName ]
1030+ servername : client [ kServerName ] ,
1031+ localAddress : client [ kLocalAddress ]
10241032 } ,
10251033 connector : client [ kConnector ]
10261034 } )
@@ -1033,7 +1041,8 @@ async function connect (client) {
10331041 hostname,
10341042 protocol,
10351043 port,
1036- servername : client [ kServerName ]
1044+ servername : client [ kServerName ] ,
1045+ localAddress : client [ kLocalAddress ]
10371046 } , ( err , socket ) => {
10381047 if ( err ) {
10391048 reject ( err )
@@ -1076,7 +1085,8 @@ async function connect (client) {
10761085 hostname,
10771086 protocol,
10781087 port,
1079- servername : client [ kServerName ]
1088+ servername : client [ kServerName ] ,
1089+ localAddress : client [ kLocalAddress ]
10801090 } ,
10811091 connector : client [ kConnector ] ,
10821092 socket
@@ -1093,7 +1103,8 @@ async function connect (client) {
10931103 hostname,
10941104 protocol,
10951105 port,
1096- servername : client [ kServerName ]
1106+ servername : client [ kServerName ] ,
1107+ localAddress : client [ kLocalAddress ]
10971108 } ,
10981109 connector : client [ kConnector ] ,
10991110 error : err
0 commit comments