package/package.json000644 001750 001750 0000002505 13070556426013031 0ustar00000000 000000 { "name": "redis-parser", "version": "2.6.0", "description": "Javascript Redis protocol (RESP) parser", "main": "index.js", "scripts": { "test": "npm run coverage", "benchmark": "node ./benchmark", "lint": "standard --fix", "posttest": "npm run lint && npm run coverage:check", "coverage": "node ./node_modules/istanbul/lib/cli.js cover --preserve-comments ./node_modules/mocha/bin/_mocha -- -R spec", "coverage:check": "node ./node_modules/istanbul/lib/cli.js check-coverage --branch 100 --statement 100" }, "repository": { "type": "git", "url": "git+https://github.com/NodeRedis/node-redis-parser.git" }, "keywords": [ "redis", "protocol", "parser", "database", "javascript", "node", "nodejs", "resp", "hiredis" ], "engines": { "node": ">=0.10.0" }, "devDependencies": { "benchmark": "^2.1.0", "codeclimate-test-reporter": "^0.4.0", "intercept-stdout": "^0.1.2", "istanbul": "^0.4.0", "standard": "^9.0.0", "mocha": "^3.1.2", "hiredis": "^0.5.0" }, "author": "Ruben Bridgewater", "license": "MIT", "bugs": { "url": "https://github.com/NodeRedis/node-redis-parser/issues" }, "homepage": "https://github.com/NodeRedis/node-redis-parser#readme", "directories": { "test": "test", "lib": "lib" } } package/.npmignore000644 001750 001750 0000000232 13061155044012524 0ustar00000000 000000 # IntelliJ project files .idea *.iml out gen # Unrelevant files and folders benchmark coverage test .travis.yml .gitignore *.log .vscode .codeclimate.ymlpackage/README.md000644 001750 001750 0000015303 13061155044012011 0ustar00000000 000000 [![Build Status](https://travis-ci.org/NodeRedis/node-redis-parser.png?branch=master)](https://travis-ci.org/NodeRedis/node-redis-parser) [![Test Coverage](https://codeclimate.com/github/NodeRedis/node-redis-parser/badges/coverage.svg)](https://codeclimate.com/github/NodeRedis/node-redis-parser/coverage) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) # redis-parser A high performance javascript redis parser built for [node_redis](https://github.com/NodeRedis/node_redis) and [ioredis](https://github.com/luin/ioredis). Parses all [RESP](http://redis.io/topics/protocol) data. ## Install Install with [NPM](https://npmjs.org/): npm install redis-parser ## Usage ```js var Parser = require('redis-parser'); var myParser = new Parser(options); ``` ### Options * `returnReply`: *function*; mandatory * `returnError`: *function*; mandatory * `returnFatalError`: *function*; optional, defaults to the returnError function * `returnBuffers`: *boolean*; optional, defaults to false * `stringNumbers`: *boolean*; optional, defaults to false ### Functions * `reset()`: reset the parser to it's initial state * `setReturnBuffers(boolean)`: (JSParser only) set the returnBuffers option on/off without resetting the parser * `setStringNumbers(boolean)`: (JSParser only) set the stringNumbers option on/off without resetting the parser ### Error classes * `RedisError` sub class of Error * `ReplyError` sub class of RedisError * `ParserError` sub class of RedisError All Redis errors will be returned as `ReplyErrors` while a parser error is returned as `ParserError`. All error classes are exported by the parser. ### Example ```js var Parser = require("redis-parser"); function Library () {} Library.prototype.returnReply = function (reply) { ... } Library.prototype.returnError = function (err) { ... } Library.prototype.returnFatalError = function (err) { ... } var lib = new Library(); var parser = new Parser({ returnReply: function(reply) { lib.returnReply(reply); }, returnError: function(err) { lib.returnError(err); }, returnFatalError: function (err) { lib.returnFatalError(err); } }); Library.prototype.streamHandler = function () { this.stream.on('data', function (buffer) { // Here the data (e.g. `new Buffer('$5\r\nHello\r\n'`)) is passed to the parser and the result is passed to either function depending on the provided data. parser.execute(buffer); }); }; ``` You do not have to use the returnFatalError function. Fatal errors will be returned in the normal error function in that case. And if you want to return buffers instead of strings, you can do this by adding the `returnBuffers` option. If you handle with big numbers that are to large for JS (Number.MAX_SAFE_INTEGER === 2^53 - 16) please use the `stringNumbers` option. That way all numbers are going to be returned as String and you can handle them safely. ```js // Same functions as in the first example var parser = new Parser({ returnReply: function(reply) { lib.returnReply(reply); }, returnError: function(err) { lib.returnError(err); }, returnBuffers: true, // All strings are returned as Buffer e.g. stringNumbers: true // All numbers are returned as String }); // The streamHandler as above ``` ## Protocol errors To handle protocol errors (this is very unlikely to happen) gracefully you should add the returnFatalError option, reject any still running command (they might have been processed properly but the reply is just wrong), destroy the socket and reconnect. Note that while doing this no new command may be added, so all new commands have to be buffered in the meantime, otherwise a chunk might still contain partial data of a following command that was already processed properly but answered in the same chunk as the command that resulted in the protocol error. ## Contribute The parser is highly optimized but there may still be further optimizations possible. npm install npm test npm run benchmark Currently the benchmark compares the performance against the hiredis parser: HIREDIS: $ multiple chunks in a bulk string x 859,880 ops/sec ±1.22% (82 runs sampled) HIREDIS BUF: $ multiple chunks in a bulk string x 608,869 ops/sec ±1.72% (85 runs sampled) JS PARSER: $ multiple chunks in a bulk string x 910,590 ops/sec ±0.87% (89 runs sampled) JS PARSER BUF: $ multiple chunks in a bulk string x 1,299,507 ops/sec ±2.18% (84 runs sampled) HIREDIS: + multiple chunks in a string x 1,787,203 ops/sec ±0.58% (96 runs sampled) HIREDIS BUF: + multiple chunks in a string x 943,584 ops/sec ±1.62% (87 runs sampled) JS PARSER: + multiple chunks in a string x 2,008,264 ops/sec ±1.01% (91 runs sampled) JS PARSER BUF: + multiple chunks in a string x 2,045,546 ops/sec ±0.78% (91 runs sampled) HIREDIS: $ 4mb bulk string x 310 ops/sec ±1.58% (75 runs sampled) HIREDIS BUF: $ 4mb bulk string x 471 ops/sec ±2.28% (78 runs sampled) JS PARSER: $ 4mb bulk string x 747 ops/sec ±2.43% (85 runs sampled) JS PARSER BUF: $ 4mb bulk string x 846 ops/sec ±5.52% (72 runs sampled) HIREDIS: + simple string x 2,324,866 ops/sec ±1.61% (90 runs sampled) HIREDIS BUF: + simple string x 1,085,823 ops/sec ±2.47% (82 runs sampled) JS PARSER: + simple string x 4,567,358 ops/sec ±1.97% (81 runs sampled) JS PARSER BUF: + simple string x 5,433,901 ops/sec ±0.66% (93 runs sampled) HIREDIS: : integer x 2,332,946 ops/sec ±0.47% (93 runs sampled) JS PARSER: : integer x 17,730,449 ops/sec ±0.73% (91 runs sampled) JS PARSER STR: : integer x 12,942,037 ops/sec ±0.51% (92 runs sampled) HIREDIS: : big integer x 2,012,572 ops/sec ±0.33% (93 runs sampled) JS PARSER: : big integer x 10,210,923 ops/sec ±0.94% (94 runs sampled) JS PARSER STR: : big integer x 4,453,320 ops/sec ±0.52% (94 runs sampled) HIREDIS: * array x 44,479 ops/sec ±0.55% (94 runs sampled) HIREDIS BUF: * array x 14,391 ops/sec ±1.04% (86 runs sampled) JS PARSER: * array x 53,796 ops/sec ±2.08% (79 runs sampled) JS PARSER BUF: * array x 72,428 ops/sec ±0.72% (93 runs sampled) HIREDIS: * big nested array x 217 ops/sec ±0.97% (83 runs sampled) HIREDIS BUF: * big nested array x 255 ops/sec ±2.28% (77 runs sampled) JS PARSER: * big nested array x 242 ops/sec ±1.10% (85 runs sampled) JS PARSER BUF: * big nested array x 375 ops/sec ±1.21% (88 runs sampled) HIREDIS: - error x 78,821 ops/sec ±0.80% (93 runs sampled) JS PARSER: - error x 143,382 ops/sec ±0.75% (92 runs sampled) Platform info: Ubuntu 16.10 Node.js 7.4.0 Intel(R) Core(TM) i7-5600U CPU ## License [MIT](./LICENSE) package/LICENSE000664 001750 001750 0000002065 12611526740011547 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2015 NodeRedis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/index.js000644 001750 001750 0000000340 13061155044012172 0ustar00000000 000000 'use strict' module.exports = require('./lib/parser') module.exports.ReplyError = require('./lib/replyError') module.exports.RedisError = require('./lib/redisError') module.exports.ParserError = require('./lib/redisError') package/changelog.md000644 001750 001750 0000007153 13070555060013011 0ustar00000000 000000 ## v.2.6.0 - 03 Apr, 2017 Internals - Use Buffer.allocUnsafe instead of new Buffer() with modern Node.js versions ## v.2.5.0 - 11 Mar, 2017 Features - Added a `ParserError` class to differentiate them to ReplyErrors. The class is also exported Bugfixes - All errors now show their error message again next to the error name in the stack trace - ParserErrors now show the offset and buffer attributes while being logged ## v.2.4.1 - 05 Feb, 2017 Bugfixes - Fixed minimal memory consumption overhead for chunked buffers ## v.2.4.0 - 25 Jan, 2017 Features - Added `reset` function to reset the parser to it's initial values - Added `setReturnBuffers` function to reset the returnBuffers option (Only for the JSParser) - Added `setStringNumbers` function to reset the stringNumbers option (Only for the JSParser) - All Errors are now of sub classes of the new `RedisError` class. It is also exported. - Improved bulk string chunked data handling performance Bugfixes - Parsing time for big nested arrays is now linear ## v.2.3.0 - 25 Nov, 2016 Features - Parsing time for big arrays (e.g. 4mb+) is now linear and works well for arbitrary array sizes This case is a magnitude faster than before OLD STR: * big array x 1.09 ops/sec ±2.15% (7 runs sampled) OLD BUF: * big array x 1.23 ops/sec ±2.67% (8 runs sampled) NEW STR: * big array x 273 ops/sec ±2.09% (85 runs sampled) NEW BUF: * big array x 259 ops/sec ±1.32% (85 runs sampled) (~10mb array with 1000 entries) ## v.2.2.0 - 18 Nov, 2016 Features - Improve `stringNumbers` parsing performance by up to 100% Bugfixes - Do not unref the interval anymore due to issues with NodeJS ## v.2.1.1 - 31 Oct, 2016 Bugfixes - Remove erroneously added const to support Node.js 0.10 ## v.2.1.0 - 30 Oct, 2016 Features - Improve parser errors by adding more detailed information to them - Accept manipulated Object.prototypes - Unref the interval if used ## v.2.0.4 - 21 Jul, 2016 Bugfixes - Fixed multi byte characters getting corrupted ## v.2.0.3 - 17 Jun, 2016 Bugfixes - Fixed parser not working with huge buffers (e.g. 300 MB) ## v.2.0.2 - 08 Jun, 2016 Bugfixes - Fixed parser with returnBuffers option returning corrupted data ## v.2.0.1 - 04 Jun, 2016 Bugfixes - Fixed multiple parsers working concurrently resulting in faulty data in some cases ## v.2.0.0 - 29 May, 2016 The javascript parser got completely rewritten by [Michael Diarmid](https://github.com/Salakar) and [Ruben Bridgewater](https://github.com/BridgeAR) and is now a lot faster than the hiredis parser. Therefore the hiredis parser was deprecated and should only be used for testing purposes and benchmarking comparison. All Errors returned by the parser are from now on of class ReplyError Features - Improved performance by up to 15x as fast as before - Improved options validation - Added ReplyError Class - Added parser benchmark - Switched default parser from hiredis to JS, no matter if hiredis is installed or not Removed - Deprecated hiredis support ## v.1.3.0 - 27 Mar, 2016 Features - Added `auto` as parser name option to check what parser is available - Non existing requested parsers falls back into auto mode instead of always choosing the JS parser ## v.1.2.0 - 27 Mar, 2016 Features - Added `stringNumbers` option to make sure all numbers are returned as string instead of a js number for precision - The parser is from now on going to print warnings if a parser is explicitly requested that does not exist and gracefully chooses the JS parser ## v.1.1.0 - 26 Jan, 2016 Features - The parser is from now on going to reset itself on protocol errors package/lib/hiredis.js000644 001750 001750 0000002740 13061155044013266 0ustar00000000 000000 'use strict' var hiredis = require('hiredis') var ReplyError = require('../lib/replyError') var ParserError = require('../lib/parserError') /** * Parse data * @param parser * @returns {*} */ function parseData (parser, data) { try { return parser.reader.get() } catch (err) { // Protocol errors land here // Reset the parser. Otherwise new commands can't be processed properly parser.reader = new hiredis.Reader(parser.options) parser.returnFatalError(new ParserError(err.message, JSON.stringify(data), -1)) } } /** * Hiredis Parser * @param options * @constructor */ function HiredisReplyParser (options) { this.returnError = options.returnError this.returnFatalError = options.returnFatalError || options.returnError this.returnReply = options.returnReply this.name = 'hiredis' this.options = { return_buffers: !!options.returnBuffers } this.reader = new hiredis.Reader(this.options) } HiredisReplyParser.prototype.execute = function (data) { this.reader.feed(data) var reply = parseData(this, data) while (reply !== undefined) { if (reply && reply.name === 'Error') { this.returnError(new ReplyError(reply.message)) } else { this.returnReply(reply) } reply = parseData(this, data) } } /** * Reset the parser values to the initial state * * @returns {undefined} */ HiredisReplyParser.prototype.reset = function () { this.reader = new hiredis.Reader(this.options) } module.exports = HiredisReplyParser package/lib/parser.js000644 001750 001750 0000034671 13070557044013151 0ustar00000000 000000 'use strict' var StringDecoder = require('string_decoder').StringDecoder var decoder = new StringDecoder() var ReplyError = require('./replyError') var ParserError = require('./parserError') var bufferPool = bufferAlloc(32 * 1024) var bufferOffset = 0 var interval = null var counter = 0 var notDecreased = 0 var isModern = typeof Buffer.allocUnsafe === 'function' /** * For backwards compatibility * @param len * @returns {Buffer} */ function bufferAlloc (len) { return isModern ? Buffer.allocUnsafe(len) : new Buffer(len) } /** * Used for lengths and numbers only, faster perf on arrays / bulks * @param parser * @returns {*} */ function parseSimpleNumbers (parser) { var offset = parser.offset var length = parser.buffer.length - 1 var number = 0 var sign = 1 if (parser.buffer[offset] === 45) { sign = -1 offset++ } while (offset < length) { var c1 = parser.buffer[offset++] if (c1 === 13) { // \r\n parser.offset = offset + 1 return sign * number } number = (number * 10) + (c1 - 48) } } /** * Used for integer numbers in case of the returnNumbers option * * The maximimum possible integer to use is: Math.floor(Number.MAX_SAFE_INTEGER / 10) * Staying in a SMI Math.floor((Math.pow(2, 32) / 10) - 1) is even more efficient though * * @param parser * @returns {*} */ function parseStringNumbers (parser) { var offset = parser.offset var length = parser.buffer.length - 1 var number = 0 var res = '' if (parser.buffer[offset] === 45) { res += '-' offset++ } while (offset < length) { var c1 = parser.buffer[offset++] if (c1 === 13) { // \r\n parser.offset = offset + 1 if (number !== 0) { res += number } return res } else if (number > 429496728) { res += (number * 10) + (c1 - 48) number = 0 } else if (c1 === 48 && number === 0) { res += 0 } else { number = (number * 10) + (c1 - 48) } } } /** * Returns a string or buffer of the provided offset start and * end ranges. Checks `optionReturnBuffers`. * * If returnBuffers is active, all return values are returned as buffers besides numbers and errors * * @param parser * @param start * @param end * @returns {*} */ function convertBufferRange (parser, start, end) { parser.offset = end + 2 if (parser.optionReturnBuffers === true) { return parser.buffer.slice(start, end) } return parser.buffer.toString('utf-8', start, end) } /** * Parse a '+' redis simple string response but forward the offsets * onto convertBufferRange to generate a string. * @param parser * @returns {*} */ function parseSimpleString (parser) { var start = parser.offset var offset = start var buffer = parser.buffer var length = buffer.length - 1 while (offset < length) { if (buffer[offset++] === 13) { // \r\n return convertBufferRange(parser, start, offset - 1) } } } /** * Returns the string length via parseSimpleNumbers * @param parser * @returns {*} */ function parseLength (parser) { var string = parseSimpleNumbers(parser) if (string !== undefined) { return string } } /** * Parse a ':' redis integer response * * If stringNumbers is activated the parser always returns numbers as string * This is important for big numbers (number > Math.pow(2, 53)) as js numbers * are 64bit floating point numbers with reduced precision * * @param parser * @returns {*} */ function parseInteger (parser) { if (parser.optionStringNumbers) { return parseStringNumbers(parser) } return parseSimpleNumbers(parser) } /** * Parse a '$' redis bulk string response * @param parser * @returns {*} */ function parseBulkString (parser) { var length = parseLength(parser) if (length === undefined) { return } if (length === -1) { return null } var offsetEnd = parser.offset + length if (offsetEnd + 2 > parser.buffer.length) { parser.bigStrSize = offsetEnd + 2 parser.bigOffset = parser.offset parser.totalChunkSize = parser.buffer.length parser.bufferCache.push(parser.buffer) return } return convertBufferRange(parser, parser.offset, offsetEnd) } /** * Parse a '-' redis error response * @param parser * @returns {Error} */ function parseError (parser) { var string = parseSimpleString(parser) if (string !== undefined) { if (parser.optionReturnBuffers === true) { string = string.toString() } return new ReplyError(string) } } /** * Parsing error handler, resets parser buffer * @param parser * @param error */ function handleError (parser, error) { parser.buffer = null parser.returnFatalError(error) } /** * Parse a '*' redis array response * @param parser * @returns {*} */ function parseArray (parser) { var length = parseLength(parser) if (length === undefined) { return } if (length === -1) { return null } var responses = new Array(length) return parseArrayElements(parser, responses, 0) } /** * Push a partly parsed array to the stack * * @param parser * @param elem * @param i * @returns {undefined} */ function pushArrayCache (parser, elem, pos) { parser.arrayCache.push(elem) parser.arrayPos.push(pos) } /** * Parse chunked redis array response * @param parser * @returns {*} */ function parseArrayChunks (parser) { var tmp = parser.arrayCache.pop() var pos = parser.arrayPos.pop() if (parser.arrayCache.length) { var res = parseArrayChunks(parser) if (!res) { pushArrayCache(parser, tmp, pos) return } tmp[pos++] = res } return parseArrayElements(parser, tmp, pos) } /** * Parse redis array response elements * @param parser * @param responses * @param i * @returns {*} */ function parseArrayElements (parser, responses, i) { var bufferLength = parser.buffer.length while (i < responses.length) { var offset = parser.offset if (parser.offset >= bufferLength) { pushArrayCache(parser, responses, i) return } var response = parseType(parser, parser.buffer[parser.offset++]) if (response === undefined) { if (!parser.arrayCache.length) { parser.offset = offset } pushArrayCache(parser, responses, i) return } responses[i] = response i++ } return responses } /** * Called the appropriate parser for the specified type. * @param parser * @param type * @returns {*} */ function parseType (parser, type) { switch (type) { case 36: // $ return parseBulkString(parser) case 58: // : return parseInteger(parser) case 43: // + return parseSimpleString(parser) case 42: // * return parseArray(parser) case 45: // - return parseError(parser) default: return handleError(parser, new ParserError( 'Protocol error, got ' + JSON.stringify(String.fromCharCode(type)) + ' as reply type byte', JSON.stringify(parser.buffer), parser.offset )) } } // All allowed options including their typeof value var optionTypes = { returnError: 'function', returnFatalError: 'function', returnReply: 'function', returnBuffers: 'boolean', stringNumbers: 'boolean', name: 'string' } /** * Javascript Redis Parser * @param options * @constructor */ function JavascriptRedisParser (options) { if (!(this instanceof JavascriptRedisParser)) { return new JavascriptRedisParser(options) } if (!options || !options.returnError || !options.returnReply) { throw new TypeError('Please provide all return functions while initiating the parser') } for (var key in options) { // eslint-disable-next-line valid-typeof if (optionTypes.hasOwnProperty(key) && typeof options[key] !== optionTypes[key]) { throw new TypeError('The options argument contains the property "' + key + '" that is either unknown or of a wrong type') } } if (options.name === 'hiredis') { /* istanbul ignore next: hiredis is only supported for legacy usage */ try { var Hiredis = require('./hiredis') console.error(new TypeError('Using hiredis is discouraged. Please use the faster JS parser by removing the name option.').stack.replace('Error', 'Warning')) return new Hiredis(options) } catch (e) { console.error(new TypeError('Hiredis is not installed. Please remove the `name` option. The (faster) JS parser is used instead.').stack.replace('Error', 'Warning')) } } this.optionReturnBuffers = !!options.returnBuffers this.optionStringNumbers = !!options.stringNumbers this.returnError = options.returnError this.returnFatalError = options.returnFatalError || options.returnError this.returnReply = options.returnReply this.name = 'javascript' this.reset() } /** * Reset the parser values to the initial state * * @returns {undefined} */ JavascriptRedisParser.prototype.reset = function () { this.offset = 0 this.buffer = null this.bigStrSize = 0 this.bigOffset = 0 this.totalChunkSize = 0 this.bufferCache = [] this.arrayCache = [] this.arrayPos = [] } /** * Set the returnBuffers option * * @param returnBuffers * @returns {undefined} */ JavascriptRedisParser.prototype.setReturnBuffers = function (returnBuffers) { if (typeof returnBuffers !== 'boolean') { throw new TypeError('The returnBuffers argument has to be a boolean') } this.optionReturnBuffers = returnBuffers } /** * Set the stringNumbers option * * @param stringNumbers * @returns {undefined} */ JavascriptRedisParser.prototype.setStringNumbers = function (stringNumbers) { if (typeof stringNumbers !== 'boolean') { throw new TypeError('The stringNumbers argument has to be a boolean') } this.optionStringNumbers = stringNumbers } /** * Decrease the bufferPool size over time * @returns {undefined} */ function decreaseBufferPool () { if (bufferPool.length > 50 * 1024) { // Balance between increasing and decreasing the bufferPool if (counter === 1 || notDecreased > counter * 2) { // Decrease the bufferPool by 10% by removing the first 10% of the current pool var sliceLength = Math.floor(bufferPool.length / 10) if (bufferOffset <= sliceLength) { bufferOffset = 0 } else { bufferOffset -= sliceLength } bufferPool = bufferPool.slice(sliceLength, bufferPool.length) } else { notDecreased++ counter-- } } else { clearInterval(interval) counter = 0 notDecreased = 0 interval = null } } /** * Check if the requested size fits in the current bufferPool. * If it does not, reset and increase the bufferPool accordingly. * * @param length * @returns {undefined} */ function resizeBuffer (length) { if (bufferPool.length < length + bufferOffset) { var multiplier = length > 1024 * 1024 * 75 ? 2 : 3 if (bufferOffset > 1024 * 1024 * 111) { bufferOffset = 1024 * 1024 * 50 } bufferPool = bufferAlloc(length * multiplier + bufferOffset) bufferOffset = 0 counter++ if (interval === null) { interval = setInterval(decreaseBufferPool, 50) } } } /** * Concat a bulk string containing multiple chunks * * Notes: * 1) The first chunk might contain the whole bulk string including the \r * 2) We are only safe to fully add up elements that are neither the first nor any of the last two elements * * @param parser * @returns {String} */ function concatBulkString (parser) { var list = parser.bufferCache var chunks = list.length var offset = parser.bigStrSize - parser.totalChunkSize parser.offset = offset if (offset <= 2) { if (chunks === 2) { return list[0].toString('utf8', parser.bigOffset, list[0].length + offset - 2) } chunks-- offset = list[list.length - 2].length + offset } var res = decoder.write(list[0].slice(parser.bigOffset)) for (var i = 1; i < chunks - 1; i++) { res += decoder.write(list[i]) } res += decoder.end(list[i].slice(0, offset - 2)) return res } /** * Concat the collected chunks from parser.bufferCache. * * Increases the bufferPool size beforehand if necessary. * * @param parser * @returns {Buffer} */ function concatBulkBuffer (parser) { var list = parser.bufferCache var chunks = list.length var length = parser.bigStrSize - parser.bigOffset - 2 var offset = parser.bigStrSize - parser.totalChunkSize parser.offset = offset if (offset <= 2) { if (chunks === 2) { return list[0].slice(parser.bigOffset, list[0].length + offset - 2) } chunks-- offset = list[list.length - 2].length + offset } resizeBuffer(length) var start = bufferOffset list[0].copy(bufferPool, start, parser.bigOffset, list[0].length) bufferOffset += list[0].length - parser.bigOffset for (var i = 1; i < chunks - 1; i++) { list[i].copy(bufferPool, bufferOffset) bufferOffset += list[i].length } list[i].copy(bufferPool, bufferOffset, 0, offset - 2) bufferOffset += offset - 2 return bufferPool.slice(start, bufferOffset) } /** * Parse the redis buffer * @param buffer * @returns {undefined} */ JavascriptRedisParser.prototype.execute = function execute (buffer) { if (this.buffer === null) { this.buffer = buffer this.offset = 0 } else if (this.bigStrSize === 0) { var oldLength = this.buffer.length var remainingLength = oldLength - this.offset var newBuffer = bufferAlloc(remainingLength + buffer.length) this.buffer.copy(newBuffer, 0, this.offset, oldLength) buffer.copy(newBuffer, remainingLength, 0, buffer.length) this.buffer = newBuffer this.offset = 0 if (this.arrayCache.length) { var arr = parseArrayChunks(this) if (!arr) { return } this.returnReply(arr) } } else if (this.totalChunkSize + buffer.length >= this.bigStrSize) { this.bufferCache.push(buffer) var tmp = this.optionReturnBuffers ? concatBulkBuffer(this) : concatBulkString(this) this.bigStrSize = 0 this.bufferCache = [] this.buffer = buffer if (this.arrayCache.length) { this.arrayCache[0][this.arrayPos[0]++] = tmp tmp = parseArrayChunks(this) if (!tmp) { return } } this.returnReply(tmp) } else { this.bufferCache.push(buffer) this.totalChunkSize += buffer.length return } while (this.offset < this.buffer.length) { var offset = this.offset var type = this.buffer[this.offset++] var response = parseType(this, type) if (response === undefined) { if (!this.arrayCache.length) { this.offset = offset } return } if (type === 45) { this.returnError(response) } else { this.returnReply(response) } } this.buffer = null } module.exports = JavascriptRedisParser package/lib/parserError.js000644 001750 001750 0000001113 13061155044014136 0ustar00000000 000000 'use strict' var util = require('util') var assert = require('assert') var RedisError = require('./redisError') var ADD_STACKTRACE = false function ParserError (message, buffer, offset) { assert(buffer) assert.strictEqual(typeof offset, 'number') RedisError.call(this, message, ADD_STACKTRACE) this.offset = offset this.buffer = buffer Error.captureStackTrace(this, ParserError) } util.inherits(ParserError, RedisError) Object.defineProperty(ParserError.prototype, 'name', { value: 'ParserError', configurable: true, writable: true }) module.exports = ParserError package/lib/redisError.js000644 001750 001750 0000000727 13061155044013762 0ustar00000000 000000 'use strict' var util = require('util') function RedisError (message, stack) { Object.defineProperty(this, 'message', { value: message || '', configurable: true, writable: true }) if (stack || stack === undefined) { Error.captureStackTrace(this, RedisError) } } util.inherits(RedisError, Error) Object.defineProperty(RedisError.prototype, 'name', { value: 'RedisError', configurable: true, writable: true }) module.exports = RedisError package/lib/replyError.js000644 001750 001750 0000001005 13061155044013775 0ustar00000000 000000 'use strict' var util = require('util') var RedisError = require('./redisError') var ADD_STACKTRACE = false function ReplyError (message) { var tmp = Error.stackTraceLimit Error.stackTraceLimit = 2 RedisError.call(this, message, ADD_STACKTRACE) Error.captureStackTrace(this, ReplyError) Error.stackTraceLimit = tmp } util.inherits(ReplyError, RedisError) Object.defineProperty(ReplyError.prototype, 'name', { value: 'ReplyError', configurable: true, writable: true }) module.exports = ReplyError