Skip to content

Commit 81b2fce

Browse files
committed
Fine tune logging capabilities of json-prune scriptlet
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.
1 parent b9f3523 commit 81b2fce

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

assets/resources/scriptlets.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ builtinScriptlets.push({
621621
name: 'object-prune.fn',
622622
fn: objectPrune,
623623
dependencies: [
624+
'get-extra-args.fn',
624625
'matches-stack-trace.fn',
625626
'pattern-to-regex.fn',
626627
],
@@ -641,18 +642,24 @@ function objectPrune(
641642
const prunePaths = rawPrunePaths !== ''
642643
? rawPrunePaths.split(/ +/)
643644
: [];
645+
const extraArgs = getExtraArgs(Array.from(arguments), 4);
644646
let needlePaths;
645647
let log, reLogNeedle;
646648
if ( prunePaths.length !== 0 ) {
647649
needlePaths = prunePaths.length !== 0 && rawNeedlePaths !== ''
648650
? rawNeedlePaths.split(/ +/)
649651
: [];
652+
if ( extraArgs.log !== undefined ) {
653+
log = console.log.bind(console);
654+
reLogNeedle = patternToRegex(extraArgs.log);
655+
}
650656
} else {
651657
log = console.log.bind(console);
652658
reLogNeedle = patternToRegex(rawNeedlePaths);
653659
}
654660
if ( stackNeedle !== '' ) {
655-
if ( matchesStackTrace(patternToRegex(stackNeedle), log ? '1' : 0) === false ) {
661+
const reStackNeedle = patternToRegex(stackNeedle);
662+
if ( matchesStackTrace(reStackNeedle, extraArgs.logstack) === false ) {
656663
return obj;
657664
}
658665
}
@@ -816,6 +823,7 @@ builtinScriptlets.push({
816823
name: 'matches-stack-trace.fn',
817824
fn: matchesStackTrace,
818825
dependencies: [
826+
'get-exception-token.fn',
819827
'safe-self.fn',
820828
],
821829
});
@@ -855,9 +863,9 @@ function matchesStackTrace(
855863
const stack = lines.join('\t');
856864
const r = safe.RegExp_test.call(reNeedle, stack);
857865
if (
858-
logLevel === '1' ||
859-
logLevel === '2' && r ||
860-
logLevel === '3' && !r
866+
logLevel === 1 ||
867+
logLevel === 2 && r ||
868+
logLevel === 3 && !r
861869
) {
862870
safe.uboLog(stack.replace(/\t/g, '\n'));
863871
}
@@ -993,31 +1001,32 @@ builtinScriptlets.push({
9931001
fn: abortOnStackTrace,
9941002
dependencies: [
9951003
'get-exception-token.fn',
1004+
'get-extra-args.fn',
9961005
'matches-stack-trace.fn',
9971006
'pattern-to-regex.fn',
9981007
],
9991008
});
10001009
// Status is currently experimental
10011010
function abortOnStackTrace(
10021011
chain = '',
1003-
needle = '',
1004-
logLevel = ''
1012+
needle = ''
10051013
) {
10061014
if ( typeof chain !== 'string' ) { return; }
10071015
const reNeedle = patternToRegex(needle);
1016+
const extraArgs = getExtraArgs(Array.from(arguments), 2);
10081017
const makeProxy = function(owner, chain) {
10091018
const pos = chain.indexOf('.');
10101019
if ( pos === -1 ) {
10111020
let v = owner[chain];
10121021
Object.defineProperty(owner, chain, {
10131022
get: function() {
1014-
if ( matchesStackTrace(reNeedle, logLevel) ) {
1023+
if ( matchesStackTrace(reNeedle, extraArgs.log) ) {
10151024
throw new ReferenceError(getExceptionToken());
10161025
}
10171026
return v;
10181027
},
10191028
set: function(a) {
1020-
if ( matchesStackTrace(reNeedle, logLevel) ) {
1029+
if ( matchesStackTrace(reNeedle, extraArgs.log) ) {
10211030
throw new ReferenceError(getExceptionToken());
10221031
}
10231032
v = a;
@@ -1136,20 +1145,28 @@ function jsonPrune(
11361145
rawNeedlePaths = '',
11371146
stackNeedle = ''
11381147
) {
1148+
const extraArgs = Array.from(arguments).slice(3);
11391149
JSON.parse = new Proxy(JSON.parse, {
11401150
apply: function(target, thisArg, args) {
11411151
return objectPrune(
11421152
Reflect.apply(target, thisArg, args),
11431153
rawPrunePaths,
11441154
rawNeedlePaths,
1145-
stackNeedle
1155+
stackNeedle,
1156+
...extraArgs
11461157
);
11471158
},
11481159
});
11491160
Response.prototype.json = new Proxy(Response.prototype.json, {
11501161
apply: function(target, thisArg, args) {
11511162
return Reflect.apply(target, thisArg, args).then(o =>
1152-
objectPrune(o, rawPrunePaths, rawNeedlePaths, stackNeedle)
1163+
objectPrune(
1164+
o,
1165+
rawPrunePaths,
1166+
rawNeedlePaths,
1167+
stackNeedle,
1168+
...extraArgs
1169+
)
11531170
);
11541171
},
11551172
});

0 commit comments

Comments
 (0)