@wildix/xbees-conversations-utils 1.1.62 → 1.1.64

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.
@@ -4,7 +4,7 @@ exports.RedisCooldown = void 0;
4
4
  const constants_1 = require("../constants");
5
5
  class RedisCooldown {
6
6
  static async getRemainingMs(cooldownKey, context) {
7
- const { redis } = context;
7
+ const { redis, logger } = context;
8
8
  try {
9
9
  const ttlMs = await redis.pttl(`${constants_1.STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`);
10
10
  if (ttlMs > 0) {
@@ -12,7 +12,7 @@ class RedisCooldown {
12
12
  }
13
13
  }
14
14
  catch (error) {
15
- console.warn('Failed to read stream rate-limit cooldown from Redis', {
15
+ logger.warn('Failed to read stream rate-limit cooldown from Redis', {
16
16
  cooldownKey,
17
17
  error,
18
18
  });
@@ -23,7 +23,7 @@ class RedisCooldown {
23
23
  if (cooldownMs <= 0) {
24
24
  return;
25
25
  }
26
- const { redis } = context;
26
+ const { redis, logger } = context;
27
27
  const redisKey = `${constants_1.STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`;
28
28
  try {
29
29
  await redis.eval(`
@@ -45,7 +45,7 @@ class RedisCooldown {
45
45
  `, 1, redisKey, cooldownMs.toString());
46
46
  }
47
47
  catch (error) {
48
- console.warn('Failed to write stream rate-limit cooldown to Redis', {
48
+ logger.warn('Failed to write stream rate-limit cooldown to Redis', {
49
49
  cooldownKey,
50
50
  cooldownMs,
51
51
  error,
@@ -20,13 +20,13 @@ function calculateRateLimitDelayMs(error, maxDelayMs) {
20
20
  return Math.min(maxDelayMs, constants_1.DEFAULT_FIXED_RATE_LIMIT_DELAY_MS);
21
21
  }
22
22
  async function executeWithRateLimitHandling(execute, operation, context, options) {
23
- const { redis } = context;
23
+ const { logger, redis } = context;
24
24
  const { enableCooldown, maxAttempts, maxDelayMs, maxRetryableDelayMs } = options;
25
25
  let attempt = 1;
26
26
  if (enableCooldown) {
27
- const cooldownMs = await RedisCooldown_1.RedisCooldown.getRemainingMs(operation, { redis });
27
+ const cooldownMs = await RedisCooldown_1.RedisCooldown.getRemainingMs(operation, { redis, logger });
28
28
  if (cooldownMs > 0) {
29
- console.warn('Skipping stream request due to active rate-limit cooldown', {
29
+ logger.warn('Skipping stream request due to active rate-limit cooldown', {
30
30
  operation,
31
31
  cooldown: cooldownMs,
32
32
  });
@@ -38,16 +38,16 @@ async function executeWithRateLimitHandling(execute, operation, context, options
38
38
  return await execute();
39
39
  }
40
40
  catch (error) {
41
- const rateLimitError = (0, processStreamRateLimitException_1.processStreamRateLimitException)(error);
41
+ const rateLimitError = (0, processStreamRateLimitException_1.processStreamRateLimitException)(error, logger);
42
42
  if (!rateLimitError) {
43
43
  throw error;
44
44
  }
45
45
  const retryAfterMs = calculateRateLimitDelayMs(rateLimitError, maxDelayMs);
46
46
  if (enableCooldown) {
47
- await RedisCooldown_1.RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis });
47
+ await RedisCooldown_1.RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis, logger });
48
48
  }
49
49
  if (attempt < maxAttempts && retryAfterMs <= maxRetryableDelayMs) {
50
- console.warn('Retrying stream request after rate limit', {
50
+ logger.warn('Retrying stream request after rate limit', {
51
51
  operation,
52
52
  attempt,
53
53
  maxAttempts,
@@ -60,7 +60,7 @@ async function executeWithRateLimitHandling(execute, operation, context, options
60
60
  attempt++;
61
61
  continue;
62
62
  }
63
- console.warn('Propagating stream rate limit w/o retry', {
63
+ logger.warn('Propagating stream rate limit w/o retry', {
64
64
  operation,
65
65
  attempt,
66
66
  maxAttempts,
@@ -53,13 +53,13 @@ function isStreamHttpRateLimitError(error) {
53
53
  error.response !== null &&
54
54
  error.response?.status === 429)));
55
55
  }
56
- function processStreamRateLimitException(error) {
56
+ function processStreamRateLimitException(error, logger) {
57
57
  if (isStreamHttpRateLimitError(error)) {
58
58
  const headers = error.response?.headers ?? {};
59
59
  const rateLimit = getRateLimitFromHeaders(headers);
60
60
  const retryAfterMs = parseRetryAfterMs(headers);
61
61
  const message = error.response?.data?.message || error.message || 'Rate limit exceeded';
62
- console.warn('Stream rate limit exceeded [HTTP 429]', {
62
+ logger.warn('Stream rate limit exceeded [HTTP 429]', {
63
63
  status: error.status ?? error.response?.status,
64
64
  retryAfterMs,
65
65
  rateLimit,
@@ -1,7 +1,7 @@
1
1
  import { STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX } from '../constants';
2
2
  export class RedisCooldown {
3
3
  static async getRemainingMs(cooldownKey, context) {
4
- const { redis } = context;
4
+ const { redis, logger } = context;
5
5
  try {
6
6
  const ttlMs = await redis.pttl(`${STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`);
7
7
  if (ttlMs > 0) {
@@ -9,7 +9,7 @@ export class RedisCooldown {
9
9
  }
10
10
  }
11
11
  catch (error) {
12
- console.warn('Failed to read stream rate-limit cooldown from Redis', {
12
+ logger.warn('Failed to read stream rate-limit cooldown from Redis', {
13
13
  cooldownKey,
14
14
  error,
15
15
  });
@@ -20,7 +20,7 @@ export class RedisCooldown {
20
20
  if (cooldownMs <= 0) {
21
21
  return;
22
22
  }
23
- const { redis } = context;
23
+ const { redis, logger } = context;
24
24
  const redisKey = `${STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX}${cooldownKey}`;
25
25
  try {
26
26
  await redis.eval(`
@@ -42,7 +42,7 @@ export class RedisCooldown {
42
42
  `, 1, redisKey, cooldownMs.toString());
43
43
  }
44
44
  catch (error) {
45
- console.warn('Failed to write stream rate-limit cooldown to Redis', {
45
+ logger.warn('Failed to write stream rate-limit cooldown to Redis', {
46
46
  cooldownKey,
47
47
  cooldownMs,
48
48
  error,
@@ -17,13 +17,13 @@ function calculateRateLimitDelayMs(error, maxDelayMs) {
17
17
  return Math.min(maxDelayMs, DEFAULT_FIXED_RATE_LIMIT_DELAY_MS);
18
18
  }
19
19
  export async function executeWithRateLimitHandling(execute, operation, context, options) {
20
- const { redis } = context;
20
+ const { logger, redis } = context;
21
21
  const { enableCooldown, maxAttempts, maxDelayMs, maxRetryableDelayMs } = options;
22
22
  let attempt = 1;
23
23
  if (enableCooldown) {
24
- const cooldownMs = await RedisCooldown.getRemainingMs(operation, { redis });
24
+ const cooldownMs = await RedisCooldown.getRemainingMs(operation, { redis, logger });
25
25
  if (cooldownMs > 0) {
26
- console.warn('Skipping stream request due to active rate-limit cooldown', {
26
+ logger.warn('Skipping stream request due to active rate-limit cooldown', {
27
27
  operation,
28
28
  cooldown: cooldownMs,
29
29
  });
@@ -35,16 +35,16 @@ export async function executeWithRateLimitHandling(execute, operation, context,
35
35
  return await execute();
36
36
  }
37
37
  catch (error) {
38
- const rateLimitError = processStreamRateLimitException(error);
38
+ const rateLimitError = processStreamRateLimitException(error, logger);
39
39
  if (!rateLimitError) {
40
40
  throw error;
41
41
  }
42
42
  const retryAfterMs = calculateRateLimitDelayMs(rateLimitError, maxDelayMs);
43
43
  if (enableCooldown) {
44
- await RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis });
44
+ await RedisCooldown.setRemainingMs(operation, retryAfterMs, { redis, logger });
45
45
  }
46
46
  if (attempt < maxAttempts && retryAfterMs <= maxRetryableDelayMs) {
47
- console.warn('Retrying stream request after rate limit', {
47
+ logger.warn('Retrying stream request after rate limit', {
48
48
  operation,
49
49
  attempt,
50
50
  maxAttempts,
@@ -57,7 +57,7 @@ export async function executeWithRateLimitHandling(execute, operation, context,
57
57
  attempt++;
58
58
  continue;
59
59
  }
60
- console.warn('Propagating stream rate limit w/o retry', {
60
+ logger.warn('Propagating stream rate limit w/o retry', {
61
61
  operation,
62
62
  attempt,
63
63
  maxAttempts,
@@ -50,13 +50,13 @@ function isStreamHttpRateLimitError(error) {
50
50
  error.response !== null &&
51
51
  error.response?.status === 429)));
52
52
  }
53
- export function processStreamRateLimitException(error) {
53
+ export function processStreamRateLimitException(error, logger) {
54
54
  if (isStreamHttpRateLimitError(error)) {
55
55
  const headers = error.response?.headers ?? {};
56
56
  const rateLimit = getRateLimitFromHeaders(headers);
57
57
  const retryAfterMs = parseRetryAfterMs(headers);
58
58
  const message = error.response?.data?.message || error.message || 'Rate limit exceeded';
59
- console.warn('Stream rate limit exceeded [HTTP 429]', {
59
+ logger.warn('Stream rate limit exceeded [HTTP 429]', {
60
60
  status: error.status ?? error.response?.status,
61
61
  retryAfterMs,
62
62
  rateLimit,
@@ -1,6 +1,9 @@
1
1
  import { StreamChat } from 'stream-chat';
2
2
  import { StreamType } from '../stream';
3
- import { LoggerContext, RateLimited, RedisContext } from './types';
3
+ import { LoggerContext, MethodWithRateLimitOptions, RedisContext } from './types';
4
+ export type RateLimited<T extends object> = {
5
+ [Key in keyof T]: MethodWithRateLimitOptions<T[Key]>;
6
+ };
4
7
  /**
5
8
  * Creates a proxy around a Stream client that transparently applies rate-limit handling
6
9
  * to method calls and recursively wraps returned channel objects with the same behavior.
@@ -1,5 +1,5 @@
1
- import { RedisContext } from '../types';
1
+ import { LoggerContext, RedisContext } from '../types';
2
2
  export declare class RedisCooldown {
3
- static getRemainingMs(cooldownKey: string, context: RedisContext): Promise<number>;
4
- static setRemainingMs(cooldownKey: string, cooldownMs: number, context: RedisContext): Promise<void>;
3
+ static getRemainingMs(cooldownKey: string, context: RedisContext & LoggerContext): Promise<number>;
4
+ static setRemainingMs(cooldownKey: string, cooldownMs: number, context: RedisContext & LoggerContext): Promise<void>;
5
5
  }
@@ -1,2 +1,3 @@
1
1
  import { RateLimitExceededException } from '@wildix/xbees-conversations-client';
2
- export declare function processStreamRateLimitException(error: unknown): RateLimitExceededException | undefined;
2
+ import { Logger } from '@aws-lambda-powertools/logger';
3
+ export declare function processStreamRateLimitException(error: unknown, logger: Logger): RateLimitExceededException | undefined;
@@ -14,9 +14,6 @@ export type StreamRateLimitOptionsToken = {
14
14
  [STREAM_RATE_LIMIT_OPTIONS_MARKER]: Partial<StreamRateLimitRetryOptions>;
15
15
  };
16
16
  export type MethodWithRateLimitOptions<T> = T extends (...args: infer Args) => infer Result ? T & ((...args: [...Args, StreamRateLimitOptionsToken?]) => Result) : T;
17
- export type RateLimited<T extends object> = {
18
- [Key in keyof T]: MethodWithRateLimitOptions<T[Key]>;
19
- };
20
17
  export type RedisContext = {
21
18
  redis: Redis;
22
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildix/xbees-conversations-utils",
3
- "version": "1.1.62",
3
+ "version": "1.1.64",
4
4
  "description": "",
5
5
  "main": "./dist-cjs/index.js",
6
6
  "module": "./dist-es/index.js",