This module provides portable module workers with the Web Worker event and messaging contract.
Installation#
bash
npm install @rcompat/workerbash
pnpm add @rcompat/workerbash
yarn add @rcompat/workerbash
bun add @rcompat/workerUsage#
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.