mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
http: add http.setGlobalProxyFromEnv()
This adds an API to dynamically enable built-in proxy support for all of fetch() and http.request()/https.request(), so that users do not have to be aware of them all and configure them one by one. PR-URL: https://github.com/nodejs/node/pull/60953 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
This commit is contained in:
61
lib/http.js
61
lib/http.js
@@ -25,11 +25,12 @@ const {
|
||||
ObjectDefineProperty,
|
||||
} = primordials;
|
||||
|
||||
const { validateInteger } = require('internal/validators');
|
||||
const { validateInteger, validateObject } = require('internal/validators');
|
||||
const httpAgent = require('_http_agent');
|
||||
const { ClientRequest } = require('_http_client');
|
||||
const { methods, parsers } = require('_http_common');
|
||||
const { IncomingMessage } = require('_http_incoming');
|
||||
const { ERR_PROXY_INVALID_CONFIG } = require('internal/errors').codes;
|
||||
const {
|
||||
validateHeaderName,
|
||||
validateHeaderValue,
|
||||
@@ -41,6 +42,11 @@ const {
|
||||
Server,
|
||||
ServerResponse,
|
||||
} = require('_http_server');
|
||||
const {
|
||||
parseProxyUrl,
|
||||
getGlobalAgent,
|
||||
} = require('internal/http');
|
||||
const { URL } = require('internal/url');
|
||||
let maxHeaderSize;
|
||||
let undici;
|
||||
|
||||
@@ -123,6 +129,58 @@ function lazyUndici() {
|
||||
return undici ??= require('internal/deps/undici/undici');
|
||||
}
|
||||
|
||||
function setGlobalProxyFromEnv(env = process.env) {
|
||||
validateObject(env, 'proxyEnv');
|
||||
const httpProxy = parseProxyUrl(env, 'http:');
|
||||
const httpsProxy = parseProxyUrl(env, 'https:');
|
||||
const noProxy = env.no_proxy || env.NO_PROXY;
|
||||
|
||||
if (!httpProxy && !httpsProxy) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
if (httpProxy && !URL.canParse(httpProxy)) {
|
||||
throw new ERR_PROXY_INVALID_CONFIG(httpProxy);
|
||||
}
|
||||
if (httpsProxy && !URL.canParse(httpsProxy)) {
|
||||
throw new ERR_PROXY_INVALID_CONFIG(httpsProxy);
|
||||
}
|
||||
|
||||
let originalDispatcher, originalHttpsAgent, originalHttpAgent;
|
||||
if (httpProxy || httpsProxy) {
|
||||
// Set it for fetch.
|
||||
const { setGlobalDispatcher, getGlobalDispatcher, EnvHttpProxyAgent } = lazyUndici();
|
||||
const envHttpProxyAgent = new EnvHttpProxyAgent({
|
||||
__proto__: null, httpProxy, httpsProxy, noProxy,
|
||||
});
|
||||
originalDispatcher = getGlobalDispatcher();
|
||||
setGlobalDispatcher(envHttpProxyAgent);
|
||||
}
|
||||
|
||||
if (httpProxy) {
|
||||
originalHttpAgent = module.exports.globalAgent;
|
||||
module.exports.globalAgent = getGlobalAgent(env, httpAgent.Agent);
|
||||
}
|
||||
if (httpsProxy && !!process.versions.openssl) {
|
||||
const https = require('https');
|
||||
originalHttpsAgent = https.globalAgent;
|
||||
https.globalAgent = getGlobalAgent(env, https.Agent);
|
||||
}
|
||||
|
||||
return function restore() {
|
||||
if (originalDispatcher) {
|
||||
const { setGlobalDispatcher } = lazyUndici();
|
||||
setGlobalDispatcher(originalDispatcher);
|
||||
}
|
||||
if (originalHttpAgent) {
|
||||
module.exports.globalAgent = originalHttpAgent;
|
||||
}
|
||||
if (originalHttpsAgent) {
|
||||
require('https').globalAgent = originalHttpsAgent;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
_connectionListener,
|
||||
METHODS: methods.toSorted(),
|
||||
@@ -142,6 +200,7 @@ module.exports = {
|
||||
validateInteger(max, 'max', 1);
|
||||
parsers.max = max;
|
||||
},
|
||||
setGlobalProxyFromEnv,
|
||||
};
|
||||
|
||||
ObjectDefineProperty(module.exports, 'maxHeaderSize', {
|
||||
|
||||
Reference in New Issue
Block a user