{"name":"stream-demux","version":"8.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":"^2.0.0","writable-consumable-stream":"^3.0.1"},"gitHead":"e3c0fc7beabdbc2e1dc348c143dfe10c6b6b6885","_id":"stream-demux@8.1.0","_nodeVersion":"16.16.0","_npmVersion":"8.17.0","dist":{"integrity":"sha512-20vtOmAj2EVzQZKZVmfyio16u/3QOKSvg+0ldgZeS+m2FNI1vKFoqggamagsPCXufdZ1Tk8VvAM/HV/YUmRbSg==","shasum":"00c09fc6a969dddd939da7d689d881c46bb37835","tarball":"http://registry-npm.ywwl.com/stream-demux/download/stream-demux-8.1.0.tgz","fileCount":6,"unpackedSize":30994,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDz8NrzsxXAnZCvMqhtZ8YHffDOjCkd7apN2JpbEXBmmgIhAKKX3Zf95lW5m1rKvMgu+1k8TIPuAS96YmVd5nEii/LU"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjQ85+ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpWzRAAiJ3S59p7080lScu6hCy3HIpakg2EN6hkl+SOXaqS+W58NGuK\r\nCvspvJCL4DQ1ERJOfAr0/YEZCDYZTsYx51qKPasnd5IppRjNGQj89gBXJNyp\r\nkB2a7F+gu6JEUZgVxFghBirv8H7+P5AX8K2190nZZI7AIMET+na92C6ixlr7\r\naZe5MojRDDGVKDojQa+tz/Q7DKW8dBCqUBPXZiZ90baRluDk/87+Q28H1wBr\r\njgDLhHcjhRsv4+sEw3sMOdUae/eKtQaTWeFHQD2ziEx3BrD/dZbL20oAbCk9\r\nNjlDf8GQn4pB7NvUE3xmoX98axQd1ClK6uKszgOgpAAG8LCyNKdc3TAtjUpf\r\n4C7YbdLezfF/ClUpU2gkNH+4hZ4dBRdxfQbTeR1ST97IF7YCz/6uIRct/GfE\r\noW6xkL9XPhLvmHZW1urdCuJ3AyxFGT4X4Wyf3YGGj0MFLB78DR0qxP0cz4Zd\r\no8rhVsKLMvihPf+57u35c0edhmLu6yOE1E+Rr/FikvYKbpwTYKnZ/pnxwqzs\r\nwBBQe+gP0nYhUDA8mNO6kOxjNh/+LKe6oevsZHM8dzQAd2Rp/L0pur2UphCs\r\n4iAwG32eF3NNgF53ZAUc9ubvWJIKrE8kOFUQOhUXcECxdEnnPQtjZartkTdW\r\n4em980TZgfNtJfjcIPQGk0652yWoBh2ex7E=\r\n=ZkWS\r\n-----END PGP SIGNATURE-----\r\n","size":5490,"noattachment":false,"key":"/stream-demux/-/stream-demux-8.1.0.tgz"},"_npmUser":{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"},"directories":{},"maintainers":[{"name":"topcloudsystems","email":"grosjona@yahoo.com.au"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/stream-demux_8.1.0_1665388158176_0.8242858673431135"},"_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":1665388158359,"_cnpm_publish_time":1665388158359,"dist-tags":{"latest":"10.2.1"}}