4 * Here are some replacers for rendering peculiar entities as JSON suitable for logging.
7 const http
= require('http');
10 * @typedef {Object} ReplacerResult
11 * @property {Boolean} replaced
17 * template for replacers
18 * @param {String} _key
20 * @returns {ReplacerResult}
22 /* istanbul ignore next */
23 function _replacer(_key
, value
) {
26 if (undefined) { // eslint-disable-line no-constant-condition
29 const newValue
= Object
.assign({}, value
);
33 return { replaced
, value
};
38 * Convert any Error to a plain Object.
39 * @param {String} _key
41 * @returns {ReplacerResult}
43 function replacerError(_key
, value
) {
45 if (value
instanceof Error
) {
48 Object
.getOwnPropertyNames(value
).forEach((propertyName
) => newValue
[propertyName
] = value
[propertyName
]); // eslint-disable-line security/detect-object-injection
51 return { replaced
, value
};
56 * Convert any BigInt type to a String.
57 * @param {String} _key
59 * @returns {ReplacerResult}
61 function replacerBigInt(_key
, value
) {
63 if (typeof value
=== 'bigint') {
65 value
= value
.toString();
67 return { replaced
, value
};
72 * Convert any IncomingMessage to a subset Object.
73 * Also sanitizes any Authorization header. We do this here rather than
74 * in sanitization stage so that we do not have to rely on a set path to
76 * @param {String} _key
78 * @returns {ReplacerResult}
80 function replacerHttpIncomingMessage(_key
, value
) {
82 if (value
instanceof http
.IncomingMessage
) {
91 ].forEach((property
) => newValue
[property
] = value
[property
]); // eslint-disable-line security/detect-object-injection
93 if (newValue
.headers
&& 'authorization' in newValue
.headers
) {
94 const authHeader
= newValue
.headers
.authorization
;
95 const spaceIndex
= authHeader
.indexOf(' ');
96 // This blurs entire auth string if no space found, because -1 from indexOf.
97 const blurredAuthHeader
= authHeader
.slice(0, spaceIndex
+ 1) + '*'.repeat(authHeader
.length
- (spaceIndex
+ 1));
98 // New headers object, as we change it.
99 newValue
.headers
= Object
.assign({}, newValue
.headers
, {
100 authorization: blurredAuthHeader
,
105 return { replaced
, value
};
110 * Convert any ServerResponse or OutgoingMessage to a subset Object.
111 * @param {String} _key
113 * @returns {ReplacerResult}
115 function replacerHttpServerResponse(_key
, value
) {
116 let replaced
= false;
117 if (value
instanceof http
.OutgoingMessage
|| value
instanceof http
.ServerResponse
) {
123 ].forEach((property
) => newValue
[property
] = value
[property
]); // eslint-disable-line security/detect-object-injection
124 newValue
.headers
= value
.getHeaders();
127 return { replaced
, value
};
134 replacerHttpIncomingMessage
,
135 replacerHttpServerResponse
,