Coder Social home page Coder Social logo

jschan's Introduction

jschan  Build Status

jschan is a JavaScript port of libchan based around node streams

Status

The jschan API should be stable at this point, but libchan is a developing standard and there may be breaking changes in future.

When used over the standard SPDY transport, jschan is compatible with the current Go reference implementation.

We also have a websocket transport to allow us to run jschan on the browser. This feature is not covered by the spec, and is not compatible with other implementations.

Install

npm install jschan --save

Example

This example exposes a service over SPDY. It is built to be interoperable with the original libchan version rexec.

Server

The server opens up a jschan server to accept new sessions, and then execute the requests that comes through the channel.

'use strict';

var jschan = require('../../');
var childProcess = require('child_process');
var server = jschan.spdyServer();
server.listen(9323);

function handleReq(req) {
  var child = childProcess.spawn(
    req.Cmd,
    req.Args,
    {
      stdio: [
        'pipe',
        'pipe',
        'pipe'
      ]
    }
  );

  req.Stdin.pipe(child.stdin);
  child.stdout.pipe(req.Stdout);
  child.stderr.pipe(req.Stderr);

  child.on('exit', function(status) {
    req.StatusChan.write({ Status: status });
  });
}

function handleChannel(channel) {
  channel.on('data', handleReq);
}

function handleSession(session) {
  session.on('channel', handleChannel);
}

server.on('session', handleSession);

Client

'use strict';

var usage = process.argv[0] + ' ' + process.argv[1] + ' command <args..>';

if (!process.argv[2]) {
  console.log(usage)
  process.exit(1)
}

var jschan = require('jschan');
var session = jschan.spdyClientSession({ port: 9323 });
var sender = session.WriteChannel();

var cmd = {
  Args: process.argv.slice(3),
  Cmd: process.argv[2],
  StatusChan: sender.ReadChannel(),
  Stderr: process.stderr,
  Stdout: process.stdout,
  Stdin: process.stdin
};

sender.write(cmd);

cmd.StatusChan.on('data', function(data) {
  sender.end();
  setTimeout(function() {
    console.log('ended with status', data.Status);
    process.exit(data.Status);
  }, 500);
})

What can we write as a message?

You can write:

  • Any plain JS object, string, number, ecc that can be serialized by msgpack5
  • Any channels, created from the Channel interface
  • Any binary node streams, these will automatically be piped to jschan bytestreams, e.g. you can send a fs.createReadStream() as it is. Duplex works too, so you can send a TCP connection, too.

What is left out?

  • Your custom objects, we do not want you to go through that route
  • objectMode streams that contains JS objects: eventually we might want to autoconvert those to channels, if you care about this get in touch.

API


Session Interface

A session identifies an exchange of channels between two parties: an initiator and a recipient. Top-level channels can only be created by the initiator in 'write' mode, with WriteChannel().

Channels are unidirectional, but they can be nested (more on that later).

session.WriteChannel()

Creates a Channel in 'write mode', e.g. a streams.Writable. The channel follows the interface defined in Channel Interface. The stream is in objectMode with an highWaterMark of 16.

Event: 'channel'

function (channel) { }

Emitted each time there is a new Channel. The channel will always be a Readable stream.


Channel Interface

A Channel is a Stream and can be a Readable or Writable depending on which side of the communication you are. A Channel is never a duplex.

In order to send messages through a Channel, you can use standards streams methods. Moreover, you can nest channels by including them in a message, like so:

var chan = session.WriteChannel();
var ret  = chan.ReadChannel();

ret.on('data', function(res) {
  console.log('response', res);
});

chan.write({ returnChannel: ret });

Each channel has two properties to indicate its direction:

  • isReadChannel, is true when you can read from the channel, e.g. chan.pipe(something)
  • isWriteChannel, is true when you can write to the channel, e.g. something.pipe(chan)

channel.ReadChannel()

Returns a nested read channel, this channel will wait for data from the other party.

channel.WriteChannel()

Returns a nested write channel, this channel will buffer data up until is received by the other party. It fully respect backpressure.

channel.BinaryStream()

Returns a nested duplex binary stream. It fully respect backpressure.


jschan.memorySession()

Returns a session that works only through the current node process memory.

This is an examples that uses the in memory session:

'use strict';

var jschan  = require('jschan');
var session = jschan.memorySession();
var assert  = require('assert');

session.on('channel', function server(chan) {
  // chan is a Readable stream
  chan.on('data', function(msg) {
    var returnChannel  = msg.returnChannel;

    returnChannel.write({ hello: 'world' });
  });
});

function client() {
  // chan is a Writable stream
  var chan = session.WriteChannel();
  var ret  = chan.ReadChannel();
  var called = false;

  ret.on('data', function(res) {
    called = true;
    console.log('response', res);
  });

  chan.write({ returnChannel: ret });

  setTimeout(function() {
    assert(called, 'no response');
  }, 200);
}

client();

jschan.streamSession(readable, writable, opts)

Returns a session that works over any pair of readable and writable streams. This session encodes all messages in msgpack, and sends them over. It can work on top of TCP, websocket or other transports.

streamSession is not compatible with libchan.

Supported options:

  • header: true or false (default true), specifies if we want to prefix every msgPack message with its length. This is not needed if the underlining streams have their own framing.
  • server: true or false (default false), specifies if this is the server component or the client component.

jschan.spdyClientSession(options)

Creates a new SPDY client session, it supports the same options of spdy.Agent. This session can only be used to create new top-level write channels.

The only option that have a different default than spdy.Agent is rejectUnauthorized which defaults to false to support ease of usage.


jschan.spdyServer(options)

Creates a new SPDY server, it supports the same options of spdy.createServer. It also return a SPDY server, which is configured to emit the 'session' event when a new Session is started.

If the certificate needed by SPDY is not passed through, a new key pair is created on the fly using self-signed.


jschan.websocketClientSession(url)

Creates a new websocket session. The url can be in the form 'ws://localhost' or passes as an object. It is based on streamSession.

websocketClientSession is not compatible with libchan.

This method can also work in the browser thanks to Browserify.

Example:

var jschan  = require('jschan');
var session = jschan.websocketClientSession('ws://localhost:3000');
var chan    = session.WriteChannel();
var ret     = chan.ReadChannel();

ret.on('data', function(res) {
  console.log(res);
  session.close();
});

chan.end({
  hello:'world',
  returnChannel: ret
});

jschan.websocketServer(options)

Creates a new websocketServer, or attach the websocket handler to the passed-through httpServer object. It is based on streamSession.

websocketServer is not compatible with libchan.

If a new http server is created, remeber to call listen, like so:

'use strict';

var jschan = require('jschan');
var server = jschan.websocketServer();

function handleMsg(msg) {
  var stream = msg.returnChannel;
  delete msg.returnChannel;
  stream.end(msg);
}

function handleChannel(chan) {
  chan.on('data', handleMsg);
}

function handleSession(session) {
  session.on('channel', handleChannel);
}

server.on('session', handleSession);

server.listen(3000);

About LibChan

It's most unique characteristic is that it replicates the semantics of go channels across network connections, while allowing for nested channels to be transferred in messages. This would let you to do things like attach a reference to a remote file on an HTTP response, that could be opened on the client side for reading or writing.

The protocol uses SPDY as it's default transport with MSGPACK as it's default serialization format. Both are able to be switched out, with http1+websockets and protobuf fallbacks planned. SPDY is encrypted over TLS by default.

While the RequestResponse pattern is the primary focus, Asynchronous Message Passing is still possible, due to the low level nature of the protocol.

Graft

The Graft project is formed to explore the possibilities of a web where servers and clients are able to communicate freely through a microservices architecture.

"instead of pretending everything is a local function even over the network (which turned out to be a bad idea), what if we did it the other way around? Pretend your components are communicating over a network even when they aren't." Solomon Hykes (of Docker fame) on LibChan - [link]

Find out more about Graft

Contributors

License

MIT

jschan's People

Contributors

adrianrossouw avatar mcollina avatar pelger avatar

Watchers

 avatar  avatar  avatar

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.