Skip to content

Commit 5e8a87b

Browse files
author
Ruben Bridgewater
committed
Improve createClient function to detect faulty input and throw
1 parent 290bf1d commit 5e8a87b

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/createClient.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module.exports = function createClient (port_arg, host_arg, options) {
1111
if (typeof host_arg === 'string') {
1212
host = host_arg;
1313
} else {
14+
if (options && host_arg) {
15+
throw new Error('Unknown type of connection in createClient()');
16+
}
1417
options = options || host_arg;
1518
}
1619
options = utils.clone(options);
@@ -23,19 +26,22 @@ module.exports = function createClient (port_arg, host_arg, options) {
2326
var parsed = URL.parse(port_arg.url || port_arg, true, true);
2427

2528
// [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
26-
if (parsed.hostname || parsed.slashes) { // The host might be an empty string
29+
if (parsed.slashes) { // We require slashes
2730
if (parsed.auth) {
2831
options.password = parsed.auth.split(':')[1];
2932
}
30-
if (!/^([a-z]+:)?\/\//i.test(parsed.href)) {
31-
throw new Error('Connection string must use the "redis:" protocol or begin with slashes //');
33+
if (parsed.protocol && parsed.protocol !== 'redis:') {
34+
console.warn('node_redis: WARNING: You passed "' + parsed.protocol.substring(0, parsed.protocol.length - 1) + '" as protocol instead of the "redis" protocol!');
3235
}
3336
if (parsed.pathname && parsed.pathname !== '/') {
3437
options.db = parsed.pathname.substr(1);
3538
}
36-
options.host = parsed.hostname;
37-
options.port = parsed.port;
38-
39+
if (parsed.hostname) {
40+
options.host = parsed.hostname;
41+
}
42+
if (parsed.port) {
43+
options.port = parsed.port;
44+
}
3945
if (parsed.search !== '') {
4046
var elem;
4147
for (elem in parsed.query) { // jshint ignore: line
@@ -50,13 +56,19 @@ module.exports = function createClient (port_arg, host_arg, options) {
5056
options[elem] = parsed.query[elem];
5157
}
5258
}
59+
} else if (parsed.hostname) {
60+
throw new Error('The redis url must begin with slashes "//" or contain slashes after the redis protocol');
5361
} else {
5462
options.path = port_arg;
5563
}
5664

5765
} else if (typeof port_arg === 'object' || port_arg === undefined) {
5866
options = utils.clone(port_arg || options);
5967
options.host = options.host || host_arg;
68+
69+
if (port_arg && arguments.length !== 1) {
70+
throw new Error('To many arguments passed to createClient. Please only pass the options object');
71+
}
6072
}
6173

6274
if (!options) {

0 commit comments

Comments
 (0)