Crypto Lib
    Preparing search index...

    Class WorkerPool

    A thread pool for offloading CPU-intensive cryptographic tasks to worker threads.

    const pool = new WorkerPool({ size: 4 });
    const result = await pool.execute({
    modulePath: "@noble/hashes/sha256",
    functionName: "sha256",
    args: [new Uint8Array([1, 2, 3])],
    });
    await pool.shutdown();
    Index
    size: number

    Number of worker threads in the pool.

    • get pending(): number

      Get the number of tasks currently waiting in the queue.

      Returns number

    • get active(): number

      Get the number of workers currently processing a task.

      Returns number

    • Execute a task on an available worker thread.

      The task specifies a module path and function name. The worker will dynamically import the module, call the function with the provided arguments, and return the result.

      Type Parameters

      • T = unknown

      Parameters

      • task: WorkerTask

        Task descriptor with module, function, and arguments.

      Returns Promise<T>

      Promise resolving to the function's return value.

    • Shut down all workers and reject any pending tasks.

      After calling shutdown, the pool cannot be reused. Any tasks still in the queue will be rejected with an error.

      Returns Promise<void>