@@ -77,12 +77,18 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
7777 } ;
7878 }
7979
80- async connect (
80+ addRequest ( req : HttpProxyAgentClientRequest , opts : AgentConnectOpts ) : void {
81+ req . _header = null ;
82+ this . setRequestProps ( req , opts ) ;
83+ // @ts -expect-error `addRequest()` isn't defined in `@types/node`
84+ super . addRequest ( req , opts ) ;
85+ }
86+
87+ setRequestProps (
8188 req : HttpProxyAgentClientRequest ,
8289 opts : AgentConnectOpts
83- ) : Promise < net . Socket > {
90+ ) : void {
8491 const { proxy } = this ;
85-
8692 const protocol = opts . secureEndpoint ? 'https:' : 'http:' ;
8793 const hostname = req . getHeader ( 'host' ) || 'localhost' ;
8894 const base = `${ protocol } //${ hostname } ` ;
@@ -96,7 +102,7 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
96102 req . path = String ( url ) ;
97103
98104 // Inject the `Proxy-Authorization` header if necessary.
99- req . _header = null ;
105+
100106 const headers : OutgoingHttpHeaders =
101107 typeof this . proxyHeaders === 'function'
102108 ? this . proxyHeaders ( )
@@ -121,15 +127,16 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
121127 req . setHeader ( name , value ) ;
122128 }
123129 }
130+ }
124131
125- // Create a socket connection to the proxy server.
126- let socket : net . Socket ;
127- if ( this . secureProxy ) {
128- debug ( 'Creating `tls .Socket`: %o' , this . connectOpts ) ;
129- socket = tls . connect ( this . connectOpts ) ;
130- } else {
131- debug ( 'Creating `net.Socket`: %o' , this . connectOpts ) ;
132- socket = net . connect ( this . connectOpts ) ;
132+ async connect (
133+ req : HttpProxyAgentClientRequest ,
134+ opts : AgentConnectOpts
135+ ) : Promise < net . Socket > {
136+ req . _header = null ;
137+
138+ if ( ! req . path . includes ( '://' ) ) {
139+ this . setRequestProps ( req , opts ) ;
133140 }
134141
135142 // At this point, the http ClientRequest's internal `_header` field
@@ -140,7 +147,6 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
140147 debug ( 'Regenerating stored HTTP header string for request' ) ;
141148 req . _implicitHeader ( ) ;
142149 if ( req . outputData && req . outputData . length > 0 ) {
143- // Node >= 12
144150 debug (
145151 'Patching connection write() output buffer with updated header'
146152 ) ;
@@ -151,6 +157,16 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
151157 debug ( 'Output buffer: %o' , req . outputData [ 0 ] . data ) ;
152158 }
153159
160+ // Create a socket connection to the proxy server.
161+ let socket : net . Socket ;
162+ if ( this . secureProxy ) {
163+ debug ( 'Creating `tls.Socket`: %o' , this . connectOpts ) ;
164+ socket = tls . connect ( this . connectOpts ) ;
165+ } else {
166+ debug ( 'Creating `net.Socket`: %o' , this . connectOpts ) ;
167+ socket = net . connect ( this . connectOpts ) ;
168+ }
169+
154170 // Wait for the socket's `connect` event, so that this `callback()`
155171 // function throws instead of the `http` request machinery. This is
156172 // important for i.e. `PacProxyAgent` which determines a failed proxy
0 commit comments