diff --git a/lib/connection.js b/lib/connection.js index 6ebb13040..97af16056 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -4,12 +4,12 @@ var events = require('events') , generate = require('./generate') , parse = require('./parse') , Writable = require("stream").Writable - , PassThrough = require("stream").PassThrough , delay = global.setImmediate; +var Writable = require("stream").Writable + if (!Writable) { Writable = require("readable-stream").Writable; - PassThrough = require("readable-stream").PassThrough; } if(!delay) { @@ -31,20 +31,17 @@ function Connection(stream, server) { }; util.inherits(Connection, events.EventEmitter); -Connection.prototype._setup = function() { +Connection.prototype._setupParser = function() { this.parser = this.stream.pipe(new PacketParser({ connection: this })); - - this.bufferer = new PassThrough(); - this.bufferer.pipe(this.stream); }; Connection.prototype.reconnect = function(stream) { var that = this; this.stream = stream; - this._setup(); + this._setupParser(); }; for (var k in protocol.types) { @@ -54,8 +51,8 @@ for (var k in protocol.types) { " var p = this.generate." + v +"(arguments[0]); " + " if (p instanceof Error) { " + " this.emit('error', p) " + - " } else if(!this.bufferer.write(p)) { " + - " this.emit('error', 'Unable to write on the stream'); " + + " } else { " + + " this.stream.write(p); " + " } " " } ";