X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fjson-replacers.js;h=14ac3343ac348e7803eeac458b8386a1a18f266b;hb=5a9119c279eca438d174c05a0c926d32468112af;hp=2160ad8635af71f9aed3fbd169c02cd17df71849;hpb=229dafe0003708b9fad190b4c0fc68136efd4f8c;p=squeep-logger-json-console diff --git a/lib/json-replacers.js b/lib/json-replacers.js index 2160ad8..14ac334 100644 --- a/lib/json-replacers.js +++ b/lib/json-replacers.js @@ -4,51 +4,21 @@ * Here are some replacers for rendering peculiar entities as JSON suitable for logging. */ -const http = require('http'); - -/** - * @typedef {Object} ReplacerResult - * @property {Boolean} replaced - * @property {*} value - */ - - -/** - * template for replacers - * @param {String} _key - * @param {*} value - * @returns {ReplacerResult} - */ -/* istanbul ignore next */ -function _replacer(_key, value) { - let replaced = false; - - if (undefined) { // eslint-disable-line no-constant-condition - replaced = true; - - const newValue = Object.assign({}, value); - - value = newValue; - } - return { replaced, value }; -} - +const http = require('node:http'); /** * Convert any Error to a plain Object. * @param {String} _key * @param {*} value - * @returns {ReplacerResult} + * @returns {Object} */ function replacerError(_key, value) { - let replaced = false; if (value instanceof Error) { - replaced = true; const newValue = {}; Object.getOwnPropertyNames(value).forEach((propertyName) => newValue[propertyName] = value[propertyName]); // eslint-disable-line security/detect-object-injection value = newValue; } - return { replaced, value }; + return value; } @@ -56,15 +26,13 @@ function replacerError(_key, value) { * Convert any BigInt type to a String. * @param {String} _key * @param {*} value - * @returns {ReplacerResult} + * @returns {String} */ function replacerBigInt(_key, value) { - let replaced = false; if (typeof value === 'bigint') { - replaced = true; value = value.toString(); } - return { replaced, value }; + return value; } @@ -75,12 +43,10 @@ function replacerBigInt(_key, value) { * this object. * @param {String} _key * @param {*} value - * @returns {ReplacerResult} + * @returns {Object} */ function replacerHttpIncomingMessage(_key, value) { - let replaced = false; if (value instanceof http.IncomingMessage) { - replaced = true; const newValue = {}; [ 'method', @@ -96,13 +62,14 @@ function replacerHttpIncomingMessage(_key, value) { // This blurs entire auth string if no space found, because -1 from indexOf. const blurredAuthHeader = authHeader.slice(0, spaceIndex + 1) + '*'.repeat(authHeader.length - (spaceIndex + 1)); // New headers object, as we change it. - newValue.headers = Object.assign({}, newValue.headers, { + newValue.headers = { + ...newValue.headers, authorization: blurredAuthHeader, - }); + }; } value = newValue; } - return { replaced, value }; + return value; } @@ -110,21 +77,19 @@ function replacerHttpIncomingMessage(_key, value) { * Convert any ServerResponse or OutgoingMessage to a subset Object. * @param {String} _key * @param {*} value - * @returns {ReplacerResult} + * @returns {Object} */ function replacerHttpServerResponse(_key, value) { - let replaced = false; if (value instanceof http.OutgoingMessage || value instanceof http.ServerResponse) { - replaced = true; const newValue = {}; [ 'statusCode', 'statusMessage', - ].forEach((property) => newValue[property] = value[property]); // eslint-disable-line security/detect-object-injection + ].forEach((property) => newValue[property] = value[property]); // eslint-disable-line security/detect-object-injection newValue.headers = value.getHeaders(); value = newValue; } - return { replaced, value }; + return value; }