perf: 系统设置中的代理设置优化为可全局生效,环境变量中的https_proxy设置将无效

This commit is contained in:
xiaojunnuo
2024-11-15 23:52:18 +08:00
parent 0ca61b4d99
commit 381a37fbaa
2 changed files with 14 additions and 3 deletions

View File

@@ -202,6 +202,9 @@ export type HttpClient = {
request<D = any, R = any>(config: HttpRequestConfig<D>): Promise<HttpClientResponse<R>>;
};
// const http_proxy_backup = process.env.HTTP_PROXY || process.env.http_proxy;
// const https_proxy_backup = process.env.HTTPS_PROXY || process.env.https_proxy;
export type CreateAgentOptions = {
httpProxy?: string;
httpsProxy?: string;
@@ -216,20 +219,28 @@ export function createAgent(opts: CreateAgentOptions = {}) {
);
let httpAgent, httpsAgent;
const httpProxy = opts.httpProxy || process.env.HTTP_PROXY || process.env.http_proxy;
const httpProxy = opts.httpProxy;
if (httpProxy) {
process.env.HTTP_PROXY = httpProxy;
process.env.http_proxy = httpProxy;
logger.info('use httpProxy:', httpProxy);
httpAgent = new HttpProxyAgent(httpProxy, opts as any);
merge(httpAgent.options, opts);
} else {
process.env.HTTP_PROXY = '';
process.env.http_proxy = '';
httpAgent = new nodeHttp.Agent(opts);
}
const httpsProxy = opts.httpsProxy || process.env.HTTPS_PROXY || process.env.https_proxy;
const httpsProxy = opts.httpsProxy;
if (httpsProxy) {
process.env.HTTPS_PROXY = httpProxy;
process.env.https_proxy = httpProxy;
logger.info('use httpsProxy:', httpsProxy);
httpsAgent = new HttpsProxyAgent(httpsProxy, opts as any);
merge(httpsAgent.options, opts);
} else {
process.env.HTTPS_PROXY = '';
process.env.https_proxy = '';
httpsAgent = new https.Agent(opts);
}
return {