Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-js): arm the timeout for a numeric-string timeoutMs

Yudhi Armyndharis4c7816a5b53e
diff --git a/sdk/javascript/src/http.ts b/sdk/javascript/src/http.ts
index 27e0f9c83..2d2a22f8e 100644
--- a/sdk/javascript/src/http.ts
+++ b/sdk/javascript/src/http.ts
@@ -125,11 +125,13 @@ async function send<T>(
consume: (res: Response) => Promise<T>,
): Promise<T> {
const url = buildUrl(config.baseUrl, options.path, options.query);
- const timeoutMs = options.timeoutMs ?? config.timeoutMs;
+ // Coerced once: untyped JS config can pass a numeric string (process.env), which Number.isFinite
+ // below would otherwise read as "no timeout".
+ const timeoutMs = Number(options.timeoutMs ?? config.timeoutMs);
const controller = new AbortController();
- // 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.
+ // 0, Infinity or NaN 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))
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index a87edf9cc..7436581ec 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -274,6 +274,22 @@ describe('OpenWAClient', () => {
}
});
+ it('arms the timeout for a numeric string from untyped config', async () => {
+ // A plain-JS caller passing process.env.OPENWA_TIMEOUT_MS hands over a string.
+ const slowFetch: FetchLike = async (_url, init) => {
+ await new Promise(resolve => setTimeout(resolve, 50));
+ if (init?.signal?.aborted) {
+ const e = new Error('aborted');
+ e.name = 'AbortError';
+ throw e;
+ }
+ return new Response('[]', { status: 200 });
+ };
+ const timeoutMs = '5' as unknown as number;
+ const c = new OpenWAClient({ baseUrl: 'http://localhost', apiKey: 'k', timeoutMs, fetch: slowFetch });
+ await expect(c.sessions.list()).rejects.toThrow(new OpenWATimeoutError(5));
+ });
+
it('keeps X-API-Key winning over defaultHeaders', async () => {
const t = new MockTransport().on('GET', '/api/sessions', { body: [] });
const c = new OpenWAClient({