@discordeno/rest 19.0.0-next.cebe1f4 → 19.0.0-next.d0db342
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/invalidBucket.js +6 -6
- package/dist/invalidBucket.js.map +1 -1
- package/dist/manager.d.ts.map +1 -1
- package/dist/manager.js +54 -19
- package/dist/manager.js.map +1 -1
- package/dist/routes.js +1 -1
- package/dist/routes.js.map +1 -1
- package/dist/types.d.ts +7 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +4 -5
package/dist/invalidBucket.js
CHANGED
|
@@ -21,7 +21,7 @@ import { delay, logger } from '@discordeno/utils';
|
|
|
21
21
|
processing: false,
|
|
22
22
|
waiting: [],
|
|
23
23
|
requestsAllowed: function() {
|
|
24
|
-
if (bucket.resetAt !== undefined && Date.now()
|
|
24
|
+
if (bucket.resetAt !== undefined && Date.now() >= bucket.resetAt) {
|
|
25
25
|
bucket.invalidRequests = 0;
|
|
26
26
|
bucket.resetAt = Date.now() + bucket.interval;
|
|
27
27
|
}
|
|
@@ -45,14 +45,13 @@ import { delay, logger } from '@discordeno/utils';
|
|
|
45
45
|
},
|
|
46
46
|
processWaiting: async function() {
|
|
47
47
|
// If already processing, that loop will handle all waiting requests.
|
|
48
|
-
if (bucket.processing)
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
48
|
+
if (bucket.processing) return;
|
|
51
49
|
// Mark as processing so other loops don't start
|
|
52
50
|
bucket.processing = true;
|
|
53
51
|
while(bucket.waiting.length > 0){
|
|
54
|
-
logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length}
|
|
55
|
-
if (bucket.resetAt !== undefined
|
|
52
|
+
logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} pending requests to be made. ${JSON.stringify(bucket)}`);
|
|
53
|
+
if (!bucket.isRequestAllowed() && bucket.resetAt !== undefined) {
|
|
54
|
+
logger.warn(`[InvalidBucket] processing waiting queue is now paused until more requests are available. ${bucket.waiting.length} pending requests. ${JSON.stringify(bucket)}`);
|
|
56
55
|
await delay(bucket.resetAt - Date.now());
|
|
57
56
|
}
|
|
58
57
|
bucket.activeRequests += 1;
|
|
@@ -74,6 +73,7 @@ import { delay, logger } from '@discordeno/utils';
|
|
|
74
73
|
bucket.resetAt = Date.now() + bucket.interval;
|
|
75
74
|
}
|
|
76
75
|
bucket.invalidRequests += 1;
|
|
76
|
+
logger.warn(`[InvalidBucket] an invalid request was made. Increasing invalidRequests count to ${bucket.invalidRequests}`);
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
79
|
return bucket;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/invalidBucket.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\n\n/**\n * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.\n * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.\n *\n * @param options The options used to configure this bucket.\n * @returns RefillingBucket\n */\nexport function createInvalidRequestBucket(options: InvalidRequestBucketOptions): InvalidRequestBucket {\n const bucket: InvalidRequestBucket = {\n invalidRequests: options.current ?? 0,\n max: options.max ?? 10000,\n interval: options.interval ?? 600_000, // 10 minutes\n resetAt: options.resetAt,\n safety: options.safety ?? 1,\n errorStatuses: options.errorStatuses ?? [401, 403, 429],\n activeRequests: options.requested ?? 0,\n processing: false,\n\n waiting: [],\n\n requestsAllowed: function () {\n if (bucket.resetAt !== undefined && Date.now()
|
|
1
|
+
{"version":3,"sources":["../src/invalidBucket.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\n\n/**\n * A invalid request bucket is used in a similar manner as a leaky bucket but a invalid request bucket can be refilled as needed.\n * It's purpose is to make sure the bot does not hit the limit to getting a 1 hr ban.\n *\n * @param options The options used to configure this bucket.\n * @returns RefillingBucket\n */\nexport function createInvalidRequestBucket(options: InvalidRequestBucketOptions): InvalidRequestBucket {\n const bucket: InvalidRequestBucket = {\n invalidRequests: options.current ?? 0,\n max: options.max ?? 10000,\n interval: options.interval ?? 600_000, // 10 minutes\n resetAt: options.resetAt,\n safety: options.safety ?? 1,\n errorStatuses: options.errorStatuses ?? [401, 403, 429],\n activeRequests: options.requested ?? 0,\n processing: false,\n\n waiting: [],\n\n requestsAllowed: function () {\n if (bucket.resetAt !== undefined && Date.now() >= bucket.resetAt) {\n bucket.invalidRequests = 0\n bucket.resetAt = Date.now() + bucket.interval\n }\n\n return bucket.max - bucket.invalidRequests - bucket.activeRequests - bucket.safety\n },\n\n isRequestAllowed: function () {\n return bucket.requestsAllowed() > 0\n },\n\n waitUntilRequestAvailable: async function () {\n // eslint-disable-next-line no-async-promise-executor\n return await new Promise(async (resolve) => {\n // If whatever amount of requests is left is more than the safety margin, allow the request\n if (bucket.isRequestAllowed()) {\n bucket.activeRequests += 1\n resolve()\n } else {\n bucket.waiting.push(resolve)\n await bucket.processWaiting()\n }\n })\n },\n\n processWaiting: async function () {\n // If already processing, that loop will handle all waiting requests.\n if (bucket.processing) return\n\n // Mark as processing so other loops don't start\n bucket.processing = true\n\n while (bucket.waiting.length > 0) {\n logger.info(`[InvalidBucket] processing waiting queue while loop ran with ${bucket.waiting.length} pending requests to be made. ${JSON.stringify(bucket)}`)\n\n if (!bucket.isRequestAllowed() && bucket.resetAt !== undefined) {\n logger.warn(`[InvalidBucket] processing waiting queue is now paused until more requests are available. ${bucket.waiting.length} pending requests. ${JSON.stringify(bucket)}`)\n await delay(bucket.resetAt - Date.now())\n }\n\n bucket.activeRequests += 1\n // Resolve the next item in the queue\n bucket.waiting.shift()?.()\n }\n\n // Mark as false so next pending request can be triggered by new loop.\n bucket.processing = false\n },\n\n handleCompletedRequest: function (code, sharedScope) {\n // Since request is complete, we can remove one from requested.\n bucket.activeRequests -= 1\n // Since it is as a valid request, we don't need to do anything\n if (!bucket.errorStatuses.includes(code)) return\n // Shared scope is not considered invalid\n if (code === 429 && sharedScope) return\n\n // INVALID REQUEST WAS MADE\n if (bucket.resetAt === undefined) {\n bucket.resetAt = Date.now() + bucket.interval\n }\n\n bucket.invalidRequests += 1\n logger.warn(`[InvalidBucket] an invalid request was made. Increasing invalidRequests count to ${bucket.invalidRequests}`)\n },\n }\n\n return bucket\n}\n\nexport interface InvalidRequestBucketOptions {\n /** current invalid amount */\n current?: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max?: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval?: number\n /** When the timeout for the bucket has started at. */\n resetAt?: number\n /** how safe to be from max. Defaults to 1 */\n safety?: number\n /** The request statuses that count as an invalid request. */\n errorStatuses?: number[]\n /** The amount of requests that were requested from this bucket. */\n requested?: number\n}\n\nexport interface InvalidRequestBucket {\n /** current invalid amount */\n invalidRequests: number\n /** max invalid requests allowed until ban. Defaults to 10,000 */\n max: number\n /** The time that discord allows to make the max number of invalid requests. Defaults to 10 minutes */\n interval: number\n /** When the timeout for this bucket has started at. */\n resetAt: number | undefined\n /** how safe to be from max. Defaults to 1 */\n safety: number\n /** The request statuses that count as an invalid request. */\n errorStatuses: number[]\n /** The amount of requests that were requested from this bucket. */\n activeRequests: number\n /** The requests that are currently pending. */\n waiting: Array<(value: void | PromiseLike<void>) => void>\n /** Whether or not the waiting queue is already processing. */\n processing: boolean\n\n /** Gives the number of requests that are currently allowed. */\n requestsAllowed: () => number\n /** Checks if a request is allowed at this time. */\n isRequestAllowed: () => boolean\n /** Waits until a request is available */\n waitUntilRequestAvailable: () => Promise<void>\n /** Begins processing the waiting queue of requests. */\n processWaiting: () => Promise<void>\n /** Handler for whenever a request is validated. This should update the requested values or trigger any other necessary stuff. */\n handleCompletedRequest: (code: number, sharedScope: boolean) => void\n}\n"],"names":["delay","logger","createInvalidRequestBucket","options","bucket","invalidRequests","current","max","interval","resetAt","safety","errorStatuses","activeRequests","requested","processing","waiting","requestsAllowed","undefined","Date","now","isRequestAllowed","waitUntilRequestAvailable","Promise","resolve","push","processW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/manager.ts"],"names":[],"mappings":"AAkDA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAkDA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAKrH,eAAO,MAAM,mBAAmB,KAAK,CAAA;AACrC,eAAO,MAAM,eAAe,4BAA4B,CAAA;AAExD,eAAO,MAAM,uBAAuB,uBAAuB,CAAA;AAC3D,eAAO,MAAM,2BAA2B,0BAA0B,CAAA;AAClE,eAAO,MAAM,6BAA6B,4BAA4B,CAAA;AACtE,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,wBAAwB,uBAAuB,CAAA;AAC5D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAC1D,eAAO,MAAM,uBAAuB,sBAAsB,CAAA;AAE1D,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CAuqChF"}
|
package/dist/manager.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ import {
|
|
2
|
-
import { calculateBits, camelize, camelToSnakeCase, delay, getBotIdFromToken, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
|
|
3
|
-
import fetch from 'node-fetch';
|
|
1
|
+
/* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ import { calculateBits, camelToSnakeCase, camelize, delay, getBotIdFromToken, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
|
|
4
2
|
import { createInvalidRequestBucket } from './invalidBucket.js';
|
|
5
3
|
import { Queue } from './queue.js';
|
|
4
|
+
import { InteractionResponseTypes } from '@discordeno/types';
|
|
6
5
|
import { createRoutes } from './routes.js';
|
|
7
6
|
// TODO: make dynamic based on package.json file
|
|
8
7
|
const version = '19.0.0-alpha.1';
|
|
@@ -56,20 +55,27 @@ export function createRestManager(options) {
|
|
|
56
55
|
}
|
|
57
56
|
const newObj = {};
|
|
58
57
|
for (const key of Object.keys(obj)){
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
58
|
+
const value = obj[key];
|
|
59
|
+
// Some falsy values should be allowed like null or 0
|
|
60
|
+
if (value !== undefined) {
|
|
61
|
+
switch(key){
|
|
62
|
+
case 'permissions':
|
|
63
|
+
case 'allow':
|
|
64
|
+
case 'deny':
|
|
65
|
+
newObj[key] = calculateBits(value);
|
|
66
|
+
continue;
|
|
67
|
+
case 'defaultMemberPermissions':
|
|
68
|
+
newObj.default_member_permissions = calculateBits(value);
|
|
69
|
+
continue;
|
|
70
|
+
case 'nameLocalizations':
|
|
71
|
+
newObj.name_localizations = value;
|
|
72
|
+
continue;
|
|
73
|
+
case 'descriptionLocalizations':
|
|
74
|
+
newObj.description_localizations = value;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
67
77
|
}
|
|
68
|
-
|
|
69
|
-
newObj.default_member_permissions = calculateBits(obj[key]);
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
|
-
newObj[camelToSnakeCase(key)] = rest.changeToDiscordFormat(obj[key]);
|
|
78
|
+
newObj[camelToSnakeCase(key)] = rest.changeToDiscordFormat(value);
|
|
73
79
|
}
|
|
74
80
|
return newObj;
|
|
75
81
|
}
|
|
@@ -219,8 +225,20 @@ export function createRestManager(options) {
|
|
|
219
225
|
authorization: 'Bot tokenhere'
|
|
220
226
|
}
|
|
221
227
|
});
|
|
222
|
-
const response = await fetch(url, payload)
|
|
228
|
+
const response = await fetch(url, payload).catch(async (error)=>{
|
|
229
|
+
logger.error(error);
|
|
230
|
+
// Mark request and completed
|
|
231
|
+
rest.invalidBucket.handleCompletedRequest(999, false);
|
|
232
|
+
options.reject({
|
|
233
|
+
ok: false,
|
|
234
|
+
status: 999,
|
|
235
|
+
error: 'Possible network or request shape issue occurred. If this is rare, its a network glitch. If it occurs a lot something is wrong.'
|
|
236
|
+
});
|
|
237
|
+
throw error;
|
|
238
|
+
});
|
|
223
239
|
logger.debug(`request fetched from ${url} with status ${response.status} & ${response.statusText}`);
|
|
240
|
+
// Mark request and completed
|
|
241
|
+
rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get(RATE_LIMIT_SCOPE_HEADER) === 'shared');
|
|
224
242
|
// Set the bucket id if it was available on the headers
|
|
225
243
|
const bucketId = rest.processHeaders(rest.simplifyUrl(options.route, options.method), response.headers);
|
|
226
244
|
if (bucketId) options.bucketId = bucketId;
|
|
@@ -247,8 +265,6 @@ export function createRestManager(options) {
|
|
|
247
265
|
return;
|
|
248
266
|
}
|
|
249
267
|
options.retryCount += 1;
|
|
250
|
-
// Rate limited, add back to queue
|
|
251
|
-
rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get(RATE_LIMIT_SCOPE_HEADER) === 'shared');
|
|
252
268
|
const resetAfter = response.headers.get(RATE_LIMIT_RESET_AFTER_HEADER);
|
|
253
269
|
if (resetAfter) await delay(Number(resetAfter) * 1000);
|
|
254
270
|
// process the response to prevent mem leak
|
|
@@ -1088,6 +1104,25 @@ export function createRestManager(options) {
|
|
|
1088
1104
|
return await rest.put(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
|
|
1089
1105
|
body
|
|
1090
1106
|
});
|
|
1107
|
+
},
|
|
1108
|
+
preferSnakeCase (enabled) {
|
|
1109
|
+
const camelizer = enabled ? (x)=>x : camelize;
|
|
1110
|
+
rest.get = async (url, options)=>{
|
|
1111
|
+
return camelizer(await rest.makeRequest('GET', url, options));
|
|
1112
|
+
};
|
|
1113
|
+
rest.post = async (url, options)=>{
|
|
1114
|
+
return camelizer(await rest.makeRequest('POST', url, options));
|
|
1115
|
+
};
|
|
1116
|
+
rest.delete = async (url, options)=>{
|
|
1117
|
+
camelizer(await rest.makeRequest('DELETE', url, options));
|
|
1118
|
+
};
|
|
1119
|
+
rest.patch = async (url, options)=>{
|
|
1120
|
+
return camelizer(await rest.makeRequest('PATCH', url, options));
|
|
1121
|
+
};
|
|
1122
|
+
rest.put = async (url, options)=>{
|
|
1123
|
+
return camelizer(await rest.makeRequest('PUT', url, options));
|
|
1124
|
+
};
|
|
1125
|
+
return rest;
|
|
1091
1126
|
}
|
|
1092
1127
|
};
|
|
1093
1128
|
return rest;
|