Skip to content

Commit 8524b28

Browse files
committed
support using proxy sockets within HttpUrlPlugin
1 parent 4abf353 commit 8524b28

1 file changed

Lines changed: 37 additions & 8 deletions

File tree

lib/schemes/HttpUriPlugin.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@ const memoize = require("../util/memoize");
1919

2020
const getHttp = memoize(() => require("http"));
2121
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();
27+
28+
const { hostname: host, port } = new URL(proxyUrl);
29+
30+
getHttp()
31+
.request({
32+
host, // IP address of proxy server
33+
port, // port of proxy server
34+
method: "CONNECT",
35+
path: url.host
36+
})
37+
.on("connect", (res, socket) => {
38+
if (res.statusCode === 200) {
39+
// connected to proxy server
40+
resolve(socket);
41+
}
42+
})
43+
.on("error", err => {
44+
console.error("Failed to connect to the proxy:", err);
45+
})
46+
.end();
47+
}).then(socket =>
48+
request.get(url, { ...options, ...(socket && { socket }) }, callback)
49+
);
2250

2351
/** @type {(() => void)[] | undefined} */
2452
let inProgressWrite = undefined;
@@ -285,12 +313,11 @@ class HttpUriPlugin {
285313
const schemes = [
286314
{
287315
scheme: "http",
288-
fetch: (url, options, callback) => getHttp().get(url, options, callback)
316+
fetch: proxyFetch(getHttp())
289317
},
290318
{
291319
scheme: "https",
292-
fetch: (url, options, callback) =>
293-
getHttps().get(url, options, callback)
320+
fetch: proxyFetch(getHttps())
294321
}
295322
];
296323
let lockfileCache;
@@ -673,11 +700,13 @@ class HttpUriPlugin {
673700
});
674701
});
675702
}
676-
).on("error", err => {
677-
logger.log(`GET ${url} (error)`);
678-
err.message += `\nwhile fetching ${url}`;
679-
callback(err);
680-
});
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+
);
681710
};
682711

683712
const fetchContent = cachedWithKey(

0 commit comments

Comments
 (0)