@@ -39,8 +39,9 @@ export type HTTPRequestParams = {
3939export const NET_DEFAULT_TIMEOUT = 30_000 ;
4040
4141export function httpRequest ( params : HTTPRequestParams , onResponse : ( r : http . IncomingMessage ) => void , onError : ( error : Error ) => void ) : { cancel ( error : Error | undefined ) : void } {
42- const parsedUrl = new URL ( params . url ) ;
43- const options : https . RequestOptions = {
42+ const parsedUrl = url . parse ( params . url ) ;
43+ let options : https . RequestOptions = {
44+ ...parsedUrl ,
4445 agent : parsedUrl . protocol === 'https:' ? httpsHappyEyeballsAgent : httpHappyEyeballsAgent ,
4546 method : params . method || 'GET' ,
4647 headers : params . headers ,
@@ -50,15 +51,19 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
5051
5152 const proxyURL = getProxyForUrl ( params . url ) ;
5253 if ( proxyURL ) {
53- const parsedProxyURL = new URL ( proxyURL ) ;
54+ const parsedProxyURL = url . parse ( proxyURL ) ;
5455 if ( params . url . startsWith ( 'http:' ) ) {
55- parsedUrl . pathname = parsedUrl . href ;
56- parsedUrl . host = parsedProxyURL . host ;
56+ options = {
57+ path : parsedUrl . href ,
58+ host : parsedProxyURL . hostname ,
59+ port : parsedProxyURL . port ,
60+ headers : options . headers ,
61+ method : options . method
62+ } ;
5763 } else {
58- options . agent = new HttpsProxyAgent ( {
59- ...convertURLtoLegacyUrl ( parsedProxyURL ) ,
60- secureProxy : parsedProxyURL . protocol === 'https:' ,
61- } ) ;
64+ ( parsedProxyURL as any ) . secureProxy = parsedProxyURL . protocol === 'https:' ;
65+
66+ options . agent = new HttpsProxyAgent ( parsedProxyURL ) ;
6267 options . rejectUnauthorized = false ;
6368 }
6469 }
@@ -76,8 +81,8 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
7681 }
7782 } ;
7883 const request = options . protocol === 'https:' ?
79- https . request ( parsedUrl , options , requestCallback ) :
80- http . request ( parsedUrl , options , requestCallback ) ;
84+ https . request ( options , requestCallback ) :
85+ http . request ( options , requestCallback ) ;
8186 request . on ( 'error' , onError ) ;
8287 if ( params . socketTimeout !== undefined ) {
8388 request . setTimeout ( params . socketTimeout , ( ) => {
@@ -132,27 +137,23 @@ export function createProxyAgent(proxy?: ProxySettings, forUrl?: URL) {
132137 if ( ! / ^ \w + : \/ \/ / . test ( proxyServer ) )
133138 proxyServer = 'http://' + proxyServer ;
134139
135- const proxyOpts = new URL ( proxyServer ) ;
140+ const proxyOpts = url . parse ( proxyServer ) ;
136141 if ( proxyOpts . protocol ?. startsWith ( 'socks' ) ) {
137142 return new SocksProxyAgent ( {
138143 host : proxyOpts . hostname ,
139144 port : proxyOpts . port || undefined ,
140145 } ) ;
141146 }
142- if ( proxy . username ) {
143- proxyOpts . username = proxy . username ;
144- proxyOpts . password = proxy . password || '' ;
145- }
147+ if ( proxy . username )
148+ proxyOpts . auth = `${ proxy . username } :${ proxy . password || '' } ` ;
146149
147150 if ( forUrl && [ 'ws:' , 'wss:' ] . includes ( forUrl . protocol ) ) {
148151 // Force CONNECT method for WebSockets.
149- // TODO: switch to URL instance instead of legacy object once https-proxy-agent supports it.
150- return new HttpsProxyAgent ( convertURLtoLegacyUrl ( proxyOpts ) ) ;
152+ return new HttpsProxyAgent ( proxyOpts ) ;
151153 }
152154
153155 // TODO: We should use HttpProxyAgent conditional on proxyOpts.protocol instead of always using CONNECT method.
154- // TODO: switch to URL instance instead of legacy object once https-proxy-agent supports it.
155- return new HttpsProxyAgent ( convertURLtoLegacyUrl ( proxyOpts ) ) ;
156+ return new HttpsProxyAgent ( proxyOpts ) ;
156157}
157158
158159export function createHttpServer ( requestListener ?: ( req : http . IncomingMessage , res : http . ServerResponse ) => void ) : http . Server ;
@@ -225,20 +226,3 @@ function decorateServer(server: net.Server) {
225226 return close . call ( server , callback ) ;
226227 } ;
227228}
228-
229- function convertURLtoLegacyUrl ( url : URL ) : url . Url {
230- return {
231- auth : url . username ? url . username + ':' + url . password : null ,
232- hash : url . hash || null ,
233- host : url . hostname ? url . hostname + ':' + url . port : null ,
234- hostname : url . hostname || null ,
235- href : url . href ,
236- path : url . pathname + url . search ,
237- pathname : url . pathname ,
238- protocol : url . protocol ,
239- search : url . search || null ,
240- slashes : true ,
241- port : url . port || null ,
242- query : url . search . slice ( 1 ) || null ,
243- } ;
244- }
0 commit comments