@wildix/xbees-conversations-utils 1.1.71 → 1.1.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist-cjs/rateLimits/helpers/createRateLimitExceededException.js +8 -6
- package/dist-cjs/rateLimits/helpers/executeWithRateLimitHandling.js +1 -1
- package/dist-es/rateLimits/helpers/createRateLimitExceededException.js +8 -6
- package/dist-es/rateLimits/helpers/executeWithRateLimitHandling.js +1 -1
- package/package.json +1 -1
|
@@ -3,23 +3,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createRateLimitExceededException = void 0;
|
|
4
4
|
const xbees_conversations_client_1 = require("@wildix/xbees-conversations-client");
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
|
-
function
|
|
7
|
-
return Math.max(0, Math.ceil(milliseconds
|
|
6
|
+
function toPositiveMilliseconds(milliseconds) {
|
|
7
|
+
return Math.max(0, Math.ceil(milliseconds));
|
|
8
8
|
}
|
|
9
9
|
function createRateLimitExceededException(options = {}) {
|
|
10
10
|
const { rateLimit, message, operation, retryAfterMs = constants_1.DEFAULT_RETRY_AFTER_MS } = options;
|
|
11
11
|
const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
|
|
12
12
|
const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
|
|
13
13
|
const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
|
|
14
|
-
const
|
|
15
|
-
const
|
|
14
|
+
const retryAfterMilliseconds = toPositiveMilliseconds(effectiveRetryAfterMs);
|
|
15
|
+
const rateLimitResetMilliseconds = rateLimit?.reset
|
|
16
|
+
? toPositiveMilliseconds(retryAfterFromResetMs ?? 0)
|
|
17
|
+
: retryAfterMilliseconds;
|
|
16
18
|
return new xbees_conversations_client_1.RateLimitExceededException({
|
|
17
19
|
$metadata: {},
|
|
18
20
|
message: message || defaultMessage,
|
|
19
21
|
rateLimitRemaining: rateLimit?.remaining || 0,
|
|
20
|
-
retryAfter:
|
|
22
|
+
retryAfter: retryAfterMilliseconds,
|
|
21
23
|
rateLimit: rateLimit?.limit?.toString() || 'unknown',
|
|
22
|
-
rateLimitReset:
|
|
24
|
+
rateLimitReset: rateLimitResetMilliseconds,
|
|
23
25
|
});
|
|
24
26
|
}
|
|
25
27
|
exports.createRateLimitExceededException = createRateLimitExceededException;
|
|
@@ -8,7 +8,7 @@ const processStreamRateLimitException_1 = require("./processStreamRateLimitExcep
|
|
|
8
8
|
const RedisCooldown_1 = require("./RedisCooldown");
|
|
9
9
|
function calculateRateLimitDelayMs(error, maxDelayMs) {
|
|
10
10
|
const hasServerRetryAfter = typeof error.retryAfter === 'number' && error.retryAfter > 0;
|
|
11
|
-
const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter
|
|
11
|
+
const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter) : 0;
|
|
12
12
|
const baseDelayMs = hasServerRetryAfter ? retryAfterMsFromError + constants_1.RETRY_AFTER_BUFFER_MS : constants_1.DEFAULT_RETRY_AFTER_MS;
|
|
13
13
|
if (baseDelayMs > 0) {
|
|
14
14
|
const jitterMs = Math.floor(Math.random() * constants_1.DEFAULT_JITTER_MS);
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import { RateLimitExceededException } from '@wildix/xbees-conversations-client';
|
|
2
2
|
import { DEFAULT_RETRY_AFTER_MS } from '../constants';
|
|
3
|
-
function
|
|
4
|
-
return Math.max(0, Math.ceil(milliseconds
|
|
3
|
+
function toPositiveMilliseconds(milliseconds) {
|
|
4
|
+
return Math.max(0, Math.ceil(milliseconds));
|
|
5
5
|
}
|
|
6
6
|
export function createRateLimitExceededException(options = {}) {
|
|
7
7
|
const { rateLimit, message, operation, retryAfterMs = DEFAULT_RETRY_AFTER_MS } = options;
|
|
8
8
|
const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
|
|
9
9
|
const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
|
|
10
10
|
const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
|
|
11
|
-
const
|
|
12
|
-
const
|
|
11
|
+
const retryAfterMilliseconds = toPositiveMilliseconds(effectiveRetryAfterMs);
|
|
12
|
+
const rateLimitResetMilliseconds = rateLimit?.reset
|
|
13
|
+
? toPositiveMilliseconds(retryAfterFromResetMs ?? 0)
|
|
14
|
+
: retryAfterMilliseconds;
|
|
13
15
|
return new RateLimitExceededException({
|
|
14
16
|
$metadata: {},
|
|
15
17
|
message: message || defaultMessage,
|
|
16
18
|
rateLimitRemaining: rateLimit?.remaining || 0,
|
|
17
|
-
retryAfter:
|
|
19
|
+
retryAfter: retryAfterMilliseconds,
|
|
18
20
|
rateLimit: rateLimit?.limit?.toString() || 'unknown',
|
|
19
|
-
rateLimitReset:
|
|
21
|
+
rateLimitReset: rateLimitResetMilliseconds,
|
|
20
22
|
});
|
|
21
23
|
}
|
|
@@ -5,7 +5,7 @@ import { processStreamRateLimitException } from './processStreamRateLimitExcepti
|
|
|
5
5
|
import { RedisCooldown } from './RedisCooldown';
|
|
6
6
|
function calculateRateLimitDelayMs(error, maxDelayMs) {
|
|
7
7
|
const hasServerRetryAfter = typeof error.retryAfter === 'number' && error.retryAfter > 0;
|
|
8
|
-
const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter
|
|
8
|
+
const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter) : 0;
|
|
9
9
|
const baseDelayMs = hasServerRetryAfter ? retryAfterMsFromError + RETRY_AFTER_BUFFER_MS : DEFAULT_RETRY_AFTER_MS;
|
|
10
10
|
if (baseDelayMs > 0) {
|
|
11
11
|
const jitterMs = Math.floor(Math.random() * DEFAULT_JITTER_MS);
|