extendedApi.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. var utils = require('./utils');
  3. var debug = require('./debug');
  4. var RedisClient = require('../').RedisClient;
  5. var Command = require('./command');
  6. var noop = function () {};
  7. /**********************************************
  8. All documented and exposed API belongs in here
  9. **********************************************/
  10. // Redirect calls to the appropriate function and use to send arbitrary / not supported commands
  11. RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = function (command, args, callback) {
  12. // Throw to fail early instead of relying in order in this case
  13. if (typeof command !== 'string') {
  14. throw new TypeError('Wrong input type "' + (command !== null && command !== undefined ? command.constructor.name : command) + '" for command name');
  15. }
  16. command = command.toLowerCase();
  17. if (!Array.isArray(args)) {
  18. if (args === undefined || args === null) {
  19. args = [];
  20. } else if (typeof args === 'function' && callback === undefined) {
  21. callback = args;
  22. args = [];
  23. } else {
  24. throw new TypeError('Wrong input type "' + args.constructor.name + '" for args');
  25. }
  26. }
  27. if (typeof callback !== 'function' && callback !== undefined) {
  28. throw new TypeError('Wrong input type "' + (callback !== null ? callback.constructor.name : 'null') + '" for callback function');
  29. }
  30. // Using the raw multi command is only possible with this function
  31. // If the command is not yet added to the client, the internal function should be called right away
  32. // Otherwise we need to redirect the calls to make sure the internal functions don't get skipped
  33. // The internal functions could actually be used for any non hooked function
  34. // but this might change from time to time and at the moment there's no good way to distinguish them
  35. // from each other, so let's just do it do it this way for the time being
  36. if (command === 'multi' || typeof this[command] !== 'function') {
  37. return this.internal_send_command(new Command(command, args, callback));
  38. }
  39. if (typeof callback === 'function') {
  40. args = args.concat([callback]); // Prevent manipulating the input array
  41. }
  42. return this[command].apply(this, args);
  43. };
  44. RedisClient.prototype.end = function (flush) {
  45. // Flush queue if wanted
  46. if (flush) {
  47. this.flush_and_error({
  48. message: 'Connection forcefully ended and command aborted.',
  49. code: 'NR_CLOSED'
  50. });
  51. } else if (arguments.length === 0) {
  52. this.warn(
  53. 'Using .end() without the flush parameter is deprecated and throws from v.3.0.0 on.\n' +
  54. 'Please check the doku (https://github.com/NodeRedis/node_redis) and explictly use flush.'
  55. );
  56. }
  57. // Clear retry_timer
  58. if (this.retry_timer) {
  59. clearTimeout(this.retry_timer);
  60. this.retry_timer = null;
  61. }
  62. this.stream.removeAllListeners();
  63. this.stream.on('error', noop);
  64. this.connected = false;
  65. this.ready = false;
  66. this.closing = true;
  67. return this.stream.destroySoon();
  68. };
  69. RedisClient.prototype.unref = function () {
  70. if (this.connected) {
  71. debug("Unref'ing the socket connection");
  72. this.stream.unref();
  73. } else {
  74. debug('Not connected yet, will unref later');
  75. this.once('connect', function () {
  76. this.unref();
  77. });
  78. }
  79. };
  80. RedisClient.prototype.duplicate = function (options, callback) {
  81. if (typeof options === 'function') {
  82. callback = options;
  83. options = null;
  84. }
  85. var existing_options = utils.clone(this.options);
  86. options = utils.clone(options);
  87. for (var elem in options) {
  88. existing_options[elem] = options[elem];
  89. }
  90. var client = new RedisClient(existing_options);
  91. client.selected_db = this.selected_db;
  92. if (typeof callback === 'function') {
  93. var ready_listener = function () {
  94. callback(null, client);
  95. client.removeAllListeners(error_listener);
  96. };
  97. var error_listener = function (err) {
  98. callback(err);
  99. client.end(true);
  100. };
  101. client.once('ready', ready_listener);
  102. client.once('error', error_listener);
  103. return;
  104. }
  105. return client;
  106. };