1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Merge remote-tracking branch 'node_redis/master'

This commit is contained in:
David Vas
2015-08-18 13:08:12 +02:00
committed by anuka
103 changed files with 5229 additions and 2476 deletions

View File

@@ -1,6 +1,9 @@
'use strict';
/*global Buffer require exports console setTimeout */
var net = require("net"),
URL = require("url"),
util = require("./lib/util"),
Queue = require("./lib/queue"),
to_array = require("./lib/to_array"),
@@ -14,10 +17,10 @@ var net = require("net"),
// can set this to true to enable for all connections
exports.debug_mode = false;
var arraySlice = Array.prototype.slice
var arraySlice = Array.prototype.slice;
function trace() {
if (!exports.debug_mode) return;
console.log.apply(null, arraySlice.call(arguments))
console.log.apply(null, arraySlice.call(arguments));
}
// hiredis might not be installed
@@ -138,7 +141,7 @@ RedisClient.prototype.unref = function () {
trace("Not connected yet, will unref later");
this.once("connect", function () {
this.unref();
})
});
}
};
@@ -216,7 +219,7 @@ RedisClient.prototype.do_auth = function () {
}, 2000); // TODO - magic number alert
return;
} else if (err.toString().match("no password is set")) {
console.log("Warning: Redis server does not require a password, but a password was supplied.")
console.log("Warning: Redis server does not require a password, but a password was supplied.");
err = null;
res = "OK";
} else {
@@ -609,7 +612,7 @@ function try_callback(callback, reply) {
function reply_to_object(reply) {
var obj = {}, j, jl, key, val;
if (reply.length === 0) {
if (reply.length === 0 || !Array.isArray(reply)) {
return null;
}
@@ -647,7 +650,7 @@ RedisClient.prototype.return_reply = function (reply) {
// If the "reply" here is actually a message received asynchronously due to a
// pubsub subscription, don't pop the command queue as we'll only be consuming
// the head command prematurely.
if (Array.isArray(reply) && reply.length > 0 && reply[0]) {
if (this.pub_sub_mode && Array.isArray(reply) && reply.length > 0 && reply[0]) {
type = reply[0].toString();
}
@@ -671,7 +674,7 @@ RedisClient.prototype.return_reply = function (reply) {
if (command_obj && !command_obj.sub_command) {
if (typeof command_obj.callback === "function") {
if (this.options.detect_buffers && command_obj.buffer_args === false) {
if (this.options.detect_buffers && command_obj.buffer_args === false && 'exec' !== command_obj.command.toLowerCase()) {
// If detect_buffers option was specified, then the reply from the parser will be Buffers.
// If this command did not use Buffer arguments, then convert the reply to Strings here.
reply = reply_to_strings(reply);
@@ -1105,15 +1108,24 @@ Multi.prototype.HMSET = Multi.prototype.hmset;
Multi.prototype.exec = function (callback) {
var self = this;
var errors = [];
var wants_buffers = [];
// drain queue, callback will catch "QUEUED" or error
// TODO - get rid of all of these anonymous functions which are elegant but slow
this.queue.forEach(function (args, index) {
var command = args[0], obj;
var command = args[0], obj, i, il, buffer_args;
if (typeof args[args.length - 1] === "function") {
args = args.slice(1, -1);
} else {
args = args.slice(1);
}
// Keep track of who wants buffer responses:
buffer_args = false;
for (i = 0, il = args.length; i < il; i += 1) {
if (Buffer.isBuffer(args[i])) {
buffer_args = true;
}
}
wants_buffers.push(buffer_args);
if (args.length === 1 && Array.isArray(args[0])) {
args = args[0];
}
@@ -1148,12 +1160,18 @@ Multi.prototype.exec = function (callback) {
}
}
var i, il, reply, args;
var i, il, reply, to_buffer, args;
if (replies) {
for (i = 1, il = self.queue.length; i < il; i += 1) {
reply = replies[i - 1];
args = self.queue[i];
to_buffer = wants_buffers[i];
// If we asked for strings, even in detect_buffers mode, then return strings:
if (self._client.options.detect_buffers && to_buffer === false) {
replies[i - 1] = reply = reply_to_strings(reply);
}
// TODO - confusing and error-prone that hgetall is special cased in two places
if (reply && args[0].toLowerCase() === "hgetall") {
@@ -1212,40 +1230,29 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () {
});
};
exports.createClient = function(arg0, arg1, arg2){
if( arguments.length === 0 ){
// createClient()
return createClient_tcp(default_port, default_host, {});
} else if( typeof arg0 === 'number' ||
typeof arg0 === 'string' && arg0.match(/^\d+$/) ){
// createClient( 3000, host, options)
// createClient('3000', host, options)
return createClient_tcp(arg0, arg1, arg2);
} else if( typeof arg0 === 'string' ){
// createClient( '/tmp/redis.sock', options)
return createClient_unix(arg0,arg1);
} else if( arg0 !== null && typeof arg0 === 'object' ){
// createClient(options)
return createClient_tcp(default_port, default_host, arg0 );
} else if( arg0 === null && arg1 === null ){
// for backward compatibility
// createClient(null,null,options)
return createClient_tcp(default_port, default_host, arg2);
} else {
throw new Error('unknown type of connection in createClient()');
exports.createClient = function(arg0, arg1, options) {
if (typeof arg0 === 'object' || arg0 === undefined) {
options = arg0 || options;
return createClient_tcp(default_port, default_host, options);
}
}
if (typeof arg0 === 'number' || typeof arg0 === 'string' && arg0.match(/^\d+$/)){
return createClient_tcp(arg0, arg1, options);
}
if (typeof arg0 === 'string') {
options = arg1 || {};
var parsed = URL.parse(arg0, true, true);
if (parsed.hostname) {
if (parsed.auth) {
options.auth_pass = parsed.auth.split(':')[1];
}
return createClient_tcp((parsed.port || default_port), parsed.hostname, options);
}
return createClient_unix(arg0, options);
}
throw new Error('unknown type of connection in createClient()');
};
var createClient_unix = function(path, options){
var cnxOptions = {
@@ -1258,7 +1265,7 @@ var createClient_unix = function(path, options){
redis_client.address = path;
return redis_client;
}
};
var createClient_tcp = function (port_arg, host_arg, options) {
var cnxOptions = {