{"name":"thread-stream","version":"0.11.0","keywords":["worker","thread","threads","stream"],"author":{"name":"Matteo Collina","email":"hello@matteocollina.com"},"license":"MIT","_id":"thread-stream@0.11.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":"375226f1488e11611f7d65fe3d0c2586a05da91e","tarball":"http://registry-npm.ywwl.com/thread-stream/download/thread-stream-0.11.0.tgz","fileCount":30,"integrity":"sha512-uzfig6tGx9SpdZSkkp++gLQnd7S7RDOQkZM8OiURY4E2ZsnanrAHQKc8z9lOlAqO4KBHh+KXU8+oZUOhFG9zbQ==","signatures":[{"sig":"MEUCIQCqJ5ImgOW/nDjYslyp5goT9pq6kYPGphPLHOXMGx/8WgIgObrItlwDQkr1FP6xFtuZ2rFnPCyGnAP63BwN9fvds9s=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":41202,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJg5c2bCRA9TVsSAnZWagAAfoUQAJyulwNitVT0n/a8heQu\n6FhPPGaY/YZOjXhb6tDcGOBKWTVmmdJnnPMlqFeipG658AZ+KVoQ8Yv0dS+5\nqsrB+5+h8GR6QBrbv1+PSxsCT5cUH8xhN2b1uM5grvEWqW1mX58YQufc9czt\n0ibD8nMkekNKyPhg1BVYJ4qQ96YGk7G2qKimrp/MxVqtRe3zQZ3yyKiHOI8i\nc+umxJ+/JtVMULxW9MoZGS35JP/zD9uYen2tfLk+f8x51jVfZioIUoqB+rBB\nGCL3ybNWNbuR5h33ScW39gR49Whvv4U3psoPOPBy2hlX124K6+Oc93ZPxJLC\nTzsEl8SmH9w79OcUxP30r4MoPdxaG4NJBmNiMoKk37gEQaC46ciuF6SgR/GI\nVCYbk8amgDnUGylFBJbnr3IntG+Dyx3+M73aQfwUhrXipKOZ267JkEg7/PEH\ncnpfSzeqo4HT4mjEGPytqth2jHMYnbk49V7pcblsByJIxHfAne+imh/+BnBK\n6w0NMHU80c+x5bt8BWxtBaZiTiiEQZs2Tt8Lbdk1xeX3zU+k1V4HVSQUgyPS\nWIFlLa/xM8aCEzktrvbZlm6/iWkbqlpE/uSAUKFKdLjlcnXjM0VW9+aXpiz2\nQnQBKGo9+dF99ngiuhoeGr+2ybUw5rgi1sfkKR2R6wDyncmQJSJlFDNSioiv\nWgU0\r\n=RGER\r\n-----END PGP SIGNATURE-----\r\n","size":9723,"noattachment":false,"key":"/thread-stream/-/thread-stream-0.11.0.tgz"},"main":"index.js","gitHead":"2dbe89cbc9a33f0df25d00459775824dee4bffb7","scripts":{"test":"standard && tap --no-check-coverage test/*.test.*js","prepare":"husky install","test:ci":"standard && tap \"test/**/*.test.*js\" --no-check-coverage --coverage-report=lcovonly"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"repository":{"url":"git+https://github.com/mcollina/thread-stream.git","type":"git"},"_npmVersion":"6.14.13","description":"A streaming way to send data to a Node.js Worker Thread","directories":{},"_nodeVersion":"14.17.0","_hasShrinkwrap":false,"devDependencies":{"tap":"^15.0.0","desm":"^1.1.0","husky":"^7.0.0","standard":"^16.0.3","fastbench":"^1.0.1","sonic-boom":"^2.0.1"},"_npmOperationalInternal":{"tmp":"tmp/thread-stream_0.11.0_1625673114768_0.8392848876769585","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":1625673115145,"_cnpm_publish_time":1625673115145,"dist-tags":{"latest":"4.2.1","three":"3.2.0"}}