UNPKG

k-highway

Version:

Websocket and browserchannel transport for Racer

63 lines (47 loc) 1.47 kB
var Duplex = require('stream').Duplex; var util = require('util'); module.exports = createBrowserChannelStream; /** * @param {EventEmitters} client is a browserchannel client session for a given * browser window/tab that is has a connection * @return {Duplex} stream */ function createBrowserChannelStream(client) { var stream = new ClientStream(client); client.on('message', function onMessage(message) { stream.push(message); }); client.on('close', function() { // Signal data writing is complete. Emits the 'end' event stream.push(null); }); return stream; } function ClientStream(client) { Duplex.call(this, {objectMode: true}); this.client = client; var self = this; this.on('error', function(error) { console.warn('BrowserChannel client message stream error', error); self._stopClient(); }); // The server ended the writable stream. Triggered by calling stream.end() // in agent.close() this.on('finish', function() { self._stopClient(); }); } util.inherits(ClientStream, Duplex); ClientStream.prototype._read = function() {}; ClientStream.prototype._write = function(chunk, encoding, callback) { // Silently drop messages after the session is closed if (this.client.state === 'closed') return callback(); this.client.send(chunk); callback(); }; ClientStream.prototype._stopClient = function() { var client = this.client; client.stop(function() { client.close(); }); };