Skip to content

Commit 8f9ad00

Browse files
author
Ruben Bridgewater
committed
Add the redis url to the options object and accept .createClient(null, host, options)
1 parent 30d2184 commit 8f9ad00

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## LICENSE - "MIT License"
1+
LICENSE - "MIT License"
22

33
Copyright (c) 2015 by NodeRedis
44

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ port and host are probably fine and you don't need to supply any arguments. `cre
180180
* `host`: *127.0.0.1*; The host to connect to
181181
* `port`: *6370*; The port to connect to
182182
* `path`: *null*; The unix socket string to connect to
183+
* `url`: *null*; The redis url to connect to
183184
* `parser`: *hiredis*; Which Redis protocol reply parser to use. If `hiredis` is not installed it will fallback to `javascript`.
184185
* `return_buffers`: *false*; If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings.
185186
* `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.

index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ var commands = require('./lib/commands');
1414
var connection_id = 0;
1515
var default_port = 6379;
1616
var default_host = '127.0.0.1';
17-
var debug = function(msg) {
18-
if (exports.debug_mode) {
19-
console.error(msg);
20-
}
21-
};
2217

2318
function noop () {}
2419
function clone (obj) { return JSON.parse(JSON.stringify(obj || {})); }
20+
function debug (msg) { if (exports.debug_mode) { console.error(msg); } }
2521

2622
exports.debug_mode = /\bredis\b/i.test(process.env.NODE_DEBUG);
2723

@@ -35,7 +31,7 @@ try {
3531

3632
parsers.push(require('./lib/parsers/javascript'));
3733

38-
function RedisClient(options) {
34+
function RedisClient (options) {
3935
// Copy the options so they are not mutated
4036
options = clone(options);
4137
events.EventEmitter.call(this);
@@ -1223,6 +1219,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12231219
var args;
12241220
if (len === 0) {
12251221
if (callback) {
1222+
// The execution order won't be obtained in this case
12261223
setImmediate(function () {
12271224
callback(null, []);
12281225
});
@@ -1257,15 +1254,13 @@ Multi.prototype.exec = Multi.prototype.EXEC = Multi.prototype.exec_batch = funct
12571254
};
12581255

12591256
var createClient = function (port_arg, host_arg, options) {
1260-
if (typeof port_arg === 'object' || port_arg === undefined) {
1261-
options = port_arg || options || {};
1262-
} else if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
1257+
if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
12631258
options = clone(options);
12641259
options.host = host_arg;
12651260
options.port = port_arg;
1266-
} else if (typeof port_arg === 'string') {
1267-
options = clone(host_arg || options);
1268-
var parsed = URL.parse(port_arg, true, true);
1261+
} else if (typeof port_arg === 'string' || port_arg && port_arg.url) {
1262+
options = clone(port_arg.url ? port_arg : host_arg || options);
1263+
var parsed = URL.parse(port_arg.url || port_arg, true, true);
12691264
if (parsed.hostname) {
12701265
if (parsed.auth) {
12711266
options.auth_pass = parsed.auth.split(':')[1];
@@ -1278,6 +1273,9 @@ var createClient = function (port_arg, host_arg, options) {
12781273
} else {
12791274
options.path = port_arg;
12801275
}
1276+
} else if (typeof port_arg === 'object' || port_arg === undefined) {
1277+
options = clone(port_arg || options);
1278+
options.host = options.host || host_arg;
12811279
}
12821280
if (!options) {
12831281
throw new Error('Unknown type of connection in createClient()');

test/connection.spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ describe("connection tests", function () {
263263
});
264264
});
265265

266+
it("connects correctly to the provided host with the port set to null", function (done) {
267+
client = redis.createClient(null, 'localhost');
268+
client.on("error", done);
269+
assert.strictEqual(client.address, 'localhost:6379');
270+
271+
client.once("ready", function () {
272+
client.set('foo', 'bar');
273+
client.get('foo', function(err, res) {
274+
assert.strictEqual(res, 'bar');
275+
done(err);
276+
});
277+
});
278+
});
279+
266280
it("connects correctly to localhost and no ready check", function (done) {
267281
client = redis.createClient(undefined, undefined, {
268282
no_ready_check: true
@@ -278,6 +292,22 @@ describe("connection tests", function () {
278292
});
279293
});
280294

295+
it("connects correctly to the provided host with the port set to undefined", function (done) {
296+
client = redis.createClient(undefined, 'localhost', {
297+
no_ready_check: true
298+
});
299+
client.on("error", done);
300+
assert.strictEqual(client.address, 'localhost:6379');
301+
302+
client.once("ready", function () {
303+
client.set('foo', 'bar');
304+
client.get('foo', function(err, res) {
305+
assert.strictEqual(res, 'bar');
306+
done(err);
307+
});
308+
});
309+
});
310+
281311
it("connects correctly even if the info command is not present on the redis server", function (done) {
282312
client = redis.createClient.apply(redis.createClient, args);
283313
client.info = function (cb) {
@@ -327,6 +357,28 @@ describe("connection tests", function () {
327357
});
328358
});
329359

360+
it('allows connecting with the redis url as first parameter and the options as second parameter', function (done) {
361+
client = redis.createClient('redis://127.0.0.1', {
362+
connect_timeout: 1000
363+
});
364+
assert.strictEqual(client.options.connect_timeout, 1000);
365+
client.on('ready', function () {
366+
done();
367+
});
368+
});
369+
370+
it('allows connecting with the redis url in the options object', function (done) {
371+
client = redis.createClient({
372+
url: 'redis://foo:porkchopsandwiches@' + config.HOST[ip]
373+
});
374+
assert.strictEqual(client.options.auth_pass, 'porkchopsandwiches');
375+
assert(!client.options.port);
376+
assert.strictEqual(client.options.host, config.HOST[ip]);
377+
client.on("ready", function () {
378+
return done();
379+
});
380+
});
381+
330382
it('allows connecting with the redis url and no auth and options as second parameter', function (done) {
331383
var options = {
332384
detect_buffers: false

0 commit comments

Comments
 (0)