Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/http-client/__tests__/keepalive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ describe('basics', () => {
_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 () => {
const res: httpm.HttpClientResponse = await _http.get(
'http://postman-echo.com/get'
Expand Down
11 changes: 3 additions & 8 deletions packages/http-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ export class HttpClient {
agent = this._proxyAgent
}

if (this._keepAlive && !useProxy) {
if (!useProxy) {
agent = this._agent
}

Expand Down Expand Up @@ -690,18 +690,13 @@ export class HttpClient {
this._proxyAgent = agent
}

// if reusing agent across request and tunneling agent isn't assigned create a new agent
if (this._keepAlive && !agent) {
// if tunneling agent isn't assigned create a new agent
if (!agent) {
const options = {keepAlive: this._keepAlive, maxSockets}
agent = usingSsl ? new https.Agent(options) : new http.Agent(options)
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) {
// 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
Expand Down