From 3f945dbf3e35f1b8f814ea42fff8eb5e35529926 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 25 Nov 2015 20:13:57 +0100 Subject: [PATCH] Fix parser regression Add regression test Rename big_offset to big_str_size Fixes #924 --- changelog.md | 8 +++++++- lib/parsers/javascript.js | 18 +++++++++--------- test/parser.spec.js | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/changelog.md b/changelog.md index 2ef417c64d..e329833334 100644 --- a/changelog.md +++ b/changelog.md @@ -1,11 +1,17 @@ Changelog ========= +## v.2.4.1 - 25 Nov, 2015 + +Bugfixes + +- Fixed a js parser regression introduced in 2.4.0 ([@BridgeAR](https://github.com/BridgeAR)) + ## v.2.4.0 - 25 Nov, 2015 Features -- Added `tls` option to iniate a connection to a redis server behind a TLS proxy. Thanks ([@paddybyers](https://github.com/paddybyers)) +- Added `tls` option to initiate a connection to a redis server behind a TLS proxy. Thanks ([@paddybyers](https://github.com/paddybyers)) - Added `prefix` option to auto key prefix any command with the provided prefix ([@luin](https://github.com/luin) & [@BridgeAR](https://github.com/BridgeAR)) - Added `url` option to pass the connection url with the options object ([@BridgeAR](https://github.com/BridgeAR)) - Added `client.duplicate([options])` to duplicate the current client and return a new one with the same options ([@BridgeAR](https://github.com/BridgeAR)) diff --git a/lib/parsers/javascript.js b/lib/parsers/javascript.js index bdb4a9f8bb..909f9b789d 100644 --- a/lib/parsers/javascript.js +++ b/lib/parsers/javascript.js @@ -6,7 +6,7 @@ function JavascriptReplyParser() { this.name = exports.name; this._buffer = new Buffer(0); this._offset = 0; - this._big_offset = 0; + this._big_str_size = 0; this._chunks_size = 0; this._buffers = []; this._type = 0; @@ -53,7 +53,7 @@ JavascriptReplyParser.prototype._parseResult = function (type) { if (end + 2 > this._buffer.length) { this._chunks_size = this._buffer.length - this._offset - 2; - this._big_offset = packetHeader; + this._big_str_size = packetHeader; throw new IncompleteReadBuffer('Wait for more data.'); } // Set the offset to after the delimiter @@ -85,17 +85,17 @@ JavascriptReplyParser.prototype._parseResult = function (type) { }; JavascriptReplyParser.prototype.execute = function (buffer) { - if (this._chunks_size !== 0 && this._big_offset > this._chunks_size + buffer.length) { - this._buffers.push(buffer); - this._chunks_size += buffer.length; - return; - } - if (this._buffers.length !== 0) { + if (this._chunks_size !== 0) { + if (this._big_str_size > this._chunks_size + buffer.length) { + this._buffers.push(buffer); + this._chunks_size += buffer.length; + return; + } this._buffers.unshift(this._offset === 0 ? this._buffer : this._buffer.slice(this._offset)); this._buffers.push(buffer); this._buffer = Buffer.concat(this._buffers); this._buffers = []; - this._big_offset = 0; + this._big_str_size = 0; this._chunks_size = 0; } else if (this._offset >= this._buffer.length) { this._buffer = buffer; diff --git a/test/parser.spec.js b/test/parser.spec.js index b50d37322d..9b8e8462e2 100644 --- a/test/parser.spec.js +++ b/test/parser.spec.js @@ -256,6 +256,23 @@ describe('parsers', function () { parser.execute(new Buffer('*1\r\n$4\r\ntest\r\n')); assert.strictEqual(reply_count, 3); }); + + it('regression test v.2.4.1', function () { + var parser = new Parser(true); + var reply_count = 0; + var entries = ['test test ', 'test test test test ', '1234']; + function check_reply(reply) { + assert.strictEqual(reply.toString(), entries[reply_count]); + reply_count++; + } + parser.send_reply = check_reply; + parser.execute(new Buffer('$10\r\ntest ')); + assert.strictEqual(reply_count, 0); + parser.execute(new Buffer('test \r\n$20\r\ntest test test test \r\n:1234\r')); + assert.strictEqual(reply_count, 2); + parser.execute(new Buffer('\n')); + assert.strictEqual(reply_count, 3); + }); }); }); });