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 🙏

© 2025 – Pkg Stats / Ryan Hefner

typed-worker-threads

v0.4.0

Published

Type-safe wrapper for Node.js Worker Threads

Downloads

43

Readme

Typed Worker Threads

Typed Worker Threads is a library designed to make working with threads in Node.js easier, while providing type safety with TypeScript. This README will guide you through setting up and using the library in your project.

Installation

Install the package using npm or yarn or pnpm.

npm install typed-worker-threads

Usage (recommended)

  1. Create a folder called threads in your project root.
  2. Create a new file in the threads directory. Name it to describe the thread's purpose, for example, pdf_thread.ts or compress_thread.ts.
  3. In the file, import the Thread class and create a new instance of it. Pass the path to the file that will be executed in the thread as the first argument. The path is relative to the threads directory. The second argument is the name of the thread, which is optional and defaults to the name of the file.
  4. Export the instance of the Thread class.
  5. In the main thread, import the created thread and use it (handle events, send messages, etc.).
  6. In the worker thread, import parentPort from the created thread instance and use it.

Here's an example of how to set up a typed worker thread:

// 'compress_thread.ts' file in 'threads' directory
import { Thread } from "typed-worker-threads";

type ParentToChild = {
  type: "compress";
  payload: {
    name: string;
    buffer: ArrayBuffer;
    outDir: string;
    compressionLevel: number;
    type: "gzip" | "deflate";
  };
};

type CompressionError = {
  type: "error";
  payload: { error: Error };
};

type CompressionSuccess = {
  type: "success";
  payload: {
    name: string;
    path: string;
  };
};
type ChildToParent = CompressionError | CompressionSuccess;

const compressionThread = new Thread<ParentToChild, ChildToParent>(
  "../dist/workers/compress_worker.js",
  "compress_thread"
);

export default compressionThread;

// Main thread
import compressionThread from "./threads/compress_thread";

compressionThread.worker.on("message", (message) => {
  switch (message.type) {
    case "success":
      console.log(`File ${message.payload.name} compressed successfully`);
      break;
    case "error":
      console.error(message.payload.error);
      break;
  }
});

// Worker thread
import { parentPort } from "./threads/compress_thread";

parentPort.on("message", (message) => {
  // Make heavy computations (compress file, etc.)
  // If an error occurs, send an error message to the main thread
});

API

  • Thread<ParentToChild, ChildToParent>: Class for creating and managing worker threads.
  • parentPort: An instance of MessagePort to communicate with the main thread.

Notice about transfer between threads

The "structured clone algorithm" is used to send messages between threads. Read more about it here. typed-worker-threads has a built-in type StructureCloned that describes the types that can be sent between threads safely. If you want to send a type that is not StructureCloned, you can use the transferList (supports ArrayBuffer, MessagePort, FileHandle) option when sending a message. Read more about it here.

License

Typed Worker Threads is released under the BSD-3-Clause License. See LICENSE for details.