{"name":"thread-stream","version":"2.1.0","keywords":["worker","thread","threads","stream"],"author":{"name":"Matteo Collina","email":"hello@matteocollina.com"},"license":"MIT","_id":"thread-stream@2.1.0","maintainers":[{"name":"davidmarkclements","email":"david.mark.clements@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"watson","email":"w@tson.dk"}],"homepage":"https://github.com/mcollina/thread-stream#readme","bugs":{"url":"https://github.com/mcollina/thread-stream/issues"},"dist":{"shasum":"d560dd8b9d09482b0e2e876a96c229c374870836","tarball":"http://registry-npm.ywwl.com/thread-stream/download/thread-stream-2.1.0.tgz","fileCount":47,"integrity":"sha512-5+Pf2Ya31CsZyIPYYkhINzdTZ3guL+jHq7D8lkBybgGcSQIKDbid3NJku3SpCKeE/gACWAccDA/rH2B6doC5aA==","signatures":[{"sig":"MEYCIQCKPtazzI51AO/bQZvECYSoFYFgCOdcdE8dKIEiVNSeSgIhAO6gXTEvoNz3WA1/5nSvs11nfaQZop2Qw/GHBmxxwflY","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":61670,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjA7dLACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpvrA//Yn2B2SB2FofczOXXm43e4B/tcXjAh14SFSNoPkwe2CRmuLv3\r\nAhcfoScsv8ENKSc6p0XP6Eg+PA5oH2jW/jKAqTyjDgo9bDO9qRIuZ+aHdosk\r\n/sDSEfsjlAeT8QC+/7pmV51XK7CIYYkZhFOPDpk87SBwNxM2U8lH6tIeC2PB\r\ni2MZ0EzeUvj/u+2KgZi9QiAqKNujPdvR4o8FIJ5z6khy05GOP0PP+TuzAN4f\r\nvIsXZqx4xAuZTZleVNJJSN3I/DO2fVEYOni79neGyJuJOMeVltDG6D6Uzoeh\r\npwI1eKbYoBng7gjctH4VuVGyGSiNGjbKqqb7SrDOugiqO037fHK9aZVLGuat\r\n0MXopxxuo0mXuSl9JkZ821iLL+0Bfuv6P7SwLp3Ua2RgsBMWR4sJtjEGUNwk\r\nJ4kaZYNEPjb0oseB58BEd37Qyy/neLlqwV0O4mPoWhSdQV31j3POAISjklj5\r\ne1AjTR2HXUvLLe96Hv928qJaxdPro6Fx4kD/vG/lcIpHvvSmyuXD+fw3Zaiv\r\nRgStiN+7anqYXYLDrYjKvyD5lXqEGzSBkbuwurEE0h/yBpNriReqB7E/Vl5t\r\nAyuaFhJaIrkVRf0UQrOPFYvb2Vq4zFM7RhKCqNwDjY2wGyx59YG4KhqhCxG/\r\nDjczuTzEeAFyAy00J1BFFOCp6YoDEPkrFdE=\r\n=qUOx\r\n-----END PGP SIGNATURE-----\r\n","size":15794,"noattachment":false,"key":"/thread-stream/-/thread-stream-2.1.0.tgz"},"main":"index.js","types":"index.d.ts","gitHead":"7135302d32a7306443c3c6d7b38fa28bc1e698ca","scripts":{"test":"standard && npm run transpile && tap test/*.test.*js && tap --ts test/*.test.*ts","prepare":"husky install","test:ci":"standard && npm run transpile && npm run test:ci:js && npm run test:ci:ts","test:yarn":"npm run transpile && tap \"test/**/*.test.js\" --no-check-coverage","transpile":"sh ./test/ts/transpile.sh","test:ci:js":"tap --no-check-coverage --coverage-report=lcovonly \"test/**/*.test.*js\"","test:ci:ts":"tap --ts --no-check-coverage --coverage-report=lcovonly \"test/**/*.test.*ts\""},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"standard":{"ignore":["test/ts/**/*"]},"repository":{"url":"git+https://github.com/mcollina/thread-stream.git","type":"git"},"_npmVersion":"8.17.0","description":"A streaming way to send data to a Node.js Worker Thread","directories":{},"_nodeVersion":"16.15.1","dependencies":{"real-require":"^0.2.0"},"_hasShrinkwrap":false,"devDependencies":{"tap":"^16.2.0","desm":"^1.3.0","husky":"^8.0.1","ts-node":"^10.8.0","standard":"^17.0.0","fastbench":"^1.0.1","@types/tap":"^15.0.0","sonic-boom":"^3.0.0","typescript":"^4.7.2","@types/node":"^18.0.0","why-is-node-running":"^2.2.2"},"_npmOperationalInternal":{"tmp":"tmp/thread-stream_2.1.0_1661187915087_0.4757516754803477","host":"s3://npm-registry-packages"},"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\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).\nThe 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":1661187915331,"_cnpm_publish_time":1661187915331,"dist-tags":{"latest":"4.2.1","three":"3.2.0"}}