Skip to content

Commit 971f409

Browse files
committed
fix handling of incomplete chunks
1 parent e233e5c commit 971f409

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

lib/postgres-pure.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,25 +259,33 @@ function Connection(args) {
259259
}
260260

261261
var queue = [];
262+
263+
function mergeFirstBuffersAndCheckInput() {
264+
if (queue.length > 1) {
265+
// Merge the first two buffers
266+
var first = queue.shift();
267+
var b = new Buffer(first.length + queue[0].length);
268+
first.copy(b);
269+
queue[0].copy(b, first.length);
270+
queue[0] = b;
271+
return checkInput();
272+
} else {
273+
return;
274+
}
275+
}
276+
262277
function checkInput() {
263278
if (queue.length === 0) { return; }
264279
var first = queue[0];
280+
if (first.length < 5) {
281+
return mergeFirstBuffersAndCheckInput();
282+
}
265283
var code = String.fromCharCode(first[0]);
266284
var length = first.int32Read(1) - 4;
267285

268286
// Make sure we have a whole message, TCP comes in chunks
269287
if (first.length < length + 5) {
270-
if (queue.length > 1) {
271-
// Merge the first two buffers
272-
queue.shift();
273-
var b = new Buffer(first.length + queue[0].length);
274-
first.copy(b);
275-
queue[0].copy(b, first.length);
276-
queue[0] = b;
277-
return checkInput();
278-
} else {
279-
return;
280-
}
288+
return mergeFirstBuffersAndCheckInput();
281289
}
282290
var message = first.slice(5, 5 + length);
283291
if (first.length === 5 + length) {

0 commit comments

Comments
 (0)