1 | const processFunction = (function_, options, proxy, unwrapped) => function (...arguments_) {
|
2 | const P = options.promiseModule;
|
3 |
|
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 |
|
30 | const self = this === proxy ? unwrapped : this;
|
31 | Reflect.apply(function_, self, arguments_);
|
32 | });
|
33 | };
|
34 |
|
35 | const filterCache = new WeakMap();
|
36 |
|
37 | export default function pify(input, options) {
|
38 | options = {
|
39 | exclude: [/.+(?:Sync|Stream)$/],
|
40 | errorFirst: true,
|
41 | promiseModule: Promise,
|
42 | ...options,
|
43 | };
|
44 |
|
45 | const objectType = typeof input;
|
46 | if (!(input !== null && (objectType === 'object' || objectType === 'function'))) {
|
47 | throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objectType}\``);
|
48 | }
|
49 |
|
50 | const filter = (target, key) => {
|
51 | let cached = filterCache.get(target);
|
52 |
|
53 | if (!cached) {
|
54 | cached = {};
|
55 | filterCache.set(target, cached);
|
56 | }
|
57 |
|
58 | if (key in cached) {
|
59 | return cached[key];
|
60 | }
|
61 |
|
62 | const match = pattern => (typeof pattern === 'string' || typeof key === 'symbol') ? key === pattern : pattern.test(key);
|
63 | const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
|
64 | const writableOrConfigurableOwn = (descriptor === undefined || descriptor.writable || descriptor.configurable);
|
65 | const included = options.include ? options.include.some(element => match(element)) : !options.exclude.some(element => match(element));
|
66 | const shouldFilter = included && writableOrConfigurableOwn;
|
67 | cached[key] = shouldFilter;
|
68 | return shouldFilter;
|
69 | };
|
70 |
|
71 | const cache = new WeakMap();
|
72 |
|
73 | const proxy = new Proxy(input, {
|
74 | apply(target, thisArg, args) {
|
75 | const cached = cache.get(target);
|
76 |
|
77 | if (cached) {
|
78 | return Reflect.apply(cached, thisArg, args);
|
79 | }
|
80 |
|
81 | const pified = options.excludeMain ? target : processFunction(target, options, proxy, target);
|
82 | cache.set(target, pified);
|
83 | return Reflect.apply(pified, thisArg, args);
|
84 | },
|
85 |
|
86 | get(target, key) {
|
87 | const property = target[key];
|
88 |
|
89 |
|
90 | if (!filter(target, key) || property === Function.prototype[key]) {
|
91 | return property;
|
92 | }
|
93 |
|
94 | const cached = cache.get(property);
|
95 |
|
96 | if (cached) {
|
97 | return cached;
|
98 | }
|
99 |
|
100 | if (typeof property === 'function') {
|
101 | const pified = processFunction(property, options, proxy, target);
|
102 | cache.set(property, pified);
|
103 | return pified;
|
104 | }
|
105 |
|
106 | return property;
|
107 | },
|
108 | });
|
109 |
|
110 | return proxy;
|
111 | }
|