UNPKG

@dudousxd/nestjs-diagnostics

Version:

A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.

105 lines • 4.81 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import diagnostics_channel from 'node:diagnostics_channel'; import { Inject, Injectable, Logger, } from '@nestjs/common'; import { DiscoveryService, MetadataScanner } from '@nestjs/core'; import { CHANNEL_PREFIX, channelName } from '../channel.js'; import { onChannelRegistered, registeredChannels } from '../registry.js'; import { ON_DIAGNOSTIC_META } from './on-diagnostic.decorator.js'; let DiagnosticsExplorer = class DiagnosticsExplorer { discovery; scanner; logger = new Logger('DiagnosticsExplorer'); subscriptions = []; wildcards = []; offChannelRegistered = null; constructor(discovery, scanner) { this.discovery = discovery; this.scanner = scanner; } onApplicationBootstrap() { for (const wrapper of this.discovery.getProviders()) { const instance = wrapper.instance; if (!instance || typeof instance !== 'object') continue; const proto = Object.getPrototypeOf(instance); for (const methodName of this.scanner.getAllMethodNames(proto)) { const metas = Reflect.getMetadata(ON_DIAGNOSTIC_META, proto, methodName); if (!metas?.length) continue; for (const meta of metas) { const invoke = (event) => this.safeInvoke(instance, methodName, event); if (meta.event !== undefined) { this.subscribe(channelName(meta.lib, meta.event), invoke); } else { const prefix = `${CHANNEL_PREFIX}:${meta.lib}:`; this.wildcards.push({ prefix, invoke }); for (const name of registeredChannels()) { if (name.startsWith(prefix)) this.subscribe(name, invoke); } } } } } this.offChannelRegistered = onChannelRegistered((name) => { for (const w of this.wildcards) { if (name.startsWith(w.prefix)) this.subscribe(name, w.invoke); } }); } onApplicationShutdown() { this.offChannelRegistered?.(); this.offChannelRegistered = null; for (const { name, listener } of this.subscriptions) { diagnostics_channel.channel(name).unsubscribe(listener); } this.subscriptions.length = 0; this.wildcards.length = 0; } /** Subscribe once to `name`; the listener fans the envelope into `invoke`. */ subscribe(name, invoke) { const listener = (msg) => invoke(msg); diagnostics_channel.channel(name).subscribe(listener); this.subscriptions.push({ name, listener }); } /** Invoke a handler, swallowing sync throws and async rejections so a buggy * reaction can never break the synchronous `emit()` that triggered it. */ safeInvoke(instance, methodName, event) { try { const fn = instance[methodName]; const result = fn.call(instance, event); if (result != null && typeof result.then === 'function') { result.catch((err) => this.logError(methodName, err)); } } catch (err) { this.logError(methodName, err); } } logError(methodName, err) { const message = err instanceof Error ? err.message : String(err); this.logger.error(`@OnDiagnostic ${methodName} handler failed: ${message}`); } }; DiagnosticsExplorer = __decorate([ Injectable(), __param(0, Inject(DiscoveryService)), __param(1, Inject(MetadataScanner)), __metadata("design:paramtypes", [DiscoveryService, MetadataScanner]) ], DiagnosticsExplorer); export { DiagnosticsExplorer }; //# sourceMappingURL=diagnostics.explorer.js.map