4 * Here are some replacers for rendering peculiar entities as JSON suitable for logging.
7 const http
= require('node:http');
10 * Convert any Error to a plain Object.
11 * @param {String} _key
15 function replacerError(_key
, value
) {
16 if (value
instanceof Error
) {
18 Object
.getOwnPropertyNames(value
).forEach((propertyName
) => newValue
[propertyName
] = value
[propertyName
]); // eslint-disable-line security/detect-object-injection
26 * Convert any BigInt type to a String.
27 * @param {String} _key
31 function replacerBigInt(_key
, value
) {
32 if (typeof value
=== 'bigint') {
33 value
= value
.toString();
40 * Convert any IncomingMessage to a subset Object.
41 * Also sanitizes any Authorization header. We do this here rather than
42 * in sanitization stage so that we do not have to rely on a set path to
44 * @param {String} _key
48 function replacerHttpIncomingMessage(_key
, value
) {
49 if (value
instanceof http
.IncomingMessage
) {
57 ].forEach((property
) => newValue
[property
] = value
[property
]); // eslint-disable-line security/detect-object-injection
59 if (newValue
.headers
&& 'authorization' in newValue
.headers
) {
60 const authHeader
= newValue
.headers
.authorization
;
61 const spaceIndex
= authHeader
.indexOf(' ');
62 // This blurs entire auth string if no space found, because -1 from indexOf.
63 const blurredAuthHeader
= authHeader
.slice(0, spaceIndex
+ 1) + '*'.repeat(authHeader
.length
- (spaceIndex
+ 1));
64 // New headers object, as we change it.
65 newValue
.headers
= Object
.assign({}, newValue
.headers
, {
66 authorization: blurredAuthHeader
,
76 * Convert any ServerResponse or OutgoingMessage to a subset Object.
77 * @param {String} _key
81 function replacerHttpServerResponse(_key
, value
) {
82 if (value
instanceof http
.OutgoingMessage
|| value
instanceof http
.ServerResponse
) {
87 ].forEach((property
) => newValue
[property
] = value
[property
]); // eslint-disable-line security/detect-object-injection
88 newValue
.headers
= value
.getHeaders();
98 replacerHttpIncomingMessage
,
99 replacerHttpServerResponse
,