Skip to content

Commit ff435e5

Browse files
authored
Make sure RequestOptions.keepAlive is applied properly on node20 runtime (#1572)
1 parent df3315b commit ff435e5

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

packages/http-client/__tests__/keepalive.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ describe('basics', () => {
1111
_http.dispose()
1212
})
1313

14+
it.each([true, false])('creates Agent with keepAlive %s', keepAlive => {
15+
const http = new httpm.HttpClient('http-client-tests', [], {keepAlive})
16+
const agent = http.getAgent('http://postman-echo.com')
17+
expect(agent).toHaveProperty('keepAlive', keepAlive)
18+
})
19+
1420
it('does basic http get request with keepAlive true', async () => {
1521
const res: httpm.HttpClientResponse = await _http.get(
1622
'http://postman-echo.com/get'

packages/http-client/src/index.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ export class HttpClient {
649649
agent = this._proxyAgent
650650
}
651651

652-
if (this._keepAlive && !useProxy) {
652+
if (!useProxy) {
653653
agent = this._agent
654654
}
655655

@@ -690,18 +690,13 @@ export class HttpClient {
690690
this._proxyAgent = agent
691691
}
692692

693-
// if reusing agent across request and tunneling agent isn't assigned create a new agent
694-
if (this._keepAlive && !agent) {
693+
// if tunneling agent isn't assigned create a new agent
694+
if (!agent) {
695695
const options = {keepAlive: this._keepAlive, maxSockets}
696696
agent = usingSsl ? new https.Agent(options) : new http.Agent(options)
697697
this._agent = agent
698698
}
699699

700-
// if not using private agent and tunnel agent isn't setup then use global agent
701-
if (!agent) {
702-
agent = usingSsl ? https.globalAgent : http.globalAgent
703-
}
704-
705700
if (usingSsl && this._ignoreSslError) {
706701
// we don't want to set NODE_TLS_REJECT_UNAUTHORIZED=0 since that will affect request for entire process
707702
// http.RequestOptions doesn't expose a way to modify RequestOptions.agent.options

0 commit comments

Comments
 (0)