Skip to content

Commit a4285c1

Browse files
author
Ruben Bridgewater
committed
Parse redis url just like IANA
1 parent 58ddd51 commit a4285c1

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ function RedisClient (options) {
9494
this.monitoring = false;
9595
this.closing = false;
9696
this.server_info = {};
97-
this.auth_pass = options.auth_pass;
98-
this.selected_db = null; // Save the selected db here, used when reconnecting
97+
this.auth_pass = options.auth_pass || options.password;
98+
this.selected_db = options.db; // Save the selected db here, used when reconnecting
9999
this.old_state = null;
100100
this.send_anyway = false;
101101
this.pipeline = 0;
@@ -1265,12 +1265,23 @@ var createClient = function (port_arg, host_arg, options) {
12651265
} else if (typeof port_arg === 'string' || port_arg && port_arg.url) {
12661266
options = clone(port_arg.url ? port_arg : host_arg || options);
12671267
var parsed = URL.parse(port_arg.url || port_arg, true, true);
1268+
// [redis:]//[user][:password@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
12681269
if (parsed.hostname) {
12691270
if (parsed.auth) {
1270-
options.auth_pass = parsed.auth.split(':')[1];
1271+
options.password = parsed.auth.split(':')[1];
12711272
}
1272-
if (parsed.protocol !== 'redis:') {
1273-
throw new Error('Connection string must use the "redis:" protocol');
1273+
if (!/^([a-z]+:)?\/\//i.test(parsed.href)) {
1274+
throw new Error('Connection string must use the "redis:" protocol or begin with slashes //');
1275+
}
1276+
if (parsed.pathname && parsed.pathname !== '/') {
1277+
options.db = parsed.pathname.substr(1);
1278+
}
1279+
if (parsed.search !== '') {
1280+
var elem;
1281+
for (elem in parsed.query) { // jshint ignore: line
1282+
// If options are passed twice, only the parsed options will be used
1283+
options[elem] = parsed.query[elem];
1284+
}
12741285
}
12751286
options.host = parsed.hostname;
12761287
options.port = parsed.port;

0 commit comments

Comments
 (0)