Skip to content

Commit f05009b

Browse files
committed
support to specify the buildHttp.proxy for HttpUrlPlugin
1 parent 8524b28 commit f05009b

9 files changed

Lines changed: 68 additions & 22 deletions

File tree

declarations/WebpackOptions.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,10 @@ export interface HttpUriOptions {
29002900
* Location of the lockfile.
29012901
*/
29022902
lockfileLocation?: string;
2903+
/**
2904+
* Proxy configuration, which can be used to specify a proxy server to use for HTTP requests.
2905+
*/
2906+
proxy?: string;
29032907
/**
29042908
* When set, resources of existing lockfile entries will be fetched and entries will be upgraded when resource content has changed.
29052909
*/

declarations/plugins/schemes/HttpUriPlugin.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export interface HttpUriOptions {
3434
* Location of the lockfile.
3535
*/
3636
lockfileLocation?: string;
37+
/**
38+
* Proxy configuration, which can be used to specify a proxy server to use for HTTP requests.
39+
*/
40+
proxy?: string;
3741
/**
3842
* When set, resources of existing lockfile entries will be fetched and entries will be upgraded when resource content has changed.
3943
*/

lib/schemes/HttpUriPlugin.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"use strict";
77

8+
const EventEmitter = require("events");
89
const { extname, basename } = require("path");
910
const { URL } = require("url");
1011
const { createGunzip, createBrotliDecompress, createInflate } = require("zlib");
@@ -19,13 +20,15 @@ const memoize = require("../util/memoize");
1920

2021
const getHttp = memoize(() => require("http"));
2122
const 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} */
5263
let 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(

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,10 @@
14251425
"type": "string",
14261426
"absolutePath": true
14271427
},
1428+
"proxy": {
1429+
"description": "Proxy configuration, which can be used to specify a proxy server to use for HTTP requests.",
1430+
"type": "string"
1431+
},
14281432
"upgrade": {
14291433
"description": "When set, resources of existing lockfile entries will be fetched and entries will be upgraded when resource content has changed.",
14301434
"type": "boolean"

schemas/plugins/schemes/HttpUriPlugin.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/plugins/schemes/HttpUriPlugin.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
"type": "string",
3030
"absolutePath": true
3131
},
32+
"proxy": {
33+
"description": "Proxy configuration, which can be used to specify a proxy server to use for HTTP requests.",
34+
"type": "string"
35+
},
3236
"upgrade": {
3337
"description": "When set, resources of existing lockfile entries will be fetched and entries will be upgraded when resource content has changed.",
3438
"type": "boolean"

test/__snapshots__/Cli.basictest.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,19 @@ Object {
568568
"multiple": false,
569569
"simpleType": "string",
570570
},
571+
"experiments-build-http-proxy": Object {
572+
"configs": Array [
573+
Object {
574+
"description": "Proxy configuration, which can be used to specify a proxy server to use for HTTP requests.",
575+
"multiple": false,
576+
"path": "experiments.buildHttp.proxy",
577+
"type": "string",
578+
},
579+
],
580+
"description": "Proxy configuration, which can be used to specify a proxy server to use for HTTP requests.",
581+
"multiple": false,
582+
"simpleType": "string",
583+
},
571584
"experiments-build-http-upgrade": Object {
572585
"configs": Array [
573586
Object {

types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4563,6 +4563,11 @@ declare interface HttpUriOptions {
45634563
*/
45644564
lockfileLocation?: string;
45654565

4566+
/**
4567+
* Proxy configuration, which can be used to specify a proxy server to use for HTTP requests.
4568+
*/
4569+
proxy?: string;
4570+
45664571
/**
45674572
* When set, resources of existing lockfile entries will be fetched and entries will be upgraded when resource content has changed.
45684573
*/

0 commit comments

Comments
 (0)