Skip to content

Commit 2f77f87

Browse files
committed
use read instead of on('data')
1 parent 5b31d82 commit 2f77f87

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

lib/connection.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,13 @@ Connection.prototype.connect = function(port, host) {
8686
Connection.prototype.attachListeners = function(stream) {
8787
var self = this;
8888
var reader = new Readable();
89-
reader.wrap(stream);
89+
reader = reader.wrap(stream);
9090
reader.on('readable', function() {
91-
self.setBuffer(reader.read());
92-
var msg = self.parseMessage();
91+
var msg = self.parseMessage(reader);
9392
while(msg) {
9493
self.emit('message', msg);
9594
self.emit(msg.name, msg);
96-
msg = self.parseMessage();
95+
msg = self.parseMessage(reader);
9796
}
9897
});
9998
reader.on('error', function(error) {
@@ -324,34 +323,39 @@ Connection.prototype.readSslResponse = function() {
324323
};
325324
};
326325

327-
Connection.prototype.parseMessage = function() {
328-
var remaining = this.buffer.length - (this.offset);
329-
if(remaining < 5) {
330-
//cannot read id + length without at least 5 bytes
331-
//just abort the read now
332-
this.lastBuffer = this.buffer;
333-
this.lastOffset = this.offset;
334-
return false;
326+
Connection.prototype.parseMessage = function(reader) {
327+
//I want to get rid of this bit here...
328+
if(!this.header) {
329+
//try to read the 5 byte header
330+
this.header = reader.read(5);
331+
if(!this.header) {
332+
return false;
333+
}
335334
}
336335

337336
//read message id code
338-
var id = this.buffer[this.offset++];
337+
var id = this.header[0];
339338
//read message length
340-
var length = this.parseInt32();
341-
342-
if(remaining <= length) {
343-
this.lastBuffer = this.buffer;
344-
//rewind the last 5 bytes we read
345-
this.lastOffset = this.offset-5;
346-
return false;
339+
var length = this.header.readInt32BE(1, true);
340+
341+
var bodyLength = length - 4;
342+
//some packets have an empty body
343+
if(bodyLength) {
344+
var body = reader.read(bodyLength);
345+
if(!body) {
346+
return false;
347+
}
348+
//blow away header
349+
this.buffer = body;
350+
this.offset = 0;
347351
}
352+
this.header = false;
348353

349354
var msg = {
350355
length: length
351356
};
352357
switch(id)
353358
{
354-
355359
case 0x52: //R
356360
msg.name = 'authenticationOk';
357361
return this.parseR(msg);
@@ -419,6 +423,7 @@ Connection.prototype.parseMessage = function() {
419423
case 0x48: //H
420424
msg.name = 'copyOutResponse';
421425
return this.parseGH(msg);
426+
422427
case 0x63: //c
423428
msg.name = 'copyDone';
424429
return msg;

test/unit/test-helper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ p.write = function(packet) {
1515
this.packets.push(packet);
1616
};
1717

18+
p.pause = function() {
19+
20+
};
21+
1822
p.writable = true;
1923

2024
createClient = function() {

0 commit comments

Comments
 (0)