hiredis.js
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict'
var hiredis = require('hiredis')
var ReplyError = require('../lib/replyError')
var ParserError = require('../lib/parserError')
/**
* Parse data
* @param parser
* @returns {*}
*/
function parseData (parser, data) {
try {
return parser.reader.get()
} catch (err) {
// Protocol errors land here
// Reset the parser. Otherwise new commands can't be processed properly
parser.reader = new hiredis.Reader(parser.options)
parser.returnFatalError(new ParserError(err.message, JSON.stringify(data), -1))
}
}
/**
* Hiredis Parser
* @param options
* @constructor
*/
function HiredisReplyParser (options) {
this.returnError = options.returnError
this.returnFatalError = options.returnFatalError || options.returnError
this.returnReply = options.returnReply
this.name = 'hiredis'
this.options = {
return_buffers: !!options.returnBuffers
}
this.reader = new hiredis.Reader(this.options)
}
HiredisReplyParser.prototype.execute = function (data) {
this.reader.feed(data)
var reply = parseData(this, data)
while (reply !== undefined) {
if (reply && reply.name === 'Error') {
this.returnError(new ReplyError(reply.message))
} else {
this.returnReply(reply)
}
reply = parseData(this, data)
}
}
/**
* Reset the parser values to the initial state
*
* @returns {undefined}
*/
HiredisReplyParser.prototype.reset = function () {
this.reader = new hiredis.Reader(this.options)
}
module.exports = HiredisReplyParser