diff --git a/index.js b/index.js index d16a15ff633..a16b6af4dcf 100644 --- a/index.js +++ b/index.js @@ -577,13 +577,12 @@ function reply_to_strings(reply) { var i; if (Buffer.isBuffer(reply)) { - return reply.toString(); + return reply.toString(); } - if (Array.isArray(reply)) { for (i = 0; i < reply.length; i++) { if (reply[i] !== null && reply[i] !== undefined) { - reply[i] = reply[i].toString(); + reply[i] = reply_to_strings(reply[i]); } } return reply; @@ -1084,6 +1083,18 @@ Multi.prototype.exec = function (callback) { for (i = 1, il = self.queue.length; i < il; i += 1) { reply = replies[i - 1]; args = self.queue[i]; + var bufferArgs = false; + args.forEach(function(arg) { + if(Buffer.isBuffer(arg)) { + bufferArgs = true; + return false; + } + }); + if (self._client.options.detect_buffers && !bufferArgs) { + // If detect_buffers option was specified, then the reply from the parser will be Buffers. + // If this command did not use Buffer arguments, then convert the reply to Strings here. + reply = reply_to_strings(reply); + } // TODO - confusing and error-prone that hgetall is special cased in two places if (reply && args[0].toLowerCase() === "hgetall") { diff --git a/lib/parser/javascript.js b/lib/parser/javascript.js index 0990cc098da..6c1e49e462f 100644 --- a/lib/parser/javascript.js +++ b/lib/parser/javascript.js @@ -42,7 +42,7 @@ function small_toString(buf, start, end) { } ReplyParser.prototype._parseResult = function (type) { - var start, end, offset, packetHeader; + var start, end, offset, packetHeader, buffer; if (type === 43 || type === 45) { // + or - // up to the delimiter @@ -58,7 +58,8 @@ ReplyParser.prototype._parseResult = function (type) { } if (this.options.return_buffers) { - return this._buffer.slice(start, end); + buffer = this._buffer.slice(start, end); + return buffer; } else { if (end - start < 65536) { // completely arbitrary return small_toString(this._buffer, start, end); @@ -79,10 +80,6 @@ ReplyParser.prototype._parseResult = function (type) { throw new IncompleteReadBuffer("Wait for more data."); } - if (this.options.return_buffers) { - return this._buffer.slice(start, end); - } - // return the coerced numeric value return +small_toString(this._buffer, start, end); } else if (type === 36) { // $ @@ -109,7 +106,8 @@ ReplyParser.prototype._parseResult = function (type) { } if (this.options.return_buffers) { - return this._buffer.slice(start, end); + buffer = this._buffer.slice(start, end); + return buffer; } else { return this._buffer.toString(this._encoding, start, end); }