Skip to content

Commit f51f759

Browse files
authored
fix(node-fetch): keepAlive for Node.js 19+ (#424)
Fix issue because of which `node-fetch` used default agent, which after Node.js 19+ has `keepAlive` enabled by default.
1 parent cf5aa71 commit f51f759

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

src/transport/node-transport.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,16 @@ export class NodeTransport implements Transport {
189189
* @internal
190190
*/
191191
private agentForTransportRequest(req: TransportRequest): HttpAgent | HttpsAgent | undefined {
192-
// Don't configure any agents if keep alive not requested.
193-
if (!this.keepAlive && !this.proxyConfiguration) return undefined;
194-
195192
// Create proxy agent (if possible).
196193
if (this.proxyConfiguration)
197194
return this.proxyAgent ? this.proxyAgent : (this.proxyAgent = new ProxyAgent(this.proxyConfiguration));
198195

199196
// Create keep alive agent.
200197
const useSecureAgent = req.origin!.startsWith('https:');
201198

202-
if (useSecureAgent && this.httpsAgent === undefined)
203-
this.httpsAgent = new HttpsAgent({ keepAlive: true, ...this.keepAliveSettings });
204-
else if (!useSecureAgent && this.httpAgent === undefined) {
205-
this.httpAgent = new HttpAgent({ keepAlive: true, ...this.keepAliveSettings });
206-
}
199+
const agentOptions = { keepAlive: this.keepAlive, ...(this.keepAlive ? this.keepAliveSettings : {}) };
200+
if (useSecureAgent && this.httpsAgent === undefined) this.httpsAgent = new HttpsAgent(agentOptions);
201+
else if (!useSecureAgent && this.httpAgent === undefined) this.httpAgent = new HttpAgent(agentOptions);
207202

208203
return useSecureAgent ? this.httpsAgent : this.httpAgent;
209204
}

0 commit comments

Comments
 (0)