1- import net from 'net' ;
2- import tls from 'tls' ;
1+ import * as net from 'net' ;
2+ import * as tls from 'tls' ;
33import url from 'url' ;
44import assert from 'assert' ;
55import createDebug from 'debug' ;
@@ -28,6 +28,8 @@ export default class HttpsProxyAgent extends Agent {
2828 private secureProxy : boolean ;
2929 private secureContext : tls . SecureContext | undefined ;
3030 private proxy : HttpsProxyAgentOptions ;
31+ public timeout : number | null ;
32+
3133
3234 constructor ( _opts : string | HttpsProxyAgentOptions ) {
3335 let opts : HttpsProxyAgentOptions ;
@@ -44,6 +46,8 @@ export default class HttpsProxyAgent extends Agent {
4446 debug ( 'creating new HttpsProxyAgent instance: %o' , opts ) ;
4547 super ( opts ) ;
4648
49+ this . timeout = opts . timeout || null ;
50+
4751 const proxy : HttpsProxyAgentOptions = { ...opts } ;
4852
4953 // If `true`, then connect to the proxy server over TLS.
@@ -99,6 +103,14 @@ export default class HttpsProxyAgent extends Agent {
99103 socket = net . connect ( proxy as net . NetConnectOpts ) ;
100104 }
101105
106+ if ( this . timeout ) {
107+ socket . setTimeout ( this . timeout ) ;
108+
109+ socket . on ( 'timeout' , ( ) => {
110+ socket . end ( ) ;
111+ } ) ;
112+ }
113+
102114 const headers : OutgoingHttpHeaders = { ...proxy . headers } ;
103115 const hostname = `${ opts . host } :${ opts . port } ` ;
104116 let payload = `CONNECT ${ hostname } HTTP/1.1\r\n` ;
@@ -112,13 +124,15 @@ export default class HttpsProxyAgent extends Agent {
112124
113125 // The `Host` header should only include the port
114126 // number when it is not the default port.
115- let { host, port, secureEndpoint } = opts ;
116- if ( ! isDefaultPort ( port , secureEndpoint ) ) {
117- host += `:${ port } ` ;
118- }
119- headers . Host = host ;
127+ // let { host, port, secureEndpoint } = opts;
128+ // if (!isDefaultPort(port, secureEndpoint)) {
129+ // host += `:${port}`;
130+ // }
131+ // headers.Host = host;
120132
133+ headers . Host = hostname ;
121134 headers . Connection = 'close' ;
135+
122136 for ( const name of Object . keys ( headers ) ) {
123137 payload += `${ name } : ${ headers [ name ] } \r\n` ;
124138 }
@@ -140,12 +154,23 @@ export default class HttpsProxyAgent extends Agent {
140154 // this socket connection to a TLS connection.
141155 debug ( 'Upgrading socket connection to TLS' ) ;
142156 const servername = opts . servername || opts . host ;
143- return tls . connect ( {
157+ const tlsSocket = tls . connect ( {
144158 ...omit ( opts , 'host' , 'hostname' , 'path' , 'port' ) ,
145159 socket,
146160 servername,
147161 secureContext : this . secureContext
148162 } ) ;
163+ return new Promise ( ( resolve , reject ) => {
164+ const errCb = ( err : Error ) => {
165+ reject ( err ) ;
166+ } ;
167+ tlsSocket . once ( 'error' , errCb ) ;
168+
169+ tlsSocket . once ( 'secureConnect' , ( ) => {
170+ resolve ( tlsSocket ) ;
171+ tlsSocket . off ( 'error' , errCb ) ;
172+ } ) ;
173+ } ) ;
149174 }
150175
151176 return socket ;
@@ -199,10 +224,11 @@ function omit<T extends object, K extends [...(keyof T)[]]>(
199224 obj : T ,
200225 ...keys : K
201226) : {
202- [ K2 in Exclude < keyof T , K [ number ] > ] : T [ K2 ] ;
203- } {
204- const ret = { } as {
205- [ K in keyof typeof obj ] : ( typeof obj ) [ K ] ;
227+ [ K2 in Exclude < keyof T , K [ number ] > ] : T [ K2 ] ;
228+ } {
229+ const ret = { } as {
230+ [ K in keyof typeof obj ] : typeof obj [ K ] ;
231+
206232 } ;
207233 let key : keyof typeof obj ;
208234 for ( key in obj ) {
0 commit comments