diff --git a/packages/bruno-requests/src/index.ts b/packages/bruno-requests/src/index.ts index dd728899c..66bea3acb 100644 --- a/packages/bruno-requests/src/index.ts +++ b/packages/bruno-requests/src/index.ts @@ -9,6 +9,7 @@ export { default as createVaultClient, VaultError } from './utils/node-vault'; export type { VaultClient, VaultConfig, VaultRequestOptions } from './utils/node-vault'; export { getHttpHttpsAgents } from './utils/http-https-agents'; export { initializeShellEnv } from './utils/shell-env'; +export { getOrCreateAgent, clearAgentCache, getAgentCacheSize } from './utils/agent-cache'; export * as scripting from './scripting'; diff --git a/packages/bruno-requests/src/utils/http-https-agents.ts b/packages/bruno-requests/src/utils/http-https-agents.ts index 8671779a2..b28ef8ffe 100644 --- a/packages/bruno-requests/src/utils/http-https-agents.ts +++ b/packages/bruno-requests/src/utils/http-https-agents.ts @@ -10,6 +10,7 @@ import { HttpProxyAgent } from 'http-proxy-agent'; import { isEmpty, get, isUndefined, isNull } from 'lodash'; import { getCACertificates } from './ca-cert'; import { transformProxyConfig } from './proxy-util'; +import { getOrCreateAgent } from './agent-cache'; const DEFAULT_PORTS: Record = { ftp: 21, @@ -113,6 +114,12 @@ type GetCertsAndProxyConfigResult = { certsConfig: CertsConfig; }; +type TimelineEntry = { + timestamp: Date; + type: 'info' | 'tls' | 'error'; + message: string; +}; + type CreateAgentsParams = { requestUrl?: string; proxyMode: ProxyMode; @@ -120,6 +127,7 @@ type CreateAgentsParams = { certsConfig: CertsConfig; httpsAgentRequestFields: HttpsAgentRequestFields; systemProxyConfig?: SystemProxyConfig; + timeline?: TimelineEntry[]; }; type GetHttpHttpsAgentsParams = { @@ -132,6 +140,7 @@ type GetHttpHttpsAgentsParams = { collectionLevelProxy?: ProxyConfig; appLevelProxyConfig?: Record; systemProxyConfig?: SystemProxyConfig; + timeline?: TimelineEntry[]; }; /** @@ -342,7 +351,8 @@ function createAgents({ proxyConfig, systemProxyConfig, certsConfig, - httpsAgentRequestFields + httpsAgentRequestFields, + timeline }: CreateAgentsParams): AgentResult { // Ensure TLS options are properly set const tlsOptions: TlsOptions = { @@ -383,14 +393,14 @@ function createAgents({ if (socksEnabled) { httpAgent = new SocksProxyAgent(proxyUri); - httpsAgent = new SocksProxyAgent(proxyUri, tlsOptions as any); + httpsAgent = getOrCreateAgent(SocksProxyAgent, tlsOptions as any, proxyUri, timeline || null) as HttpsAgent; } else { httpAgent = new HttpProxyAgent(proxyUri); - httpsAgent = new PatchedHttpsProxyAgent(proxyUri, tlsOptions); + httpsAgent = getOrCreateAgent(PatchedHttpsProxyAgent, tlsOptions as any, proxyUri, timeline || null) as HttpsAgent; } } else { // If proxy should not be used, set default HTTPS agent - httpsAgent = new https.Agent(tlsOptions as any); + httpsAgent = getOrCreateAgent(https.Agent, tlsOptions as any, null, timeline || null) as HttpsAgent; } } else if (proxyMode === 'system') { const http_proxy = get(systemProxyConfig, 'http_proxy'); @@ -409,18 +419,18 @@ function createAgents({ try { if (https_proxy?.length) { new URL(https_proxy); - httpsAgent = new PatchedHttpsProxyAgent(https_proxy, tlsOptions as any); + httpsAgent = getOrCreateAgent(PatchedHttpsProxyAgent, tlsOptions as any, https_proxy, timeline || null) as HttpsAgent; } else { - httpsAgent = new https.Agent(tlsOptions as any); + httpsAgent = getOrCreateAgent(https.Agent, tlsOptions as any, null, timeline || null) as HttpsAgent; } } catch (error) { throw new Error('Invalid system https_proxy'); } } else { - httpsAgent = new https.Agent(tlsOptions as any); + httpsAgent = getOrCreateAgent(https.Agent, tlsOptions as any, null, timeline || null) as HttpsAgent; } } else { - httpsAgent = new https.Agent(tlsOptions as any); + httpsAgent = getOrCreateAgent(https.Agent, tlsOptions as any, null, timeline || null) as HttpsAgent; } return { httpAgent, httpsAgent }; @@ -433,7 +443,8 @@ const getHttpHttpsAgents = async ({ collectionLevelProxy, appLevelProxyConfig, systemProxyConfig, - options + options, + timeline }: GetHttpHttpsAgentsParams): Promise => { const { proxyMode, proxyConfig, certsConfig } = getCertsAndProxyConfig({ requestUrl, @@ -460,7 +471,8 @@ const getHttpHttpsAgents = async ({ proxyConfig, systemProxyConfig, certsConfig, - httpsAgentRequestFields + httpsAgentRequestFields, + timeline }); return { httpAgent, httpsAgent };