@wildix/xbees-conversations-utils 1.1.75 → 1.1.76

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.
@@ -3,20 +3,22 @@ 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
+ const RETRY_AFTER_HEADER_PADDING_MS = 1500;
6
7
  function toPositiveMilliseconds(milliseconds) {
7
8
  return Math.max(0, Math.ceil(milliseconds));
8
9
  }
9
- function toHttpDateFromDelayMs(delayMs) {
10
+ function toRetryAfterSecondsFromDelayMs(delayMs) {
10
11
  const positiveDelayMs = toPositiveMilliseconds(delayMs);
11
- return new Date(Date.now() + positiveDelayMs).toUTCString();
12
+ return Math.ceil(positiveDelayMs / 1000);
12
13
  }
13
14
  function createRateLimitExceededException(options = {}) {
14
15
  const { rateLimit, message, operation, retryAfterMs = constants_1.DEFAULT_RETRY_AFTER_MS } = options;
15
16
  const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
16
17
  const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
17
18
  const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
19
+ const paddedRetryAfterMs = effectiveRetryAfterMs + RETRY_AFTER_HEADER_PADDING_MS;
18
20
  const retryAfterMilliseconds = toPositiveMilliseconds(effectiveRetryAfterMs);
19
- const retryAfterHttpDate = toHttpDateFromDelayMs(effectiveRetryAfterMs);
21
+ const retryAfterSeconds = toRetryAfterSecondsFromDelayMs(paddedRetryAfterMs);
20
22
  const rateLimitResetMilliseconds = rateLimit?.reset
21
23
  ? toPositiveMilliseconds(retryAfterFromResetMs ?? 0)
22
24
  : retryAfterMilliseconds;
@@ -24,7 +26,7 @@ function createRateLimitExceededException(options = {}) {
24
26
  $metadata: {},
25
27
  message: message || defaultMessage,
26
28
  rateLimitRemaining: rateLimit?.remaining || 0,
27
- retryAfter: retryAfterHttpDate,
29
+ retryAfter: retryAfterSeconds,
28
30
  rateLimit: rateLimit?.limit?.toString() || 'unknown',
29
31
  rateLimitReset: rateLimitResetMilliseconds,
30
32
  });
@@ -1,19 +1,21 @@
1
1
  import { RateLimitExceededException } from '@wildix/xbees-conversations-client';
2
2
  import { DEFAULT_RETRY_AFTER_MS } from '../constants';
3
+ const RETRY_AFTER_HEADER_PADDING_MS = 1500;
3
4
  function toPositiveMilliseconds(milliseconds) {
4
5
  return Math.max(0, Math.ceil(milliseconds));
5
6
  }
6
- function toHttpDateFromDelayMs(delayMs) {
7
+ function toRetryAfterSecondsFromDelayMs(delayMs) {
7
8
  const positiveDelayMs = toPositiveMilliseconds(delayMs);
8
- return new Date(Date.now() + positiveDelayMs).toUTCString();
9
+ return Math.ceil(positiveDelayMs / 1000);
9
10
  }
10
11
  export function createRateLimitExceededException(options = {}) {
11
12
  const { rateLimit, message, operation, retryAfterMs = DEFAULT_RETRY_AFTER_MS } = options;
12
13
  const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
13
14
  const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
14
15
  const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
16
+ const paddedRetryAfterMs = effectiveRetryAfterMs + RETRY_AFTER_HEADER_PADDING_MS;
15
17
  const retryAfterMilliseconds = toPositiveMilliseconds(effectiveRetryAfterMs);
16
- const retryAfterHttpDate = toHttpDateFromDelayMs(effectiveRetryAfterMs);
18
+ const retryAfterSeconds = toRetryAfterSecondsFromDelayMs(paddedRetryAfterMs);
17
19
  const rateLimitResetMilliseconds = rateLimit?.reset
18
20
  ? toPositiveMilliseconds(retryAfterFromResetMs ?? 0)
19
21
  : retryAfterMilliseconds;
@@ -21,7 +23,7 @@ export function createRateLimitExceededException(options = {}) {
21
23
  $metadata: {},
22
24
  message: message || defaultMessage,
23
25
  rateLimitRemaining: rateLimit?.remaining || 0,
24
- retryAfter: retryAfterHttpDate,
26
+ retryAfter: retryAfterSeconds,
25
27
  rateLimit: rateLimit?.limit?.toString() || 'unknown',
26
28
  rateLimitReset: rateLimitResetMilliseconds,
27
29
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-conversations-utils",
3
- "version": "1.1.75",
3
+ "version": "1.1.76",
4
4
  "description": "",
5
5
  "main": "./dist-cjs/index.js",
6
6
  "module": "./dist-es/index.js",