From ef0e4f1f9d1477ef07d531fe2dab8aa331fb76d2 Mon Sep 17 00:00:00 2001 From: Chris Opperwall Date: Fri, 3 Jul 2020 18:32:39 -0700 Subject: [PATCH 1/6] refactor: remove @ts-ignore --- src/error-request.ts | 18 ++++++++++++++---- src/wrap-request.ts | 25 +++++++++++++++++++------ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/error-request.ts b/src/error-request.ts index 59589c9e..a4b814b5 100644 --- a/src/error-request.ts +++ b/src/error-request.ts @@ -1,6 +1,14 @@ -// @ts-ignore +import type { Octokit } from "@octokit/core"; +import { RequestOptions } from "@octokit/types"; +import { RequestError } from "@octokit/request-error"; +import { RetryState } from './types' -export async function errorRequest(octokit, state, error, options) { +export async function errorRequest( + octokit: Octokit, + state: RetryState, + error: RequestError, + options: RequestOptions +) { if (!error.request || !error.request.request) { // address https://github.com/octokit/plugin-retry.js/issues/8 throw error; @@ -9,8 +17,10 @@ export async function errorRequest(octokit, state, error, options) { // retry all >= 400 && not doNotRetry if (error.status >= 400 && !state.doNotRetry.includes(error.status)) { const retries = - options.request.retries != null ? options.request.retries : state.retries; - const retryAfter = Math.pow((options.request.retryCount || 0) + 1, 2); + options.request?.retries != null + ? options.request.retries + : state.retries; + const retryAfter = Math.pow((options.request?.retryCount || 0) + 1, 2); throw octokit.retry.retryRequest(error, retries, retryAfter); } diff --git a/src/wrap-request.ts b/src/wrap-request.ts index 079afe01..abdbee0f 100644 --- a/src/wrap-request.ts +++ b/src/wrap-request.ts @@ -1,14 +1,24 @@ -// @ts-ignore +import { + RequestOptions, + OctokitResponse, +} from "@octokit/types"; import Bottleneck from "bottleneck/light"; +import { RetryState } from './types' -// @ts-ignore -export async function wrapRequest(state, request, options) { - const limiter = new Bottleneck(); +export async function wrapRequest( + state: RetryState, + request: (a: RequestOptions) => Promise>, + options: RequestOptions +) { + const limiter = new Bottleneck.Bottleneck(); - // @ts-ignore limiter.on("failed", function (error, info) { const maxRetries = ~~error.request.request.retries; const after = ~~error.request.request.retryAfter; + + if (!options.request) { + options.request = {}; + } options.request.retryCount = info.retryCount + 1; if (maxRetries > info.retryCount) { @@ -18,5 +28,8 @@ export async function wrapRequest(state, request, options) { } }); - return limiter.schedule(request, options); + return limiter.schedule, RequestOptions>( + request, + options + ); } From 2bc11bbe52417417c4543893ade4a83f02f75427 Mon Sep 17 00:00:00 2001 From: Chris Opperwall Date: Fri, 3 Jul 2020 18:38:55 -0700 Subject: [PATCH 2/6] types: add bottleneck types to library --- src/@types/bottleneck.d.ts | 1227 ++++++++++++++++++++++++++++++++++++ src/types.js | 0 src/types.ts | 6 + src/wrap-request.ts | 2 +- 4 files changed, 1234 insertions(+), 1 deletion(-) create mode 100644 src/@types/bottleneck.d.ts create mode 100644 src/types.js create mode 100644 src/types.ts diff --git a/src/@types/bottleneck.d.ts b/src/@types/bottleneck.d.ts new file mode 100644 index 00000000..a93d3e88 --- /dev/null +++ b/src/@types/bottleneck.d.ts @@ -0,0 +1,1227 @@ +declare module "bottleneck/light" { + namespace Bottleneck { + type ConstructorOptions = { + /** + * How many jobs can be running at the same time. + */ + readonly maxConcurrent?: number | null; + /** + * How long to wait after launching a job before launching another one. + */ + readonly minTime?: number | null; + /** + * How long can the queue get? When the queue length exceeds that value, the selected `strategy` is executed to shed the load. + */ + readonly highWater?: number | null; + /** + * Which strategy to use if the queue gets longer than the high water mark. + */ + readonly strategy?: Bottleneck.Strategy | null; + /** + * The `penalty` value used by the `Bottleneck.strategy.BLOCK` strategy. + */ + readonly penalty?: number | null; + /** + * How many jobs can be executed before the limiter stops executing jobs. If `reservoir` reaches `0`, no jobs will be executed until it is no longer `0`. + */ + readonly reservoir?: number | null; + /** + * Every `reservoirRefreshInterval` milliseconds, the `reservoir` value will be automatically reset to `reservoirRefreshAmount`. + */ + readonly reservoirRefreshInterval?: number | null; + /** + * The value to reset `reservoir` to when `reservoirRefreshInterval` is in use. + */ + readonly reservoirRefreshAmount?: number | null; + /** + * The increment applied to `reservoir` when `reservoirIncreaseInterval` is in use. + */ + readonly reservoirIncreaseAmount?: number | null; + /** + * Every `reservoirIncreaseInterval` milliseconds, the `reservoir` value will be automatically incremented by `reservoirIncreaseAmount`. + */ + readonly reservoirIncreaseInterval?: number | null; + /** + * The maximum value that `reservoir` can reach when `reservoirIncreaseInterval` is in use. + */ + readonly reservoirIncreaseMaximum?: number | null; + /** + * Optional identifier + */ + readonly id?: string | null; + /** + * Set to true to leave your failed jobs hanging instead of failing them. + */ + readonly rejectOnDrop?: boolean | null; + /** + * Set to true to keep track of done jobs with counts() and jobStatus(). Uses more memory. + */ + readonly trackDoneStatus?: boolean | null; + /** + * Where the limiter stores its internal state. The default (`local`) keeps the state in the limiter itself. Set it to `redis` to enable Clustering. + */ + readonly datastore?: string | null; + /** + * Override the Promise library used by Bottleneck. + */ + readonly Promise?: any; + /** + * This object is passed directly to the redis client library you've selected. + */ + readonly clientOptions?: any; + /** + * **ioredis only.** When `clusterNodes` is not null, the client will be instantiated by calling `new Redis.Cluster(clusterNodes, clientOptions)`. + */ + readonly clusterNodes?: any; + /** + * An existing Bottleneck.RedisConnection or Bottleneck.IORedisConnection object to use. + * If using, `datastore`, `clientOptions` and `clusterNodes` will be ignored. + */ + /** + * Optional Redis/IORedis library from `require('ioredis')` or equivalent. If not, Bottleneck will attempt to require Redis/IORedis at runtime. + */ + readonly Redis?: any; + /** + * When set to `true`, on initial startup, the limiter will wipe any existing Bottleneck state data on the Redis db. + */ + readonly clearDatastore?: boolean | null; + /** + * The Redis TTL in milliseconds for the keys created by the limiter. When `timeout` is set, the limiter's state will be automatically removed from Redis after timeout milliseconds of inactivity. Note: timeout is 300000 (5 minutes) by default when using a Group. + */ + readonly timeout?: number | null; + + [propName: string]: any; + }; + type JobOptions = { + /** + * A priority between `0` and `9`. A job with a priority of `4` will _always_ be executed before a job with a priority of `5`. + */ + readonly priority?: number | null; + /** + * Must be an integer equal to or higher than `0`. The `weight` is what increases the number of running jobs (up to `maxConcurrent`, if using) and decreases the `reservoir` value (if using). + */ + readonly weight?: number | null; + /** + * The number milliseconds a job has to finish. Jobs that take longer than their `expiration` will be failed with a `BottleneckError`. + */ + readonly expiration?: number | null; + /** + * Optional identifier, helps with debug output. + */ + readonly id?: string | null; + }; + type StopOptions = { + /** + * When `true`, drop all the RECEIVED, QUEUED and RUNNING jobs. When `false`, allow those jobs to complete before resolving the Promise returned by this method. + */ + readonly dropWaitingJobs?: boolean | null; + /** + * The error message used to drop jobs when `dropWaitingJobs` is `true`. + */ + readonly dropErrorMessage?: string | null; + /** + * The error message used to reject a job added to the limiter after `stop()` has been called. + */ + readonly enqueueErrorMessage?: string | null; + }; + type Callback = (err: any, result: T) => void; + type ClientsList = { client?: any; subscriber?: any }; + type GroupLimiterPair = { key: string; limiter: Bottleneck }; + interface Strategy {} + + type EventInfo = { + readonly args: any[]; + readonly options: { + readonly id: string; + readonly priority: number; + readonly weight: number; + readonly expiration?: number; + }; + }; + type EventInfoDropped = EventInfo & { + readonly task: Function; + readonly promise: Promise; + }; + type EventInfoQueued = EventInfo & { + readonly reachedHWM: boolean; + readonly blocked: boolean; + }; + type EventInfoRetryable = EventInfo & { readonly retryCount: number }; + + enum Status { + RECEIVED = "RECEIVED", + QUEUED = "QUEUED", + RUNNING = "RUNNING", + EXECUTING = "EXECUTING", + DONE = "DONE", + } + type Counts = { + RECEIVED: number; + QUEUED: number; + RUNNING: number; + EXECUTING: number; + DONE?: number; + }; + class Bottleneck { + public static readonly strategy: { + /** + * When adding a new job to a limiter, if the queue length reaches `highWater`, drop the oldest job with the lowest priority. This is useful when jobs that have been waiting for too long are not important anymore. If all the queued jobs are more important (based on their `priority` value) than the one being added, it will not be added. + */ + readonly LEAK: Bottleneck.Strategy; + /** + * Same as `LEAK`, except it will only drop jobs that are less important than the one being added. If all the queued jobs are as or more important than the new one, it will not be added. + */ + readonly OVERFLOW_PRIORITY: Bottleneck.Strategy; + /** + * When adding a new job to a limiter, if the queue length reaches `highWater`, do not add the new job. This strategy totally ignores priority levels. + */ + readonly OVERFLOW: Bottleneck.Strategy; + /** + * When adding a new job to a limiter, if the queue length reaches `highWater`, the limiter falls into "blocked mode". All queued jobs are dropped and no new jobs will be accepted until the limiter unblocks. It will unblock after `penalty` milliseconds have passed without receiving a new job. `penalty` is equal to `15 * minTime` (or `5000` if `minTime` is `0`) by default and can be changed by calling `changePenalty()`. This strategy is ideal when bruteforce attacks are to be expected. This strategy totally ignores priority levels. + */ + readonly BLOCK: Bottleneck.Strategy; + }; + + constructor(options?: Bottleneck.ConstructorOptions); + + id: string; + datastore: string; + + /** + * Returns a promise which will be resolved once the limiter is ready to accept jobs + * or rejected if it fails to start up. + */ + ready(): Promise; + + /** + * Returns a datastore-specific object of redis clients. + */ + clients(): Bottleneck.ClientsList; + + /** + * Returns the name of the Redis pubsub channel used for this limiter + */ + channel(): string; + + /** + * Disconnects the underlying redis clients, unless the limiter was created with the `connection` option. + * @param flush - Write transient data before closing. + */ + disconnect(flush?: boolean): Promise; + + /** + * Broadcast a string to every limiter in the Cluster. + */ + publish(message: string): Promise; + + /** + * Returns an object with the current number of jobs per status. + */ + counts(): Bottleneck.Counts; + + /** + * Returns the status of the job with the provided job id. + */ + jobStatus(id: string): Bottleneck.Status; + + /** + * Returns the status of the job with the provided job id. + */ + jobs(status?: Bottleneck.Status): string[]; + + /** + * Returns the number of requests queued. + * @param priority - Returns the number of requests queued with the specified priority. + */ + queued(priority?: number): number; + + /** + * Returns the number of requests queued across the Cluster. + */ + clusterQueued(): Promise; + + /** + * Returns whether there are any jobs currently in the queue or in the process of being added to the queue. + */ + empty(): boolean; + + /** + * Returns the total weight of jobs in a RUNNING or EXECUTING state in the Cluster. + */ + running(): Promise; + + /** + * Returns the total weight of jobs in a DONE state in the Cluster. + */ + done(): Promise; + + /** + * If a request was added right now, would it be run immediately? + * @param weight - The weight of the request + */ + check(weight?: number): Promise; + + /** + * Register an event listener. + * @param name - The event name. + * @param fn - The callback function. + */ + on(name: "error", fn: (error: any) => void): void; + on(name: "empty", fn: () => void): void; + on(name: "idle", fn: () => void): void; + on(name: "depleted", fn: (empty: boolean) => void): void; + on(name: "message", fn: (message: string) => void): void; + on(name: "debug", fn: (message: string, info: any) => void): void; + on( + name: "dropped", + fn: (dropped: Bottleneck.EventInfoDropped) => void + ): void; + on(name: "received", fn: (info: Bottleneck.EventInfo) => void): void; + on(name: "queued", fn: (info: Bottleneck.EventInfoQueued) => void): void; + on(name: "scheduled", fn: (info: Bottleneck.EventInfo) => void): void; + on( + name: "executing", + fn: (info: Bottleneck.EventInfoRetryable) => void + ): void; + on( + name: "failed", + fn: ( + error: any, + info: Bottleneck.EventInfoRetryable + ) => Promise | number | void | null + ): void; + on( + name: "retry", + fn: (message: string, info: Bottleneck.EventInfoRetryable) => void + ): void; + on(name: "done", fn: (info: Bottleneck.EventInfoRetryable) => void): void; + + /** + * Register an event listener for one event only. + * @param name - The event name. + * @param fn - The callback function. + */ + once(name: "error", fn: (error: any) => void): void; + once(name: "empty", fn: () => void): void; + once(name: "idle", fn: () => void): void; + once(name: "depleted", fn: (empty: boolean) => void): void; + once(name: "message", fn: (message: string) => void): void; + once(name: "debug", fn: (message: string, info: any) => void): void; + once( + name: "dropped", + fn: (dropped: Bottleneck.EventInfoDropped) => void + ): void; + once(name: "received", fn: (info: Bottleneck.EventInfo) => void): void; + once( + name: "queued", + fn: (info: Bottleneck.EventInfoQueued) => void + ): void; + once(name: "scheduled", fn: (info: Bottleneck.EventInfo) => void): void; + once( + name: "executing", + fn: (info: Bottleneck.EventInfoRetryable) => void + ): void; + once( + name: "failed", + fn: ( + error: any, + info: Bottleneck.EventInfoRetryable + ) => Promise | number | void | null + ): void; + once( + name: "retry", + fn: (message: string, info: Bottleneck.EventInfoRetryable) => void + ): void; + once( + name: "done", + fn: (info: Bottleneck.EventInfoRetryable) => void + ): void; + + /** + * Removes all registered event listeners. + * @param name - The optional event name to remove listeners from. + */ + removeAllListeners(name?: string): void; + + /** + * Changes the settings for future requests. + * @param options - The new settings. + */ + updateSettings(options?: Bottleneck.ConstructorOptions): Bottleneck; + + /** + * Adds to the reservoir count and returns the new value. + */ + incrementReservoir(incrementBy: number): Promise; + + /** + * The `stop()` method is used to safely shutdown a limiter. It prevents any new jobs from being added to the limiter and waits for all Executing jobs to complete. + */ + stop(options?: Bottleneck.StopOptions): Promise; + + /** + * Returns the current reservoir count, if any. + */ + currentReservoir(): Promise; + + /** + * Chain this limiter to another. + * @param limiter - The limiter that requests to this limiter must also follow. + */ + chain(limiter?: Bottleneck): Bottleneck; + + wrap( + fn: () => PromiseLike + ): (() => Promise) & { + withOptions: (options: Bottleneck.JobOptions) => Promise; + }; + wrap( + fn: (arg1: A1) => PromiseLike + ): ((arg1: A1) => Promise) & { + withOptions: (options: Bottleneck.JobOptions, arg1: A1) => Promise; + }; + wrap( + fn: (arg1: A1, arg2: A2) => PromiseLike + ): ((arg1: A1, arg2: A2) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2 + ) => Promise; + }; + wrap( + fn: (arg1: A1, arg2: A2, arg3: A3) => PromiseLike + ): ((arg1: A1, arg2: A2, arg3: A3) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3 + ) => Promise; + }; + wrap( + fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => PromiseLike + ): ((arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4 + ) => Promise; + }; + wrap( + fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => PromiseLike + ): ((arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5 + ) => Promise; + }; + wrap( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6 + ) => PromiseLike + ): (( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6 + ) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6 + ) => Promise; + }; + wrap( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7 + ) => PromiseLike + ): (( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7 + ) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7 + ) => Promise; + }; + wrap( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8 + ) => PromiseLike + ): (( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8 + ) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8 + ) => Promise; + }; + wrap( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9 + ) => PromiseLike + ): (( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9 + ) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9 + ) => Promise; + }; + wrap( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10 + ) => PromiseLike + ): (( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10 + ) => Promise) & { + withOptions: ( + options: Bottleneck.JobOptions, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10 + ) => Promise; + }; + + submit( + fn: (callback: Bottleneck.Callback) => void, + callback: Bottleneck.Callback + ): void; + submit( + fn: (arg1: A1, callback: Bottleneck.Callback) => void, + arg1: A1, + callback: Bottleneck.Callback + ): void; + submit( + fn: (arg1: A1, arg2: A2, callback: Bottleneck.Callback) => void, + arg1: A1, + arg2: A2, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + callback: Bottleneck.Callback + ): void; + submit( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10, + callback: Bottleneck.Callback + ): void; + + submit( + options: Bottleneck.JobOptions, + fn: (callback: Bottleneck.Callback) => void, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: (arg1: A1, callback: Bottleneck.Callback) => void, + arg1: A1, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: (arg1: A1, arg2: A2, callback: Bottleneck.Callback) => void, + arg1: A1, + arg2: A2, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + callback: Bottleneck.Callback + ): void; + submit( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10, + callback: Bottleneck.Callback + ) => void, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10, + callback: Bottleneck.Callback + ): void; + + schedule(fn: () => PromiseLike): Promise; + schedule(fn: (arg1: A1) => PromiseLike, arg1: A1): Promise; + schedule( + fn: (arg1: A1, arg2: A2) => PromiseLike, + arg1: A1, + arg2: A2 + ): Promise; + schedule( + fn: (arg1: A1, arg2: A2, arg3: A3) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3 + ): Promise; + schedule( + fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4 + ): Promise; + schedule( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5 + ): Promise; + schedule( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6 + ): Promise; + schedule( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7 + ): Promise; + schedule( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8 + ): Promise; + schedule( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9 + ): Promise; + schedule( + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10 + ): Promise; + + schedule( + options: Bottleneck.JobOptions, + fn: () => PromiseLike + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: (arg1: A1) => PromiseLike, + arg1: A1 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: (arg1: A1, arg2: A2) => PromiseLike, + arg1: A1, + arg2: A2 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: (arg1: A1, arg2: A2, arg3: A3) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9 + ): Promise; + schedule( + options: Bottleneck.JobOptions, + fn: ( + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10 + ) => PromiseLike, + arg1: A1, + arg2: A2, + arg3: A3, + arg4: A4, + arg5: A5, + arg6: A6, + arg7: A7, + arg8: A8, + arg9: A9, + arg10: A10 + ): Promise; + } + } + + export default Bottleneck; +} diff --git a/src/types.js b/src/types.js new file mode 100644 index 00000000..e69de29b diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 00000000..cea8c6ca --- /dev/null +++ b/src/types.ts @@ -0,0 +1,6 @@ +export interface RetryState { + enabled: boolean; + retryAfterBaseValue: number; + doNotRetry: number[]; + retries: number; +} diff --git a/src/wrap-request.ts b/src/wrap-request.ts index abdbee0f..6de7770c 100644 --- a/src/wrap-request.ts +++ b/src/wrap-request.ts @@ -7,7 +7,7 @@ import { RetryState } from './types' export async function wrapRequest( state: RetryState, - request: (a: RequestOptions) => Promise>, + request: (options: RequestOptions) => Promise>, options: RequestOptions ) { const limiter = new Bottleneck.Bottleneck(); From 1f4608c50aa7fdde844d0a4d1dc7ebfec232132d Mon Sep 17 00:00:00 2001 From: Chris Opperwall Date: Fri, 3 Jul 2020 20:55:12 -0700 Subject: [PATCH 3/6] style: run lint:fix --- src/error-request.ts | 2 +- src/wrap-request.ts | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/error-request.ts b/src/error-request.ts index a4b814b5..00f51d53 100644 --- a/src/error-request.ts +++ b/src/error-request.ts @@ -1,7 +1,7 @@ import type { Octokit } from "@octokit/core"; import { RequestOptions } from "@octokit/types"; import { RequestError } from "@octokit/request-error"; -import { RetryState } from './types' +import { RetryState } from "./types"; export async function errorRequest( octokit: Octokit, diff --git a/src/wrap-request.ts b/src/wrap-request.ts index 6de7770c..01f8b158 100644 --- a/src/wrap-request.ts +++ b/src/wrap-request.ts @@ -1,9 +1,6 @@ -import { - RequestOptions, - OctokitResponse, -} from "@octokit/types"; +import { RequestOptions, OctokitResponse } from "@octokit/types"; import Bottleneck from "bottleneck/light"; -import { RetryState } from './types' +import { RetryState } from "./types"; export async function wrapRequest( state: RetryState, From 292863fa1c223530fda6c8a473a328add4175121 Mon Sep 17 00:00:00 2001 From: Chris Opperwall Date: Fri, 3 Jul 2020 21:03:13 -0700 Subject: [PATCH 4/6] fix: export default Bottleneck class type --- src/@types/bottleneck.d.ts | 2 +- src/wrap-request.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/@types/bottleneck.d.ts b/src/@types/bottleneck.d.ts index a93d3e88..9d68c2fd 100644 --- a/src/@types/bottleneck.d.ts +++ b/src/@types/bottleneck.d.ts @@ -1223,5 +1223,5 @@ declare module "bottleneck/light" { } } - export default Bottleneck; + export default Bottleneck.Bottleneck; } diff --git a/src/wrap-request.ts b/src/wrap-request.ts index 01f8b158..bb09f553 100644 --- a/src/wrap-request.ts +++ b/src/wrap-request.ts @@ -7,7 +7,7 @@ export async function wrapRequest( request: (options: RequestOptions) => Promise>, options: RequestOptions ) { - const limiter = new Bottleneck.Bottleneck(); + const limiter = new Bottleneck(); limiter.on("failed", function (error, info) { const maxRetries = ~~error.request.request.retries; From c249be891d41afb05de8805e0a806da124577bc3 Mon Sep 17 00:00:00 2001 From: Chris Opperwall Date: Fri, 3 Jul 2020 23:08:25 -0700 Subject: [PATCH 5/6] fix: guarantee request from code use --- src/error-request.ts | 11 ++++------- src/types.ts | 6 ++++++ src/wrap-request.ts | 15 +++++++-------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/error-request.ts b/src/error-request.ts index 00f51d53..622e423d 100644 --- a/src/error-request.ts +++ b/src/error-request.ts @@ -1,13 +1,12 @@ import type { Octokit } from "@octokit/core"; -import { RequestOptions } from "@octokit/types"; import { RequestError } from "@octokit/request-error"; -import { RetryState } from "./types"; +import { RetryState, RequestOptionsWithRequest } from "./types"; export async function errorRequest( octokit: Octokit, state: RetryState, error: RequestError, - options: RequestOptions + options: RequestOptionsWithRequest ) { if (!error.request || !error.request.request) { // address https://github.com/octokit/plugin-retry.js/issues/8 @@ -17,10 +16,8 @@ export async function errorRequest( // retry all >= 400 && not doNotRetry if (error.status >= 400 && !state.doNotRetry.includes(error.status)) { const retries = - options.request?.retries != null - ? options.request.retries - : state.retries; - const retryAfter = Math.pow((options.request?.retryCount || 0) + 1, 2); + options.request.retries != null ? options.request.retries : state.retries; + const retryAfter = Math.pow((options.request.retryCount || 0) + 1, 2); throw octokit.retry.retryRequest(error, retries, retryAfter); } diff --git a/src/types.ts b/src/types.ts index cea8c6ca..9302f83c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,12 @@ +import { RequestOptions, RequestRequestOptions } from "@octokit/types"; + export interface RetryState { enabled: boolean; retryAfterBaseValue: number; doNotRetry: number[]; retries: number; } + +export interface RequestOptionsWithRequest extends RequestOptions { + request: RequestRequestOptions; +} diff --git a/src/wrap-request.ts b/src/wrap-request.ts index bb09f553..1f31f407 100644 --- a/src/wrap-request.ts +++ b/src/wrap-request.ts @@ -1,11 +1,13 @@ -import { RequestOptions, OctokitResponse } from "@octokit/types"; +import { OctokitResponse } from "@octokit/types"; import Bottleneck from "bottleneck/light"; -import { RetryState } from "./types"; +import { RetryState, RequestOptionsWithRequest } from "./types"; export async function wrapRequest( state: RetryState, - request: (options: RequestOptions) => Promise>, - options: RequestOptions + request: ( + options: RequestOptionsWithRequest + ) => Promise>, + options: RequestOptionsWithRequest ) { const limiter = new Bottleneck(); @@ -13,9 +15,6 @@ export async function wrapRequest( const maxRetries = ~~error.request.request.retries; const after = ~~error.request.request.retryAfter; - if (!options.request) { - options.request = {}; - } options.request.retryCount = info.retryCount + 1; if (maxRetries > info.retryCount) { @@ -25,7 +24,7 @@ export async function wrapRequest( } }); - return limiter.schedule, RequestOptions>( + return limiter.schedule, RequestOptionsWithRequest>( request, options ); From 913dfd640703588aa509bfdc38b3ee2252568cc9 Mon Sep 17 00:00:00 2001 From: Chris Opperwall Date: Sat, 11 Jul 2020 12:55:52 -0700 Subject: [PATCH 6/6] types: remove bottleneck types --- src/@types/bottleneck.d.ts | 1227 ------------------------------------ 1 file changed, 1227 deletions(-) delete mode 100644 src/@types/bottleneck.d.ts diff --git a/src/@types/bottleneck.d.ts b/src/@types/bottleneck.d.ts deleted file mode 100644 index 9d68c2fd..00000000 --- a/src/@types/bottleneck.d.ts +++ /dev/null @@ -1,1227 +0,0 @@ -declare module "bottleneck/light" { - namespace Bottleneck { - type ConstructorOptions = { - /** - * How many jobs can be running at the same time. - */ - readonly maxConcurrent?: number | null; - /** - * How long to wait after launching a job before launching another one. - */ - readonly minTime?: number | null; - /** - * How long can the queue get? When the queue length exceeds that value, the selected `strategy` is executed to shed the load. - */ - readonly highWater?: number | null; - /** - * Which strategy to use if the queue gets longer than the high water mark. - */ - readonly strategy?: Bottleneck.Strategy | null; - /** - * The `penalty` value used by the `Bottleneck.strategy.BLOCK` strategy. - */ - readonly penalty?: number | null; - /** - * How many jobs can be executed before the limiter stops executing jobs. If `reservoir` reaches `0`, no jobs will be executed until it is no longer `0`. - */ - readonly reservoir?: number | null; - /** - * Every `reservoirRefreshInterval` milliseconds, the `reservoir` value will be automatically reset to `reservoirRefreshAmount`. - */ - readonly reservoirRefreshInterval?: number | null; - /** - * The value to reset `reservoir` to when `reservoirRefreshInterval` is in use. - */ - readonly reservoirRefreshAmount?: number | null; - /** - * The increment applied to `reservoir` when `reservoirIncreaseInterval` is in use. - */ - readonly reservoirIncreaseAmount?: number | null; - /** - * Every `reservoirIncreaseInterval` milliseconds, the `reservoir` value will be automatically incremented by `reservoirIncreaseAmount`. - */ - readonly reservoirIncreaseInterval?: number | null; - /** - * The maximum value that `reservoir` can reach when `reservoirIncreaseInterval` is in use. - */ - readonly reservoirIncreaseMaximum?: number | null; - /** - * Optional identifier - */ - readonly id?: string | null; - /** - * Set to true to leave your failed jobs hanging instead of failing them. - */ - readonly rejectOnDrop?: boolean | null; - /** - * Set to true to keep track of done jobs with counts() and jobStatus(). Uses more memory. - */ - readonly trackDoneStatus?: boolean | null; - /** - * Where the limiter stores its internal state. The default (`local`) keeps the state in the limiter itself. Set it to `redis` to enable Clustering. - */ - readonly datastore?: string | null; - /** - * Override the Promise library used by Bottleneck. - */ - readonly Promise?: any; - /** - * This object is passed directly to the redis client library you've selected. - */ - readonly clientOptions?: any; - /** - * **ioredis only.** When `clusterNodes` is not null, the client will be instantiated by calling `new Redis.Cluster(clusterNodes, clientOptions)`. - */ - readonly clusterNodes?: any; - /** - * An existing Bottleneck.RedisConnection or Bottleneck.IORedisConnection object to use. - * If using, `datastore`, `clientOptions` and `clusterNodes` will be ignored. - */ - /** - * Optional Redis/IORedis library from `require('ioredis')` or equivalent. If not, Bottleneck will attempt to require Redis/IORedis at runtime. - */ - readonly Redis?: any; - /** - * When set to `true`, on initial startup, the limiter will wipe any existing Bottleneck state data on the Redis db. - */ - readonly clearDatastore?: boolean | null; - /** - * The Redis TTL in milliseconds for the keys created by the limiter. When `timeout` is set, the limiter's state will be automatically removed from Redis after timeout milliseconds of inactivity. Note: timeout is 300000 (5 minutes) by default when using a Group. - */ - readonly timeout?: number | null; - - [propName: string]: any; - }; - type JobOptions = { - /** - * A priority between `0` and `9`. A job with a priority of `4` will _always_ be executed before a job with a priority of `5`. - */ - readonly priority?: number | null; - /** - * Must be an integer equal to or higher than `0`. The `weight` is what increases the number of running jobs (up to `maxConcurrent`, if using) and decreases the `reservoir` value (if using). - */ - readonly weight?: number | null; - /** - * The number milliseconds a job has to finish. Jobs that take longer than their `expiration` will be failed with a `BottleneckError`. - */ - readonly expiration?: number | null; - /** - * Optional identifier, helps with debug output. - */ - readonly id?: string | null; - }; - type StopOptions = { - /** - * When `true`, drop all the RECEIVED, QUEUED and RUNNING jobs. When `false`, allow those jobs to complete before resolving the Promise returned by this method. - */ - readonly dropWaitingJobs?: boolean | null; - /** - * The error message used to drop jobs when `dropWaitingJobs` is `true`. - */ - readonly dropErrorMessage?: string | null; - /** - * The error message used to reject a job added to the limiter after `stop()` has been called. - */ - readonly enqueueErrorMessage?: string | null; - }; - type Callback = (err: any, result: T) => void; - type ClientsList = { client?: any; subscriber?: any }; - type GroupLimiterPair = { key: string; limiter: Bottleneck }; - interface Strategy {} - - type EventInfo = { - readonly args: any[]; - readonly options: { - readonly id: string; - readonly priority: number; - readonly weight: number; - readonly expiration?: number; - }; - }; - type EventInfoDropped = EventInfo & { - readonly task: Function; - readonly promise: Promise; - }; - type EventInfoQueued = EventInfo & { - readonly reachedHWM: boolean; - readonly blocked: boolean; - }; - type EventInfoRetryable = EventInfo & { readonly retryCount: number }; - - enum Status { - RECEIVED = "RECEIVED", - QUEUED = "QUEUED", - RUNNING = "RUNNING", - EXECUTING = "EXECUTING", - DONE = "DONE", - } - type Counts = { - RECEIVED: number; - QUEUED: number; - RUNNING: number; - EXECUTING: number; - DONE?: number; - }; - class Bottleneck { - public static readonly strategy: { - /** - * When adding a new job to a limiter, if the queue length reaches `highWater`, drop the oldest job with the lowest priority. This is useful when jobs that have been waiting for too long are not important anymore. If all the queued jobs are more important (based on their `priority` value) than the one being added, it will not be added. - */ - readonly LEAK: Bottleneck.Strategy; - /** - * Same as `LEAK`, except it will only drop jobs that are less important than the one being added. If all the queued jobs are as or more important than the new one, it will not be added. - */ - readonly OVERFLOW_PRIORITY: Bottleneck.Strategy; - /** - * When adding a new job to a limiter, if the queue length reaches `highWater`, do not add the new job. This strategy totally ignores priority levels. - */ - readonly OVERFLOW: Bottleneck.Strategy; - /** - * When adding a new job to a limiter, if the queue length reaches `highWater`, the limiter falls into "blocked mode". All queued jobs are dropped and no new jobs will be accepted until the limiter unblocks. It will unblock after `penalty` milliseconds have passed without receiving a new job. `penalty` is equal to `15 * minTime` (or `5000` if `minTime` is `0`) by default and can be changed by calling `changePenalty()`. This strategy is ideal when bruteforce attacks are to be expected. This strategy totally ignores priority levels. - */ - readonly BLOCK: Bottleneck.Strategy; - }; - - constructor(options?: Bottleneck.ConstructorOptions); - - id: string; - datastore: string; - - /** - * Returns a promise which will be resolved once the limiter is ready to accept jobs - * or rejected if it fails to start up. - */ - ready(): Promise; - - /** - * Returns a datastore-specific object of redis clients. - */ - clients(): Bottleneck.ClientsList; - - /** - * Returns the name of the Redis pubsub channel used for this limiter - */ - channel(): string; - - /** - * Disconnects the underlying redis clients, unless the limiter was created with the `connection` option. - * @param flush - Write transient data before closing. - */ - disconnect(flush?: boolean): Promise; - - /** - * Broadcast a string to every limiter in the Cluster. - */ - publish(message: string): Promise; - - /** - * Returns an object with the current number of jobs per status. - */ - counts(): Bottleneck.Counts; - - /** - * Returns the status of the job with the provided job id. - */ - jobStatus(id: string): Bottleneck.Status; - - /** - * Returns the status of the job with the provided job id. - */ - jobs(status?: Bottleneck.Status): string[]; - - /** - * Returns the number of requests queued. - * @param priority - Returns the number of requests queued with the specified priority. - */ - queued(priority?: number): number; - - /** - * Returns the number of requests queued across the Cluster. - */ - clusterQueued(): Promise; - - /** - * Returns whether there are any jobs currently in the queue or in the process of being added to the queue. - */ - empty(): boolean; - - /** - * Returns the total weight of jobs in a RUNNING or EXECUTING state in the Cluster. - */ - running(): Promise; - - /** - * Returns the total weight of jobs in a DONE state in the Cluster. - */ - done(): Promise; - - /** - * If a request was added right now, would it be run immediately? - * @param weight - The weight of the request - */ - check(weight?: number): Promise; - - /** - * Register an event listener. - * @param name - The event name. - * @param fn - The callback function. - */ - on(name: "error", fn: (error: any) => void): void; - on(name: "empty", fn: () => void): void; - on(name: "idle", fn: () => void): void; - on(name: "depleted", fn: (empty: boolean) => void): void; - on(name: "message", fn: (message: string) => void): void; - on(name: "debug", fn: (message: string, info: any) => void): void; - on( - name: "dropped", - fn: (dropped: Bottleneck.EventInfoDropped) => void - ): void; - on(name: "received", fn: (info: Bottleneck.EventInfo) => void): void; - on(name: "queued", fn: (info: Bottleneck.EventInfoQueued) => void): void; - on(name: "scheduled", fn: (info: Bottleneck.EventInfo) => void): void; - on( - name: "executing", - fn: (info: Bottleneck.EventInfoRetryable) => void - ): void; - on( - name: "failed", - fn: ( - error: any, - info: Bottleneck.EventInfoRetryable - ) => Promise | number | void | null - ): void; - on( - name: "retry", - fn: (message: string, info: Bottleneck.EventInfoRetryable) => void - ): void; - on(name: "done", fn: (info: Bottleneck.EventInfoRetryable) => void): void; - - /** - * Register an event listener for one event only. - * @param name - The event name. - * @param fn - The callback function. - */ - once(name: "error", fn: (error: any) => void): void; - once(name: "empty", fn: () => void): void; - once(name: "idle", fn: () => void): void; - once(name: "depleted", fn: (empty: boolean) => void): void; - once(name: "message", fn: (message: string) => void): void; - once(name: "debug", fn: (message: string, info: any) => void): void; - once( - name: "dropped", - fn: (dropped: Bottleneck.EventInfoDropped) => void - ): void; - once(name: "received", fn: (info: Bottleneck.EventInfo) => void): void; - once( - name: "queued", - fn: (info: Bottleneck.EventInfoQueued) => void - ): void; - once(name: "scheduled", fn: (info: Bottleneck.EventInfo) => void): void; - once( - name: "executing", - fn: (info: Bottleneck.EventInfoRetryable) => void - ): void; - once( - name: "failed", - fn: ( - error: any, - info: Bottleneck.EventInfoRetryable - ) => Promise | number | void | null - ): void; - once( - name: "retry", - fn: (message: string, info: Bottleneck.EventInfoRetryable) => void - ): void; - once( - name: "done", - fn: (info: Bottleneck.EventInfoRetryable) => void - ): void; - - /** - * Removes all registered event listeners. - * @param name - The optional event name to remove listeners from. - */ - removeAllListeners(name?: string): void; - - /** - * Changes the settings for future requests. - * @param options - The new settings. - */ - updateSettings(options?: Bottleneck.ConstructorOptions): Bottleneck; - - /** - * Adds to the reservoir count and returns the new value. - */ - incrementReservoir(incrementBy: number): Promise; - - /** - * The `stop()` method is used to safely shutdown a limiter. It prevents any new jobs from being added to the limiter and waits for all Executing jobs to complete. - */ - stop(options?: Bottleneck.StopOptions): Promise; - - /** - * Returns the current reservoir count, if any. - */ - currentReservoir(): Promise; - - /** - * Chain this limiter to another. - * @param limiter - The limiter that requests to this limiter must also follow. - */ - chain(limiter?: Bottleneck): Bottleneck; - - wrap( - fn: () => PromiseLike - ): (() => Promise) & { - withOptions: (options: Bottleneck.JobOptions) => Promise; - }; - wrap( - fn: (arg1: A1) => PromiseLike - ): ((arg1: A1) => Promise) & { - withOptions: (options: Bottleneck.JobOptions, arg1: A1) => Promise; - }; - wrap( - fn: (arg1: A1, arg2: A2) => PromiseLike - ): ((arg1: A1, arg2: A2) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2 - ) => Promise; - }; - wrap( - fn: (arg1: A1, arg2: A2, arg3: A3) => PromiseLike - ): ((arg1: A1, arg2: A2, arg3: A3) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3 - ) => Promise; - }; - wrap( - fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => PromiseLike - ): ((arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4 - ) => Promise; - }; - wrap( - fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => PromiseLike - ): ((arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5 - ) => Promise; - }; - wrap( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6 - ) => PromiseLike - ): (( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6 - ) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6 - ) => Promise; - }; - wrap( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7 - ) => PromiseLike - ): (( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7 - ) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7 - ) => Promise; - }; - wrap( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8 - ) => PromiseLike - ): (( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8 - ) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8 - ) => Promise; - }; - wrap( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9 - ) => PromiseLike - ): (( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9 - ) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9 - ) => Promise; - }; - wrap( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10 - ) => PromiseLike - ): (( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10 - ) => Promise) & { - withOptions: ( - options: Bottleneck.JobOptions, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10 - ) => Promise; - }; - - submit( - fn: (callback: Bottleneck.Callback) => void, - callback: Bottleneck.Callback - ): void; - submit( - fn: (arg1: A1, callback: Bottleneck.Callback) => void, - arg1: A1, - callback: Bottleneck.Callback - ): void; - submit( - fn: (arg1: A1, arg2: A2, callback: Bottleneck.Callback) => void, - arg1: A1, - arg2: A2, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - callback: Bottleneck.Callback - ): void; - submit( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10, - callback: Bottleneck.Callback - ): void; - - submit( - options: Bottleneck.JobOptions, - fn: (callback: Bottleneck.Callback) => void, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: (arg1: A1, callback: Bottleneck.Callback) => void, - arg1: A1, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: (arg1: A1, arg2: A2, callback: Bottleneck.Callback) => void, - arg1: A1, - arg2: A2, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - callback: Bottleneck.Callback - ): void; - submit( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10, - callback: Bottleneck.Callback - ) => void, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10, - callback: Bottleneck.Callback - ): void; - - schedule(fn: () => PromiseLike): Promise; - schedule(fn: (arg1: A1) => PromiseLike, arg1: A1): Promise; - schedule( - fn: (arg1: A1, arg2: A2) => PromiseLike, - arg1: A1, - arg2: A2 - ): Promise; - schedule( - fn: (arg1: A1, arg2: A2, arg3: A3) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3 - ): Promise; - schedule( - fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4 - ): Promise; - schedule( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5 - ): Promise; - schedule( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6 - ): Promise; - schedule( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7 - ): Promise; - schedule( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8 - ): Promise; - schedule( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9 - ): Promise; - schedule( - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10 - ): Promise; - - schedule( - options: Bottleneck.JobOptions, - fn: () => PromiseLike - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: (arg1: A1) => PromiseLike, - arg1: A1 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: (arg1: A1, arg2: A2) => PromiseLike, - arg1: A1, - arg2: A2 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: (arg1: A1, arg2: A2, arg3: A3) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9 - ): Promise; - schedule( - options: Bottleneck.JobOptions, - fn: ( - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10 - ) => PromiseLike, - arg1: A1, - arg2: A2, - arg3: A3, - arg4: A4, - arg5: A5, - arg6: A6, - arg7: A7, - arg8: A8, - arg9: A9, - arg10: A10 - ): Promise; - } - } - - export default Bottleneck.Bottleneck; -}