You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.0 KiB

4 years ago
  1. 'use strict';
  2. var commands = require('redis-commands');
  3. var Multi = require('./multi');
  4. var RedisClient = require('../').RedisClient;
  5. var Command = require('./command');
  6. var addCommand = function (command) {
  7. // Some rare Redis commands use special characters in their command name
  8. // Convert those to a underscore to prevent using invalid function names
  9. var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1');
  10. // Do not override existing functions
  11. if (!RedisClient.prototype[command]) {
  12. RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command] = function () {
  13. var arr;
  14. var len = arguments.length;
  15. var callback;
  16. var i = 0;
  17. if (Array.isArray(arguments[0])) {
  18. arr = arguments[0];
  19. if (len === 2) {
  20. callback = arguments[1];
  21. }
  22. } else if (len > 1 && Array.isArray(arguments[1])) {
  23. if (len === 3) {
  24. callback = arguments[2];
  25. }
  26. len = arguments[1].length;
  27. arr = new Array(len + 1);
  28. arr[0] = arguments[0];
  29. for (; i < len; i += 1) {
  30. arr[i + 1] = arguments[1][i];
  31. }
  32. } else {
  33. // The later should not be the average use case
  34. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  35. len--;
  36. callback = arguments[len];
  37. }
  38. arr = new Array(len);
  39. for (; i < len; i += 1) {
  40. arr[i] = arguments[i];
  41. }
  42. }
  43. return this.internal_send_command(new Command(command, arr, callback));
  44. };
  45. // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run)
  46. if (commandName !== command) {
  47. RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command];
  48. }
  49. Object.defineProperty(RedisClient.prototype[command], 'name', {
  50. value: commandName
  51. });
  52. }
  53. // Do not override existing functions
  54. if (!Multi.prototype[command]) {
  55. Multi.prototype[command.toUpperCase()] = Multi.prototype[command] = function () {
  56. var arr;
  57. var len = arguments.length;
  58. var callback;
  59. var i = 0;
  60. if (Array.isArray(arguments[0])) {
  61. arr = arguments[0];
  62. if (len === 2) {
  63. callback = arguments[1];
  64. }
  65. } else if (len > 1 && Array.isArray(arguments[1])) {
  66. if (len === 3) {
  67. callback = arguments[2];
  68. }
  69. len = arguments[1].length;
  70. arr = new Array(len + 1);
  71. arr[0] = arguments[0];
  72. for (; i < len; i += 1) {
  73. arr[i + 1] = arguments[1][i];
  74. }
  75. } else {
  76. // The later should not be the average use case
  77. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  78. len--;
  79. callback = arguments[len];
  80. }
  81. arr = new Array(len);
  82. for (; i < len; i += 1) {
  83. arr[i] = arguments[i];
  84. }
  85. }
  86. this.queue.push(new Command(command, arr, callback));
  87. return this;
  88. };
  89. // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run)
  90. if (commandName !== command) {
  91. Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command];
  92. }
  93. Object.defineProperty(Multi.prototype[command], 'name', {
  94. value: commandName
  95. });
  96. }
  97. };
  98. commands.list.forEach(addCommand);
  99. module.exports = addCommand;