{"library":"redis-parser","title":"Redis Protocol Parser for Node.js","description":"redis-parser is a high-performance JavaScript implementation of the Redis Serialization Protocol (RESP) parser, specifically designed for Node.js environments. It serves as the underlying parsing engine for popular Redis clients like node-redis and ioredis. The current stable version is 3.0.0. This library focuses on efficiently converting raw Redis protocol buffers into usable JavaScript data structures, supporting various RESP data types including strings, numbers, arrays, and errors. Its development prioritizes performance, especially for large data structures, and graceful handling of malformed input, making it a critical component for robust Redis client libraries. It also exports dedicated error classes for specific parsing and reply errors.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install redis-parser"],"cli":null},"imports":["const Parser = require('redis-parser');","const { RedisError } = require('redis-parser');","const { ParserError } = require('redis-parser');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Parser = require('redis-parser');\n\nclass MyRedisClient {\n  constructor() {\n    this.replies = [];\n    this.errors = [];\n    this.fatalErrors = [];\n    this.parser = new Parser({\n      returnReply: this.handleReply.bind(this),\n      returnError: this.handleError.bind(this),\n      returnFatalError: this.handleFatalError.bind(this),\n      returnBuffers: false, // Default: return strings instead of Buffers\n      stringNumbers: false  // Default: return JavaScript numbers, not strings\n    });\n  }\n\n  handleReply(reply) {\n    this.replies.push(reply);\n    console.log('Received Reply:', reply);\n  }\n\n  handleError(err) {\n    this.errors.push(err);\n    console.error('Received Error:', err.message);\n  }\n\n  handleFatalError(err) {\n    this.fatalErrors.push(err);\n    console.error('Received FATAL Error (protocol error):', err.message);\n    // In a real Redis client, this is where you would typically:\n    // 1. Log the error for debugging.\n    // 2. Reject all outstanding commands.\n    // 3. Destroy the current socket connection.\n    // 4. Attempt to reconnect to the Redis server.\n    // This prevents further data corruption due to protocol desynchronization.\n  }\n\n  processStreamData(dataBuffer) {\n    // Simulate receiving raw Redis protocol data (as a Buffer) from a stream\n    this.parser.execute(dataBuffer);\n  }\n}\n\nconst client = new MyRedisClient();\n\n// Simulate receiving various Redis protocol responses\nclient.processStreamData(Buffer.from('$5\\r\\nHello\\r\\n')); // Bulk String \"Hello\"\nclient.processStreamData(Buffer.from(':12345\\r\\n'));    // Integer 12345\nclient.processStreamData(Buffer.from('*2\\r\\n$3\\r\\nfoo\\r\\n$3\\r\\nbar\\r\\n')); // Array [\"foo\", \"bar\"]\nclient.processStreamData(Buffer.from('-ERR unknown command `FOO`\\r\\n')); // Error Reply\n\nconsole.log('\\n--- Parser State & Collected Data ---');\nconsole.log('Total Replies:', client.replies.length, client.replies);\nconsole.log('Total Errors:', client.errors.length, client.errors.map(e => e.message));\n\n// Demonstrate dynamic option change: return numbers as strings for large integers\nclient.parser.setStringNumbers(true);\nclient.processStreamData(Buffer.from(':90071992547409920\\r\\n')); // A number larger than Number.MAX_SAFE_INTEGER\nconsole.log('Last Reply (string number):', client.replies[client.replies.length - 1]);","lang":"javascript","description":"This quickstart initializes `redis-parser` with necessary callback functions for handling parsed replies, standard errors, and critical protocol errors. It demonstrates feeding various Redis protocol buffers to the parser's `execute` method and dynamically adjusting parsing options like `stringNumbers` for safe handling of large integers.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}