Détail du package

redis-parser

NodeRedis28.8mMIT3.0.0

Javascript Redis protocol (RESP) parser

redis, protocol, parser, database

readme

Build Status Test Coverage js-standard-style

redis-parser

A high performance javascript redis parser built for node_redis and ioredis. Parses all RESP data.

Install

Install with NPM:

npm install redis-parser

Usage

const Parser = require('redis-parser');

const 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): set the returnBuffers option on/off without resetting the parser
  • setStringNumbers(boolean): 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 can be imported by the npm redis-errors package.

Example

const Parser = require("redis-parser");

class Library {
  returnReply(reply) { /* ... */ }
  returnError(err) { /* ... */ }
  returnFatalError(err) { /* ... */ }

  streamHandler() {
    this.stream.on('data', (buffer) => {
      // Here the data (e.g. `Buffer.from('$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);
    });
  }
}

const lib = new Library();

const parser = new Parser({
  returnReply(reply) {
    lib.returnReply(reply);
  },
  returnError(err) {
    lib.returnError(err);
  },
  returnFatalError(err) {
    lib.returnFatalError(err);
  }
});

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.

// Same functions as in the first example

const parser = new Parser({
  returnReply(reply) {
    lib.returnReply(reply);
  },
  returnError(err) {
    lib.returnError(err);
  },
  returnBuffers: true, // All strings are returned as Buffer e.g. <Buffer 48 65 6c 6c 6f>
  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 994,387 ops/sec ±0.22% (554 runs sampled)
JS PARSER: $ multiple chunks in a bulk string x 1,010,728 ops/sec ±0.28% (559 runs sampled)
HIREDIS BUF:   $ multiple chunks in a bulk string x 648,742 ops/sec ±0.80% (526 runs sampled)
JS PARSER BUF: $ multiple chunks in a bulk string x 1,728,849 ops/sec ±0.41% (555 runs sampled)

HIREDIS:   + multiple chunks in a string x 1,861,132 ops/sec ±0.18% (564 runs sampled)
JS PARSER: + multiple chunks in a string x 2,131,892 ops/sec ±0.31% (558 runs sampled)
HIREDIS BUF:   + multiple chunks in a string x 965,132 ops/sec ±0.58% (521 runs sampled)
JS PARSER BUF: + multiple chunks in a string x 2,304,482 ops/sec ±0.31% (559 runs sampled)

HIREDIS:   $ 4mb bulk string x 269 ops/sec ±0.56% (452 runs sampled)
JS PARSER: $ 4mb bulk string x 763 ops/sec ±0.25% (466 runs sampled)
HIREDIS BUF:   $ 4mb bulk string x 336 ops/sec ±0.59% (459 runs sampled)
JS PARSER BUF: $ 4mb bulk string x 994 ops/sec ±0.36% (482 runs sampled)

HIREDIS:   + simple string x 2,504,305 ops/sec ±0.19% (563 runs sampled)
JS PARSER: + simple string x 5,121,952 ops/sec ±0.30% (560 runs sampled)
HIREDIS BUF:   + simple string x 1,122,899 ops/sec ±0.52% (516 runs sampled)
JS PARSER BUF: + simple string x 5,907,323 ops/sec ±0.23% (562 runs sampled)

HIREDIS:   : integer x 2,461,376 ops/sec ±0.14% (561 runs sampled)
JS PARSER: : integer x 18,543,688 ops/sec ±0.19% (539 runs sampled)
JS PARSER STR: : integer x 14,149,305 ops/sec ±0.24% (561 runs sampled)

HIREDIS:   : big integer x 2,114,270 ops/sec ±0.15% (561 runs sampled)
JS PARSER: : big integer x 10,794,439 ops/sec ±0.25% (560 runs sampled)
JS PARSER STR: : big integer x 4,594,807 ops/sec ±0.24% (558 runs sampled)

HIREDIS:   * array x 45,597 ops/sec ±0.23% (565 runs sampled)
JS PARSER: * array x 68,396 ops/sec ±0.30% (563 runs sampled)
HIREDIS BUF:   * array x 14,726 ops/sec ±0.39% (498 runs sampled)
JS PARSER BUF: * array x 80,961 ops/sec ±0.25% (561 runs sampled)

HIREDIS:   * big nested array x 212 ops/sec ±0.17% (511 runs sampled)
JS PARSER: * big nested array x 243 ops/sec ±0.21% (496 runs sampled)
HIREDIS BUF:   * big nested array x 207 ops/sec ±0.37% (430 runs sampled)
JS PARSER BUF: * big nested array x 297 ops/sec ±1.10% (421 runs sampled)

HIREDIS:   - error x 168,761 ops/sec ±0.28% (559 runs sampled)
JS PARSER: - error x 424,257 ops/sec ±0.28% (557 runs sampled)

Platform info:
Ubuntu 17.04
Node.js 7.10.0
Intel(R) Core(TM) i7-5600U CPU

License

MIT

changelog

Changelog

v.3.0.0 - 25 May, 2017

Breaking Changes

  • Drop support for Node.js < 4
  • Removed support for hiredis completely

Internals

  • Due to the changes to ES6 the error performance improved by factor 2-3x
  • Improved length calculation performance (bulk strings + arrays)

Features

  • The parser now handles weird input graceful

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 and Ruben Bridgewater 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