@nestjs/observe 0.1.6 → 0.1.7
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.
|
@@ -123,9 +123,18 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
|
|
|
123
123
|
// Such classes get the raw instance as receiver instead - trading away
|
|
124
124
|
// nested `this.other()` spans (those calls no longer pass through the
|
|
125
125
|
// proxy) for methods that run at all.
|
|
126
|
+
//
|
|
127
|
+
// Built-ins backed by internal slots (`Map`, `Set`, `Date`, ...) hit the
|
|
128
|
+
// same wall for the same reason: `Map.prototype.has` reads `[[MapData]]`
|
|
129
|
+
// off its receiver, internal slots never forward through a proxy, so the
|
|
130
|
+
// call throws "called on incompatible receiver" (nestjs/nest#17569).
|
|
126
131
|
let hasPrivateMembers;
|
|
132
|
+
let slotBacked;
|
|
133
|
+
let bareBuiltIn;
|
|
127
134
|
try {
|
|
128
135
|
hasPrivateMembers = usesNativePrivateMembers(instance);
|
|
136
|
+
slotBacked = isSlotBackedBuiltIn(instance);
|
|
137
|
+
bareBuiltIn = slotBacked && isNativeFunction(instance.constructor);
|
|
129
138
|
}
|
|
130
139
|
catch {
|
|
131
140
|
// Reading the prototype ran a trap that threw (nestjs-cls proxy
|
|
@@ -133,6 +142,17 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
|
|
|
133
142
|
// inspected cannot be instrumented; hand it back untouched.
|
|
134
143
|
return instance;
|
|
135
144
|
}
|
|
145
|
+
// A bare built-in registered as a provider (`useValue: new Map()`) has no
|
|
146
|
+
// user code to trace - every method on it is native, and each one would
|
|
147
|
+
// throw through the proxy. Hand it back untouched. A user *subclass* of a
|
|
148
|
+
// built-in still carries methods worth tracing, so it falls through to the
|
|
149
|
+
// raw-receiver path below instead.
|
|
150
|
+
if (bareBuiltIn) {
|
|
151
|
+
return instance;
|
|
152
|
+
}
|
|
153
|
+
// Slot-backed subclasses share the private-member trade-off: methods run
|
|
154
|
+
// against the raw instance so the natives they inherit keep working.
|
|
155
|
+
const requiresRawReceiver = hasPrivateMembers || slotBacked;
|
|
136
156
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
137
157
|
const methodRefsCache = new WeakMap();
|
|
138
158
|
const currentlyTracing = new WeakMap();
|
|
@@ -163,10 +183,10 @@ export function createInstanceDecorator(als, operationTraceRegistry, options) {
|
|
|
163
183
|
// For a private-member class, `Reflect.get` with the proxy as receiver
|
|
164
184
|
// would already throw on an accessor that touches `this.#x`, so the
|
|
165
185
|
// value fetched against the target above is reused instead.
|
|
166
|
-
const originalMethod =
|
|
186
|
+
const originalMethod = requiresRawReceiver
|
|
167
187
|
? attributeValue
|
|
168
188
|
: Reflect.get(target, prop, receiver);
|
|
169
|
-
const tracedReceiver =
|
|
189
|
+
const tracedReceiver = requiresRawReceiver ? target : receiver;
|
|
170
190
|
const proxyFn = createTracedWrapper({
|
|
171
191
|
className,
|
|
172
192
|
methodName,
|
|
@@ -296,6 +316,68 @@ function isCallableObject(fn) {
|
|
|
296
316
|
return (!INTRINSIC_FUNCTION_PROTOTYPES.has(Object.getPrototypeOf(fn)) ||
|
|
297
317
|
Object.keys(fn).length > 0);
|
|
298
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* Prototypes of the built-ins whose methods read internal slots
|
|
321
|
+
* (`[[MapData]]`, `[[SetData]]`, `[[DateValue]]`, ...) off their receiver.
|
|
322
|
+
* Internal slots live on the target and never forward through a proxy, so any
|
|
323
|
+
* such method invoked with the instrumentation proxy as receiver throws
|
|
324
|
+
* "called on incompatible receiver".
|
|
325
|
+
*
|
|
326
|
+
* `%TypedArray%.prototype` (reached via `Uint8Array`'s parent) covers every
|
|
327
|
+
* typed array - and, through it, Node's `Buffer`. `Array` is deliberately
|
|
328
|
+
* absent: its methods are generic and proxy-safe.
|
|
329
|
+
*/
|
|
330
|
+
const SLOT_BACKED_PROTOTYPES = new Set([
|
|
331
|
+
Map.prototype,
|
|
332
|
+
Set.prototype,
|
|
333
|
+
WeakMap.prototype,
|
|
334
|
+
WeakSet.prototype,
|
|
335
|
+
Date.prototype,
|
|
336
|
+
RegExp.prototype,
|
|
337
|
+
Promise.prototype,
|
|
338
|
+
ArrayBuffer.prototype,
|
|
339
|
+
...(typeof SharedArrayBuffer === "undefined"
|
|
340
|
+
? []
|
|
341
|
+
: [SharedArrayBuffer.prototype]),
|
|
342
|
+
DataView.prototype,
|
|
343
|
+
WeakRef.prototype,
|
|
344
|
+
FinalizationRegistry.prototype,
|
|
345
|
+
Object.getPrototypeOf(Uint8Array.prototype),
|
|
346
|
+
]);
|
|
347
|
+
/**
|
|
348
|
+
* Whether the instance's prototype chain passes through a slot-backed
|
|
349
|
+
* built-in, meaning its (inherited) native methods would throw if invoked with
|
|
350
|
+
* a proxy as their receiver.
|
|
351
|
+
*/
|
|
352
|
+
function isSlotBackedBuiltIn(instance) {
|
|
353
|
+
let proto = Object.getPrototypeOf(instance);
|
|
354
|
+
while (proto) {
|
|
355
|
+
if (SLOT_BACKED_PROTOTYPES.has(proto)) {
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
proto = Object.getPrototypeOf(proto);
|
|
359
|
+
}
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Whether a constructor is engine- or platform-provided rather than user
|
|
364
|
+
* code. A slot-backed instance whose entire chain is native (`new Map()`,
|
|
365
|
+
* `Buffer.from(...)`) carries nothing to trace; one with a user constructor on
|
|
366
|
+
* top (`class Cache extends Map`) does.
|
|
367
|
+
*/
|
|
368
|
+
function isNativeFunction(fn) {
|
|
369
|
+
if (typeof fn !== "function") {
|
|
370
|
+
// No constructor at all (`Object.create(Map.prototype)`): nothing
|
|
371
|
+
// user-defined to trace either.
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
try {
|
|
375
|
+
return /\{\s*\[native code\]\s*\}$/.test(Function.prototype.toString.call(fn));
|
|
376
|
+
}
|
|
377
|
+
catch {
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
299
381
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
300
382
|
const privateMemberCache = new WeakMap();
|
|
301
383
|
/**
|
package/package.json
CHANGED