@nestjs/observe 0.1.6 → 0.1.8

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 = hasPrivateMembers
186
+ const originalMethod = requiresRawReceiver
167
187
  ? attributeValue
168
188
  : Reflect.get(target, prop, receiver);
169
- const tracedReceiver = hasPrivateMembers ? target : receiver;
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
  /**
@@ -71,13 +71,13 @@ let QueueObserveAgentService = QueueObserveAgentService_1 = class QueueObserveAg
71
71
  * is, but does not expose the decorator service where expected.
72
72
  */
73
73
  loadProcessorDecoratorService() {
74
- const result = loadOptionalPeer("@nestjs/bullmq", "@nestjs/bullmq/dist/instrument/processor-decorator.service.js");
74
+ const result = loadOptionalPeer("@nestjs/bullmq");
75
75
  if (!result.installed) {
76
76
  return undefined;
77
77
  }
78
78
  if (result.error) {
79
- // The real cause - a version that moved the file, an exports map that
80
- // hides it - so the "update to the latest version" advice below is
79
+ // The real cause - a version that no longer re-exports it, a broken
80
+ // install - so the "update to the latest version" advice below is
81
81
  // never the only diagnostic.
82
82
  this.logger.warn(`@nestjs/bullmq is installed but its processor decorator could not be loaded: ${describePeerLoadError(result.error)}`);
83
83
  return null;
@@ -77,20 +77,31 @@ let ScheduleObserveAgentService = ScheduleObserveAgentService_1 = class Schedule
77
77
  * wrapped every handler.
78
78
  */
79
79
  loadScheduleExplorer() {
80
- const result = loadOptionalPeer("@nestjs/schedule", "@nestjs/schedule/dist/schedule.explorer.js");
81
- if (!result.installed) {
80
+ // The entry point is the supported source, but it only re-exports the
81
+ // explorer from 12.0.1 onwards; every earlier version keeps it behind a
82
+ // deep path. Try the public export first, then the file, so a supported
83
+ // import is preferred wherever one exists.
84
+ const entryPoint = loadOptionalPeer("@nestjs/schedule");
85
+ if (!entryPoint.installed) {
82
86
  // Nothing scheduled, nothing to patch, and that is not a
83
87
  // misconfiguration.
84
88
  return undefined;
85
89
  }
86
- if (result.error) {
87
- // Installed but the explorer cannot be loaded - a version that moved
88
- // the file, say. Worth saying out loud, with the actual cause: the
89
- // symptom otherwise is a service whose jobs silently never appear.
90
- this.logger.warn(`@nestjs/schedule is installed but its ScheduleExplorer could not be loaded, so scheduled jobs will not be instrumented: ${describePeerLoadError(result.error)}`);
91
- return undefined;
90
+ if (entryPoint.module?.ScheduleExplorer) {
91
+ return entryPoint.module.ScheduleExplorer;
92
+ }
93
+ const deepPath = loadOptionalPeer("@nestjs/schedule", "@nestjs/schedule/dist/schedule.explorer.js");
94
+ if (deepPath.installed && deepPath.module?.ScheduleExplorer) {
95
+ return deepPath.module.ScheduleExplorer;
92
96
  }
93
- return result.module?.ScheduleExplorer;
97
+ // Installed, but the explorer is in neither place - a version that moved
98
+ // it, say. Worth saying out loud, with whichever cause was recorded: the
99
+ // symptom otherwise is a service whose jobs silently never appear.
100
+ const cause = (deepPath.installed && deepPath.error) ||
101
+ entryPoint.error ||
102
+ new Error("ScheduleExplorer is not exported by @nestjs/schedule");
103
+ this.logger.warn(`@nestjs/schedule is installed but its ScheduleExplorer could not be loaded, so scheduled jobs will not be instrumented: ${describePeerLoadError(cause)}`);
104
+ return undefined;
94
105
  }
95
106
  patchScheduleExplorer() {
96
107
  const ScheduleExplorer = this.loadScheduleExplorer();
@@ -30,6 +30,12 @@ export type OptionalPeerResult<T> = {
30
30
  * say - is treated as "not installed": with no resolver there is nothing to
31
31
  * load, and crashing the constructor would defeat the point of the peer being
32
32
  * optional.
33
+ *
34
+ * A deep specifier that an `exports` map refuses (Nest v12 added one to
35
+ * several packages) is retried as an absolute path built from the package's
36
+ * own `package.json` - always exported, and an absolute path is not subject
37
+ * to the map. The file still has to be there, so a genuinely moved or removed
38
+ * entry keeps reporting its own failure.
33
39
  */
34
40
  /** Names a load failure for a log line: the message for an `Error`, `String` otherwise. */
35
41
  export declare function describePeerLoadError(error: unknown): string;
@@ -1,4 +1,5 @@
1
1
  import { createRequire } from "module";
2
+ import { dirname, join } from "path";
2
3
  /**
3
4
  * Loads an optional peer dependency without a static import, so a service
4
5
  * that does not use the feature need not install the package.
@@ -15,6 +16,12 @@ import { createRequire } from "module";
15
16
  * say - is treated as "not installed": with no resolver there is nothing to
16
17
  * load, and crashing the constructor would defeat the point of the peer being
17
18
  * optional.
19
+ *
20
+ * A deep specifier that an `exports` map refuses (Nest v12 added one to
21
+ * several packages) is retried as an absolute path built from the package's
22
+ * own `package.json` - always exported, and an absolute path is not subject
23
+ * to the map. The file still has to be there, so a genuinely moved or removed
24
+ * entry keeps reporting its own failure.
18
25
  */
19
26
  /** Names a load failure for a log line: the message for an `Error`, `String` otherwise. */
20
27
  export function describePeerLoadError(error) {
@@ -33,6 +40,35 @@ export function loadOptionalPeer(packageName, specifier = packageName) {
33
40
  return { installed: true, module: require(specifier) };
34
41
  }
35
42
  catch (error) {
43
+ const withinPackage = resolveWithinPackage(require, packageName, specifier);
44
+ if (withinPackage) {
45
+ try {
46
+ return { installed: true, module: require(withinPackage) };
47
+ }
48
+ catch {
49
+ // Fall through and report the original failure: it names the
50
+ // specifier the caller actually asked for.
51
+ }
52
+ }
36
53
  return { installed: true, module: undefined, error };
37
54
  }
38
55
  }
56
+ /**
57
+ * Turns a deep specifier such as `@nestjs/schedule/dist/schedule.explorer.js`
58
+ * into an absolute path inside the installed package, so an `exports` map
59
+ * that hides the subpath is bypassed. Returns `undefined` when the specifier
60
+ * is not a subpath of `packageName` or the package cannot be located.
61
+ */
62
+ function resolveWithinPackage(require, packageName, specifier) {
63
+ const prefix = `${packageName}/`;
64
+ if (!specifier.startsWith(prefix)) {
65
+ return undefined;
66
+ }
67
+ try {
68
+ const manifest = require.resolve(`${packageName}/package.json`);
69
+ return join(dirname(manifest), specifier.slice(prefix.length));
70
+ }
71
+ catch {
72
+ return undefined;
73
+ }
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/observe",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Observability and APM agent for NestJS - traces, runtime metrics, custom metrics and CPU profiles",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://nestjs.com",
@@ -63,12 +63,12 @@
63
63
  "@jridgewell/trace-mapping": "0.3.31"
64
64
  },
65
65
  "peerDependencies": {
66
- "@nestjs/bullmq": "^11.0.0",
67
- "@nestjs/common": "^11.0.0",
68
- "@nestjs/core": "^11.0.0",
69
- "@nestjs/graphql": "^13.0.0",
70
- "@nestjs/microservices": "^11.0.0",
71
- "@nestjs/schedule": "^4.0.0 || ^5.0.0 || ^6.0.0",
66
+ "@nestjs/bullmq": "^11.0.0 || ^12.0.0",
67
+ "@nestjs/common": "^11.0.0 || ^12.0.0",
68
+ "@nestjs/core": "^11.0.0 || ^12.0.0",
69
+ "@nestjs/graphql": "^13.0.0 || ^14.0.0",
70
+ "@nestjs/microservices": "^11.0.0 || ^12.0.0",
71
+ "@nestjs/schedule": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^12.0.0",
72
72
  "bullmq": "^5.0.0 || ^6.0.0",
73
73
  "reflect-metadata": "^0.1.12 || ^0.2.0",
74
74
  "rxjs": "^7.0.0"
@@ -97,15 +97,15 @@
97
97
  "@commitlint/config-angular": "21.2.2",
98
98
  "@grpc/grpc-js": "1.14.4",
99
99
  "@grpc/proto-loader": "0.8.1",
100
- "@nestjs/apollo": "13.4.5",
101
- "@nestjs/bullmq": "11.0.5",
102
- "@nestjs/common": "11.2.1",
103
- "@nestjs/core": "11.2.2",
104
- "@nestjs/graphql": "13.4.5",
105
- "@nestjs/microservices": "11.2.1",
106
- "@nestjs/platform-express": "11.2.1",
107
- "@nestjs/schedule": "^6.1.3",
108
- "@nestjs/testing": "11.2.1",
100
+ "@nestjs/apollo": "14.0.0",
101
+ "@nestjs/bullmq": "12.0.0",
102
+ "@nestjs/common": "12.0.1",
103
+ "@nestjs/core": "12.0.1",
104
+ "@nestjs/graphql": "14.0.0",
105
+ "@nestjs/microservices": "12.0.1",
106
+ "@nestjs/platform-express": "12.0.1",
107
+ "@nestjs/schedule": "12.0.1",
108
+ "@nestjs/testing": "12.0.1",
109
109
  "@swc/core": "1.16.1",
110
110
  "@types/node": "22.20.1",
111
111
  "@types/supertest": "7.2.1",