# @wordpress/worker-threads

> Utilities for type-safe Web Worker communication with RPC.

Latest version **1.15.0** (published 2026-09-10) · GPL-2.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @wordpress/worker-threads
pnpm add @wordpress/worker-threads
yarn add @wordpress/worker-threads
bun add @wordpress/worker-threads
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; popular repo.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.15.0 |
| Published | 2026-09-10 |
| First published | 2026-02-20 |
| Weekly downloads | 0 |
| License | GPL-2.0-or-later |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18.12.0 |
| Dependencies | 1 |
| Unpacked size | 119.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11750 |
| Author | The WordPress Contributors |
| Maintainers | garypendergast, adamsilverstein, gziolo, ntwb, riad, noisysocks, kadamwhite, gutenbergplugin, jorgefilipecosta, ellatrix, iandunn206, whyisjake, ockham, sirreal, nosolosw, wpisabel, ntsekouras, nerrad, desrosj, talldanwp, peterwilsoncc, ryanwelcher, mamaduka, aduth, johnbillion |
| Keywords | wordpress, worker, web-worker, rpc, threads |

## Links

- npm: https://www.npmjs.com/package/@wordpress/worker-threads
- Repository: https://github.com/WordPress/gutenberg
- Homepage: https://github.com/WordPress/gutenberg/tree/HEAD/packages/worker-threads/README.md
- Issues: https://github.com/WordPress/gutenberg/issues
- npm.io page: https://npm.io/package/@wordpress/worker-threads

## Dependencies (1)

- [comctx](https://npm.io/package/comctx.md) ^1.4.3

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.15.0 (latest) — 2026-09-10
- 1.14.1-next.v.202609031004.0 (next) — 2026-09-03
- 1.0.1 (wp-7.0) — 2026-06-30
- 1.14.1-next.v.202608281122.0 — 2026-08-28
- 1.14.0 — 2026-08-26
- 1.13.0 — 2026-08-12
- 1.12.0 — 2026-07-29
- 1.11.0 — 2026-07-14
- 1.10.1-next.v.202607070741.0 — 2026-07-07
- 1.10.0 — 2026-07-01
- 1.9.0 — 2026-06-24
- 1.8.2-next.v.202606191442.0 — 2026-06-19
- 1.8.1 — 2026-06-16
- 1.8.0 — 2026-06-04
- 1.7.0 — 2026-05-27
- … 16 more at https://npm.io/package/@wordpress/worker-threads/versions

## README

# Worker Threads

Utilities for type-safe Web Worker communication with RPC (Remote Procedure Call).

This package provides a simple and efficient way to communicate between the main thread and Web Workers, automatically handling:

- Promise-based async method calls
- Automatic transferable detection (ArrayBuffer, etc.)
- Type-safe API with full TypeScript support
- Error propagation from worker to main thread

## Installation

Install the module:

```bash
npm install @wordpress/worker-threads
```

## Usage

### Worker Thread

Create a worker file that exposes methods to the main thread:

```typescript
// worker.ts
import { expose } from '@wordpress/worker-threads/worker';

const api = {
    async processImage( buffer: ArrayBuffer, quality: number ): Promise<ArrayBuffer> {
        // ... image processing logic
        return resultBuffer;
    },

    async calculateSum( a: number, b: number ): Promise<number> {
        return a + b;
    },
};

expose( api );

// Export the type for use with wrap() on the main thread
export type WorkerAPI = typeof api;
```

### Main Thread

Wrap the worker to get a type-safe proxy:

```typescript
// main.ts
import { wrap, terminate } from '@wordpress/worker-threads';
import type { WorkerAPI } from './worker';

// Create the worker
const worker = new Worker( new URL( './worker.js', import.meta.url ), {
    type: 'module',
} );

// Wrap it to get the RPC proxy
const api = wrap<WorkerAPI>( worker );

// Call methods as async functions
const result = await api.processImage( imageBuffer, 0.82 );
const sum = await api.calculateSum( 1, 2 );

// Clean up when done
terminate( api );
```

## API Reference

### Main Thread API

#### `wrap<T>( worker: Worker ): Remote<T>`

Wraps a Worker to provide a type-safe RPC interface. The returned proxy object allows calling methods on the worker as if they were local async functions.

#### `terminate( remote: Remote<unknown> ): void`

Terminates a wrapped worker and cleans up resources. Any pending calls will be rejected.

### Worker Thread API

#### `expose<T>( target: T ): void`

Exposes an object's methods to be called from the main thread. Should be called once in the worker script.

### Types

#### `Remote<T>`

A type that converts all methods of `T` to their async versions. Each method returns `Promise<Awaited<ReturnType>>`.

## Features

### Automatic Transferable Detection

The package automatically detects and transfers `ArrayBuffer` and other transferable objects, providing zero-copy performance for large data:

```typescript
// ArrayBuffers are automatically transferred, not copied
const result = await api.processImage( largeImageBuffer );
```

### Error Handling

Errors thrown in the worker are properly propagated to the main thread:

```typescript
try {
    await api.riskyOperation();
} catch ( error ) {
    console.error( 'Worker error:', error.message );
}
```

### TypeScript Support

Full type inference for method signatures:

```typescript
// TypeScript knows the return type and parameter types
const result: ArrayBuffer = await api.processImage( buffer, 0.82 );
```

## Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).

---
_Source: https://npm.io/package/@wordpress/worker-threads · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
