diff --git a/lib/postgres-pure.js b/lib/postgres-pure.js index 678f288..c27ec01 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) { @@ -330,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(); }