Fix/improve logging in `json-prune` scriptlet

This commit is contained in:
Raymond Hill 2023-08-09 08:02:45 -04:00
parent 2c04b5a982
commit b4ffd16db6
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 68 additions and 72 deletions

View File

@ -640,36 +640,27 @@ function objectPrune(
extraArgs = {} extraArgs = {}
) { ) {
if ( typeof rawPrunePaths !== 'string' ) { return obj; } if ( typeof rawPrunePaths !== 'string' ) { return obj; }
const safe = safeSelf();
const prunePaths = rawPrunePaths !== '' const prunePaths = rawPrunePaths !== ''
? rawPrunePaths.split(/ +/) ? rawPrunePaths.split(/ +/)
: []; : [];
const safe = safeSelf(); const needlePaths = prunePaths.length !== 0 && rawNeedlePaths !== ''
let needlePaths;
let log, reLogNeedle;
if ( prunePaths.length !== 0 ) {
needlePaths = prunePaths.length !== 0 && rawNeedlePaths !== ''
? rawNeedlePaths.split(/ +/) ? rawNeedlePaths.split(/ +/)
: []; : [];
if ( extraArgs.log !== undefined ) { const logLevel = shouldLog({ log: rawPrunePaths === '' || extraArgs.log });
log = console.log.bind(console); const log = logLevel ? ((...args) => { safe.uboLog(...args); }) : (( ) => { });
reLogNeedle = safe.patternToRegex(extraArgs.log); const reLogNeedle = safe.patternToRegex(logLevel === true ? rawNeedlePaths : '');
}
} else {
log = console.log.bind(console);
reLogNeedle = safe.patternToRegex(rawNeedlePaths);
}
if ( stackNeedleDetails.matchAll !== true ) { if ( stackNeedleDetails.matchAll !== true ) {
if ( matchesStackTrace(stackNeedleDetails, extraArgs.logstack) === false ) { if ( matchesStackTrace(stackNeedleDetails, extraArgs.logstack) === false ) {
return obj; return obj;
} }
} }
const findOwner = function(root, path, prune = false) { if ( objectPrune.findOwner === undefined ) {
objectPrune.findOwner = (root, path, prune = false) => {
let owner = root; let owner = root;
let chain = path; let chain = path;
for (;;) { for (;;) {
if ( typeof owner !== 'object' || owner === null ) { if ( typeof owner !== 'object' || owner === null ) { return false; }
return false;
}
const pos = chain.indexOf('.'); const pos = chain.indexOf('.');
if ( pos === -1 ) { if ( pos === -1 ) {
if ( prune === false ) { if ( prune === false ) {
@ -693,7 +684,7 @@ function objectPrune(
const next = chain.slice(pos + 1); const next = chain.slice(pos + 1);
let found = false; let found = false;
for ( const key of Object.keys(owner) ) { for ( const key of Object.keys(owner) ) {
found = findOwner(owner[key], next, prune) || found; found = objectPrune.findOwner(owner[key], next, prune) || found;
} }
return found; return found;
} }
@ -702,24 +693,25 @@ function objectPrune(
chain = chain.slice(pos + 1); chain = chain.slice(pos + 1);
} }
}; };
const mustProcess = function(root) { objectPrune.mustProcess = (root, needlePaths) => {
for ( const needlePath of needlePaths ) { for ( const needlePath of needlePaths ) {
if ( findOwner(root, needlePath) === false ) { if ( objectPrune.findOwner(root, needlePath) === false ) {
return false; return false;
} }
} }
return true; return true;
}; };
if ( log !== undefined ) { }
if ( logLevel === true || logLevel === 'all' ) {
const json = JSON.stringify(obj, null, 2); const json = JSON.stringify(obj, null, 2);
if ( reLogNeedle.test(json) ) { if ( reLogNeedle.test(json) ) {
log('uBO:', location.hostname, json); log(location.hostname, json);
} }
return obj;
} }
if ( mustProcess(obj) === false ) { return obj; } if ( prunePaths.length === 0 ) { return obj; }
if ( objectPrune.mustProcess(obj, needlePaths) === false ) { return obj; }
for ( const path of prunePaths ) { for ( const path of prunePaths ) {
findOwner(obj, path, true); objectPrune.findOwner(obj, path, true);
} }
return obj; return obj;
} }
@ -1222,6 +1214,9 @@ function jsonPrune(
) { ) {
JSON.parse = new Proxy(JSON.parse, { JSON.parse = new Proxy(JSON.parse, {
apply: function(target, thisArg, args) { apply: function(target, thisArg, args) {
if ( logLevel ) {
safe.uboLog('json-prune / JSON.parse()');
}
return objectPrune( return objectPrune(
Reflect.apply(target, thisArg, args), Reflect.apply(target, thisArg, args),
rawPrunePaths, rawPrunePaths,
@ -1235,19 +1230,20 @@ function jsonPrune(
Response.prototype.json = new Proxy(Response.prototype.json, { Response.prototype.json = new Proxy(Response.prototype.json, {
apply: function(target, thisArg, args) { apply: function(target, thisArg, args) {
const dataPromise = Reflect.apply(target, thisArg, args); const dataPromise = Reflect.apply(target, thisArg, args);
let outcome = 'match';
if ( fetchPropNeedles.size !== 0 ) { if ( fetchPropNeedles.size !== 0 ) {
const outcome = matchObjectProperties(fetchPropNeedles, thisArg) if ( matchObjectProperties(fetchPropNeedles, thisArg) === false ) {
? 'match' outcome = 'nomatch';
: 'nomatch'; }
}
if ( outcome === logLevel || logLevel === 'all' ) { if ( outcome === logLevel || logLevel === 'all' ) {
safe.uboLog( safe.uboLog(
`json-prune (${outcome})`, `json-prune / Response.json() (${outcome})`,
`\n\tpropsToMatch: ${JSON.stringify(Array.from(fetchPropNeedles)).slice(1,-1)}`, `\n\tfetchPropsToMatch: ${JSON.stringify(Array.from(fetchPropNeedles)).slice(1,-1)}`,
'\n\tprops:', thisArg, '\n\tprops:', thisArg,
); );
} }
if ( outcome === 'nomatch' ) { return dataPromise; } if ( outcome === 'nomatch' ) { return dataPromise; }
}
return dataPromise.then(data => objectPrune( return dataPromise.then(data => objectPrune(
data, data,
rawPrunePaths, rawPrunePaths,