1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Improve createClient function to detect faulty input and throw

This commit is contained in:
Ruben Bridgewater
2016-03-01 16:43:46 +01:00
parent 290bf1d651
commit 5e8a87b4dc

View File

@@ -11,6 +11,9 @@ module.exports = function createClient (port_arg, host_arg, options) {
if (typeof host_arg === 'string') {
host = host_arg;
} else {
if (options && host_arg) {
throw new Error('Unknown type of connection in createClient()');
}
options = options || host_arg;
}
options = utils.clone(options);
@@ -23,19 +26,22 @@ module.exports = function createClient (port_arg, host_arg, options) {
var parsed = URL.parse(port_arg.url || port_arg, true, true);
// [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
if (parsed.hostname || parsed.slashes) { // The host might be an empty string
if (parsed.slashes) { // We require slashes
if (parsed.auth) {
options.password = parsed.auth.split(':')[1];
}
if (!/^([a-z]+:)?\/\//i.test(parsed.href)) {
throw new Error('Connection string must use the "redis:" protocol or begin with slashes //');
if (parsed.protocol && parsed.protocol !== 'redis:') {
console.warn('node_redis: WARNING: You passed "' + parsed.protocol.substring(0, parsed.protocol.length - 1) + '" as protocol instead of the "redis" protocol!');
}
if (parsed.pathname && parsed.pathname !== '/') {
options.db = parsed.pathname.substr(1);
}
options.host = parsed.hostname;
options.port = parsed.port;
if (parsed.hostname) {
options.host = parsed.hostname;
}
if (parsed.port) {
options.port = parsed.port;
}
if (parsed.search !== '') {
var elem;
for (elem in parsed.query) { // jshint ignore: line
@@ -50,6 +56,8 @@ module.exports = function createClient (port_arg, host_arg, options) {
options[elem] = parsed.query[elem];
}
}
} else if (parsed.hostname) {
throw new Error('The redis url must begin with slashes "//" or contain slashes after the redis protocol');
} else {
options.path = port_arg;
}
@@ -57,6 +65,10 @@ module.exports = function createClient (port_arg, host_arg, options) {
} else if (typeof port_arg === 'object' || port_arg === undefined) {
options = utils.clone(port_arg || options);
options.host = options.host || host_arg;
if (port_arg && arguments.length !== 1) {
throw new Error('To many arguments passed to createClient. Please only pass the options object');
}
}
if (!options) {