Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
redis-parser
Advanced tools
The redis-parser npm package is designed for parsing responses from a Redis server. It supports both JavaScript and TypeScript and is capable of handling different types of Redis responses, including errors, simple strings, integers, bulk strings, arrays, and more. It's particularly useful for developers working with Redis in Node.js environments, providing a straightforward way to interpret the data returned by Redis commands.
Error Handling
This feature allows handling of Redis errors. When a Redis server responds with an error, the parser can catch and process this error appropriately.
{"const { Parser } = require('redis-parser');
const parser = new Parser({
returnError: function(err) { console.error('Error:', err); },
returnReply: function(reply) { console.log('Reply:', reply); }
});
parser.execute(Buffer.from('-Error message\r\n'));
}
Parsing Simple Strings
This demonstrates how to parse simple string responses from Redis. It's useful for commands that return a simple status response.
{"const { Parser } = require('redis-parser');
const parser = new Parser({
returnError: function(err) { console.error('Error:', err); },
returnReply: function(reply) { console.log('Reply:', reply); }
});
parser.execute(Buffer.from('+OK\r\n'));
}
Parsing Arrays
This feature is for parsing array responses from Redis, which is common for commands that return multiple values.
{"const { Parser } = require('redis-parser');
const parser = new Parser({
returnError: function(err) { console.error('Error:', err); },
returnReply: function(reply) { console.log('Reply:', reply); }
});
parser.execute(Buffer.from('*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n'));
}
ioredis is a robust, performance-focused, and full-featured Redis client for Node.js. Unlike redis-parser, which focuses solely on parsing responses, ioredis provides a comprehensive solution for interacting with Redis, including connection, command execution, and response parsing.
A high performance redis parser solution built for node_redis and ioredis.
Generally all RESP data will be properly parsed by the parser.
Install with NPM:
npm install redis-parser
var Parser = require('redis-parser');
new Parser(options);
returnReply
: function; mandatoryreturnError
: function; mandatoryreturnFatalError
: function; optional, defaults to the returnError functionreturnBuffers
: boolean; optional, defaults to falsename
: 'javascript'|'hiredis'|'auto'|null; optional, defaults to hiredis and falls back to the js parser if not available or if the stringNumbers option is choosen. Setting this to 'auto' or null is going to automatically determine what parser is available and chooses that one.stringNumbers
: boolean; optional, defaults to false. This is only available for the javascript parser at the moment!var Parser = require("redis-parser");
function Library () {}
Library.prototype.returnReply = function (reply) { ... }
Library.prototype.returnError = function (err) { ... }
Library.prototype.returnFatalError = function (err) { ... }
var lib = new Library();
var parser = new Parser({
returnReply: function(reply) {
lib.returnReply(reply);
},
returnError: function(err) {
lib.returnError(err);
},
returnFatalError: function (err) {
lib.returnFatalError(err);
},
name: 'auto' // This returns either the hiredis or the js parser instance depending on what's available
});
Library.prototype.streamHandler = function () {
this.stream.on('data', function (buffer) {
// 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.
parser.execute(buffer);
});
};
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 big numbers, you should pass the stringNumbers
option. That case numbers above 2^53 can be handled properly without reduced precision.
// Same functions as in the first example
var parser = new Parser({
returnReply: function(reply) {
lib.returnReply(reply);
},
returnError: function(err) {
lib.returnError(err);
},
name: 'javascript', // Use the Javascript parser
stringNumbers: true, // Return all numbers as string instead of a js number
returnBuffers: true // All strings are returned as buffer e.g. <Buffer 48 65 6c 6c 6f>
});
// The streamHandler as above
The hiredis parser is still the fasted parser for Node.js and therefor used as default in redis-parser if the hiredis parser is available.
Otherwise the pure js NodeRedis parser is choosen that is almost as fast as the hiredis parser besides some situations in which it'll be a bit slower.
To handle protocol errors (this is very unlikely to happen) gracefuly 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. 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.
The js parser is already optimized but there are likely further optimizations possible. Besides running the tests you'll also have to run the change at least against the node_redis benchmark suite and post the improvement in the PR. If you want to write a own parser benchmark, that would also be great!
npm install
npm test
# Run node_redis benchmark (let's guess you cloned node_redis in another folder)
cd ../redis
npm install
npm run benchmark parser=javascript > old.log
# Replace the changed parser in the node_modules
npm run benchmark parser=javascript > new.log
node benchmarks/diff_multi_bench_output.js old.log new.log > improvement.log
v.1.3.0 - 27 Mar, 2016
Features
auto
as parser name option to check what parser is availableFAQs
Javascript Redis protocol (RESP) parser
The npm package redis-parser receives a total of 4,700,174 weekly downloads. As such, redis-parser popularity was classified as popular.
We found that redis-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.