@discordeno/rest 19.0.0-next.40c19da → 19.0.0-next.4287398

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/queue.js CHANGED
@@ -11,6 +11,7 @@ export class Queue {
11
11
  /** Whether the first request is pending. */ this.firstRequest = false;
12
12
  /** When requests started being made to determine when the interval will reset it. */ this.frozenAt = 0;
13
13
  /** The time in milliseconds to wait before deleting this queue if it is empty. Defaults to 60000(one minute). */ this.deleteQueueDelay = 60000;
14
+ /** The authentication header used for the OAuth2 request. Defaults to an empty string for non-OAuth2 requests */ this.authentication = '';
14
15
  this.rest = rest;
15
16
  this.url = options.url;
16
17
  if (options.interval) this.interval = options.interval;
@@ -18,6 +19,7 @@ export class Queue {
18
19
  if (options.remaining) this.remaining = options.remaining;
19
20
  if (options.timeoutId) this.timeoutId = options.timeoutId;
20
21
  if (options.deleteQueueDelay) this.deleteQueueDelay = options.deleteQueueDelay;
22
+ if (options.authentication) this.authentication = options.authentication;
21
23
  }
22
24
  /** Check if there is any remaining requests that are allowed. */ isRequestAllowed() {
23
25
  return this.remaining > 0;
@@ -41,7 +43,7 @@ export class Queue {
41
43
  // Mark as processing so other loops don't start
42
44
  this.processing = true;
43
45
  while(this.waiting.length > 0){
44
- logger.debug(`[Queue] ${this.url} process waiting while loop ran.`);
46
+ logger.debug(`[Queue] ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url} process waiting while loop ran.`);
45
47
  if (this.isRequestAllowed()) {
46
48
  // Resolve the next item in the queue
47
49
  this.waiting.shift()?.();
@@ -58,7 +60,7 @@ export class Queue {
58
60
  // Mark as processing so other loops don't start
59
61
  this.processingPending = true;
60
62
  while(this.pending.length > 0){
61
- logger.debug(`Queue ${this.url} process pending while loop ran with ${this.pending.length}.`);
63
+ logger.debug(`Queue ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url} process pending while loop ran with ${this.pending.length}.`);
62
64
  if (!this.firstRequest && !this.isRequestAllowed()) {
63
65
  const now = Date.now();
64
66
  const future = this.frozenAt + this.interval;
@@ -67,13 +69,12 @@ export class Queue {
67
69
  }
68
70
  const request = this.pending[0];
69
71
  if (request) {
70
- const basicURL = this.rest.simplifyUrl(request.url, request.method);
71
- // IF THIS URL IS STILL RATE LIMITED, TRY AGAIN
72
+ const basicURL = this.rest.simplifyUrl(request.route, request.method);
72
73
  // If this url is still rate limited, try again
73
- const urlResetIn = this.rest.checkRateLimits(basicURL);
74
+ const urlResetIn = this.rest.checkRateLimits(basicURL, request.requestBodyOptions?.headers);
74
75
  if (urlResetIn) await delay(urlResetIn);
75
76
  // IF A BUCKET EXISTS, CHECK THE BUCKET'S RATE LIMITS
76
- const bucketResetIn = request.bucketId ? this.rest.checkRateLimits(request.bucketId) : false;
77
+ const bucketResetIn = request.bucketId ? this.rest.checkRateLimits(request.bucketId, request.requestBodyOptions?.headers) : false;
77
78
  if (bucketResetIn) await delay(bucketResetIn);
78
79
  this.firstRequest = false;
79
80
  this.remaining--;
@@ -91,7 +92,7 @@ export class Queue {
91
92
  .catch(()=>null);
92
93
  }
93
94
  }
94
- logger.debug(`Queue ${this.url} process pending while loop exited with ${this.pending.length}.`);
95
+ logger.debug(`Queue ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url} process pending while loop exited with ${this.pending.length}.`);
95
96
  // Mark as false so next pending request can be triggered by new loop.
96
97
  this.processingPending = false;
97
98
  this.cleanup();
@@ -121,21 +122,21 @@ export class Queue {
121
122
  this.processPending();
122
123
  return;
123
124
  }
124
- logger.debug(`[Queue] ${this.url}. Delaying delete for ${this.deleteQueueDelay}ms`);
125
+ logger.debug(`[Queue] ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url}. Delaying delete for ${this.deleteQueueDelay}ms`);
125
126
  // Delete in a minute giving a bit of time to allow new requests that may reuse this queue
126
127
  setTimeout(async ()=>{
127
128
  if (!this.isQueueClearable()) {
128
- logger.debug(`[Queue] ${this.url}. is not clearable. Restarting processing of queue.`);
129
+ logger.debug(`[Queue] ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url}. is not clearable. Restarting processing of queue.`);
129
130
  this.processPending();
130
131
  return;
131
132
  }
132
133
  logger.debug(`[Queue] ${this.url}. Deleting`);
133
134
  if (this.timeoutId) clearTimeout(this.timeoutId);
134
135
  // No requests have been requested for this queue so we nuke this queue
135
- this.rest.queues.delete(this.url);
136
+ this.rest.queues.delete(`${this.authentication}${this.url}`);
136
137
  logger.debug(`[Queue] ${this.url}. Deleted! Remaining: (${this.rest.queues.size})`, [
137
- ...this.rest.queues.keys()
138
- ]);
138
+ ...this.rest.queues.values()
139
+ ].map((queue)=>`${queue.isOauth2Queue() ? '' : 'Bearer '}${queue.url}`));
139
140
  if (this.rest.queues.size) this.processPending();
140
141
  }, this.deleteQueueDelay);
141
142
  }
@@ -148,6 +149,9 @@ export class Queue {
148
149
  if (this.processingPending) return false;
149
150
  return true;
150
151
  }
152
+ isOauth2Queue() {
153
+ return this.authentication === '';
154
+ }
151
155
  }
152
156
 
153
157
  //# sourceMappingURL=queue.js.map
package/dist/queue.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/queue.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\nimport type { RestManager, SendRequestOptions } from './types.js'\n\nexport class Queue {\n /** The rest manager */\n rest: RestManager\n /** Amount of requests that have are remaining. Defaults to 1. */\n remaining: number = 1\n /** Max requests for this this. Defaults to 1. */\n max: number = 1\n /** The time that discord allows to make the max number of requests. Defaults to 0 */\n interval: number = 0\n /** timer to reset to 0 */\n timeoutId: NodeJS.Timeout | undefined\n /** The requests that are currently pending. */\n waiting: Array<(value: void | PromiseLike<void>) => void> = []\n /** The requests that are currently pending. */\n pending: SendRequestOptions[] = []\n /** Whether or not the waiting queue is already processing. */\n processing: boolean = false\n /** Whether or not the pending queue is already processing. */\n processingPending: boolean = false\n /** Whether the first request is pending. */\n firstRequest: boolean = false\n /** The url that all the requests in this queue are sent to. */\n url: string\n /** When requests started being made to determine when the interval will reset it. */\n frozenAt: number = 0\n /** The time in milliseconds to wait before deleting this queue if it is empty. Defaults to 60000(one minute). */\n deleteQueueDelay: number = 60000\n\n constructor(rest: RestManager, options: QueueOptions) {\n this.rest = rest\n this.url = options.url\n\n if (options.interval) this.interval = options.interval\n if (options.max) this.max = options.max\n if (options.remaining) this.remaining = options.remaining\n if (options.timeoutId) this.timeoutId = options.timeoutId\n if (options.deleteQueueDelay) this.deleteQueueDelay = options.deleteQueueDelay\n }\n\n /** Check if there is any remaining requests that are allowed. */\n isRequestAllowed(): boolean {\n return this.remaining > 0\n }\n\n /** Pauses the execution until a request is allowed to be made. */\n async waitUntilRequestAvailable(): Promise<void> {\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 (this.isRequestAllowed()) {\n // this.remaining++;\n resolve()\n } else {\n this.waiting.push(resolve)\n await this.processWaiting()\n }\n })\n }\n\n /** Process the queue of requests waiting to be handled. */\n async processWaiting(): Promise<void> {\n // If already processing, that loop will handle all waiting requests.\n if (this.processing) return\n // Mark as processing so other loops don't start\n this.processing = true\n\n while (this.waiting.length > 0) {\n logger.debug(`[Queue] ${this.url} process waiting while loop ran.`)\n if (this.isRequestAllowed()) {\n // Resolve the next item in the queue\n this.waiting.shift()?.()\n } else {\n await delay(1000)\n }\n }\n\n // Mark as false so next pending request can be triggered by new loop.\n this.processing = false\n }\n\n /** Process the queue of all requests pending to be sent. */\n async processPending(): Promise<void> {\n // If already processing, that loop will handle all pending requests.\n if (this.processingPending || !this.pending.length) return\n\n // Mark as processing so other loops don't start\n this.processingPending = true\n\n while (this.pending.length > 0) {\n logger.debug(`Queue ${this.url} process pending while loop ran with ${this.pending.length}.`)\n if (!this.firstRequest && !this.isRequestAllowed()) {\n const now = Date.now()\n const future = this.frozenAt + this.interval\n await delay(future > now ? future - now : 1000)\n continue\n }\n\n const request = this.pending[0]\n if (request) {\n const basicURL = this.rest.simplifyUrl(request.url, request.method)\n\n // IF THIS URL IS STILL RATE LIMITED, TRY AGAIN\n // If this url is still rate limited, try again\n const urlResetIn = this.rest.checkRateLimits(basicURL)\n if (urlResetIn) await delay(urlResetIn)\n\n // IF A BUCKET EXISTS, CHECK THE BUCKET'S RATE LIMITS\n const bucketResetIn = request.bucketId ? this.rest.checkRateLimits(request.bucketId) : false\n if (bucketResetIn) await delay(bucketResetIn)\n\n this.firstRequest = false\n this.remaining--\n\n if (this.timeoutId && this.remaining === 0 && this.interval !== 0) {\n this.timeoutId = setTimeout(() => {\n this.remaining = this.max\n this.timeoutId = undefined\n }, this.interval)\n }\n\n // Remove from queue, we are executing it.\n this.pending.shift()\n // Check if this request is able to be made globally\n await this.rest.invalidBucket.waitUntilRequestAvailable()\n\n await this.rest\n .sendRequest(request)\n // Should be handled in sendRequest, this catch just prevents bots from dying\n .catch(() => null)\n }\n }\n\n logger.debug(`Queue ${this.url} process pending while loop exited with ${this.pending.length}.`)\n\n // Mark as false so next pending request can be triggered by new loop.\n this.processingPending = false\n this.cleanup()\n }\n\n handleCompletedRequest(headers: { max?: number; interval?: number; remaining?: number }): void {\n if (headers.max === 0) {\n this.remaining++\n return\n }\n\n if (!this.frozenAt) this.frozenAt = Date.now()\n if (headers.interval !== undefined) this.interval = headers.interval\n if (headers.remaining !== undefined) this.remaining = headers.remaining\n\n if (this.remaining <= 1) {\n this.timeoutId = setTimeout(() => {\n this.remaining = this.max\n this.timeoutId = undefined\n }, headers.interval)\n }\n }\n\n /** Checks if a request is available and adds it to the queue. Also triggers queue processing if not already processing. */\n async makeRequest(options: SendRequestOptions): Promise<void> {\n await this.waitUntilRequestAvailable()\n this.pending.push(options)\n this.processPending()\n }\n\n /** Cleans up the queue by checking if there is nothing left and removing it. */\n cleanup(): void {\n if (!this.isQueueClearable()) {\n this.processPending()\n return\n }\n\n logger.debug(`[Queue] ${this.url}. Delaying delete for ${this.deleteQueueDelay}ms`)\n // Delete in a minute giving a bit of time to allow new requests that may reuse this queue\n setTimeout(async () => {\n if (!this.isQueueClearable()) {\n logger.debug(`[Queue] ${this.url}. is not clearable. Restarting processing of queue.`)\n this.processPending()\n return\n }\n\n logger.debug(`[Queue] ${this.url}. Deleting`)\n if (this.timeoutId) clearTimeout(this.timeoutId)\n // No requests have been requested for this queue so we nuke this queue\n this.rest.queues.delete(this.url)\n logger.debug(`[Queue] ${this.url}. Deleted! Remaining: (${this.rest.queues.size})`, [...this.rest.queues.keys()])\n if (this.rest.queues.size) this.processPending()\n }, this.deleteQueueDelay)\n }\n\n /** Simply checks if the queue is able to be cleared or it has requests pending. */\n isQueueClearable(): boolean {\n if (this.firstRequest) return false\n if (this.waiting.length > 0) return false\n if (this.pending.length > 0) return false\n if (this.interval === 0) return false\n if (this.processing) return false\n if (this.processingPending) return false\n\n return true\n }\n}\n\nexport interface QueueOptions {\n /** How many requests are remaining. Defaults to 1 */\n remaining?: number\n /** Max number of requests allowed in this this. Defaults to 1. */\n max?: number\n /** The time in milliseconds that discord allows to make the max number of invalid requests. Defaults to 0 */\n interval?: number\n /** timer to reset to 0 */\n timeoutId?: NodeJS.Timeout\n /** The url this queue will be handling. */\n url: string\n /** The time in milliseconds to wait before deleting this queue if it is empty. Defaults to 60000(one minute). */\n deleteQueueDelay?: number\n}\n"],"names":["delay","logger","Queue","constructor","rest","options","remaining","max","interval","waiting","pending","processing","processingPending","firstRequest","frozenAt","deleteQueueDelay","url","timeoutId","isRequestAllowed","waitUntilRequestAvailable","Promise","resolve","push","processWaiting","length","debug","shift","processPending","now","Date","future","request","basicURL","simplifyUrl","method","urlResetIn","checkRateLimits","bucketResetIn","bucketId","setTimeout","undefined","invalidBucket","sendRequest","catch","cleanup","handleCompletedRequest","headers","makeRequest","isQueueClearable","clearTimeout","queues","delete","size","keys"],"mappings":"AAAA,SAASA,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AAGjD,OAAO,MAAMC;IA4BXC,YAAYC,IAAiB,EAAEC,OAAqB,CAAE;QAzBtD,+DAA+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C,MAAM,CAAC,IAAI,CAACnC,GAAG;YAChCf,OAAOwB,KAAK,CAAC,CAAC,QAAQ,EAAE,IAAI,CAACT,GAAG,CAAC,uBAAuB,EAAE,IAAI,CAACZ,IAAI,CAAC8C,MAAM,CAACE,IAAI,CAAC,CAAC,CAAC,EAAE;mBAAI,IAAI,CAAChD,IAAI,CAAC8C,MAAM,CAACG,IAAI;aAAG;YAChH,IAAI,IAAI,CAACjD,IAAI,CAAC8C,MAAM,CAACE,IAAI,EAAE,IAAI,CAACzB,cAAc;QAChD,GAAG,IAAI,CAACZ,gBAAgB;IAC1B;IAEA,iFAAiF,GACjFiC,mBAA4B;QAC1B,IAAI,IAAI,CAACnC,YAAY,EAAE,OAAO,KAAK;QACnC,IAAI,IAAI,CAACJ,OAAO,CAACe,MAAM,GAAG,GAAG,OAAO,KAAK;QACzC,IAAI,IAAI,CAACd,OAAO,CAACc,MAAM,GAAG,GAAG,OAAO,KAAK;QACzC,IAAI,IAAI,CAAChB,QAAQ,KAAK,GAAG,OAAO,KAAK;QACrC,IAAI,IAAI,CAACG,UAAU,EAAE,OAAO,KAAK;QACjC,IAAI,IAAI,CAACC,iBAAiB,EAAE,OAAO,KAAK;QAExC,OAAO,IAAI;IACb;AACF,CAAC"}
1
+ {"version":3,"sources":["../src/queue.ts"],"sourcesContent":["import { delay, logger } from '@discordeno/utils'\nimport type { RestManager, SendRequestOptions } from './types.js'\n\nexport class Queue {\n /** The rest manager */\n rest: RestManager\n /** Amount of requests that have are remaining. Defaults to 1. */\n remaining: number = 1\n /** Max requests for this this. Defaults to 1. */\n max: number = 1\n /** The time that discord allows to make the max number of requests. Defaults to 0 */\n interval: number = 0\n /** timer to reset to 0 */\n timeoutId: NodeJS.Timeout | undefined\n /** The requests that are currently pending. */\n waiting: Array<(value: void | PromiseLike<void>) => void> = []\n /** The requests that are currently pending. */\n pending: SendRequestOptions[] = []\n /** Whether or not the waiting queue is already processing. */\n processing: boolean = false\n /** Whether or not the pending queue is already processing. */\n processingPending: boolean = false\n /** Whether the first request is pending. */\n firstRequest: boolean = false\n /** The url that all the requests in this queue are sent to. */\n url: string\n /** When requests started being made to determine when the interval will reset it. */\n frozenAt: number = 0\n /** The time in milliseconds to wait before deleting this queue if it is empty. Defaults to 60000(one minute). */\n deleteQueueDelay: number = 60000\n /** The authentication header used for the OAuth2 request. Defaults to an empty string for non-OAuth2 requests */\n authentication: string = ''\n\n constructor(rest: RestManager, options: QueueOptions) {\n this.rest = rest\n this.url = options.url\n\n if (options.interval) this.interval = options.interval\n if (options.max) this.max = options.max\n if (options.remaining) this.remaining = options.remaining\n if (options.timeoutId) this.timeoutId = options.timeoutId\n if (options.deleteQueueDelay) this.deleteQueueDelay = options.deleteQueueDelay\n if (options.authentication) this.authentication = options.authentication\n }\n\n /** Check if there is any remaining requests that are allowed. */\n isRequestAllowed(): boolean {\n return this.remaining > 0\n }\n\n /** Pauses the execution until a request is allowed to be made. */\n async waitUntilRequestAvailable(): Promise<void> {\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 (this.isRequestAllowed()) {\n // this.remaining++;\n resolve()\n } else {\n this.waiting.push(resolve)\n await this.processWaiting()\n }\n })\n }\n\n /** Process the queue of requests waiting to be handled. */\n async processWaiting(): Promise<void> {\n // If already processing, that loop will handle all waiting requests.\n if (this.processing) return\n // Mark as processing so other loops don't start\n this.processing = true\n\n while (this.waiting.length > 0) {\n logger.debug(`[Queue] ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url} process waiting while loop ran.`)\n if (this.isRequestAllowed()) {\n // Resolve the next item in the queue\n this.waiting.shift()?.()\n } else {\n await delay(1000)\n }\n }\n\n // Mark as false so next pending request can be triggered by new loop.\n this.processing = false\n }\n\n /** Process the queue of all requests pending to be sent. */\n async processPending(): Promise<void> {\n // If already processing, that loop will handle all pending requests.\n if (this.processingPending || !this.pending.length) return\n\n // Mark as processing so other loops don't start\n this.processingPending = true\n\n while (this.pending.length > 0) {\n logger.debug(`Queue ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url} process pending while loop ran with ${this.pending.length}.`)\n if (!this.firstRequest && !this.isRequestAllowed()) {\n const now = Date.now()\n const future = this.frozenAt + this.interval\n await delay(future > now ? future - now : 1000)\n continue\n }\n\n const request = this.pending[0]\n if (request) {\n const basicURL = this.rest.simplifyUrl(request.route, request.method)\n\n // If this url is still rate limited, try again\n const urlResetIn = this.rest.checkRateLimits(basicURL, request.requestBodyOptions?.headers)\n if (urlResetIn) await delay(urlResetIn)\n\n // IF A BUCKET EXISTS, CHECK THE BUCKET'S RATE LIMITS\n const bucketResetIn = request.bucketId ? this.rest.checkRateLimits(request.bucketId, request.requestBodyOptions?.headers) : false\n if (bucketResetIn) await delay(bucketResetIn)\n\n this.firstRequest = false\n this.remaining--\n\n if (this.timeoutId && this.remaining === 0 && this.interval !== 0) {\n this.timeoutId = setTimeout(() => {\n this.remaining = this.max\n this.timeoutId = undefined\n }, this.interval)\n }\n\n // Remove from queue, we are executing it.\n this.pending.shift()\n // Check if this request is able to be made globally\n await this.rest.invalidBucket.waitUntilRequestAvailable()\n\n await this.rest\n .sendRequest(request)\n // Should be handled in sendRequest, this catch just prevents bots from dying\n .catch(() => null)\n }\n }\n\n logger.debug(`Queue ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url} process pending while loop exited with ${this.pending.length}.`)\n\n // Mark as false so next pending request can be triggered by new loop.\n this.processingPending = false\n this.cleanup()\n }\n\n handleCompletedRequest(headers: { max?: number; interval?: number; remaining?: number }): void {\n if (headers.max === 0) {\n this.remaining++\n return\n }\n\n if (!this.frozenAt) this.frozenAt = Date.now()\n if (headers.interval !== undefined) this.interval = headers.interval\n if (headers.remaining !== undefined) this.remaining = headers.remaining\n\n if (this.remaining <= 1) {\n this.timeoutId = setTimeout(() => {\n this.remaining = this.max\n this.timeoutId = undefined\n }, headers.interval)\n }\n }\n\n /** Checks if a request is available and adds it to the queue. Also triggers queue processing if not already processing. */\n async makeRequest(options: SendRequestOptions): Promise<void> {\n await this.waitUntilRequestAvailable()\n this.pending.push(options)\n this.processPending()\n }\n\n /** Cleans up the queue by checking if there is nothing left and removing it. */\n cleanup(): void {\n if (!this.isQueueClearable()) {\n this.processPending()\n return\n }\n\n logger.debug(`[Queue] ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url}. Delaying delete for ${this.deleteQueueDelay}ms`)\n // Delete in a minute giving a bit of time to allow new requests that may reuse this queue\n setTimeout(async () => {\n if (!this.isQueueClearable()) {\n logger.debug(`[Queue] ${this.isOauth2Queue() ? '' : 'Bearer '}${this.url}. is not clearable. Restarting processing of queue.`)\n this.processPending()\n return\n }\n\n logger.debug(`[Queue] ${this.url}. Deleting`)\n if (this.timeoutId) clearTimeout(this.timeoutId)\n // No requests have been requested for this queue so we nuke this queue\n this.rest.queues.delete(`${this.authentication}${this.url}`)\n logger.debug(\n `[Queue] ${this.url}. Deleted! Remaining: (${this.rest.queues.size})`,\n [...this.rest.queues.values()].map((queue) => `${queue.isOauth2Queue() ? '' : 'Bearer '}${queue.url}`),\n )\n if (this.rest.queues.size) this.processPending()\n }, this.deleteQueueDelay)\n }\n\n /** Simply checks if the queue is able to be cleared or it has requests pending. */\n isQueueClearable(): boolean {\n if (this.firstRequest) return false\n if (this.waiting.length > 0) return false\n if (this.pending.length > 0) return false\n if (this.interval === 0) return false\n if (this.processing) return false\n if (this.processingPending) return false\n\n return true\n }\n\n isOauth2Queue(): boolean {\n return this.authentication === ''\n }\n}\n\nexport interface QueueOptions {\n /** How many requests are remaining. Defaults to 1 */\n remaining?: number\n /** Max number of requests allowed in this this. Defaults to 1. */\n max?: number\n /** The time in milliseconds that discord allows to make the max number of invalid requests. Defaults to 0 */\n interval?: number\n /** timer to reset to 0 */\n timeoutId?: NodeJS.Timeout\n /** The url this queue will be handling. */\n url: string\n /** The time in milliseconds to wait before deleting this queue if it is empty. Defaults to 60000(one minute). */\n deleteQueueDelay?: number\n /** Authentication used for the request. In non-OAuth2 situations should be an empty string. Defaults to an empty string */\n authentication?: string\n}\n"],"names":["delay","logger","Queue","constructor","rest","options","remaining","max","interval","waiting","pending","processing","processingPending","firstRequest","frozenAt","deleteQueueDelay","authentication","url","timeoutId","isRequestAllowed","waitUntilRequestAvailable","Promise","resolve","push","processWaiting","length","debug","isOauth2Queue","shift","processPending","now","Date","future","request","basicURL","simplifyUrl","route","method","urlResetIn","checkRateLimits","requestBodyOptions","headers","bucketResetIn","bucketId","setTimeout","undefined","invalidBucket","sendRequest","catch","cleanup","handleCompletedRequest","makeRequest","isQueueClearable","clearTimeout","queues","delete","size","values","map","queue"],"mappings":"AAAA,SAASA,KAAK,EAAEC,MAAM,QAAQ,oBAAmB;AAGjD,OAAO,MAAMC;IA8BXC,YAAYC,IAAiB,EAAEC,OAAqB,CAAE;QA3BtD,+DAA+D,QAC/DC,YAAoB;QACpB,+CAA+C,QAC/CC,MAAc;QACd,mFAAmF,QACnFC,WAAmB;QAGnB,6CAA6C,QAC7CC,UAA4D,EAAE;QAC9D,6CAA6C,QAC7CC,UAAgC,EAAE;QAClC,4DAA4D,QAC5DC,aAAsB,KAAK;QAC3B,4DAA4D,QAC5DC,oBAA6B,KAAK;QAClC,0CAA0C,QAC1CC,eAAwB,KAAK;QAG7B,mFAAmF,QACnFC,WAAmB;QACnB,+GAA+G,QAC/GC,mBAA2B;QAC3B,+GAA+G,QAC/GC,iBAAyB;QAGvB,IAAI,CAACZ,IAAI,GAAGA;QACZ,IAAI,CAACa,GAAG,GAAGZ,QAAQY,GAAG;QAEtB,IAAIZ,QAAQG,QAAQ,EAAE,IAAI,CAACA,QAAQ,GAAGH,QAAQG,QAAQ;QACtD,IAAIH,QAAQE,GAAG,EAAE,IAAI,CAACA,GAAG,GAAGF,QAAQE,GAAG;QACvC,IAAIF,QAAQC,SAAS,EAAE,IAAI,CAACA,SAAS,GAAGD,QAAQC,SAAS;QACzD,IAAID,QAAQa,SAAS,EAAE,IAAI,CAACA,SAAS,GAAGb,QA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CAAC,CAACC,QAAU,CAAC,EAAEA,MAAMhC,aAAa,KAAK,KAAK,SAAS,CAAC,EAAEgC,MAAM1C,GAAG,CAAC,CAAC;YAEvG,IAAI,IAAI,CAACb,IAAI,CAACkD,MAAM,CAACE,IAAI,EAAE,IAAI,CAAC3B,cAAc;QAChD,GAAG,IAAI,CAACd,gBAAgB;IAC1B;IAEA,iFAAiF,GACjFqC,mBAA4B;QAC1B,IAAI,IAAI,CAACvC,YAAY,EAAE,OAAO,KAAK;QACnC,IAAI,IAAI,CAACJ,OAAO,CAACgB,MAAM,GAAG,GAAG,OAAO,KAAK;QACzC,IAAI,IAAI,CAACf,OAAO,CAACe,MAAM,GAAG,GAAG,OAAO,KAAK;QACzC,IAAI,IAAI,CAACjB,QAAQ,KAAK,GAAG,OAAO,KAAK;QACrC,IAAI,IAAI,CAACG,UAAU,EAAE,OAAO,KAAK;QACjC,IAAI,IAAI,CAACC,iBAAiB,EAAE,OAAO,KAAK;QAExC,OAAO,IAAI;IACb;IAEAe,gBAAyB;QACvB,OAAO,IAAI,CAACX,cAAc,KAAK;IACjC;AACF,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { RestRoutes } from './typings/routes.js';
2
+ export declare function createRoutes(): RestRoutes;
3
+ //# sourceMappingURL=routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAErD,wBAAgB,YAAY,IAAI,UAAU,CA+kBzC"}