This commit is contained in:
tamaina 2023-01-02 19:00:05 +00:00
parent e55c7d5062
commit 64f59ea55b
1 changed files with 17 additions and 21 deletions

View File

@ -19,32 +19,26 @@ export class HttpRequestService {
/** /**
* Get http non-proxy agent (undici) * Get http non-proxy agent (undici)
*/ */
private agent: undici.Agent; private nonProxiedAgent: undici.Agent;
/** /**
* Get http proxy or non-proxy agent (undici) * Get http proxy or non-proxy agent (undici)
*/ */
public proxiedAgent: undici.ProxyAgent | undici.Agent; public agent: undici.ProxyAgent | undici.Agent;
/** //#region for old http/https, only used in S3Service
* Get http non-proxy agent // http non-proxy agent
*/
private http: http.Agent; private http: http.Agent;
/** // https non-proxy agent
* Get https non-proxy agent
*/
private https: https.Agent; private https: https.Agent;
/** // http proxy or non-proxy agent
* Get http proxy or non-proxy agent
*/
public httpAgent: http.Agent; public httpAgent: http.Agent;
/** // https proxy or non-proxy agent
* Get https proxy or non-proxy agent
*/
public httpsAgent: https.Agent; public httpsAgent: https.Agent;
//#endregion
public readonly dnsCache: CacheableLookup; public readonly dnsCache: CacheableLookup;
public readonly clientDefaults: undici.Agent.Options; public readonly clientDefaults: undici.Agent.Options;
@ -70,7 +64,7 @@ export class HttpRequestService {
}, },
} }
this.agent = new undici.Agent({ this.nonProxiedAgent = new undici.Agent({
...this.clientDefaults, ...this.clientDefaults,
}); });
@ -88,15 +82,16 @@ export class HttpRequestService {
const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128); const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128);
this.proxiedAgent = config.proxy this.agent = config.proxy
? new undici.ProxyAgent({ ? new undici.ProxyAgent({
...this.clientDefaults, ...this.clientDefaults,
connections: maxSockets, connections: maxSockets,
uri: config.proxy, uri: config.proxy,
}) })
: this.agent; : this.nonProxiedAgent;
//#region for old http/https, only used in S3Service
this.httpAgent = config.proxy this.httpAgent = config.proxy
? new HttpProxyAgent({ ? new HttpProxyAgent({
keepAlive: true, keepAlive: true,
@ -119,6 +114,7 @@ export class HttpRequestService {
}) })
: this.https; : this.https;
} }
//#endregion
/** /**
* Get agent by URL * Get agent by URL
@ -128,14 +124,14 @@ export class HttpRequestService {
@bindThis @bindThis
public getAgentByUrl(url: URL, bypassProxy = false): undici.Agent | undici.ProxyAgent { public getAgentByUrl(url: URL, bypassProxy = false): undici.Agent | undici.ProxyAgent {
if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) { if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) {
return this.agent; return this.nonProxiedAgent;
} else { } else {
return this.proxiedAgent; return this.agent;
} }
} }
/** /**
* Get agent by URL * Get http agent by URL
* @param url URL * @param url URL
* @param bypassProxy Allways bypass proxy * @param bypassProxy Allways bypass proxy
*/ */
@ -177,7 +173,7 @@ export class HttpRequestService {
size: 1024 * 256, size: 1024 * 256,
}); });
return await res.json(); return await res.json() as T;
} }
@bindThis @bindThis