You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.3 KiB

4 years ago
  1. 'use strict';
  2. var utils = require('./utils');
  3. var URL = require('url');
  4. module.exports = function createClient (port_arg, host_arg, options) {
  5. if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
  6. var host;
  7. if (typeof host_arg === 'string') {
  8. host = host_arg;
  9. } else {
  10. if (options && host_arg) {
  11. throw new TypeError('Unknown type of connection in createClient()');
  12. }
  13. options = options || host_arg;
  14. }
  15. options = utils.clone(options);
  16. options.host = host || options.host;
  17. options.port = port_arg;
  18. } else if (typeof port_arg === 'string' || port_arg && port_arg.url) {
  19. options = utils.clone(port_arg.url ? port_arg : host_arg || options);
  20. var url = port_arg.url || port_arg;
  21. var parsed = URL.parse(url, true, true);
  22. // [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
  23. if (parsed.slashes) { // We require slashes
  24. if (parsed.auth) {
  25. options.password = parsed.auth.slice(parsed.auth.indexOf(':') + 1);
  26. }
  27. if (parsed.protocol) {
  28. if (parsed.protocol === 'rediss:') {
  29. options.tls = options.tls || {};
  30. } else if (parsed.protocol !== 'redis:') {
  31. console.warn('node_redis: WARNING: You passed "' + parsed.protocol.substring(0, parsed.protocol.length - 1) + '" as protocol instead of the "redis" protocol!');
  32. }
  33. }
  34. if (parsed.pathname && parsed.pathname !== '/') {
  35. options.db = parsed.pathname.substr(1);
  36. }
  37. if (parsed.hostname) {
  38. options.host = parsed.hostname;
  39. }
  40. if (parsed.port) {
  41. options.port = parsed.port;
  42. }
  43. if (parsed.search !== '') {
  44. var elem;
  45. for (elem in parsed.query) {
  46. // If options are passed twice, only the parsed options will be used
  47. if (elem in options) {
  48. if (options[elem] === parsed.query[elem]) {
  49. console.warn('node_redis: WARNING: You passed the ' + elem + ' option twice!');
  50. } else {
  51. throw new RangeError('The ' + elem + ' option is added twice and does not match');
  52. }
  53. }
  54. options[elem] = parsed.query[elem];
  55. }
  56. }
  57. } else if (parsed.hostname) {
  58. throw new RangeError('The redis url must begin with slashes "//" or contain slashes after the redis protocol');
  59. } else {
  60. options.path = url;
  61. }
  62. } else if (typeof port_arg === 'object' || port_arg === undefined) {
  63. options = utils.clone(port_arg || options);
  64. options.host = options.host || host_arg;
  65. if (port_arg && arguments.length !== 1) {
  66. throw new TypeError('Too many arguments passed to createClient. Please only pass the options object');
  67. }
  68. }
  69. if (!options) {
  70. throw new TypeError('Unknown type of connection in createClient()');
  71. }
  72. return options;
  73. };