redis-parser
A high performance javascript redis parser built for node_redis and ioredis. Parses all RESP data.
Install
Install with NPM:
npm install redis-parser
Usage
var Parser = require('redis-parser');
var myParser = new Parser(options);
Possible options
returnReply
: function; mandatoryreturnError
: function; mandatoryreturnFatalError
: function; optional, defaults to the returnError functionreturnBuffers
: boolean; optional, defaults to false
Example
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);
}
});
Library.prototype.streamHandler = function () {
this.stream.on('data', function (buffer) {
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.
Big numbers that are too large for JS are automatically stringified.
var parser = new Parser({
returnReply: function(reply) {
lib.returnReply(reply);
},
returnError: function(err) {
lib.returnError(err);
},
returnBuffers: true
});
Protocol errors
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.
Contribute
The parser is highly optimized but there may still be further optimizations possible.
npm install
npm test
npm run benchmark
License
MIT