A blazing-fast, fully-typed TypeScript library for decoding and encoding Redis RESP protocol messages. Perfect for custom Redis server implementations, testing tools, proxies, or educational projects.
- Incremental RESP parsing (simple string, error, integer, bulk string, array, null)
- Buffer-safe for streaming/TCP β handles fragmented messages
- Robust error reporting with auto-recovery options
- Universal encoder β encode any JS or RESP value to valid RESP wire protocol
- Comprehensive TypeScript types β safe and intuitive DX
- Event-driven API with sync parsing and buffer inspection
npm install redis-parser-ts
const parser = new RESPParser()
parser.on('message', msg => {
// Handle decoded RESPValue object
console.log(msg)
})
parser.on('error', err => {
// Typed error object
console.error('Parse error:', err)
})
// Feed chunks (as Buffer or string) from network or files
parser.feed('+OK\r\n:42\r\n')
import { RESLEncoder } from 'redis-parser-ts'
// Simple reply
const respOk = RESLEncoder.simple('OK') // Buffer with '+OK\r\n'
// Bulk string
const respBulk = RESLEncoder.bulk('foobar') // Buffer with '$6\r\nfoobar\r\n'
// Array
const respArray = RESLEncoder.array(['foo', 'bar']) // Buffer with '*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n'
// Send Buffer to TCP socket, etc.
.feed(data: Buffer | string)
β Add a chunk to the parser, emits 'message' event(s) for each complete message..on('message', callback)
β Event for RESPValue objects..on('error', callback)
β Event for protocol errors..parseSync()
β Synchronously parse one message from buffer, returns RESPValue or null..reset()
β Reset buffer and state..clearBuffer()
β Wipe buffer while retaining parser state..getBufferLength()
β Get length of current buffer.
.simple(value: string): Buffer
β Encode a simple string..error(value: string): Buffer
β Encode an error reply..integer(value: number): Buffer
β Encode integer..bulk(value: string | Buffer | null): Buffer
β Encode bulk string or null..array(values: Array<string | number | null | Buffer | RESPValue | Array<any>>): Buffer
β Encode array..encode(value: any): Buffer
β Smart generic encoder for any valid value.
All parser messages and encoder values conform to:
type RESPValue =
| { type: 'simple', value: string }
| { type: 'error', value: string }
| { type: 'integer', value: number }
| { type: 'bulk', value: string | null }
| { type: 'array', value: RESPValue[] | null }
MIT