index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getKeyIndexes = exports.hasFlag = exports.exists = exports.list = void 0;
  7. const commands_json_1 = __importDefault(require("./commands.json"));
  8. /**
  9. * Redis command list
  10. *
  11. * All commands are lowercased.
  12. */
  13. exports.list = Object.keys(commands_json_1.default);
  14. const flags = {};
  15. exports.list.forEach((commandName) => {
  16. flags[commandName] = commands_json_1.default[commandName].flags.reduce(function (flags, flag) {
  17. flags[flag] = true;
  18. return flags;
  19. }, {});
  20. });
  21. /**
  22. * Check if the command exists
  23. */
  24. function exists(commandName) {
  25. return Boolean(commands_json_1.default[commandName]);
  26. }
  27. exports.exists = exists;
  28. /**
  29. * Check if the command has the flag
  30. *
  31. * Some of possible flags: readonly, noscript, loading
  32. */
  33. function hasFlag(commandName, flag) {
  34. if (!flags[commandName]) {
  35. throw new Error("Unknown command " + commandName);
  36. }
  37. return Boolean(flags[commandName][flag]);
  38. }
  39. exports.hasFlag = hasFlag;
  40. /**
  41. * Get indexes of keys in the command arguments
  42. *
  43. * @example
  44. * ```javascript
  45. * getKeyIndexes('set', ['key', 'value']) // [0]
  46. * getKeyIndexes('mget', ['key1', 'key2']) // [0, 1]
  47. * ```
  48. */
  49. function getKeyIndexes(commandName, args, options) {
  50. const command = commands_json_1.default[commandName];
  51. if (!command) {
  52. throw new Error("Unknown command " + commandName);
  53. }
  54. if (!Array.isArray(args)) {
  55. throw new Error("Expect args to be an array");
  56. }
  57. const keys = [];
  58. const parseExternalKey = Boolean(options && options.parseExternalKey);
  59. switch (commandName) {
  60. case "zunionstore":
  61. case "zinterstore":
  62. keys.push(0);
  63. // fall through
  64. case "eval":
  65. case "evalsha":
  66. case "eval_ro":
  67. case "evalsha_ro":
  68. case "fcall":
  69. case "fcall_ro":
  70. const keyStop = Number(args[1]) + 2;
  71. for (let i = 2; i < keyStop; i++) {
  72. keys.push(i);
  73. }
  74. break;
  75. case "sort":
  76. keys.push(0);
  77. for (let i = 1; i < args.length - 1; i++) {
  78. let arg = args[i];
  79. if (typeof arg !== "string") {
  80. continue;
  81. }
  82. const directive = arg.toUpperCase();
  83. if (directive === "GET") {
  84. i += 1;
  85. arg = args[i];
  86. if (arg !== "#") {
  87. if (parseExternalKey) {
  88. keys.push([i, getExternalKeyNameLength(arg)]);
  89. }
  90. else {
  91. keys.push(i);
  92. }
  93. }
  94. }
  95. else if (directive === "BY") {
  96. i += 1;
  97. if (parseExternalKey) {
  98. keys.push([i, getExternalKeyNameLength(args[i])]);
  99. }
  100. else {
  101. keys.push(i);
  102. }
  103. }
  104. else if (directive === "STORE") {
  105. i += 1;
  106. keys.push(i);
  107. }
  108. }
  109. break;
  110. case "migrate":
  111. if (args[2] === "") {
  112. for (let i = 5; i < args.length - 1; i++) {
  113. const arg = args[i];
  114. if (typeof arg === "string" && arg.toUpperCase() === "KEYS") {
  115. for (let j = i + 1; j < args.length; j++) {
  116. keys.push(j);
  117. }
  118. break;
  119. }
  120. }
  121. }
  122. else {
  123. keys.push(2);
  124. }
  125. break;
  126. case "xreadgroup":
  127. case "xread":
  128. // Keys are 1st half of the args after STREAMS argument.
  129. for (let i = commandName === "xread" ? 0 : 3; i < args.length - 1; i++) {
  130. if (String(args[i]).toUpperCase() === "STREAMS") {
  131. for (let j = i + 1; j <= i + (args.length - 1 - i) / 2; j++) {
  132. keys.push(j);
  133. }
  134. break;
  135. }
  136. }
  137. break;
  138. default:
  139. // Step has to be at least one in this case, otherwise the command does
  140. // not contain a key.
  141. if (command.step > 0) {
  142. const keyStart = command.keyStart - 1;
  143. const keyStop = command.keyStop > 0
  144. ? command.keyStop
  145. : args.length + command.keyStop + 1;
  146. for (let i = keyStart; i < keyStop; i += command.step) {
  147. keys.push(i);
  148. }
  149. }
  150. break;
  151. }
  152. return keys;
  153. }
  154. exports.getKeyIndexes = getKeyIndexes;
  155. function getExternalKeyNameLength(key) {
  156. if (typeof key !== "string") {
  157. key = String(key);
  158. }
  159. const hashPos = key.indexOf("->");
  160. return hashPos === -1 ? key.length : hashPos;
  161. }