55
66"use strict" ;
77
8+ const EventEmitter = require ( "events" ) ;
89const { extname, basename } = require ( "path" ) ;
910const { URL } = require ( "url" ) ;
1011const { createGunzip, createBrotliDecompress, createInflate } = require ( "zlib" ) ;
@@ -19,13 +20,15 @@ const memoize = require("../util/memoize");
1920
2021const getHttp = memoize ( ( ) => require ( "http" ) ) ;
2122const getHttps = memoize ( ( ) => require ( "https" ) ) ;
22- const proxyFetch = request => ( url , options , callback ) =>
23- new Promise ( resolve => {
24- const key = "http_proxy" ;
25- const proxyUrl = process . env [ key ] || process . env [ key . toUpperCase ( ) ] ;
26- if ( ! proxyUrl ) return resolve ( ) ;
23+ const proxyFetch = ( request , proxy ) => ( url , options , callback ) => {
24+ const eventEmitter = new EventEmitter ( ) ;
25+ const doRequest = socket =>
26+ request
27+ . get ( url , { ...options , ...( socket && { socket } ) } , callback )
28+ . on ( "error" , eventEmitter . emit . bind ( eventEmitter , "error" ) ) ;
2729
28- const { hostname : host , port } = new URL ( proxyUrl ) ;
30+ if ( proxy ) {
31+ const { hostname : host , port } = new URL ( proxy ) ;
2932
3033 getHttp ( )
3134 . request ( {
@@ -37,16 +40,24 @@ const proxyFetch = request => (url, options, callback) =>
3740 . on ( "connect" , ( res , socket ) => {
3841 if ( res . statusCode === 200 ) {
3942 // connected to proxy server
40- resolve ( socket ) ;
43+ doRequest ( socket ) ;
4144 }
4245 } )
4346 . on ( "error" , err => {
44- console . error ( "Failed to connect to the proxy:" , err ) ;
47+ eventEmitter . emit (
48+ "error" ,
49+ new Error (
50+ `Failed to connect to proxy server "${ proxy } ": ${ err . message } `
51+ )
52+ ) ;
4553 } )
4654 . end ( ) ;
47- } ) . then ( socket =>
48- request . get ( url , { ...options , ...( socket && { socket } ) } , callback )
49- ) ;
55+ } else {
56+ doRequest ( ) ;
57+ }
58+
59+ return eventEmitter ;
60+ } ;
5061
5162/** @type {(() => void)[] | undefined } */
5263let inProgressWrite = undefined ;
@@ -302,6 +313,7 @@ class HttpUriPlugin {
302313 this . _upgrade = options . upgrade ;
303314 this . _frozen = options . frozen ;
304315 this . _allowedUris = options . allowedUris ;
316+ this . _proxy = options . proxy ;
305317 }
306318
307319 /**
@@ -310,14 +322,16 @@ class HttpUriPlugin {
310322 * @returns {void }
311323 */
312324 apply ( compiler ) {
325+ const proxy =
326+ this . _proxy || process . env [ "http_proxy" ] || process . env [ "HTTP_PROXY" ] ;
313327 const schemes = [
314328 {
315329 scheme : "http" ,
316- fetch : proxyFetch ( getHttp ( ) )
330+ fetch : proxyFetch ( getHttp ( ) , proxy )
317331 } ,
318332 {
319333 scheme : "https" ,
320- fetch : proxyFetch ( getHttps ( ) )
334+ fetch : proxyFetch ( getHttps ( ) , proxy )
321335 }
322336 ] ;
323337 let lockfileCache ;
@@ -700,13 +714,11 @@ class HttpUriPlugin {
700714 } ) ;
701715 } ) ;
702716 }
703- ) . then ( request =>
704- request . on ( "error" , err => {
705- logger . log ( `GET ${ url } (error)` ) ;
706- err . message += `\nwhile fetching ${ url } ` ;
707- callback ( err ) ;
708- } )
709- ) ;
717+ ) . on ( "error" , err => {
718+ logger . log ( `GET ${ url } (error)` ) ;
719+ err . message += `\nwhile fetching ${ url } ` ;
720+ callback ( err ) ;
721+ } ) ;
710722 } ;
711723
712724 const fetchContent = cachedWithKey (
0 commit comments