From 34b1a477ee381e2227b81f54b7cfe0a748383e91 Mon Sep 17 00:00:00 2001 From: jezell Date: Sat, 3 Aug 2013 23:08:08 -0700 Subject: [PATCH 1/3] Fix for multi operations with detect_buffers enabled --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d16a15ff633..8f10de2054a 100644 --- a/index.js +++ b/index.js @@ -583,7 +583,7 @@ function reply_to_strings(reply) { 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 +1084,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") { From 6164c377e4896a466958fb6e6aaa6882d2fd8760 Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Wed, 14 Aug 2013 15:43:48 -0700 Subject: [PATCH 2/3] numbers should be returned as numbers even if detect_buffers is true --- index.js | 8 ++++++-- lib/parser/javascript.js | 14 ++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 8f10de2054a..6903e4ae8b0 100644 --- a/index.js +++ b/index.js @@ -577,9 +577,13 @@ function reply_to_strings(reply) { var i; if (Buffer.isBuffer(reply)) { - return reply.toString(); + if(reply.redisType == 58) { // : + return parseInt(reply.toString(), 10); + } + else { + return reply.toString(); + } } - if (Array.isArray(reply)) { for (i = 0; i < reply.length; i++) { if (reply[i] !== null && reply[i] !== undefined) { diff --git a/lib/parser/javascript.js b/lib/parser/javascript.js index 0990cc098da..6a2336e3c4d 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,9 @@ ReplyParser.prototype._parseResult = function (type) { } if (this.options.return_buffers) { - return this._buffer.slice(start, end); + buffer = this._buffer.slice(start, end); + buffer.redisType = type; + return buffer; } else { if (end - start < 65536) { // completely arbitrary return small_toString(this._buffer, start, end); @@ -80,7 +82,9 @@ ReplyParser.prototype._parseResult = function (type) { } if (this.options.return_buffers) { - return this._buffer.slice(start, end); + buffer = this._buffer.slice(start, end); + buffer.redisType = type; + return buffer; } // return the coerced numeric value @@ -109,7 +113,9 @@ ReplyParser.prototype._parseResult = function (type) { } if (this.options.return_buffers) { - return this._buffer.slice(start, end); + buffer = this._buffer.slice(start, end); + buffer.redisType = type; + return buffer; } else { return this._buffer.toString(this._encoding, start, end); } From df3e1d03875bb361da4365c9c1ac37d5036d457f Mon Sep 17 00:00:00 2001 From: Jesse Ezell Date: Mon, 17 Mar 2014 02:00:50 -0700 Subject: [PATCH 3/3] don't return buffers for integer replies in javascript parser to match hiredis parser behavior --- index.js | 7 +------ lib/parser/javascript.js | 8 -------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/index.js b/index.js index 6903e4ae8b0..a16b6af4dcf 100644 --- a/index.js +++ b/index.js @@ -577,12 +577,7 @@ function reply_to_strings(reply) { var i; if (Buffer.isBuffer(reply)) { - if(reply.redisType == 58) { // : - return parseInt(reply.toString(), 10); - } - else { - return reply.toString(); - } + return reply.toString(); } if (Array.isArray(reply)) { for (i = 0; i < reply.length; i++) { diff --git a/lib/parser/javascript.js b/lib/parser/javascript.js index 6a2336e3c4d..6c1e49e462f 100644 --- a/lib/parser/javascript.js +++ b/lib/parser/javascript.js @@ -59,7 +59,6 @@ ReplyParser.prototype._parseResult = function (type) { if (this.options.return_buffers) { buffer = this._buffer.slice(start, end); - buffer.redisType = type; return buffer; } else { if (end - start < 65536) { // completely arbitrary @@ -81,12 +80,6 @@ ReplyParser.prototype._parseResult = function (type) { throw new IncompleteReadBuffer("Wait for more data."); } - if (this.options.return_buffers) { - buffer = this._buffer.slice(start, end); - buffer.redisType = type; - return buffer; - } - // return the coerced numeric value return +small_toString(this._buffer, start, end); } else if (type === 36) { // $ @@ -114,7 +107,6 @@ ReplyParser.prototype._parseResult = function (type) { if (this.options.return_buffers) { buffer = this._buffer.slice(start, end); - buffer.redisType = type; return buffer; } else { return this._buffer.toString(this._encoding, start, end);