Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-js): cap retryAfterSeconds at what setTimeout accepts

Yudhi Armyndharis8f368c7766a1
diff --git a/sdk/javascript/README.md b/sdk/javascript/README.md
index 61b56c13b..a646aefd2 100644
--- a/sdk/javascript/README.md
+++ b/sdk/javascript/README.md
@@ -59,7 +59,8 @@ repeating a throttled request straight away prolongs the throttle; a catalog 503
can persist because WhatsApp may never answer that query, so bound any retry. A
429 from the global rate limiter lifts when its window expires (seconds for the
per-second tier, up to an hour for the hourly tier by default), and
-`.retryAfterSeconds` carries its `Retry-After` header. A 429 whose `.code` is
+`.retryAfterSeconds` carries its `Retry-After` header, never above 2147483 seconds (about 24.8 days),
+so that `retryAfterSeconds * 1000` is always a valid `setTimeout` delay. A 429 whose `.code` is
`"SEND_PACING_LIMITED"` is usually not transient: do not retry it before
`.retryAfterSeconds`, which then comes from the body: a few seconds when only
sends still in flight caused it, the rest of the failure breaker's cooldown
diff --git a/sdk/javascript/src/errors.ts b/sdk/javascript/src/errors.ts
index b40a1c22f..eb2421cfa 100644
--- a/sdk/javascript/src/errors.ts
+++ b/sdk/javascript/src/errors.ts
@@ -35,7 +35,8 @@ export class OpenWAApiError extends OpenWAError {
readonly code?: string;
/**
* Seconds to wait before retrying: the body's `retryAfterSeconds` when present, else the
- * `Retry-After` response header (seconds or an HTTP date). Undefined when neither is sent.
+ * `Retry-After` response header (seconds or an HTTP date). Undefined when neither is sent. A longer
+ * wait is reported as 2147483 (about 24.8 days), the most `setTimeout` accepts once multiplied by 1000.
*/
readonly retryAfterSeconds?: number;
/** The response headers, when the error came from a response. */
@@ -180,15 +181,24 @@ export function classifyApiError(
* date, clamped at 0.
*/
function retryAfterSeconds(fromBody: unknown, header: string | null | undefined): number | undefined {
- if (typeof fromBody === 'number' && Number.isFinite(fromBody) && fromBody >= 0) return Math.ceil(fromBody);
+ if (typeof fromBody === 'number' && Number.isFinite(fromBody) && fromBody >= 0) {
+ return capSeconds(Math.ceil(fromBody));
+ }
const value = header?.trim();
if (!value) return undefined;
- // 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;
+ // A digit string past Number's range reads as Infinity, which the cap brings back to the ceiling.
+ if (/^\d+$/.test(value)) return capSeconds(Number(value));
// 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);
- return Number.isNaN(at) ? undefined : Math.max(0, Math.ceil((at - Date.now()) / 1000));
+ return Number.isNaN(at) ? undefined : capSeconds(Math.max(0, Math.ceil((at - Date.now()) / 1000)));
+}
+
+/** setTimeout fires at once for a delay over 2^31-1 ms, so a longer wait would turn `seconds * 1000` into a retry now. */
+const MAX_RETRY_AFTER_SECONDS = 2_147_483;
+
+function capSeconds(seconds: number): number {
+ return Math.min(seconds, MAX_RETRY_AFTER_SECONDS);
}
/**
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index 3307c1d00..6ea81d000 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -336,10 +336,25 @@ 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', '9'.repeat(400)]) {
+ for (const bad of ['soon', '-5', '1.5', 'Tue 5']) {
expect((await fail({ status: 503, headers: { 'Retry-After': bad } })).retryAfterSeconds).toBeUndefined();
}
+ // A wait is capped at 2147483 seconds, the longest a caller can pass to setTimeout (times 1000) without
+ // it firing at once, whether it comes from the header, an HTTP date or the body.
+ const ceiling = 2147483;
+ for (const huge of ['9999999999', '1'.repeat(25), '9'.repeat(400), '2147484']) {
+ expect((await fail({ status: 503, headers: { 'Retry-After': huge } })).retryAfterSeconds).toBe(ceiling);
+ }
+ expect((await fail({ status: 503, headers: { 'Retry-After': '2147483' } })).retryAfterSeconds).toBe(ceiling);
+ expect((await fail({ status: 503, headers: { 'Retry-After': '86400' } })).retryAfterSeconds).toBe(86400);
+ const farDate = await fail({ status: 503, headers: { 'Retry-After': 'Fri, 31 Dec 9999 23:59:59 GMT' } });
+ expect(farDate.retryAfterSeconds).toBe(ceiling);
+ for (const retryAfterSeconds of [1e300, 9999999999, 2147483.5]) {
+ const err = await fail({ status: 429, body: { statusCode: 429, message: 'x', retryAfterSeconds } });
+ expect(err.retryAfterSeconds).toBe(ceiling);
+ }
+
const logout = await fail({
status: 502,
body: { statusCode: 502, message: 'x', code: 'SESSION_LOGOUT_INCOMPLETE' },