Skip to content

Commit

Permalink
Try to use hiredis as default reply parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pietern committed Nov 30, 2010
1 parent 1e698e3 commit 4bef57c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
var net = require("net"),
util = require("./lib/util").util,
events = require("events"),
reply_parser = require("./lib/parser/javascript"),
reply_parser,
default_port = 6379,
default_host = "127.0.0.1";

// Try to use hiredis for reply parsing and fall back on the Javascript-based
// reply parsing code when its not available.
try {
if (process.env["DISABLE_HIREDIS"])
throw new Error(); // Fall back to the Javascript reply parsing code
reply_parser = require("./lib/parser/hiredis");
} catch(err) {
reply_parser = require("./lib/parser/javascript");
}

// can can set this to true to enable for all connections
exports.debug_mode = false;

Expand Down
35 changes: 35 additions & 0 deletions lib/parser/hiredis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var events = require("events"),
util = require("../util").util,
hiredis = require("hiredis");

function HiredisReplyParser() {
this.reset();
events.EventEmitter.call(this);
}

util.inherits(HiredisReplyParser, events.EventEmitter);

exports.Parser = HiredisReplyParser;
exports.debug_mode = false;
exports.type = "hiredis";

HiredisReplyParser.prototype.reset = function() {
this.reader = new hiredis.Reader({ return_buffers: true });
}

HiredisReplyParser.prototype.execute = function(data) {
var reply;
this.reader.feed(data);
try {
while ((reply = this.reader.get()) !== undefined) {
if (reply && reply.constructor == Error) {
this.emit("reply error", reply);
} else {
this.emit("reply", reply);
}
}
} catch(err) {
this.emit("error", err);
}
}

0 comments on commit 4bef57c

Please sign in to comment.