Skip to content

Commit b92a62d

Browse files
Merge pull request #658 from kuwabarahiroshi/features/add-url-support-to-createClient
added URL support to createClient
2 parents 9b02ae6 + a67d3ac commit b92a62d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

index.js

+13-2
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

+19
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,25 @@ tests.auth2 = function () {
21802180
});
21812181
};
21822182

2183+
// auth password specified by URL string.
2184+
tests.auth3 = function () {
2185+
var name = "AUTH3", client4, ready_count = 0;
2186+
2187+
client4 = redis.createClient('redis://redistogo:[email protected]:9006/');
2188+
2189+
// test auth, then kill the connection so it'll auto-reconnect and auto-re-auth
2190+
client4.on("ready", function () {
2191+
ready_count++;
2192+
if (ready_count === 1) {
2193+
client4.stream.destroy();
2194+
} else {
2195+
client4.quit(function (err, res) {
2196+
next(name);
2197+
});
2198+
}
2199+
});
2200+
};
2201+
21832202
tests.reconnectRetryMaxDelay = function() {
21842203
var time = new Date().getTime(),
21852204
name = 'reconnectRetryMaxDelay',

0 commit comments

Comments
 (0)