1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

Add remaining commands. Parse error replies.

This commit is contained in:
Matt Ranney
2010-09-14 01:11:59 -07:00
parent deb0fe87e4
commit 9f770d99ef

View File

@@ -47,6 +47,10 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
this.state = "bulk length"; this.state = "bulk length";
this.tmp_buffer.end = 0; this.tmp_buffer.end = 0;
break; break;
case 45: // -
this.state = "error line";
this.return_buffer.end = 0;
break;
default: default:
this.state = "unknown type"; this.state = "unknown type";
} }
@@ -60,6 +64,17 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
this.return_buffer.end += 1; this.return_buffer.end += 1;
// TODO - check for return_buffer overflow and then grow, copy, continue, and drink. // TODO - check for return_buffer overflow and then grow, copy, continue, and drink.
} }
pos += 1;
break;
case "error line":
if (incoming_buf[pos] === 13) {
this.emit("error reply", new Error(this.return_buffer.slice(0, this.return_buffer.end)));
this.state = "final lf";
} else {
this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
this.return_buffer.end += 1;
}
pos += 1; pos += 1;
break; break;
case "single line": case "single line":
@@ -222,6 +237,9 @@ RedisClient.prototype.on_connect = function () {
this.reply_parser = new RedisReplyParser(); this.reply_parser = new RedisReplyParser();
var self = this; var self = this;
this.reply_parser.on("error reply", function (err) {
self.return_error(err);
});
this.reply_parser.on("null reply", function () { this.reply_parser.on("null reply", function () {
self.return_reply(null); self.return_reply(null);
}); });
@@ -252,6 +270,13 @@ RedisClient.prototype.on_data = function (data) {
} }
}; };
RedisClient.prototype.return_error = function (err) {
var command_obj = this.command_queue.shift();
console.log("Error on " + command_obj.command + " " + command_obj.args + ": " + err);
command_obj.callback(err);
}
RedisClient.prototype.return_reply = function (response_buffer) { RedisClient.prototype.return_reply = function (response_buffer) {
var command_obj = this.command_queue.shift(); var command_obj = this.command_queue.shift();
@@ -303,7 +328,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
} }
command_str += "$" + arg.length + "\r\n" + arg + "\r\n"; command_str += "$" + arg.length + "\r\n" + arg + "\r\n";
}); });
console.log("non-buffer full command: " + command_str); // console.log("non-buffer full command: " + command_str);
if (stream.write(command_str) === false) { if (stream.write(command_str) === false) {
console.log("Buffered write 0"); console.log("Buffered write 0");
} }
@@ -330,24 +355,42 @@ RedisClient.prototype.send_command = function (command, args, callback) {
}; };
// http://code.google.com/p/redis/wiki/CommandReference // http://code.google.com/p/redis/wiki/CommandReference
[ // Commands operating on all value types exports.commands = [
"EXISTS", "DEL", "TYPE", "KEYS", "RANDOMKEY", "RENAME", "RENAMENX", "DBSIZE", "EXPIRE", "PERSIST", "TTL", "SELECT", // Commands operating on all value types
"MOVE", "FLUSHDB", "FLUSHALL", "INFO", "SET", "EXISTS", "DEL", "TYPE", "KEYS", "RANDOMKEY", "RENAME", "RENAMENX", "DBSIZE", "EXPIRE", "TTL", "SELECT",
"MOVE", "FLUSHDB", "FLUSHALL",
// Commands operating on string values // Commands operating on string values
"SET", "GET", "GETSET", "MGET", "SETNX", "SETEX", "MSET", "MSETNX", "INCR", "INCRBY", "DECR", "DECRBY", "APPEND", "SUBSTR", "SET", "GET", "GETSET", "MGET", "SETNX", "SETEX", "MSET", "MSETNX", "INCR", "INCRBY", "DECR", "DECRBY", "APPEND", "SUBSTR",
// Commands operating on lists // Commands operating on lists
"RPUSH", "LPUSH", "LLEN", "LRANGE", "LTRIM", "LINDEX", "LSET", "LREM", "LPOP", "RPOP", "BLPOP", "BRPOP", "RPOPLPUSH" "RPUSH", "LPUSH", "LLEN", "LRANGE", "LTRIM", "LINDEX", "LSET", "LREM", "LPOP", "RPOP", "BLPOP", "BRPOP", "RPOPLPUSH",
// Commands operating on sets // Commands operating on sets
// TODO - type all of these in "SADD", "SREM", "SPOP", "SMOVE", "SCARD", "SISMEMBER", "SINTER", "SINTERSTORE", "SUNION", "SUNIONSTORE", "SDIFF", "SDIFFSTORE",
] "SMEMBERS", "SRANDMEMBER",
.forEach(function (command) { // Commands operating on sorted zsets (sorted sets)
RedisClient.prototype[command] = function (args, callback) { "ZADD", "ZREM", "ZINCRBY", "ZRANK", "ZREVRANK", "ZRANGE", "ZREVRANGE", "ZRANGEBYSCORE", "ZCOUNT", "ZCARD", "ZSCORE",
this.send_command(command, args, callback) "ZREMRANGEBYRANK", "ZREMRANGEBYSCORE", "ZUNIONSTORE", "ZINTERSTORE",
}; // Commands operating on hashes
RedisClient.prototype[command.toLowerCase()] = function (args, callback) { "HSET", "HGET", "HMGET", "HMSET", "HINCRBY", "HEXISTS", "HDEL", "HLEN", "HKEYS", "HVALS", "HGETALL",
this.send_command(command, args, callback) // Sorting
}; "SORT",
}); // Transactions
"MULTI", "EXEC", "DISCARD", "WATCH", "UNWATCH",
// Publish/Subscribe
"SUBSCRIBE", "UNSUBSCRIBE", "PUBLISH",
// Persistence control commands
"SAVE", "BGSAVE", "LASTSAVE", "SHUTDOWN", "BGREWRITEAOF",
// Remote server control commands
"INFO", "MONITOR", "SLAVEOF", "CONFIG"
];
exports.commands.forEach(function (command) {
RedisClient.prototype[command] = function (args, callback) {
this.send_command(command, args, callback)
};
RedisClient.prototype[command.toLowerCase()] = function (args, callback) {
this.send_command(command, args, callback)
};
});
exports.createClient = function (port_arg, host_arg, options) { exports.createClient = function (port_arg, host_arg, options) {
var port = port_arg || default_port, var port = port_arg || default_port,