X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=c1b8703d58d435a127a80fcfaec70b73358a2a2c;hb=57cb2f2fc75ae447dfcc750bb62dcc8bb487cd15;hp=dc0585471303780c5720ab0d5f62fdf55d11d059;hpb=cf9590ecbcd4b0a7c01f153cacade619518f84f0;p=squeep-indieauth-helper diff --git a/lib/common.js b/lib/common.js index dc05854..c1b8703 100644 --- a/lib/common.js +++ b/lib/common.js @@ -1,35 +1,44 @@ 'use strict'; const path = require('path'); +const { name: packageName, version: packageVersion } = require('../package'); + +const libraryIdentifier = `${packageName}@${packageVersion}`; /** * Return a function which combines a part of the filename with a scope, for use in logging. * @param {string} filename */ const fileScope = (filename) => { - let fScope = path.basename(filename, '.js'); - if (fScope === 'index') { - fScope = path.basename(path.dirname(filename)); - } - return (scope) => `${fScope}:${scope}`; + const shortFilename = path.basename(filename, '.js'); + const fScope = (shortFilename === 'index') ? path.basename(path.dirname(filename)) : shortFilename; + return (scope) => [libraryIdentifier, fScope, scope].join(':'); } /** - * Pick out useful axios response fields. - * @param {*} res - * @returns + * Pick out useful got response fields. + * @param {GotResponse} res + * @returns {Object} */ -const axiosResponseLogData = (res) => { +const gotResponseLogData = (res) => { const data = pick(res, [ - 'status', - 'statusText', + 'statusCode', + 'statusMessage', 'headers', - 'elapsedTimeMs', - 'data', + 'body', ]); - if (data.data) { - data.data = logTruncate(data.data, 100); + if (typeof res.body === 'string') { + data.body = logTruncate(data.body, 100); + } else if (res.body instanceof Buffer) { + data.body = ``; + } + data.elapsedTimeMs = res?.timings?.phases?.total; + if (res?.redirectUrls?.length) { + data.redirectUrls = res.redirectUrls; + } + if (res?.retryCount) { + data.retryCount = res.retryCount; } return data; }; @@ -99,7 +108,7 @@ const properURLComponentName = (component) => { module.exports = { fileScope, - axiosResponseLogData, + gotResponseLogData, logTruncate, pick, setSymmetricDifference,