diff --git a/copy-to.js b/copy-to.js index a45ee6e..4063abb 100644 --- a/copy-to.js +++ b/copy-to.js @@ -42,6 +42,10 @@ CopyStreamQuery.prototype._transform = function(chunk, enc, cb) { var messageCode; var needPush = false; + var buffer = Buffer.alloc(chunk.length); + var buffer_offset = 0; + var buffer_sent = false; + while((chunk.length - offset) >= (Byte1Len + Int32Len)) { var messageCode = chunk[offset] @@ -70,6 +74,11 @@ CopyStreamQuery.prototype._transform = function(chunk, enc, cb) { case code.ErrorResponse: case code.CopyDone: + if(needPush && !buffer_sent && buffer_offset > 0) { + this.push(buffer.slice(0, buffer_offset)) + buffer_sent = true; + buffer_offset = 0; + } this._detach() this.push(null) return cb(); @@ -84,7 +93,8 @@ CopyStreamQuery.prototype._transform = function(chunk, enc, cb) { if (needPush) { var row = chunk.slice(offset, offset + length - Int32Len) this.rowCount++ - this.push(row) + row.copy(buffer, buffer_offset); + buffer_offset += row.length; } offset += (length - Int32Len) } else { @@ -93,6 +103,12 @@ CopyStreamQuery.prototype._transform = function(chunk, enc, cb) { } } + if(needPush && !buffer_sent && buffer_offset > 0) { + this.push(buffer.slice(0, buffer_offset)) + buffer_sent = true; + buffer_offset = 0; + } + if(chunk.length - offset) { var slice = chunk.slice(offset) this._remainder = slice