aboutsummaryrefslogtreecommitdiff
path: root/node_modules/pify/index.js
diff options
context:
space:
mode:
authorMinteck <nekostarfan@gmail.com>2021-08-24 14:41:48 +0200
committerMinteck <nekostarfan@gmail.com>2021-08-24 14:41:48 +0200
commitd25e11bee6ca5ca523884da132d18e1400e077b9 (patch)
tree8af39fde19f7ed640a60fb397c7edd647dff1c4c /node_modules/pify/index.js
downloadkartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.gz
kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.bz2
kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.zip
Initial commit
Diffstat (limited to 'node_modules/pify/index.js')
-rw-r--r--node_modules/pify/index.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/node_modules/pify/index.js b/node_modules/pify/index.js
new file mode 100644
index 0000000..a3877e1
--- /dev/null
+++ b/node_modules/pify/index.js
@@ -0,0 +1,113 @@
+'use strict';
+
+const processFn = (fn, 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(fn, self, arguments_);
+ });
+};
+
+const filterCache = new WeakMap();
+
+module.exports = (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 desc = Reflect.getOwnPropertyDescriptor(target, key);
+ const writableOrConfigurableOwn = (desc === undefined || desc.writable || desc.configurable);
+ const included = options.include ? options.include.some(match) : !options.exclude.some(match);
+ 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 : processFn(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 = processFn(property, options, proxy, target);
+ cache.set(property, pified);
+ return pified;
+ }
+
+ return property;
+ }
+ });
+
+ return proxy;
+};