{"name":"thread-stream","version":"0.2.0","keywords":["worker","thread","threads","stream"],"author":{"name":"Matteo Collina","email":"hello@matteocollina.com"},"license":"MIT","_id":"thread-stream@0.2.0","maintainers":[{"name":"matteo.collina","email":"hello@matteocollina.com"}],"homepage":"https://github.com/mcollina/thread-stream#readme","bugs":{"url":"https://github.com/mcollina/thread-stream/issues"},"dist":{"shasum":"44fc209027a1ce44ce21510e1e7adf160d935f4d","tarball":"http://registry-npm.ywwl.com/thread-stream/download/thread-stream-0.2.0.tgz","fileCount":11,"integrity":"sha512-u9phgGhVmj41g2dHhU8Lw84Tr+UlWIl7XCk8GqGOjitL0fziGFoR4QXx8FNvCGTPg86101OuDipYwIclrRChxQ==","signatures":[{"sig":"MEUCIQCImKmHjkr31H1SztM41nxxchzFAnKXAwvLdvXL+uXIZwIgc/bdKF68uGxDZcsmKuhWSAJJzPME7XSxgGZC+p3k5JE=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":16957,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgUSiuCRA9TVsSAnZWagAAiM8P/i0re8EmNvB1iwI1CdNQ\nxWOGxmv8xUBwQomGqLdLPv2jxuwOIC/SBfMlKNmQ6p8UYaCAReelA5HxYMRQ\nlvK8QPxMNGOvUiynV19VSX89OC8+RZjOJQOelQmFiSnw7XrYnuHY1Jl0dYlx\n3zLMv/z1xEeK0ieT6V+6RfBc+Ypm9L9v1htFiqoUuodR/opRZyo++9DD1Ksg\nokGjdi6n0j9XIMtbcORj4L78+FjKMBWwkbrf4vPLh4OiYlE68hYJPLxdXVvn\negCDWC0r4RvF2D3QCAZ7v4it0zAK6y28m2lhIL248V6NOwEbGOoWfHbpjdw3\nZ92zJenEJJeSiNBfq4t5miZzWnp+5PdRY/iYUqLjgATDWulKyCzZb/m+diez\n4hNDdT0lvRz51hJJTetJlfqXfK3P/fi1hhIQLfIELm22MNuMttwBD5HC3ydx\nSEbvNuQf8pCm+BEVWtpdpqB95wzUltLM1gKxDKm10jyb93HdgBoTKpEXLxDI\n2GUZbSFWtqLywcZWqkeWQf0dfO9hu40gjmk711/xxTY29WfuAHGaa/NDi61P\n6amgFSjqHxCLygQoGvpezCel61ZevHPh9pSW0wfShdFG1wTpT/bs58eV++ov\n1xA1qO1Z+dewilbv5VSIZjkezGAvGhZszl5R+wwNulFldCRtUMA/P6iwGJ7I\n+jdg\r\n=bov3\r\n-----END PGP SIGNATURE-----\r\n","size":4948,"noattachment":false,"key":"/thread-stream/-/thread-stream-0.2.0.tgz"},"main":"index.js","hooks":{"pre-commit":"npm test"},"gitHead":"cfb008b0529caf4d9be41b3c1990101c4291f909","scripts":{"cov":"nyc tape test/*.test.js | tap-dot","test":"standard && tape test/*.test.js | tap-dot"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"repository":{"url":"git+https://github.com/mcollina/thread-stream.git","type":"git"},"_npmVersion":"7.6.3","description":"A streaming way to send data to a Node.js Worker Thread","directories":{},"_nodeVersion":"14.16.0","_hasShrinkwrap":false,"devDependencies":{"nyc":"^15.1.0","tape":"^5.2.2","husky":"^5.1.3","tap-dot":"^2.0.0","standard":"^16.0.3","fastbench":"^1.0.1","sonic-boom":"^1.3.2"},"_npmOperationalInternal":{"tmp":"tmp/thread-stream_0.2.0_1615931566115_0.15341319235121786","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":1615931566260,"_cnpm_publish_time":1615931566260,"dist-tags":{"latest":"4.2.1","three":"3.2.0"}}