Version v3.0.0 Size 4.5 KB gzip
Why Familiar?
Raw workers force every application to maintain its own message contract, lifecycle, cancellation, and pool scheduler. Familiar provides those boundaries while keeping worker code in normal typed ES modules.
ts
// Before
const worker = new Worker(new URL('./sum.worker.ts', import.meta.url), { type: 'module' });
worker.postMessage([1, 2, 3]);
// After
const pool = createWorker<number[], number>(new URL('./sum.worker.ts', import.meta.url));
await pool.run([1, 2, 3]);| Feature | Familiar | Raw Worker | Comlink |
|---|---|---|---|
| Bundle size | 4.5 KB | built-in | ~2 kB |
| Module-worker contract | manual | ||
| Pool scheduling | |||
| AbortSignal cancellation | manual | manual | |
| Versioned protocol | implementation-specific | ||
| Zero dependencies |
Use Familiar when worker jobs need bounded concurrency, typed errors, cancellation, or queue policy.
Consider raw Worker when one isolated worker and custom messaging are enough.
Installation
sh
pnpm add @vielzeug/familiarsh
npm install @vielzeug/familiarsh
yarn add @vielzeug/familiarQuick Start
Register task logic inside a worker module.
ts
// double.worker.ts
import { exposeTask } from '@vielzeug/familiar/protocol';
exposeTask((value: number) => value * 2);Create pool from module URL and dispose it after use.
ts
import { createWorker } from '@vielzeug/familiar';
const worker = createWorker<number, number>(new URL('./double.worker.ts', import.meta.url));
try {
console.log(await worker.run(21));
} finally {
worker.dispose();
}Features
createWorker()— versioned task protocol over ES module workerscreateStreamWorker()— stream-only worker capabilityrun()— validated priority scheduling, transferables, timeout, and cancellationrunBatch()— ordered progressive results with shared fail-fast cancellationstats— active, queued, completed, and failed counterscreateTestWorker()— faithful in-process task-pool testingdispose()anddrain()— immediate or draining teardown, withusingsupport- Application-owned promise composition — domain task groups without pool-owned orchestration state