From 922627daaf38937d4ffa85bf5f817f957b2f2fdc Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Mon, 11 Jun 2018 12:14:28 +0200 Subject: [PATCH] Small refactor --- copy-to.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/copy-to.js b/copy-to.js index 4063abb..2334459 100644 --- a/copy-to.js +++ b/copy-to.js @@ -46,6 +46,14 @@ CopyStreamQuery.prototype._transform = function(chunk, enc, cb) { var buffer_offset = 0; var buffer_sent = false; + this.pushBufferIfneeded = function() { + if(needPush && !buffer_sent && buffer_offset > 0) { + this.push(buffer.slice(0, buffer_offset)) + buffer_sent = true; + buffer_offset = 0; + } + } + while((chunk.length - offset) >= (Byte1Len + Int32Len)) { var messageCode = chunk[offset] @@ -74,11 +82,7 @@ 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.pushBufferIfneeded(); this._detach() this.push(null) return cb(); @@ -103,11 +107,7 @@ 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; - } + this.pushBufferIfneeded(); if(chunk.length - offset) { var slice = chunk.slice(offset)