You've already forked node-redis
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:
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
## LICENSE - "MIT License"
|
LICENSE - "MIT License"
|
||||||
|
|
||||||
Copyright (c) 2015 by NodeRedis
|
Copyright (c) 2015 by NodeRedis
|
||||||
|
|
||||||
|
@@ -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
|
* `host`: *127.0.0.1*; The host to connect to
|
||||||
* `port`: *6370*; The port to connect to
|
* `port`: *6370*; The port to connect to
|
||||||
* `path`: *null*; The unix socket string 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`.
|
* `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.
|
* `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.
|
* `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.
|
||||||
|
22
index.js
22
index.js
@@ -14,14 +14,10 @@ var commands = require('./lib/commands');
|
|||||||
var connection_id = 0;
|
var connection_id = 0;
|
||||||
var default_port = 6379;
|
var default_port = 6379;
|
||||||
var default_host = '127.0.0.1';
|
var default_host = '127.0.0.1';
|
||||||
var debug = function(msg) {
|
|
||||||
if (exports.debug_mode) {
|
|
||||||
console.error(msg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
function noop () {}
|
function noop () {}
|
||||||
function clone (obj) { return JSON.parse(JSON.stringify(obj || {})); }
|
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);
|
exports.debug_mode = /\bredis\b/i.test(process.env.NODE_DEBUG);
|
||||||
|
|
||||||
@@ -35,7 +31,7 @@ try {
|
|||||||
|
|
||||||
parsers.push(require('./lib/parsers/javascript'));
|
parsers.push(require('./lib/parsers/javascript'));
|
||||||
|
|
||||||
function RedisClient(options) {
|
function RedisClient (options) {
|
||||||
// Copy the options so they are not mutated
|
// Copy the options so they are not mutated
|
||||||
options = clone(options);
|
options = clone(options);
|
||||||
events.EventEmitter.call(this);
|
events.EventEmitter.call(this);
|
||||||
@@ -1223,6 +1219,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
|
|||||||
var args;
|
var args;
|
||||||
if (len === 0) {
|
if (len === 0) {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
|
// The execution order won't be obtained in this case
|
||||||
setImmediate(function () {
|
setImmediate(function () {
|
||||||
callback(null, []);
|
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) {
|
var createClient = function (port_arg, host_arg, options) {
|
||||||
if (typeof port_arg === 'object' || port_arg === undefined) {
|
if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
|
||||||
options = port_arg || options || {};
|
|
||||||
} else if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
|
|
||||||
options = clone(options);
|
options = clone(options);
|
||||||
options.host = host_arg;
|
options.host = host_arg;
|
||||||
options.port = port_arg;
|
options.port = port_arg;
|
||||||
} else if (typeof port_arg === 'string') {
|
} else if (typeof port_arg === 'string' || port_arg && port_arg.url) {
|
||||||
options = clone(host_arg || options);
|
options = clone(port_arg.url ? port_arg : host_arg || options);
|
||||||
var parsed = URL.parse(port_arg, true, true);
|
var parsed = URL.parse(port_arg.url || port_arg, true, true);
|
||||||
if (parsed.hostname) {
|
if (parsed.hostname) {
|
||||||
if (parsed.auth) {
|
if (parsed.auth) {
|
||||||
options.auth_pass = parsed.auth.split(':')[1];
|
options.auth_pass = parsed.auth.split(':')[1];
|
||||||
@@ -1278,6 +1273,9 @@ var createClient = function (port_arg, host_arg, options) {
|
|||||||
} else {
|
} else {
|
||||||
options.path = port_arg;
|
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) {
|
if (!options) {
|
||||||
throw new Error('Unknown type of connection in createClient()');
|
throw new Error('Unknown type of connection in createClient()');
|
||||||
|
@@ -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) {
|
it("connects correctly to localhost and no ready check", function (done) {
|
||||||
client = redis.createClient(undefined, undefined, {
|
client = redis.createClient(undefined, undefined, {
|
||||||
no_ready_check: true
|
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) {
|
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 = redis.createClient.apply(redis.createClient, args);
|
||||||
client.info = function (cb) {
|
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) {
|
it('allows connecting with the redis url and no auth and options as second parameter', function (done) {
|
||||||
var options = {
|
var options = {
|
||||||
detect_buffers: false
|
detect_buffers: false
|
||||||
|
Reference in New Issue
Block a user