2022-01-11 23:17:41 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-05-06 00:09:29 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-01-31 21:09:58 +00:00
|
|
|
import { SECOND, MINUTE } from './durations';
|
2021-05-06 00:09:29 +00:00
|
|
|
import { isNormalNumber } from './isNormalNumber';
|
|
|
|
|
2022-01-31 21:09:58 +00:00
|
|
|
const DEFAULT_RETRY_AFTER = MINUTE;
|
2022-01-11 23:17:41 +00:00
|
|
|
const MINIMAL_RETRY_AFTER = SECOND;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-04-25 23:05:23 +00:00
|
|
|
export function parseRetryAfterWithDefault(value: unknown): number {
|
|
|
|
const retryAfter = parseRetryAfter(value);
|
|
|
|
if (retryAfter === undefined) {
|
2022-01-31 21:09:58 +00:00
|
|
|
return DEFAULT_RETRY_AFTER;
|
2021-09-02 22:31:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 23:05:23 +00:00
|
|
|
return Math.max(retryAfter, MINIMAL_RETRY_AFTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseRetryAfter(value: unknown): number | undefined {
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:09:58 +00:00
|
|
|
const retryAfter = parseInt(value, 10);
|
2021-05-06 00:09:29 +00:00
|
|
|
if (!isNormalNumber(retryAfter) || retryAfter.toString() !== value) {
|
2022-04-25 23:05:23 +00:00
|
|
|
return undefined;
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 23:05:23 +00:00
|
|
|
return retryAfter * SECOND;
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|