Skip to content

Commit d2c52cb

Browse files
authored
Merge pull request #15354 from aleen42/proxy
support using proxy sockets within HttpUrlPlugin
2 parents 1f0266b + f05009b commit d2c52cb

9 files changed

Lines changed: 80 additions & 5 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: 44 additions & 3 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,6 +20,44 @@ const memoize = require("../util/memoize");
1920

2021
const getHttp = memoize(() => require("http"));
2122
const getHttps = memoize(() => require("https"));
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"));
29+
30+
if (proxy) {
31+
const { hostname: host, port } = new URL(proxy);
32+
33+
getHttp()
34+
.request({
35+
host, // IP address of proxy server
36+
port, // port of proxy server
37+
method: "CONNECT",
38+
path: url.host
39+
})
40+
.on("connect", (res, socket) => {
41+
if (res.statusCode === 200) {
42+
// connected to proxy server
43+
doRequest(socket);
44+
}
45+
})
46+
.on("error", err => {
47+
eventEmitter.emit(
48+
"error",
49+
new Error(
50+
`Failed to connect to proxy server "${proxy}": ${err.message}`
51+
)
52+
);
53+
})
54+
.end();
55+
} else {
56+
doRequest();
57+
}
58+
59+
return eventEmitter;
60+
};
2261

2362
/** @type {(() => void)[] | undefined} */
2463
let inProgressWrite = undefined;
@@ -274,6 +313,7 @@ class HttpUriPlugin {
274313
this._upgrade = options.upgrade;
275314
this._frozen = options.frozen;
276315
this._allowedUris = options.allowedUris;
316+
this._proxy = options.proxy;
277317
}
278318

279319
/**
@@ -282,15 +322,16 @@ class HttpUriPlugin {
282322
* @returns {void}
283323
*/
284324
apply(compiler) {
325+
const proxy =
326+
this._proxy || process.env["http_proxy"] || process.env["HTTP_PROXY"];
285327
const schemes = [
286328
{
287329
scheme: "http",
288-
fetch: (url, options, callback) => getHttp().get(url, options, callback)
330+
fetch: proxyFetch(getHttp(), proxy)
289331
},
290332
{
291333
scheme: "https",
292-
fetch: (url, options, callback) =>
293-
getHttps().get(url, options, callback)
334+
fetch: proxyFetch(getHttps(), proxy)
294335
}
295336
];
296337
let lockfileCache;

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)