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