# thread-stream

> A streaming way to send data to a Node.js Worker Thread

Latest version **4.2.0** (published 2026-05-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install thread-stream
pnpm add thread-stream
yarn add thread-stream
bun add thread-stream
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 4.2.0 |
| Published | 2026-05-14 |
| First published | 2021-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=20 |
| Dependencies | 1 |
| Unpacked size | 81.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 261 |
| Author | Matteo Collina |
| Maintainers | matteo.collina, jsumners, watson |
| Keywords | worker, thread, threads, stream |

## Links

- npm: https://www.npmjs.com/package/thread-stream
- Repository: https://github.com/mcollina/thread-stream
- Homepage: https://github.com/mcollina/thread-stream#readme
- Issues: https://github.com/mcollina/thread-stream/issues
- npm.io page: https://npm.io/package/thread-stream

## Dependencies (1)

- [real-require](https://npm.io/package/real-require.md) ^1.0.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 4.2.0 (latest) — 2026-05-14
- 3.2.0 (three) — 2026-06-02
- 4.1.0 — 2026-05-10
- 4.0.0 — 2025-12-02
- 3.1.0 — 2024-06-13
- 3.0.2 — 2024-05-31
- 3.0.1 — 2024-05-23
- 3.0.0 — 2024-04-29
- 2.7.0 — 2024-04-24
- 2.6.0 — 2024-04-22
- 2.5.0 — 2024-04-22
- 2.4.1 — 2023-10-03
- 2.4.0 — 2023-08-11
- 2.3.0 — 2023-01-11
- 2.2.0 — 2022-09-07
- … 32 more at https://npm.io/package/thread-stream/versions

## README

# thread-stream
[![npm version](https://img.shields.io/npm/v/thread-stream)](https://www.npmjs.com/package/thread-stream)
[![Build Status](https://img.shields.io/github/actions/workflow/status/pinojs/thread-stream/ci.yml?branch=main)](https://github.com/pinojs/thread-stream/actions)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

A streaming way to send data to a Node.js Worker Thread.

## install

```sh
npm i thread-stream
```

## Usage

```js
'use strict'

const ThreadStream = require('thread-stream')
const { join } = require('path')

const stream = new ThreadStream({
  filename: join(__dirname, 'worker.js'),
  workerData: { dest },
  workerOpts: {}, // Other options to be passed to Worker
  sync: false, // default
})

stream.write('hello')

// Asynchronous flushing
stream.flush(function () {
  stream.write(' ')
  stream.write('world')

  // Synchronous flushing
  stream.flushSync()
  stream.end()
})
```

`flush(cb)` waits for the worker destination flush when supported (`flush`, `flushSync`, or pending `drain`).

In `worker.js`:

```js
'use strict'

const fs = require('fs')
const { once } = require('events')

async function run (opts) {
  const stream = fs.createWriteStream(opts.dest)
  await once(stream, 'open')
  return stream
}

module.exports = run
```

Make sure that the stream emits `'close'` when the stream completes.
This can usually be achieved by passing the [`autoDestroy: true`](https://nodejs.org/api/stream.html#stream_new_stream_writable_options)
flag your stream classes.

The underlining worker is automatically closed if the stream is garbage collected.


### External modules

You may use this module within compatible external modules, that exports the `worker.js` interface.

```js
const ThreadStream = require('thread-stream')

const modulePath = require.resolve('pino-elasticsearch')

const stream = new ThreadStream({
  filename: modulePath,
  workerData: { node: 'http://localhost:9200' }
})

stream.write('log to elasticsearch!')
stream.flushSync()
stream.end()
```

This module works with `yarn` in PnP (plug'n play) mode too!

### Emit events

You can emit events on the ThreadStream from your worker using [`worker.parentPort.postMessage()`](https://nodejs.org/api/worker_threads.html#workerparentport).
Messages that do not carry a thread-stream protocol `code` are ignored.
For custom events, the message (JSON object) must have the following data structure:

```js
parentPort.postMessage({
  code: 'EVENT',
  name: 'eventName',
  args: ['list', 'of', 'args', 123, new Error('Boom')]
})
```

On your ThreadStream, you can add a listener function for this event name:

```js
const stream = new ThreadStream({
  filename: join(__dirname, 'worker.js'),
  workerData: {},
})
stream.on('eventName', function (a, b, c, n, err) {
  console.log('received:', a, b, c, n, err) // received: list of args 123 Error: Boom
})
```

### Post Messages

You can post messages to the worker by emitting a `message` event on the ThreadStream.

```js
const stream = new ThreadStream({
  filename: join(__dirname, 'worker.js'),
  workerData: {},
})
stream.emit('message', message)
```

On your worker, you can listen for this message using [`worker.parentPort.on('message', cb)`](https://nodejs.org/api/worker_threads.html#event-message).

```js
const { parentPort } = require('worker_threads')
parentPort.on('message', function (message) {
  console.log('received:', message)
})
```

## License

MIT

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