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