import { WorkerPool } from '../../core/types'; /** * Type for the createWorker function that creates a Worker instance. * @internal */ type CreateWorkerFn = (workerURL?: string | URL) => Promise; /** * Options for configuring the ReusableWorkerPool. */ export interface ReusableWorkerPoolOptions { /** * Maximum number of worker instances in the pool. * * @default 1 * * @remarks * **Security Recommendation:** * For production applications that accept user uploads, set this to a reasonable limit (e.g., 2-4) * to prevent resource exhaustion attacks. Without limits, malicious users could spawn unlimited * workers by uploading multiple large CSV files simultaneously, leading to memory exhaustion and DoS. * * @example * ```ts * // Recommended for production * const pool = new ReusableWorkerPool({ maxWorkers: 4 }); * ``` */ maxWorkers?: number | undefined; /** * Custom worker URL to use for all workers in the pool. */ workerURL?: string | URL | undefined; } export declare class ReusableWorkerPool implements WorkerPool, Disposable { private workers; private requestId; private currentWorkerIndex; private readonly maxWorkers; private readonly customWorkerURL?; private pendingWorkerCreations; private pendingCreationsByURL; private disposed; private nextPendingId; protected readonly createWorker: CreateWorkerFn; /** * Create a new ReusableWorkerPool. * * @param options - Configuration options for the pool * @param createWorkerFn - Function to create worker instances * @internal */ constructor(options: ReusableWorkerPoolOptions | undefined, createWorkerFn: CreateWorkerFn); /** * Get a worker instance from the pool using round-robin load balancing. * * @param workerURL - Optional custom worker URL (overrides pool's workerURL) * @returns A worker instance from the pool * @internal */ getWorker(workerURL?: string | URL): Promise; /** * Get the next request ID for this pool. * * @returns The next request ID * @internal */ getNextRequestId(): number; /** * Release a worker back to the pool. * For ReusableWorkerPool, this does nothing as workers are kept alive and reused. * * @param _worker - The worker to release * @internal */ releaseWorker(_worker: Worker): void; /** * Get the current number of workers in the pool. * * @returns The number of active workers */ get size(): number; /** * Check if the pool has reached its maximum capacity. * * @returns True if the pool is at maximum capacity, false otherwise * * @remarks * This method is useful for implementing early rejection of requests * when the worker pool is saturated, preventing resource exhaustion. * * @example * ```ts * import { Hono } from 'hono'; * import { ReusableWorkerPool } from 'web-csv-toolbox'; * * const pool = new ReusableWorkerPool({ maxWorkers: 4 }); * * app.post('/validate-csv', async (c) => { * // Early rejection if pool is saturated * if (pool.isFull()) { * return c.json({ error: 'Service busy, please try again later' }, 503); * } * * // Process CSV... * }); * ``` */ isFull(): boolean; /** * Terminate all workers in the pool and clean up resources. * * This method should be called when the pool is no longer needed, * typically during application shutdown. * * @example * ```ts * const pool = new ReusableWorkerPool({ maxWorkers: 4 }); * * // When shutting down * pool.terminate(); * ``` * * @example With Hono * ```ts * import { Hono } from 'hono'; * import { ReusableWorkerPool } from 'web-csv-toolbox'; * * const app = new Hono(); * const pool = new ReusableWorkerPool({ maxWorkers: 4 }); * * app.onShutdown(() => { * pool.terminate(); * }); * ``` */ terminate(): void; /** * Dispose of the worker pool, terminating all workers. * * This method is called automatically when using the `using` syntax. * For manual cleanup, use {@link terminate} instead. * * @example With `using` syntax (automatic cleanup) * ```ts * using pool = new ReusableWorkerPool({ maxWorkers: 4 }); * // Workers are automatically terminated when leaving scope * ``` * * @example Manual cleanup * ```ts * const pool = new ReusableWorkerPool({ maxWorkers: 4 }); * try { * // Use pool * } finally { * pool.terminate(); // Preferred over pool[Symbol.dispose]() * } * ``` */ [Symbol.dispose](): void; } export {};