{"id":13483302,"url":"https://github.com/NodeRedis/node-redis-parser","last_synced_at":"2025-03-27T14:31:16.733Z","repository":{"id":57139320,"uuid":"44634462","full_name":"NodeRedis/node-redis-parser","owner":"NodeRedis","description":"A high performance Redis protocol (RESP) parser for JavaScript. Used by Node Redis \u0026 ioredis.","archived":false,"fork":false,"pushed_at":"2020-06-01T01:12:00.000Z","size":278,"stargazers_count":88,"open_issues_count":9,"forks_count":36,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-15T13:36:03.002Z","etag":null,"topics":["node-redis","redis-parser","resp"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NodeRedis.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-20T20:55:59.000Z","updated_at":"2024-10-28T18:29:31.000Z","dependencies_parsed_at":"2022-08-24T10:10:23.791Z","dependency_job_id":null,"html_url":"https://github.com/NodeRedis/node-redis-parser","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NodeRedis%2Fnode-redis-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NodeRedis%2Fnode-redis-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NodeRedis%2Fnode-redis-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NodeRedis%2Fnode-redis-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NodeRedis","download_url":"https://codeload.github.com/NodeRedis/node-redis-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245136368,"owners_count":20566587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["node-redis","redis-parser","resp"],"created_at":"2024-07-31T17:01:09.888Z","updated_at":"2025-03-27T14:31:11.669Z","avatar_url":"https://github.com/NodeRedis.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Build Status](https://github.com/NodeRedis/node-redis-parser/workflows/Tests/badge.svg)](https://github.com/NodeRedis/node-redis-parser/actions)\n[![Coverage Status](https://coveralls.io/repos/github/NodeRedis/node-redis-parser/badge.svg?branch=)](https://coveralls.io/github/NodeRedis/node-redis-parser?branch=master)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n\n# redis-parser\n\nA 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.\n\n## Install\n\nInstall with [NPM](https://npmjs.org/):\n\n    npm install redis-parser\n\n## Usage\n\n```js\nconst Parser = require('redis-parser');\n\nconst myParser = new Parser(options);\n```\n\n### Options\n\n* `returnReply`: *function*; mandatory\n* `returnError`: *function*; mandatory\n* `returnFatalError`: *function*; optional, defaults to the returnError function\n* `returnBuffers`: *boolean*; optional, defaults to false\n* `stringNumbers`: *boolean*; optional, defaults to false\n\n### Functions\n\n* `reset()`: reset the parser to it's initial state\n* `setReturnBuffers(boolean)`: set the returnBuffers option on/off without resetting the parser\n* `setStringNumbers(boolean)`: set the stringNumbers option on/off without resetting the parser\n\n### Error classes\n\n* `RedisError` sub class of Error\n* `ReplyError` sub class of RedisError\n* `ParserError` sub class of RedisError\n\nAll Redis errors will be returned as `ReplyErrors` while a parser error is returned as `ParserError`.  \nAll error classes can be imported by the npm `redis-errors` package.\n\n### Example\n\n```js\nconst Parser = require(\"redis-parser\");\n\nclass Library {\n  returnReply(reply) { /* ... */ }\n  returnError(err) { /* ... */ }\n  returnFatalError(err) { /* ... */ }\n\n  streamHandler() {\n    this.stream.on('data', (buffer) =\u003e {\n      // Here the data (e.g. `Buffer.from('$5\\r\\nHello\\r\\n'`))\n      // is passed to the parser and the result is passed to\n      // either function depending on the provided data.\n      parser.execute(buffer);\n    });\n  }\n}\n\nconst lib = new Library();\n\nconst parser = new Parser({\n  returnReply(reply) {\n    lib.returnReply(reply);\n  },\n  returnError(err) {\n    lib.returnError(err);\n  },\n  returnFatalError(err) {\n    lib.returnFatalError(err);\n  }\n});\n```\n\nYou do not have to use the returnFatalError function. Fatal errors will be returned in the normal error function in that case.\n\nAnd if you want to return buffers instead of strings, you can do this by adding the `returnBuffers` option.\n\nIf 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.\n\n```js\n// Same functions as in the first example\n\nconst parser = new Parser({\n  returnReply(reply) {\n    lib.returnReply(reply);\n  },\n  returnError(err) {\n    lib.returnError(err);\n  },\n  returnBuffers: true, // All strings are returned as Buffer e.g. \u003cBuffer 48 65 6c 6c 6f\u003e\n  stringNumbers: true // All numbers are returned as String\n});\n\n// The streamHandler as above\n```\n\n## Protocol errors\n\nTo 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.\n\n## Contribute\n\nThe parser is highly optimized but there may still be further optimizations possible.\n\n    npm install\n    npm test\n    npm run benchmark\n\nCurrently the benchmark compares the performance against the hiredis parser:\n\n    HIREDIS:   $ multiple chunks in a bulk string x 1,169,386 ops/sec ±1.24% (92 runs sampled)\n    JS PARSER: $ multiple chunks in a bulk string x 1,354,290 ops/sec ±1.69% (88 runs sampled)\n    HIREDIS BUF:   $ multiple chunks in a bulk string x 633,639 ops/sec ±2.64% (84 runs sampled)\n    JS PARSER BUF: $ multiple chunks in a bulk string x 1,783,922 ops/sec ±0.47% (94 runs sampled)\n\n    HIREDIS:   + multiple chunks in a string x 2,394,900 ops/sec ±0.31% (93 runs sampled)\n    JS PARSER: + multiple chunks in a string x 2,264,354 ops/sec ±0.29% (94 runs sampled)\n    HIREDIS BUF:   + multiple chunks in a string x 953,733 ops/sec ±2.03% (82 runs sampled)\n    JS PARSER BUF: + multiple chunks in a string x 2,298,458 ops/sec ±0.79% (96 runs sampled)\n\n    HIREDIS:   $ 4mb bulk string x 152 ops/sec ±2.03% (72 runs sampled)\n    JS PARSER: $ 4mb bulk string x 971 ops/sec ±0.79% (86 runs sampled)\n    HIREDIS BUF:   $ 4mb bulk string x 169 ops/sec ±2.25% (71 runs sampled)\n    JS PARSER BUF: $ 4mb bulk string x 797 ops/sec ±7.08% (77 runs sampled)\n\n    HIREDIS:   + simple string x 3,341,956 ops/sec ±1.01% (94 runs sampled)\n    JS PARSER: + simple string x 5,979,545 ops/sec ±0.38% (96 runs sampled)\n    HIREDIS BUF: + simple string x 1,031,745 ops/sec ±2.17% (76 runs sampled)\n    JS PARSER BUF: + simple string x 6,960,184 ops/sec ±0.28% (93 runs sampled)\n\n    HIREDIS:   : integer x 3,897,626 ops/sec ±0.42% (91 runs sampled)\n    JS PARSER: : integer x 37,035,812 ops/sec ±0.32% (94 runs sampled)\n    JS PARSER STR: : integer x 25,515,070 ops/sec ±1.79% (83 runs sampled)\n\n    HIREDIS:   : big integer x 3,036,704 ops/sec ±0.47% (92 runs sampled)\n    JS PARSER: : big integer x 10,616,464 ops/sec ±0.94% (94 runs sampled)\n    JS PARSER STR: : big integer x 7,098,146 ops/sec ±0.47% (94 runs sampled)\n\n    HIREDIS:   * array x 51,542 ops/sec ±0.35% (94 runs sampled)\n    JS PARSER: * array x 87,090 ops/sec ±2.17% (94 runs sampled)\n    HIREDIS BUF:   * array x 11,733 ops/sec ±1.80% (80 runs sampled)\n    JS PARSER BUF: * array x 149,430 ops/sec ±1.50% (88 runs sampled)\n\n    HIREDIS:   * big nested array x 247 ops/sec ±0.93% (73 runs sampled)\n    JS PARSER: * big nested array x 286 ops/sec ±0.79% (83 runs sampled)\n    HIREDIS BUF:   * big nested array x 217 ops/sec ±1.80% (73 runs sampled)\n    JS PARSER BUF: * big nested array x 175 ops/sec ±2.49% (37 runs sampled)\n\n    HIREDIS:   - error x 108,110 ops/sec ±0.63% (84 runs sampled)\n    JS PARSER: - error x 172,665 ops/sec ±0.57% (85 runs sampled)\n\n    Platform info:\n    OSX 10.12.6\n    Node.js 10.0.0\n    Intel(R) Core(TM) i7-5600U CPU\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNodeRedis%2Fnode-redis-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNodeRedis%2Fnode-redis-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNodeRedis%2Fnode-redis-parser/lists"}