@wildix/xbees-conversations-utils 1.1.68 → 1.1.70

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,16 +3,23 @@ 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 toPositiveSeconds(milliseconds) {
7
+ return Math.max(0, Math.ceil(milliseconds / 1000));
8
+ }
6
9
  function createRateLimitExceededException(options = {}) {
7
10
  const { rateLimit, message, operation, retryAfterMs = constants_1.DEFAULT_RETRY_AFTER_MS } = options;
8
11
  const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
12
+ const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
13
+ const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
14
+ const retryAfterSeconds = toPositiveSeconds(effectiveRetryAfterMs);
15
+ const rateLimitResetSeconds = rateLimit?.reset ? toPositiveSeconds(retryAfterFromResetMs ?? 0) : retryAfterSeconds;
9
16
  return new xbees_conversations_client_1.RateLimitExceededException({
10
17
  $metadata: {},
11
18
  message: message || defaultMessage,
12
19
  rateLimitRemaining: rateLimit?.remaining || 0,
13
- retryAfter: rateLimit?.reset ? rateLimit.reset - Date.now() : retryAfterMs,
20
+ retryAfter: retryAfterSeconds,
14
21
  rateLimit: rateLimit?.limit?.toString() || 'unknown',
15
- rateLimitReset: rateLimit?.reset || Date.now() + retryAfterMs,
22
+ rateLimitReset: rateLimitResetSeconds,
16
23
  });
17
24
  }
18
25
  exports.createRateLimitExceededException = createRateLimitExceededException;
@@ -8,7 +8,8 @@ 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 baseDelayMs = hasServerRetryAfter ? error.retryAfter + constants_1.RETRY_AFTER_BUFFER_MS : constants_1.DEFAULT_RETRY_AFTER_MS;
11
+ const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter * 1000) : 0;
12
+ const baseDelayMs = hasServerRetryAfter ? retryAfterMsFromError + constants_1.RETRY_AFTER_BUFFER_MS : constants_1.DEFAULT_RETRY_AFTER_MS;
12
13
  if (baseDelayMs > 0) {
13
14
  const jitterMs = Math.floor(Math.random() * constants_1.DEFAULT_JITTER_MS);
14
15
  const computedDelayMs = baseDelayMs + jitterMs;
@@ -1,14 +1,21 @@
1
1
  import { RateLimitExceededException } from '@wildix/xbees-conversations-client';
2
2
  import { DEFAULT_RETRY_AFTER_MS } from '../constants';
3
+ function toPositiveSeconds(milliseconds) {
4
+ return Math.max(0, Math.ceil(milliseconds / 1000));
5
+ }
3
6
  export function createRateLimitExceededException(options = {}) {
4
7
  const { rateLimit, message, operation, retryAfterMs = DEFAULT_RETRY_AFTER_MS } = options;
5
8
  const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
9
+ const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
10
+ const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
11
+ const retryAfterSeconds = toPositiveSeconds(effectiveRetryAfterMs);
12
+ const rateLimitResetSeconds = rateLimit?.reset ? toPositiveSeconds(retryAfterFromResetMs ?? 0) : retryAfterSeconds;
6
13
  return new RateLimitExceededException({
7
14
  $metadata: {},
8
15
  message: message || defaultMessage,
9
16
  rateLimitRemaining: rateLimit?.remaining || 0,
10
- retryAfter: rateLimit?.reset ? rateLimit.reset - Date.now() : retryAfterMs,
17
+ retryAfter: retryAfterSeconds,
11
18
  rateLimit: rateLimit?.limit?.toString() || 'unknown',
12
- rateLimitReset: rateLimit?.reset || Date.now() + retryAfterMs,
19
+ rateLimitReset: rateLimitResetSeconds,
13
20
  });
14
21
  }
@@ -5,7 +5,8 @@ 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 baseDelayMs = hasServerRetryAfter ? error.retryAfter + RETRY_AFTER_BUFFER_MS : DEFAULT_RETRY_AFTER_MS;
8
+ const retryAfterMsFromError = hasServerRetryAfter ? Math.round(error.retryAfter * 1000) : 0;
9
+ const baseDelayMs = hasServerRetryAfter ? retryAfterMsFromError + RETRY_AFTER_BUFFER_MS : DEFAULT_RETRY_AFTER_MS;
9
10
  if (baseDelayMs > 0) {
10
11
  const jitterMs = Math.floor(Math.random() * DEFAULT_JITTER_MS);
11
12
  const computedDelayMs = baseDelayMs + jitterMs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-conversations-utils",
3
- "version": "1.1.68",
3
+ "version": "1.1.70",
4
4
  "description": "",
5
5
  "main": "./dist-cjs/index.js",
6
6
  "module": "./dist-es/index.js",