b52b819da963d5172a06779e279cf3e2531acbcb
3 const path
= require('path');
4 const { name: packageName
, version: packageVersion
} = require('../package');
6 const libraryIdentifier
= `${packageName}@${packageVersion}`;
9 * Return a function which combines a part of the filename with a scope, for use in logging.
10 * @param {string} filename
12 const fileScope
= (filename
) => {
13 const shortFilename
= path
.basename(filename
, '.js');
14 const fScope
= (shortFilename
=== 'index') ? path
.basename(path
.dirname(filename
)) : shortFilename
;
15 return (scope
) => [libraryIdentifier
, fScope
, scope
].join(':');
20 * Pick out useful got response fields.
21 * @param {GotResponse} res
24 const gotResponseLogData
= (res
) => {
25 const data
= pick(res
, [
32 if (typeof res
.body
=== 'string') {
33 data
.body
= logTruncate(data
.body
, 100);
34 } else if (res
.body
instanceof Buffer
) {
35 data
.body
= `<Buffer ${res.body.byteLength} bytes>`;
37 data
.elapsedTimeMs
= res
?.timings
?.phases
?.total
;
38 if (res
?.redirectUrls
?.length
) {
39 data
.redirectUrls
= res
.redirectUrls
;
41 if (res
?.retryCount
) {
42 data
.retryCount
= res
.retryCount
;
49 * Limit length of string to keep logs sane
54 const logTruncate
= (str
, len
) => {
55 if (typeof str
!== 'string' || str
.toString().length
<= len
) {
58 return str
.toString().slice(0, len
) + `... (${str.toString().length} bytes)`;
63 * Return a new object with selected props.
65 * @param {String[]} props
67 const pick
= (obj
, props
) => {
68 return props
.reduce((acc
, prop
) => {
70 acc
[prop
] = obj
[prop
]; // eslint-disable-line security/detect-object-injection
78 * Return a set containing non-shared items between two sets.
83 const setSymmetricDifference
= (a
, b
) => {
97 * URL objects have weird names.
98 * @param {String} component
101 const properURLComponentName
= (component
) => {
102 // eslint-disable-next-line security/detect-object-injection
106 }[component
] || component
;
115 setSymmetricDifference
,
116 properURLComponentName
,