You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Allow variable argument types on all commands.
This commit is contained in:
111
index.js
111
index.js
@@ -1,3 +1,5 @@
|
|||||||
|
/*global Buffer require exports console setTimeout */
|
||||||
|
|
||||||
var net = require("net"),
|
var net = require("net"),
|
||||||
sys = require("sys"),
|
sys = require("sys"),
|
||||||
events = require("events"),
|
events = require("events"),
|
||||||
@@ -52,7 +54,7 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
|
|||||||
break;
|
break;
|
||||||
case "integer line":
|
case "integer line":
|
||||||
if (incoming_buf[pos] === 13) {
|
if (incoming_buf[pos] === 13) {
|
||||||
this.send_reply(parseInt(this.return_buffer.slice(0, this.return_buffer.end)),10);
|
this.send_reply(parseInt(this.return_buffer.slice(0, this.return_buffer.end) ,10));
|
||||||
this.state = "final lf";
|
this.state = "final lf";
|
||||||
} else {
|
} else {
|
||||||
this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
|
this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
|
||||||
@@ -178,7 +180,7 @@ RedisReplyParser.prototype.send_error = function (reply) {
|
|||||||
} else {
|
} else {
|
||||||
this.emit("reply error", reply);
|
this.emit("reply error", reply);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
RedisReplyParser.prototype.send_reply = function (reply) {
|
RedisReplyParser.prototype.send_reply = function (reply) {
|
||||||
if (this.multi_bulk_length > 0) {
|
if (this.multi_bulk_length > 0) {
|
||||||
@@ -289,7 +291,7 @@ RedisClient.prototype.connection_gone = function () {
|
|||||||
self.stream.destroy();
|
self.stream.destroy();
|
||||||
self.stream.connect(self.port, self.host);
|
self.stream.connect(self.port, self.host);
|
||||||
}, self.retry_delay);
|
}, self.retry_delay);
|
||||||
}
|
};
|
||||||
|
|
||||||
RedisClient.prototype.on_data = function (data) {
|
RedisClient.prototype.on_data = function (data) {
|
||||||
if (exports.debug_mode) {
|
if (exports.debug_mode) {
|
||||||
@@ -313,7 +315,7 @@ RedisClient.prototype.return_error = function (err) {
|
|||||||
// this will probably not make it anywhere useful, but we might as well try
|
// this will probably not make it anywhere useful, but we might as well try
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
RedisClient.prototype.return_reply = function (reply_buffer) {
|
RedisClient.prototype.return_reply = function (reply_buffer) {
|
||||||
var command_obj = this.command_queue.shift();
|
var command_obj = this.command_queue.shift();
|
||||||
@@ -327,23 +329,32 @@ RedisClient.prototype.return_reply = function (reply_buffer) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
RedisClient.prototype.send_command = function (command, args, callback) {
|
RedisClient.prototype.send_command = function () {
|
||||||
if (! command) {
|
var command, callback, args, this_args;
|
||||||
throw new Error("First argument of send_command must be the command name");
|
|
||||||
return;
|
this_args = Array.prototype.slice.call(arguments); // convert arguments into real array
|
||||||
|
|
||||||
|
command = this_args[0];
|
||||||
|
if (this_args[1] && Array.isArray(this_args[1])) {
|
||||||
|
args = this_args[1];
|
||||||
|
if (typeof this_args[2] === "function") {
|
||||||
|
callback = this_args[2];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (typeof this_args[this_args.length - 1] === "function") {
|
||||||
|
callback = this_args[this_args.length - 1];
|
||||||
|
args = this_args.slice(1, this_args.length - 1);
|
||||||
|
} else {
|
||||||
|
args = this_args.slice(1, this_args.length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! Array.isArray(args)) {
|
if (typeof command !== "string") {
|
||||||
throw new Error("Second argument of send_command must an array of arguments");
|
throw new Error("First argument of send_command must be the command name");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (callback !== undefined && typeof callback !== "function") {
|
|
||||||
throw new Error("Third argument of send_command must a results callback function, or omitted");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! this.connected) {
|
if (! this.connected) {
|
||||||
|
// TODO - queue this command and send it once we are connected.
|
||||||
callback(new Error("Redis client is not connected"));
|
callback(new Error("Redis client is not connected"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -386,18 +397,18 @@ RedisClient.prototype.send_command = function (command, args, callback) {
|
|||||||
|
|
||||||
args.forEach(function (arg) {
|
args.forEach(function (arg) {
|
||||||
if (arg.length === undefined) {
|
if (arg.length === undefined) {
|
||||||
arg = String(number);
|
arg = String(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arg instanceof Buffer) {
|
if (arg instanceof Buffer) {
|
||||||
stream.write("$" + arg.length + "\r\n")
|
stream.write("$" + arg.length + "\r\n");
|
||||||
stream.write(arg);
|
stream.write(arg);
|
||||||
stream.write("\r\n");
|
stream.write("\r\n");
|
||||||
} else {
|
} else {
|
||||||
stream.write("$" + arg.length + "\r\n" + arg + "\r\n");
|
stream.write("$" + arg.length + "\r\n" + arg + "\r\n");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// http://code.google.com/p/redis/wiki/CommandReference
|
// http://code.google.com/p/redis/wiki/CommandReference
|
||||||
@@ -426,11 +437,15 @@ exports.commands = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
exports.commands.forEach(function (command) {
|
exports.commands.forEach(function (command) {
|
||||||
RedisClient.prototype[command] = function (args, callback) {
|
RedisClient.prototype[command] = function () {
|
||||||
this.send_command(command, args, callback)
|
var args = Array.prototype.slice.call(arguments); // convert "arguments" into a real Array
|
||||||
|
args.unshift(command); // put command at the beginning
|
||||||
|
this.send_command.apply(this, args);
|
||||||
};
|
};
|
||||||
RedisClient.prototype[command.toLowerCase()] = function (args, callback) {
|
RedisClient.prototype[command.toLowerCase()] = function (args, callback) {
|
||||||
this.send_command(command, args, callback)
|
var args = Array.prototype.slice.call(arguments); // convert "arguments" into a real Array
|
||||||
|
args.unshift(command); // put command at the beginning
|
||||||
|
this.send_command.apply(this, args);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -442,38 +457,34 @@ exports.commands.forEach(function (command) {
|
|||||||
RedisClient.prototype.multi = function (commands) {
|
RedisClient.prototype.multi = function (commands) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
try {
|
this.send_command("MULTI", function (err, reply) {
|
||||||
this.send_command("MULTI", [], function (err, reply) {
|
if (err) {
|
||||||
|
console.warn("Error starting MULTI request: " + err.stack);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
commands.forEach(function (args, command_num) {
|
||||||
|
self.send_command(args[0], args[1], function (err, reply) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.warn("Error starting MULTI request: " + err.stack);
|
args[2](err);
|
||||||
|
commands.splice(command_num, 1); // what if this runs before all commands are sent?
|
||||||
|
} else {
|
||||||
|
if (reply !== "QUEUED") {
|
||||||
|
console.warn("Unexpected MULTI reply: " + reply + " instead of 'QUEUED'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
commands.forEach(function (args, command_num) {
|
});
|
||||||
self.send_command(args[0], args[1], function (err, reply) {
|
this.send_command("EXEC", function (err, replies) {
|
||||||
if (err) {
|
replies.forEach(function (reply, reply_num) {
|
||||||
args[2](err);
|
if (typeof commands[reply_num][2] === "function") {
|
||||||
commands.splice(command_num, 1); // what if this runs before all commands are sent?
|
commands[reply_num][2](null, reply);
|
||||||
} else {
|
} else {
|
||||||
if (reply !== "QUEUED") {
|
if (exports.debug_mode) {
|
||||||
console.warn("Unexpected MULTI reply: " + reply + " instead of 'QUEUED'");
|
console.log("no callback for multi response " + reply_num + ", skipping.");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
this.send_command("EXEC", [], function (err, replies) {
|
});
|
||||||
replies.forEach(function (reply, reply_num) {
|
|
||||||
if (typeof commands[reply_num][2] === "function") {
|
|
||||||
commands[reply_num][2](null, reply)
|
|
||||||
} else {
|
|
||||||
if (exports.debug_mode) {
|
|
||||||
console.log("no callback for multi response " + reply_num + ", skipping.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
console.log("Caught exception in MULTI: " + err.stack);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.createClient = function (port_arg, host_arg, options) {
|
exports.createClient = function (port_arg, host_arg, options) {
|
||||||
@@ -481,7 +492,7 @@ exports.createClient = function (port_arg, host_arg, options) {
|
|||||||
host = host || default_host,
|
host = host || default_host,
|
||||||
red_client, net_client;
|
red_client, net_client;
|
||||||
|
|
||||||
net_client = net.createConnection(port, host)
|
net_client = net.createConnection(port, host);
|
||||||
|
|
||||||
red_client = new RedisClient(net_client);
|
red_client = new RedisClient(net_client);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user