diff --git a/packages/github/src/internal/utils.ts b/packages/github/src/internal/utils.ts index d7edde7e..7993f9a9 100644 --- a/packages/github/src/internal/utils.ts +++ b/packages/github/src/internal/utils.ts @@ -21,7 +21,7 @@ export function getProxyAgent(destinationUrl: string): http.Agent { return hc.getAgent(destinationUrl) } -export function getProxyAgentDispatcher(destinationUrl: string): ProxyAgent | undefined { +export function getProxyAgentDispatcher(destinationUrl: string): ProxyAgent | Agent { const hc = new httpClient.HttpClient() return hc.getAgentDispatcher(destinationUrl) } diff --git a/packages/http-client/__tests__/proxy.test.ts b/packages/http-client/__tests__/proxy.test.ts index d23ec107..10e1dda5 100644 --- a/packages/http-client/__tests__/proxy.test.ts +++ b/packages/http-client/__tests__/proxy.test.ts @@ -299,7 +299,7 @@ describe('proxy', () => { it('proxy settings return ProxyAgent', async () => { process.env['https_proxy'] = 'http://127.0.0.1:8080' const httpClient = new httpm.HttpClient() - const agent = httpClient.getAgentDispatcher('https://some-url') + const agent: Agent | ProxyAgent = httpClient.getAgentDispatcher('https://some-url') // eslint-disable-next-line no-console console.log(agent) expect(agent instanceof ProxyAgent).toBe(true) @@ -307,10 +307,10 @@ describe('proxy', () => { it('proxyAuth is set in tunnel agent when authentication is provided', async () => { const httpClient = new httpm.HttpClient() - const agent = httpClient.getAgentDispatcher('https://some-url') + const agent: Agent | ProxyAgent = httpClient.getAgentDispatcher('https://some-url') // eslint-disable-next-line no-console console.log(agent) - expect(agent).toBe(undefined) + expect(agent instanceof Agent).toBe(true) }) }) diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.ts index e93b62d2..9566ad78 100644 --- a/packages/http-client/src/index.ts +++ b/packages/http-client/src/index.ts @@ -567,14 +567,16 @@ export class HttpClient { return this._getAgent(parsedUrl) } - getAgentDispatcher(serverUrl: string): ProxyAgent | undefined { + getAgentDispatcher(serverUrl: string): ProxyAgent | Agent { const parsedUrl = new URL(serverUrl) const proxyUrl = pm.getProxyUrl(parsedUrl) const useProxy = proxyUrl && proxyUrl.hostname if (useProxy) { return this._getProxyAgentDispatcher(parsedUrl, proxyUrl) } - return; + else { + return this._getAgentDispatcher(parsedUrl) + } } private _prepareRequest( @@ -761,6 +763,46 @@ export class HttpClient { return proxyAgent } + private _getAgentDispatcher(parsedUrl: URL): Agent { + let agent; + + if (this._keepAlive) { + agent = this._agentDispatcher + } + + // if agent is already assigned use that agent. + if (agent) { + return agent + } + + const usingSsl = parsedUrl.protocol === 'https:' + let maxSockets = 100 + if (this.requestOptions) { + maxSockets = this.requestOptions.maxSockets || http.globalAgent.maxSockets + } + + // if reusing agent across request and tunneling agent isn't assigned create a new agent + if (!agent) { + agent = new Agent( + { + pipelining: (!this._keepAlive ? 0 : 1), + } + ) + this._agentDispatcher = agent + } + + if (usingSsl && this._ignoreSslError) { + // we don't want to set NODE_TLS_REJECT_UNAUTHORIZED=0 since that will affect request for entire process + // http.RequestOptions doesn't expose a way to modify RequestOptions.agent.options + // we have to cast it to any and change it directly + agent.options = Object.assign(agent.options.connect || {}, { + rejectUnauthorized: false + }) + } + + return agent + } + private async _performExponentialBackoff(retryNumber: number): Promise { retryNumber = Math.min(ExponentialBackoffCeiling, retryNumber) const ms: number = ExponentialBackoffTimeSlice * Math.pow(2, retryNumber)