Socket
Socket
Sign inDemoInstall

redis-parser

Package Overview
Dependencies
0
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    redis-parser

Javascript Redis protocol (RESP) parser


Version published
Weekly downloads
4.7M
decreased by-9.73%
Maintainers
1
Install size
21.2 kB
Created
Weekly downloads
 

Package description

What is redis-parser?

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.

What are redis-parser's main functionalities?

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'));
}

Other packages similar to redis-parser

Changelog

Source

v.2.0.4 - 21 Jul, 2016

Bugfixes

  • Fixed multi byte characters getting corrupted

Readme

Source

Build Status Code Climate Test Coverage js-standard-style

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; mandatory
  • returnError: function; mandatory
  • returnFatalError: function; optional, defaults to the returnError function
  • returnBuffers: 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) {
        // 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.

Big numbers that are too large for JS are automatically stringified.

// Same functions as in the first example

var parser = new Parser({
    returnReply: function(reply) {
        lib.returnReply(reply);
    },
    returnError: function(err) {
        lib.returnError(err);
    },
    returnBuffers: true // All strings are returned as buffer e.g. <Buffer 48 65 6c 6c 6f>
});

// The streamHandler as above

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

Keywords

FAQs

Last updated on 21 Jul 2016

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc