c1b8703d58d435a127a80fcfaec70b73358a2a2c
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
, [
31 if (typeof res
.body
=== 'string') {
32 data
.body
= logTruncate(data
.body
, 100);
33 } else if (res
.body
instanceof Buffer
) {
34 data
.body
= `<Buffer ${res.body.byteLength} bytes>`;
36 data
.elapsedTimeMs
= res
?.timings
?.phases
?.total
;
37 if (res
?.redirectUrls
?.length
) {
38 data
.redirectUrls
= res
.redirectUrls
;
40 if (res
?.retryCount
) {
41 data
.retryCount
= res
.retryCount
;
48 * Limit length of string to keep logs sane
53 const logTruncate
= (str
, len
) => {
54 if (typeof str
!== 'string' || str
.toString().length
<= len
) {
57 return str
.toString().slice(0, len
) + `... (${str.toString().length} bytes)`;
62 * Return a new object with selected props.
64 * @param {String[]} props
66 const pick
= (obj
, props
) => {
67 return props
.reduce((acc
, prop
) => {
69 acc
[prop
] = obj
[prop
]; // eslint-disable-line security/detect-object-injection
77 * Return a set containing non-shared items between two sets.
82 const setSymmetricDifference
= (a
, b
) => {
96 * URL objects have weird names.
97 * @param {String} component
100 const properURLComponentName
= (component
) => {
101 // eslint-disable-next-line security/detect-object-injection
105 }[component
] || component
;
114 setSymmetricDifference
,
115 properURLComponentName
,