Giter VIP home page Giter VIP logo

dataplex's Introduction

dataplex

binary stream multiplex router

testling badge

build status

Use this module to organize a collection of streaming interfaces multiplexed over a single bidirectional stream such as a web socket or a tcp connection.

example

We can build a tcp server to host up some book data and other assorted streams at different endpoints:

var net = require('net');
var dataplex = require('dataplex');
var through = require('through2');
var db = require('level')('books.db', { encoding: 'json' });
db.batch(require('./data.json'));

var server = net.createServer(function (stream) {
    var plex = dataplex();
    plex.add('/upper', function (opts) {
        return through(function (buf, enc, next) {
            this.push(buf.toString('utf8').toUpperCase());
            next();
        });
    });
    
    plex.add('/books', function (opts) {
        return db.createReadStream({ lt: 'book!\uffff', gt: 'book!' })
            .pipe(through.obj(function (row, enc, next) {
                this.push(row.key.split('!')[1] + '\n');
                next();
            }))
        ;
    });
    
    plex.add('/book/:name', function (opts, cb) {
        db.get('book!' + opts.name, function (err, row) {
            cb(err, JSON.stringify(row) + '\n');
        });
    });
    
    stream.pipe(plex).pipe(stream);
});
server.listen(5000);

and now we can build a client to open up multiple streams from the server, all multiplexed over a single tcp connection:

var dataplex = require('dataplex');
var net = require('net');

var con = net.connect(5000);
var plex = dataplex();
con.pipe(plex).pipe(con);

var stream = plex.open('/upper');
stream.pipe(process.stdout);
stream.end('beep boop\n');

plex.open('/book/snow-crash').pipe(process.stdout);
plex.open('/books').pipe(process.stdout);

All the streams get dumped to stdout as expected:

$ node server.js &
[1] 12025
$ node client.js
BEEP BOOP
{"author":"Neil Stephenson","year":1993}
cryptonomicon
diamond-age
snow-crash
^C

This example showed a client and server, but the protocol is fully symmetric so either side can define and consume streams from the other end.

methods

var dataplex = require('dataplex')

var plex = dataplex(opts)

You can optionally pass in an opts.router to use. The API of opts.router should match the routes module.

Passing in a custom opts.route is useful if you want to define your routes outside of a connection callback more similarly to how http routers are typically used. Consult the example/router directory for an example.

plex.add(pattern, fn)

Define a route on the underlying router instance.

pattern may contain parameters according to the syntax used by the routes module. These parameters and other parameters supplied to open() or get() will be available to the fn(opts, cb) function as the opts.

fn(opts, cb) will be called when the route is opened. fn should return a stream (readable, writable, or duplex) or it may call cb(err, result), with a single result to send on the outgoing stream.

var stream = plex.open(pathname, params={}, cb)

Return a duplex stream from the remote endpoint matching pathname. You can encode parameters directly into the pathname or pass them explicitly with params. params takes precedence.

Optionally, you can pass in a cb(err, body) to buffer the stream output into a single buffer.

var stream = plex.get(pathname, params={}, cb)

Return a duplex stream from the locally-defined routes matching pathname. You can encode parameters directly into the pathname or pass them explicitly with params. params takes precedence.

Optionally, you can pass in a cb(err, body) to buffer the stream output into a single buffer.

install

With npm do:

npm install dataplex

license

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.