jschan is a JavaScript port of libchan based around node streams
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.
npm install jschan --save
This example exposes a service over SPDY. It is built to be interoperable with the original libchan version rexec.
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);
'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);
})
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.
- Session Interface
session.WriteChannel()
- Channel Interface
channel.ReadChannel()
channel.WriteChannel()
channel.BinaryStream()
jschan.memorySession()
jschan.streamSession()
jschan.spdyClientSession()
jschan.spdyServer()
jschan.websocketClientSession()
jschan.websocketServer()
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).
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.
function (channel) { }
Emitted each time there is a new Channel. The channel will always be a Readable stream.
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
, istrue
when you can read from the channel, e.g.chan.pipe(something)
isWriteChannel
, istrue
when you can write to the channel, e.g.something.pipe(chan)
Returns a nested read channel, this channel will wait for data from the other party.
Returns a nested write channel, this channel will buffer data up until is received by the other party. It fully respect backpressure.
Returns a nested duplex binary stream. It fully respect backpressure.
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();
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
orfalse
(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
orfalse
(default false), specifies if this is the server component or the client component.
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.
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.
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
});
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);
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.
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]
MIT