From 971f409bcc55f6616ae73cb09776c9862a759e34 Mon Sep 17 00:00:00 2001 From: Ryan Berdeen Date: Mon, 5 Jul 2010 14:14:27 -0400 Subject: [PATCH 1/2] fix handling of incomplete chunks --- lib/postgres-pure.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/postgres-pure.js b/lib/postgres-pure.js index 678f288..74d9b13 100644 --- a/lib/postgres-pure.js +++ b/lib/postgres-pure.js @@ -259,25 +259,33 @@ function Connection(args) { } var queue = []; + + function mergeFirstBuffersAndCheckInput() { + if (queue.length > 1) { + // Merge the first two buffers + var first = queue.shift(); + var b = new Buffer(first.length + queue[0].length); + first.copy(b); + queue[0].copy(b, first.length); + queue[0] = b; + return checkInput(); + } else { + return; + } + } + function checkInput() { if (queue.length === 0) { return; } var first = queue[0]; + if (first.length < 5) { + return mergeFirstBuffersAndCheckInput(); + } var code = String.fromCharCode(first[0]); var length = first.int32Read(1) - 4; // Make sure we have a whole message, TCP comes in chunks if (first.length < length + 5) { - if (queue.length > 1) { - // Merge the first two buffers - queue.shift(); - var b = new Buffer(first.length + queue[0].length); - first.copy(b); - queue[0].copy(b, first.length); - queue[0] = b; - return checkInput(); - } else { - return; - } + return mergeFirstBuffersAndCheckInput(); } var message = first.slice(5, 5 + length); if (first.length === 5 + length) { From 88e6b8f3fb9346254a5f21cdd7686b3b61000bd6 Mon Sep 17 00:00:00 2001 From: Ryan Berdeen Date: Mon, 5 Jul 2010 14:14:56 -0400 Subject: [PATCH 2/2] call callback on query error --- lib/postgres-pure.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/postgres-pure.js b/lib/postgres-pure.js index 74d9b13..c27ec01 100644 --- a/lib/postgres-pure.js +++ b/lib/postgres-pure.js @@ -338,6 +338,9 @@ function Connection(args) { }); events.addListener('ErrorResponse', function (e) { conn.emit('error', e.S + ": " + e.M); + if (query_callback) { + query_callback.call(this, null, e); + } if (e.S === 'FATAL') { connection.end(); }