Skip to content

Length prefixed stream encoder and decoder for network transport

License

Notifications You must be signed in to change notification settings

rumkin/boundary-stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Boundary Stream

npm

Boundary stream converts passed data into length prefixed buffers. It helps to send data via tcp connection. Length prefixed protocols are more speed efficient then delimiter based (like HTTP).

Boundary stream can handle any size of message. The only limit is RAM size. It supports String or Buffer transfer and fits for JSON, MsgPack, CBOR and BORN encoders.

Install

Install via npm:

npm i boundary-stream

Usage

Example of raw Buffer message transfer.

Client

const net = require('net');
const boundary = require('boundary-stream');

const conn = net.connect(9090);

conn.on('connect', () => {
    const writer = boundary.writer();

    writer.pipe(conn);

    writer.write(Buffer.alloc(10 * 1024 * 1024)); // Send 10 MiB buffer
    writer.end();
});

// ...

Server

const net = require('net');
const boundary = require('boundary-stream');

const server = net.createServer(function(conn){
    let reader = boundary.reader();

    reader.on('data', (message) => {
        console.log(message.length); // -> 10485760
    });

    conn.pipe(reader);
});

// ...

API

writer() -> Writer{}

Create Writer instance.

reader() -> Reader{}

Create Reader instance.

Writer()

Writer constructor

Writer().write(String|Buffer)

Write message to stream.

Reader()

Reader constructor

License

MIT

About

Length prefixed stream encoder and decoder for network transport

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published