Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-js): stop a 0 or Infinity timeout from aborting every request

Yudhi Armyndharis2dc6462b0570
diff --git a/sdk/javascript/src/client.ts b/sdk/javascript/src/client.ts
index 5d5ddc698..2d62694c2 100644
--- a/sdk/javascript/src/client.ts
+++ b/sdk/javascript/src/client.ts
@@ -47,7 +47,7 @@ export interface OpenWAClientOptions {
baseUrl: string;
/** API key sent as `X-API-Key`. */
apiKey: string;
- /** Per-request timeout in milliseconds (default 30000). */
+ /** Per-request timeout in milliseconds (default 30000); `0` or `Infinity` turns it off. */
timeoutMs?: number;
/** Default headers applied to every request. */
defaultHeaders?: Record<string, string>;
diff --git a/sdk/javascript/src/http.ts b/sdk/javascript/src/http.ts
index eafbe8fe2..8004cbae4 100644
--- a/sdk/javascript/src/http.ts
+++ b/sdk/javascript/src/http.ts
@@ -25,7 +25,7 @@ export interface RequestOptions {
query?: object;
/** JSON-serializable request body. */
body?: unknown;
- /** Override the per-client timeout (ms) for this single request. */
+ /** Override the per-client timeout (ms) for this single request; `0` or `Infinity` turns it off. */
timeoutMs?: number;
/** Extra headers merged on top of the client defaults (auth/JSON win). */
headers?: Record<string, string>;
@@ -36,7 +36,7 @@ export interface ClientConfig {
baseUrl: string;
/** API key sent as `X-API-Key`. */
apiKey: string;
- /** Per-request timeout in milliseconds (default 30000). */
+ /** Per-request timeout in milliseconds (default 30000); `0` or `Infinity` turns it off. */
timeoutMs?: number;
/** Default headers applied to every request. */
defaultHeaders?: Record<string, string>;
@@ -128,7 +128,12 @@ async function send<T>(
const timeoutMs = options.timeoutMs ?? config.timeoutMs;
const controller = new AbortController();
- const timer = setTimeout(() => controller.abort(), timeoutMs);
+ // 0 or Infinity means no client timeout. setTimeout fires after 1 ms for a delay that is not finite
+ // or exceeds 2^31-1, so cap it rather than abort every request.
+ const timer =
+ Number.isFinite(timeoutMs) && timeoutMs > 0
+ ? setTimeout(() => controller.abort(), Math.min(timeoutMs, 2_147_483_647))
+ : undefined;
// Auth and JSON content-type WIN over caller-supplied defaults/per-request headers — the SDK only
// ever sends a JSON body, and this matches the Python and PHP SDKs (which force JSON) and the
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index c5e6b2fb7..ab67f9f95 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -241,6 +241,24 @@ describe('OpenWAClient', () => {
await expect(c.sessions.list()).rejects.toBeInstanceOf(OpenWATimeoutError);
});
+ it('turns the timeout off for 0 or Infinity, and caps a delay setTimeout cannot hold', async () => {
+ // setTimeout fires after 1 ms for a delay that is not finite or exceeds 2^31-1, which would
+ // abort every request instead of waiting longer.
+ const slowFetch: FetchLike = async (_url, init) => {
+ await new Promise(resolve => setTimeout(resolve, 20));
+ if (init?.signal?.aborted) {
+ const e = new Error('aborted');
+ e.name = 'AbortError';
+ throw e;
+ }
+ return new Response('[]', { status: 200 });
+ };
+ for (const timeoutMs of [0, Infinity, 2 ** 31]) {
+ const c = new OpenWAClient({ baseUrl: 'http://localhost', apiKey: 'k', timeoutMs, fetch: slowFetch });
+ await expect(c.sessions.list()).resolves.toEqual([]);
+ }
+ });
+
it('keeps X-API-Key winning over defaultHeaders', async () => {
const t = new MockTransport().on('GET', '/api/sessions', { body: [] });
const c = new OpenWAClient({