CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
README.md136 linesDownload Raw Back to thread-stream
1# thread-stream2[![npm version](https://img.shields.io/npm/v/thread-stream)](https://www.npmjs.com/package/thread-stream)3[![Build Status](https://img.shields.io/github/actions/workflow/status/pinojs/thread-stream/ci.yml?branch=main)](https://github.com/pinojs/thread-stream/actions)4[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)5 6A streaming way to send data to a Node.js Worker Thread.7 8## install9 10```sh11npm i thread-stream12```13 14## Usage15 16```js17'use strict'18 19const ThreadStream = require('thread-stream')20const { join } = require('path')21 22const stream = new ThreadStream({23  filename: join(__dirname, 'worker.js'),24  workerData: { dest },25  workerOpts: {}, // Other options to be passed to Worker26  sync: false, // default27})28 29stream.write('hello')30 31// Asynchronous flushing32stream.flush(function () {33  stream.write(' ')34  stream.write('world')35 36  // Synchronous flushing37  stream.flushSync()38  stream.end()39})40```41 42In `worker.js`:43 44```js45'use strict'46 47const fs = require('fs')48const { once } = require('events')49 50async function run (opts) {51  const stream = fs.createWriteStream(opts.dest)52  await once(stream, 'open')53  return stream54}55 56module.exports = run57```58 59Make sure that the stream emits `'close'` when the stream completes.60This can usually be achieved by passing the [`autoDestroy: true`](https://nodejs.org/api/stream.html#stream_new_stream_writable_options)61flag your stream classes.62 63The underlining worker is automatically closed if the stream is garbage collected.64 65 66### External modules67 68You may use this module within compatible external modules, that exports the `worker.js` interface.69 70```js71const ThreadStream = require('thread-stream')72 73const modulePath = require.resolve('pino-elasticsearch')74 75const stream = new ThreadStream({76  filename: modulePath,77  workerData: { node: 'http://localhost:9200' }78})79 80stream.write('log to elasticsearch!')81stream.flushSync()82stream.end()83```84 85This module works with `yarn` in PnP (plug'n play) mode too!86 87### Emit events88 89You can emit events on the ThreadStream from your worker using [`worker.parentPort.postMessage()`](https://nodejs.org/api/worker_threads.html#workerparentport).90The message (JSON object) must have the following data structure:91 92```js93parentPort.postMessage({94  code: 'EVENT',95  name: 'eventName',96  args: ['list', 'of', 'args', 123, new Error('Boom')]97})98```99 100On your ThreadStream, you can add a listener function for this event name:101 102```js103const stream = new ThreadStream({104  filename: join(__dirname, 'worker.js'),105  workerData: {},106})107stream.on('eventName', function (a, b, c, n, err) {108  console.log('received:', a, b, c, n, err) // received: list of args 123 Error: Boom109})110```111 112### Post Messages113 114You can post messages to the worker by emitting a `message` event on the ThreadStream.115 116```js117const stream = new ThreadStream({118  filename: join(__dirname, 'worker.js'),119  workerData: {},120})121stream.emit('message', message)122```123 124On your worker, you can listen for this message using [`worker.parentPort.on('message', cb)`](https://nodejs.org/api/worker_threads.html#event-message).125 126```js127const { parentPort } = require('worker_threads')128parentPort.on('message', function (message) {129  console.log('received:', message)130})131```132 133## License134 135MIT136