commands.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // TODO: Rewrite this including the invidual commands into a Commands class
  19. // that provided a functionality to add new commands to the client
  20. commands.list.forEach(function (command) {
  21. // Some rare Redis commands use special characters in their command name
  22. // Convert those to a underscore to prevent using invalid function names
  23. var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1');
  24. // Do not override existing functions
  25. if (!RedisClient.prototype[command]) {
  26. RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command] = function () {
  27. var arr;
  28. var len = arguments.length;
  29. var callback;
  30. var i = 0;
  31. if (Array.isArray(arguments[0])) {
  32. arr = arguments[0];
  33. if (len === 2) {
  34. callback = arguments[1];
  35. }
  36. } else if (len > 1 && Array.isArray(arguments[1])) {
  37. if (len === 3) {
  38. callback = arguments[2];
  39. }
  40. len = arguments[1].length;
  41. arr = new Array(len + 1);
  42. arr[0] = arguments[0];
  43. for (; i < len; i += 1) {
  44. arr[i + 1] = arguments[1][i];
  45. }
  46. } else {
  47. // The later should not be the average use case
  48. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  49. len--;
  50. callback = arguments[len];
  51. }
  52. arr = new Array(len);
  53. for (; i < len; i += 1) {
  54. arr[i] = arguments[i];
  55. }
  56. }
  57. return this.internal_send_command(new Command(command, arr, callback));
  58. };
  59. if (changeFunctionName) {
  60. Object.defineProperty(RedisClient.prototype[command], 'name', {
  61. value: commandName
  62. });
  63. }
  64. }
  65. // Do not override existing functions
  66. if (!Multi.prototype[command]) {
  67. Multi.prototype[command.toUpperCase()] = Multi.prototype[command] = function () {
  68. var arr;
  69. var len = arguments.length;
  70. var callback;
  71. var i = 0;
  72. if (Array.isArray(arguments[0])) {
  73. arr = arguments[0];
  74. if (len === 2) {
  75. callback = arguments[1];
  76. }
  77. } else if (len > 1 && Array.isArray(arguments[1])) {
  78. if (len === 3) {
  79. callback = arguments[2];
  80. }
  81. len = arguments[1].length;
  82. arr = new Array(len + 1);
  83. arr[0] = arguments[0];
  84. for (; i < len; i += 1) {
  85. arr[i + 1] = arguments[1][i];
  86. }
  87. } else {
  88. // The later should not be the average use case
  89. if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
  90. len--;
  91. callback = arguments[len];
  92. }
  93. arr = new Array(len);
  94. for (; i < len; i += 1) {
  95. arr[i] = arguments[i];
  96. }
  97. }
  98. this.queue.push(new Command(command, arr, callback));
  99. return this;
  100. };
  101. if (changeFunctionName) {
  102. Object.defineProperty(Multi.prototype[command], 'name', {
  103. value: commandName
  104. });
  105. }
  106. }
  107. });