@wildix/xbees-conversations-utils 1.1.72 → 1.1.73

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.
@@ -6,12 +6,17 @@ const constants_1 = require("../constants");
6
6
  function toPositiveMilliseconds(milliseconds) {
7
7
  return Math.max(0, Math.ceil(milliseconds));
8
8
  }
9
+ function toUnixEpochSecondsFromDelayMs(delayMs) {
10
+ const positiveDelayMs = toPositiveMilliseconds(delayMs);
11
+ return Math.ceil((Date.now() + positiveDelayMs) / 1000);
12
+ }
9
13
  function createRateLimitExceededException(options = {}) {
10
14
  const { rateLimit, message, operation, retryAfterMs = constants_1.DEFAULT_RETRY_AFTER_MS } = options;
11
15
  const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
12
16
  const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
13
17
  const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
14
18
  const retryAfterMilliseconds = toPositiveMilliseconds(effectiveRetryAfterMs);
19
+ const retryAfterUnixEpochSeconds = toUnixEpochSecondsFromDelayMs(effectiveRetryAfterMs);
15
20
  const rateLimitResetMilliseconds = rateLimit?.reset
16
21
  ? toPositiveMilliseconds(retryAfterFromResetMs ?? 0)
17
22
  : retryAfterMilliseconds;
@@ -19,7 +24,7 @@ function createRateLimitExceededException(options = {}) {
19
24
  $metadata: {},
20
25
  message: message || defaultMessage,
21
26
  rateLimitRemaining: rateLimit?.remaining || 0,
22
- retryAfter: retryAfterMilliseconds,
27
+ retryAfter: retryAfterUnixEpochSeconds,
23
28
  rateLimit: rateLimit?.limit?.toString() || 'unknown',
24
29
  rateLimitReset: rateLimitResetMilliseconds,
25
30
  });
@@ -6,9 +6,18 @@ const constants_1 = require("../constants");
6
6
  const createRateLimitExceededException_1 = require("./createRateLimitExceededException");
7
7
  const processStreamRateLimitException_1 = require("./processStreamRateLimitException");
8
8
  const RedisCooldown_1 = require("./RedisCooldown");
9
+ function normalizeRetryAfterToDelayMs(retryAfter) {
10
+ if (retryAfter > 1000000000) {
11
+ return Math.max(0, Math.round(retryAfter * 1000 - Date.now()));
12
+ }
13
+ if (retryAfter >= 1000) {
14
+ return Math.max(0, Math.round(retryAfter));
15
+ }
16
+ return Math.max(0, Math.round(retryAfter * 1000));
17
+ }
9
18
  function calculateRateLimitDelayMs(error, maxDelayMs) {
10
19
  const hasServerRetryAfter = typeof error.retryAfter === 'number' && error.retryAfter > 0;
11
- const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter) : 0;
20
+ const retryAfterMsFromError = hasServerRetryAfter ? normalizeRetryAfterToDelayMs(error.retryAfter) : 0;
12
21
  const baseDelayMs = hasServerRetryAfter ? retryAfterMsFromError + constants_1.RETRY_AFTER_BUFFER_MS : constants_1.DEFAULT_RETRY_AFTER_MS;
13
22
  if (baseDelayMs > 0) {
14
23
  const jitterMs = Math.floor(Math.random() * constants_1.DEFAULT_JITTER_MS);
@@ -3,12 +3,17 @@ import { DEFAULT_RETRY_AFTER_MS } from '../constants';
3
3
  function toPositiveMilliseconds(milliseconds) {
4
4
  return Math.max(0, Math.ceil(milliseconds));
5
5
  }
6
+ function toUnixEpochSecondsFromDelayMs(delayMs) {
7
+ const positiveDelayMs = toPositiveMilliseconds(delayMs);
8
+ return Math.ceil((Date.now() + positiveDelayMs) / 1000);
9
+ }
6
10
  export function createRateLimitExceededException(options = {}) {
7
11
  const { rateLimit, message, operation, retryAfterMs = DEFAULT_RETRY_AFTER_MS } = options;
8
12
  const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
9
13
  const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
10
14
  const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
11
15
  const retryAfterMilliseconds = toPositiveMilliseconds(effectiveRetryAfterMs);
16
+ const retryAfterUnixEpochSeconds = toUnixEpochSecondsFromDelayMs(effectiveRetryAfterMs);
12
17
  const rateLimitResetMilliseconds = rateLimit?.reset
13
18
  ? toPositiveMilliseconds(retryAfterFromResetMs ?? 0)
14
19
  : retryAfterMilliseconds;
@@ -16,7 +21,7 @@ export function createRateLimitExceededException(options = {}) {
16
21
  $metadata: {},
17
22
  message: message || defaultMessage,
18
23
  rateLimitRemaining: rateLimit?.remaining || 0,
19
- retryAfter: retryAfterMilliseconds,
24
+ retryAfter: retryAfterUnixEpochSeconds,
20
25
  rateLimit: rateLimit?.limit?.toString() || 'unknown',
21
26
  rateLimitReset: rateLimitResetMilliseconds,
22
27
  });
@@ -3,9 +3,18 @@ import { DEFAULT_JITTER_MS, DEFAULT_MAX_DELAY_MS, DEFAULT_RETRY_AFTER_MS, RETRY_
3
3
  import { createRateLimitExceededException } from './createRateLimitExceededException';
4
4
  import { processStreamRateLimitException } from './processStreamRateLimitException';
5
5
  import { RedisCooldown } from './RedisCooldown';
6
+ function normalizeRetryAfterToDelayMs(retryAfter) {
7
+ if (retryAfter > 1000000000) {
8
+ return Math.max(0, Math.round(retryAfter * 1000 - Date.now()));
9
+ }
10
+ if (retryAfter >= 1000) {
11
+ return Math.max(0, Math.round(retryAfter));
12
+ }
13
+ return Math.max(0, Math.round(retryAfter * 1000));
14
+ }
6
15
  function calculateRateLimitDelayMs(error, maxDelayMs) {
7
16
  const hasServerRetryAfter = typeof error.retryAfter === 'number' && error.retryAfter > 0;
8
- const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter) : 0;
17
+ const retryAfterMsFromError = hasServerRetryAfter ? normalizeRetryAfterToDelayMs(error.retryAfter) : 0;
9
18
  const baseDelayMs = hasServerRetryAfter ? retryAfterMsFromError + RETRY_AFTER_BUFFER_MS : DEFAULT_RETRY_AFTER_MS;
10
19
  if (baseDelayMs > 0) {
11
20
  const jitterMs = Math.floor(Math.random() * DEFAULT_JITTER_MS);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-conversations-utils",
3
- "version": "1.1.72",
3
+ "version": "1.1.73",
4
4
  "description": "",
5
5
  "main": "./dist-cjs/index.js",
6
6
  "module": "./dist-es/index.js",