mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-30 08:04:09 +00:00
refactor(cache): rename getOrCreateAgent to getOrCreateHttpsAgent
This commit is contained in:
@@ -23,7 +23,7 @@ const { createFormData } = require('../utils/form-data');
|
||||
const protocolRegex = /^([-+\w]{1,25})(:?\/\/|:)/;
|
||||
const { NtlmClient } = require('axios-ntlm');
|
||||
const { addDigestInterceptor, getHttpHttpsAgents, makeAxiosInstance: makeAxiosInstanceForOauth2 } = require('@usebruno/requests');
|
||||
const { getCACertificates, transformProxyConfig, getOrCreateAgent, getOrCreateHttpAgent } = require('@usebruno/requests');
|
||||
const { getCACertificates, transformProxyConfig, getOrCreateHttpsAgent, getOrCreateHttpAgent } = require('@usebruno/requests');
|
||||
const { getOAuth2Token, getFormattedOauth2Credentials } = require('../utils/oauth2');
|
||||
const tokenStore = require('../store/tokenStore');
|
||||
const { encodeUrl, buildFormUrlEncodedPayload, extractPromptVariables, isFormData } = require('@usebruno/common').utils;
|
||||
@@ -462,13 +462,13 @@ const runSingleRequest = async function (
|
||||
// Only set the agent needed for the request protocol
|
||||
if (socksEnabled) {
|
||||
if (isHttpsRequest) {
|
||||
request.httpsAgent = getOrCreateAgent({ AgentClass: SocksProxyAgent, options: tlsOptions, proxyUri, disableCache, hostname });
|
||||
request.httpsAgent = getOrCreateHttpsAgent({ AgentClass: SocksProxyAgent, options: tlsOptions, proxyUri, disableCache, hostname });
|
||||
} else {
|
||||
request.httpAgent = getOrCreateHttpAgent({ AgentClass: SocksProxyAgent, options: httpAgentOptions, proxyUri, disableCache, hostname });
|
||||
}
|
||||
} else {
|
||||
if (isHttpsRequest) {
|
||||
request.httpsAgent = getOrCreateAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri, disableCache, hostname });
|
||||
request.httpsAgent = getOrCreateHttpsAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri, disableCache, hostname });
|
||||
} else {
|
||||
request.httpAgent = getOrCreateHttpAgent({ AgentClass: HttpProxyAgent, options: httpAgentOptions, proxyUri, disableCache, hostname });
|
||||
}
|
||||
@@ -490,7 +490,7 @@ const runSingleRequest = async function (
|
||||
try {
|
||||
if (https_proxy?.length && isHttpsRequest) {
|
||||
new URL(https_proxy);
|
||||
request.httpsAgent = getOrCreateAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri: https_proxy, disableCache, hostname });
|
||||
request.httpsAgent = getOrCreateHttpsAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri: https_proxy, disableCache, hostname });
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error('Invalid system https_proxy');
|
||||
@@ -504,7 +504,7 @@ const runSingleRequest = async function (
|
||||
|
||||
if (!request.httpAgent && !request.httpsAgent) {
|
||||
if (isHttpsRequest) {
|
||||
request.httpsAgent = getOrCreateAgent({ AgentClass: https.Agent, options: tlsOptions, disableCache, hostname });
|
||||
request.httpsAgent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: tlsOptions, disableCache, hostname });
|
||||
} else {
|
||||
request.httpAgent = getOrCreateHttpAgent({ AgentClass: http.Agent, options: httpAgentOptions, disableCache, hostname });
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ const { interpolateString } = require('../ipc/network/interpolate-string');
|
||||
const { SocksProxyAgent } = require('socks-proxy-agent');
|
||||
const { HttpProxyAgent } = require('http-proxy-agent');
|
||||
const { isEmpty, get, isUndefined, isNull } = require('lodash');
|
||||
const { getOrCreateAgent, getOrCreateHttpAgent } = require('@usebruno/requests');
|
||||
const { getOrCreateHttpsAgent, getOrCreateHttpAgent } = require('@usebruno/requests');
|
||||
const { preferencesUtil } = require('../store/preferences');
|
||||
|
||||
const DEFAULT_PORTS = {
|
||||
@@ -143,13 +143,13 @@ function setupProxyAgents({
|
||||
// Only set the agent needed for the request protocol
|
||||
if (socksEnabled) {
|
||||
if (isHttpsRequest) {
|
||||
requestConfig.httpsAgent = getOrCreateAgent({ AgentClass: SocksProxyAgent, options: tlsOptions, proxyUri, timeline, disableCache, hostname });
|
||||
requestConfig.httpsAgent = getOrCreateHttpsAgent({ AgentClass: SocksProxyAgent, options: tlsOptions, proxyUri, timeline, disableCache, hostname });
|
||||
} else {
|
||||
requestConfig.httpAgent = getOrCreateHttpAgent({ AgentClass: SocksProxyAgent, options: { keepAlive: true }, proxyUri, timeline, disableCache, hostname });
|
||||
}
|
||||
} else {
|
||||
if (isHttpsRequest) {
|
||||
requestConfig.httpsAgent = getOrCreateAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri, timeline, disableCache, hostname });
|
||||
requestConfig.httpsAgent = getOrCreateHttpsAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri, timeline, disableCache, hostname });
|
||||
} else {
|
||||
requestConfig.httpAgent = getOrCreateHttpAgent({ AgentClass: HttpProxyAgent, options: { keepAlive: true }, proxyUri, timeline, disableCache, hostname });
|
||||
}
|
||||
@@ -184,7 +184,7 @@ function setupProxyAgents({
|
||||
message: `Using system proxy: ${https_proxy}`
|
||||
});
|
||||
}
|
||||
requestConfig.httpsAgent = getOrCreateAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri: https_proxy, timeline, disableCache, hostname });
|
||||
requestConfig.httpsAgent = getOrCreateHttpsAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions, proxyUri: https_proxy, timeline, disableCache, hostname });
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Invalid system https_proxy "${https_proxy}": ${error.message}`);
|
||||
@@ -194,7 +194,7 @@ function setupProxyAgents({
|
||||
|
||||
if (!requestConfig.httpAgent && !requestConfig.httpsAgent) {
|
||||
if (isHttpsRequest) {
|
||||
requestConfig.httpsAgent = getOrCreateAgent({ AgentClass: https.Agent, options: tlsOptions, proxyUri: null, timeline, disableCache, hostname });
|
||||
requestConfig.httpsAgent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: tlsOptions, proxyUri: null, timeline, disableCache, hostname });
|
||||
} else {
|
||||
requestConfig.httpAgent = getOrCreateHttpAgent({ AgentClass: http.Agent, options: { keepAlive: true }, proxyUri: null, timeline, disableCache, hostname });
|
||||
}
|
||||
|
||||
@@ -9,7 +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, getOrCreateHttpAgent, clearAgentCache, getAgentCacheSize } from './utils/agent-cache';
|
||||
export { getOrCreateHttpsAgent, getOrCreateHttpAgent, clearAgentCache, getAgentCacheSize } from './utils/agent-cache';
|
||||
|
||||
export * as scripting from './scripting';
|
||||
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import https from 'node:https';
|
||||
import http from 'node:http';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { getOrCreateAgent, getOrCreateHttpAgent, clearAgentCache, getAgentCacheSize } from './agent-cache';
|
||||
import { getOrCreateHttpsAgent, getOrCreateHttpAgent, clearAgentCache, getAgentCacheSize } from './agent-cache';
|
||||
|
||||
describe('Agent Cache', () => {
|
||||
beforeEach(() => {
|
||||
clearAgentCache();
|
||||
});
|
||||
|
||||
describe('getOrCreateAgent', () => {
|
||||
describe('getOrCreateHttpsAgent', () => {
|
||||
it('creates a new agent when cache is empty', () => {
|
||||
const agent = getOrCreateAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true } });
|
||||
const agent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true } });
|
||||
|
||||
expect(agent).toBeInstanceOf(https.Agent);
|
||||
expect(getAgentCacheSize()).toBe(1);
|
||||
@@ -19,56 +19,56 @@ describe('Agent Cache', () => {
|
||||
it('returns cached agent for identical options', () => {
|
||||
const options = { rejectUnauthorized: true, keepAlive: true };
|
||||
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options });
|
||||
|
||||
expect(agent1).toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(1);
|
||||
});
|
||||
|
||||
it('creates separate agents for different rejectUnauthorized values', () => {
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true } });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: false } });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true } });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: false } });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
});
|
||||
|
||||
it('creates separate agents for different CA certificates', () => {
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: { ca: 'cert-a' } });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: { ca: 'cert-b' } });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { ca: 'cert-a' } });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { ca: 'cert-b' } });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
});
|
||||
|
||||
it('creates separate agents for different cert values', () => {
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: { cert: Buffer.from('cert-a') } });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: { cert: Buffer.from('cert-b') } });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { cert: Buffer.from('cert-a') } });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { cert: Buffer.from('cert-b') } });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
});
|
||||
|
||||
it('creates separate agents for different key values', () => {
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: { key: Buffer.from('key-a') } });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: { key: Buffer.from('key-b') } });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { key: Buffer.from('key-a') } });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { key: Buffer.from('key-b') } });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
});
|
||||
|
||||
it('creates separate agents for different pfx values', () => {
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: { pfx: Buffer.from('pfx-a') } });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: { pfx: Buffer.from('pfx-b') } });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { pfx: Buffer.from('pfx-a') } });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { pfx: Buffer.from('pfx-b') } });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
});
|
||||
|
||||
it('creates separate agents for different passphrase values', () => {
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: { passphrase: 'pass-a' } });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: { passphrase: 'pass-b' } });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { passphrase: 'pass-a' } });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { passphrase: 'pass-b' } });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
@@ -77,8 +77,8 @@ describe('Agent Cache', () => {
|
||||
it('creates separate agents for different proxy URIs', () => {
|
||||
const options = { rejectUnauthorized: true };
|
||||
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options, proxyUri: 'http://proxy1:8080' });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options, proxyUri: 'http://proxy2:8080' });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, proxyUri: 'http://proxy1:8080' });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, proxyUri: 'http://proxy2:8080' });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
@@ -87,16 +87,16 @@ describe('Agent Cache', () => {
|
||||
it('creates separate agents for different agent classes', () => {
|
||||
const options = { keepAlive: true };
|
||||
|
||||
const httpsAgent = getOrCreateAgent({ AgentClass: https.Agent, options });
|
||||
const httpAgent = getOrCreateAgent({ AgentClass: http.Agent, options });
|
||||
const httpsAgent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options });
|
||||
const httpAgent = getOrCreateHttpsAgent({ AgentClass: http.Agent, options });
|
||||
|
||||
expect(httpsAgent).not.toBe(httpAgent);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
});
|
||||
|
||||
it('creates separate agents for different keepAlive values', () => {
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: { keepAlive: true } });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: { keepAlive: false } });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { keepAlive: true } });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { keepAlive: false } });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
@@ -105,8 +105,8 @@ describe('Agent Cache', () => {
|
||||
it('creates separate agents for different hostnames', () => {
|
||||
const options = { rejectUnauthorized: true };
|
||||
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options, hostname: 'auth.example.com' });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, hostname: 'auth.example.com' });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
@@ -115,8 +115,8 @@ describe('Agent Cache', () => {
|
||||
it('returns cached agent for the same hostname', () => {
|
||||
const options = { rejectUnauthorized: true };
|
||||
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
|
||||
expect(agent1).toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(1);
|
||||
@@ -125,8 +125,8 @@ describe('Agent Cache', () => {
|
||||
it('creates separate agents for null hostname vs explicit hostname', () => {
|
||||
const options = { rejectUnauthorized: true };
|
||||
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options, hostname: null });
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, hostname: null });
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options, hostname: 'api.example.com' });
|
||||
|
||||
expect(agent1).not.toBe(agent2);
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
@@ -135,14 +135,14 @@ describe('Agent Cache', () => {
|
||||
|
||||
describe('timeline support', () => {
|
||||
it('initializes timeline array on new agents', () => {
|
||||
const agent = getOrCreateAgent({ AgentClass: https.Agent, options: {} }) as any;
|
||||
const agent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {} }) as any;
|
||||
|
||||
expect(Array.isArray(agent.timeline)).toBe(true);
|
||||
});
|
||||
|
||||
it('uses provided timeline array', () => {
|
||||
const timeline: any[] = [];
|
||||
const agent = getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline }) as any;
|
||||
const agent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline }) as any;
|
||||
|
||||
expect(agent.timeline).toBe(timeline);
|
||||
});
|
||||
@@ -151,10 +151,10 @@ describe('Agent Cache', () => {
|
||||
const timeline1: any[] = [];
|
||||
const timeline2: any[] = [];
|
||||
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 }) as any;
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 }) as any;
|
||||
expect(agent1.timeline).toBe(timeline1);
|
||||
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline: timeline2 }) as any;
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline: timeline2 }) as any;
|
||||
expect(agent1).toBe(agent2);
|
||||
expect(agent2.timeline).toBe(timeline2);
|
||||
});
|
||||
@@ -164,11 +164,11 @@ describe('Agent Cache', () => {
|
||||
const timeline2: any[] = [];
|
||||
|
||||
// First call creates new agent - no reuse message
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 });
|
||||
expect(timeline1.some((e) => e.message.includes('Reusing cached https agent'))).toBe(false);
|
||||
|
||||
// Second call reuses cached agent - should log reuse message with SSL session reuse
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline: timeline2 });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline: timeline2 });
|
||||
expect(timeline2.some((e) => e.message.includes('Reusing cached https agent'))).toBe(true);
|
||||
});
|
||||
|
||||
@@ -187,7 +187,7 @@ describe('Agent Cache', () => {
|
||||
|
||||
it('logs SSL validation status on agent creation', () => {
|
||||
const timeline: any[] = [];
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true }, timeline });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true }, timeline });
|
||||
|
||||
const sslEntry = timeline.find((e) => e.message.includes('SSL validation'));
|
||||
expect(sslEntry).toBeDefined();
|
||||
@@ -196,7 +196,7 @@ describe('Agent Cache', () => {
|
||||
|
||||
it('logs SSL validation disabled when rejectUnauthorized is false', () => {
|
||||
const timeline: any[] = [];
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: false }, timeline });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: false }, timeline });
|
||||
|
||||
const sslEntry = timeline.find((e) => e.message.includes('SSL validation'));
|
||||
expect(sslEntry).toBeDefined();
|
||||
@@ -206,8 +206,8 @@ describe('Agent Cache', () => {
|
||||
|
||||
describe('clearAgentCache', () => {
|
||||
it('removes all cached agents', () => {
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true } });
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: false } });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: true } });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { rejectUnauthorized: false } });
|
||||
expect(getAgentCacheSize()).toBe(2);
|
||||
|
||||
clearAgentCache();
|
||||
@@ -219,7 +219,7 @@ describe('Agent Cache', () => {
|
||||
|
||||
// Create several agents and attach mock destroy functions
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const agent = getOrCreateAgent({ AgentClass: https.Agent, options: { ca: `cert-${i}` } }) as any;
|
||||
const agent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { ca: `cert-${i}` } }) as any;
|
||||
const mock = jest.fn();
|
||||
agent.destroy = mock;
|
||||
destroyMocks.push(mock);
|
||||
@@ -241,7 +241,7 @@ describe('Agent Cache', () => {
|
||||
it('maintains cache size under limit', () => {
|
||||
// Create many agents with different options
|
||||
for (let i = 0; i < 150; i++) {
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: { ca: `cert-${i}` } });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { ca: `cert-${i}` } });
|
||||
}
|
||||
|
||||
// Cache should be capped at MAX_AGENT_CACHE_SIZE (100)
|
||||
@@ -250,13 +250,13 @@ describe('Agent Cache', () => {
|
||||
|
||||
it('destroys evicted agents to prevent memory leaks', () => {
|
||||
// Create first agent and attach a mock destroy function
|
||||
const firstAgent = getOrCreateAgent({ AgentClass: https.Agent, options: { ca: 'cert-to-evict' } }) as any;
|
||||
const firstAgent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { ca: 'cert-to-evict' } }) as any;
|
||||
const destroyMock = jest.fn();
|
||||
firstAgent.destroy = destroyMock;
|
||||
|
||||
// Fill cache to trigger eviction (100 more agents will evict the first one)
|
||||
for (let i = 0; i < 100; i++) {
|
||||
getOrCreateAgent({ AgentClass: https.Agent, options: { ca: `cert-${i}` } });
|
||||
getOrCreateHttpsAgent({ AgentClass: https.Agent, options: { ca: `cert-${i}` } });
|
||||
}
|
||||
|
||||
// First agent should have been evicted and destroyed
|
||||
@@ -270,8 +270,8 @@ describe('Agent Cache', () => {
|
||||
const timeline2: any[] = [];
|
||||
|
||||
// Get the same agent twice with different timelines (simulating concurrent requests)
|
||||
const agent1 = getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 }) as any;
|
||||
const agent2 = getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline: timeline2 }) as any;
|
||||
const agent1 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 }) as any;
|
||||
const agent2 = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline: timeline2 }) as any;
|
||||
|
||||
// Both should return the same cached agent
|
||||
expect(agent1).toBe(agent2);
|
||||
@@ -345,7 +345,7 @@ describe('Agent Cache', () => {
|
||||
const timeline1: any[] = [];
|
||||
const timeline2: any[] = [];
|
||||
|
||||
const agent = getOrCreateAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 }) as any;
|
||||
const agent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: {}, timeline: timeline1 }) as any;
|
||||
|
||||
// Create a mock socket
|
||||
const mockSocket = new EventEmitter() as any;
|
||||
|
||||
@@ -221,7 +221,7 @@ function getOrCreateAgentInternal<TOptions extends HttpAgentOptions>(
|
||||
* Uses LRU-style eviction when cache exceeds MAX_AGENT_CACHE_SIZE.
|
||||
* Automatically wraps the agent class with timeline logging support.
|
||||
*/
|
||||
function getOrCreateAgent({
|
||||
function getOrCreateHttpsAgent({
|
||||
AgentClass,
|
||||
options,
|
||||
proxyUri = null,
|
||||
@@ -303,4 +303,4 @@ function getAgentCacheSize(): number {
|
||||
return agentCache.size;
|
||||
}
|
||||
|
||||
export { getOrCreateAgent, getOrCreateHttpAgent, clearAgentCache, getAgentCacheSize };
|
||||
export { getOrCreateHttpsAgent, getOrCreateHttpAgent, clearAgentCache, getAgentCacheSize };
|
||||
|
||||
@@ -11,7 +11,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, getOrCreateHttpAgent } from './agent-cache';
|
||||
import { getOrCreateHttpsAgent, getOrCreateHttpAgent } from './agent-cache';
|
||||
import type { TimelineEntry } from './timeline-agent';
|
||||
|
||||
const DEFAULT_PORTS: Record<string, number> = {
|
||||
@@ -408,13 +408,13 @@ function createAgents({
|
||||
// Only set the agent needed for the request protocol
|
||||
if (socksEnabled) {
|
||||
if (isHttpsRequest) {
|
||||
httpsAgent = getOrCreateAgent({ AgentClass: SocksProxyAgent, options: tlsOptions as any, proxyUri, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
httpsAgent = getOrCreateHttpsAgent({ AgentClass: SocksProxyAgent, options: tlsOptions as any, proxyUri, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
} else {
|
||||
httpAgent = getOrCreateHttpAgent({ AgentClass: SocksProxyAgent, options: { keepAlive: true }, proxyUri, timeline: timeline || null, disableCache, hostname });
|
||||
}
|
||||
} else {
|
||||
if (isHttpsRequest) {
|
||||
httpsAgent = getOrCreateAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions as any, proxyUri, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
httpsAgent = getOrCreateHttpsAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions as any, proxyUri, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
} else {
|
||||
httpAgent = getOrCreateHttpAgent({ AgentClass: HttpProxyAgent, options: { keepAlive: true }, proxyUri, timeline: timeline || null, disableCache, hostname });
|
||||
}
|
||||
@@ -422,7 +422,7 @@ function createAgents({
|
||||
} else {
|
||||
// If proxy should not be used, only set HTTPS agent for HTTPS requests
|
||||
if (isHttpsRequest) {
|
||||
httpsAgent = getOrCreateAgent({ AgentClass: https.Agent, options: tlsOptions as any, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
httpsAgent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: tlsOptions as any, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
}
|
||||
// HTTP requests without proxy don't need a custom agent
|
||||
}
|
||||
@@ -443,7 +443,7 @@ function createAgents({
|
||||
try {
|
||||
if (https_proxy?.length && isHttpsRequest) {
|
||||
new URL(https_proxy);
|
||||
httpsAgent = getOrCreateAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions as any, proxyUri: https_proxy, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
httpsAgent = getOrCreateHttpsAgent({ AgentClass: PatchedHttpsProxyAgent, options: tlsOptions as any, proxyUri: https_proxy, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error('Invalid system https_proxy');
|
||||
@@ -453,7 +453,7 @@ function createAgents({
|
||||
|
||||
if (!httpAgent && !httpsAgent) {
|
||||
if (isHttpsRequest) {
|
||||
httpsAgent = getOrCreateAgent({ AgentClass: https.Agent, options: tlsOptions as any, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
httpsAgent = getOrCreateHttpsAgent({ AgentClass: https.Agent, options: tlsOptions as any, timeline: timeline || null, disableCache, hostname }) as HttpsAgent;
|
||||
} else {
|
||||
httpAgent = getOrCreateHttpAgent({ AgentClass: http.Agent, options: { keepAlive: true }, timeline: timeline || null, disableCache, hostname });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user