From 44287c22b4a41e4461b74c7d5cbfb4b4b9ad6b2f Mon Sep 17 00:00:00 2001 From: Reeshika Hosmani Date: Tue, 4 Aug 2026 19:39:34 +0530 Subject: [PATCH] Revert "Default to keep-alive agents in Node environments for better performance" --- src/lib/contentstack-core.ts | 15 ++------------- test/contentstack-core.node-agent.spec.ts | 22 ---------------------- test/contentstack-core.spec.ts | 23 ----------------------- 3 files changed, 2 insertions(+), 58 deletions(-) delete mode 100644 test/contentstack-core.node-agent.spec.ts diff --git a/src/lib/contentstack-core.ts b/src/lib/contentstack-core.ts index 8a2aa2d..69f7b3c 100644 --- a/src/lib/contentstack-core.ts +++ b/src/lib/contentstack-core.ts @@ -4,17 +4,6 @@ import axios, { AxiosRequestHeaders, getAdapter } from 'axios'; import { AxiosInstance, HttpClientParams } from './types'; import { ERROR_MESSAGES } from './error-messages'; -const isNodeEnvironment = typeof window === 'undefined'; - -// Guarded require: keeps 'http'/'https' out of browser bundles, which have no browser field of their own to redirect this. -function createKeepAliveAgent(moduleName: 'http' | 'https') { - if (!isNodeEnvironment) { - return false as const; - } - - return new (require(moduleName).Agent)({ keepAlive: true }); -} - export function httpClient(options: HttpClientParams): AxiosInstance { const defaultConfig = { insecure: false, @@ -22,8 +11,8 @@ export function httpClient(options: HttpClientParams): AxiosInstance { headers: {} as AxiosRequestHeaders, basePath: '', proxy: false as const, - httpAgent: createKeepAliveAgent('http'), - httpsAgent: createKeepAliveAgent('https'), + httpAgent: false, + httpsAgent: false, timeout: 30000, logHandler: (level: string, data?: any) => { if (level === 'error') { diff --git a/test/contentstack-core.node-agent.spec.ts b/test/contentstack-core.node-agent.spec.ts deleted file mode 100644 index 508345b..0000000 --- a/test/contentstack-core.node-agent.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -/** - * @jest-environment node - */ -import http from 'http'; -import https from 'https'; -import { httpClient } from '../src/lib/contentstack-core'; - -describe('httpClient default connection agents (Node environment)', () => { - it('should default httpAgent to a keepAlive http.Agent when not explicitly provided', () => { - const instance = httpClient({}); - - expect(instance.defaults.httpAgent).toBeInstanceOf(http.Agent); - expect((instance.defaults.httpAgent as any).keepAlive).toBe(true); - }); - - it('should default httpsAgent to a keepAlive https.Agent when not explicitly provided', () => { - const instance = httpClient({}); - - expect(instance.defaults.httpsAgent).toBeInstanceOf(https.Agent); - expect((instance.defaults.httpsAgent as any).keepAlive).toBe(true); - }); -}); diff --git a/test/contentstack-core.spec.ts b/test/contentstack-core.spec.ts index abd0e7b..0c4da51 100644 --- a/test/contentstack-core.spec.ts +++ b/test/contentstack-core.spec.ts @@ -77,29 +77,6 @@ describe('contentstackCore', () => { }); }); - describe('connection agents', () => { - it.each(['httpAgent', 'httpsAgent'])( - 'should preserve an explicitly provided %s instead of defaulting it', - (agentOption) => { - const customAgent = { custom: true }; - const options = { [agentOption]: customAgent }; - - const instance = httpClient(options as any); - - expect((instance.defaults as any)[agentOption]).toEqual(customAgent); - } - ); - - it.each(['httpAgent', 'httpsAgent'])('should default %s to false in a browser-like environment', (agentOption) => { - // This spec file runs under jsdom (see jest.preset.js), so `window` is - // already defined here - matching a real browser, unlike the Node-only - // agent behavior covered in contentstack-core.node-agent.spec.ts. - const instance = httpClient({}); - - expect((instance.defaults as any)[agentOption]).toBe(false); - }); - }); - describe('config.headers', () => { it('should include apiKey in headers when provided', () => { const options = {