@bymax-one/nest-logger 1.4.12 → 1.4.14
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 +144 -1
- package/README.md +25 -12
- package/dist/server/index.cjs +356 -126
- package/dist/server/index.d.cts +182 -106
- package/dist/server/index.d.ts +182 -106
- package/dist/server/index.mjs +357 -127
- package/package.json +2 -1
package/dist/server/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
import { fileURLToPath } from 'url';
|
|
3
3
|
import { Injectable, Inject, HttpStatus, Catch, ConfigurableModuleBuilder, Module, HttpException, SetMetadata } from '@nestjs/common';
|
|
4
|
-
import { APP_INTERCEPTOR, APP_FILTER } from '@nestjs/core';
|
|
4
|
+
import { HttpAdapterHost, APP_INTERCEPTOR, APP_FILTER } from '@nestjs/core';
|
|
5
5
|
import { types } from 'util';
|
|
6
6
|
import { tap, catchError, throwError } from 'rxjs';
|
|
7
7
|
import { randomUUID } from 'crypto';
|
|
@@ -28,7 +28,8 @@ var DEFAULT_HTTP = {
|
|
|
28
28
|
shouldCaptureExceptions: true,
|
|
29
29
|
shouldGenerateRequestId: true,
|
|
30
30
|
excludePaths: [/^\/health$/, /^\/health\/live$/, /^\/health\/ready$/, /^\/metrics$/],
|
|
31
|
-
tenantIdHeader: "x-tenant-id"
|
|
31
|
+
tenantIdHeader: "x-tenant-id",
|
|
32
|
+
tenantIdResolver: void 0
|
|
32
33
|
};
|
|
33
34
|
var DEFAULT_OTEL = {
|
|
34
35
|
shouldAutoInjectTraceContext: true,
|
|
@@ -293,7 +294,14 @@ function validateHttp(options) {
|
|
|
293
294
|
}
|
|
294
295
|
});
|
|
295
296
|
}
|
|
296
|
-
|
|
297
|
+
const tenantIdHeader = http["tenantIdHeader"];
|
|
298
|
+
if (tenantIdHeader !== void 0 && tenantIdHeader !== false && !isNonEmptyString(tenantIdHeader)) {
|
|
299
|
+
reject("http.tenantIdHeader", "must be a non-empty string or false", tenantIdHeader);
|
|
300
|
+
}
|
|
301
|
+
const tenantIdResolver = http["tenantIdResolver"];
|
|
302
|
+
if (tenantIdResolver !== void 0 && typeof tenantIdResolver !== "function") {
|
|
303
|
+
reject("http.tenantIdResolver", "must be a function", tenantIdResolver);
|
|
304
|
+
}
|
|
297
305
|
}
|
|
298
306
|
function validateOtel(options) {
|
|
299
307
|
const otel = checkGroup("otel", options.otel);
|
|
@@ -962,6 +970,86 @@ DefaultStdoutDestination = __decorateClass([
|
|
|
962
970
|
Injectable()
|
|
963
971
|
], DefaultStdoutDestination);
|
|
964
972
|
|
|
973
|
+
// src/server/utils/exposed-client-error.util.ts
|
|
974
|
+
var MIN_CLIENT_STATUS = 400;
|
|
975
|
+
var MAX_CLIENT_STATUS = 499;
|
|
976
|
+
var FIXED_MESSAGES = /* @__PURE__ */ new Map([
|
|
977
|
+
["encoding.unsupported", "unsupported content encoding"],
|
|
978
|
+
["charset.unsupported", "unsupported charset"]
|
|
979
|
+
]);
|
|
980
|
+
var FIXED_LOG_MESSAGES = new Map([
|
|
981
|
+
...FIXED_MESSAGES,
|
|
982
|
+
["entity.parse.failed", "request body could not be parsed"],
|
|
983
|
+
["entity.verify.failed", "request body verification failed"]
|
|
984
|
+
]);
|
|
985
|
+
function readExposedClientError(value) {
|
|
986
|
+
try {
|
|
987
|
+
const candidate = value;
|
|
988
|
+
const status = candidate.status ?? candidate.statusCode;
|
|
989
|
+
const message = value.message;
|
|
990
|
+
const isClientStatus = typeof status === "number" && Number.isInteger(status) && status >= MIN_CLIENT_STATUS && status <= MAX_CLIENT_STATUS;
|
|
991
|
+
if (candidate.expose !== true || !isClientStatus || typeof message !== "string")
|
|
992
|
+
return void 0;
|
|
993
|
+
const type = candidate.type;
|
|
994
|
+
const sent = FIXED_MESSAGES.get(type) ?? message;
|
|
995
|
+
const logged = FIXED_LOG_MESSAGES.get(type);
|
|
996
|
+
return logged === void 0 ? { status, message: sent, logMessage: sent } : { status, message: sent, logMessage: logged, bodyParserType: type };
|
|
997
|
+
} catch {
|
|
998
|
+
return void 0;
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
// src/server/utils/http-log-state.util.ts
|
|
1003
|
+
var HTTP_REDIRECT_MIN = 300;
|
|
1004
|
+
var HTTP_CLIENT_ERROR_MIN = 400;
|
|
1005
|
+
var HTTP_SERVER_ERROR_MIN = 500;
|
|
1006
|
+
var UNKNOWN_USER_AGENT = "unknown";
|
|
1007
|
+
var RECORDER_ACTIVE = /* @__PURE__ */ Symbol("bymax-one/nest-logger:recorder");
|
|
1008
|
+
var RECORDED_ERROR = /* @__PURE__ */ Symbol("bymax-one/nest-logger:error");
|
|
1009
|
+
var PARSER_ERROR = /* @__PURE__ */ Symbol("bymax-one/nest-logger:parser-error");
|
|
1010
|
+
var OWN_REQUEST_ID = /* @__PURE__ */ Symbol("bymax-one/nest-logger:request-id");
|
|
1011
|
+
var CORRELATION_OPENED = /* @__PURE__ */ Symbol("bymax-one/nest-logger:correlation");
|
|
1012
|
+
function markRecorderActive(req) {
|
|
1013
|
+
Reflect.set(req, RECORDER_ACTIVE, true);
|
|
1014
|
+
}
|
|
1015
|
+
function isRecorderActive(req) {
|
|
1016
|
+
return Reflect.get(req, RECORDER_ACTIVE) === true;
|
|
1017
|
+
}
|
|
1018
|
+
function markCorrelationOpened(req) {
|
|
1019
|
+
Reflect.set(req, CORRELATION_OPENED, true);
|
|
1020
|
+
}
|
|
1021
|
+
function isCorrelationOpened(req) {
|
|
1022
|
+
return Reflect.get(req, CORRELATION_OPENED) === true;
|
|
1023
|
+
}
|
|
1024
|
+
function markOwnRequestId(req, requestId) {
|
|
1025
|
+
Reflect.set(req, OWN_REQUEST_ID, requestId);
|
|
1026
|
+
}
|
|
1027
|
+
function readOwnRequestId(req) {
|
|
1028
|
+
return Reflect.get(req, OWN_REQUEST_ID);
|
|
1029
|
+
}
|
|
1030
|
+
function recordError(req, error) {
|
|
1031
|
+
Reflect.set(req, RECORDED_ERROR, { error });
|
|
1032
|
+
}
|
|
1033
|
+
function readRecordedError(req) {
|
|
1034
|
+
return Reflect.get(req, RECORDED_ERROR);
|
|
1035
|
+
}
|
|
1036
|
+
function recordParserError(req, error) {
|
|
1037
|
+
Reflect.set(req, PARSER_ERROR, error);
|
|
1038
|
+
}
|
|
1039
|
+
function readRecordedParserError(req) {
|
|
1040
|
+
return Reflect.get(req, PARSER_ERROR);
|
|
1041
|
+
}
|
|
1042
|
+
function toError(thrown) {
|
|
1043
|
+
return isError(thrown) ? thrown : new Error(String(thrown));
|
|
1044
|
+
}
|
|
1045
|
+
function readUserId(req) {
|
|
1046
|
+
return req.user?.sub ?? req.user?.id;
|
|
1047
|
+
}
|
|
1048
|
+
function readUserAgent(req) {
|
|
1049
|
+
const raw = req.headers["user-agent"];
|
|
1050
|
+
return typeof raw === "string" ? raw : UNKNOWN_USER_AGENT;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
965
1053
|
// src/server/utils/normalize-url.util.ts
|
|
966
1054
|
var UUID_REGEX = /\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(?![0-9a-f])/gi;
|
|
967
1055
|
var ULID_REGEX = /\/[0-9A-HJKMNP-TV-Z]{26}(?![0-9A-HJKMNP-TV-Z])/g;
|
|
@@ -975,6 +1063,20 @@ function stripQueryString(url) {
|
|
|
975
1063
|
const queryStart = url.indexOf("?");
|
|
976
1064
|
return queryStart === -1 ? url : url.slice(0, queryStart);
|
|
977
1065
|
}
|
|
1066
|
+
function withoutRequestQuery(text, ...urls) {
|
|
1067
|
+
let result = text;
|
|
1068
|
+
for (const url of urls) {
|
|
1069
|
+
if (url === void 0) continue;
|
|
1070
|
+
const queryStart = url.indexOf("?");
|
|
1071
|
+
if (queryStart !== -1 && url.length - queryStart > 1) {
|
|
1072
|
+
result = result.split(url.slice(queryStart)).join("");
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
return result;
|
|
1076
|
+
}
|
|
1077
|
+
function withoutRequestQueryIfText(value, ...urls) {
|
|
1078
|
+
return typeof value === "string" ? withoutRequestQuery(value, ...urls) : value;
|
|
1079
|
+
}
|
|
978
1080
|
|
|
979
1081
|
// src/server/filters/http-exception.filter.ts
|
|
980
1082
|
var INTERNAL_ERROR_BODY = {
|
|
@@ -1004,9 +1106,15 @@ var HttpExceptionFilter = class {
|
|
|
1004
1106
|
const userId = req.user?.sub ?? req.user?.id;
|
|
1005
1107
|
const url = stripQueryString(req.url);
|
|
1006
1108
|
if (exception instanceof HttpException) {
|
|
1007
|
-
const
|
|
1008
|
-
const
|
|
1009
|
-
|
|
1109
|
+
const status = exception.getStatus();
|
|
1110
|
+
const parserError = readRecordedParserError(req);
|
|
1111
|
+
const metadata = {
|
|
1112
|
+
method: req.method,
|
|
1113
|
+
url,
|
|
1114
|
+
status,
|
|
1115
|
+
bodyParserType: parserError?.bodyParserType
|
|
1116
|
+
};
|
|
1117
|
+
if (status >= HttpStatus.INTERNAL_SERVER_ERROR) {
|
|
1010
1118
|
this.logger.errorStructured(
|
|
1011
1119
|
RESERVED_LOG_KEYS.HTTP_EXCEPTION_UNHANDLED,
|
|
1012
1120
|
sanitizeError(exception),
|
|
@@ -1016,24 +1124,65 @@ var HttpExceptionFilter = class {
|
|
|
1016
1124
|
} else {
|
|
1017
1125
|
this.logger.warnStructured(
|
|
1018
1126
|
RESERVED_LOG_KEYS.HTTP_EXCEPTION_HANDLED,
|
|
1019
|
-
exception.message,
|
|
1127
|
+
parserError?.logMessage ?? withoutRequestQuery(exception.message, req.originalUrl, req.url),
|
|
1020
1128
|
userId,
|
|
1021
1129
|
metadata
|
|
1022
1130
|
);
|
|
1023
1131
|
}
|
|
1024
|
-
res.status(
|
|
1132
|
+
res.status(status).json(exception.getResponse());
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
const clientError = readExposedClientError(exception);
|
|
1136
|
+
if (clientError !== void 0) {
|
|
1137
|
+
this.answerClientError(clientError, res, { method: req.method, url, userId }, [
|
|
1138
|
+
req.originalUrl,
|
|
1139
|
+
req.url
|
|
1140
|
+
]);
|
|
1025
1141
|
return;
|
|
1026
1142
|
}
|
|
1143
|
+
this.answerUnhandled(exception, res, { method: req.method, url, userId });
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Log an exposed client error as handled and answer it with its own status and message.
|
|
1147
|
+
*
|
|
1148
|
+
* @param clientError - The status and message the error exposes.
|
|
1149
|
+
* @param res - The response to write.
|
|
1150
|
+
* @param request - The request facts every entry carries.
|
|
1151
|
+
* @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
|
|
1152
|
+
*/
|
|
1153
|
+
answerClientError(clientError, res, request, rawUrls) {
|
|
1154
|
+
const { method, url, userId } = request;
|
|
1155
|
+
this.logger.warnStructured(
|
|
1156
|
+
RESERVED_LOG_KEYS.HTTP_EXCEPTION_HANDLED,
|
|
1157
|
+
withoutRequestQuery(clientError.logMessage, ...rawUrls),
|
|
1158
|
+
userId,
|
|
1159
|
+
{
|
|
1160
|
+
method,
|
|
1161
|
+
url,
|
|
1162
|
+
status: clientError.status,
|
|
1163
|
+
bodyParserType: clientError.bodyParserType
|
|
1164
|
+
}
|
|
1165
|
+
);
|
|
1166
|
+
res.status(clientError.status).json({
|
|
1167
|
+
statusCode: clientError.status,
|
|
1168
|
+
message: clientError.message
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Log any other non-`HttpException` as unhandled and answer it with the fixed 500 body.
|
|
1173
|
+
*
|
|
1174
|
+
* @param exception - The thrown value.
|
|
1175
|
+
* @param res - The response to write.
|
|
1176
|
+
* @param request - The request facts every entry carries.
|
|
1177
|
+
*/
|
|
1178
|
+
answerUnhandled(exception, res, request) {
|
|
1179
|
+
const { method, url, userId } = request;
|
|
1027
1180
|
const status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
1028
1181
|
this.logger.errorStructured(
|
|
1029
1182
|
RESERVED_LOG_KEYS.HTTP_EXCEPTION_UNHANDLED,
|
|
1030
1183
|
sanitizeError(exception),
|
|
1031
1184
|
userId,
|
|
1032
|
-
{
|
|
1033
|
-
method: req.method,
|
|
1034
|
-
url,
|
|
1035
|
-
status
|
|
1036
|
-
}
|
|
1185
|
+
{ method, url, status }
|
|
1037
1186
|
);
|
|
1038
1187
|
res.status(status).json(INTERNAL_ERROR_BODY);
|
|
1039
1188
|
}
|
|
@@ -1043,50 +1192,6 @@ HttpExceptionFilter = __decorateClass([
|
|
|
1043
1192
|
__decorateParam(0, Inject(PinoLoggerService))
|
|
1044
1193
|
], HttpExceptionFilter);
|
|
1045
1194
|
|
|
1046
|
-
// src/server/utils/http-log-state.util.ts
|
|
1047
|
-
var HTTP_REDIRECT_MIN = 300;
|
|
1048
|
-
var HTTP_CLIENT_ERROR_MIN = 400;
|
|
1049
|
-
var HTTP_SERVER_ERROR_MIN = 500;
|
|
1050
|
-
var UNKNOWN_USER_AGENT = "unknown";
|
|
1051
|
-
var RECORDER_ACTIVE = /* @__PURE__ */ Symbol("bymax-one/nest-logger:recorder");
|
|
1052
|
-
var RECORDED_ERROR = /* @__PURE__ */ Symbol("bymax-one/nest-logger:error");
|
|
1053
|
-
var OWN_REQUEST_ID = /* @__PURE__ */ Symbol("bymax-one/nest-logger:request-id");
|
|
1054
|
-
var CORRELATION_OPENED = /* @__PURE__ */ Symbol("bymax-one/nest-logger:correlation");
|
|
1055
|
-
function markRecorderActive(req) {
|
|
1056
|
-
Reflect.set(req, RECORDER_ACTIVE, true);
|
|
1057
|
-
}
|
|
1058
|
-
function isRecorderActive(req) {
|
|
1059
|
-
return Reflect.get(req, RECORDER_ACTIVE) === true;
|
|
1060
|
-
}
|
|
1061
|
-
function markCorrelationOpened(req) {
|
|
1062
|
-
Reflect.set(req, CORRELATION_OPENED, true);
|
|
1063
|
-
}
|
|
1064
|
-
function isCorrelationOpened(req) {
|
|
1065
|
-
return Reflect.get(req, CORRELATION_OPENED) === true;
|
|
1066
|
-
}
|
|
1067
|
-
function markOwnRequestId(req, requestId) {
|
|
1068
|
-
Reflect.set(req, OWN_REQUEST_ID, requestId);
|
|
1069
|
-
}
|
|
1070
|
-
function readOwnRequestId(req) {
|
|
1071
|
-
return Reflect.get(req, OWN_REQUEST_ID);
|
|
1072
|
-
}
|
|
1073
|
-
function recordError(req, error) {
|
|
1074
|
-
Reflect.set(req, RECORDED_ERROR, { error });
|
|
1075
|
-
}
|
|
1076
|
-
function readRecordedError(req) {
|
|
1077
|
-
return Reflect.get(req, RECORDED_ERROR);
|
|
1078
|
-
}
|
|
1079
|
-
function toError(thrown) {
|
|
1080
|
-
return isError(thrown) ? thrown : new Error(String(thrown));
|
|
1081
|
-
}
|
|
1082
|
-
function readUserId(req) {
|
|
1083
|
-
return req.user?.sub ?? req.user?.id;
|
|
1084
|
-
}
|
|
1085
|
-
function readUserAgent(req) {
|
|
1086
|
-
const raw = req.headers["user-agent"];
|
|
1087
|
-
return typeof raw === "string" ? raw : UNKNOWN_USER_AGENT;
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
1195
|
// src/server/utils/matches-exclude-path.util.ts
|
|
1091
1196
|
function matchesExcludePath(patterns, path2) {
|
|
1092
1197
|
return patterns.some((pattern) => {
|
|
@@ -1158,7 +1263,10 @@ var HttpLoggingInterceptor = class {
|
|
|
1158
1263
|
this.logSuccess(res.statusCode, method, normalizedUrl, userId, Date.now() - start);
|
|
1159
1264
|
}),
|
|
1160
1265
|
catchError((err) => {
|
|
1161
|
-
this.logError(err, method, normalizedUrl, userId, Date.now() - start
|
|
1266
|
+
this.logError(err, method, normalizedUrl, userId, Date.now() - start, [
|
|
1267
|
+
req.originalUrl,
|
|
1268
|
+
url
|
|
1269
|
+
]);
|
|
1162
1270
|
return throwError(() => err);
|
|
1163
1271
|
})
|
|
1164
1272
|
);
|
|
@@ -1214,16 +1322,17 @@ var HttpLoggingInterceptor = class {
|
|
|
1214
1322
|
}
|
|
1215
1323
|
/**
|
|
1216
1324
|
* Log a thrown completion: SERVER_ERROR (error level, with stack) for 5xx,
|
|
1217
|
-
* CLIENT_ERROR (warn level) for an `HttpException` below 500
|
|
1218
|
-
*
|
|
1325
|
+
* CLIENT_ERROR (warn level) for an `HttpException` below 500 or an exposed `http-errors`
|
|
1326
|
+
* client error. Any other non-`HttpException` maps to 500, so it is reported as a server error.
|
|
1219
1327
|
*
|
|
1220
1328
|
* @param err - The thrown value (anything).
|
|
1221
1329
|
* @param method - The HTTP method.
|
|
1222
1330
|
* @param url - The normalized request URL.
|
|
1223
1331
|
* @param userId - The acting user id, when known.
|
|
1224
1332
|
* @param duration - The elapsed time in milliseconds.
|
|
1333
|
+
* @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
|
|
1225
1334
|
*/
|
|
1226
|
-
logError(err, method, url, userId, duration) {
|
|
1335
|
+
logError(err, method, url, userId, duration, rawUrls) {
|
|
1227
1336
|
if (err instanceof HttpException) {
|
|
1228
1337
|
const statusCode = err.getStatus();
|
|
1229
1338
|
if (statusCode >= HTTP_SERVER_ERROR_MIN) {
|
|
@@ -1238,11 +1347,22 @@ var HttpLoggingInterceptor = class {
|
|
|
1238
1347
|
RESERVED_LOG_KEYS.HTTP_REQUEST_CLIENT_ERROR,
|
|
1239
1348
|
`${method} ${url} \u2192 ${statusCode}`,
|
|
1240
1349
|
userId,
|
|
1241
|
-
{
|
|
1350
|
+
{
|
|
1351
|
+
method,
|
|
1352
|
+
url,
|
|
1353
|
+
statusCode,
|
|
1354
|
+
duration,
|
|
1355
|
+
errorMessage: cutIfText(withoutRequestQueryIfText(err.message, ...rawUrls))
|
|
1356
|
+
}
|
|
1242
1357
|
);
|
|
1243
1358
|
}
|
|
1244
1359
|
return;
|
|
1245
1360
|
}
|
|
1361
|
+
const clientError = readExposedClientError(err);
|
|
1362
|
+
if (clientError !== void 0) {
|
|
1363
|
+
this.logClientError(clientError, method, url, userId, duration, rawUrls);
|
|
1364
|
+
return;
|
|
1365
|
+
}
|
|
1246
1366
|
this.logger.errorStructured(RESERVED_LOG_KEYS.HTTP_REQUEST_SERVER_ERROR, toError(err), userId, {
|
|
1247
1367
|
method,
|
|
1248
1368
|
url,
|
|
@@ -1250,6 +1370,31 @@ var HttpLoggingInterceptor = class {
|
|
|
1250
1370
|
duration
|
|
1251
1371
|
});
|
|
1252
1372
|
}
|
|
1373
|
+
/**
|
|
1374
|
+
* Log an exposed `http-errors` client error as CLIENT_ERROR at warn level.
|
|
1375
|
+
*
|
|
1376
|
+
* @param clientError - The status and message the error exposes.
|
|
1377
|
+
* @param method - The HTTP method.
|
|
1378
|
+
* @param url - The normalized request URL.
|
|
1379
|
+
* @param userId - The acting user id, when known.
|
|
1380
|
+
* @param duration - The elapsed time in milliseconds.
|
|
1381
|
+
* @param rawUrls - The request's own URLs, whose query string a logged message must not carry.
|
|
1382
|
+
*/
|
|
1383
|
+
logClientError(clientError, method, url, userId, duration, rawUrls) {
|
|
1384
|
+
this.logger.warnStructured(
|
|
1385
|
+
RESERVED_LOG_KEYS.HTTP_REQUEST_CLIENT_ERROR,
|
|
1386
|
+
`${method} ${url} \u2192 ${clientError.status}`,
|
|
1387
|
+
userId,
|
|
1388
|
+
{
|
|
1389
|
+
method,
|
|
1390
|
+
url,
|
|
1391
|
+
statusCode: clientError.status,
|
|
1392
|
+
duration,
|
|
1393
|
+
errorMessage: cutIfText(withoutRequestQueryIfText(clientError.logMessage, ...rawUrls)),
|
|
1394
|
+
bodyParserType: clientError.bodyParserType
|
|
1395
|
+
}
|
|
1396
|
+
);
|
|
1397
|
+
}
|
|
1253
1398
|
};
|
|
1254
1399
|
HttpLoggingInterceptor = __decorateClass([
|
|
1255
1400
|
Injectable(),
|
|
@@ -1414,6 +1559,9 @@ LogContextService = __decorateClass([
|
|
|
1414
1559
|
var REQUEST_ID_HEADER = "x-request-id";
|
|
1415
1560
|
var MAX_CORRELATION_ID_LENGTH = 256;
|
|
1416
1561
|
var CORRELATION_ID_CHARSET = /^[A-Za-z0-9._:/+=-]+$/;
|
|
1562
|
+
function settleLate(answer) {
|
|
1563
|
+
Promise.resolve(answer).catch(() => void 0);
|
|
1564
|
+
}
|
|
1417
1565
|
var INHERITABLE_CONTEXT_KEYS = ["requestId", "tenantId"];
|
|
1418
1566
|
function isAcceptableHeaderValue(value) {
|
|
1419
1567
|
return typeof value === "string" && value.length <= MAX_CORRELATION_ID_LENGTH && CORRELATION_ID_CHARSET.test(value);
|
|
@@ -1429,6 +1577,7 @@ var RequestIdMiddleware = class {
|
|
|
1429
1577
|
constructor(logContext, options) {
|
|
1430
1578
|
this.logContext = logContext;
|
|
1431
1579
|
this.tenantIdHeader = options.http.tenantIdHeader;
|
|
1580
|
+
this.tenantIdResolver = options.http.tenantIdResolver;
|
|
1432
1581
|
this.shouldGenerateRequestId = options.http.shouldGenerateRequestId;
|
|
1433
1582
|
}
|
|
1434
1583
|
/**
|
|
@@ -1543,18 +1692,36 @@ var RequestIdMiddleware = class {
|
|
|
1543
1692
|
return fromHeader ?? (this.shouldGenerateRequestId ? randomUUID() : void 0);
|
|
1544
1693
|
}
|
|
1545
1694
|
/**
|
|
1546
|
-
* The tenant id
|
|
1695
|
+
* The tenant id of the request, when it is acceptable.
|
|
1547
1696
|
*
|
|
1548
|
-
*
|
|
1549
|
-
*
|
|
1697
|
+
* From the host's resolver when it gave one, else from the configured header, else none. Never
|
|
1698
|
+
* minted: a tenant is an assertion about who the request belongs to, and inventing one would be a
|
|
1699
|
+
* fabricated claim rather than a correlation aid.
|
|
1550
1700
|
*
|
|
1551
1701
|
* @param req - The incoming request.
|
|
1552
1702
|
* @returns The tenant id, or `undefined`.
|
|
1553
1703
|
*/
|
|
1554
1704
|
resolveTenantId(req) {
|
|
1705
|
+
if (this.tenantIdResolver !== void 0)
|
|
1706
|
+
return this.resolvedTenantId(this.tenantIdResolver, req);
|
|
1707
|
+
if (this.tenantIdHeader === false) return void 0;
|
|
1555
1708
|
const raw = Reflect.get(req.headers, this.tenantIdHeader);
|
|
1556
1709
|
return isAcceptableHeaderValue(raw) ? raw : void 0;
|
|
1557
1710
|
}
|
|
1711
|
+
/**
|
|
1712
|
+
* The answer of the host's resolver, when it is acceptable. A resolver that throws gives none: a
|
|
1713
|
+
* log context never costs the request.
|
|
1714
|
+
*/
|
|
1715
|
+
resolvedTenantId(resolver, req) {
|
|
1716
|
+
try {
|
|
1717
|
+
const answer = resolver(req);
|
|
1718
|
+
if (isAcceptableHeaderValue(answer)) return answer;
|
|
1719
|
+
settleLate(answer);
|
|
1720
|
+
return void 0;
|
|
1721
|
+
} catch {
|
|
1722
|
+
return void 0;
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1558
1725
|
};
|
|
1559
1726
|
RequestIdMiddleware = __decorateClass([
|
|
1560
1727
|
Injectable(),
|
|
@@ -1581,6 +1748,16 @@ function applyRequestContext(app) {
|
|
|
1581
1748
|
});
|
|
1582
1749
|
}
|
|
1583
1750
|
|
|
1751
|
+
// src/server/middlewares/record-parser-error.middleware.ts
|
|
1752
|
+
function recordParserErrorMiddleware(error, req, _res, next) {
|
|
1753
|
+
try {
|
|
1754
|
+
const exposed = readExposedClientError(error);
|
|
1755
|
+
if (exposed?.bodyParserType !== void 0) recordParserError(req, exposed);
|
|
1756
|
+
} catch {
|
|
1757
|
+
}
|
|
1758
|
+
next(error);
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1584
1761
|
// src/server/constants/default-redact-paths.constants.ts
|
|
1585
1762
|
var REDACT_MAX_DEPTH = 4;
|
|
1586
1763
|
var WILDCARD_PREFIXES = Array.from(
|
|
@@ -1860,7 +2037,7 @@ function destinationToStream(destination, health) {
|
|
|
1860
2037
|
|
|
1861
2038
|
// src/server/utils/redact-error-parts.util.ts
|
|
1862
2039
|
function defineOwn(target, key, value) {
|
|
1863
|
-
Reflect.defineProperty(target, key, { value, enumerable: true });
|
|
2040
|
+
Reflect.defineProperty(target, key, { value, enumerable: true, writable: true });
|
|
1864
2041
|
}
|
|
1865
2042
|
var linkDepths = /* @__PURE__ */ new WeakMap();
|
|
1866
2043
|
var ERROR_TEXT_FIELDS = /* @__PURE__ */ new Set(["name", "message", "stack"]);
|
|
@@ -1956,27 +2133,46 @@ var REDACT_BUDGET_EXCEEDED = "[REDACTION_BUDGET_EXCEEDED]";
|
|
|
1956
2133
|
var REDACT_CIRCULAR = "[Circular]";
|
|
1957
2134
|
function resolveToJson(value, viaGet = false) {
|
|
1958
2135
|
let candidate = viaGet ? Reflect.get(value, "toJSON") : void 0;
|
|
2136
|
+
let accessor = false;
|
|
2137
|
+
let proxied = false;
|
|
1959
2138
|
let current = viaGet ? null : value;
|
|
1960
2139
|
while (current !== null) {
|
|
2140
|
+
proxied = proxied || types.isProxy(current);
|
|
1961
2141
|
const descriptor = Object.getOwnPropertyDescriptor(current, "toJSON");
|
|
1962
2142
|
if (descriptor !== void 0) {
|
|
2143
|
+
accessor = descriptor.get !== void 0;
|
|
1963
2144
|
candidate = descriptor.get === void 0 ? descriptor.value : descriptor.get.call(value);
|
|
1964
2145
|
break;
|
|
1965
2146
|
}
|
|
1966
2147
|
current = Object.getPrototypeOf(current);
|
|
1967
2148
|
}
|
|
1968
|
-
return {
|
|
2149
|
+
return {
|
|
2150
|
+
method: typeof candidate === "function" ? candidate : void 0,
|
|
2151
|
+
volatile: accessor || proxied
|
|
2152
|
+
};
|
|
2153
|
+
}
|
|
2154
|
+
function shadowHook(target) {
|
|
2155
|
+
Reflect.defineProperty(target, "toJSON", { value: void 0, configurable: true });
|
|
2156
|
+
}
|
|
2157
|
+
function withoutEnumerableKeys(clone) {
|
|
2158
|
+
for (const key of Object.keys(clone)) Reflect.deleteProperty(clone, key);
|
|
2159
|
+
}
|
|
2160
|
+
function discardWrite() {
|
|
1969
2161
|
}
|
|
1970
2162
|
function cloneError(error) {
|
|
1971
2163
|
const descriptors = Object.getOwnPropertyDescriptors(error);
|
|
1972
2164
|
for (const descriptor of Object.values(descriptors)) {
|
|
1973
2165
|
descriptor.configurable = true;
|
|
2166
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
2167
|
+
else descriptor.set ??= discardWrite;
|
|
1974
2168
|
}
|
|
1975
2169
|
const clone = Object.create(Object.getPrototypeOf(error), descriptors);
|
|
1976
2170
|
const { stack } = error;
|
|
1977
2171
|
if (typeof stack === "string") {
|
|
1978
|
-
Reflect.defineProperty(clone, "stack", { value: stack, enumerable: false });
|
|
2172
|
+
Reflect.defineProperty(clone, "stack", { value: stack, enumerable: false, writable: true });
|
|
1979
2173
|
}
|
|
2174
|
+
withoutEnumerableKeys(clone);
|
|
2175
|
+
shadowHook(clone);
|
|
1980
2176
|
return clone;
|
|
1981
2177
|
}
|
|
1982
2178
|
function walkArray(value, rules, depth2, ancestors, inSerialized) {
|
|
@@ -2031,69 +2227,77 @@ function walkObject(value, rules, depth2, ancestors, honorToJson, inSerialized)
|
|
|
2031
2227
|
if (ArrayBuffer.isView(value)) {
|
|
2032
2228
|
return withBinaryAsSize(value);
|
|
2033
2229
|
}
|
|
2034
|
-
const
|
|
2035
|
-
const resolvedToJson = resolveToJson(value, plain);
|
|
2230
|
+
const resolved = resolveToJson(value, rules.ownFields.safe);
|
|
2036
2231
|
ancestors.add(value);
|
|
2037
2232
|
try {
|
|
2038
|
-
|
|
2039
|
-
const toJson = honorToJson && !ownsItsForm ? resolvedToJson.method : void 0;
|
|
2040
|
-
if (toJson !== void 0) {
|
|
2041
|
-
if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
|
|
2042
|
-
return rules.censor;
|
|
2043
|
-
}
|
|
2044
|
-
return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
|
|
2045
|
-
}
|
|
2046
|
-
if (typeof value === "function") {
|
|
2047
|
-
return plain ? void 0 : value;
|
|
2048
|
-
}
|
|
2049
|
-
if (Array.isArray(value)) {
|
|
2050
|
-
return walkArray(value, rules, depth2, ancestors, inSerialized);
|
|
2051
|
-
}
|
|
2052
|
-
const source = value;
|
|
2053
|
-
const keys = Object.keys(source);
|
|
2054
|
-
const asData = isError(value) && (plain || rules.errorsAsData && !inSerialized);
|
|
2055
|
-
const clone = isError(value) ? cloneError(value) : void 0;
|
|
2056
|
-
const copy = clone !== void 0 && !asData ? clone : {};
|
|
2057
|
-
const scrubbed = isError(value) && (plain || !inSerialized);
|
|
2058
|
-
const omitted = /* @__PURE__ */ new Set();
|
|
2059
|
-
for (const key of keys) {
|
|
2060
|
-
const read = Reflect.get(source, key);
|
|
2061
|
-
const current = scrubbed ? ownFieldForLog(source, key, read, rules.ownFields) : read;
|
|
2062
|
-
if (current === OMIT_OWN_FIELD) {
|
|
2063
|
-
Reflect.deleteProperty(copy, key);
|
|
2064
|
-
omitted.add(key);
|
|
2065
|
-
continue;
|
|
2066
|
-
}
|
|
2067
|
-
defineOwn(
|
|
2068
|
-
copy,
|
|
2069
|
-
key,
|
|
2070
|
-
rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
|
|
2071
|
-
// An own enumerable `name`, `message` or `stack` of an `Error` is the same text as
|
|
2072
|
-
// the part the walk would add, so it is cut the same way.
|
|
2073
|
-
scrubbed && ERROR_TEXT_FIELDS.has(key) ? cutIfText(current) : current,
|
|
2074
|
-
rules,
|
|
2075
|
-
depth2 + 1,
|
|
2076
|
-
ancestors,
|
|
2077
|
-
true,
|
|
2078
|
-
inSerialized || !honorToJson && rules.serializedKeys.has(key)
|
|
2079
|
-
)
|
|
2080
|
-
);
|
|
2081
|
-
}
|
|
2082
|
-
if (asData) {
|
|
2083
|
-
const written = keys.filter((key) => !omitted.has(key));
|
|
2084
|
-
addErrorParts(
|
|
2085
|
-
copy,
|
|
2086
|
-
value,
|
|
2087
|
-
written,
|
|
2088
|
-
errorPartsHost(rules, ancestors, inSerialized),
|
|
2089
|
-
depth2
|
|
2090
|
-
);
|
|
2091
|
-
}
|
|
2092
|
-
return copy;
|
|
2233
|
+
return walkForm(value, resolved, rules, depth2, ancestors, honorToJson, inSerialized);
|
|
2093
2234
|
} finally {
|
|
2094
2235
|
ancestors.delete(value);
|
|
2095
2236
|
}
|
|
2096
2237
|
}
|
|
2238
|
+
function walkForm(value, resolved, rules, depth2, ancestors, honorToJson, inSerialized) {
|
|
2239
|
+
const plain = rules.ownFields.safe;
|
|
2240
|
+
const toJson = honorToJson && !(plain && isError(value)) ? resolved.method : void 0;
|
|
2241
|
+
if (toJson !== void 0) {
|
|
2242
|
+
return walkToJsonOutput(value, toJson, rules, depth2, ancestors, inSerialized);
|
|
2243
|
+
}
|
|
2244
|
+
if (typeof value === "function") {
|
|
2245
|
+
return plain || resolved.volatile ? void 0 : value;
|
|
2246
|
+
}
|
|
2247
|
+
if (Array.isArray(value)) {
|
|
2248
|
+
return walkArray(value, rules, depth2, ancestors, inSerialized);
|
|
2249
|
+
}
|
|
2250
|
+
return walkProperties(value, rules, depth2, ancestors, honorToJson, inSerialized);
|
|
2251
|
+
}
|
|
2252
|
+
function walkToJsonOutput(value, toJson, rules, depth2, ancestors, inSerialized) {
|
|
2253
|
+
if (Object.keys(value).some((key) => rules.sensitive.has(key.toLowerCase()))) {
|
|
2254
|
+
return rules.censor;
|
|
2255
|
+
}
|
|
2256
|
+
return walk(toJson.call(value), rules, depth2 + 1, ancestors, true, inSerialized);
|
|
2257
|
+
}
|
|
2258
|
+
function walkProperties(value, rules, depth2, ancestors, honorToJson, inSerialized) {
|
|
2259
|
+
const source = value;
|
|
2260
|
+
const keys = Object.keys(source);
|
|
2261
|
+
const { safe } = rules.ownFields;
|
|
2262
|
+
const asData = isError(value) && (safe || rules.errorsAsData && !inSerialized);
|
|
2263
|
+
const copy = startCopy(value, asData);
|
|
2264
|
+
const scrubbed = isError(value) && (safe || !inSerialized);
|
|
2265
|
+
const omitted = /* @__PURE__ */ new Set();
|
|
2266
|
+
for (const key of keys) {
|
|
2267
|
+
const read = Reflect.get(source, key);
|
|
2268
|
+
const current = scrubbed ? ownFieldForLog(source, key, read, rules.ownFields) : read;
|
|
2269
|
+
const hook = honorToJson && key === "toJSON" && typeof current === "function";
|
|
2270
|
+
if (current === OMIT_OWN_FIELD || hook) {
|
|
2271
|
+
omitted.add(key);
|
|
2272
|
+
continue;
|
|
2273
|
+
}
|
|
2274
|
+
Reflect.deleteProperty(copy, key);
|
|
2275
|
+
defineOwn(
|
|
2276
|
+
copy,
|
|
2277
|
+
key,
|
|
2278
|
+
rules.sensitive.has(key.toLowerCase()) ? rules.censor : walk(
|
|
2279
|
+
scrubbed && ERROR_TEXT_FIELDS.has(key) ? cutIfText(current) : current,
|
|
2280
|
+
rules,
|
|
2281
|
+
depth2 + 1,
|
|
2282
|
+
ancestors,
|
|
2283
|
+
true,
|
|
2284
|
+
inSerialized || !honorToJson && rules.serializedKeys.has(key)
|
|
2285
|
+
)
|
|
2286
|
+
);
|
|
2287
|
+
}
|
|
2288
|
+
if (asData) {
|
|
2289
|
+
const host = errorPartsHost(rules, ancestors, inSerialized);
|
|
2290
|
+
addErrorParts(copy, value, without(keys, omitted), host, depth2);
|
|
2291
|
+
}
|
|
2292
|
+
return copy;
|
|
2293
|
+
}
|
|
2294
|
+
function without(keys, omitted) {
|
|
2295
|
+
return keys.filter((key) => !omitted.has(key));
|
|
2296
|
+
}
|
|
2297
|
+
function startCopy(value, asData) {
|
|
2298
|
+
const clone = isError(value) ? cloneError(value) : void 0;
|
|
2299
|
+
return clone !== void 0 && !asData ? clone : {};
|
|
2300
|
+
}
|
|
2097
2301
|
function errorPartsHost(rules, ancestors, inSerialized) {
|
|
2098
2302
|
return {
|
|
2099
2303
|
sensitive: rules.sensitive,
|
|
@@ -2986,6 +3190,26 @@ function augmentLoggerModule(base, providers) {
|
|
|
2986
3190
|
};
|
|
2987
3191
|
}
|
|
2988
3192
|
var BymaxLoggerModule = class extends BymaxLoggerModuleBase {
|
|
3193
|
+
/**
|
|
3194
|
+
* @param httpAdapterHost - The HTTP adapter, absent in an application with no HTTP server.
|
|
3195
|
+
*/
|
|
3196
|
+
constructor(httpAdapterHost) {
|
|
3197
|
+
super();
|
|
3198
|
+
this.httpAdapterHost = httpAdapterHost;
|
|
3199
|
+
}
|
|
3200
|
+
/**
|
|
3201
|
+
* Mount the error layer that keeps the type of a body-parser error for the exception filter.
|
|
3202
|
+
* It runs after the body parser, which Nest registers first, and before Nest's exception
|
|
3203
|
+
* handler, and it passes every error on. Only the Express adapter has such a layer.
|
|
3204
|
+
*
|
|
3205
|
+
* @param _consumer - The middleware consumer, unused: an error layer is not consumer middleware.
|
|
3206
|
+
*/
|
|
3207
|
+
configure(_consumer) {
|
|
3208
|
+
const adapter = this.httpAdapterHost.httpAdapter;
|
|
3209
|
+
if (adapter?.getType() === "express") {
|
|
3210
|
+
adapter.getInstance().use(recordParserErrorMiddleware);
|
|
3211
|
+
}
|
|
3212
|
+
}
|
|
2989
3213
|
/**
|
|
2990
3214
|
* Synchronous registration.
|
|
2991
3215
|
*
|
|
@@ -3103,7 +3327,8 @@ var BymaxLoggerModule = class extends BymaxLoggerModuleBase {
|
|
|
3103
3327
|
}
|
|
3104
3328
|
};
|
|
3105
3329
|
BymaxLoggerModule = __decorateClass([
|
|
3106
|
-
Module({})
|
|
3330
|
+
Module({}),
|
|
3331
|
+
__decorateParam(0, Inject(HttpAdapterHost))
|
|
3107
3332
|
], BymaxLoggerModule);
|
|
3108
3333
|
|
|
3109
3334
|
// src/server/destinations/pretty-dev.destination.ts
|
|
@@ -3433,7 +3658,12 @@ var HttpAccessLogMiddleware = class {
|
|
|
3433
3658
|
RESERVED_LOG_KEYS.HTTP_REQUEST_CLIENT_ERROR,
|
|
3434
3659
|
`${method} ${url} \u2192 ${statusCode}`,
|
|
3435
3660
|
userId,
|
|
3436
|
-
recorded === void 0 ? meta : {
|
|
3661
|
+
recorded === void 0 ? meta : {
|
|
3662
|
+
...meta,
|
|
3663
|
+
errorMessage: cutIfText(
|
|
3664
|
+
withoutRequestQueryIfText(recorded.error.message, req.originalUrl, req.url)
|
|
3665
|
+
)
|
|
3666
|
+
}
|
|
3437
3667
|
);
|
|
3438
3668
|
return;
|
|
3439
3669
|
}
|