@wildix/xbees-conversations-utils 1.1.84 → 1.1.86
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/README.md +118 -1
- package/dist-cjs/index.js +2 -1
- package/dist-cjs/rateLimits/RedisCooldown.js +66 -0
- package/dist-cjs/rateLimits/constants.js +43 -0
- package/dist-cjs/rateLimits/createRateLimitedStreamProxy.js +92 -0
- package/dist-cjs/rateLimits/exception/createRateLimitExceededException.js +21 -0
- package/dist-cjs/rateLimits/exception/processStreamRateLimitException.js +47 -0
- package/dist-cjs/rateLimits/executeStreamRequestWithRateLimitHandling.js +94 -0
- package/dist-cjs/rateLimits/generatedMethodNames.js +320 -0
- package/dist-cjs/rateLimits/helpers/RedisCooldown.js +174 -0
- package/dist-cjs/rateLimits/helpers/createRateLimitExceededException.js +32 -0
- package/dist-cjs/rateLimits/helpers/executeWithRateLimitHandling.js +144 -0
- package/dist-cjs/rateLimits/helpers/headerUtils.js +40 -0
- package/dist-cjs/rateLimits/helpers/isStreamChannelLike.js +23 -0
- package/dist-cjs/rateLimits/helpers/processStreamBudgetHeaders.js +93 -0
- package/dist-cjs/rateLimits/helpers/processStreamRateLimitException.js +63 -0
- package/dist-cjs/rateLimits/helpers/processStreamRateLimitHeaders.js +50 -0
- package/dist-cjs/rateLimits/helpers/rateLimitSnapshot.js +31 -0
- package/dist-cjs/rateLimits/helpers/registerStreamBudgetInterceptors.js +48 -0
- package/dist-cjs/rateLimits/helpers/registerStreamInterceptors.js +58 -0
- package/dist-cjs/rateLimits/helpers/splitCallArgsAndOptions.js +21 -0
- package/dist-cjs/rateLimits/index.js +7 -0
- package/dist-cjs/rateLimits/types.js +3 -0
- package/dist-cjs/rateLimits/withStreamRateLimitOptions.js +10 -0
- package/dist-cjs/streamRateLimitHandlingProxy/constants.js +43 -0
- package/dist-cjs/streamRateLimitHandlingProxy/createRateLimitedStreamProxy.js +92 -0
- package/dist-cjs/streamRateLimitHandlingProxy/generatedMethodNames.js +320 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/RedisCooldown.js +174 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/createRateLimitExceededException.js +32 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/executeWithRateLimitHandling.js +144 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/headerUtils.js +40 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/isStreamChannelLike.js +23 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/processStreamBudgetHeaders.js +93 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/processStreamRateLimitException.js +63 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/processStreamRateLimitHeaders.js +72 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/rateLimitSnapshot.js +31 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/registerStreamInterceptors.js +58 -0
- package/dist-cjs/streamRateLimitHandlingProxy/helpers/splitCallArgsAndOptions.js +21 -0
- package/dist-cjs/streamRateLimitHandlingProxy/index.js +5 -0
- package/dist-cjs/streamRateLimitHandlingProxy/types.js +3 -0
- package/dist-cjs/streamRateLimitHandlingProxy/withStreamRateLimitOptions.js +10 -0
- package/dist-es/index.js +2 -1
- package/dist-es/rateLimits/RedisCooldown.js +62 -0
- package/dist-es/rateLimits/constants.js +33 -0
- package/dist-es/rateLimits/createRateLimitedStreamProxy.js +88 -0
- package/dist-es/rateLimits/exception/createRateLimitExceededException.js +17 -0
- package/dist-es/rateLimits/exception/processStreamRateLimitException.js +37 -0
- package/dist-es/rateLimits/executeStreamRequestWithRateLimitHandling.js +93 -0
- package/dist-es/rateLimits/generatedMethodNames.js +317 -0
- package/dist-es/rateLimits/helpers/RedisCooldown.js +170 -0
- package/dist-es/rateLimits/helpers/createRateLimitExceededException.js +28 -0
- package/dist-es/rateLimits/helpers/executeWithRateLimitHandling.js +140 -0
- package/dist-es/rateLimits/helpers/headerUtils.js +34 -0
- package/dist-es/rateLimits/helpers/isStreamChannelLike.js +19 -0
- package/dist-es/rateLimits/helpers/processStreamBudgetHeaders.js +89 -0
- package/dist-es/rateLimits/helpers/processStreamRateLimitException.js +59 -0
- package/dist-es/rateLimits/helpers/processStreamRateLimitHeaders.js +46 -0
- package/dist-es/rateLimits/helpers/rateLimitSnapshot.js +26 -0
- package/dist-es/rateLimits/helpers/registerStreamBudgetInterceptors.js +44 -0
- package/dist-es/rateLimits/helpers/registerStreamInterceptors.js +54 -0
- package/dist-es/rateLimits/helpers/splitCallArgsAndOptions.js +17 -0
- package/dist-es/rateLimits/index.js +4 -0
- package/dist-es/rateLimits/types.js +1 -0
- package/dist-es/rateLimits/withStreamRateLimitOptions.js +6 -0
- package/dist-es/streamRateLimitHandlingProxy/constants.js +33 -0
- package/dist-es/streamRateLimitHandlingProxy/createRateLimitedStreamProxy.js +88 -0
- package/dist-es/streamRateLimitHandlingProxy/generatedMethodNames.js +317 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/RedisCooldown.js +170 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/createRateLimitExceededException.js +28 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/executeWithRateLimitHandling.js +140 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/headerUtils.js +34 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/isStreamChannelLike.js +19 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/processStreamBudgetHeaders.js +89 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/processStreamRateLimitException.js +59 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/processStreamRateLimitHeaders.js +68 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/rateLimitSnapshot.js +26 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/registerStreamInterceptors.js +54 -0
- package/dist-es/streamRateLimitHandlingProxy/helpers/splitCallArgsAndOptions.js +17 -0
- package/dist-es/streamRateLimitHandlingProxy/index.js +2 -0
- package/dist-es/streamRateLimitHandlingProxy/types.js +1 -0
- package/dist-es/streamRateLimitHandlingProxy/withStreamRateLimitOptions.js +6 -0
- package/dist-types/index.d.ts +2 -1
- package/dist-types/rateLimits/RedisCooldown.d.ts +7 -0
- package/dist-types/rateLimits/constants.d.ts +29 -0
- package/dist-types/rateLimits/createRateLimitedStreamProxy.d.ts +11 -0
- package/dist-types/rateLimits/exception/createRateLimitExceededException.d.ts +12 -0
- package/dist-types/rateLimits/exception/processStreamRateLimitException.d.ts +8 -0
- package/dist-types/rateLimits/executeStreamRequestWithRateLimitHandling.d.ts +8 -0
- package/dist-types/rateLimits/generatedMethodNames.d.ts +11 -0
- package/dist-types/rateLimits/helpers/RedisCooldown.d.ts +11 -0
- package/dist-types/rateLimits/helpers/createRateLimitExceededException.d.ts +10 -0
- package/dist-types/rateLimits/helpers/executeWithRateLimitHandling.d.ts +2 -0
- package/dist-types/rateLimits/helpers/headerUtils.d.ts +4 -0
- package/dist-types/rateLimits/helpers/isStreamChannelLike.d.ts +6 -0
- package/dist-types/rateLimits/helpers/processStreamBudgetHeaders.d.ts +9 -0
- package/dist-types/rateLimits/helpers/processStreamRateLimitException.d.ts +3 -0
- package/dist-types/rateLimits/helpers/processStreamRateLimitHeaders.d.ts +6 -0
- package/dist-types/rateLimits/helpers/rateLimitSnapshot.d.ts +3 -0
- package/dist-types/rateLimits/helpers/registerStreamBudgetInterceptors.d.ts +2 -0
- package/dist-types/rateLimits/helpers/registerStreamInterceptors.d.ts +2 -0
- package/dist-types/rateLimits/helpers/splitCallArgsAndOptions.d.ts +6 -0
- package/dist-types/rateLimits/index.d.ts +4 -0
- package/dist-types/rateLimits/types.d.ts +68 -0
- package/dist-types/rateLimits/withStreamRateLimitOptions.d.ts +6 -0
- package/dist-types/streamRateLimitHandlingProxy/constants.d.ts +29 -0
- package/dist-types/streamRateLimitHandlingProxy/createRateLimitedStreamProxy.d.ts +11 -0
- package/dist-types/streamRateLimitHandlingProxy/generatedMethodNames.d.ts +11 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/RedisCooldown.d.ts +11 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/createRateLimitExceededException.d.ts +10 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/executeWithRateLimitHandling.d.ts +2 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/headerUtils.d.ts +4 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/isStreamChannelLike.d.ts +6 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/processStreamBudgetHeaders.d.ts +9 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/processStreamRateLimitException.d.ts +3 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/processStreamRateLimitHeaders.d.ts +6 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/rateLimitSnapshot.d.ts +3 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/registerStreamInterceptors.d.ts +2 -0
- package/dist-types/streamRateLimitHandlingProxy/helpers/splitCallArgsAndOptions.d.ts +6 -0
- package/dist-types/streamRateLimitHandlingProxy/index.d.ts +2 -0
- package/dist-types/streamRateLimitHandlingProxy/types.d.ts +68 -0
- package/dist-types/streamRateLimitHandlingProxy/withStreamRateLimitOptions.d.ts +6 -0
- package/dist-types/types.d.ts +1 -1
- package/package.json +9 -1
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
import { executeWithRateLimitHandling } from './helpers/executeWithRateLimitHandling';
|
|
3
|
+
import { isStreamChannelLike } from './helpers/isStreamChannelLike';
|
|
4
|
+
import { registerStreamInterceptors } from './helpers/registerStreamInterceptors';
|
|
5
|
+
import { splitCallArgsAndOptions } from './helpers/splitCallArgsAndOptions';
|
|
6
|
+
const STREAM_SYNC_METHODS = new Set(STREAM_SYNC_METHOD_NAMES);
|
|
7
|
+
const STREAM_CHANNEL_RESULT_METHODS = new Set(STREAM_CHANNEL_RESULT_METHOD_NAMES);
|
|
8
|
+
const STREAM_SYNC_CHANNEL_RESULT_METHODS = new Set(STREAM_SYNC_CHANNEL_RESULT_METHOD_NAMES);
|
|
9
|
+
const CHANNEL_SYNC_METHODS = new Set(CHANNEL_SYNC_METHOD_NAMES);
|
|
10
|
+
const CHANNEL_SYNC_PROXY_RESULT_METHODS = new Set(CHANNEL_SYNC_PROXY_RESULT_METHOD_NAMES);
|
|
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
|
+
}
|
|
24
|
+
if (Array.isArray(result)) {
|
|
25
|
+
return result.map((item) => normalizeStreamCallResult(item, dependencies));
|
|
26
|
+
}
|
|
27
|
+
if (!isStreamChannelLike(result)) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
return wrapTarget(result, dependencies);
|
|
31
|
+
}
|
|
32
|
+
function createRateLimitedMethodWrapper(target, value, operation, dependencies, wrapAsChannelResult) {
|
|
33
|
+
return async (...args) => {
|
|
34
|
+
const { callArgs, callOptions } = splitCallArgsAndOptions(args);
|
|
35
|
+
const result = await executeWithRateLimitHandling(() => Reflect.apply(value, target, callArgs), operation, dependencies.context, {
|
|
36
|
+
...DEFAULT_OPTIONS,
|
|
37
|
+
...callOptions,
|
|
38
|
+
});
|
|
39
|
+
return normalizeStreamCallResult(result, dependencies, wrapAsChannelResult);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function wrapTarget(target, dependencies) {
|
|
43
|
+
const cachedObject = dependencies.cache.get(target);
|
|
44
|
+
if (cachedObject) {
|
|
45
|
+
return cachedObject;
|
|
46
|
+
}
|
|
47
|
+
const proxy = new Proxy(target, {
|
|
48
|
+
get: (originalTarget, property, receiver) => {
|
|
49
|
+
const value = Reflect.get(originalTarget, property, receiver);
|
|
50
|
+
if (typeof property !== 'string' || typeof value !== 'function') {
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
if (STREAM_SYNC_CHANNEL_RESULT_METHODS.has(property)) {
|
|
54
|
+
return (...args) => {
|
|
55
|
+
const { callArgs } = splitCallArgsAndOptions(args);
|
|
56
|
+
const result = Reflect.apply(value, originalTarget, callArgs);
|
|
57
|
+
return normalizeStreamCallResult(result, dependencies, true);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (CHANNEL_SYNC_PROXY_RESULT_METHODS.has(property)) {
|
|
61
|
+
return (...args) => {
|
|
62
|
+
const { callArgs } = splitCallArgsAndOptions(args);
|
|
63
|
+
const result = Reflect.apply(value, originalTarget, callArgs);
|
|
64
|
+
if (typeof result === 'object' && result !== null) {
|
|
65
|
+
return wrapTarget(result, dependencies);
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (STREAM_SYNC_METHODS.has(property) || CHANNEL_SYNC_METHODS.has(property)) {
|
|
71
|
+
return (...args) => {
|
|
72
|
+
const { callArgs } = splitCallArgsAndOptions(args);
|
|
73
|
+
return Reflect.apply(value, originalTarget, callArgs);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return createRateLimitedMethodWrapper(originalTarget, value, property, dependencies, STREAM_CHANNEL_RESULT_METHODS.has(property));
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
dependencies.cache.set(target, proxy);
|
|
80
|
+
return proxy;
|
|
81
|
+
}
|
|
82
|
+
export function createRateLimitedStreamProxy(stream, context) {
|
|
83
|
+
registerStreamInterceptors(stream, context);
|
|
84
|
+
return wrapTarget(stream, {
|
|
85
|
+
context,
|
|
86
|
+
cache: new WeakMap(),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
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'];
|
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
}
|