{"name":"stream-demux","version":"6.1.0","description":"A consumable stream demultiplexer.","main":"index.js","scripts":{"test":"./node_modules/mocha/bin/_mocha --reporter spec --timeout 10000 --slow 10000"},"repository":{"type":"git","url":"git+https://github.com/SocketCluster/stream-demux.git"},"keywords":["iterable","async","stream","demux","demultiplexer","for-await-of"],"author":{"name":"Jonathan Gros-Dubois"},"license":"MIT","bugs":{"url":"https://github.com/SocketCluster/stream-demux/issues"},"homepage":"https://github.com/SocketCluster/stream-demux#readme","devDependencies":{"mocha":"^5.0.5"},"dependencies":{"consumable-stream":"^1.0.0","writable-consumable-stream":"^1.1.1"},"gitHead":"c4d21aed13aa9d3b1125ba347da60dac9ead09ba","_id":"stream-demux@6.1.0","_npmVersion":"5.6.0","_nodeVersion":"10.0.0","_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"dist":{"integrity":"sha512-8SozCEpiH5KI9b/Yb5lGnudDiSCp7tf425Cm3mrwQoiXqMD+Akukhg6DFNbkrS7TzFRUHzJ4thZ9WUKIcw1sCg==","shasum":"78a73433c7ce27f7e82d569ef867357cac0dbbba","tarball":"http://registry-npm.ywwl.com/stream-demux/download/stream-demux-6.1.0.tgz","fileCount":6,"unpackedSize":34801,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcbfLBCRA9TVsSAnZWagAA9SoP/2zszpcVJCMHSYKq9Gap\nnFyZl5K6kaeojCJe2dtSpSWOlCP+DdoXpDizsVrp7uifmFqWbcbrndKrfYqG\nvj5bnlLeqlwFUCP6OH/6IguM1nB6drfiPMYlAQeVj9Hi7RBcUzUl0Uf2VVMP\n04vRYqKEVYr3vtde+3Txww+lm4IfNgRzw/C6scVP3FD33oMlnzD5QmhUEVxc\nVE1pD2DYjlGmGZla5l69BQrcP0rjKmW+kTX8SlCJm7IvAq+O+GaHORmO/5qh\nxd9Cl1PvPvQzD0peb/edwWYfGz/++Y9n9P2TosAJnANdiz0JwXvYfmhMMPsC\nupYsbitdHPx3eJUp+z3EEdYo6HuaAcpptw/cuPLGcRrAt9h3qhnsaVl/JYT6\nWnZMW14BQKt+5w+gdkG3R8lTZNuN4WSgPaqlKmcJDaS1FwNI4iG9SP3rA2Ck\n9jnkB75CXHcMr315hd8LWzAdAo/7bOeT/0twXmpgFxxrDtY3SS5ZAXE5fERx\nfcg8m5tRVto+9CY+DoqfUAdUJjkSmDiKCOAdachOQya0LC0UWfZ/yf8JWqcl\nnALxSSX8HHVAEntwYe/MIe1SUhZmjXvX3kLAROp+FJweuwYmvB5RN5kOxR+4\nOv3X/waJ1VvdS7As852ySBfosYOMueraCiKgB9mCUnRoPo7x0HrQHcXFt1HY\nIfWw\r\n=QcM5\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCID9RRrk+Gwcby5uPjpeE9hToqBTfQwgE/EBkVKsjpdW+AiEAhBNZXk/IcpmiPXhLt0gtKeTYa0b9sRlXmpVJXjNEcwo="}],"size":5886,"noattachment":false,"key":"/stream-demux/-/stream-demux-6.1.0.tgz"},"maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/stream-demux_6.1.0_1550709440783_0.562626675940576"},"_hasShrinkwrap":false,"readme":"# stream-demux\nAn consumable stream demultiplexer.\n\nLets you write data to multiple consumable streams from a central place without keeping any references to those streams.\nThe `StreamDemux` class returns streams of class `DemuxedConsumableStream` (base class `ConsumableStream`).  \nSee https://github.com/SocketCluster/consumable-stream\n\nThis library uses a queue which is implemented as a singly-linked list; this allows each loop to consume at its own pace without missing any events (supports nested await statements). An 'event' in the queue can be garbage-collected as soon as the slowest consumer moves its pointer past it.\n\n## Installation\n\n```\nnpm install stream-demux\n```\n\n## Usage\n\n### Consuming using async loops\n\n```js\nlet demux = new StreamDemux();\n\n(async () => {\n  // Consume data from 'abc' stream.\n  let substream = demux.stream('abc');\n  for await (let packet of substream) {\n    console.log('ABC:', packet);\n  }\n})();\n\n(async () => {\n  // Consume data from 'def' stream.\n  let substream = demux.stream('def');\n  for await (let packet of substream) {\n    console.log('DEF:', packet);\n  }\n})();\n\n(async () => {\n  // Consume data from 'def' stream.\n  // Can also work with a while loop for older environments.\n  // Can have multiple loops consuming the same stream at\n  // the same time.\n  // Note that you can optionally pass a number n to the\n  // createConsumer(n) method to force the iteration to\n  // timeout after n milliseconds of inactivity.\n  let consumer = demux.stream('def').createConsumer();\n  while (true) {\n    let packet = await consumer.next();\n    if (packet.done) break;\n    console.log('DEF (while loop):', packet.value);\n  }\n})();\n\n(async () => {\n  for (let i = 0; i < 10; i++) {\n    await wait(10);\n    demux.write('abc', 'message-abc-' + i);\n    demux.write('def', 'message-def-' + i);\n  }\n  demux.close('abc');\n  demux.close('def');\n})();\n\n// Utility function for using setTimeout() with async/await.\nfunction wait(duration) {\n  return new Promise((resolve) => {\n    setTimeout(() => {\n      resolve();\n    }, duration);\n  });\n}\n```\n\n### Consuming using the once method\n\n```js\n// Log the next received packet from the abc stream.\n(async () => {\n  // The returned promise never times out.\n  let packet = await demux.stream('abc').once();\n  console.log('Packet:', packet);\n})();\n\n// Same as above, except with a timeout of 10 seconds.\n(async () => {\n  try {\n    let packet = await demux.stream('abc').once(10000);\n    console.log('Packet:', packet);\n  } catch (err) {\n    // If no packets are written to the 'abc' stream before\n    // the timeout, an error will be thrown and handled here.\n    // The err.name property will be 'TimeoutError'.\n    console.log('Error:', err);\n  }\n})();\n```\n\n## Goal\n\nThe goal of this module is to facilitate functional programming patterns which decrease the probability of memory leaks and race conditions.\nIt serves as an alternative to callback-based event handling.\n","publish_time":1550709440962,"_cnpm_publish_time":1550709440962,"dist-tags":{"latest":"10.2.1"}}