{
"repo": "protomux",
"timestamp": "2026-01-16T23:05:20.547Z",
"entryPoint": "index.js",
"exports": {
"packageJson": {
"main": "index.js"
}
},
"api": {
"file": "index.js",
"exports": [
{
"type": "default",
"name": "class"
}
],
"classes": [
{
"name": "Channel"
},
{
"name": "Protomux"
}
],
"functions": [
{
"name": "noop"
},
{
"name": "toKey"
},
{
"name": "byteSize"
},
{
"name": "isPromise"
},
{
"name": "encodingLength"
}
]
},
"readme": {
"content": "# 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",
"sections": [
"intro",
"usage",
"api",
"license"
],
"hasApi": true,
"hasExamples": true,
"exampleCount": 5,
"examples": [
"npm install protomux",
"const 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 // Called when the muxer wants to allocate a message that is written, defaults to Buffer.allocUnsafe.\n alloc (size) {}\n}"
]
},
"patterns": [
"peer-discovery",
"stream-multiplexing",
"binary-encoding"
]
}