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

Add the redis url to the options object and accept .createClient(null, host, options)

This commit is contained in:
Ruben Bridgewater
2015-11-22 22:48:13 +01:00
parent 30d2184dbb
commit 8f9ad00de2
4 changed files with 64 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
## LICENSE - "MIT License"
LICENSE - "MIT License"
Copyright (c) 2015 by NodeRedis

View File

@@ -180,6 +180,7 @@ port and host are probably fine and you don't need to supply any arguments. `cre
* `host`: *127.0.0.1*; The host to connect to
* `port`: *6370*; The port to connect to
* `path`: *null*; The unix socket string to connect to
* `url`: *null*; The redis url to connect to
* `parser`: *hiredis*; Which Redis protocol reply parser to use. If `hiredis` is not installed it will fallback to `javascript`.
* `return_buffers`: *false*; If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings.
* `detect_buffers`: *false*; If set to `true`, then replies will be sent to callbacks as Buffers. Please be aware that this can't work properly with the pubsub mode. A subscriber has to either always return strings or buffers.

View File

@@ -14,14 +14,10 @@ var commands = require('./lib/commands');
var connection_id = 0;
var default_port = 6379;
var default_host = '127.0.0.1';
var debug = function(msg) {
if (exports.debug_mode) {
console.error(msg);
}
};
function noop () {}
function clone (obj) { return JSON.parse(JSON.stringify(obj || {})); }
function debug (msg) { if (exports.debug_mode) { console.error(msg); } }
exports.debug_mode = /\bredis\b/i.test(process.env.NODE_DEBUG);
@@ -1223,6 +1219,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
var args;
if (len === 0) {
if (callback) {
// The execution order won't be obtained in this case
setImmediate(function () {
callback(null, []);
});
@@ -1257,15 +1254,13 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
};
var createClient = function (port_arg, host_arg, options) {
if (typeof port_arg === 'object' || port_arg === undefined) {
options = port_arg || options || {};
} else if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
options = clone(options);
options.host = host_arg;
options.port = port_arg;
} else if (typeof port_arg === 'string') {
options = clone(host_arg || options);
var parsed = URL.parse(port_arg, true, true);
} else if (typeof port_arg === 'string' || port_arg && port_arg.url) {
options = clone(port_arg.url ? port_arg : host_arg || options);
var parsed = URL.parse(port_arg.url || port_arg, true, true);
if (parsed.hostname) {
if (parsed.auth) {
options.auth_pass = parsed.auth.split(':')[1];
@@ -1278,6 +1273,9 @@ var createClient = function (port_arg, host_arg, options) {
} else {
options.path = port_arg;
}
} else if (typeof port_arg === 'object' || port_arg === undefined) {
options = clone(port_arg || options);
options.host = options.host || host_arg;
}
if (!options) {
throw new Error('Unknown type of connection in createClient()');

View File

@@ -263,6 +263,20 @@ describe("connection tests", function () {
});
});
it("connects correctly to the provided host with the port set to null", function (done) {
client = redis.createClient(null, 'localhost');
client.on("error", done);
assert.strictEqual(client.address, 'localhost:6379');
client.once("ready", function () {
client.set('foo', 'bar');
client.get('foo', function(err, res) {
assert.strictEqual(res, 'bar');
done(err);
});
});
});
it("connects correctly to localhost and no ready check", function (done) {
client = redis.createClient(undefined, undefined, {
no_ready_check: true
@@ -278,6 +292,22 @@ describe("connection tests", function () {
});
});
it("connects correctly to the provided host with the port set to undefined", function (done) {
client = redis.createClient(undefined, 'localhost', {
no_ready_check: true
});
client.on("error", done);
assert.strictEqual(client.address, 'localhost:6379');
client.once("ready", function () {
client.set('foo', 'bar');
client.get('foo', function(err, res) {
assert.strictEqual(res, 'bar');
done(err);
});
});
});
it("connects correctly even if the info command is not present on the redis server", function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.info = function (cb) {
@@ -327,6 +357,28 @@ describe("connection tests", function () {
});
});
it('allows connecting with the redis url as first parameter and the options as second parameter', function (done) {
client = redis.createClient('redis://127.0.0.1', {
connect_timeout: 1000
});
assert.strictEqual(client.options.connect_timeout, 1000);
client.on('ready', function () {
done();
});
});
it('allows connecting with the redis url in the options object', function (done) {
client = redis.createClient({
url: 'redis://foo:porkchopsandwiches@' + config.HOST[ip]
});
assert.strictEqual(client.options.auth_pass, 'porkchopsandwiches');
assert(!client.options.port);
assert.strictEqual(client.options.host, config.HOST[ip]);
client.on("ready", function () {
return done();
});
});
it('allows connecting with the redis url and no auth and options as second parameter', function (done) {
var options = {
detect_buffers: false