Skip to content

Commit cf506ba

Browse files
committed
Adding a little buffer to cope with slow connections.
1 parent 72d01af commit cf506ba

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/connection.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ var events = require('events')
22
, util = require('util')
33
, protocol = require('./protocol')
44
, generate = require('./generate')
5-
, parse = require('./parse');
6-
7-
var Writable = require("stream").Writable
5+
, parse = require('./parse')
6+
, Writable = require("stream").Writable
7+
, PassThrough = require("stream").PassThrough;
88

99
if (!Writable) {
1010
Writable = require("readable-stream").Writable;
11+
PassThrough = require("readable-stream").PassThrough;
1112
}
1213

1314
var Connection = module.exports =
@@ -23,17 +24,20 @@ function Connection(stream, server) {
2324
};
2425
util.inherits(Connection, events.EventEmitter);
2526

26-
Connection.prototype._setupParser = function() {
27+
Connection.prototype._setup = function() {
2728
this.parser = this.stream.pipe(new PacketParser({
2829
connection: this
2930
}));
31+
32+
this.bufferer = new PassThrough();
33+
this.bufferer.pipe(this.stream);
3034
};
3135

3236
Connection.prototype.reconnect = function(stream) {
3337
var that = this;
3438

3539
this.stream = stream;
36-
this._setupParser();
40+
this._setup();
3741
};
3842

3943
for (var k in protocol.types) {
@@ -43,8 +47,8 @@ for (var k in protocol.types) {
4347
" var p = this.generate." + v +"(arguments[0]); " +
4448
" if (p instanceof Error) { " +
4549
" this.emit('error', p) " +
46-
" } else { " +
47-
" this.stream.write(p); " +
50+
" } else if(!this.bufferer.write(p)) { " +
51+
" this.emit('error', 'Unable to write on the stream'); " +
4852
" } "
4953
" } ";
5054

0 commit comments

Comments
 (0)