index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Module dependencies.
  3. */
  4. var thenify = require('thenify');
  5. var EventEmitter = require('events').EventEmitter;
  6. /**
  7. * List of API functions that do not take a callback as an argument
  8. * since they are not redis commands.
  9. *
  10. * @constant
  11. * @type {Array}
  12. */
  13. var API_FUNCTIONS = ['end', 'unref'];
  14. /**
  15. * Wrap `client`.
  16. *
  17. * @param {Redis} client
  18. * @return {Object}
  19. */
  20. module.exports = function (client) {
  21. var wrap = {};
  22. wrap.multi = function () {
  23. var multi = client.multi();
  24. multi.exec = thenify(multi.exec);
  25. return multi;
  26. };
  27. Object.keys(client).forEach(function (key) {
  28. wrap[key] = client[key];
  29. });
  30. Object.keys(EventEmitter.prototype).forEach(function (key) {
  31. if (typeof client[key] != 'function') return;
  32. wrap[key] = client[key].bind(client);
  33. });
  34. Object.defineProperty(wrap, 'connected', {
  35. get: function () { return client.connected }
  36. });
  37. Object.defineProperty(wrap, 'retry_delay', {
  38. get: function () { return client.retry_delay }
  39. });
  40. Object.defineProperty(wrap, 'retry_backoff', {
  41. get: function () { return client.retry_backoff }
  42. });
  43. Object.keys(Object.getPrototypeOf(client)).forEach(function (key) {
  44. var protoFunction = client[key].bind(client);
  45. var isCommand = API_FUNCTIONS.indexOf(key) === -1;
  46. var isMulti = key == 'multi';
  47. if (isMulti) return;
  48. if (isCommand) {
  49. protoFunction = thenify(protoFunction);
  50. }
  51. wrap[key] = protoFunction;
  52. });
  53. return wrap;
  54. };