mux-demux-stream
Version:
Multiplexer and demultiplexter for binary/text streams.
118 lines (78 loc) • 3.19 kB
Markdown
Multiplexer and demultiplexter for binary/text streams with inspiration from the
book [Node.js Design Patterns](https://www.packtpub.com/web-development/nodejs-design-patterns)
by [Mario Casciaro](https://github.com/mariocasciaro).
Multiplex and demultiplex up to eight binary/text streams with a single
multiplexer or demultiplexer. Each chunk of data is prepended with a five bytes
header, including channel id and payload length.
| 1 byte (Channel ID) | 4 bytes (Payload length) | Original payload of variable length |
|---------------------|--------------------------|-------------------------------------|
```js
npm install mux-demux-stream
```
The module can be used both with a "simplex" syntax and with a "duplex" syntax
that offers some syntactic sugar over the simplex variant.
The simplex syntax is used for multiplexing several readable streams into a single
writable stream, or demultiplexing several writable streams into a single
readable stream.
The duplex syntax is more compact when for example implementing a protocol like
[](http://www.jsonrpc.org/specification) in a peer-to-peer fashion,
where each RPC node consist of a server and a client that share a common
bidirectional channel such as
[](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API).
(See [json-rpc-server-stream](https://github.com/claudijo/json-rpc-server-stream)
and [json-rpc-client-stream](https://github.com/claudijo/json-rpc-client-stream)
for a more comprehensive discussion about using JSON RPC 2.0 in a peer-to-peer
fashion.)
Require module and specific functions.
```js
var mux = require('mux-demux-stream').mux;
var demux = require('mux-demux-stream').demux;
```
Combine multiple streams to allow transmission over a single stream.
```js
var sourceA = createReadableStreamSomehow();
var sourceB = createReadableStreamSomehow();
var destination = createWritableStreamSomehow();
mux([sourceA, sourceB], destination);
```
Reconstruct the original streams from the data received from a shared stream.
```js
var source = createReadableStreamSomehow();
var destinationA = createWritableStreamSomehow();
var destinationB = createWritableStreamSomehow();
demux(source, [destinationA, destinationB]);
```
Require module. (Note that this is not the same `mux` function as in the example
above.)
```js
var mux = require('mux-demux-stream');
```
Internally the duplex variant uses "chaining" and closures to form a natural
stream syntax when several duplex streams are multiplexed into and from a shared
channel stream.
The following simplex syntax:
```js
mux([localClientStream, localServerStream], sharedChannel);
demux(sharedChannel, [localClientStream, localServerStream]);
```
is equivalent to the following duplex syntax:
```js
mux(localClientStream, localServerStream)
.pipe(sharedChannel)
.demux(localClientStream, localServerStream);
```
Run unit tests:
```js
npm test
```
[](LICENSE)