-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRequestHandler.ts
570 lines (499 loc) · 18.3 KB
/
RequestHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* eslint-disable no-async-promise-executor */
import fs = require("fs");
import path = require("path");
import { EventEmitter } from "events";
import crypto = require("crypto");
import Endpoints = require("./Endpoints");
const { version } = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json"), { encoding: "utf8" })); // otherwise, the json was included in the build
import Constants = require("./Constants");
export type HTTPMethod = "get" | "post" | "patch" | "head" | "put" | "delete" | "connect" | "options" | "trace";
export type RESTPostAPIAttachmentsRefreshURLsResult = {
refreshed_urls: Array<{
original: string;
refreshed: string;
}>;
}
export type RatelimitInfo = {
message: string;
retry_after: number;
global: boolean;
code?: number;
}
/**
* Interface for Queue types to implement
* @since 0.12.0
*/
export interface Queue {
/**
* Array of functions waiting to be executed
*/
calls: Array<() => any>;
/**
* If the queue is currently executing functions
*/
running: boolean;
/**
* If the queue is blocked from executing functions
*/
blocked: boolean;
/**
* Queue a function to be executed
* @since 0.12.0
* @param fn function to be executed
* @returns Result of the function if any
*/
enqueue<T>(fn: () => T): Promise<T>;
/**
* Set if this queue should be blocked from executing functions
* @since 0.12.0
* @param blocked If this queue should be blocked from executing functions
*/
setBlocked(blocked: boolean): void;
/**
* @since 0.12.0
* Drop the queue of functions
*/
drop(): void;
}
// const applicationJSONRegex = /application\/json/;
const routeRegex = /\/([a-z-]+)\/(?:\d+)/g;
const reactionsRegex = /\/reactions\/[^/]+/g;
const reactionsUserRegex = /\/reactions\/:id\/[^/]+/g;
const webhooksRegex = /^\/webhooks\/(\d+)\/[A-Za-z0-9-_]{64,}/;
const isMessageEndpointRegex = /\/messages\/:id$/;
const isGuildChannelsRegex = /\/guilds\/\d+\/channels$/;
const disallowedBodyMethods = new Set(["head", "get", "delete"]);
/**
* @since 0.3.0
*/
export class DiscordAPIError extends Error {
public code: number;
public httpStatus: number;
public constructor(public path: string, error: { message?: string; code?: number; }, public method: string, status: number) {
super();
this.name = "DiscordAPIError";
this.message = error.message ?? String(error);
this.code = error.code ?? 4000;
this.httpStatus = status;
}
}
/**
* A structure to queue (async) functions to run one at a time, waiting for each one to finish before continuing
* @since 0.12.0
* @protected
*/
export class AsyncSequentialQueue implements Queue {
public calls: Array<() => any> = [];
private _blocked = false;
private _running = false;
public get blocked(): boolean {
return this._blocked;
}
public get running(): boolean {
return this._running;
}
public enqueue<T>(fn: () => T): Promise<T> {
return new Promise((resolve, reject) => {
const wrapped = async () => {
try {
resolve(await fn());
} catch (e) {
reject(e as Error);
}
}
this.calls.push(wrapped);
this._tryRun();
});
}
public setBlocked(blocked: boolean): void {
this._blocked = blocked;
this._tryRun();
}
public drop(): void {
this.calls.length = 0;
this._running = false;
}
private _tryRun(): void {
if (this._blocked || this._running) return;
this._running = true;
this._next();
}
private async _next(): Promise<void> {
if (this._blocked || !this._running) return
const cb = this.calls.shift();
await cb?.();
if (this.calls.length && !this._blocked) setImmediate(() => this._next());
else this._running = false;
}
}
/**
* Ratelimiter used for handling the ratelimits imposed by the rest api
* @since 0.1.0
* @protected
*/
export class Ratelimiter<B extends typeof GlobalBucket = typeof GlobalBucket> {
/**
* A Map of Buckets keyed by route keys that store rate limit info
*/
public buckets = new Map<string, InstanceType<B>>();
/**
* The bucket that limits how many requests per second you can make globally
*/
public globalBucket = new LocalBucket(Constants.GLOBAL_REQUESTS_PER_SECOND, 1000);
/**
* If you're being globally rate limited
*/
public get global() {
return this.globalBucket.remaining === 0;
}
/**
* Construct a new Ratelimiter
* @param BucketConstructor The constructor function to call new on when creating buckets to cache and use
*/
public constructor(public BucketConstructor: B = GlobalBucket as B) {}
/**
* Returns a key for saving ratelimits for routes
* (Taken from https://github.com/abalabahaha/eris/blob/master/lib/rest/RequestHandler.js) -> I luv u abal <3
* @since 0.1.0
* @param url url to reduce to a key something like /channels/266277541646434305/messages/266277541646434305/
* @param method method of the request, usual http methods like get, etc.
* @returns reduced url: /channels/266277541646434305/messages/:id/
*/
public routify(url: string, method: string): string {
let route = url.replace(routeRegex, function (match, p: string) {
return p === "channels" || p === "guilds" || p === "webhooks" ? match : `/${p}/:id`;
}).replace(reactionsRegex, "/reactions/:id").replace(reactionsUserRegex, "/reactions/:id/:userId").replace(webhooksRegex, "/webhooks/$1/:token");
if (method === "DELETE" && isMessageEndpointRegex.test(route)) route = method + route;
else if (method === "GET" && isGuildChannelsRegex.test(route)) route = "/guilds/:id/channels";
if (method === "PUT" || method === "DELETE") {
const index = route.indexOf("/reactions");
if (index !== -1) route = "MODIFY" + route.slice(0, index + 10);
}
return route;
}
/**
* Queue a rest call to be executed
* @since 0.1.0
* @param fn function to call once the ratelimit is ready
* @param url Endpoint of the request
* @param method Http method used by the request
*/
public queue<T>(fn: (bucket: InstanceType<B>) => T, url: string, method: string): Promise<T> {
const routeKey = this.routify(url, method);
let bucket = this.buckets.get(routeKey);
if (!bucket) {
bucket = new this.BucketConstructor(this, routeKey) as InstanceType<B>;
this.buckets.set(routeKey, bucket);
}
return new Promise<T>((resolve, reject) => {
this.globalBucket.enqueue(() => {
bucket.enqueue(fn).then(resolve).catch(reject);
});
});
}
/**
* Set if this Ratelimiter is hitting a global ratelimit for `ms` duration
* @param ms How long in milliseconds this Ratelimiter is globally ratelimited for
*/
public setGlobal(ms: number): void {
this.globalBucket.remaining = 0;
this.globalBucket.queue.setBlocked(true);
this.globalBucket.makeResetTimeout(ms);
}
}
/**
* Bucket used for saving ratelimits
* @since 0.1.0
* @protected
*/
export class LocalBucket {
/**
* The backing Queue of functions passed to this bucket
*/
public queue: Queue = new AsyncSequentialQueue();
/**
* Remaining amount of executions during the current timeframe
*/
public remaining: number;
private resetTimeout: NodeJS.Timeout | null = null;
/**
* Create a new base bucket
* @param limit Number of functions that may be executed during the timeframe set in reset
* @param reset Timeframe in milliseconds until the ratelimit resets after first
* @param remaining Remaining amount of executions during the current timeframe
*/
public constructor(public limit = 5, public reset = 5000, remaining?: number) {
this.remaining = remaining ?? limit;
}
/**
* Queue a function to be executed
* @since 0.12.0
* @param fn function to be executed
* @returns Result of the function if any
*/
public enqueue<T>(fn: (bkt: this) => T): Promise<T> {
return this.queue.enqueue<T>(() => {
this.remaining--;
if (this.remaining === 0) this.queue.setBlocked(true);
if (!this.resetTimeout) this.makeResetTimeout(this.reset);
return fn(this);
});
}
/**
* Reset/make the timeout of this bucket
* @since 0.8.3
* @param ms Timeframe in milliseconds until the ratelimit resets after
*/
public makeResetTimeout(ms: number): void {
if (this.resetTimeout) clearTimeout(this.resetTimeout);
this.resetTimeout = setTimeout(() => this.resetRemaining(), ms).unref();
}
/**
* Reset the remaining tokens to the base limit
* @since 0.1.0
*/
public resetRemaining(): void {
if (this.resetTimeout) clearTimeout(this.resetTimeout);
this.resetTimeout = null;
this.remaining = this.limit;
this.queue.setBlocked(false);
}
/**
* Clear the current queue of events to be sent
* @since 0.1.0
*/
public dropQueue(): void {
this.queue.drop();
}
}
/**
* Extended bucket that respects global ratelimits
* @since 0.10.0
* @protected
*/
export class GlobalBucket extends LocalBucket {
/**
* Create a new bucket that respects global rate limits
* @param ratelimiter ratelimiter used for ratelimiting requests. Assigned by ratelimiter
* @param routeKey Key used internally to routify requests. Assigned by ratelimiter
*/
public constructor(public readonly ratelimiter: Ratelimiter, public readonly routeKey: string, limit = 5, reset = 5000, remaining?: number) {
super(limit, reset, remaining);
}
/**
* Reset the remaining tokens to the base limit
* @since 0.10.0
*/
public resetRemaining(): void {
if (!this.queue.calls.length) this.ratelimiter.buckets.delete(this.routeKey);
super.resetRemaining();
}
}
export type HandlerEvents = {
request: [string, { endpoint: string, method: string, dataType: "json" | "multipart", data: any; }];
done: [string, Response];
requestError: [string, Error];
rateLimit: [{ timeout: number; remaining: number; limit: number; method: string; path: string; route: string; }];
}
export interface RequestHandler {
addListener<E extends keyof HandlerEvents>(event: E, listener: (...args: HandlerEvents[E]) => any): this;
emit<E extends keyof HandlerEvents>(event: E, ...args: HandlerEvents[E]): boolean;
eventNames(): Array<keyof HandlerEvents>;
listenerCount(event: keyof HandlerEvents): number;
listeners(event: keyof HandlerEvents): Array<(...args: Array<any>) => any>;
off<E extends keyof HandlerEvents>(event: E, listener: (...args: HandlerEvents[E]) => any): this;
on<E extends keyof HandlerEvents>(event: E, listener: (...args: HandlerEvents[E]) => any): this;
once<E extends keyof HandlerEvents>(event: E, listener: (...args: HandlerEvents[E]) => any): this;
prependListener<E extends keyof HandlerEvents>(event: E, listener: (...args: HandlerEvents[E]) => any): this;
prependOnceListener<E extends keyof HandlerEvents>(event: E, listener: (...args: HandlerEvents[E]) => any): this;
rawListeners(event: keyof HandlerEvents): Array<(...args: Array<any>) => any>;
removeAllListeners(event?: keyof HandlerEvents): this;
removeListener<E extends keyof HandlerEvents>(event: E, listener: (...args: HandlerEvents[E]) => any): this;
}
/**
* Request Handler class
* @since 0.1.0
*/
export class RequestHandler extends EventEmitter {
public options: {
/** The base URL to use when making requests. Defaults to https://discord.com */
baseHost: string;
/** The base path of the base URL to use for the API. Defaults to /api/v${Constants.REST_API_VERSION} */
baseURL: string;
/** If rate limit buckets should be ignored */
bypassBuckets: boolean;
/** If failed requests that can be retried should be retried, up to retryLimit times. */
retryFailed: boolean;
/** How many times requests should be retried if they fail and can be retried. */
retryLimit: number;
headers: { Authorization?: string; "User-Agent": string; }
};
public latency: number;
public apiURL: string;
/**
* Create a new request handler
* @param ratelimiter ratelimiter to use for ratelimiting requests
* @param options options
*/
public constructor(public ratelimiter: Ratelimiter, options?: { token?: string; } & Partial<Omit<RequestHandler["options"], "headers">>) {
super();
this.options = {
baseHost: options?.baseHost ?? Endpoints.BASE_HOST,
baseURL: options?.baseURL ?? Endpoints.BASE_URL,
bypassBuckets: options?.bypassBuckets ?? false,
retryFailed: options?.retryFailed ?? false,
retryLimit: options?.retryLimit ?? Constants.DEFAULT_RETRY_LIMIT,
headers: {
"User-Agent": `Discordbot (https://github.com/DasWolke/SnowTransfer, ${version}) Node.js/${process.version}`
}
};
if (options?.token) this.options.headers.Authorization = options.token;
this.apiURL = this.options.baseHost + Endpoints.BASE_URL;
this.latency = 500;
}
/**
* Request a route from the discord api
* @since 0.1.0
* @param endpoint endpoint to request
* @param params URL query parameters to add on to the URL
* @param method http method to use
* @param dataType type of the data being sent
* @param data data to send, if any
* @returns Result of the request
*/
public request(endpoint: string, params: Record<string, any> | undefined, method: HTTPMethod, dataType: "json", data?: any, extraHeaders?: Record<string, string>, retries?: number): Promise<any>
public request(endpoint: string, params: Record<string, any> | undefined, method: HTTPMethod, dataType: "multipart", data?: FormData, extraHeaders?: Record<string, string>, retries?: number): Promise<any>
public request(endpoint: string, params: Record<string, any> = {}, method: HTTPMethod, dataType: "json" | "multipart", data?: any, extraHeaders?: Record<string, string>, retries = this.options.retryLimit): Promise<any> {
// const stack = new Error().stack as string;
return new Promise(async (resolve, reject) => {
const fn = async (bkt?: GlobalBucket | undefined) => {
const reqId = crypto.randomBytes(20).toString("hex");
try {
this.emit("request", reqId, { endpoint, method: method.toUpperCase(), dataType, data: data ?? {} });
const before = Date.now();
let response: Response;
switch (dataType) {
case "json":
response = await this._request(endpoint, params, method, data, extraHeaders);
break;
case "multipart":
if (!data) throw new Error("No multipart data");
response = await this._multiPartRequest(endpoint, params, method, data, extraHeaders);
break;
default:
throw new Error("Forbidden dataType. Use json or multipart or ensure multipart has FormData");
}
this.latency = Date.now() - before;
if (bkt) this._applyRatelimitHeaders(bkt, response.headers);
if (response.status && !Constants.OK_STATUS_CODES.has(response.status) && response.status !== 429) {
if (this.options.retryFailed && !Constants.DO_NOT_RETRY_STATUS_CODES.has(response.status) && retries !== 0) return this.request(endpoint, params, method, dataType as any, data, extraHeaders, retries--).then(resolve).catch(reject);
throw new DiscordAPIError(
endpoint,
{ message: await response.text() },
method.toUpperCase(),
response.status
);
}
if (response.status === 429) {
const b = await response.json() as RatelimitInfo; // Discord says it will be a JSON, so if there's an error, sucks
if (b.global) this.ratelimiter.setGlobal(b.retry_after * 1000);
else bkt?.queue.setBlocked(true);
this.emit("rateLimit", {
timeout: bkt?.reset ?? b.retry_after * 1000,
remaining: 0,
limit: bkt?.limit ?? Infinity,
method: method.toUpperCase(),
path: endpoint,
route: bkt?.routeKey ?? this.ratelimiter.routify(endpoint, method.toUpperCase())
});
throw new DiscordAPIError(endpoint, { message: b.message, code: b.code ?? 429 }, method.toUpperCase(), response.status);
}
this.emit("done", reqId, response);
if (response.body) {
let b: any;
try {
b = await response.json();
} catch {
return resolve(undefined);
}
return resolve(b);
} else return resolve(undefined);
} catch (error) {
// if (error && error.stack) error.stack = stack;
this.emit("requestError", reqId, error);
return reject(error as Error);
}
};
if (this.options.bypassBuckets) fn();
else this.ratelimiter.queue(fn, endpoint, method.toUpperCase());
});
}
/**
* Apply the received ratelimit headers to the ratelimit bucket
* @since 0.1.0
* @param bkt Ratelimit bucket to apply the headers to
* @param headers Http headers received from discord
*/
private _applyRatelimitHeaders(bkt: GlobalBucket, headers: Headers): void {
const remaining = headers.get("x-ratelimit-remaining");
const limit = headers.get("x-ratelimit-limit");
const resetAfter = headers.get("x-ratelimit-reset-after");
if (remaining) bkt.remaining = parseInt(remaining);
if (limit) bkt.limit = parseInt(limit);
if (resetAfter) {
bkt.reset = parseFloat(resetAfter) * 1000;
bkt.makeResetTimeout(bkt.reset);
}
}
/**
* Execute a normal json request
* @since 0.1.0
* @param endpoint Endpoint to use
* @param params URL query parameters to add on to the URL
* @param data Data to send
* @returns Result of the request
*/
private async _request(endpoint: string, params: Record<string, any> = {}, method: HTTPMethod, data?: any, extraHeaders?: Record<string, string>): Promise<Response> {
const headers = { ...this.options.headers, ...extraHeaders };
if (typeof data !== "string" && data?.reason) {
headers["X-Audit-Log-Reason"] = encodeURIComponent(data.reason);
delete data.reason;
}
let body: string | undefined = undefined;
if (!disallowedBodyMethods.has(method)) {
if (typeof data === "object") body = JSON.stringify(data);
else body = String(data);
}
headers["Content-Type"] = "application/json";
return fetch(`${this.apiURL}${endpoint}${appendQuery(params)}`, {
method: method.toUpperCase(),
headers,
body
});
}
/**
* Execute a multipart/form-data request
* @since 0.1.0
* @param endpoint Endpoint to use
* @param params URL query parameters to add on to the URL
* @param method Http Method to use
* @param data data to send
* @returns Result of the request
*/
private async _multiPartRequest(endpoint: string, params: Record<string, any> = {}, method: HTTPMethod, data: FormData, extraHeaders?: Record<string, string>): Promise<Response> {
const headers = { ...this.options.headers, ...extraHeaders };
return fetch(`${this.apiURL}${endpoint}${appendQuery(params)}`, {
method: method.toUpperCase(),
headers,
body: data
});
}
}
function appendQuery(query: Record<string, any>): string {
let count = 0;
for (const [key, value] of Object.entries(query)) {
if (value == undefined) delete query[key];
else count++;
}
return count > 0 ? `?${new URLSearchParams(query).toString()}` : "";
}