1
0
Fork 0

Make sure RequestOptions.keepAlive is applied properly on node20 runtime (#1572)

pull/1677/head
teatimeguest 2024-02-29 02:10:57 +09:00 committed by GitHub
parent df3315bbea
commit ff435e591d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 8 deletions

View File

@ -11,6 +11,12 @@ describe('basics', () => {
_http.dispose() _http.dispose()
}) })
it.each([true, false])('creates Agent with keepAlive %s', keepAlive => {
const http = new httpm.HttpClient('http-client-tests', [], {keepAlive})
const agent = http.getAgent('http://postman-echo.com')
expect(agent).toHaveProperty('keepAlive', keepAlive)
})
it('does basic http get request with keepAlive true', async () => { it('does basic http get request with keepAlive true', async () => {
const res: httpm.HttpClientResponse = await _http.get( const res: httpm.HttpClientResponse = await _http.get(
'http://postman-echo.com/get' 'http://postman-echo.com/get'

View File

@ -649,7 +649,7 @@ export class HttpClient {
agent = this._proxyAgent agent = this._proxyAgent
} }
if (this._keepAlive && !useProxy) { if (!useProxy) {
agent = this._agent agent = this._agent
} }
@ -690,18 +690,13 @@ export class HttpClient {
this._proxyAgent = agent this._proxyAgent = agent
} }
// if reusing agent across request and tunneling agent isn't assigned create a new agent // if tunneling agent isn't assigned create a new agent
if (this._keepAlive && !agent) { if (!agent) {
const options = {keepAlive: this._keepAlive, maxSockets} const options = {keepAlive: this._keepAlive, maxSockets}
agent = usingSsl ? new https.Agent(options) : new http.Agent(options) agent = usingSsl ? new https.Agent(options) : new http.Agent(options)
this._agent = agent this._agent = agent
} }
// if not using private agent and tunnel agent isn't setup then use global agent
if (!agent) {
agent = usingSsl ? https.globalAgent : http.globalAgent
}
if (usingSsl && this._ignoreSslError) { if (usingSsl && this._ignoreSslError) {
// we don't want to set NODE_TLS_REJECT_UNAUTHORIZED=0 since that will affect request for entire process // 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 // http.RequestOptions doesn't expose a way to modify RequestOptions.agent.options