import { AsyncLocalStorage } from 'node:async_hooks'; export interface AbortContext { cancelTask: AbortController; } /** * `AsyncLocalStorage` instance that is used for baring the AbortContext inside user provided handler. * We can use it to access the `AbortContext` instance via `storage.getStore()`, and there we can access * `cancelTask` instance of `AbortController`. */ export declare const storage: AsyncLocalStorage; /** * Custom error class that will be used for timeout error. */ export declare class TimeoutError extends Error { } /** * Checks whether we are inside timeout handler created by this package, and cancels current * task execution by throwing `TimeoutError`. This error will be ignored if the promise timed * out already, or explicitly skipped in `addTimeoutToPromise`. * * Use this function after every async call that runs inside the timeout handler: * * ```ts * async function doSomething() { * await doSomethingTimeConsuming(); * tryCancel(); * await doSomethingElse(); * tryCancel(); * } * ``` */ export declare function tryCancel(): void; /** * Runs given handler and rejects with the given `errorMessage` (or `Error` instance) * after given `timeoutMillis`, unless the original promise resolves or rejects earlier. * Use `tryCancel()` function inside the handler after each await to finish its execution * early when the timeout appears. * * ```ts * const res = await addTimeoutToPromise( * () => handler(), * 200, * 'Handler timed out after 200ms!', * ); * ``` */ export declare function addTimeoutToPromise(handler: () => Promise, timeoutMillis: number, errorMessage: string | Error): Promise; export interface ExtendTimeoutOptions { /** * Whether to extend the enclosing timeouts by the same amount as well. Keep this enabled (the * default) unless an enclosing timeout is meant to be a hard limit on the total time - with it * disabled, a nested handler can be killed by an outer timeout right after extending its own. * @default true */ propagate?: boolean; } /** * Pushes the deadline of the currently running timeout back by `extraMillis`, for cases where the * required time is only known once the handler is already running. Call it from inside a handler * wrapped in {@link addTimeoutToPromise}; outside of one it is a no-op. * * ```ts * await addTimeoutToPromise(async () => { * const pageCount = await countPages(); * tryCancel(); * // we only now know this page is a big one, so ask for 10 more seconds per page * extendTimeout(pageCount * 10_000); * await scrapeAllPages(); * }, 30_000, 'Handler timed out!'); * ``` * * Enclosing timeouts are extended by the same amount by default, so that a nested handler asking for * more time is not killed moments later by an outer timeout it cannot see. Pass `propagate: false` to * extend only the innermost timeout, leaving any enclosing ones as a hard limit on the total time. * * Extending an already timed out (or already finished) handler does nothing. */ export declare function extendTimeout(extraMillis: number, options?: ExtendTimeoutOptions): void;