import { BOUNDED_TASK_GLOBAL, type BoundedTaskSource } from "./bounded-task-source.js"; export { BOUNDED_TASK_GLOBAL, type BoundedTaskSource }; export type BoundedTaskErrorCode = "unavailable" | "closed" | "queue_full" | "queue_timeout" | "invalid_input" | "input_too_large" | "startup_failed" | "task_unknown" | "task_threw" | "deadline_exceeded" | "memory_exceeded" | "invalid_output" | "output_too_large" | "worker_crashed"; export declare class BoundedTaskError extends Error { readonly code: BoundedTaskErrorCode; readonly task: string; /** `name` of the error the task threw, for `task_threw`. */ readonly taskErrorName?: string; constructor(code: BoundedTaskErrorCode, task: string, message: string, options?: { cause?: unknown; taskErrorName?: string; }); } export interface BoundedTaskRunnerOptions { source: BoundedTaskSource; /** Wall-clock budget for one task once a worker has it. Default 2000 ms. */ deadlineMs?: number; /** Budget for a fresh worker to evaluate the bundle. Default 10000 ms. */ startupTimeoutMs?: number; /** Workers alive at once; also the number of tasks running at once. Default 1. */ maxWorkers?: number; /** Tasks allowed to wait for a worker. Beyond this, `queue_full`. Default 8. */ maxQueue?: number; /** * Longest a task may wait for a busy or terminating worker. A task waiting * only for a worker to start is bounded by `startupTimeoutMs` instead. * Default `deadlineMs`. */ queueTimeoutMs?: number; /** Strings count two bytes per UTF-16 unit. Default 8 MiB. */ maxInputBytes?: number; /** Checked inside the worker before the result is copied out. Default 16 MiB. */ maxOutputBytes?: number; /** Worker old-generation heap. Default 128 MiB. */ memoryLimitMb?: number; /** Default 4 MiB. */ stackSizeMb?: number; /** An idle worker is stopped after this long. Default 30000 ms. */ idleTimeoutMs?: number; } export interface BoundedTaskRunnerStats { workers: number; running: number; queued: number; } export interface BoundedTaskRunner { readonly name: string; run(task: string, input: unknown): Promise; stats(): BoundedTaskRunnerStats; close(): Promise; } /** * Size of a structured-clone payload in bytes, or -1 when it holds anything * but plain data (or more than 200,000 values). Stops counting once * `limit` is passed. The worker host evaluates this function's source, so it * must not reference anything outside its own body. */ export declare function measureBoundedPayload(value: unknown, limit: number): number; /** * Runs functions from one bundled module in worker threads, each under a * terminable deadline and a heap limit, with a fixed number of workers and a * bounded wait queue. A task that overruns is stopped by terminating its * worker, so synchronous work in the task never blocks the caller's event * loop. Where worker threads do not exist (Cloudflare Workers), every run * fails with `unavailable`; it never falls back to running inline. */ export declare function createBoundedTaskRunner(options: BoundedTaskRunnerOptions): BoundedTaskRunner;