@wildix/xbees-conversations-utils 1.1.76 → 1.1.78
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/constants.js +17 -62
- package/dist-cjs/rateLimits/createRateLimitedStreamProxy.js +20 -7
- package/dist-cjs/rateLimits/generatedMethodNames.js +320 -0
- package/dist-cjs/rateLimits/helpers/RedisCooldown.js +16 -0
- package/dist-cjs/rateLimits/helpers/createRateLimitExceededException.js +5 -7
- package/dist-cjs/rateLimits/helpers/executeWithRateLimitHandling.js +21 -35
- package/dist-cjs/rateLimits/helpers/headerUtils.js +40 -0
- package/dist-cjs/rateLimits/helpers/isStreamChannelLike.js +9 -1
- package/dist-cjs/rateLimits/helpers/processStreamBudgetHeaders.js +41 -35
- package/dist-cjs/rateLimits/helpers/processStreamRateLimitException.js +8 -18
- package/dist-cjs/rateLimits/helpers/processStreamRateLimitHeaders.js +4 -28
- package/dist-cjs/rateLimits/helpers/registerStreamInterceptors.js +48 -0
- package/dist-es/rateLimits/constants.js +9 -61
- package/dist-es/rateLimits/createRateLimitedStreamProxy.js +21 -8
- package/dist-es/rateLimits/generatedMethodNames.js +317 -0
- package/dist-es/rateLimits/helpers/RedisCooldown.js +16 -0
- package/dist-es/rateLimits/helpers/createRateLimitExceededException.js +5 -7
- package/dist-es/rateLimits/helpers/executeWithRateLimitHandling.js +22 -36
- package/dist-es/rateLimits/helpers/headerUtils.js +34 -0
- package/dist-es/rateLimits/helpers/isStreamChannelLike.js +9 -1
- package/dist-es/rateLimits/helpers/processStreamBudgetHeaders.js +42 -36
- package/dist-es/rateLimits/helpers/processStreamRateLimitException.js +6 -16
- package/dist-es/rateLimits/helpers/processStreamRateLimitHeaders.js +4 -28
- package/dist-es/rateLimits/helpers/registerStreamInterceptors.js +44 -0
- package/dist-types/rateLimits/constants.d.ts +8 -4
- package/dist-types/rateLimits/generatedMethodNames.d.ts +11 -0
- package/dist-types/rateLimits/helpers/RedisCooldown.d.ts +1 -0
- package/dist-types/rateLimits/helpers/headerUtils.d.ts +4 -0
- package/dist-types/rateLimits/helpers/processStreamBudgetHeaders.d.ts +2 -1
- package/dist-types/rateLimits/helpers/registerStreamInterceptors.d.ts +2 -0
- package/package.json +2 -1
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isStreamChannelLike = void 0;
|
|
4
|
+
const stream_chat_1 = require("stream-chat");
|
|
4
5
|
function hasFunctionProperty(value, key) {
|
|
5
6
|
return key in value && typeof value[key] === 'function';
|
|
6
7
|
}
|
|
8
|
+
function hasStringProperty(value, key) {
|
|
9
|
+
return key in value && typeof value[key] === 'string';
|
|
10
|
+
}
|
|
7
11
|
function isStreamChannelLike(value) {
|
|
8
12
|
if (typeof value !== 'object' || value === null) {
|
|
9
13
|
return false;
|
|
10
14
|
}
|
|
11
|
-
|
|
15
|
+
if (value instanceof stream_chat_1.Channel) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
return (hasStringProperty(value, 'cid') &&
|
|
19
|
+
hasFunctionProperty(value, 'sendMessage') &&
|
|
12
20
|
hasFunctionProperty(value, 'queryMembers') &&
|
|
13
21
|
hasFunctionProperty(value, 'updatePartial'));
|
|
14
22
|
}
|
|
@@ -2,38 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.processStreamBudgetHeaders = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
|
+
const headerUtils_1 = require("./headerUtils");
|
|
5
6
|
const RedisCooldown_1 = require("./RedisCooldown");
|
|
6
|
-
function parseHeaderNumber(value) {
|
|
7
|
-
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
8
|
-
return value;
|
|
9
|
-
}
|
|
10
|
-
if (typeof value === 'string') {
|
|
11
|
-
const parsed = Number(value);
|
|
12
|
-
if (Number.isFinite(parsed)) {
|
|
13
|
-
return parsed;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function getHeaderCaseInsensitive(headers, key) {
|
|
18
|
-
if (!headers) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const direct = headers[key];
|
|
22
|
-
if (direct !== undefined) {
|
|
23
|
-
return direct;
|
|
24
|
-
}
|
|
25
|
-
const normalizedKey = key.toLowerCase();
|
|
26
|
-
for (const [headerKey, headerValue] of Object.entries(headers)) {
|
|
27
|
-
if (headerKey.toLowerCase() === normalizedKey) {
|
|
28
|
-
return headerValue;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
7
|
function extractBudgetHeaders(headers) {
|
|
33
8
|
return {
|
|
34
|
-
limitMs: parseHeaderNumber(
|
|
35
|
-
remainingMs: parseHeaderNumber(
|
|
36
|
-
usedMs: parseHeaderNumber(
|
|
9
|
+
limitMs: (0, headerUtils_1.parseHeaderNumber)(headers ?? {}, 'x-budget-limit-ms'),
|
|
10
|
+
remainingMs: (0, headerUtils_1.parseHeaderNumber)(headers ?? {}, 'x-budget-remaining-ms'),
|
|
11
|
+
usedMs: (0, headerUtils_1.parseHeaderNumber)(headers ?? {}, 'x-budget-used-ms'),
|
|
37
12
|
};
|
|
38
13
|
}
|
|
39
14
|
function isBudgetThresholdReached(limitMs, remainingMs, usedMs) {
|
|
@@ -49,8 +24,20 @@ function isBudgetThresholdReached(limitMs, remainingMs, usedMs) {
|
|
|
49
24
|
}
|
|
50
25
|
return false;
|
|
51
26
|
}
|
|
52
|
-
function
|
|
53
|
-
return Math.round(
|
|
27
|
+
function getRandomDelayInRange(minMs, maxMs) {
|
|
28
|
+
return Math.round(minMs + Math.random() * (maxMs - minMs));
|
|
29
|
+
}
|
|
30
|
+
function getBudgetUsage(limitMs, remainingMs, usedMs) {
|
|
31
|
+
if (limitMs <= 0) {
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
if (typeof usedMs === 'number' && Number.isFinite(usedMs) && usedMs >= 0) {
|
|
35
|
+
return usedMs / limitMs;
|
|
36
|
+
}
|
|
37
|
+
if (typeof remainingMs === 'number' && Number.isFinite(remainingMs)) {
|
|
38
|
+
return Math.max(0, 1 - remainingMs / limitMs);
|
|
39
|
+
}
|
|
40
|
+
return 0;
|
|
54
41
|
}
|
|
55
42
|
async function processStreamBudgetHeaders(headers, context) {
|
|
56
43
|
const { logger } = context;
|
|
@@ -61,27 +48,46 @@ async function processStreamBudgetHeaders(headers, context) {
|
|
|
61
48
|
if (typeof limitMs !== 'number' || !Number.isFinite(limitMs)) {
|
|
62
49
|
return;
|
|
63
50
|
}
|
|
51
|
+
const usage = getBudgetUsage(limitMs, remainingMs, usedMs);
|
|
64
52
|
const thresholdReached = isBudgetThresholdReached(limitMs, remainingMs, usedMs);
|
|
65
53
|
if (thresholdReached) {
|
|
66
|
-
const delayMs =
|
|
67
|
-
await RedisCooldown_1.RedisCooldown.
|
|
54
|
+
const delayMs = getRandomDelayInRange(constants_1.BUDGET_DELAY_MIN_MS, constants_1.BUDGET_DELAY_MAX_MS);
|
|
55
|
+
await RedisCooldown_1.RedisCooldown.setRemainingMsExact(constants_1.STREAM_BUDGET_COOLDOWN_KEY, delayMs, context, {
|
|
68
56
|
keyPrefix: constants_1.STREAM_BUDGET_COOLDOWN_KEY_PREFIX,
|
|
69
57
|
});
|
|
70
58
|
logger.warn('Stream budget threshold reached, enabling cooldown', {
|
|
71
59
|
budgetLimitMs: limitMs,
|
|
72
60
|
budgetRemainingMs: remainingMs,
|
|
73
61
|
budgetUsedMs: usedMs,
|
|
62
|
+
budgetUsage: usage,
|
|
74
63
|
cooldownMs: delayMs,
|
|
75
64
|
});
|
|
76
65
|
return;
|
|
77
66
|
}
|
|
78
|
-
|
|
67
|
+
if (usage < constants_1.BUDGET_CLEAR_USAGE_THRESHOLD) {
|
|
68
|
+
await RedisCooldown_1.RedisCooldown.clear(constants_1.STREAM_BUDGET_COOLDOWN_KEY, context, {
|
|
69
|
+
keyPrefix: constants_1.STREAM_BUDGET_COOLDOWN_KEY_PREFIX,
|
|
70
|
+
});
|
|
71
|
+
logger.info('Stream budget is low, clearing cooldown', {
|
|
72
|
+
budgetLimitMs: limitMs,
|
|
73
|
+
budgetRemainingMs: remainingMs,
|
|
74
|
+
budgetUsedMs: usedMs,
|
|
75
|
+
budgetUsage: usage,
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const delayMs = usage >= constants_1.BUDGET_RELAXED_USAGE_THRESHOLD
|
|
80
|
+
? getRandomDelayInRange(constants_1.BUDGET_RELAXED_DELAY_MIN_MS, constants_1.BUDGET_RELAXED_DELAY_MAX_MS)
|
|
81
|
+
: getRandomDelayInRange(constants_1.BUDGET_MINIMAL_DELAY_MIN_MS, constants_1.BUDGET_MINIMAL_DELAY_MAX_MS);
|
|
82
|
+
await RedisCooldown_1.RedisCooldown.setRemainingMsExact(constants_1.STREAM_BUDGET_COOLDOWN_KEY, delayMs, context, {
|
|
79
83
|
keyPrefix: constants_1.STREAM_BUDGET_COOLDOWN_KEY_PREFIX,
|
|
80
84
|
});
|
|
81
|
-
logger.
|
|
85
|
+
logger.info('Stream budget is below threshold, reducing cooldown', {
|
|
82
86
|
budgetLimitMs: limitMs,
|
|
83
87
|
budgetRemainingMs: remainingMs,
|
|
84
88
|
budgetUsedMs: usedMs,
|
|
89
|
+
budgetUsage: usage,
|
|
90
|
+
cooldownMs: delayMs,
|
|
85
91
|
});
|
|
86
92
|
}
|
|
87
93
|
exports.processStreamBudgetHeaders = processStreamBudgetHeaders;
|
|
@@ -2,20 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.processStreamRateLimitException = void 0;
|
|
4
4
|
const createRateLimitExceededException_1 = require("./createRateLimitExceededException");
|
|
5
|
-
|
|
6
|
-
const raw = headers[key];
|
|
7
|
-
if (typeof raw === 'number' && Number.isFinite(raw)) {
|
|
8
|
-
return raw;
|
|
9
|
-
}
|
|
10
|
-
if (typeof raw === 'string') {
|
|
11
|
-
const parsed = Number(raw);
|
|
12
|
-
if (Number.isFinite(parsed)) {
|
|
13
|
-
return parsed;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
5
|
+
const headerUtils_1 = require("./headerUtils");
|
|
17
6
|
function parseRetryAfterMs(headers) {
|
|
18
|
-
const retryAfter = headers
|
|
7
|
+
const retryAfter = (0, headerUtils_1.getHeaderCaseInsensitive)(headers, 'retry-after');
|
|
19
8
|
if (typeof retryAfter === 'number' && Number.isFinite(retryAfter) && retryAfter >= 0) {
|
|
20
9
|
return Math.round(retryAfter * 1000);
|
|
21
10
|
}
|
|
@@ -32,16 +21,16 @@ function parseRetryAfterMs(headers) {
|
|
|
32
21
|
}
|
|
33
22
|
}
|
|
34
23
|
function getRateLimitFromHeaders(headers) {
|
|
35
|
-
const limit = parseHeaderNumber(headers, 'x-ratelimit-limit');
|
|
36
|
-
const remaining = parseHeaderNumber(headers, 'x-ratelimit-remaining');
|
|
37
|
-
const
|
|
38
|
-
if (limit === undefined || remaining === undefined ||
|
|
24
|
+
const limit = (0, headerUtils_1.parseHeaderNumber)(headers, 'x-ratelimit-limit');
|
|
25
|
+
const remaining = (0, headerUtils_1.parseHeaderNumber)(headers, 'x-ratelimit-remaining');
|
|
26
|
+
const resetRaw = (0, headerUtils_1.parseHeaderNumber)(headers, 'x-ratelimit-reset');
|
|
27
|
+
if (limit === undefined || remaining === undefined || resetRaw === undefined) {
|
|
39
28
|
return;
|
|
40
29
|
}
|
|
41
30
|
return {
|
|
42
31
|
limit,
|
|
43
32
|
remaining,
|
|
44
|
-
reset:
|
|
33
|
+
reset: (0, headerUtils_1.normalizeResetToEpochMs)(resetRaw),
|
|
45
34
|
};
|
|
46
35
|
}
|
|
47
36
|
function isStreamHttpRateLimitError(error) {
|
|
@@ -56,6 +45,7 @@ function isStreamHttpRateLimitError(error) {
|
|
|
56
45
|
function processStreamRateLimitException(error, logger) {
|
|
57
46
|
if (isStreamHttpRateLimitError(error)) {
|
|
58
47
|
const headers = error.response?.headers ?? {};
|
|
48
|
+
logger.warn(`rate limit headers`, { headers });
|
|
59
49
|
const rateLimit = getRateLimitFromHeaders(headers);
|
|
60
50
|
const retryAfterMs = parseRetryAfterMs(headers);
|
|
61
51
|
const message = error.response?.data?.message || error.message || 'Rate limit exceeded';
|
|
@@ -2,33 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.processStreamRateLimitHeaders = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
|
+
const headerUtils_1 = require("./headerUtils");
|
|
5
6
|
const RedisCooldown_1 = require("./RedisCooldown");
|
|
6
|
-
function parseHeaderNumber(value) {
|
|
7
|
-
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
8
|
-
return value;
|
|
9
|
-
}
|
|
10
|
-
if (typeof value === 'string') {
|
|
11
|
-
const parsed = Number(value);
|
|
12
|
-
if (Number.isFinite(parsed)) {
|
|
13
|
-
return parsed;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function getHeaderCaseInsensitive(headers, key) {
|
|
18
|
-
if (!headers) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
const direct = headers[key];
|
|
22
|
-
if (direct !== undefined) {
|
|
23
|
-
return direct;
|
|
24
|
-
}
|
|
25
|
-
const normalizedKey = key.toLowerCase();
|
|
26
|
-
for (const [headerKey, headerValue] of Object.entries(headers)) {
|
|
27
|
-
if (headerKey.toLowerCase() === normalizedKey) {
|
|
28
|
-
return headerValue;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
7
|
function computeSoftThrottleDelayMs(limit, remaining) {
|
|
33
8
|
if (limit <= 0) {
|
|
34
9
|
return 0;
|
|
@@ -47,8 +22,8 @@ function computeSoftThrottleDelayMs(limit, remaining) {
|
|
|
47
22
|
}
|
|
48
23
|
async function processStreamRateLimitHeaders(headers, context) {
|
|
49
24
|
const { logger } = context;
|
|
50
|
-
const limit = parseHeaderNumber(
|
|
51
|
-
const remaining = parseHeaderNumber(
|
|
25
|
+
const limit = (0, headerUtils_1.parseHeaderNumber)(headers ?? {}, 'x-ratelimit-limit');
|
|
26
|
+
const remaining = (0, headerUtils_1.parseHeaderNumber)(headers ?? {}, 'x-ratelimit-remaining');
|
|
52
27
|
if (limit === undefined || remaining === undefined) {
|
|
53
28
|
return;
|
|
54
29
|
}
|
|
@@ -70,5 +45,6 @@ async function processStreamRateLimitHeaders(headers, context) {
|
|
|
70
45
|
await RedisCooldown_1.RedisCooldown.clear(constants_1.STREAM_RATE_LIMIT_SOFT_COOLDOWN_KEY, context, {
|
|
71
46
|
keyPrefix: constants_1.STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX,
|
|
72
47
|
});
|
|
48
|
+
logger.debug('Clearing soft stream rate-limit cooldown');
|
|
73
49
|
}
|
|
74
50
|
exports.processStreamRateLimitHeaders = processStreamRateLimitHeaders;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerStreamInterceptors = void 0;
|
|
4
|
+
const processStreamBudgetHeaders_1 = require("./processStreamBudgetHeaders");
|
|
5
|
+
const processStreamRateLimitHeaders_1 = require("./processStreamRateLimitHeaders");
|
|
6
|
+
const attachedInterceptors = new WeakSet();
|
|
7
|
+
function hasResponseInterceptors(value) {
|
|
8
|
+
return (typeof value === 'object' &&
|
|
9
|
+
value !== null &&
|
|
10
|
+
typeof value.interceptors?.response?.use === 'function');
|
|
11
|
+
}
|
|
12
|
+
function resolveAxiosInstance(stream) {
|
|
13
|
+
if (hasResponseInterceptors(stream)) {
|
|
14
|
+
return stream;
|
|
15
|
+
}
|
|
16
|
+
if (typeof stream !== 'object' || stream === null) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const maybeStream = stream;
|
|
20
|
+
if (hasResponseInterceptors(maybeStream.axiosInstance)) {
|
|
21
|
+
return maybeStream.axiosInstance;
|
|
22
|
+
}
|
|
23
|
+
if (hasResponseInterceptors(maybeStream._client?.axiosInstance)) {
|
|
24
|
+
return maybeStream._client?.axiosInstance;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function registerStreamInterceptors(stream, context) {
|
|
28
|
+
const { logger } = context;
|
|
29
|
+
const axiosInstance = resolveAxiosInstance(stream);
|
|
30
|
+
if (!axiosInstance || !axiosInstance.interceptors?.response?.use) {
|
|
31
|
+
logger.warn('Unable to register stream budget & rate limit interceptors: axios instance not found');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (attachedInterceptors.has(axiosInstance)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
axiosInstance.interceptors.response.use(async (response) => {
|
|
38
|
+
await (0, processStreamBudgetHeaders_1.processStreamBudgetHeaders)(response?.headers, context);
|
|
39
|
+
await (0, processStreamRateLimitHeaders_1.processStreamRateLimitHeaders)(response?.headers, context);
|
|
40
|
+
return response;
|
|
41
|
+
}, async (error) => {
|
|
42
|
+
await (0, processStreamBudgetHeaders_1.processStreamBudgetHeaders)(error?.response?.headers, context);
|
|
43
|
+
await (0, processStreamRateLimitHeaders_1.processStreamRateLimitHeaders)(error?.response?.headers, context);
|
|
44
|
+
return Promise.reject(error);
|
|
45
|
+
});
|
|
46
|
+
attachedInterceptors.add(axiosInstance);
|
|
47
|
+
}
|
|
48
|
+
exports.registerStreamInterceptors = registerStreamInterceptors;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { CHANNEL_METHOD_NAMES, CHANNEL_SYNC_METHOD_NAMES, CHANNEL_SYNC_PROXY_RESULT_METHOD_NAMES, STREAM_CHANNEL_RESULT_METHOD_NAMES, STREAM_METHOD_NAMES, STREAM_SYNC_CHANNEL_RESULT_METHOD_NAMES, STREAM_SYNC_METHOD_NAMES, } from './generatedMethodNames';
|
|
1
2
|
export const DEFAULT_MAX_DELAY_MS = 5000;
|
|
2
3
|
export const DEFAULT_RETRY_AFTER_MS = 10000;
|
|
3
4
|
export const DEFAULT_JITTER_MS = 150;
|
|
@@ -10,75 +11,22 @@ export const STREAM_BUDGET_COOLDOWN_KEY = 'stream-budget-global';
|
|
|
10
11
|
export const BUDGET_THRESHOLD = 0.8;
|
|
11
12
|
export const BUDGET_DELAY_MIN_MS = 30000;
|
|
12
13
|
export const BUDGET_DELAY_MAX_MS = 60000;
|
|
14
|
+
export const BUDGET_CLEAR_USAGE_THRESHOLD = 0.6;
|
|
15
|
+
export const BUDGET_RELAXED_USAGE_THRESHOLD = 0.7;
|
|
16
|
+
export const BUDGET_RELAXED_DELAY_MIN_MS = 5000;
|
|
17
|
+
export const BUDGET_RELAXED_DELAY_MAX_MS = 10000;
|
|
18
|
+
export const BUDGET_MINIMAL_DELAY_MIN_MS = 1000;
|
|
19
|
+
export const BUDGET_MINIMAL_DELAY_MAX_MS = 2000;
|
|
20
|
+
export const BUDGET_COOLDOWN_SLEEP_CHUNK_MS = 3000;
|
|
13
21
|
export const SOFT_THROTTLE_USAGE_LOW = 0.7;
|
|
14
22
|
export const SOFT_THROTTLE_USAGE_MEDIUM = 0.85;
|
|
15
23
|
export const SOFT_THROTTLE_USAGE_HIGH = 0.95;
|
|
16
24
|
export const SOFT_THROTTLE_DELAY_LOW_MS = 500;
|
|
17
25
|
export const SOFT_THROTTLE_DELAY_MEDIUM_MS = 1500;
|
|
18
26
|
export const SOFT_THROTTLE_DELAY_HIGH_MS = 3000;
|
|
19
|
-
export const STREAM_SYNC_CHANNEL_RESULT_METHOD_NAMES = [
|
|
20
|
-
'channel',
|
|
21
|
-
'getChannelById',
|
|
22
|
-
'getChannelByMembers',
|
|
23
|
-
'hydrateActiveChannels',
|
|
24
|
-
];
|
|
25
|
-
export const STREAM_SYNC_METHOD_NAMES = [
|
|
26
|
-
'_addChannelConfig',
|
|
27
|
-
'_buildWSPayload',
|
|
28
|
-
'_callClientListeners',
|
|
29
|
-
'_deleteUserMessageReference',
|
|
30
|
-
'_enrichAxiosOptions',
|
|
31
|
-
'_getConnectionID',
|
|
32
|
-
'_getToken',
|
|
33
|
-
'_handleClientEvent',
|
|
34
|
-
'_handleUserEvent',
|
|
35
|
-
'_hasConnectionID',
|
|
36
|
-
'_isUsingServerAuth',
|
|
37
|
-
'_logApiError',
|
|
38
|
-
'_logApiRequest',
|
|
39
|
-
'_logApiResponse',
|
|
40
|
-
'_muteStatus',
|
|
41
|
-
'_normalizeDate',
|
|
42
|
-
'_normalizeExpiration',
|
|
43
|
-
'_sayHi',
|
|
44
|
-
'_setUser',
|
|
45
|
-
'_startCleaning',
|
|
46
|
-
'_updateMemberWatcherReferences',
|
|
47
|
-
'_updateUserMessageReferences',
|
|
48
|
-
'_updateUserReferences',
|
|
49
|
-
'_validateAndGetMessageId',
|
|
50
|
-
'createAbortControllerForNextRequest',
|
|
51
|
-
'createToken',
|
|
52
|
-
'devToken',
|
|
53
|
-
'dispatchEvent',
|
|
54
|
-
'errorFromResponse',
|
|
55
|
-
'getAuthType',
|
|
56
|
-
'getUserAgent',
|
|
57
|
-
'handleEvent',
|
|
58
|
-
'handleResponse',
|
|
59
|
-
'off',
|
|
60
|
-
'on',
|
|
61
|
-
'setBaseURL',
|
|
62
|
-
'setLocalDevice',
|
|
63
|
-
'setUserAgent',
|
|
64
|
-
'userMuteStatus',
|
|
65
|
-
'verifyWebhook',
|
|
66
|
-
];
|
|
67
|
-
export const CHANNEL_SYNC_METHOD_NAMES = [
|
|
68
|
-
'_channelURL',
|
|
69
|
-
'countUnread',
|
|
70
|
-
'countUnreadMentions',
|
|
71
|
-
'getConfig',
|
|
72
|
-
'lastMessage',
|
|
73
|
-
'lastRead',
|
|
74
|
-
'muteStatus',
|
|
75
|
-
'off',
|
|
76
|
-
'on',
|
|
77
|
-
];
|
|
78
|
-
export const CHANNEL_SYNC_PROXY_RESULT_METHOD_NAMES = ['getClient'];
|
|
79
27
|
export const DEFAULT_OPTIONS = {
|
|
80
28
|
maxAttempts: 3,
|
|
81
29
|
enableCooldown: true,
|
|
82
|
-
maxDelayMs:
|
|
30
|
+
maxDelayMs: 5000,
|
|
83
31
|
maxRetryableDelayMs: 10000,
|
|
84
32
|
};
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
-
import { CHANNEL_SYNC_METHOD_NAMES, CHANNEL_SYNC_PROXY_RESULT_METHOD_NAMES, DEFAULT_OPTIONS, STREAM_SYNC_CHANNEL_RESULT_METHOD_NAMES, STREAM_SYNC_METHOD_NAMES, } from './constants';
|
|
1
|
+
import { CHANNEL_SYNC_METHOD_NAMES, CHANNEL_SYNC_PROXY_RESULT_METHOD_NAMES, DEFAULT_OPTIONS, STREAM_CHANNEL_RESULT_METHOD_NAMES, STREAM_SYNC_CHANNEL_RESULT_METHOD_NAMES, STREAM_SYNC_METHOD_NAMES, } from './constants';
|
|
2
2
|
import { executeWithRateLimitHandling } from './helpers/executeWithRateLimitHandling';
|
|
3
3
|
import { isStreamChannelLike } from './helpers/isStreamChannelLike';
|
|
4
|
-
import {
|
|
4
|
+
import { registerStreamInterceptors } from './helpers/registerStreamInterceptors';
|
|
5
5
|
import { splitCallArgsAndOptions } from './helpers/splitCallArgsAndOptions';
|
|
6
6
|
const STREAM_SYNC_METHODS = new Set(STREAM_SYNC_METHOD_NAMES);
|
|
7
|
+
const STREAM_CHANNEL_RESULT_METHODS = new Set(STREAM_CHANNEL_RESULT_METHOD_NAMES);
|
|
7
8
|
const STREAM_SYNC_CHANNEL_RESULT_METHODS = new Set(STREAM_SYNC_CHANNEL_RESULT_METHOD_NAMES);
|
|
8
9
|
const CHANNEL_SYNC_METHODS = new Set(CHANNEL_SYNC_METHOD_NAMES);
|
|
9
10
|
const CHANNEL_SYNC_PROXY_RESULT_METHODS = new Set(CHANNEL_SYNC_PROXY_RESULT_METHOD_NAMES);
|
|
10
|
-
function
|
|
11
|
+
function wrapKnownChannelResult(result, dependencies) {
|
|
12
|
+
if (Array.isArray(result)) {
|
|
13
|
+
return result.map((item) => wrapKnownChannelResult(item, dependencies));
|
|
14
|
+
}
|
|
15
|
+
if (typeof result !== 'object' || result === null) {
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
return wrapTarget(result, dependencies);
|
|
19
|
+
}
|
|
20
|
+
function normalizeStreamCallResult(result, dependencies, wrapAsChannelResult = false) {
|
|
21
|
+
if (wrapAsChannelResult) {
|
|
22
|
+
return wrapKnownChannelResult(result, dependencies);
|
|
23
|
+
}
|
|
11
24
|
if (Array.isArray(result)) {
|
|
12
25
|
return result.map((item) => normalizeStreamCallResult(item, dependencies));
|
|
13
26
|
}
|
|
@@ -16,14 +29,14 @@ function normalizeStreamCallResult(result, dependencies) {
|
|
|
16
29
|
}
|
|
17
30
|
return wrapTarget(result, dependencies);
|
|
18
31
|
}
|
|
19
|
-
function createRateLimitedMethodWrapper(target, value, operation, dependencies) {
|
|
32
|
+
function createRateLimitedMethodWrapper(target, value, operation, dependencies, wrapAsChannelResult) {
|
|
20
33
|
return async (...args) => {
|
|
21
34
|
const { callArgs, callOptions } = splitCallArgsAndOptions(args);
|
|
22
35
|
const result = await executeWithRateLimitHandling(() => Reflect.apply(value, target, callArgs), operation, dependencies.context, {
|
|
23
36
|
...DEFAULT_OPTIONS,
|
|
24
37
|
...callOptions,
|
|
25
38
|
});
|
|
26
|
-
return normalizeStreamCallResult(result, dependencies);
|
|
39
|
+
return normalizeStreamCallResult(result, dependencies, wrapAsChannelResult);
|
|
27
40
|
};
|
|
28
41
|
}
|
|
29
42
|
function wrapTarget(target, dependencies) {
|
|
@@ -40,7 +53,7 @@ function wrapTarget(target, dependencies) {
|
|
|
40
53
|
if (STREAM_SYNC_CHANNEL_RESULT_METHODS.has(property)) {
|
|
41
54
|
return (...args) => {
|
|
42
55
|
const result = Reflect.apply(value, originalTarget, args);
|
|
43
|
-
return normalizeStreamCallResult(result, dependencies);
|
|
56
|
+
return normalizeStreamCallResult(result, dependencies, true);
|
|
44
57
|
};
|
|
45
58
|
}
|
|
46
59
|
if (CHANNEL_SYNC_PROXY_RESULT_METHODS.has(property)) {
|
|
@@ -55,14 +68,14 @@ function wrapTarget(target, dependencies) {
|
|
|
55
68
|
if (STREAM_SYNC_METHODS.has(property) || CHANNEL_SYNC_METHODS.has(property)) {
|
|
56
69
|
return (...args) => Reflect.apply(value, originalTarget, args);
|
|
57
70
|
}
|
|
58
|
-
return createRateLimitedMethodWrapper(originalTarget, value, property, dependencies);
|
|
71
|
+
return createRateLimitedMethodWrapper(originalTarget, value, property, dependencies, STREAM_CHANNEL_RESULT_METHODS.has(property));
|
|
59
72
|
},
|
|
60
73
|
});
|
|
61
74
|
dependencies.cache.set(target, proxy);
|
|
62
75
|
return proxy;
|
|
63
76
|
}
|
|
64
77
|
export function createRateLimitedStreamProxy(stream, context) {
|
|
65
|
-
|
|
78
|
+
registerStreamInterceptors(stream, context);
|
|
66
79
|
return wrapTarget(stream, {
|
|
67
80
|
context,
|
|
68
81
|
cache: new WeakMap(),
|