commander.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const lodash_1 = require("./utils/lodash");
  4. const command_1 = require("./command");
  5. const script_1 = require("./script");
  6. const PromiseContainer = require("./promiseContainer");
  7. const standard_as_callback_1 = require("standard-as-callback");
  8. var DROP_BUFFER_SUPPORT_ERROR = "*Buffer methods are not available " +
  9. 'because "dropBufferSupport" option is enabled.' +
  10. "Refer to https://github.com/luin/ioredis/wiki/Improve-Performance for more details.";
  11. /**
  12. * Commander
  13. *
  14. * This is the base class of Redis, Redis.Cluster and Pipeline
  15. *
  16. * @param {boolean} [options.showFriendlyErrorStack=false] - Whether to show a friendly error stack.
  17. * Will decrease the performance significantly.
  18. * @constructor
  19. */
  20. function Commander() {
  21. this.options = lodash_1.defaults({}, this.options || {}, {
  22. showFriendlyErrorStack: false
  23. });
  24. this.scriptsSet = {};
  25. }
  26. exports.default = Commander;
  27. var commands = require("redis-commands").list.filter(function (command) {
  28. return command !== "monitor";
  29. });
  30. commands.push("sentinel");
  31. /**
  32. * Return supported builtin commands
  33. *
  34. * @return {string[]} command list
  35. * @public
  36. */
  37. Commander.prototype.getBuiltinCommands = function () {
  38. return commands.slice(0);
  39. };
  40. /**
  41. * Create a builtin command
  42. *
  43. * @param {string} commandName - command name
  44. * @return {object} functions
  45. * @public
  46. */
  47. Commander.prototype.createBuiltinCommand = function (commandName) {
  48. return {
  49. string: generateFunction(commandName, "utf8"),
  50. buffer: generateFunction(commandName, null)
  51. };
  52. };
  53. commands.forEach(function (commandName) {
  54. Commander.prototype[commandName] = generateFunction(commandName, "utf8");
  55. Commander.prototype[commandName + "Buffer"] = generateFunction(commandName, null);
  56. });
  57. Commander.prototype.call = generateFunction("utf8");
  58. Commander.prototype.callBuffer = generateFunction(null);
  59. // eslint-disable-next-line @typescript-eslint/camelcase
  60. Commander.prototype.send_command = Commander.prototype.call;
  61. /**
  62. * Define a custom command using lua script
  63. *
  64. * @param {string} name - the command name
  65. * @param {object} definition
  66. * @param {string} definition.lua - the lua code
  67. * @param {number} [definition.numberOfKeys=null] - the number of keys.
  68. * If omit, you have to pass the number of keys as the first argument every time you invoke the command
  69. */
  70. Commander.prototype.defineCommand = function (name, definition) {
  71. var script = new script_1.default(definition.lua, definition.numberOfKeys, this.options.keyPrefix);
  72. this.scriptsSet[name] = script;
  73. this[name] = generateScriptingFunction(script, "utf8");
  74. this[name + "Buffer"] = generateScriptingFunction(script, null);
  75. };
  76. /**
  77. * Send a command
  78. *
  79. * @abstract
  80. * @public
  81. */
  82. Commander.prototype.sendCommand = function () { };
  83. function generateFunction(_commandName, _encoding) {
  84. if (typeof _encoding === "undefined") {
  85. _encoding = _commandName;
  86. _commandName = null;
  87. }
  88. return function () {
  89. var firstArgIndex = 0;
  90. var commandName = _commandName;
  91. if (commandName === null) {
  92. commandName = arguments[0];
  93. firstArgIndex = 1;
  94. }
  95. var length = arguments.length;
  96. var lastArgIndex = length - 1;
  97. var callback = arguments[lastArgIndex];
  98. if (typeof callback !== "function") {
  99. callback = undefined;
  100. }
  101. else {
  102. length = lastArgIndex;
  103. }
  104. var args = new Array(length - firstArgIndex);
  105. for (var i = firstArgIndex; i < length; ++i) {
  106. args[i - firstArgIndex] = arguments[i];
  107. }
  108. var options;
  109. if (this.options.dropBufferSupport) {
  110. if (!_encoding) {
  111. return standard_as_callback_1.default(PromiseContainer.get().reject(new Error(DROP_BUFFER_SUPPORT_ERROR)), callback);
  112. }
  113. options = { replyEncoding: null };
  114. }
  115. else {
  116. options = { replyEncoding: _encoding };
  117. }
  118. if (this.options.showFriendlyErrorStack) {
  119. options.errorStack = new Error().stack;
  120. }
  121. if (this.options.keyPrefix) {
  122. options.keyPrefix = this.options.keyPrefix;
  123. }
  124. return this.sendCommand(new command_1.default(commandName, args, options, callback));
  125. };
  126. }
  127. function generateScriptingFunction(_script, _encoding) {
  128. return function () {
  129. var length = arguments.length;
  130. var lastArgIndex = length - 1;
  131. var callback = arguments[lastArgIndex];
  132. if (typeof callback !== "function") {
  133. callback = undefined;
  134. }
  135. else {
  136. length = lastArgIndex;
  137. }
  138. var args = new Array(length);
  139. for (var i = 0; i < length; i++) {
  140. args[i] = arguments[i];
  141. }
  142. var options;
  143. if (this.options.dropBufferSupport) {
  144. if (!_encoding) {
  145. return standard_as_callback_1.default(PromiseContainer.get().reject(new Error(DROP_BUFFER_SUPPORT_ERROR)), callback);
  146. }
  147. options = { replyEncoding: null };
  148. }
  149. else {
  150. options = { replyEncoding: _encoding };
  151. }
  152. if (this.options.showFriendlyErrorStack) {
  153. options.errorStack = new Error().stack;
  154. }
  155. return _script.execute(this, args, options, callback);
  156. };
  157. }