@discordeno/rest 19.0.0-next.fc13bdf → 19.0.0-next.fdf0d53
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/manager.d.ts.map +1 -1
- package/dist/manager.js +404 -233
- package/dist/manager.js.map +1 -1
- package/dist/queue.js +10 -10
- package/dist/queue.js.map +1 -1
- package/dist/types.d.ts +201 -193
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +17 -16
package/dist/queue.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { delay, logger } from '@discordeno/utils';
|
|
2
2
|
export class Queue {
|
|
3
|
-
/** Amount of requests that have are remaining. Defaults to 1. */ remaining = 1;
|
|
4
|
-
/** Max requests for this this. Defaults to 1. */ max = 1;
|
|
5
|
-
/** The time that discord allows to make the max number of requests. Defaults to 0 */ interval = 0;
|
|
6
|
-
/** The requests that are currently pending. */ waiting = [];
|
|
7
|
-
/** The requests that are currently pending. */ pending = [];
|
|
8
|
-
/** Whether or not the waiting queue is already processing. */ processing = false;
|
|
9
|
-
/** Whether or not the pending queue is already processing. */ processingPending = false;
|
|
10
|
-
/** Whether the first request is pending. */ firstRequest = false;
|
|
11
|
-
/** When requests started being made to determine when the interval will reset it. */ frozenAt = 0;
|
|
12
|
-
/** The time in milliseconds to wait before deleting this queue if it is empty. Defaults to 60000(one minute). */ deleteQueueDelay = 60000;
|
|
13
3
|
constructor(rest, options){
|
|
4
|
+
/** Amount of requests that have are remaining. Defaults to 1. */ this.remaining = 1;
|
|
5
|
+
/** Max requests for this this. Defaults to 1. */ this.max = 1;
|
|
6
|
+
/** The time that discord allows to make the max number of requests. Defaults to 0 */ this.interval = 0;
|
|
7
|
+
/** The requests that are currently pending. */ this.waiting = [];
|
|
8
|
+
/** The requests that are currently pending. */ this.pending = [];
|
|
9
|
+
/** Whether or not the waiting queue is already processing. */ this.processing = false;
|
|
10
|
+
/** Whether or not the pending queue is already processing. */ this.processingPending = false;
|
|
11
|
+
/** Whether the first request is pending. */ this.firstRequest = false;
|
|
12
|
+
/** When requests started being made to determine when the interval will reset it. */ this.frozenAt = 0;
|
|
13
|
+
/** The time in milliseconds to wait before deleting this queue if it is empty. Defaults to 60000(one minute). */ this.deleteQueueDelay = 60000;
|
|
14
14
|
this.rest = rest;
|
|
15
15
|
this.url = options.url;
|
|
16
16
|
if (options.interval) this.interval = options.interval;
|
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","remaining","max","interval","waiting","pending","processing","processingPending","firstRequest","frozenAt","deleteQueueDelay","constructor","rest","options","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;IAGX,+DAA+D,GAC/DC,YAAoB,EAAC;IACrB,+CAA+C,GAC/CC,MAAc,EAAC;IACf,mFAAmF,GACnFC,WAAmB,EAAC;IAGpB,6CAA6C,GAC7CC,UAA4D,EAAE,CAAA;IAC9D,6CAA6C,GAC7CC,UAAgC,EAAE,CAAA;IAClC,4DAA4D,GAC5DC,aAAsB,KAAK,CAAA;IAC3B,4DAA4D,GAC5DC,oBAA6B,KAAK,CAAA;IAClC,0CAA0C,GAC1CC,eAAwB,KAAK,CAAA;IAG7B,mFAAmF,GACnFC,WAAmB,EAAC;IACpB,+GAA+G,GAC/GC,mBAA2B,MAAK;IAEhCC,YAAYC,IAAiB,EAAEC,OA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AAE,IAAI,CAACzB,cAAc;QAChD,GAAG,IAAI,CAACf,gBAAgB;IAC1B;IAEA,iFAAiF,GACjFoC,mBAA4B;QAC1B,IAAI,IAAI,CAACtC,YAAY,EAAE,OAAO,KAAK;QACnC,IAAI,IAAI,CAACJ,OAAO,CAACkB,MAAM,GAAG,GAAG,OAAO,KAAK;QACzC,IAAI,IAAI,CAACjB,OAAO,CAACiB,MAAM,GAAG,GAAG,OAAO,KAAK;QACzC,IAAI,IAAI,CAACnB,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\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+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,oB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