1
0
Fork 0
takost/update-github-package
Tatyana Kostromskaya 2023-10-04 15:45:37 +00:00
parent f699b96ecf
commit 844682e182
3 changed files with 48 additions and 6 deletions

View File

@ -21,7 +21,7 @@ export function getProxyAgent(destinationUrl: string): http.Agent {
return hc.getAgent(destinationUrl) return hc.getAgent(destinationUrl)
} }
export function getProxyAgentDispatcher(destinationUrl: string): ProxyAgent | undefined { export function getProxyAgentDispatcher(destinationUrl: string): ProxyAgent | Agent {
const hc = new httpClient.HttpClient() const hc = new httpClient.HttpClient()
return hc.getAgentDispatcher(destinationUrl) return hc.getAgentDispatcher(destinationUrl)
} }

View File

@ -299,7 +299,7 @@ describe('proxy', () => {
it('proxy settings return ProxyAgent', async () => { it('proxy settings return ProxyAgent', async () => {
process.env['https_proxy'] = 'http://127.0.0.1:8080' process.env['https_proxy'] = 'http://127.0.0.1:8080'
const httpClient = new httpm.HttpClient() 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 // eslint-disable-next-line no-console
console.log(agent) console.log(agent)
expect(agent instanceof ProxyAgent).toBe(true) expect(agent instanceof ProxyAgent).toBe(true)
@ -307,10 +307,10 @@ describe('proxy', () => {
it('proxyAuth is set in tunnel agent when authentication is provided', async () => { it('proxyAuth is set in tunnel agent when authentication is provided', async () => {
const httpClient = new httpm.HttpClient() 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 // eslint-disable-next-line no-console
console.log(agent) console.log(agent)
expect(agent).toBe(undefined) expect(agent instanceof Agent).toBe(true)
}) })
}) })

View File

@ -567,14 +567,16 @@ export class HttpClient {
return this._getAgent(parsedUrl) return this._getAgent(parsedUrl)
} }
getAgentDispatcher(serverUrl: string): ProxyAgent | undefined { getAgentDispatcher(serverUrl: string): ProxyAgent | Agent {
const parsedUrl = new URL(serverUrl) const parsedUrl = new URL(serverUrl)
const proxyUrl = pm.getProxyUrl(parsedUrl) const proxyUrl = pm.getProxyUrl(parsedUrl)
const useProxy = proxyUrl && proxyUrl.hostname const useProxy = proxyUrl && proxyUrl.hostname
if (useProxy) { if (useProxy) {
return this._getProxyAgentDispatcher(parsedUrl, proxyUrl) return this._getProxyAgentDispatcher(parsedUrl, proxyUrl)
} }
return; else {
return this._getAgentDispatcher(parsedUrl)
}
} }
private _prepareRequest( private _prepareRequest(
@ -761,6 +763,46 @@ export class HttpClient {
return proxyAgent 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<void> { private async _performExponentialBackoff(retryNumber: number): Promise<void> {
retryNumber = Math.min(ExponentialBackoffCeiling, retryNumber) retryNumber = Math.min(ExponentialBackoffCeiling, retryNumber)
const ms: number = ExponentialBackoffTimeSlice * Math.pow(2, retryNumber) const ms: number = ExponentialBackoffTimeSlice * Math.pow(2, retryNumber)