X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=6ede0a80fde15d00e7de72edfaae252bedf1bec8;hb=HEAD;hp=f199b4947ed1ba537a29350c617322cf64678512;hpb=30851a8cb9f8823b1b395ace8f53d62c5c53abd8;p=squeep-indieauth-helper diff --git a/lib/common.js b/lib/common.js index f199b49..6ede0a8 100644 --- a/lib/common.js +++ b/lib/common.js @@ -1,35 +1,39 @@ 'use strict'; -const path = require('path'); - /** - * Return a function which combines a part of the filename with a scope, for use in logging. - * @param {string} filename + * @typedef {object} GotResponse + * @property {Buffer|string} body response body + * @property {Array} redirectUrls followed redirect urls + * @property {number=} retryCount retries + * @property {object=} timings timings object + * @property {object=} timings.phases timing phases object + * @property {number=} timings.phases.total timing total ms */ -const fileScope = (filename) => { - let fScope = path.basename(filename, '.js'); - if (fScope === 'index') { - fScope = path.basename(path.dirname(filename)); - } - return (scope) => `${fScope}:${scope}`; -} - - /** - * Pick out useful axios response fields. - * @param {AxiosResponse} res - * @returns {Object} + * Pick out useful got response fields. + * @param {GotResponse} res response + * @returns {object} filtered response */ -const axiosResponseLogData = (res) => { +const gotResponseLogData = (res) => { const data = pick(res, [ - 'status', - 'statusText', + 'statusCode', + 'statusMessage', 'headers', - 'elapsedTimeMs', - 'data', + 'body', + 'url', + 'error', ]); - 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; }; @@ -37,9 +41,9 @@ const axiosResponseLogData = (res) => { /** * Limit length of string to keep logs sane - * @param {String} str - * @param {Number} len - * @returns {String} + * @param {string} str string + * @param {number} len length + * @returns {string} truncated string */ const logTruncate = (str, len) => { if (typeof str !== 'string' || str.toString().length <= len) { @@ -51,8 +55,9 @@ const logTruncate = (str, len) => { /** * Return a new object with selected props. - * @param {Object} obj - * @param {String[]} props + * @param {object} obj object + * @param {string[]} props list of properties + * @returns {object} filtered object */ const pick = (obj, props) => { return props.reduce((acc, prop) => { @@ -66,9 +71,9 @@ const pick = (obj, props) => { /** * Return a set containing non-shared items between two sets. - * @param {Set} a - * @param {Set} b - * @returns {Set} + * @param {Set} a set a + * @param {Set} b set b + * @returns {Set} set difference */ const setSymmetricDifference = (a, b) => { const d = new Set(a); @@ -85,8 +90,8 @@ const setSymmetricDifference = (a, b) => { /** * URL objects have weird names. - * @param {String} component - * @returns {String} + * @param {string} component url component name + * @returns {string} translated url component name */ const properURLComponentName = (component) => { // eslint-disable-next-line security/detect-object-injection @@ -94,26 +99,13 @@ const properURLComponentName = (component) => { hash: 'fragment', protocol: 'scheme', }[component] || component; -} - - -/** - * Encodes single-level object as form data string. - * @param {Object} data - */ -const formData = (data) => { - const formData = new URLSearchParams(); - Object.entries(data).forEach(([name, value]) => formData.set(name, value)); - return formData.toString(); }; module.exports = { - fileScope, - axiosResponseLogData, + gotResponseLogData, logTruncate, pick, setSymmetricDifference, properURLComponentName, - formData, -}; \ No newline at end of file +};