Skip to content

Commit

Permalink
Fine tune logging capabilities of json-prune scriptlet
Browse files Browse the repository at this point in the history
This extends logging capabilities of `json-prune` scriptlet as
follow:

  ...##+js(json-prune, a, b, stackNeedle, log, [logneedle], logstack, 1)

Whereas before, the only way to log `json-prune` usage was to skip
providing the property chain:

  ...##+js(json-prune, , b)

Where `b` was the expression to filter out logging output.

With the extended logging capabilities, the logging output can
be filtered out with `logneedle`, which can be a regex literal.

Additionally, to log the stack trace the `stackNeedle` argument
must be set to non-empty string. You can use `/.^/` to log the
stack trace without matching it.
  • Loading branch information
gorhill committed Jul 29, 2023
1 parent b9f3523 commit 81b2fce
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ builtinScriptlets.push({
name: 'object-prune.fn',
fn: objectPrune,
dependencies: [
'get-extra-args.fn',
'matches-stack-trace.fn',
'pattern-to-regex.fn',
],
Expand All @@ -641,18 +642,24 @@ function objectPrune(
const prunePaths = rawPrunePaths !== ''
? rawPrunePaths.split(/ +/)
: [];
const extraArgs = getExtraArgs(Array.from(arguments), 4);
let needlePaths;
let log, reLogNeedle;
if ( prunePaths.length !== 0 ) {
needlePaths = prunePaths.length !== 0 && rawNeedlePaths !== ''
? rawNeedlePaths.split(/ +/)
: [];
if ( extraArgs.log !== undefined ) {
log = console.log.bind(console);
reLogNeedle = patternToRegex(extraArgs.log);
}
} else {
log = console.log.bind(console);
reLogNeedle = patternToRegex(rawNeedlePaths);
}
if ( stackNeedle !== '' ) {
if ( matchesStackTrace(patternToRegex(stackNeedle), log ? '1' : 0) === false ) {
const reStackNeedle = patternToRegex(stackNeedle);
if ( matchesStackTrace(reStackNeedle, extraArgs.logstack) === false ) {
return obj;
}
}
Expand Down Expand Up @@ -816,6 +823,7 @@ builtinScriptlets.push({
name: 'matches-stack-trace.fn',
fn: matchesStackTrace,
dependencies: [
'get-exception-token.fn',
'safe-self.fn',
],
});
Expand Down Expand Up @@ -855,9 +863,9 @@ function matchesStackTrace(
const stack = lines.join('\t');
const r = safe.RegExp_test.call(reNeedle, stack);
if (
logLevel === '1' ||
logLevel === '2' && r ||
logLevel === '3' && !r
logLevel === 1 ||
logLevel === 2 && r ||
logLevel === 3 && !r
) {
safe.uboLog(stack.replace(/\t/g, '\n'));
}
Expand Down Expand Up @@ -993,31 +1001,32 @@ builtinScriptlets.push({
fn: abortOnStackTrace,
dependencies: [
'get-exception-token.fn',
'get-extra-args.fn',
'matches-stack-trace.fn',
'pattern-to-regex.fn',
],
});
// Status is currently experimental
function abortOnStackTrace(
chain = '',
needle = '',
logLevel = ''
needle = ''
) {
if ( typeof chain !== 'string' ) { return; }
const reNeedle = patternToRegex(needle);
const extraArgs = getExtraArgs(Array.from(arguments), 2);
const makeProxy = function(owner, chain) {
const pos = chain.indexOf('.');
if ( pos === -1 ) {
let v = owner[chain];
Object.defineProperty(owner, chain, {
get: function() {
if ( matchesStackTrace(reNeedle, logLevel) ) {
if ( matchesStackTrace(reNeedle, extraArgs.log) ) {
throw new ReferenceError(getExceptionToken());
}
return v;
},
set: function(a) {
if ( matchesStackTrace(reNeedle, logLevel) ) {
if ( matchesStackTrace(reNeedle, extraArgs.log) ) {
throw new ReferenceError(getExceptionToken());
}
v = a;
Expand Down Expand Up @@ -1136,20 +1145,28 @@ function jsonPrune(
rawNeedlePaths = '',
stackNeedle = ''
) {
const extraArgs = Array.from(arguments).slice(3);
JSON.parse = new Proxy(JSON.parse, {
apply: function(target, thisArg, args) {
return objectPrune(
Reflect.apply(target, thisArg, args),
rawPrunePaths,
rawNeedlePaths,
stackNeedle
stackNeedle,
...extraArgs
);
},
});
Response.prototype.json = new Proxy(Response.prototype.json, {
apply: function(target, thisArg, args) {
return Reflect.apply(target, thisArg, args).then(o =>
objectPrune(o, rawPrunePaths, rawNeedlePaths, stackNeedle)
objectPrune(
o,
rawPrunePaths,
rawNeedlePaths,
stackNeedle,
...extraArgs
)
);
},
});
Expand Down

0 comments on commit 81b2fce

Please sign in to comment.