{"_id":"protomux@3.13.0","bugs":{"url":"https://github.com/holepunchto/protomux/issues"},"dist":{"shasum":"2b82a4100b207edf28be2ca08a96f2481d75df8f","tarball":"http://registry-npm.ywwl.com/protomux/download/protomux-3.13.0.tgz","fileCount":4,"integrity":"sha512-+3mF/lv4LHtYgB5/lfe5pf6PxE0TlFlccKJriRB738t3d8olbWgRLgBKDuthuZhoxZjIL7vYchS9im8OcnCCJA==","signatures":[{"sig":"MEQCIH60jLFwVNGprdPSwGH1Yz3oQCclIK4YHw7PIkuyAw4ZAiB3X0r1QD+3UyhVd3TUYjSE6eurcbTreTFtENVpRgsa8w==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIDJfgpn4zjdn4ukllLYcMQMkAKv0rmaGev38krhtgqrAAiEAqvvNCtIRMzp8KAOQPlqRcrxKLnZ7pNRflk4Sb85gQkY="}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/protomux@3.13.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":28595,"size":8017,"noattachment":false,"key":"/protomux/-/protomux-3.13.0.tgz"},"main":"index.js","name":"protomux","author":{"url":"@mafintosh","name":"Mathias Buus"},"gitHead":"30a595e292dbf38397580e30899de3eae5993f2f","license":"MIT","scripts":{"lint":"prettier --check . && lunte","test":"npm run test:node && npm run test:bare","format":"prettier --write .","test:bare":"brittle-bare test.js","test:node":"brittle-node test.js"},"version":"3.13.0","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"01c2994d-26c7-4a71-8a41-e9e9e010da42"}},"homepage":"https://github.com/holepunchto/protomux","repository":{"url":"git+https://github.com/holepunchto/protomux.git","type":"git"},"_npmVersion":"11.19.0","description":"Multiplex multiple message oriented protocols over a stream","directories":{},"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"}],"_nodeVersion":"24.21.0","dependencies":{"b4a":"^1.3.1","unslab":"^1.3.0","queue-tick":"^1.0.0","safety-catch":"^1.0.1","compact-encoding":"^3.0.0"},"_hasShrinkwrap":false,"devDependencies":{"lunte":"^1.0.0","brittle":"^3.0.0","prettier":"^3.6.2","@hyperswarm/secret-stream":"^6.0.0","prettier-config-holepunch":"^2.0.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/protomux_3.13.0_1791827079503_0.4107787334527333"},"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 = null }, callback)`\n\nRegister a callback to be called everytime a new channel is requested.\n\n#### `mux.unpair({ protocol, id = null })`\n\nUnregisters the pair callback.\n\n#### `mux.watch({ protocol, id = null }, callback)`\n\nRegister a callback to be called every time a channel for this `protocol` (and `id`, if given) becomes fully open. Unlike `pair`, any number of watchers can be registered for the same `protocol`/`id` - it's purely observational.\n\nIf `id` is `null` every channel for the given `protocol` is watched regardless of `id`.\n\n`callback` is called as `callback(channel)`. Watchers are passive and never affect the channel or the stream: throwing or returning a rejected promise emits the error via the `warning` event on the stream.\n\n#### `mux.unwatch({ protocol, id = null }, callback)`\n\nUnregisters a watcher callback for the given `protocol`/`id`.\n\n#### `mux.unwatchAll({ protocol, id = null })`\n\nUnregisters all watcher callbacks for the given `protocol`/`id`.\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  // unless the channel has already closed, in which case the stream\n  // is left alone but the error is emitted via the `warning` event\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":1791827079583,"_cnpm_publish_time":1791827079583,"dist-tags":{"latest":"3.13.1"}}