index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const processFn = (fn, options, proxy, unwrapped) => function (...arguments_) {
  3. const P = options.promiseModule;
  4. return new P((resolve, reject) => {
  5. if (options.multiArgs) {
  6. arguments_.push((...result) => {
  7. if (options.errorFirst) {
  8. if (result[0]) {
  9. reject(result);
  10. } else {
  11. result.shift();
  12. resolve(result);
  13. }
  14. } else {
  15. resolve(result);
  16. }
  17. });
  18. } else if (options.errorFirst) {
  19. arguments_.push((error, result) => {
  20. if (error) {
  21. reject(error);
  22. } else {
  23. resolve(result);
  24. }
  25. });
  26. } else {
  27. arguments_.push(resolve);
  28. }
  29. const self = this === proxy ? unwrapped : this;
  30. Reflect.apply(fn, self, arguments_);
  31. });
  32. };
  33. const filterCache = new WeakMap();
  34. module.exports = (input, options) => {
  35. options = {
  36. exclude: [/.+(?:Sync|Stream)$/],
  37. errorFirst: true,
  38. promiseModule: Promise,
  39. ...options
  40. };
  41. const objectType = typeof input;
  42. if (!(input !== null && (objectType === 'object' || objectType === 'function'))) {
  43. throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objectType}\``);
  44. }
  45. const filter = (target, key) => {
  46. let cached = filterCache.get(target);
  47. if (!cached) {
  48. cached = {};
  49. filterCache.set(target, cached);
  50. }
  51. if (key in cached) {
  52. return cached[key];
  53. }
  54. const match = pattern => (typeof pattern === 'string' || typeof key === 'symbol') ? key === pattern : pattern.test(key);
  55. const desc = Reflect.getOwnPropertyDescriptor(target, key);
  56. const writableOrConfigurableOwn = (desc === undefined || desc.writable || desc.configurable);
  57. const included = options.include ? options.include.some(match) : !options.exclude.some(match);
  58. const shouldFilter = included && writableOrConfigurableOwn;
  59. cached[key] = shouldFilter;
  60. return shouldFilter;
  61. };
  62. const cache = new WeakMap();
  63. const proxy = new Proxy(input, {
  64. apply(target, thisArg, args) {
  65. const cached = cache.get(target);
  66. if (cached) {
  67. return Reflect.apply(cached, thisArg, args);
  68. }
  69. const pified = options.excludeMain ? target : processFn(target, options, proxy, target);
  70. cache.set(target, pified);
  71. return Reflect.apply(pified, thisArg, args);
  72. },
  73. get(target, key) {
  74. const property = target[key];
  75. // eslint-disable-next-line no-use-extend-native/no-use-extend-native
  76. if (!filter(target, key) || property === Function.prototype[key]) {
  77. return property;
  78. }
  79. const cached = cache.get(property);
  80. if (cached) {
  81. return cached;
  82. }
  83. if (typeof property === 'function') {
  84. const pified = processFn(property, options, proxy, target);
  85. cache.set(property, pified);
  86. return pified;
  87. }
  88. return property;
  89. }
  90. });
  91. return proxy;
  92. };