CLUSTER_NODES.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformReply = exports.RedisClusterNodeLinkStates = exports.transformArguments = void 0;
  4. function transformArguments() {
  5. return ['CLUSTER', 'NODES'];
  6. }
  7. exports.transformArguments = transformArguments;
  8. var RedisClusterNodeLinkStates;
  9. (function (RedisClusterNodeLinkStates) {
  10. RedisClusterNodeLinkStates["CONNECTED"] = "connected";
  11. RedisClusterNodeLinkStates["DISCONNECTED"] = "disconnected";
  12. })(RedisClusterNodeLinkStates || (exports.RedisClusterNodeLinkStates = RedisClusterNodeLinkStates = {}));
  13. function transformReply(reply) {
  14. const lines = reply.split('\n');
  15. lines.pop(); // last line is empty
  16. const mastersMap = new Map(), replicasMap = new Map();
  17. for (const line of lines) {
  18. const [id, address, flags, masterId, pingSent, pongRecv, configEpoch, linkState, ...slots] = line.split(' '), node = {
  19. id,
  20. address,
  21. ...transformNodeAddress(address),
  22. flags: flags.split(','),
  23. pingSent: Number(pingSent),
  24. pongRecv: Number(pongRecv),
  25. configEpoch: Number(configEpoch),
  26. linkState: linkState
  27. };
  28. if (masterId === '-') {
  29. let replicas = replicasMap.get(id);
  30. if (!replicas) {
  31. replicas = [];
  32. replicasMap.set(id, replicas);
  33. }
  34. mastersMap.set(id, {
  35. ...node,
  36. slots: slots.map(slot => {
  37. // TODO: importing & exporting (https://redis.io/commands/cluster-nodes#special-slot-entries)
  38. const [fromString, toString] = slot.split('-', 2), from = Number(fromString);
  39. return {
  40. from,
  41. to: toString ? Number(toString) : from
  42. };
  43. }),
  44. replicas
  45. });
  46. }
  47. else {
  48. const replicas = replicasMap.get(masterId);
  49. if (!replicas) {
  50. replicasMap.set(masterId, [node]);
  51. }
  52. else {
  53. replicas.push(node);
  54. }
  55. }
  56. }
  57. return [...mastersMap.values()];
  58. }
  59. exports.transformReply = transformReply;
  60. function transformNodeAddress(address) {
  61. const indexOfColon = address.lastIndexOf(':'), indexOfAt = address.indexOf('@', indexOfColon), host = address.substring(0, indexOfColon);
  62. if (indexOfAt === -1) {
  63. return {
  64. host,
  65. port: Number(address.substring(indexOfColon + 1)),
  66. cport: null
  67. };
  68. }
  69. return {
  70. host: address.substring(0, indexOfColon),
  71. port: Number(address.substring(indexOfColon + 1, indexOfAt)),
  72. cport: Number(address.substring(indexOfAt + 1))
  73. };
  74. }