@bymax-one/nest-core 1.3.2 → 1.4.0
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.
- package/CHANGELOG.md +142 -2
- package/README.md +222 -36
- package/dist/index.cjs +335 -161
- package/dist/index.d.cts +242 -16
- package/dist/index.d.ts +242 -16
- package/dist/index.mjs +336 -163
- package/dist/openapi/index.cjs +182 -19
- package/dist/openapi/index.d.cts +147 -2
- package/dist/openapi/index.d.ts +147 -2
- package/dist/openapi/index.mjs +182 -22
- package/package.json +3 -1
package/dist/index.cjs
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
var common = require('@nestjs/common');
|
|
4
4
|
var core = require('@nestjs/core');
|
|
5
|
-
var rxjs = require('rxjs');
|
|
6
5
|
var crypto = require('crypto');
|
|
6
|
+
var async_hooks = require('async_hooks');
|
|
7
|
+
var rxjs = require('rxjs');
|
|
7
8
|
|
|
8
9
|
var __defProp = Object.defineProperty;
|
|
9
10
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -112,6 +113,9 @@ function resolveOpenApi(raw) {
|
|
|
112
113
|
// otherwise reach into objects the consumer still holds a reference to.
|
|
113
114
|
security: structuredClone(raw?.security ?? []),
|
|
114
115
|
operationSecurity: structuredClone(raw?.operationSecurity ?? {}),
|
|
116
|
+
// Carried by reference, not cloned: it is a function the consumer owns, and
|
|
117
|
+
// `structuredClone` cannot copy one at all.
|
|
118
|
+
...raw?.operationIdFactory === void 0 ? {} : { operationIdFactory: raw.operationIdFactory },
|
|
115
119
|
includeCoreSchemas: raw?.includeCoreSchemas ?? true
|
|
116
120
|
};
|
|
117
121
|
}
|
|
@@ -592,141 +596,6 @@ exports.BymaxExceptionFilter = __decorateClass([
|
|
|
592
596
|
__decorateParam(3, common.Optional()),
|
|
593
597
|
__decorateParam(3, common.Inject(BYMAX_TRACE_CONTEXT))
|
|
594
598
|
], exports.BymaxExceptionFilter);
|
|
595
|
-
|
|
596
|
-
// src/timing/request-info.accessor.ts
|
|
597
|
-
function stripQueryString(rawUrl) {
|
|
598
|
-
const queryIndex = rawUrl.indexOf("?");
|
|
599
|
-
return queryIndex === -1 ? rawUrl : rawUrl.slice(0, queryIndex);
|
|
600
|
-
}
|
|
601
|
-
function readExpressTemplate(request) {
|
|
602
|
-
const path = request.route?.path;
|
|
603
|
-
return path === void 0 ? void 0 : `${request.baseUrl ?? ""}${path}`;
|
|
604
|
-
}
|
|
605
|
-
function readFastifyTemplate(request) {
|
|
606
|
-
return request.routeOptions?.url;
|
|
607
|
-
}
|
|
608
|
-
function extractRequestInfo(context) {
|
|
609
|
-
const request = context.switchToHttp().getRequest();
|
|
610
|
-
const template = readExpressTemplate(request) ?? readFastifyTemplate(request);
|
|
611
|
-
const rawUrl = request.originalUrl ?? request.url ?? "";
|
|
612
|
-
return { method: request.method ?? "", route: template ?? stripQueryString(rawUrl) };
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
// src/timing/timing.interceptor.ts
|
|
616
|
-
var DEFAULT_SUCCESS_STATUS = 200;
|
|
617
|
-
var UNKNOWN_ERROR_STATUS = 500;
|
|
618
|
-
exports.TimingInterceptor = class TimingInterceptor {
|
|
619
|
-
/**
|
|
620
|
-
* @param options - Resolved core options; supplies `slowRequestThresholdMs`.
|
|
621
|
-
* @param sink - The bound timing sink; its `record` failures are swallowed.
|
|
622
|
-
* Injected with `@Optional()`: `BymaxCoreModule` binds no local default for
|
|
623
|
-
* this token on the sync path when the metrics bridge is not registered, so
|
|
624
|
-
* a consumer's own `BYMAX_TIMING_SINK` binding is not shadowed by one; when
|
|
625
|
-
* nothing resolves, this falls back to a no-op sink.
|
|
626
|
-
* @param clock - Monotonic clock seam; defaults to `performance.now()`, and
|
|
627
|
-
* is bound explicitly through {@link BYMAX_TIMING_CLOCK} so tests inject a
|
|
628
|
-
* stub advancing by controlled amounts.
|
|
629
|
-
* @param traceContext - Reads the active span's identifiers. Injected with
|
|
630
|
-
* `@Optional()` so this interceptor stays constructible on its own; when
|
|
631
|
-
* nothing resolves, a no-op resolves no trace and the sample simply omits
|
|
632
|
-
* the fields.
|
|
633
|
-
*/
|
|
634
|
-
constructor(options, sink, clock = DEFAULT_MONOTONIC_CLOCK, traceContext) {
|
|
635
|
-
this.options = options;
|
|
636
|
-
this.clock = clock;
|
|
637
|
-
this.sink = sink ?? new NoopTimingSink();
|
|
638
|
-
this.traceContext = traceContext ?? new NoopTraceContextProvider();
|
|
639
|
-
}
|
|
640
|
-
/**
|
|
641
|
-
* Measure the handler chain and record exactly one sample per completed
|
|
642
|
-
* request, on the success path and on the error path alike.
|
|
643
|
-
*
|
|
644
|
-
* @param context - The execution context of the current request.
|
|
645
|
-
* @param next - The next handler in the chain.
|
|
646
|
-
* @returns The downstream response stream, unmodified beyond the measurement.
|
|
647
|
-
*/
|
|
648
|
-
intercept(context, next) {
|
|
649
|
-
if (context.getType() !== "http") {
|
|
650
|
-
return next.handle();
|
|
651
|
-
}
|
|
652
|
-
const start = this.clock.now();
|
|
653
|
-
const { method, route } = extractRequestInfo(context);
|
|
654
|
-
return next.handle().pipe(
|
|
655
|
-
rxjs.tap({
|
|
656
|
-
complete: () => {
|
|
657
|
-
this.recordSample(method, route, this.readSuccessStatus(context), start);
|
|
658
|
-
}
|
|
659
|
-
}),
|
|
660
|
-
rxjs.catchError((error) => {
|
|
661
|
-
this.recordSample(method, route, this.readErrorStatus(error), start);
|
|
662
|
-
return rxjs.throwError(() => error);
|
|
663
|
-
})
|
|
664
|
-
);
|
|
665
|
-
}
|
|
666
|
-
/**
|
|
667
|
-
* Read the final status code from the response object on the success path.
|
|
668
|
-
*
|
|
669
|
-
* @param context - The execution context of the current request.
|
|
670
|
-
* @returns The response's status code, or the default success status when absent.
|
|
671
|
-
*/
|
|
672
|
-
readSuccessStatus(context) {
|
|
673
|
-
const response = context.switchToHttp().getResponse();
|
|
674
|
-
return response.statusCode ?? DEFAULT_SUCCESS_STATUS;
|
|
675
|
-
}
|
|
676
|
-
/**
|
|
677
|
-
* Derive the final status code for an error that escaped the handler.
|
|
678
|
-
*
|
|
679
|
-
* @param error - The error propagated by the handler chain.
|
|
680
|
-
* @returns The `HttpException` status, or the generic 500 for anything else.
|
|
681
|
-
*/
|
|
682
|
-
readErrorStatus(error) {
|
|
683
|
-
return error instanceof common.HttpException ? error.getStatus() : UNKNOWN_ERROR_STATUS;
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Build the sample, compute the slow flag, and deliver it to the sink inside
|
|
687
|
-
* a try/catch that silences any failure: a throwing sink must never affect
|
|
688
|
-
* the request it is observing.
|
|
689
|
-
*
|
|
690
|
-
* @param method - HTTP method of the request.
|
|
691
|
-
* @param route - Route template of the request.
|
|
692
|
-
* @param statusCode - Final status code, success or error.
|
|
693
|
-
* @param start - Monotonic start timestamp captured before the handler ran.
|
|
694
|
-
*/
|
|
695
|
-
recordSample(method, route, statusCode, start) {
|
|
696
|
-
const durationMs = this.clock.now() - start;
|
|
697
|
-
const threshold = this.options.timing.slowRequestThresholdMs;
|
|
698
|
-
const slow = threshold !== void 0 && durationMs > threshold;
|
|
699
|
-
let trace;
|
|
700
|
-
try {
|
|
701
|
-
trace = this.traceContext.getTraceContext();
|
|
702
|
-
} catch {
|
|
703
|
-
}
|
|
704
|
-
try {
|
|
705
|
-
this.sink.record({
|
|
706
|
-
method,
|
|
707
|
-
route,
|
|
708
|
-
statusCode,
|
|
709
|
-
durationMs,
|
|
710
|
-
slow,
|
|
711
|
-
// Spread rather than assigned: an absent trace must leave the keys off
|
|
712
|
-
// the sample entirely, so a sink cannot mistake `undefined` for an id.
|
|
713
|
-
...trace !== void 0 ? { traceId: trace.traceId, spanId: trace.spanId } : {}
|
|
714
|
-
});
|
|
715
|
-
} catch {
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
};
|
|
719
|
-
exports.TimingInterceptor = __decorateClass([
|
|
720
|
-
common.Injectable(),
|
|
721
|
-
__decorateParam(0, common.Inject(BYMAX_CORE_OPTIONS)),
|
|
722
|
-
__decorateParam(1, common.Optional()),
|
|
723
|
-
__decorateParam(1, common.Inject(BYMAX_TIMING_SINK)),
|
|
724
|
-
__decorateParam(2, common.Inject(BYMAX_TIMING_CLOCK)),
|
|
725
|
-
__decorateParam(3, common.Optional()),
|
|
726
|
-
__decorateParam(3, common.Inject(BYMAX_TRACE_CONTEXT))
|
|
727
|
-
], exports.TimingInterceptor);
|
|
728
|
-
|
|
729
|
-
// src/passthrough.providers.ts
|
|
730
599
|
var PassThroughExceptionFilter = class {
|
|
731
600
|
constructor(adapterHost) {
|
|
732
601
|
this.adapterHost = adapterHost;
|
|
@@ -745,18 +614,6 @@ var PassThroughExceptionFilter = class {
|
|
|
745
614
|
PassThroughExceptionFilter = __decorateClass([
|
|
746
615
|
common.Catch()
|
|
747
616
|
], PassThroughExceptionFilter);
|
|
748
|
-
var PassThroughInterceptor = class {
|
|
749
|
-
/**
|
|
750
|
-
* Forward the request to the next handler unchanged.
|
|
751
|
-
*
|
|
752
|
-
* @param _context - The execution context; unused by a transparent forwarder.
|
|
753
|
-
* @param next - The next handler in the chain.
|
|
754
|
-
* @returns The downstream response stream, unmodified.
|
|
755
|
-
*/
|
|
756
|
-
intercept(_context, next) {
|
|
757
|
-
return next.handle();
|
|
758
|
-
}
|
|
759
|
-
};
|
|
760
617
|
function assertAsyncFeatureEnabled(feature, enabled) {
|
|
761
618
|
if (!enabled) {
|
|
762
619
|
throw new common.NotFoundException(
|
|
@@ -767,9 +624,6 @@ function assertAsyncFeatureEnabled(feature, enabled) {
|
|
|
767
624
|
function selectAsyncExceptionFilter(options, correlation, adapterHost) {
|
|
768
625
|
return options.envelope.enabled ? new exports.BymaxExceptionFilter(options, correlation, adapterHost) : new PassThroughExceptionFilter(adapterHost);
|
|
769
626
|
}
|
|
770
|
-
function selectAsyncTimingInterceptor(options, sink, clock) {
|
|
771
|
-
return options.timing.enabled ? new exports.TimingInterceptor(options, sink, clock) : new PassThroughInterceptor();
|
|
772
|
-
}
|
|
773
627
|
|
|
774
628
|
// src/discovery.ts
|
|
775
629
|
function labelFor(className, token) {
|
|
@@ -1225,12 +1079,12 @@ var TimingMetricsSink = class {
|
|
|
1225
1079
|
this.histogram = getOrCreateHistogram(promClient, registry);
|
|
1226
1080
|
}
|
|
1227
1081
|
/**
|
|
1228
|
-
* Record one
|
|
1082
|
+
* Record one closed request: increment the counter once and observe the
|
|
1229
1083
|
* duration in seconds, both under the bounded label set. Any failure is
|
|
1230
1084
|
* swallowed so a metrics backend problem can never break the request being
|
|
1231
1085
|
* observed.
|
|
1232
1086
|
*
|
|
1233
|
-
* @param sample - The timing sample for a
|
|
1087
|
+
* @param sample - The timing sample for a closed request, however it ended.
|
|
1234
1088
|
*/
|
|
1235
1089
|
record(sample) {
|
|
1236
1090
|
const labels = {
|
|
@@ -1282,6 +1136,148 @@ function buildMetricsTimingSinkProvider() {
|
|
|
1282
1136
|
};
|
|
1283
1137
|
}
|
|
1284
1138
|
|
|
1139
|
+
// src/timing/fastify-route.bridge.ts
|
|
1140
|
+
function bridgeFastifyRouteMetadata(adapter) {
|
|
1141
|
+
if (adapter?.getType?.() !== "fastify") {
|
|
1142
|
+
return false;
|
|
1143
|
+
}
|
|
1144
|
+
const instance = adapter.getInstance?.();
|
|
1145
|
+
if (!isFastifyInstance(instance)) {
|
|
1146
|
+
return false;
|
|
1147
|
+
}
|
|
1148
|
+
instance.addHook("onRequest", (request, _reply, done) => {
|
|
1149
|
+
if (request.raw !== void 0 && request.routeOptions !== void 0) {
|
|
1150
|
+
request.raw["routeOptions"] = request.routeOptions;
|
|
1151
|
+
}
|
|
1152
|
+
done();
|
|
1153
|
+
});
|
|
1154
|
+
return true;
|
|
1155
|
+
}
|
|
1156
|
+
function isFastifyInstance(instance) {
|
|
1157
|
+
return typeof instance?.addHook === "function";
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
// src/timing/request-info.accessor.ts
|
|
1161
|
+
var UNMATCHED_ROUTE = "<unmatched>";
|
|
1162
|
+
function readExpressTemplate(request) {
|
|
1163
|
+
const path = request.route?.path;
|
|
1164
|
+
return path === void 0 ? void 0 : `${request.baseUrl ?? ""}${path}`;
|
|
1165
|
+
}
|
|
1166
|
+
function readFastifyTemplate(request) {
|
|
1167
|
+
return request.routeOptions?.url;
|
|
1168
|
+
}
|
|
1169
|
+
function readRequestInfo(request) {
|
|
1170
|
+
const template = readExpressTemplate(request) ?? readFastifyTemplate(request);
|
|
1171
|
+
return { method: request.method ?? "", route: template ?? UNMATCHED_ROUTE };
|
|
1172
|
+
}
|
|
1173
|
+
function extractRequestInfo(context) {
|
|
1174
|
+
return readRequestInfo(context.switchToHttp().getRequest());
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
// src/timing/timing.sample.ts
|
|
1178
|
+
function readTraceContext(traceContext) {
|
|
1179
|
+
try {
|
|
1180
|
+
return traceContext.getTraceContext();
|
|
1181
|
+
} catch {
|
|
1182
|
+
return void 0;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
function buildTimingSample(input) {
|
|
1186
|
+
const { method, route, statusCode, durationMs, threshold, trace } = input;
|
|
1187
|
+
return {
|
|
1188
|
+
method,
|
|
1189
|
+
route,
|
|
1190
|
+
statusCode,
|
|
1191
|
+
durationMs,
|
|
1192
|
+
slow: threshold !== void 0 && durationMs > threshold,
|
|
1193
|
+
// Spread rather than assigned: an absent trace must leave the keys off the
|
|
1194
|
+
// sample entirely, so a sink cannot mistake `undefined` for an id.
|
|
1195
|
+
...trace !== void 0 ? { traceId: trace.traceId, spanId: trace.spanId } : {}
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
// src/timing/timing.middleware.ts
|
|
1200
|
+
exports.BymaxTimingMiddleware = class BymaxTimingMiddleware {
|
|
1201
|
+
/**
|
|
1202
|
+
* @param options - The resolved options, read for the slow-request threshold.
|
|
1203
|
+
* @param sink - The bound timing sink; its failures are swallowed, because a
|
|
1204
|
+
* throwing sink must never affect the request it is observing.
|
|
1205
|
+
* @param clock - Monotonic clock seam, so a test advances time by controlled
|
|
1206
|
+
* amounts instead of sleeping.
|
|
1207
|
+
* @param traceContext - Reads the active span's identifiers. Optional: the
|
|
1208
|
+
* middleware stays constructible on its own, and a no-op resolves nothing.
|
|
1209
|
+
*/
|
|
1210
|
+
constructor(options, sink, clock = DEFAULT_MONOTONIC_CLOCK, traceContext) {
|
|
1211
|
+
this.options = options;
|
|
1212
|
+
this.clock = clock;
|
|
1213
|
+
this.sink = sink ?? new NoopTimingSink();
|
|
1214
|
+
this.traceContext = traceContext ?? new NoopTraceContextProvider();
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* Start the measurement and arrange for the sample to be recorded once the
|
|
1218
|
+
* connection closes, then hand the request straight on.
|
|
1219
|
+
*
|
|
1220
|
+
* The route is read inside the listener rather than here: at middleware time
|
|
1221
|
+
* the router has not matched yet, so `req.route` is still empty. By the time
|
|
1222
|
+
* the connection closes it is populated — including for a request a guard
|
|
1223
|
+
* rejected, since matching happens before guards run.
|
|
1224
|
+
*
|
|
1225
|
+
* The trace lookup is tried in the live context first and in this moment's
|
|
1226
|
+
* captured context second; see this file's header for the measurements that
|
|
1227
|
+
* decided that order.
|
|
1228
|
+
*
|
|
1229
|
+
* @param request - The framework request object.
|
|
1230
|
+
* @param response - The framework response object.
|
|
1231
|
+
* @param next - Continues the chain; called synchronously and unconditionally.
|
|
1232
|
+
*/
|
|
1233
|
+
use(request, response, next) {
|
|
1234
|
+
const start = this.clock.now();
|
|
1235
|
+
const readCapturedTrace = async_hooks.AsyncResource.bind(() => readTraceContext(this.traceContext));
|
|
1236
|
+
response.on("close", () => {
|
|
1237
|
+
this.record(
|
|
1238
|
+
request,
|
|
1239
|
+
response,
|
|
1240
|
+
start,
|
|
1241
|
+
readTraceContext(this.traceContext) ?? readCapturedTrace()
|
|
1242
|
+
);
|
|
1243
|
+
});
|
|
1244
|
+
next();
|
|
1245
|
+
}
|
|
1246
|
+
/**
|
|
1247
|
+
* Build the sample and hand it to the sink, guarding both steps.
|
|
1248
|
+
*
|
|
1249
|
+
* @param request - The framework request object.
|
|
1250
|
+
* @param response - The framework response object.
|
|
1251
|
+
* @param start - Monotonic timestamp captured before the chain ran.
|
|
1252
|
+
* @param trace - The span identifiers already resolved by the caller, which
|
|
1253
|
+
* owns the choice of which context to read them from.
|
|
1254
|
+
*/
|
|
1255
|
+
record(request, response, start, trace) {
|
|
1256
|
+
const { method, route } = readRequestInfo(request);
|
|
1257
|
+
const sample = buildTimingSample({
|
|
1258
|
+
method,
|
|
1259
|
+
route,
|
|
1260
|
+
statusCode: response.statusCode ?? 0,
|
|
1261
|
+
durationMs: this.clock.now() - start,
|
|
1262
|
+
threshold: this.options.timing.slowRequestThresholdMs,
|
|
1263
|
+
trace
|
|
1264
|
+
});
|
|
1265
|
+
try {
|
|
1266
|
+
this.sink.record(sample);
|
|
1267
|
+
} catch {
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
exports.BymaxTimingMiddleware = __decorateClass([
|
|
1272
|
+
common.Injectable(),
|
|
1273
|
+
__decorateParam(0, common.Inject(BYMAX_CORE_OPTIONS)),
|
|
1274
|
+
__decorateParam(1, common.Optional()),
|
|
1275
|
+
__decorateParam(1, common.Inject(BYMAX_TIMING_SINK)),
|
|
1276
|
+
__decorateParam(2, common.Inject(BYMAX_TIMING_CLOCK)),
|
|
1277
|
+
__decorateParam(3, common.Optional()),
|
|
1278
|
+
__decorateParam(3, common.Inject(BYMAX_TRACE_CONTEXT))
|
|
1279
|
+
], exports.BymaxTimingMiddleware);
|
|
1280
|
+
|
|
1285
1281
|
// src/core.module.ts
|
|
1286
1282
|
var {
|
|
1287
1283
|
ConfigurableModuleClass: BymaxCoreModuleBase,
|
|
@@ -1300,7 +1296,7 @@ function buildSyncProviders(resolved) {
|
|
|
1300
1296
|
providers.push({ provide: core.APP_FILTER, useClass: exports.BymaxExceptionFilter });
|
|
1301
1297
|
}
|
|
1302
1298
|
if (resolved.timing.enabled) {
|
|
1303
|
-
providers.push(
|
|
1299
|
+
providers.push(exports.BymaxTimingMiddleware);
|
|
1304
1300
|
}
|
|
1305
1301
|
if (resolved.health.enabled) {
|
|
1306
1302
|
providers.push(HealthService);
|
|
@@ -1337,11 +1333,6 @@ function buildAsyncSlots() {
|
|
|
1337
1333
|
{ token: BYMAX_CORRELATION_PROVIDER, optional: true },
|
|
1338
1334
|
core.HttpAdapterHost
|
|
1339
1335
|
]
|
|
1340
|
-
},
|
|
1341
|
-
{
|
|
1342
|
-
provide: core.APP_INTERCEPTOR,
|
|
1343
|
-
useFactory: (options, sink, clock) => selectAsyncTimingInterceptor(options, sink, clock),
|
|
1344
|
-
inject: [BYMAX_CORE_OPTIONS, BYMAX_TIMING_SINK, BYMAX_TIMING_CLOCK]
|
|
1345
1336
|
}
|
|
1346
1337
|
];
|
|
1347
1338
|
}
|
|
@@ -1356,6 +1347,77 @@ function augmentModule(base, providers, controllers, exportTokens = [BYMAX_CORE_
|
|
|
1356
1347
|
};
|
|
1357
1348
|
}
|
|
1358
1349
|
exports.BymaxCoreModule = class BymaxCoreModule extends BymaxCoreModuleBase {
|
|
1350
|
+
/**
|
|
1351
|
+
* @param options - The resolved snapshot, read to decide whether the timing
|
|
1352
|
+
* middleware is applied at all.
|
|
1353
|
+
*/
|
|
1354
|
+
constructor(resolved, adapterHost) {
|
|
1355
|
+
super();
|
|
1356
|
+
this.resolved = resolved;
|
|
1357
|
+
this.adapterHost = adapterHost;
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Apply the request-timing middleware to every route.
|
|
1361
|
+
*
|
|
1362
|
+
* Middleware rather than the interceptor this replaced, because guards run
|
|
1363
|
+
* before interceptors: a request rejected by an authentication, authorization
|
|
1364
|
+
* or throttling guard never reached the interceptor, and one matching no
|
|
1365
|
+
* route never reached a controller at all. A deployment could therefore be
|
|
1366
|
+
* under a credential-stuffing run — a flood of 401s — with a flat error
|
|
1367
|
+
* graph, which is why this is a security fix rather than a metrics
|
|
1368
|
+
* improvement.
|
|
1369
|
+
*
|
|
1370
|
+
* Applied on both registration paths, and only when timing is enabled: the
|
|
1371
|
+
* middleware is the sole recorder now, since a second one would count every
|
|
1372
|
+
* matched request twice.
|
|
1373
|
+
*
|
|
1374
|
+
* There is no single pattern that covers both adapters, which is the whole
|
|
1375
|
+
* reason this reads the adapter first. Measured, requesting the root, a
|
|
1376
|
+
* parameterised route, a nested path and an unmatched path, with and without
|
|
1377
|
+
* `setGlobalPrefix('api')`:
|
|
1378
|
+
*
|
|
1379
|
+
* | `forRoutes(...)` | Express | Fastify |
|
|
1380
|
+
* | ---------------- | ---------------- | -------------------- |
|
|
1381
|
+
* | `'*splat'` | skips the root | — |
|
|
1382
|
+
* | `'{*splat}'` | skips `/api` | every path |
|
|
1383
|
+
* | `'/'` | every path | matches `/` only |
|
|
1384
|
+
*
|
|
1385
|
+
* On Express `'/'` is a mount and matches everything beneath whatever prefix
|
|
1386
|
+
* it is scoped to, while `'{*splat}'` — the form Nest 11's migration guide
|
|
1387
|
+
* prescribes for "all routes" — stops matching the prefixed root once an
|
|
1388
|
+
* application calls `setGlobalPrefix`. That was reported as nest#14520 and
|
|
1389
|
+
* fixed by nest#14522, whose regression test covers Fastify; on
|
|
1390
|
+
* `@nestjs/core` 11.1.28 with the Express adapter the prefixed root still
|
|
1391
|
+
* reaches no middleware while resolving to `200`.
|
|
1392
|
+
*
|
|
1393
|
+
* On Fastify the same `'/'` is an exact match rather than a mount — one of
|
|
1394
|
+
* three requests reached the middleware — so the wildcard is the only form
|
|
1395
|
+
* that works there.
|
|
1396
|
+
*
|
|
1397
|
+
* A recorder that quietly omits one route is the same class of defect this
|
|
1398
|
+
* middleware exists to fix, so each adapter gets the form that omits none.
|
|
1399
|
+
*
|
|
1400
|
+
* Fastify also needs {@link bridgeFastifyRouteMetadata}, because middie hands
|
|
1401
|
+
* middleware the raw request, which carries no route metadata at all; see
|
|
1402
|
+
* that file for why the label would otherwise be `<unmatched>` for every
|
|
1403
|
+
* request.
|
|
1404
|
+
*
|
|
1405
|
+
* One limit stays and is documented in the README: Nest scopes module
|
|
1406
|
+
* middleware to the global prefix, so with `setGlobalPrefix('api')` a request
|
|
1407
|
+
* to `/nope` — outside the prefix entirely — reaches no middleware and is not
|
|
1408
|
+
* recorded, while `/api/nope` is. No `forRoutes` argument changes that, and
|
|
1409
|
+
* nothing this module can register reaches outside its own scope.
|
|
1410
|
+
*
|
|
1411
|
+
* @param consumer - Nest's middleware consumer.
|
|
1412
|
+
*/
|
|
1413
|
+
configure(consumer) {
|
|
1414
|
+
if (!this.resolved.timing.enabled) {
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1417
|
+
const adapter = this.adapterHost?.httpAdapter;
|
|
1418
|
+
const onFastify = bridgeFastifyRouteMetadata(adapter);
|
|
1419
|
+
consumer.apply(exports.BymaxTimingMiddleware).forRoutes(onFastify ? "{*splat}" : "/");
|
|
1420
|
+
}
|
|
1359
1421
|
/**
|
|
1360
1422
|
* Register the module synchronously. Options are known now, so disabled
|
|
1361
1423
|
* features are omitted from the providers and controllers arrays and the
|
|
@@ -1381,7 +1443,7 @@ exports.BymaxCoreModule = class BymaxCoreModule extends BymaxCoreModuleBase {
|
|
|
1381
1443
|
exportTokens.push(BYMAX_TIMING_SINK);
|
|
1382
1444
|
}
|
|
1383
1445
|
}
|
|
1384
|
-
const scansProviders = resolved.health.enabled && resolved.health.autoDiscover || resolved.metrics.enabled;
|
|
1446
|
+
const scansProviders = resolved.health.enabled && resolved.health.autoDiscover || resolved.metrics.enabled || resolved.openapi.enabled;
|
|
1385
1447
|
const imports = scansProviders ? [core.DiscoveryModule] : [];
|
|
1386
1448
|
return augmentModule(
|
|
1387
1449
|
super.forRoot(options),
|
|
@@ -1420,6 +1482,11 @@ exports.BymaxCoreModule = class BymaxCoreModule extends BymaxCoreModuleBase {
|
|
|
1420
1482
|
},
|
|
1421
1483
|
...buildDefaultProviders(),
|
|
1422
1484
|
...buildAsyncSlots(),
|
|
1485
|
+
// Registered unconditionally, applied conditionally: the class must be
|
|
1486
|
+
// resolvable before `configure` can apply it, and whether timing is
|
|
1487
|
+
// enabled is unknown until the consumer's factory has run. An unapplied
|
|
1488
|
+
// middleware provider costs one construction and observes nothing.
|
|
1489
|
+
exports.BymaxTimingMiddleware,
|
|
1423
1490
|
HealthService,
|
|
1424
1491
|
buildMetricsRegistryProvider(),
|
|
1425
1492
|
buildMetricsTimingSinkProvider(),
|
|
@@ -1436,8 +1503,114 @@ exports.BymaxCoreModule = class BymaxCoreModule extends BymaxCoreModuleBase {
|
|
|
1436
1503
|
}
|
|
1437
1504
|
};
|
|
1438
1505
|
exports.BymaxCoreModule = __decorateClass([
|
|
1439
|
-
common.Module({})
|
|
1506
|
+
common.Module({}),
|
|
1507
|
+
__decorateParam(0, common.Inject(BYMAX_CORE_OPTIONS)),
|
|
1508
|
+
__decorateParam(1, common.Optional()),
|
|
1509
|
+
__decorateParam(1, common.Inject(core.HttpAdapterHost))
|
|
1440
1510
|
], exports.BymaxCoreModule);
|
|
1511
|
+
var DEFAULT_SUCCESS_STATUS = 200;
|
|
1512
|
+
var UNKNOWN_ERROR_STATUS = 500;
|
|
1513
|
+
exports.TimingInterceptor = class TimingInterceptor {
|
|
1514
|
+
/**
|
|
1515
|
+
* @param options - Resolved core options; supplies `slowRequestThresholdMs`.
|
|
1516
|
+
* @param sink - The bound timing sink; its `record` failures are swallowed.
|
|
1517
|
+
* Injected with `@Optional()`: `BymaxCoreModule` binds no local default for
|
|
1518
|
+
* this token on the sync path when the metrics bridge is not registered, so
|
|
1519
|
+
* a consumer's own `BYMAX_TIMING_SINK` binding is not shadowed by one; when
|
|
1520
|
+
* nothing resolves, this falls back to a no-op sink.
|
|
1521
|
+
* @param clock - Monotonic clock seam; defaults to `performance.now()`, and
|
|
1522
|
+
* is bound explicitly through {@link BYMAX_TIMING_CLOCK} so tests inject a
|
|
1523
|
+
* stub advancing by controlled amounts.
|
|
1524
|
+
* @param traceContext - Reads the active span's identifiers. Injected with
|
|
1525
|
+
* `@Optional()` so this interceptor stays constructible on its own; when
|
|
1526
|
+
* nothing resolves, a no-op resolves no trace and the sample simply omits
|
|
1527
|
+
* the fields.
|
|
1528
|
+
*/
|
|
1529
|
+
constructor(options, sink, clock = DEFAULT_MONOTONIC_CLOCK, traceContext) {
|
|
1530
|
+
this.options = options;
|
|
1531
|
+
this.clock = clock;
|
|
1532
|
+
this.sink = sink ?? new NoopTimingSink();
|
|
1533
|
+
this.traceContext = traceContext ?? new NoopTraceContextProvider();
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* Measure the handler chain and record exactly one sample per completed
|
|
1537
|
+
* request, on the success path and on the error path alike.
|
|
1538
|
+
*
|
|
1539
|
+
* @param context - The execution context of the current request.
|
|
1540
|
+
* @param next - The next handler in the chain.
|
|
1541
|
+
* @returns The downstream response stream, unmodified beyond the measurement.
|
|
1542
|
+
*/
|
|
1543
|
+
intercept(context, next) {
|
|
1544
|
+
if (context.getType() !== "http") {
|
|
1545
|
+
return next.handle();
|
|
1546
|
+
}
|
|
1547
|
+
const start = this.clock.now();
|
|
1548
|
+
const { method, route } = extractRequestInfo(context);
|
|
1549
|
+
return next.handle().pipe(
|
|
1550
|
+
rxjs.tap({
|
|
1551
|
+
complete: () => {
|
|
1552
|
+
this.recordSample(method, route, this.readSuccessStatus(context), start);
|
|
1553
|
+
}
|
|
1554
|
+
}),
|
|
1555
|
+
rxjs.catchError((error) => {
|
|
1556
|
+
this.recordSample(method, route, this.readErrorStatus(error), start);
|
|
1557
|
+
return rxjs.throwError(() => error);
|
|
1558
|
+
})
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* Read the final status code from the response object on the success path.
|
|
1563
|
+
*
|
|
1564
|
+
* @param context - The execution context of the current request.
|
|
1565
|
+
* @returns The response's status code, or the default success status when absent.
|
|
1566
|
+
*/
|
|
1567
|
+
readSuccessStatus(context) {
|
|
1568
|
+
const response = context.switchToHttp().getResponse();
|
|
1569
|
+
return response.statusCode ?? DEFAULT_SUCCESS_STATUS;
|
|
1570
|
+
}
|
|
1571
|
+
/**
|
|
1572
|
+
* Derive the final status code for an error that escaped the handler.
|
|
1573
|
+
*
|
|
1574
|
+
* @param error - The error propagated by the handler chain.
|
|
1575
|
+
* @returns The `HttpException` status, or the generic 500 for anything else.
|
|
1576
|
+
*/
|
|
1577
|
+
readErrorStatus(error) {
|
|
1578
|
+
return error instanceof common.HttpException ? error.getStatus() : UNKNOWN_ERROR_STATUS;
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Build the sample and deliver it to the sink inside a try/catch that
|
|
1582
|
+
* silences any failure: a throwing sink must never affect the request it is
|
|
1583
|
+
* observing.
|
|
1584
|
+
*
|
|
1585
|
+
* @param method - HTTP method of the request.
|
|
1586
|
+
* @param route - Route template of the request.
|
|
1587
|
+
* @param statusCode - Final status code, success or error.
|
|
1588
|
+
* @param start - Monotonic start timestamp captured before the handler ran.
|
|
1589
|
+
*/
|
|
1590
|
+
recordSample(method, route, statusCode, start) {
|
|
1591
|
+
const sample = buildTimingSample({
|
|
1592
|
+
method,
|
|
1593
|
+
route,
|
|
1594
|
+
statusCode,
|
|
1595
|
+
durationMs: this.clock.now() - start,
|
|
1596
|
+
threshold: this.options.timing.slowRequestThresholdMs,
|
|
1597
|
+
trace: readTraceContext(this.traceContext)
|
|
1598
|
+
});
|
|
1599
|
+
try {
|
|
1600
|
+
this.sink.record(sample);
|
|
1601
|
+
} catch {
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
};
|
|
1605
|
+
exports.TimingInterceptor = __decorateClass([
|
|
1606
|
+
common.Injectable(),
|
|
1607
|
+
__decorateParam(0, common.Inject(BYMAX_CORE_OPTIONS)),
|
|
1608
|
+
__decorateParam(1, common.Optional()),
|
|
1609
|
+
__decorateParam(1, common.Inject(BYMAX_TIMING_SINK)),
|
|
1610
|
+
__decorateParam(2, common.Inject(BYMAX_TIMING_CLOCK)),
|
|
1611
|
+
__decorateParam(3, common.Optional()),
|
|
1612
|
+
__decorateParam(3, common.Inject(BYMAX_TRACE_CONTEXT))
|
|
1613
|
+
], exports.TimingInterceptor);
|
|
1441
1614
|
|
|
1442
1615
|
exports.BYMAX_BAD_GATEWAY = BYMAX_BAD_GATEWAY;
|
|
1443
1616
|
exports.BYMAX_BAD_REQUEST = BYMAX_BAD_REQUEST;
|
|
@@ -1461,5 +1634,6 @@ exports.BYMAX_UNAUTHORIZED = BYMAX_UNAUTHORIZED;
|
|
|
1461
1634
|
exports.BYMAX_UNPROCESSABLE_ENTITY = BYMAX_UNPROCESSABLE_ENTITY;
|
|
1462
1635
|
exports.BYMAX_UNSUPPORTED_MEDIA_TYPE = BYMAX_UNSUPPORTED_MEDIA_TYPE;
|
|
1463
1636
|
exports.BYMAX_VALIDATION_FAILED = BYMAX_VALIDATION_FAILED;
|
|
1637
|
+
exports.UNMATCHED_ROUTE = UNMATCHED_ROUTE;
|
|
1464
1638
|
exports.buildErrorEnvelope = buildErrorEnvelope;
|
|
1465
1639
|
exports.codeForStatus = codeForStatus;
|