From c0483b36c1c6f76506d4fda4fbe1cbe104587b60 Mon Sep 17 00:00:00 2001 From: Serhii Mamontov Date: Wed, 11 Dec 2024 22:30:49 +0200 Subject: [PATCH] fix(node-fetch): `keepAlive` for Node.js 19+ Fix issue because of which `node-fetch` used default agent, which after Node.js 19+ has `keepAlive` enabled by default. --- src/transport/node-transport.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/transport/node-transport.ts b/src/transport/node-transport.ts index a77a6fe7e..63526bfd2 100644 --- a/src/transport/node-transport.ts +++ b/src/transport/node-transport.ts @@ -189,9 +189,6 @@ export class NodeTransport implements Transport { * @internal */ private agentForTransportRequest(req: TransportRequest): HttpAgent | HttpsAgent | undefined { - // Don't configure any agents if keep alive not requested. - if (!this.keepAlive && !this.proxyConfiguration) return undefined; - // Create proxy agent (if possible). if (this.proxyConfiguration) return this.proxyAgent ? this.proxyAgent : (this.proxyAgent = new ProxyAgent(this.proxyConfiguration)); @@ -199,11 +196,9 @@ export class NodeTransport implements Transport { // Create keep alive agent. const useSecureAgent = req.origin!.startsWith('https:'); - if (useSecureAgent && this.httpsAgent === undefined) - this.httpsAgent = new HttpsAgent({ keepAlive: true, ...this.keepAliveSettings }); - else if (!useSecureAgent && this.httpAgent === undefined) { - this.httpAgent = new HttpAgent({ keepAlive: true, ...this.keepAliveSettings }); - } + const agentOptions = { keepAlive: this.keepAlive, ...(this.keepAlive ? this.keepAliveSettings : {}) }; + if (useSecureAgent && this.httpsAgent === undefined) this.httpsAgent = new HttpsAgent(agentOptions); + else if (!useSecureAgent && this.httpAgent === undefined) this.httpAgent = new HttpAgent(agentOptions); return useSecureAgent ? this.httpsAgent : this.httpAgent; }