{"_id":"thread-stream@4.2.1","tsd":{"directory":"test/types"},"bugs":{"url":"https://github.com/mcollina/thread-stream/issues"},"dist":{"shasum":"8963c203149421081c574e658d7a46566f082eae","tarball":"http://registry-npm.ywwl.com/thread-stream/download/thread-stream-4.2.1.tgz","fileCount":74,"integrity":"sha512-Stg6edJXC/6/xqGwSXwFokrUhChJ18amEjpKH89WrQQ0oVFDF+pUUcTpiwWhFUrE89Vf16FBOlwhaw+lescxMQ==","signatures":[{"sig":"MEUCIGHu0YE5T4CyJvlh5HL7Ti+8W1v1Je2K1BY547FPhfvYAiEAlLhEtYyHmSSkn/OHRgBon7Jy689X3VnwSOL692rTaHw=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDWi0Mya0jmCy/AfYwed1LEHXEK0rl5bUyP7NVosi93wQIgaPj47mkFzysNbETeVjWtLD/Bh7t6k7/lu7iotoRzDS4="}],"unpackedSize":102614,"size":26050,"noattachment":false,"key":"/thread-stream/-/thread-stream-4.2.1.tgz"},"main":"index.js","name":"thread-stream","types":"index.d.ts","author":{"name":"Matteo Collina","email":"hello@matteocollina.com"},"engines":{"node":">=20"},"gitHead":"aed8d4c37feef1d97d8b2f5c959c6d89e9656329","license":"MIT","scripts":{"lint":"eslint","test":"npm run lint && npm run build && npm run test:types && npm run transpile && borp --expose-gc --pattern \"test/*.test.{js,mjs}\"","build":"tsc --noEmit","test:ci":"npm run lint && npm run test:types && npm run transpile && borp --expose-gc --pattern \"test/*.test.{js,mjs}\"","test:yarn":"npm run transpile && borp --expose-gc --pattern \"test/*.test.js\"","transpile":"sh ./test/ts/transpile.sh","test:types":"tsd"},"version":"4.2.1","_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"homepage":"https://github.com/mcollina/thread-stream#readme","keywords":["worker","thread","threads","stream"],"repository":{"url":"git+https://github.com/mcollina/thread-stream.git","type":"git"},"_npmVersion":"11.16.0","description":"A streaming way to send data to a Node.js Worker Thread","directories":{},"maintainers":[{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"jsumners","email":"james.sumners@gmail.com"}],"_nodeVersion":"24.18.0","dependencies":{"real-require":"^1.0.0"},"_hasShrinkwrap":false,"devDependencies":{"tsd":"^0.33.0","borp":"^1.0.0","desm":"^1.3.0","eslint":"^9.39.1","ts-node":"^10.8.0","fastbench":"^1.0.1","sonic-boom":"^5.0.0","typescript":"~5.7.3","@types/node":"^26.1.1","neostandard":"^0.13.0","@yao-pkg/pkg":"^6.0.0","pino-elasticsearch":"^9.0.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/thread-stream_4.2.1_1793640608832_0.013054984775716982"},"readme":"# thread-stream\n[![npm version](https://img.shields.io/npm/v/thread-stream)](https://www.npmjs.com/package/thread-stream)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/pinojs/thread-stream/ci.yml?branch=main)](https://github.com/pinojs/thread-stream/actions)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)\n\nA streaming way to send data to a Node.js Worker Thread.\n\n## install\n\n```sh\nnpm i thread-stream\n```\n\n## Usage\n\n```js\n'use strict'\n\nconst ThreadStream = require('thread-stream')\nconst { join } = require('path')\n\nconst stream = new ThreadStream({\n  filename: join(__dirname, 'worker.js'),\n  workerData: { dest },\n  workerOpts: {}, // Other options to be passed to Worker\n  sync: false, // default\n})\n\nstream.write('hello')\n\n// Asynchronous flushing\nstream.flush(function () {\n  stream.write(' ')\n  stream.write('world')\n\n  // Synchronous flushing\n  stream.flushSync()\n  stream.end()\n})\n```\n\n`flush(cb)` waits for the worker destination flush when supported (`flush`, `flushSync`, or pending `drain`).\n\nIn `worker.js`:\n\n```js\n'use strict'\n\nconst fs = require('fs')\nconst { once } = require('events')\n\nasync function run (opts) {\n  const stream = fs.createWriteStream(opts.dest)\n  await once(stream, 'open')\n  return stream\n}\n\nmodule.exports = run\n```\n\nMake sure that the stream emits `'close'` when the stream completes.\nThis can usually be achieved by passing the [`autoDestroy: true`](https://nodejs.org/api/stream.html#stream_new_stream_writable_options)\nflag your stream classes.\n\nThe underlining worker is automatically closed if the stream is garbage collected.\n\n\n### External modules\n\nYou may use this module within compatible external modules, that exports the `worker.js` interface.\n\n```js\nconst ThreadStream = require('thread-stream')\n\nconst modulePath = require.resolve('pino-elasticsearch')\n\nconst stream = new ThreadStream({\n  filename: modulePath,\n  workerData: { node: 'http://localhost:9200' }\n})\n\nstream.write('log to elasticsearch!')\nstream.flushSync()\nstream.end()\n```\n\nThis module works with `yarn` in PnP (plug'n play) mode too!\n\n### Emit events\n\nYou can emit events on the ThreadStream from your worker using [`worker.parentPort.postMessage()`](https://nodejs.org/api/worker_threads.html#workerparentport).\nMessages that do not carry a thread-stream protocol `code` are ignored.\nFor custom events, the message (JSON object) must have the following data structure:\n\n```js\nparentPort.postMessage({\n  code: 'EVENT',\n  name: 'eventName',\n  args: ['list', 'of', 'args', 123, new Error('Boom')]\n})\n```\n\nOn your ThreadStream, you can add a listener function for this event name:\n\n```js\nconst stream = new ThreadStream({\n  filename: join(__dirname, 'worker.js'),\n  workerData: {},\n})\nstream.on('eventName', function (a, b, c, n, err) {\n  console.log('received:', a, b, c, n, err) // received: list of args 123 Error: Boom\n})\n```\n\n### Post Messages\n\nYou can post messages to the worker by emitting a `message` event on the ThreadStream.\n\n```js\nconst stream = new ThreadStream({\n  filename: join(__dirname, 'worker.js'),\n  workerData: {},\n})\nstream.emit('message', message)\n```\n\nOn your worker, you can listen for this message using [`worker.parentPort.on('message', cb)`](https://nodejs.org/api/worker_threads.html#event-message).\n\n```js\nconst { parentPort } = require('worker_threads')\nparentPort.on('message', function (message) {\n  console.log('received:', message)\n})\n```\n\n## License\n\nMIT\n","publish_time":1793640608932,"_cnpm_publish_time":1793640608932,"dist-tags":{"latest":"4.2.1","three":"3.2.0"}}