Commander.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const commands_1 = require("@ioredis/commands");
  4. const autoPipelining_1 = require("../autoPipelining");
  5. const Command_1 = require("../Command");
  6. const Script_1 = require("../Script");
  7. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  8. class Commander {
  9. constructor() {
  10. this.options = {};
  11. /**
  12. * @ignore
  13. */
  14. this.scriptsSet = {};
  15. /**
  16. * @ignore
  17. */
  18. this.addedBuiltinSet = new Set();
  19. }
  20. /**
  21. * Return supported builtin commands
  22. */
  23. getBuiltinCommands() {
  24. return commands.slice(0);
  25. }
  26. /**
  27. * Create a builtin command
  28. */
  29. createBuiltinCommand(commandName) {
  30. return {
  31. string: generateFunction(null, commandName, "utf8"),
  32. buffer: generateFunction(null, commandName, null),
  33. };
  34. }
  35. /**
  36. * Create add builtin command
  37. */
  38. addBuiltinCommand(commandName) {
  39. this.addedBuiltinSet.add(commandName);
  40. this[commandName] = generateFunction(commandName, commandName, "utf8");
  41. this[commandName + "Buffer"] = generateFunction(commandName + "Buffer", commandName, null);
  42. }
  43. /**
  44. * Define a custom command using lua script
  45. */
  46. defineCommand(name, definition) {
  47. const script = new Script_1.default(definition.lua, definition.numberOfKeys, this.options.keyPrefix, definition.readOnly);
  48. this.scriptsSet[name] = script;
  49. this[name] = generateScriptingFunction(name, name, script, "utf8");
  50. this[name + "Buffer"] = generateScriptingFunction(name + "Buffer", name, script, null);
  51. }
  52. /**
  53. * @ignore
  54. */
  55. sendCommand(command, stream, node) {
  56. throw new Error('"sendCommand" is not implemented');
  57. }
  58. }
  59. const commands = commands_1.list.filter((command) => command !== "monitor");
  60. commands.push("sentinel");
  61. commands.forEach(function (commandName) {
  62. Commander.prototype[commandName] = generateFunction(commandName, commandName, "utf8");
  63. Commander.prototype[commandName + "Buffer"] = generateFunction(commandName + "Buffer", commandName, null);
  64. });
  65. Commander.prototype.call = generateFunction("call", "utf8");
  66. Commander.prototype.callBuffer = generateFunction("callBuffer", null);
  67. // @ts-expect-error
  68. Commander.prototype.send_command = Commander.prototype.call;
  69. function generateFunction(functionName, _commandName, _encoding) {
  70. if (typeof _encoding === "undefined") {
  71. _encoding = _commandName;
  72. _commandName = null;
  73. }
  74. return function (...args) {
  75. const commandName = (_commandName || args.shift());
  76. let callback = args[args.length - 1];
  77. if (typeof callback === "function") {
  78. args.pop();
  79. }
  80. else {
  81. callback = undefined;
  82. }
  83. const options = {
  84. errorStack: this.options.showFriendlyErrorStack ? new Error() : undefined,
  85. keyPrefix: this.options.keyPrefix,
  86. replyEncoding: _encoding,
  87. };
  88. // No auto pipeline, use regular command sending
  89. if (!(0, autoPipelining_1.shouldUseAutoPipelining)(this, functionName, commandName)) {
  90. return this.sendCommand(
  91. // @ts-expect-error
  92. new Command_1.default(commandName, args, options, callback));
  93. }
  94. // Create a new pipeline and make sure it's scheduled
  95. return (0, autoPipelining_1.executeWithAutoPipelining)(this, functionName, commandName,
  96. // @ts-expect-error
  97. args, callback);
  98. };
  99. }
  100. function generateScriptingFunction(functionName, commandName, script, encoding) {
  101. return function (...args) {
  102. const callback = typeof args[args.length - 1] === "function" ? args.pop() : undefined;
  103. const options = {
  104. replyEncoding: encoding,
  105. };
  106. if (this.options.showFriendlyErrorStack) {
  107. options.errorStack = new Error();
  108. }
  109. // No auto pipeline, use regular command sending
  110. if (!(0, autoPipelining_1.shouldUseAutoPipelining)(this, functionName, commandName)) {
  111. return script.execute(this, args, options, callback);
  112. }
  113. // Create a new pipeline and make sure it's scheduled
  114. return (0, autoPipelining_1.executeWithAutoPipelining)(this, functionName, commandName, args, callback);
  115. };
  116. }
  117. exports.default = Commander;