@dudousxd/nestjs-diagnostics
Version:
A standard convention for @dudousxd/nestjs-* libs to emit observability events over node:diagnostics_channel.
78 lines • 3.33 kB
JavaScript
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 { Inject, Injectable, Optional, } from '@nestjs/common';
import { DEFAULT_RECENT_SIZE, recentEventsBuffer, startRecentEvents, stopRecentEvents, } from '../recent.js';
import { DIAGNOSTICS_OPTIONS } from './diagnostics.options.js';
/**
* The buffer size `recent` asks for, or `null` when the buffer stays off: `false` → off; `true` or
* `{ size }` → on; omitted → on unless `NODE_ENV` is `'production'`.
*/
export function resolveRecentSize(recent) {
if (recent === false)
return null;
if (recent === true)
return DEFAULT_RECENT_SIZE;
if (typeof recent === 'object' && recent !== null) {
const size = recent.size ?? DEFAULT_RECENT_SIZE;
if (!Number.isInteger(size) || size < 1) {
throw new Error(`@dudousxd/nestjs-diagnostics: recent.size must be a positive integer (got ${String(size)})`);
}
return size;
}
return process.env.NODE_ENV === 'production' ? null : DEFAULT_RECENT_SIZE;
}
/**
* The recent-events buffer of this process, as a provider: starts it at module init when `recent`
* says so, stops it at shutdown, and reads it.
*
* ```ts
* constructor(private readonly recent: DiagnosticsRecentEvents) {}
* this.recent.list({ lib: 'durable', limit: 50 });
* ```
*/
let DiagnosticsRecentEvents = class DiagnosticsRecentEvents {
size;
owned = null;
constructor(options) {
this.size = resolveRecentSize(options?.recent);
}
onModuleInit() {
if (this.size !== null)
this.owned = startRecentEvents(this.size);
}
onApplicationShutdown() {
if (this.owned !== null && recentEventsBuffer() === this.owned)
stopRecentEvents();
this.owned = null;
}
/** Whether the buffer runs in this process. */
get enabled() {
return recentEventsBuffer() !== null;
}
/** The newest buffered events matching `filter`, newest first. `[]` when the buffer is off. */
list(filter) {
return recentEventsBuffer()?.list(filter) ?? [];
}
/** Drop every buffered event. */
clear() {
recentEventsBuffer()?.clear();
}
};
DiagnosticsRecentEvents = __decorate([
Injectable(),
__param(0, Optional()),
__param(0, Inject(DIAGNOSTICS_OPTIONS)),
__metadata("design:paramtypes", [Object])
], DiagnosticsRecentEvents);
export { DiagnosticsRecentEvents };
//# sourceMappingURL=recent-events.service.js.map