commands.js 4.4 KB

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