Skip to content

Commit a67d3ac

Browse files
added URL support to createClient
1 parent de66927 commit a67d3ac

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*global Buffer require exports console setTimeout */
22

33
var net = require("net"),
4+
URL = require("url"),
45
util = require("./lib/util"),
56
Queue = require("./lib/queue"),
67
to_array = require("./lib/to_array"),
@@ -1227,9 +1228,19 @@ exports.createClient = function(arg0, arg1, arg2){
12271228
return createClient_tcp(arg0, arg1, arg2);
12281229

12291230
} else if( typeof arg0 === 'string' ){
1231+
var parsed = URL.parse(arg0, true, true),
1232+
options = (arg1 || {});
12301233

1231-
// createClient( '/tmp/redis.sock', options)
1232-
return createClient_unix(arg0,arg1);
1234+
if (parsed.hostname) {
1235+
if (parsed.auth) {
1236+
options.auth_pass = parsed.auth.split(':')[1];
1237+
}
1238+
// createClient(3000, host, options)
1239+
return createClient_tcp((parsed.port || default_port), parsed.hostname, options);
1240+
} else {
1241+
// createClient( '/tmp/redis.sock', options)
1242+
return createClient_unix(arg0,options);
1243+
}
12331244

12341245
} else if( arg0 !== null && typeof arg0 === 'object' ){
12351246

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,25 @@ tests.auth2 = function () {
21692169
});
21702170
};
21712171

2172+
// auth password specified by URL string.
2173+
tests.auth3 = function () {
2174+
var name = "AUTH3", client4, ready_count = 0;
2175+
2176+
client4 = redis.createClient('redis://redistogo:664b1b6aaf134e1ec281945a8de702a9@filefish.redistogo.com:9006/');
2177+
2178+
// test auth, then kill the connection so it'll auto-reconnect and auto-re-auth
2179+
client4.on("ready", function () {
2180+
ready_count++;
2181+
if (ready_count === 1) {
2182+
client4.stream.destroy();
2183+
} else {
2184+
client4.quit(function (err, res) {
2185+
next(name);
2186+
});
2187+
}
2188+
});
2189+
};
2190+
21722191
tests.reconnectRetryMaxDelay = function() {
21732192
var time = new Date().getTime(),
21742193
name = 'reconnectRetryMaxDelay',

0 commit comments

Comments
 (0)