Browse packages

Package · v0.18.0

worker

Standard library workers

Runtime support

  • Node
  • Deno
  • Bun
  • Browser

This module provides portable module workers with the Web Worker event and messaging contract.

Installation#

bash
npm install @rcompat/worker
bash
pnpm add @rcompat/worker
bash
yarn add @rcompat/worker
bash
bun add @rcompat/worker

Usage#

js
import Worker from "@rcompat/worker";

const worker = new Worker(new URL("./worker.js", import.meta.url), {
  name: "example",
});

worker.addEventListener("message", event => {
  console.log(event.data);
});
worker.postMessage("hello");

The package exposes the Worker interface in browsers, Bun, Deno, and Node. Node workers are bootstrapped with self, postMessage, onmessage, and the standard event-target methods so the same module worker can run in every supported runtime.

Workers are always ES modules, so the module mode can normally be omitted from the optional constructor options.

Worker scripts should use the Web Worker messaging API rather than runtime-specific APIs such as Node's parentPort.