npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

worker-lib

v2.2.0

Published

Library for easy use of web-worker

Readme

worker-lib

Ask DeepWiki

Overview

worker-lib is a lightweight, type-safe library designed to make Web Workers (Browser) and worker_threads (Node.js) as easy to use as standard asynchronous functions.

Features

  • 🚀 Universal API: Supports both Browser and Node.js with a single unified entry point.
  • 🛡️ Type-Safe: Full TypeScript support with automatic type inference for worker functions.
  • ⚡ Parallelism: Built-in worker pool management with configurable concurrency limits.
  • 🔄 Deep Proxying: Automatically converts functions within objects or arrays into bidirectional proxies. Binary data (Uint8Array, ArrayBuffer, TypedArray, etc.) are correctly identified and transferred as data. No more DataCloneError.
  • 🔄 Bidirectional Callbacks: Supports passing callbacks from main to worker, and vice versa, with full async/await support.
  • 📦 Flexible Instantiation: Pass a Worker instance, a file path (string), or a URL.
  • 📦 Zero Config: Minimal setup required to get started.

Installation

npm install worker-lib
# or
pnpm add worker-lib

Basic Usage

1. Define Worker (worker.ts)

Register your functions using initWorker. You can now pass complex objects containing functions.

import { initWorker } from "worker-lib";

interface RenderOptions {
  html: string;
  resolveResource: (url: string, fallback: (url: string) => Promise<Uint8Array>) => Promise<Uint8Array>;
}

const render = async (options: RenderOptions) => {
  // options.resolveResource is a proxy to the main thread!
  // It even receives a second argument 'fallback' which is a proxy back to the worker!
  const data = await options.resolveResource("logo.png", async (url) => {
    return new Uint8Array([1, 2, 3]); // Worker-side fallback logic
  });
  return `Rendered with ${data.length} bytes`;
};

const workerMap = initWorker({ render });
export type MyWorker = typeof workerMap;

2. Use Worker in Main Thread

Unified Usage (Browser & Node.js)

Since worker-lib uses deep proxying, you can pass nested functions directly.

import { createWorker } from "worker-lib";
import type { MyWorker } from "./worker";

const { execute } = createWorker<MyWorker>(
  () => new URL("./worker.ts", import.meta.url),
  4
);

const result = await execute("render", {
  html: "<div>Hello</div>",
  resolveResource: async (url, fallback) => {
    if (url === "special.png") return await fallback(url); // Call back to worker!
    const resp = await fetch(url);
    return new Uint8Array(await resp.arrayBuffer());
  }
});
console.log(result);

Node.js Specific (Optional)

If you need to use node:worker_threads features explicitly:

import { Worker } from "worker-lib"; // Automatically uses node:worker_threads in Node.js
import { createWorker } from "worker-lib";
import path from "node:path";

const { execute } = createWorker(
  () => new Worker(path.resolve(__dirname, "./worker.js")),
  2
);

API Reference

initWorker(workerProcess)

Initializes the worker side.

  • workerProcess: An object containing the functions to be exposed.

createWorker(builder, limit?)

Creates a worker pool.

  • builder: A function that returns a Worker, string (path), or URL.
  • limit: (Optional) Maximum number of concurrent workers. Default is 4.

Returns an object with:

  • execute(name, ...args): Executes a worker function.
  • waitAll(): Waits for all running tasks to complete.
  • waitReady(retryTime?): Waits for an available worker slot.
  • launchWorker(): Pre-launches all workers in the pool.
  • setLimit(limit): Dynamically changes the worker pool size.
  • close(): Terminates all workers.

execute(name, ...args)

Executes a worker function.

  • name: The name of the function to execute.
  • args: Arguments to pass to the function (supports callbacks).

waitAll()

Returns a promise that resolves when all currently running tasks are complete.

waitReady(retryTime?)

Returns a promise that resolves when there is an available slot in the worker pool.

  • retryTime: (Optional) Milliseconds to wait between checks.

launchWorker()

Forcefully initializes all workers up to the limit. By default, workers are created lazily.

setLimit(limit)

Changes the maximum number of concurrent workers. This will terminate existing workers and reset the pool.

close()

Terminates all workers in the pool immediately.

Worker

The environment-specific Worker class (Web Worker in browser, worker_threads in Node.js).

Examples

For more detailed examples, check the samples repository.