You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.7 KiB

4 years ago
  1. [![Build Status](https://travis-ci.org/NodeRedis/node-redis-parser.png?branch=master)](https://travis-ci.org/NodeRedis/node-redis-parser)
  2. [![Test Coverage](https://codeclimate.com/github/NodeRedis/node-redis-parser/badges/coverage.svg)](https://codeclimate.com/github/NodeRedis/node-redis-parser/coverage)
  3. [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
  4. # redis-parser
  5. 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.
  6. ## Install
  7. Install with [NPM](https://npmjs.org/):
  8. npm install redis-parser
  9. ## Usage
  10. ```js
  11. var Parser = require('redis-parser');
  12. var myParser = new Parser(options);
  13. ```
  14. ### Options
  15. * `returnReply`: *function*; mandatory
  16. * `returnError`: *function*; mandatory
  17. * `returnFatalError`: *function*; optional, defaults to the returnError function
  18. * `returnBuffers`: *boolean*; optional, defaults to false
  19. * `stringNumbers`: *boolean*; optional, defaults to false
  20. ### Functions
  21. * `reset()`: reset the parser to it's initial state
  22. * `setReturnBuffers(boolean)`: (JSParser only) set the returnBuffers option on/off without resetting the parser
  23. * `setStringNumbers(boolean)`: (JSParser only) set the stringNumbers option on/off without resetting the parser
  24. ### Error classes
  25. * `RedisError` sub class of Error
  26. * `ReplyError` sub class of RedisError
  27. * `ParserError` sub class of RedisError
  28. All Redis errors will be returned as `ReplyErrors` while a parser error is returned as `ParserError`.
  29. All error classes are exported by the parser.
  30. ### Example
  31. ```js
  32. var Parser = require("redis-parser");
  33. function Library () {}
  34. Library.prototype.returnReply = function (reply) { ... }
  35. Library.prototype.returnError = function (err) { ... }
  36. Library.prototype.returnFatalError = function (err) { ... }
  37. var lib = new Library();
  38. var parser = new Parser({
  39. returnReply: function(reply) {
  40. lib.returnReply(reply);
  41. },
  42. returnError: function(err) {
  43. lib.returnError(err);
  44. },
  45. returnFatalError: function (err) {
  46. lib.returnFatalError(err);
  47. }
  48. });
  49. Library.prototype.streamHandler = function () {
  50. this.stream.on('data', function (buffer) {
  51. // 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.
  52. parser.execute(buffer);
  53. });
  54. };
  55. ```
  56. You do not have to use the returnFatalError function. Fatal errors will be returned in the normal error function in that case.
  57. And if you want to return buffers instead of strings, you can do this by adding the `returnBuffers` option.
  58. 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.
  59. ```js
  60. // Same functions as in the first example
  61. var parser = new Parser({
  62. returnReply: function(reply) {
  63. lib.returnReply(reply);
  64. },
  65. returnError: function(err) {
  66. lib.returnError(err);
  67. },
  68. returnBuffers: true, // All strings are returned as Buffer e.g. <Buffer 48 65 6c 6c 6f>
  69. stringNumbers: true // All numbers are returned as String
  70. });
  71. // The streamHandler as above
  72. ```
  73. ## Protocol errors
  74. 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.
  75. ## Contribute
  76. The parser is highly optimized but there may still be further optimizations possible.
  77. npm install
  78. npm test
  79. npm run benchmark
  80. Currently the benchmark compares the performance against the hiredis parser:
  81. HIREDIS: $ multiple chunks in a bulk string x 859,880 ops/sec ±1.22% (82 runs sampled)
  82. HIREDIS BUF: $ multiple chunks in a bulk string x 608,869 ops/sec ±1.72% (85 runs sampled)
  83. JS PARSER: $ multiple chunks in a bulk string x 910,590 ops/sec ±0.87% (89 runs sampled)
  84. JS PARSER BUF: $ multiple chunks in a bulk string x 1,299,507 ops/sec ±2.18% (84 runs sampled)
  85. HIREDIS: + multiple chunks in a string x 1,787,203 ops/sec ±0.58% (96 runs sampled)
  86. HIREDIS BUF: + multiple chunks in a string x 943,584 ops/sec ±1.62% (87 runs sampled)
  87. JS PARSER: + multiple chunks in a string x 2,008,264 ops/sec ±1.01% (91 runs sampled)
  88. JS PARSER BUF: + multiple chunks in a string x 2,045,546 ops/sec ±0.78% (91 runs sampled)
  89. HIREDIS: $ 4mb bulk string x 310 ops/sec ±1.58% (75 runs sampled)
  90. HIREDIS BUF: $ 4mb bulk string x 471 ops/sec ±2.28% (78 runs sampled)
  91. JS PARSER: $ 4mb bulk string x 747 ops/sec ±2.43% (85 runs sampled)
  92. JS PARSER BUF: $ 4mb bulk string x 846 ops/sec ±5.52% (72 runs sampled)
  93. HIREDIS: + simple string x 2,324,866 ops/sec ±1.61% (90 runs sampled)
  94. HIREDIS BUF: + simple string x 1,085,823 ops/sec ±2.47% (82 runs sampled)
  95. JS PARSER: + simple string x 4,567,358 ops/sec ±1.97% (81 runs sampled)
  96. JS PARSER BUF: + simple string x 5,433,901 ops/sec ±0.66% (93 runs sampled)
  97. HIREDIS: : integer x 2,332,946 ops/sec ±0.47% (93 runs sampled)
  98. JS PARSER: : integer x 17,730,449 ops/sec ±0.73% (91 runs sampled)
  99. JS PARSER STR: : integer x 12,942,037 ops/sec ±0.51% (92 runs sampled)
  100. HIREDIS: : big integer x 2,012,572 ops/sec ±0.33% (93 runs sampled)
  101. JS PARSER: : big integer x 10,210,923 ops/sec ±0.94% (94 runs sampled)
  102. JS PARSER STR: : big integer x 4,453,320 ops/sec ±0.52% (94 runs sampled)
  103. HIREDIS: * array x 44,479 ops/sec ±0.55% (94 runs sampled)
  104. HIREDIS BUF: * array x 14,391 ops/sec ±1.04% (86 runs sampled)
  105. JS PARSER: * array x 53,796 ops/sec ±2.08% (79 runs sampled)
  106. JS PARSER BUF: * array x 72,428 ops/sec ±0.72% (93 runs sampled)
  107. HIREDIS: * big nested array x 217 ops/sec ±0.97% (83 runs sampled)
  108. HIREDIS BUF: * big nested array x 255 ops/sec ±2.28% (77 runs sampled)
  109. JS PARSER: * big nested array x 242 ops/sec ±1.10% (85 runs sampled)
  110. JS PARSER BUF: * big nested array x 375 ops/sec ±1.21% (88 runs sampled)
  111. HIREDIS: - error x 78,821 ops/sec ±0.80% (93 runs sampled)
  112. JS PARSER: - error x 143,382 ops/sec ±0.75% (92 runs sampled)
  113. Platform info:
  114. Ubuntu 16.10
  115. Node.js 7.4.0
  116. Intel(R) Core(TM) i7-5600U CPU
  117. ## License
  118. [MIT](./LICENSE)