MCPcopy Index your code
hub / github.com/nodejs/node / setGlobalProxyFromEnv

Function setGlobalProxyFromEnv

lib/http.js:132–182  ·  view source on GitHub ↗
(env = process.env)

Source from the content-addressed store, hash-verified

130}
131
132function setGlobalProxyFromEnv(env = process.env) {
133 validateObject(env, 'proxyEnv');
134 const httpProxy = parseProxyUrl(env, 'http:');
135 const httpsProxy = parseProxyUrl(env, 'https:');
136 const noProxy = env.no_proxy || env.NO_PROXY;
137
138 if (!httpProxy && !httpsProxy) {
139 return () => {};
140 }
141
142 if (httpProxy && !URL.canParse(httpProxy)) {
143 throw new ERR_PROXY_INVALID_CONFIG(httpProxy);
144 }
145 if (httpsProxy && !URL.canParse(httpsProxy)) {
146 throw new ERR_PROXY_INVALID_CONFIG(httpsProxy);
147 }
148
149 let originalDispatcher, originalHttpsAgent, originalHttpAgent;
150 if (httpProxy || httpsProxy) {
151 // Set it for fetch.
152 const { setGlobalDispatcher, getGlobalDispatcher, EnvHttpProxyAgent } = lazyUndici();
153 const envHttpProxyAgent = new EnvHttpProxyAgent({
154 __proto__: null, httpProxy, httpsProxy, noProxy,
155 });
156 originalDispatcher = getGlobalDispatcher();
157 setGlobalDispatcher(envHttpProxyAgent);
158 }
159
160 if (httpProxy) {
161 originalHttpAgent = module.exports.globalAgent;
162 module.exports.globalAgent = getGlobalAgent(env, httpAgent.Agent);
163 }
164 if (httpsProxy && !!process.versions.openssl) {
165 const https = require('https');
166 originalHttpsAgent = https.globalAgent;
167 https.globalAgent = getGlobalAgent(env, https.Agent);
168 }
169
170 return function restore() {
171 if (originalDispatcher) {
172 const { setGlobalDispatcher } = lazyUndici();
173 setGlobalDispatcher(originalDispatcher);
174 }
175 if (originalHttpAgent) {
176 module.exports.globalAgent = originalHttpAgent;
177 }
178 if (originalHttpsAgent) {
179 require('https').globalAgent = originalHttpsAgent;
180 }
181 };
182}
183
184module.exports = {
185 _connectionListener,

Callers

nothing calls this directly

Calls 7

parseProxyUrlFunction · 0.85
getGlobalDispatcherFunction · 0.85
setGlobalDispatcherFunction · 0.85
getGlobalAgentFunction · 0.85
lazyUndiciFunction · 0.70
canParseMethod · 0.65
requireFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…