util.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const utils_1 = require("../utils");
  4. const net_1 = require("net");
  5. function getNodeKey(node) {
  6. node.port = node.port || 6379;
  7. node.host = node.host || "127.0.0.1";
  8. return node.host + ":" + node.port;
  9. }
  10. exports.getNodeKey = getNodeKey;
  11. function nodeKeyToRedisOptions(nodeKey) {
  12. const portIndex = nodeKey.lastIndexOf(":");
  13. if (portIndex === -1) {
  14. throw new Error(`Invalid node key ${nodeKey}`);
  15. }
  16. return {
  17. host: nodeKey.slice(0, portIndex),
  18. port: Number(nodeKey.slice(portIndex + 1))
  19. };
  20. }
  21. exports.nodeKeyToRedisOptions = nodeKeyToRedisOptions;
  22. function normalizeNodeOptions(nodes) {
  23. return nodes.map(node => {
  24. const options = {};
  25. if (typeof node === "object") {
  26. Object.assign(options, node);
  27. }
  28. else if (typeof node === "string") {
  29. Object.assign(options, utils_1.parseURL(node));
  30. }
  31. else if (typeof node === "number") {
  32. options.port = node;
  33. }
  34. else {
  35. throw new Error("Invalid argument " + node);
  36. }
  37. if (typeof options.port === "string") {
  38. options.port = parseInt(options.port, 10);
  39. }
  40. // Cluster mode only support db 0
  41. delete options.db;
  42. if (!options.port) {
  43. options.port = 6379;
  44. }
  45. if (!options.host) {
  46. options.host = "127.0.0.1";
  47. }
  48. return options;
  49. });
  50. }
  51. exports.normalizeNodeOptions = normalizeNodeOptions;
  52. function getUniqueHostnamesFromOptions(nodes) {
  53. const uniqueHostsMap = {};
  54. nodes.forEach(node => {
  55. uniqueHostsMap[node.host] = true;
  56. });
  57. return Object.keys(uniqueHostsMap).filter(host => !net_1.isIP(host));
  58. }
  59. exports.getUniqueHostnamesFromOptions = getUniqueHostnamesFromOptions;