{"name":"protomux","version":"3.2.1","author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"protomux@3.2.1","homepage":"https://github.com/mafintosh/protomux","bugs":{"url":"https://github.com/mafintosh/protomux/issues"},"dist":{"shasum":"401b11b3c3b066d54a7492d295e7dcc773141b0b","tarball":"http://registry-npm.ywwl.com/protomux/download/protomux-3.2.1.tgz","fileCount":6,"integrity":"sha512-UM1pVR64JE0QHkptRitW2SlqtGKLu2hZFzvP9G0UifdneKepNhz7hbT6IlhUjCguuFdW9qYZQ7fZM2qooSTpvA==","signatures":[{"sig":"MEUCIQCkCAi4Nue9OhU9qU1JyfrCt6gSWVE88sMNxkMva7VC9AIgJigRK87LqrG1xQhcjtmof45OXu2te6vQ3htR/h66k/s=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":26063,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiPH4UACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrMtA/9HUR/yMNp+VynhxIuHcgymIm5koJeZxALeYNagOayOpustZt3\r\nG5VEgS96ZHBEq9iPBeaf9l6PwWEBraWo1AAykv1u6b6VBDJ7oZb4EFjL1RLr\r\nLluxthI9RikAThvvZHQ3TZPTSpquC7RDTRSjo5eG55F8n35NPMnuI0FtTBeJ\r\nHT0hH7wMi278r00LmBVxdl62rqSZgxBwRyx4SbBYJMmqi5Wg94x+mkCsSXf5\r\nT+t04Hd2SLcsH+GCJiATAob36zrQWJcXTHMgM360cHz46JcBD9OZ7LF/fpyE\r\nl8U51TZ+0IYdoJkBukkgzgmDnuAr5CKKKEfChHNPQEF0LlTsyc9Mls/pe36b\r\nmXGMhNBQTqNUS9Oc0wKqyC4ZDKhXf5Rli3gpMTFTxRkK2+6w9Dw11Ttpp9do\r\n+Peg+lkFk6KF8/BZctLcn4qiMW6Mdy83PXk5QT4YAq4GJYoAYIOilr8RAoxc\r\nI5qhsi8RgSuXUv9E6Q1dXu4QIwm0yqJVth66jw/et/EJK5hu3EUIZZm6Kbkr\r\nzpba71tTWV4tWRVjWgG+2qN2EzoR2EC4yaVTYwuNadYWCx1ml2N/aQbGbig3\r\nNWHTftLu4obBLY9JDcYrVLW8gM6mAzWGd6eFL77wmPmFmarlH6CGLeWAETQD\r\n1gMb+I9ynS7BakgQ2uC34bClPYyiPeepdgc=\r\n=KcNE\r\n-----END PGP SIGNATURE-----\r\n","size":7142,"noattachment":false,"key":"/protomux/-/protomux-3.2.1.tgz"},"main":"index.js","gitHead":"734be1f07011c63e34f1c56d374a09e261eff248","scripts":{"test":"standard && brittle test.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"git+https://github.com/mafintosh/protomux.git","type":"git"},"_npmVersion":"6.14.16","description":"Multiplex multiple message oriented protocols over a stream","directories":{},"_nodeVersion":"16.13.2","dependencies":{"b4a":"^1.3.1","queue-tick":"^1.0.0","safety-catch":"^1.0.1","compact-encoding":"^2.5.1"},"_hasShrinkwrap":false,"devDependencies":{"brittle":"^2.0.1","standard":"^16.0.4","@hyperswarm/secret-stream":"^5.1.4"},"_npmOperationalInternal":{"tmp":"tmp/protomux_3.2.1_1648131604373_0.503494014493753","host":"s3://npm-registry-packages"},"readme":"# protomux\n\nMultiplex multiple message oriented protocols over a stream\n\n```\nnpm install protomux\n```\n\n## Usage\n\n``` js\nconst Protomux = require('protomux')\nconst c = require('compact-encoding')\n\n// By framed stream, it has be a stream that preserves the messages, ie something that length prefixes\n// like @hyperswarm/secret-stream\n\nconst mux = new Protomux(aStreamThatFrames)\n\n// Now add some protocol channels\n\nconst cool = mux.createChannel({\n  protocol: 'cool-protocol',\n  id: Buffer.from('optional binary id'),\n  onopen () {\n    console.log('the other side opened this protocol!')\n  },\n  onclose () {\n    console.log('either side closed the protocol')\n  }\n})\n\n// And add some messages\n\nconst one = cool.addMessage({\n  encoding: c.string,\n  onmessage (m) {\n    console.log('recv message (1)', m)\n  }\n})\n\nconst two = cool.addMessage({\n  encoding: c.bool,\n  onmessage (m) {\n    console.log('recv message (2)', m)\n  }\n})\n\n// open the channel\n\ncool.open()\n\n// And send some data\n\none.send('a string')\ntwo.send(true)\n```\n\n## API\n\n#### `mux = new Protomux(stream, [options])`\n\nMake a new instance. `stream` should be a framed stream, preserving the messages written.\n\nOptions include:\n\n``` js\n{\n  // Called when the muxer wants to allocate a message that is written, defaults to Buffer.allocUnsafe.\n  alloc (size) {}\n}\n```\n\n#### `mux = Protomux.from(stream | muxer, [options])`\n\nHelper to accept either an existing muxer instance or a stream (which creates a new one).\n\n#### `const channel = mux.createChannel(opts)`\n\nAdd a new protocol channel.\n\nOptions include:\n\n``` js\n{\n  // Used to match the protocol\n  protocol: 'name of the protocol',\n  // Optional additional binary id to identify this channel\n  id: buffer,\n  // Optional encoding for a handshake\n  handshake: encoding,\n  // Optional array of messages types you want to send/receive.\n  messages: [],\n  // Called when the remote side adds this protocol.\n  // Errors here are caught and forwared to stream.destroy\n  async onopen (handshake) {},\n  // Called when the channel closes - ie the remote side closes or rejects this protocol or we closed it.\n  // Errors here are caught and forwared to stream.destroy\n  async onclose () {},\n  // Called after onclose when all pending promises has resolved.\n  async ondestroy () {}\n}\n```\n\nSessions are paired based on a queue, so the first remote channel with the same `protocol` and `id`.\n\n__NOTE__: `mux.createChannel` returns `null` if the channel should not be opened, ie it's a duplicate channel or the remote has already closed this one.\n\nIf you want multiple sessions with the same `protocol` and `id`, set `unique: false` as an option.\n\n#### `const opened = mux.opened({ protocol, id })`\n\nBoolean that indicates if the channel is opened.\n\n#### `mux.pair({ protocol, id }, callback)`\n\nRegister a callback to be called everytime a new channel is requested.\n\n#### `mux.unpair({ protocol, id })`\n\nUnregisters the pair callback.\n\n#### `channel.open([handshake])`\n\nOpen the channel.\n\n#### `const m = channel.addMessage(opts)`\n\nAdd/register a message type for a certain encoding. Options include:\n\n``` js\n{\n  // compact-encoding specifying how to encode/decode this message\n  encoding: c.binary,\n  // Called when the remote side sends a message.\n  // Errors here are caught and forwared to stream.destroy\n  async onmessage (message) { }\n}\n```\n\n#### `m.send(data)`\n\nSend a message.\n\n#### `m.onmessage`\n\nFunction that is called when a message arrives.\n\n#### `m.encoding`\n\nThe encoding for this message.\n\n#### `channel.close()`\n\nCloses the protocol channel.\n\n#### `channel.cork()`\n\nCorking the protocol channel, makes it buffer messages and send them all in a batch when it uncorks.\n\n#### `channel.uncork()`\n\nUncork and send the batch.\n\n#### `mux.cork()`\n\nSame as `channel.cork` but on the muxer instance.\n\n#### `mux.uncork()`\n\nSame as `channel.uncork` but on the muxer instance.\n\n#### `for (const channel of muxer) { ... }`\n\nThe muxer instance is iterable, so you can iterate over all the channels.\n\n#### `mux.isIdle()`\n\nConvenience method that returns true if the number of channels is currently 0.\n\n## License\n\nMIT\n","publish_time":1648131604550,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_cnpm_publish_time":1648131604550,"dist-tags":{"latest":"3.13.1"}}