Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-js): ignore a Retry-After beyond the number range

Yudhi Armyndharisc7ef79a5dc9c
diff --git a/sdk/javascript/src/errors.ts b/sdk/javascript/src/errors.ts
index ecdf7941c..b40a1c22f 100644
--- a/sdk/javascript/src/errors.ts
+++ b/sdk/javascript/src/errors.ts
@@ -183,7 +183,8 @@ function retryAfterSeconds(fromBody: unknown, header: string | null | undefined)
if (typeof fromBody === 'number' && Number.isFinite(fromBody) && fromBody >= 0) return Math.ceil(fromBody);
const value = header?.trim();
if (!value) return undefined;
- if (/^\d+$/.test(value)) return Number(value);
+ // A digit string past Number's range reads as Infinity, which setTimeout turns into an immediate retry.
+ if (/^\d+$/.test(value)) return Number.isFinite(Number(value)) ? Number(value) : undefined;
// Date.parse also accepts '-5' or '1.5' as a past date, which would read as "retry now".
if (!/^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/.test(value)) return undefined;
const at = Date.parse(value);
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index 26264c565..b9aa7827a 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -321,7 +321,7 @@ describe('OpenWAClient', () => {
expect(dated.retryAfterSeconds).toBeGreaterThanOrEqual(0);
expect(dated.retryAfterSeconds).toBeLessThanOrEqual(3);
// Only whole seconds or an HTTP date count; Date.parse would read '-5' or '1.5' as a past date.
- for (const bad of ['soon', '-5', '1.5', 'Tue 5']) {
+ for (const bad of ['soon', '-5', '1.5', 'Tue 5', '9'.repeat(400)]) {
expect((await fail({ status: 503, headers: { 'Retry-After': bad } })).retryAfterSeconds).toBeUndefined();
}