@wildix/xbees-conversations-utils 4.0.2 → 4.1.0
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/anonymousEmail/index.js +3 -3
- package/dist-cjs/normalization.js +6 -0
- package/dist-es/anonymousEmail/index.js +1 -1
- package/dist-es/normalization.js +6 -0
- package/dist-types/anonymousEmail/index.d.ts +0 -1
- package/dist-types/normalization.d.ts +25 -0
- package/dist-types/stream.d.ts +7 -1
- package/package.json +3 -3
- package/dist-cjs/rateLimits/RedisCooldown.js +0 -66
- package/dist-cjs/rateLimits/constants.js +0 -43
- package/dist-cjs/rateLimits/createRateLimitedStreamProxy.js +0 -92
- package/dist-cjs/rateLimits/exception/createRateLimitExceededException.js +0 -21
- package/dist-cjs/rateLimits/exception/processStreamRateLimitException.js +0 -47
- package/dist-cjs/rateLimits/executeStreamRequestWithRateLimitHandling.js +0 -94
- package/dist-cjs/rateLimits/generatedMethodNames.js +0 -320
- package/dist-cjs/rateLimits/helpers/RedisCooldown.js +0 -174
- package/dist-cjs/rateLimits/helpers/createRateLimitExceededException.js +0 -32
- package/dist-cjs/rateLimits/helpers/executeWithRateLimitHandling.js +0 -144
- package/dist-cjs/rateLimits/helpers/headerUtils.js +0 -40
- package/dist-cjs/rateLimits/helpers/isStreamChannelLike.js +0 -23
- package/dist-cjs/rateLimits/helpers/processStreamBudgetHeaders.js +0 -93
- package/dist-cjs/rateLimits/helpers/processStreamRateLimitException.js +0 -63
- package/dist-cjs/rateLimits/helpers/processStreamRateLimitHeaders.js +0 -50
- package/dist-cjs/rateLimits/helpers/rateLimitSnapshot.js +0 -31
- package/dist-cjs/rateLimits/helpers/registerStreamBudgetInterceptors.js +0 -48
- package/dist-cjs/rateLimits/helpers/registerStreamInterceptors.js +0 -58
- package/dist-cjs/rateLimits/helpers/splitCallArgsAndOptions.js +0 -21
- package/dist-cjs/rateLimits/index.js +0 -7
- package/dist-cjs/rateLimits/types.js +0 -3
- package/dist-cjs/rateLimits/withStreamRateLimitOptions.js +0 -10
- package/dist-cjs/streamRateLimitHandlingProxy/constants.js +0 -42
- package/dist-cjs/streamRateLimitHandlingProxy/createRateLimitedStreamProxy.js +0 -92
- package/dist-cjs/streamRateLimitHandlingProxy/generatedMethodNames.js +0 -320
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/RedisCooldown.js +0 -174
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/createRateLimitExceededException.js +0 -29
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/executeWithRateLimitHandling.js +0 -153
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/headerUtils.js +0 -48
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/isStreamChannelLike.js +0 -23
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/processStreamBudgetHeaders.js +0 -93
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/processStreamRateLimitException.js +0 -63
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/processStreamRateLimitHeaders.js +0 -46
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/rateLimitSnapshot.js +0 -31
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/registerStreamInterceptors.js +0 -64
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/sleep.js +0 -6
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/splitCallArgsAndOptions.js +0 -21
- package/dist-cjs/streamRateLimitHandlingProxy/index.js +0 -5
- package/dist-cjs/streamRateLimitHandlingProxy/types.js +0 -3
- package/dist-cjs/streamRateLimitHandlingProxy/withStreamRateLimitOptions.js +0 -10
- package/dist-es/rateLimits/RedisCooldown.js +0 -62
- package/dist-es/rateLimits/constants.js +0 -33
- package/dist-es/rateLimits/createRateLimitedStreamProxy.js +0 -88
- package/dist-es/rateLimits/exception/createRateLimitExceededException.js +0 -17
- package/dist-es/rateLimits/exception/processStreamRateLimitException.js +0 -37
- package/dist-es/rateLimits/executeStreamRequestWithRateLimitHandling.js +0 -93
- package/dist-es/rateLimits/generatedMethodNames.js +0 -317
- package/dist-es/rateLimits/helpers/RedisCooldown.js +0 -170
- package/dist-es/rateLimits/helpers/createRateLimitExceededException.js +0 -28
- package/dist-es/rateLimits/helpers/executeWithRateLimitHandling.js +0 -140
- package/dist-es/rateLimits/helpers/headerUtils.js +0 -34
- package/dist-es/rateLimits/helpers/isStreamChannelLike.js +0 -19
- package/dist-es/rateLimits/helpers/processStreamBudgetHeaders.js +0 -89
- package/dist-es/rateLimits/helpers/processStreamRateLimitException.js +0 -59
- package/dist-es/rateLimits/helpers/processStreamRateLimitHeaders.js +0 -46
- package/dist-es/rateLimits/helpers/rateLimitSnapshot.js +0 -26
- package/dist-es/rateLimits/helpers/registerStreamBudgetInterceptors.js +0 -44
- package/dist-es/rateLimits/helpers/registerStreamInterceptors.js +0 -54
- package/dist-es/rateLimits/helpers/splitCallArgsAndOptions.js +0 -17
- package/dist-es/rateLimits/index.js +0 -4
- package/dist-es/rateLimits/types.js +0 -1
- package/dist-es/rateLimits/withStreamRateLimitOptions.js +0 -6
- package/dist-es/streamRateLimitHandlingProxy/constants.js +0 -32
- package/dist-es/streamRateLimitHandlingProxy/createRateLimitedStreamProxy.js +0 -88
- package/dist-es/streamRateLimitHandlingProxy/generatedMethodNames.js +0 -317
- package/dist-es/streamRateLimitHandlingProxy/helpers/RedisCooldown.js +0 -170
- package/dist-es/streamRateLimitHandlingProxy/helpers/createRateLimitExceededException.js +0 -25
- package/dist-es/streamRateLimitHandlingProxy/helpers/executeWithRateLimitHandling.js +0 -148
- package/dist-es/streamRateLimitHandlingProxy/helpers/headerUtils.js +0 -42
- package/dist-es/streamRateLimitHandlingProxy/helpers/isStreamChannelLike.js +0 -19
- package/dist-es/streamRateLimitHandlingProxy/helpers/processStreamBudgetHeaders.js +0 -89
- package/dist-es/streamRateLimitHandlingProxy/helpers/processStreamRateLimitException.js +0 -59
- package/dist-es/streamRateLimitHandlingProxy/helpers/processStreamRateLimitHeaders.js +0 -42
- package/dist-es/streamRateLimitHandlingProxy/helpers/rateLimitSnapshot.js +0 -26
- package/dist-es/streamRateLimitHandlingProxy/helpers/registerStreamInterceptors.js +0 -60
- package/dist-es/streamRateLimitHandlingProxy/helpers/sleep.js +0 -4
- package/dist-es/streamRateLimitHandlingProxy/helpers/splitCallArgsAndOptions.js +0 -17
- package/dist-es/streamRateLimitHandlingProxy/index.js +0 -2
- package/dist-es/streamRateLimitHandlingProxy/types.js +0 -1
- package/dist-es/streamRateLimitHandlingProxy/withStreamRateLimitOptions.js +0 -6
- package/dist-types/rateLimits/RedisCooldown.d.ts +0 -7
- package/dist-types/rateLimits/constants.d.ts +0 -29
- package/dist-types/rateLimits/createRateLimitedStreamProxy.d.ts +0 -11
- package/dist-types/rateLimits/exception/createRateLimitExceededException.d.ts +0 -12
- package/dist-types/rateLimits/exception/processStreamRateLimitException.d.ts +0 -8
- package/dist-types/rateLimits/executeStreamRequestWithRateLimitHandling.d.ts +0 -8
- package/dist-types/rateLimits/generatedMethodNames.d.ts +0 -11
- package/dist-types/rateLimits/helpers/RedisCooldown.d.ts +0 -11
- package/dist-types/rateLimits/helpers/createRateLimitExceededException.d.ts +0 -10
- package/dist-types/rateLimits/helpers/executeWithRateLimitHandling.d.ts +0 -2
- package/dist-types/rateLimits/helpers/headerUtils.d.ts +0 -4
- package/dist-types/rateLimits/helpers/isStreamChannelLike.d.ts +0 -6
- package/dist-types/rateLimits/helpers/processStreamBudgetHeaders.d.ts +0 -9
- package/dist-types/rateLimits/helpers/processStreamRateLimitException.d.ts +0 -3
- package/dist-types/rateLimits/helpers/processStreamRateLimitHeaders.d.ts +0 -6
- package/dist-types/rateLimits/helpers/rateLimitSnapshot.d.ts +0 -3
- package/dist-types/rateLimits/helpers/registerStreamBudgetInterceptors.d.ts +0 -2
- package/dist-types/rateLimits/helpers/registerStreamInterceptors.d.ts +0 -2
- package/dist-types/rateLimits/helpers/splitCallArgsAndOptions.d.ts +0 -6
- package/dist-types/rateLimits/index.d.ts +0 -4
- package/dist-types/rateLimits/types.d.ts +0 -68
- package/dist-types/rateLimits/withStreamRateLimitOptions.d.ts +0 -6
- package/dist-types/streamRateLimitHandlingProxy/constants.d.ts +0 -28
- package/dist-types/streamRateLimitHandlingProxy/createRateLimitedStreamProxy.d.ts +0 -11
- package/dist-types/streamRateLimitHandlingProxy/generatedMethodNames.d.ts +0 -11
- package/dist-types/streamRateLimitHandlingProxy/helpers/RedisCooldown.d.ts +0 -11
- package/dist-types/streamRateLimitHandlingProxy/helpers/createRateLimitExceededException.d.ts +0 -10
- package/dist-types/streamRateLimitHandlingProxy/helpers/executeWithRateLimitHandling.d.ts +0 -2
- package/dist-types/streamRateLimitHandlingProxy/helpers/headerUtils.d.ts +0 -4
- package/dist-types/streamRateLimitHandlingProxy/helpers/isStreamChannelLike.d.ts +0 -6
- package/dist-types/streamRateLimitHandlingProxy/helpers/processStreamBudgetHeaders.d.ts +0 -9
- package/dist-types/streamRateLimitHandlingProxy/helpers/processStreamRateLimitException.d.ts +0 -3
- package/dist-types/streamRateLimitHandlingProxy/helpers/processStreamRateLimitHeaders.d.ts +0 -6
- package/dist-types/streamRateLimitHandlingProxy/helpers/rateLimitSnapshot.d.ts +0 -3
- package/dist-types/streamRateLimitHandlingProxy/helpers/registerStreamInterceptors.d.ts +0 -2
- package/dist-types/streamRateLimitHandlingProxy/helpers/sleep.d.ts +0 -2
- package/dist-types/streamRateLimitHandlingProxy/helpers/splitCallArgsAndOptions.d.ts +0 -6
- package/dist-types/streamRateLimitHandlingProxy/index.d.ts +0 -2
- package/dist-types/streamRateLimitHandlingProxy/types.d.ts +0 -68
- package/dist-types/streamRateLimitHandlingProxy/withStreamRateLimitOptions.d.ts +0 -6
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import {setTimeout as sleep} from 'node:timers/promises';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
DEFAULT_FIXED_RATE_LIMIT_DELAY_MS,
|
|
5
|
-
DEFAULT_JITTER_MS,
|
|
6
|
-
DEFAULT_OPTS_1_ATTEMPT_NO_COOLDOWN,
|
|
7
|
-
DEFAULT_RETRY_AFTER_MS,
|
|
8
|
-
RETRY_AFTER_BUFFER_MS,
|
|
9
|
-
} from './constants';
|
|
10
|
-
import {createRateLimitExceededException} from './exception/createRateLimitExceededException';
|
|
11
|
-
import {processStreamRateLimitException} from './exception/processStreamRateLimitException';
|
|
12
|
-
import {RedisCooldown} from './RedisCooldown';
|
|
13
|
-
|
|
14
|
-
/** Computes actual backoff delay from rate-limit error and configured caps. */
|
|
15
|
-
function calculateRateLimitDelayMs(error, maxDelayMs) {
|
|
16
|
-
const retryAfterMs =
|
|
17
|
-
typeof error.retryAfter === 'number' ? error.retryAfter + RETRY_AFTER_BUFFER_MS : DEFAULT_RETRY_AFTER_MS;
|
|
18
|
-
|
|
19
|
-
if (retryAfterMs > 0) {
|
|
20
|
-
const jitterMs = Math.floor(Math.random() * DEFAULT_JITTER_MS);
|
|
21
|
-
|
|
22
|
-
return Math.min(maxDelayMs, retryAfterMs + jitterMs);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return Math.min(maxDelayMs, DEFAULT_FIXED_RATE_LIMIT_DELAY_MS);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Executes Stream request with cooldown check, retry policy and rate-limit normalization. */
|
|
29
|
-
export async function executeStreamRequestWithRateLimitHandling(execute, operation, context, options = {}) {
|
|
30
|
-
const {redis} = context;
|
|
31
|
-
const {enableCooldown, maxAttempts, maxDelayMs, maxRetryableDelayMs} = {
|
|
32
|
-
...DEFAULT_OPTS_1_ATTEMPT_NO_COOLDOWN,
|
|
33
|
-
...options,
|
|
34
|
-
};
|
|
35
|
-
let attempt = 1;
|
|
36
|
-
|
|
37
|
-
if (enableCooldown) {
|
|
38
|
-
const cooldownMs = await RedisCooldown.getRemainingMs(operation, {redis});
|
|
39
|
-
|
|
40
|
-
if (cooldownMs > 0) {
|
|
41
|
-
console.warn('Skipping stream request due to active rate-limit cooldown', {
|
|
42
|
-
operation,
|
|
43
|
-
cooldown: cooldownMs,
|
|
44
|
-
});
|
|
45
|
-
throw createRateLimitExceededException({operation, retryAfterMs: cooldownMs});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
for (;;) {
|
|
50
|
-
try {
|
|
51
|
-
return await execute();
|
|
52
|
-
} catch (error) {
|
|
53
|
-
const rateLimitError = processStreamRateLimitException(error);
|
|
54
|
-
|
|
55
|
-
if (!rateLimitError) {
|
|
56
|
-
throw error;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const retryAfterMs = calculateRateLimitDelayMs(rateLimitError, maxDelayMs);
|
|
60
|
-
|
|
61
|
-
if (enableCooldown) {
|
|
62
|
-
await RedisCooldown.setRemainingMs(operation, retryAfterMs, {redis});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (attempt < maxAttempts && retryAfterMs <= maxRetryableDelayMs) {
|
|
66
|
-
console.warn('Retrying stream request after rate limit', {
|
|
67
|
-
operation,
|
|
68
|
-
attempt,
|
|
69
|
-
maxAttempts,
|
|
70
|
-
retryAfterMs,
|
|
71
|
-
rateLimit: rateLimitError.rateLimit,
|
|
72
|
-
rateLimitRemaining: rateLimitError.rateLimitRemaining,
|
|
73
|
-
rateLimitReset: rateLimitError.rateLimitReset,
|
|
74
|
-
});
|
|
75
|
-
await sleep(retryAfterMs);
|
|
76
|
-
attempt++;
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
console.warn('Propagating stream rate limit w/o retry', {
|
|
81
|
-
operation,
|
|
82
|
-
attempt,
|
|
83
|
-
maxAttempts,
|
|
84
|
-
retryAfterMs,
|
|
85
|
-
maxRetryableDelayMs,
|
|
86
|
-
rateLimit: rateLimitError.rateLimit,
|
|
87
|
-
rateLimitRemaining: rateLimitError.rateLimitRemaining,
|
|
88
|
-
rateLimitReset: rateLimitError.rateLimitReset,
|
|
89
|
-
});
|
|
90
|
-
throw rateLimitError;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
@@ -1,317 +0,0 @@
|
|
|
1
|
-
export const STREAM_METHOD_NAMES = [
|
|
2
|
-
'_addChannelConfig',
|
|
3
|
-
'_buildWSPayload',
|
|
4
|
-
'_callClientListeners',
|
|
5
|
-
'_createImport',
|
|
6
|
-
'_createImportURL',
|
|
7
|
-
'_deleteUserMessageReference',
|
|
8
|
-
'_enrichAxiosOptions',
|
|
9
|
-
'_getConnectionID',
|
|
10
|
-
'_getImport',
|
|
11
|
-
'_getToken',
|
|
12
|
-
'_handleClientEvent',
|
|
13
|
-
'_handleUserEvent',
|
|
14
|
-
'_hasConnectionID',
|
|
15
|
-
'_isUsingServerAuth',
|
|
16
|
-
'_listImports',
|
|
17
|
-
'_logApiError',
|
|
18
|
-
'_logApiRequest',
|
|
19
|
-
'_logApiResponse',
|
|
20
|
-
'_muteStatus',
|
|
21
|
-
'_normalizeDate',
|
|
22
|
-
'_normalizeExpiration',
|
|
23
|
-
'_queryFlagReports',
|
|
24
|
-
'_queryFlags',
|
|
25
|
-
'_reviewFlagReport',
|
|
26
|
-
'_sayHi',
|
|
27
|
-
'_setToken',
|
|
28
|
-
'_setupConnection',
|
|
29
|
-
'_setUser',
|
|
30
|
-
'_startCleaning',
|
|
31
|
-
'_unblockMessage',
|
|
32
|
-
'_updateMemberWatcherReferences',
|
|
33
|
-
'_updateUserMessageReferences',
|
|
34
|
-
'_updateUserReferences',
|
|
35
|
-
'_validateAndGetMessageId',
|
|
36
|
-
'addDevice',
|
|
37
|
-
'banUser',
|
|
38
|
-
'channel',
|
|
39
|
-
'closeConnection',
|
|
40
|
-
'commitMessage',
|
|
41
|
-
'connect',
|
|
42
|
-
'connectAnonymousUser',
|
|
43
|
-
'connectUser',
|
|
44
|
-
'createAbortControllerForNextRequest',
|
|
45
|
-
'createBlockList',
|
|
46
|
-
'createCampaign',
|
|
47
|
-
'createChannelType',
|
|
48
|
-
'createCommand',
|
|
49
|
-
'createPermission',
|
|
50
|
-
'createRole',
|
|
51
|
-
'createSegment',
|
|
52
|
-
'createToken',
|
|
53
|
-
'deactivateUser',
|
|
54
|
-
'deactivateUsers',
|
|
55
|
-
'delete',
|
|
56
|
-
'deleteBlockList',
|
|
57
|
-
'deleteCampaign',
|
|
58
|
-
'deleteChannels',
|
|
59
|
-
'deleteChannelType',
|
|
60
|
-
'deleteCommand',
|
|
61
|
-
'deleteMessage',
|
|
62
|
-
'deletePermission',
|
|
63
|
-
'deletePushProvider',
|
|
64
|
-
'deleteRole',
|
|
65
|
-
'deleteSegment',
|
|
66
|
-
'deleteUser',
|
|
67
|
-
'deleteUsers',
|
|
68
|
-
'devToken',
|
|
69
|
-
'disconnect',
|
|
70
|
-
'disconnectUser',
|
|
71
|
-
'dispatchEvent',
|
|
72
|
-
'doAxiosRequest',
|
|
73
|
-
'enrichURL',
|
|
74
|
-
'errorFromResponse',
|
|
75
|
-
'exportChannel',
|
|
76
|
-
'exportChannels',
|
|
77
|
-
'exportUser',
|
|
78
|
-
'exportUsers',
|
|
79
|
-
'flagMessage',
|
|
80
|
-
'flagUser',
|
|
81
|
-
'get',
|
|
82
|
-
'getAppSettings',
|
|
83
|
-
'getAuthType',
|
|
84
|
-
'getBlockList',
|
|
85
|
-
'getCallToken',
|
|
86
|
-
'getChannelById',
|
|
87
|
-
'getChannelByMembers',
|
|
88
|
-
'getChannelType',
|
|
89
|
-
'getCommand',
|
|
90
|
-
'getDevices',
|
|
91
|
-
'getExportChannelStatus',
|
|
92
|
-
'getMessage',
|
|
93
|
-
'getPermission',
|
|
94
|
-
'getRateLimits',
|
|
95
|
-
'getTask',
|
|
96
|
-
'getUnreadCount',
|
|
97
|
-
'getUserAgent',
|
|
98
|
-
'handleEvent',
|
|
99
|
-
'handleResponse',
|
|
100
|
-
'hydrateActiveChannels',
|
|
101
|
-
'listBlockLists',
|
|
102
|
-
'listChannelTypes',
|
|
103
|
-
'listCommands',
|
|
104
|
-
'listPermissions',
|
|
105
|
-
'listPushProviders',
|
|
106
|
-
'listRoles',
|
|
107
|
-
'markAllRead',
|
|
108
|
-
'markChannelsRead',
|
|
109
|
-
'muteUser',
|
|
110
|
-
'off',
|
|
111
|
-
'on',
|
|
112
|
-
'openConnection',
|
|
113
|
-
'partialUpdateMessage',
|
|
114
|
-
'partialUpdateUser',
|
|
115
|
-
'partialUpdateUsers',
|
|
116
|
-
'patch',
|
|
117
|
-
'pinMessage',
|
|
118
|
-
'post',
|
|
119
|
-
'put',
|
|
120
|
-
'queryBannedUsers',
|
|
121
|
-
'queryCampaigns',
|
|
122
|
-
'queryChannels',
|
|
123
|
-
'queryMessageFlags',
|
|
124
|
-
'queryRecipients',
|
|
125
|
-
'querySegments',
|
|
126
|
-
'queryUsers',
|
|
127
|
-
'reactivateUser',
|
|
128
|
-
'reactivateUsers',
|
|
129
|
-
'recoverState',
|
|
130
|
-
'removeDevice',
|
|
131
|
-
'removeShadowBan',
|
|
132
|
-
'restoreUsers',
|
|
133
|
-
'resumeCampaign',
|
|
134
|
-
'revokeTokens',
|
|
135
|
-
'revokeUsersToken',
|
|
136
|
-
'revokeUserToken',
|
|
137
|
-
'scheduleCampaign',
|
|
138
|
-
'search',
|
|
139
|
-
'sendFile',
|
|
140
|
-
'sendUserCustomEvent',
|
|
141
|
-
'setAnonymousUser',
|
|
142
|
-
'setBaseURL',
|
|
143
|
-
'setGuestUser',
|
|
144
|
-
'setLocalDevice',
|
|
145
|
-
'setUser',
|
|
146
|
-
'setUserAgent',
|
|
147
|
-
'shadowBan',
|
|
148
|
-
'stopCampaign',
|
|
149
|
-
'sync',
|
|
150
|
-
'testCampaign',
|
|
151
|
-
'testPushSettings',
|
|
152
|
-
'testSQSSettings',
|
|
153
|
-
'translateMessage',
|
|
154
|
-
'unbanUser',
|
|
155
|
-
'unblockMessage',
|
|
156
|
-
'unflagMessage',
|
|
157
|
-
'unflagUser',
|
|
158
|
-
'unmuteUser',
|
|
159
|
-
'unpinMessage',
|
|
160
|
-
'updateAppSettings',
|
|
161
|
-
'updateBlockList',
|
|
162
|
-
'updateCampaign',
|
|
163
|
-
'updateChannelType',
|
|
164
|
-
'updateCommand',
|
|
165
|
-
'updateMessage',
|
|
166
|
-
'updatePermission',
|
|
167
|
-
'updateSegment',
|
|
168
|
-
'updateUser',
|
|
169
|
-
'updateUsers',
|
|
170
|
-
'upsertPushProvider',
|
|
171
|
-
'upsertUser',
|
|
172
|
-
'upsertUsers',
|
|
173
|
-
'userMuteStatus',
|
|
174
|
-
'verifyWebhook',
|
|
175
|
-
];
|
|
176
|
-
export const STREAM_SYNC_METHOD_NAMES = [
|
|
177
|
-
'_addChannelConfig',
|
|
178
|
-
'_buildWSPayload',
|
|
179
|
-
'_callClientListeners',
|
|
180
|
-
'_deleteUserMessageReference',
|
|
181
|
-
'_enrichAxiosOptions',
|
|
182
|
-
'_getConnectionID',
|
|
183
|
-
'_getToken',
|
|
184
|
-
'_handleClientEvent',
|
|
185
|
-
'_handleUserEvent',
|
|
186
|
-
'_hasConnectionID',
|
|
187
|
-
'_isUsingServerAuth',
|
|
188
|
-
'_logApiError',
|
|
189
|
-
'_logApiRequest',
|
|
190
|
-
'_logApiResponse',
|
|
191
|
-
'_muteStatus',
|
|
192
|
-
'_normalizeDate',
|
|
193
|
-
'_normalizeExpiration',
|
|
194
|
-
'_sayHi',
|
|
195
|
-
'_setUser',
|
|
196
|
-
'_startCleaning',
|
|
197
|
-
'_updateMemberWatcherReferences',
|
|
198
|
-
'_updateUserMessageReferences',
|
|
199
|
-
'_updateUserReferences',
|
|
200
|
-
'_validateAndGetMessageId',
|
|
201
|
-
'createAbortControllerForNextRequest',
|
|
202
|
-
'createToken',
|
|
203
|
-
'devToken',
|
|
204
|
-
'dispatchEvent',
|
|
205
|
-
'errorFromResponse',
|
|
206
|
-
'getAuthType',
|
|
207
|
-
'getUserAgent',
|
|
208
|
-
'handleEvent',
|
|
209
|
-
'handleResponse',
|
|
210
|
-
'off',
|
|
211
|
-
'on',
|
|
212
|
-
'setBaseURL',
|
|
213
|
-
'setLocalDevice',
|
|
214
|
-
'setUserAgent',
|
|
215
|
-
'userMuteStatus',
|
|
216
|
-
'verifyWebhook',
|
|
217
|
-
];
|
|
218
|
-
export const STREAM_CHANNEL_RESULT_METHOD_NAMES = [
|
|
219
|
-
'channel',
|
|
220
|
-
'getChannelById',
|
|
221
|
-
'getChannelByMembers',
|
|
222
|
-
'hydrateActiveChannels',
|
|
223
|
-
'queryChannels',
|
|
224
|
-
];
|
|
225
|
-
export const STREAM_SYNC_CHANNEL_RESULT_METHOD_NAMES = [
|
|
226
|
-
'channel',
|
|
227
|
-
'getChannelById',
|
|
228
|
-
'getChannelByMembers',
|
|
229
|
-
'hydrateActiveChannels',
|
|
230
|
-
];
|
|
231
|
-
export const CHANNEL_METHOD_NAMES = [
|
|
232
|
-
'_callChannelListeners',
|
|
233
|
-
'_channelURL',
|
|
234
|
-
'_checkInitialized',
|
|
235
|
-
'_countMessageAsUnread',
|
|
236
|
-
'_disconnect',
|
|
237
|
-
'_extendEventWithOwnReactions',
|
|
238
|
-
'_handleChannelEvent',
|
|
239
|
-
'_initializeState',
|
|
240
|
-
'_update',
|
|
241
|
-
'acceptInvite',
|
|
242
|
-
'addMembers',
|
|
243
|
-
'addModerators',
|
|
244
|
-
'assignRoles',
|
|
245
|
-
'banUser',
|
|
246
|
-
'clean',
|
|
247
|
-
'countUnread',
|
|
248
|
-
'countUnreadMentions',
|
|
249
|
-
'create',
|
|
250
|
-
'createCall',
|
|
251
|
-
'delete',
|
|
252
|
-
'deleteFile',
|
|
253
|
-
'deleteImage',
|
|
254
|
-
'deleteReaction',
|
|
255
|
-
'demoteModerators',
|
|
256
|
-
'disableSlowMode',
|
|
257
|
-
'enableSlowMode',
|
|
258
|
-
'getClient',
|
|
259
|
-
'getConfig',
|
|
260
|
-
'getMessagesById',
|
|
261
|
-
'getPinnedMessages',
|
|
262
|
-
'getReactions',
|
|
263
|
-
'getReplies',
|
|
264
|
-
'hide',
|
|
265
|
-
'inviteMembers',
|
|
266
|
-
'keystroke',
|
|
267
|
-
'lastMessage',
|
|
268
|
-
'lastRead',
|
|
269
|
-
'markRead',
|
|
270
|
-
'markUnread',
|
|
271
|
-
'mute',
|
|
272
|
-
'muteStatus',
|
|
273
|
-
'off',
|
|
274
|
-
'on',
|
|
275
|
-
'query',
|
|
276
|
-
'queryMembers',
|
|
277
|
-
'rejectInvite',
|
|
278
|
-
'removeMembers',
|
|
279
|
-
'removeShadowBan',
|
|
280
|
-
'search',
|
|
281
|
-
'sendAction',
|
|
282
|
-
'sendEvent',
|
|
283
|
-
'sendFile',
|
|
284
|
-
'sendImage',
|
|
285
|
-
'sendMessage',
|
|
286
|
-
'sendReaction',
|
|
287
|
-
'shadowBan',
|
|
288
|
-
'show',
|
|
289
|
-
'stopTyping',
|
|
290
|
-
'stopWatching',
|
|
291
|
-
'truncate',
|
|
292
|
-
'unbanUser',
|
|
293
|
-
'unmute',
|
|
294
|
-
'update',
|
|
295
|
-
'updatePartial',
|
|
296
|
-
'watch',
|
|
297
|
-
];
|
|
298
|
-
export const CHANNEL_SYNC_METHOD_NAMES = [
|
|
299
|
-
'_callChannelListeners',
|
|
300
|
-
'_channelURL',
|
|
301
|
-
'_checkInitialized',
|
|
302
|
-
'_countMessageAsUnread',
|
|
303
|
-
'_disconnect',
|
|
304
|
-
'_extendEventWithOwnReactions',
|
|
305
|
-
'_handleChannelEvent',
|
|
306
|
-
'_initializeState',
|
|
307
|
-
'clean',
|
|
308
|
-
'countUnread',
|
|
309
|
-
'countUnreadMentions',
|
|
310
|
-
'getConfig',
|
|
311
|
-
'lastMessage',
|
|
312
|
-
'lastRead',
|
|
313
|
-
'muteStatus',
|
|
314
|
-
'off',
|
|
315
|
-
'on',
|
|
316
|
-
];
|
|
317
|
-
export const CHANNEL_SYNC_PROXY_RESULT_METHOD_NAMES = ['getClient'];
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX } from '../constants';
|
|
2
|
-
export class RedisCooldown {
|
|
3
|
-
static resolveRedisKey(cooldownKey, options) {
|
|
4
|
-
const keyPrefix = options?.keyPrefix ?? STREAM_RATE_LIMIT_COOLDOWN_KEY_PREFIX;
|
|
5
|
-
return `${keyPrefix}${cooldownKey}`;
|
|
6
|
-
}
|
|
7
|
-
static async getRemainingMs(cooldownKey, context, options) {
|
|
8
|
-
const { redis, logger } = context;
|
|
9
|
-
try {
|
|
10
|
-
const ttlMs = await redis.pttl(RedisCooldown.resolveRedisKey(cooldownKey, options));
|
|
11
|
-
if (ttlMs > 0) {
|
|
12
|
-
return ttlMs;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
catch (error) {
|
|
16
|
-
logger.warn('Failed to read stream rate-limit cooldown from Redis', {
|
|
17
|
-
cooldownKey,
|
|
18
|
-
error,
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
return 0;
|
|
22
|
-
}
|
|
23
|
-
static async setRemainingMs(cooldownKey, cooldownMs, context, options) {
|
|
24
|
-
if (cooldownMs <= 0) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const { redis, logger } = context;
|
|
28
|
-
const redisKey = RedisCooldown.resolveRedisKey(cooldownKey, options);
|
|
29
|
-
try {
|
|
30
|
-
await redis.eval(`
|
|
31
|
-
local key = KEYS[1]
|
|
32
|
-
local new_ttl = tonumber(ARGV[1])
|
|
33
|
-
local current_ttl = redis.call('PTTL', key)
|
|
34
|
-
|
|
35
|
-
if current_ttl == -2 then
|
|
36
|
-
redis.call('SET', key, '1', 'PX', new_ttl)
|
|
37
|
-
return 1
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
if current_ttl == -1 or current_ttl < new_ttl then
|
|
41
|
-
redis.call('PEXPIRE', key, new_ttl)
|
|
42
|
-
return 2
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
return 0
|
|
46
|
-
`, 1, redisKey, cooldownMs.toString());
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
logger.warn('Failed to write stream rate-limit cooldown to Redis', {
|
|
50
|
-
cooldownKey,
|
|
51
|
-
cooldownMs,
|
|
52
|
-
error,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
static async setRemainingMsExact(cooldownKey, cooldownMs, context, options) {
|
|
57
|
-
if (cooldownMs <= 0) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const { redis, logger } = context;
|
|
61
|
-
try {
|
|
62
|
-
await redis.set(RedisCooldown.resolveRedisKey(cooldownKey, options), '1', 'PX', cooldownMs);
|
|
63
|
-
}
|
|
64
|
-
catch (error) {
|
|
65
|
-
logger.warn('Failed to write exact stream rate-limit cooldown to Redis', {
|
|
66
|
-
cooldownKey,
|
|
67
|
-
cooldownMs,
|
|
68
|
-
error,
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
static async getValue(cooldownKey, context, options) {
|
|
73
|
-
const { redis, logger } = context;
|
|
74
|
-
try {
|
|
75
|
-
const value = await redis.get(RedisCooldown.resolveRedisKey(cooldownKey, options));
|
|
76
|
-
if (typeof value === 'string' && value.length > 0) {
|
|
77
|
-
return value;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
logger.warn('Failed to read stream rate-limit metadata from Redis', {
|
|
82
|
-
cooldownKey,
|
|
83
|
-
error,
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
static async setValueWithRemainingMs(cooldownKey, value, cooldownMs, context, options) {
|
|
88
|
-
if (value.length === 0 || cooldownMs <= 0) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
const { redis, logger } = context;
|
|
92
|
-
try {
|
|
93
|
-
await redis.eval(`
|
|
94
|
-
local key = KEYS[1]
|
|
95
|
-
local value = ARGV[1]
|
|
96
|
-
local new_ttl = tonumber(ARGV[2])
|
|
97
|
-
local current_ttl = redis.call('PTTL', key)
|
|
98
|
-
|
|
99
|
-
if current_ttl == -2 then
|
|
100
|
-
redis.call('SET', key, value, 'PX', new_ttl)
|
|
101
|
-
return 1
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
if current_ttl == -1 or current_ttl < new_ttl then
|
|
105
|
-
redis.call('SET', key, value, 'PX', new_ttl)
|
|
106
|
-
return 2
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
redis.call('SET', key, value, 'KEEPTTL')
|
|
110
|
-
return 0
|
|
111
|
-
`, 1, RedisCooldown.resolveRedisKey(cooldownKey, options), value, cooldownMs.toString());
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
logger.warn('Failed to write stream rate-limit metadata to Redis', {
|
|
115
|
-
cooldownKey,
|
|
116
|
-
cooldownMs,
|
|
117
|
-
error,
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
static async setRemainingMsWithValue(cooldownKey, valueKey, value, cooldownMs, context, options) {
|
|
122
|
-
if (value.length === 0 || cooldownMs <= 0) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
const { redis, logger } = context;
|
|
126
|
-
const cooldownRedisKey = RedisCooldown.resolveRedisKey(cooldownKey, options);
|
|
127
|
-
const valueRedisKey = RedisCooldown.resolveRedisKey(valueKey, options);
|
|
128
|
-
try {
|
|
129
|
-
await redis.eval(`
|
|
130
|
-
local cooldown_key = KEYS[1]
|
|
131
|
-
local value_key = KEYS[2]
|
|
132
|
-
local value = ARGV[1]
|
|
133
|
-
local new_ttl = tonumber(ARGV[2])
|
|
134
|
-
local current_ttl = redis.call('PTTL', cooldown_key)
|
|
135
|
-
local final_ttl = new_ttl
|
|
136
|
-
|
|
137
|
-
if current_ttl == -2 then
|
|
138
|
-
redis.call('SET', cooldown_key, '1', 'PX', new_ttl)
|
|
139
|
-
elseif current_ttl == -1 or current_ttl < new_ttl then
|
|
140
|
-
redis.call('PEXPIRE', cooldown_key, new_ttl)
|
|
141
|
-
else
|
|
142
|
-
final_ttl = current_ttl
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
redis.call('SET', value_key, value, 'PX', final_ttl)
|
|
146
|
-
return final_ttl
|
|
147
|
-
`, 2, cooldownRedisKey, valueRedisKey, value, cooldownMs.toString());
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
logger.warn('Failed to write stream rate-limit cooldown and metadata to Redis', {
|
|
151
|
-
cooldownKey,
|
|
152
|
-
valueKey,
|
|
153
|
-
cooldownMs,
|
|
154
|
-
error,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
static async clear(cooldownKey, context, options) {
|
|
159
|
-
const { redis, logger } = context;
|
|
160
|
-
try {
|
|
161
|
-
await redis.del(RedisCooldown.resolveRedisKey(cooldownKey, options));
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
logger.warn('Failed to clear stream rate-limit cooldown from Redis', {
|
|
165
|
-
cooldownKey,
|
|
166
|
-
error,
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { RateLimitExceededException } from '@wildix/xbees-conversations-client';
|
|
2
|
-
import { DEFAULT_RETRY_AFTER_MS } from '../constants';
|
|
3
|
-
const RETRY_AFTER_HEADER_PADDING_MS = 3000;
|
|
4
|
-
function toPositiveMilliseconds(milliseconds) {
|
|
5
|
-
return Math.max(0, Math.ceil(milliseconds));
|
|
6
|
-
}
|
|
7
|
-
function toEpochSecondsFromNow(delayMs) {
|
|
8
|
-
return Math.ceil((Date.now() + toPositiveMilliseconds(delayMs)) / 1000);
|
|
9
|
-
}
|
|
10
|
-
export function createRateLimitExceededException(options = {}) {
|
|
11
|
-
const { rateLimit, message, operation, retryAfterMs = DEFAULT_RETRY_AFTER_MS } = options;
|
|
12
|
-
const defaultMessage = operation ? `Rate limit exceeded for ${operation}` : 'Rate limit exceeded';
|
|
13
|
-
const retryAfterFromResetMs = rateLimit?.reset ? rateLimit.reset - Date.now() : undefined;
|
|
14
|
-
const effectiveRetryAfterMs = retryAfterFromResetMs ?? retryAfterMs;
|
|
15
|
-
const retryAfterMilliseconds = toPositiveMilliseconds(effectiveRetryAfterMs);
|
|
16
|
-
const rateLimitResetMilliseconds = rateLimit?.reset
|
|
17
|
-
? toPositiveMilliseconds(retryAfterFromResetMs ?? 0)
|
|
18
|
-
: retryAfterMilliseconds;
|
|
19
|
-
const retryAfterEpochSeconds = toEpochSecondsFromNow(effectiveRetryAfterMs + RETRY_AFTER_HEADER_PADDING_MS);
|
|
20
|
-
return new RateLimitExceededException({
|
|
21
|
-
$metadata: {},
|
|
22
|
-
message: message || defaultMessage,
|
|
23
|
-
rateLimitRemaining: rateLimit?.remaining || 0,
|
|
24
|
-
retryAfter: retryAfterEpochSeconds,
|
|
25
|
-
rateLimit: rateLimit?.limit?.toString() || 'unknown',
|
|
26
|
-
rateLimitReset: rateLimitResetMilliseconds,
|
|
27
|
-
});
|
|
28
|
-
}
|