@cldmv/slothlet 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -41,6 +41,9 @@ const EXCLUDED_CONSTRUCTORS = new Set([Object, Array, Promise, Date, RegExp, Err
|
|
|
41
41
|
const EXCLUDED_INSTANCEOF_CLASSES = [ArrayBuffer, Map, Set, WeakMap, WeakSet];
|
|
42
42
|
|
|
43
43
|
|
|
44
|
+
const PROMISE_METHODS = new Set(["then", "catch", "finally"]);
|
|
45
|
+
|
|
46
|
+
|
|
44
47
|
function runtime_shouldWrapMethod(value, prop) {
|
|
45
48
|
return (
|
|
46
49
|
typeof value === "function" &&
|
|
@@ -127,6 +130,7 @@ function runtime_wrapClassInstance(instance, ctx, wrapFn, instanceCache) {
|
|
|
127
130
|
export const makeWrapper = (ctx) => {
|
|
128
131
|
const cache = new WeakMap();
|
|
129
132
|
const instanceCache = new WeakMap();
|
|
133
|
+
const promiseMethodCache = new WeakMap();
|
|
130
134
|
const wrap = (val) => {
|
|
131
135
|
if (val == null || (typeof val !== "object" && typeof val !== "function")) return val;
|
|
132
136
|
if (cache.has(val)) return cache.get(val);
|
|
@@ -165,11 +169,66 @@ export const makeWrapper = (ctx) => {
|
|
|
165
169
|
return result;
|
|
166
170
|
},
|
|
167
171
|
get(target, prop, receiver) {
|
|
168
|
-
|
|
172
|
+
const value = Reflect.get(target, prop, receiver);
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
const isPromiseMethod = typeof value === "function" && PROMISE_METHODS.has(prop);
|
|
177
|
+
const isNativePromise = util.types.isPromise(target);
|
|
178
|
+
const hasThen = typeof target?.then === "function";
|
|
179
|
+
|
|
180
|
+
if (isPromiseMethod && (isNativePromise || hasThen)) {
|
|
181
|
+
|
|
182
|
+
let targetMethodCache = promiseMethodCache.get(target);
|
|
183
|
+
if (!targetMethodCache) {
|
|
184
|
+
targetMethodCache = new Map();
|
|
185
|
+
promiseMethodCache.set(target, targetMethodCache);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (targetMethodCache.has(prop)) {
|
|
189
|
+
return targetMethodCache.get(prop);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const wrappedMethod = function (...args) {
|
|
193
|
+
|
|
194
|
+
const wrappedArgs = args.map((arg) => {
|
|
195
|
+
if (typeof arg === "function") {
|
|
196
|
+
return function (...callbackArgs) {
|
|
197
|
+
return runWithCtx(ctx, arg, undefined, callbackArgs);
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return arg;
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
const result = Reflect.apply(value, target, wrappedArgs);
|
|
205
|
+
|
|
206
|
+
return wrap(result);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
targetMethodCache.set(prop, wrappedMethod);
|
|
210
|
+
return wrappedMethod;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return wrap(value);
|
|
214
|
+
},
|
|
215
|
+
set(target, prop, value, receiver) {
|
|
216
|
+
|
|
217
|
+
const methodCache = promiseMethodCache.get(target);
|
|
218
|
+
if (methodCache && methodCache.has(prop)) {
|
|
219
|
+
methodCache.delete(prop);
|
|
220
|
+
}
|
|
221
|
+
return Reflect.set(target, prop, value, receiver);
|
|
169
222
|
},
|
|
170
|
-
set: Reflect.set,
|
|
171
223
|
defineProperty: Reflect.defineProperty,
|
|
172
|
-
deleteProperty
|
|
224
|
+
deleteProperty(target, prop) {
|
|
225
|
+
|
|
226
|
+
const methodCache = promiseMethodCache.get(target);
|
|
227
|
+
if (methodCache && methodCache.has(prop)) {
|
|
228
|
+
methodCache.delete(prop);
|
|
229
|
+
}
|
|
230
|
+
return Reflect.deleteProperty(target, prop);
|
|
231
|
+
},
|
|
173
232
|
ownKeys: Reflect.ownKeys,
|
|
174
233
|
getOwnPropertyDescriptor: Reflect.getOwnPropertyDescriptor,
|
|
175
234
|
has: Reflect.has
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","sourceRoot":"","sources":["../../../../dist/lib/runtime/runtime.mjs"],"names":[],"mappings":"AA0CO,gCAdI,MAAM,yBAEN,GAAG,gBAED,GAAG,CA6Bf;AAiBM,0BAZM,MAAM,GAAC,IAAI,CAY0B;
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","sourceRoot":"","sources":["../../../../dist/lib/runtime/runtime.mjs"],"names":[],"mappings":"AA0CO,gCAdI,MAAM,yBAEN,GAAG,gBAED,GAAG,CA6Bf;AAiBM,0BAZM,MAAM,GAAC,IAAI,CAY0B;AAiL3C,iCAjBI,MAAM,YAgIhB;AA4RD;;;;;;;;;;;;;GAaG;AACH,mBATU,WAAS,MAAM,CAS6B;AAEtD;;;;;;;;;;;;;GAaG;AACH,sBATU,MAAM,CAS4C;AAE5D;;;;;;;;;;;;;GAaG;AACH,wBATU,MAAM,CASgD"}
|