Skip to content

Commit

Permalink
lib: pause() before unshift() extra data
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed Jul 14, 2014
1 parent 6022596 commit 114b23b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
2 changes: 2 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Client.prototype._onConnect = function() {
socket.end();
}
});
self._sock.resume();
return;
}
}
Expand All @@ -107,6 +108,7 @@ Client.prototype._onConnect = function() {
}).on('reply', function(repInfo) {
self._ready = true;
self.emit('connect', self._sock);
self._sock.resume();
});
};

Expand Down
2 changes: 2 additions & 0 deletions lib/client.parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,15 @@ Parser.prototype.start = function() {
return;
this._listening = true;
this._stream.on('data', this.__onData);
this._stream.resume();
};

Parser.prototype.stop = function() {
if (!this._listening)
return;
this._listening = false;
this._stream.removeListener('data', this.__onData);
this._stream.pause();
};

module.exports = Parser;
5 changes: 5 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Server.prototype._onConnection = function(socket) {
}
});
socket.write(new Buffer([0x05, auths[a].METHOD]));
socket.resume();
return;
}
}
Expand All @@ -102,6 +103,9 @@ Server.prototype._onConnection = function(socket) {
if (intercept) {
socket.write(BUF_REP_INTR_SUCCESS);
socket.removeListener('error', onErrorNoop);
process.nextTick(function() {
socket.resume();
});
return socket;
} else
proxySocket(socket, reqInfo);
Expand Down Expand Up @@ -216,6 +220,7 @@ function proxySocket(socket, req) {

socket.write(bufrep);
socket.pipe(dstSock).pipe(socket);
socket.resume();
} else if (dstSock.writable)
dstSock.end();
})
Expand Down
2 changes: 2 additions & 0 deletions lib/server.parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,15 @@ Parser.prototype.start = function() {
return;
this._listening = true;
this._stream.on('data', this.__onData);
this._stream.resume();
};

Parser.prototype.stop = function() {
if (!this._listening)
return;
this._listening = false;
this._stream.removeListener('data', this.__onData);
this._stream.pause();
};

module.exports = Parser;
33 changes: 22 additions & 11 deletions test/test-client-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var Parser = require('../lib/client.parser');
var EventEmitter = require('events').EventEmitter,
path = require('path'),
assert = require('assert'),
inspect = require('util').inspect;
inspect = require('util').inspect
inherits = require('util').inherits;

var t = -1,
group = path.basename(__filename, '.js') + '/';
Expand All @@ -12,7 +13,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
method;
parser.on('method', function(m) {
Expand All @@ -32,7 +33,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
method;
parser.on('method', function(m) {
Expand All @@ -53,7 +54,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.on('method', function(m) {
Expand All @@ -74,7 +75,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
reply;
parser.authed = true;
Expand All @@ -101,7 +102,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
reply;
parser.authed = true;
Expand Down Expand Up @@ -131,7 +132,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
reply;
parser.authed = true;
Expand Down Expand Up @@ -160,7 +161,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
reply;
parser.authed = true;
Expand All @@ -187,7 +188,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.authed = true;
Expand All @@ -209,7 +210,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.authed = true;
Expand All @@ -231,7 +232,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.authed = true;
Expand Down Expand Up @@ -278,4 +279,14 @@ process.once('exit', function() {
'Only finished ' + (t + 1) + '/' + tests.length + ' tests'));
});



function FakeStream() {
EventEmitter.call(this);
}
inherits(FakeStream, EventEmitter);
FakeStream.prototype.pause = function() {};
FakeStream.prototype.resume = function() {};


next();
39 changes: 25 additions & 14 deletions test/test-server-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var Parser = require('../lib/server.parser');
var EventEmitter = require('events').EventEmitter,
path = require('path'),
assert = require('assert'),
inspect = require('util').inspect;
inspect = require('util').inspect,
inherits = require('util').inherits;

var t = -1,
group = path.basename(__filename, '.js') + '/';
Expand All @@ -12,7 +13,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
methods;
parser.on('methods', function(m) {
Expand All @@ -33,7 +34,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
methods;
parser.on('methods', function(m) {
Expand All @@ -56,7 +57,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.on('methods', function(m) {
Expand All @@ -77,7 +78,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.on('methods', function(m) {
Expand All @@ -98,7 +99,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
request;
parser.authed = true;
Expand Down Expand Up @@ -129,7 +130,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
request;
parser.authed = true;
Expand Down Expand Up @@ -160,7 +161,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
request;
parser.authed = true;
Expand Down Expand Up @@ -191,7 +192,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
request;
parser.authed = true;
Expand Down Expand Up @@ -224,7 +225,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
request;
parser.authed = true;
Expand Down Expand Up @@ -256,7 +257,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
request;
parser.authed = true;
Expand Down Expand Up @@ -289,7 +290,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.authed = true;
Expand All @@ -316,7 +317,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.authed = true;
Expand All @@ -343,7 +344,7 @@ var tests = [
{ run: function() {
var self = this,
what = this.what,
stream = new EventEmitter(),
stream = new FakeStream(),
parser = new Parser(stream),
errors = [];
parser.authed = true;
Expand Down Expand Up @@ -391,4 +392,14 @@ process.once('exit', function() {
'Only finished ' + (t + 1) + '/' + tests.length + ' tests'));
});



function FakeStream() {
EventEmitter.call(this);
}
inherits(FakeStream, EventEmitter);
FakeStream.prototype.pause = function() {};
FakeStream.prototype.resume = function() {};


next();

0 comments on commit 114b23b

Please sign in to comment.