UNPKG

dd-trace

Version:

Datadog APM tracing client for JavaScript

1 lines 24.8 kB
(()=>{"use strict";var e={};e.d=(t,r)=>{for(var i in r)e.o(r,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:r[i]})},e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),e.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var t={};e.r(t),e.d(t,{JSONPath:()=>u});let r=require("vm");class i{static get version(){return"1.4.0"}static toString(){return"JavaScript Expression Parser (JSEP) v"+i.version}static addUnaryOp(e){return i.max_unop_len=Math.max(e.length,i.max_unop_len),i.unary_ops[e]=1,i}static addBinaryOp(e,t,r){return i.max_binop_len=Math.max(e.length,i.max_binop_len),i.binary_ops[e]=t,r?i.right_associative.add(e):i.right_associative.delete(e),i}static addIdentifierChar(e){return i.additional_identifier_chars.add(e),i}static addLiteral(e,t){return i.literals[e]=t,i}static removeUnaryOp(e){return delete i.unary_ops[e],e.length===i.max_unop_len&&(i.max_unop_len=i.getMaxKeyLen(i.unary_ops)),i}static removeAllUnaryOps(){return i.unary_ops={},i.max_unop_len=0,i}static removeIdentifierChar(e){return i.additional_identifier_chars.delete(e),i}static removeBinaryOp(e){return delete i.binary_ops[e],e.length===i.max_binop_len&&(i.max_binop_len=i.getMaxKeyLen(i.binary_ops)),i.right_associative.delete(e),i}static removeAllBinaryOps(){return i.binary_ops={},i.max_binop_len=0,i}static removeLiteral(e){return delete i.literals[e],i}static removeAllLiterals(){return i.literals={},i}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(e){return new i(e).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map(e=>e.length))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(e){return i.binary_ops[e]||0}static isIdentifierStart(e){return e>=65&&e<=90||e>=97&&e<=122||e>=128&&!i.binary_ops[String.fromCharCode(e)]||i.additional_identifier_chars.has(String.fromCharCode(e))}static isIdentifierPart(e){return i.isIdentifierStart(e)||i.isDecimalDigit(e)}throwError(e){let t=Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(e,t){if(i.hooks[e]){let r={context:this,node:t};return i.hooks.run(e,r),r.node}return t}searchHook(e){if(i.hooks[e]){let t={context:this};return i.hooks[e].find(function(e){return e.call(t.context,t),t.node}),t.node}}gobbleSpaces(){let e=this.code;for(;e===i.SPACE_CODE||e===i.TAB_CODE||e===i.LF_CODE||e===i.CR_CODE;)e=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");let e=this.gobbleExpressions(),t=1===e.length?e[0]:{type:i.COMPOUND,body:e};return this.runHook("after-all",t)}gobbleExpressions(e){let t=[],r,s;for(;this.index<this.expr.length;)if((r=this.code)===i.SEMCOL_CODE||r===i.COMMA_CODE)this.index++;else if(s=this.gobbleExpression())t.push(s);else if(this.index<this.expr.length){if(r===e)break;this.throwError('Unexpected "'+this.char+'"')}return t}gobbleExpression(){let e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let e=this.expr.substr(this.index,i.max_binop_len),t=e.length;for(;t>0;){if(i.binary_ops.hasOwnProperty(e)&&(!i.isIdentifierStart(this.code)||this.index+e.length<this.expr.length&&!i.isIdentifierPart(this.expr.charCodeAt(this.index+e.length))))return this.index+=t,e;e=e.substr(0,--t)}return!1}gobbleBinaryExpression(){let e,t,r,s,n,a,o,l,h;if(!(a=this.gobbleToken())||!(t=this.gobbleBinaryOp()))return a;for(n={value:t,prec:i.binaryPrecedence(t),right_a:i.right_associative.has(t)},(o=this.gobbleToken())||this.throwError("Expected expression after "+t),s=[a,n,o];t=this.gobbleBinaryOp();){if(0===(r=i.binaryPrecedence(t))){this.index-=t.length;break}n={value:t,prec:r,right_a:i.right_associative.has(t)},h=t;let l=e=>n.right_a&&e.right_a?r>e.prec:r<=e.prec;for(;s.length>2&&l(s[s.length-2]);)o=s.pop(),t=s.pop().value,a=s.pop(),e={type:i.BINARY_EXP,operator:t,left:a,right:o},s.push(e);(e=this.gobbleToken())||this.throwError("Expected expression after "+h),s.push(n,e)}for(l=s.length-1,e=s[l];l>1;)e={type:i.BINARY_EXP,operator:s[l-1].value,left:s[l-2],right:e},l-=2;return e}gobbleToken(){let e,t,r,s;if(this.gobbleSpaces(),s=this.searchHook("gobble-token"))return this.runHook("after-token",s);if(e=this.code,i.isDecimalDigit(e)||e===i.PERIOD_CODE)return this.gobbleNumericLiteral();if(e===i.SQUOTE_CODE||e===i.DQUOTE_CODE)s=this.gobbleStringLiteral();else if(e===i.OBRACK_CODE)s=this.gobbleArray();else{for(r=(t=this.expr.substr(this.index,i.max_unop_len)).length;r>0;){if(i.unary_ops.hasOwnProperty(t)&&(!i.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!i.isIdentifierPart(this.expr.charCodeAt(this.index+t.length)))){this.index+=r;let e=this.gobbleToken();return e||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:i.UNARY_EXP,operator:t,argument:e,prefix:!0})}t=t.substr(0,--r)}i.isIdentifierStart(e)?(s=this.gobbleIdentifier(),i.literals.hasOwnProperty(s.name)?s={type:i.LITERAL,value:i.literals[s.name],raw:s.name}:s.name===i.this_str&&(s={type:i.THIS_EXP})):e===i.OPAREN_CODE&&(s=this.gobbleGroup())}return s?(s=this.gobbleTokenProperty(s),this.runHook("after-token",s)):this.runHook("after-token",!1)}gobbleTokenProperty(e){this.gobbleSpaces();let t=this.code;for(;t===i.PERIOD_CODE||t===i.OBRACK_CODE||t===i.OPAREN_CODE||t===i.QUMARK_CODE;){let r;if(t===i.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==i.PERIOD_CODE)break;r=!0,this.index+=2,this.gobbleSpaces(),t=this.code}this.index++,t===i.OBRACK_CODE?((e={type:i.MEMBER_EXP,computed:!0,object:e,property:this.gobbleExpression()}).property||this.throwError('Unexpected "'+this.char+'"'),this.gobbleSpaces(),(t=this.code)!==i.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):t===i.OPAREN_CODE?e={type:i.CALL_EXP,arguments:this.gobbleArguments(i.CPAREN_CODE),callee:e}:(t===i.PERIOD_CODE||r)&&(r&&this.index--,this.gobbleSpaces(),e={type:i.MEMBER_EXP,computed:!1,object:e,property:this.gobbleIdentifier()}),r&&(e.optional=!0),this.gobbleSpaces(),t=this.code}return e}gobbleNumericLiteral(){let e="",t,r;for(;i.isDecimalDigit(this.code);)e+=this.expr.charAt(this.index++);if(this.code===i.PERIOD_CODE)for(e+=this.expr.charAt(this.index++);i.isDecimalDigit(this.code);)e+=this.expr.charAt(this.index++);if("e"===(t=this.char)||"E"===t){for(e+=this.expr.charAt(this.index++),("+"===(t=this.char)||"-"===t)&&(e+=this.expr.charAt(this.index++));i.isDecimalDigit(this.code);)e+=this.expr.charAt(this.index++);i.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+e+this.char+")")}return r=this.code,i.isIdentifierStart(r)?this.throwError("Variable names cannot start with a number ("+e+this.char+")"):(r===i.PERIOD_CODE||1===e.length&&e.charCodeAt(0)===i.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:i.LITERAL,value:parseFloat(e),raw:e}}gobbleStringLiteral(){let e="",t=this.index,r=this.expr.charAt(this.index++),s=!1;for(;this.index<this.expr.length;){let t=this.expr.charAt(this.index++);if(t===r){s=!0;break}if("\\"===t)switch(t=this.expr.charAt(this.index++)){case"n":e+="\n";break;case"r":e+="\r";break;case"t":e+=" ";break;case"b":e+="\b";break;case"f":e+="\f";break;case"v":e+="\v";break;default:e+=t}else e+=t}return s||this.throwError('Unclosed quote after "'+e+'"'),{type:i.LITERAL,value:e,raw:this.expr.substring(t,this.index)}}gobbleIdentifier(){let e=this.code,t=this.index;for(i.isIdentifierStart(e)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length;)if(e=this.code,i.isIdentifierPart(e))this.index++;else break;return{type:i.IDENTIFIER,name:this.expr.slice(t,this.index)}}gobbleArguments(e){let t=[],r=!1,s=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let n=this.code;if(n===e){r=!0,this.index++,e===i.CPAREN_CODE&&s&&s>=t.length&&this.throwError("Unexpected token "+String.fromCharCode(e));break}if(n===i.COMMA_CODE){if(this.index++,++s!==t.length){if(e===i.CPAREN_CODE)this.throwError("Unexpected token ,");else if(e===i.CBRACK_CODE)for(let e=t.length;e<s;e++)t.push(null)}}else if(t.length!==s&&0!==s)this.throwError("Expected comma");else{let e=this.gobbleExpression();e&&e.type!==i.COMPOUND||this.throwError("Expected comma"),t.push(e)}}return r||this.throwError("Expected "+String.fromCharCode(e)),t}gobbleGroup(){this.index++;let e=this.gobbleExpressions(i.CPAREN_CODE);if(this.code===i.CPAREN_CODE)return(this.index++,1===e.length)?e[0]:!!e.length&&{type:i.SEQUENCE_EXP,expressions:e};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:i.ARRAY_EXP,elements:this.gobbleArguments(i.CBRACK_CODE)}}}Object.assign(i,{hooks:new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach(function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)},this)}run(e,t){this[e]=this[e]||[],this[e].forEach(function(e){e.call(t&&t.context?t.context:t,t)})}},plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach(e=>{if("object"!=typeof e||!e.name||!e.init)throw Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)})}}(i),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"??":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10,"**":11},right_associative:new Set(["**"]),additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),i.max_unop_len=i.getMaxKeyLen(i.unary_ops),i.max_binop_len=i.getMaxKeyLen(i.binary_ops);let s=e=>new i(e).parse(),n=Object.getOwnPropertyNames(class{});Object.getOwnPropertyNames(i).filter(e=>!n.includes(e)&&void 0===s[e]).forEach(e=>{s[e]=i[e]}),s.Jsep=i,s.plugins.register({name:"ternary",init(e){e.hooks.add("after-expression",function(t){if(t.node&&this.code===e.QUMARK_CODE){this.index++;let r=t.node,i=this.gobbleExpression();if(i||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;let s=this.gobbleExpression();if(s||this.throwError("Expected expression"),t.node={type:"ConditionalExpression",test:r,consequent:i,alternate:s},r.operator&&e.binary_ops[r.operator]<=.9){let i=r;for(;i.right.operator&&e.binary_ops[i.right.operator]<=.9;)i=i.right;t.node.test=i.right,i.right=t.node,t.node=r}}else this.throwError("Expected :")}})}});let a={name:"assignment",assignmentOperators:new Set(["=","*=","**=","/=","%=","+=","-=","<<=",">>=",">>>=","&=","^=","|=","||=","&&=","??="]),updateOperators:[43,45],assignmentPrecedence:.9,init(e){let t=[e.IDENTIFIER,e.MEMBER_EXP];a.assignmentOperators.forEach(t=>e.addBinaryOp(t,a.assignmentPrecedence,!0)),e.hooks.add("gobble-token",function(e){let r=this.code;a.updateOperators.some(e=>e===r&&e===this.expr.charCodeAt(this.index+1))&&(this.index+=2,e.node={type:"UpdateExpression",operator:43===r?"++":"--",argument:this.gobbleTokenProperty(this.gobbleIdentifier()),prefix:!0},e.node.argument&&t.includes(e.node.argument.type)||this.throwError(`Unexpected ${e.node.operator}`))}),e.hooks.add("after-token",function(e){if(e.node){let r=this.code;a.updateOperators.some(e=>e===r&&e===this.expr.charCodeAt(this.index+1))&&(t.includes(e.node.type)||this.throwError(`Unexpected ${e.node.operator}`),this.index+=2,e.node={type:"UpdateExpression",operator:43===r?"++":"--",argument:e.node,prefix:!1})}}),e.hooks.add("after-expression",function(e){e.node&&function e(t){a.assignmentOperators.has(t.operator)?(t.type="AssignmentExpression",e(t.left),e(t.right)):t.operator||Object.values(t).forEach(t=>{t&&"object"==typeof t&&e(t)})}(e.node)})}};s.plugins.register({name:"regex",init(e){e.hooks.add("gobble-token",function(t){if(47===this.code){let r=++this.index,i=!1;for(;this.index<this.expr.length;){if(47===this.code&&!i){let i,s=this.expr.slice(r,this.index),n="";for(;++this.index<this.expr.length;){let e=this.code;if(e>=97&&e<=122||e>=65&&e<=90||e>=48&&e<=57)n+=this.char;else break}try{i=new RegExp(s,n)}catch(e){this.throwError(e.message)}return t.node={type:e.LITERAL,value:i,raw:this.expr.slice(r-1,this.index)},t.node=this.gobbleTokenProperty(t.node),t.node}this.code===e.OBRACK_CODE?i=!0:i&&this.code===e.CBRACK_CODE&&(i=!1),this.index+=92===this.code?2:1}this.throwError("Unclosed Regex")}})}},a),s.addUnaryOp("typeof"),s.addLiteral("null",null),s.addLiteral("undefined",void 0);let o=new Set(["constructor","__proto__","__defineGetter__","__defineSetter__"]),l={evalAst(e,t){switch(e.type){case"BinaryExpression":case"LogicalExpression":return l.evalBinaryExpression(e,t);case"Compound":return l.evalCompound(e,t);case"ConditionalExpression":return l.evalConditionalExpression(e,t);case"Identifier":return l.evalIdentifier(e,t);case"Literal":return l.evalLiteral(e,t);case"MemberExpression":return l.evalMemberExpression(e,t);case"UnaryExpression":return l.evalUnaryExpression(e,t);case"ArrayExpression":return l.evalArrayExpression(e,t);case"CallExpression":return l.evalCallExpression(e,t);case"AssignmentExpression":return l.evalAssignmentExpression(e,t);default:throw SyntaxError("Unexpected expression",e)}},evalBinaryExpression:(e,t)=>({"||":(e,t)=>e||t(),"&&":(e,t)=>e&&t(),"|":(e,t)=>e|t(),"^":(e,t)=>e^t(),"&":(e,t)=>e&t(),"==":(e,t)=>e==t(),"!=":(e,t)=>e!=t(),"===":(e,t)=>e===t(),"!==":(e,t)=>e!==t(),"<":(e,t)=>e<t(),">":(e,t)=>e>t(),"<=":(e,t)=>e<=t(),">=":(e,t)=>e>=t(),"<<":(e,t)=>e<<t(),">>":(e,t)=>e>>t(),">>>":(e,t)=>e>>>t(),"+":(e,t)=>e+t(),"-":(e,t)=>e-t(),"*":(e,t)=>e*t(),"/":(e,t)=>e/t(),"%":(e,t)=>e%t()})[e.operator](l.evalAst(e.left,t),()=>l.evalAst(e.right,t)),evalCompound(e,t){let r;for(let i=0;i<e.body.length;i++){"Identifier"===e.body[i].type&&["var","let","const"].includes(e.body[i].name)&&e.body[i+1]&&"AssignmentExpression"===e.body[i+1].type&&(i+=1);let s=e.body[i];r=l.evalAst(s,t)}return r},evalConditionalExpression:(e,t)=>l.evalAst(e.test,t)?l.evalAst(e.consequent,t):l.evalAst(e.alternate,t),evalIdentifier(e,t){if(Object.hasOwn(t,e.name))return t[e.name];throw ReferenceError(`${e.name} is not defined`)},evalLiteral:e=>e.value,evalMemberExpression(e,t){let r=String(e.computed?l.evalAst(e.property):e.property.name),i=l.evalAst(e.object,t);if(null==i||!Object.hasOwn(i,r)&&o.has(r))throw TypeError(`Cannot read properties of ${i} (reading '${r}')`);let s=i[r];return"function"==typeof s?s.bind(i):s},evalUnaryExpression:(e,t)=>({"-":e=>-l.evalAst(e,t),"!":e=>!l.evalAst(e,t),"~":e=>~l.evalAst(e,t),"+":e=>+l.evalAst(e,t),typeof:e=>typeof l.evalAst(e,t)})[e.operator](e.argument),evalArrayExpression:(e,t)=>e.elements.map(e=>l.evalAst(e,t)),evalCallExpression(e,t){let r=e.arguments.map(e=>l.evalAst(e,t));return l.evalAst(e.callee,t)(...r)},evalAssignmentExpression(e,t){if("Identifier"!==e.left.type)throw SyntaxError("Invalid left-hand side in assignment");let r=e.left.name,i=l.evalAst(e.right,t);return t[r]=i,t[r]}};function h(e,t){return(e=e.slice()).push(t),e}function p(e,t){return(t=t.slice()).unshift(e),t}class c extends Error{constructor(e){super('JSONPath should not be called with "new" (it prevents return of (unwrapped) scalar values)'),this.avoidNew=!0,this.value=e,this.name="NewError"}}function u(e,t,r,i,s){if(!(this instanceof u))try{return new u(e,t,r,i,s)}catch(e){if(!e.avoidNew)throw e;return e.value}"string"==typeof e&&(s=i,i=r,r=t,t=e,e=null);let n=e&&"object"==typeof e;if(e=e||{},this.json=e.json||r,this.path=e.path||t,this.resultType=e.resultType||"value",this.flatten=e.flatten||!1,this.wrap=!Object.hasOwn(e,"wrap")||e.wrap,this.sandbox=e.sandbox||{},this.eval=void 0===e.eval?"safe":e.eval,this.ignoreEvalErrors=void 0!==e.ignoreEvalErrors&&e.ignoreEvalErrors,this.parent=e.parent||null,this.parentProperty=e.parentProperty||null,this.callback=e.callback||i||null,this.otherTypeCallback=e.otherTypeCallback||s||function(){throw TypeError("You must supply an otherTypeCallback callback option with the @other() operator.")},!1!==e.autostart){let i={path:n?e.path:t};n?"json"in e&&(i.json=e.json):i.json=r;let s=this.evaluate(i);if(!s||"object"!=typeof s)throw new c(s);return s}}u.prototype.evaluate=function(e,t,r,i){let s=this.parent,n=this.parentProperty,{flatten:a,wrap:o}=this;if(this.currResultType=this.resultType,this.currEval=this.eval,this.currSandbox=this.sandbox,r=r||this.callback,this.currOtherTypeCallback=i||this.otherTypeCallback,t=t||this.json,(e=e||this.path)&&"object"==typeof e&&!Array.isArray(e)){if(!e.path&&""!==e.path)throw TypeError('You must supply a "path" property when providing an object argument to JSONPath.evaluate().');if(!Object.hasOwn(e,"json"))throw TypeError('You must supply a "json" property when providing an object argument to JSONPath.evaluate().');({json:t}=e),a=Object.hasOwn(e,"flatten")?e.flatten:a,this.currResultType=Object.hasOwn(e,"resultType")?e.resultType:this.currResultType,this.currSandbox=Object.hasOwn(e,"sandbox")?e.sandbox:this.currSandbox,o=Object.hasOwn(e,"wrap")?e.wrap:o,this.currEval=Object.hasOwn(e,"eval")?e.eval:this.currEval,r=Object.hasOwn(e,"callback")?e.callback:r,this.currOtherTypeCallback=Object.hasOwn(e,"otherTypeCallback")?e.otherTypeCallback:this.currOtherTypeCallback,s=Object.hasOwn(e,"parent")?e.parent:s,n=Object.hasOwn(e,"parentProperty")?e.parentProperty:n,e=e.path}if(s=s||null,n=n||null,Array.isArray(e)&&(e=u.toPathString(e)),!e&&""!==e||!t)return;let l=u.toPathArray(e);"$"===l[0]&&l.length>1&&l.shift(),this._hasParentSelector=null;let h=this._trace(l,t,["$"],s,n,r).filter(function(e){return e&&!e.isParentSelector});return h.length?o||1!==h.length||h[0].hasArrExpr?h.reduce((e,t)=>{let r=this._getPreferredOutput(t);return a&&Array.isArray(r)?e=e.concat(r):e.push(r),e},[]):this._getPreferredOutput(h[0]):o?[]:void 0},u.prototype._getPreferredOutput=function(e){let t=this.currResultType;switch(t){case"all":{let t=Array.isArray(e.path)?e.path:u.toPathArray(e.path);return e.pointer=u.toPointer(t),e.path="string"==typeof e.path?e.path:u.toPathString(e.path),e}case"value":case"parent":case"parentProperty":return e[t];case"path":return u.toPathString(e[t]);case"pointer":return u.toPointer(e.path);default:throw TypeError("Unknown result type")}},u.prototype._handleCallback=function(e,t,r){if(t){let i=this._getPreferredOutput(e);e.path="string"==typeof e.path?e.path:u.toPathString(e.path),t(i,r,e)}},u.prototype._trace=function(e,t,r,i,s,n,a,o){let l;if(!e.length)return l={path:r,value:t,parent:i,parentProperty:s,hasArrExpr:a},this._handleCallback(l,n,"value"),l;let c=e[0],u=e.slice(1),d=[];function b(e){Array.isArray(e)?e.forEach(e=>{d.push(e)}):d.push(e)}if(("string"!=typeof c||o)&&t&&Object.hasOwn(t,c))b(this._trace(u,t[c],h(r,c),t,c,n,a));else if("*"===c)this._walk(t,e=>{b(this._trace(u,t[e],h(r,e),t,e,n,!0,!0))});else if(".."===c)b(this._trace(u,t,r,i,s,n,a)),this._walk(t,i=>{"object"==typeof t[i]&&b(this._trace(e.slice(),t[i],h(r,i),t,i,n,!0))});else if("^"===c)return this._hasParentSelector=!0,{path:r.slice(0,-1),expr:u,isParentSelector:!0};else if("~"===c)return l={path:h(r,c),value:s,parent:i,parentProperty:null},this._handleCallback(l,n,"property"),l;else if("$"===c)b(this._trace(u,t,r,null,null,n,a));else if(/^(-?\d*):(-?\d*):?(\d*)$/u.test(c))b(this._slice(c,u,t,r,i,s,n));else if(0===c.indexOf("?(")){if(!1===this.currEval)throw Error("Eval [?(expr)] prevented in JSONPath expression.");let e=c.replace(/^\?\((.*?)\)$/u,"$1"),a=/@.?([^?]*)[['](\??\(.*?\))(?!.\)\])[\]']/gu.exec(e);a?this._walk(t,e=>{let o=[a[2]],l=a[1]?t[e][a[1]]:t[e];this._trace(o,l,r,i,s,n,!0).length>0&&b(this._trace(u,t[e],h(r,e),t,e,n,!0))}):this._walk(t,a=>{this._eval(e,t[a],a,r,i,s)&&b(this._trace(u,t[a],h(r,a),t,a,n,!0))})}else if("("===c[0]){if(!1===this.currEval)throw Error("Eval [(expr)] prevented in JSONPath expression.");b(this._trace(p(this._eval(c,t,r.at(-1),r.slice(0,-1),i,s),u),t,r,i,s,n,a))}else if("@"===c[0]){let e=!1,a=c.slice(1,-2);switch(a){case"scalar":t&&["object","function"].includes(typeof t)||(e=!0);break;case"boolean":case"string":case"undefined":case"function":typeof t===a&&(e=!0);break;case"integer":Number.isFinite(t)&&!(t%1)&&(e=!0);break;case"number":Number.isFinite(t)&&(e=!0);break;case"nonFinite":"number"!=typeof t||Number.isFinite(t)||(e=!0);break;case"object":t&&typeof t===a&&(e=!0);break;case"array":Array.isArray(t)&&(e=!0);break;case"other":e=this.currOtherTypeCallback(t,r,i,s);break;case"null":null===t&&(e=!0);break;default:throw TypeError("Unknown value type "+a)}if(e)return l={path:r,value:t,parent:i,parentProperty:s},this._handleCallback(l,n,"value"),l}else if("`"===c[0]&&t&&Object.hasOwn(t,c.slice(1))){let e=c.slice(1);b(this._trace(u,t[e],h(r,e),t,e,n,a,!0))}else if(c.includes(","))for(let e of c.split(","))b(this._trace(p(e,u),t,r,i,s,n,!0));else!o&&t&&Object.hasOwn(t,c)&&b(this._trace(u,t[c],h(r,c),t,c,n,a,!0));if(this._hasParentSelector)for(let e=0;e<d.length;e++){let r=d[e];if(r&&r.isParentSelector){let o=this._trace(r.expr,t,r.path,i,s,n,a);if(Array.isArray(o)){d[e]=o[0];let t=o.length;for(let r=1;r<t;r++)e++,d.splice(e,0,o[r])}else d[e]=o}}return d},u.prototype._walk=function(e,t){if(Array.isArray(e)){let r=e.length;for(let e=0;e<r;e++)t(e)}else e&&"object"==typeof e&&Object.keys(e).forEach(e=>{t(e)})},u.prototype._slice=function(e,t,r,i,s,n,a){if(!Array.isArray(r))return;let o=r.length,l=e.split(":"),h=l[2]&&Number.parseInt(l[2])||1,c=l[0]&&Number.parseInt(l[0])||0,u=l[1]&&Number.parseInt(l[1])||o;c=c<0?Math.max(0,c+o):Math.min(o,c),u=u<0?Math.max(0,u+o):Math.min(o,u);let d=[];for(let e=c;e<u;e+=h)this._trace(p(e,t),r,i,s,n,a,!0).forEach(e=>{d.push(e)});return d},u.prototype._eval=function(e,t,r,i,s,n){this.currSandbox._$_parentProperty=n,this.currSandbox._$_parent=s,this.currSandbox._$_property=r,this.currSandbox._$_root=this.json,this.currSandbox._$_v=t;let a=e.includes("@path");a&&(this.currSandbox._$_path=u.toPathString(i.concat([r])));let o=this.currEval+"Script:"+e;if(!u.cache[o]){let t=e.replaceAll("@parentProperty","_$_parentProperty").replaceAll("@parent","_$_parent").replaceAll("@property","_$_property").replaceAll("@root","_$_root").replaceAll(/@([.\s)[])/gu,"_$_v$1");if(a&&(t=t.replaceAll("@path","_$_path")),"safe"===this.currEval||!0===this.currEval||void 0===this.currEval)u.cache[o]=new this.safeVm.Script(t);else if("native"===this.currEval)u.cache[o]=new this.vm.Script(t);else if("function"==typeof this.currEval&&this.currEval.prototype&&Object.hasOwn(this.currEval.prototype,"runInNewContext")){let e=this.currEval;u.cache[o]=new e(t)}else if("function"==typeof this.currEval)u.cache[o]={runInNewContext:e=>this.currEval(t,e)};else throw TypeError(`Unknown "eval" property "${this.currEval}"`)}try{return u.cache[o].runInNewContext(this.currSandbox)}catch(t){if(this.ignoreEvalErrors)return!1;throw Error("jsonPath: "+t.message+": "+e)}},u.cache={},u.toPathString=function(e){let t=e.length,r="$";for(let i=1;i<t;i++)/^(~|\^|@.*?\(\))$/u.test(e[i])||(r+=/^[0-9*]+$/u.test(e[i])?"["+e[i]+"]":"['"+e[i]+"']");return r},u.toPointer=function(e){let t=e.length,r="";for(let i=1;i<t;i++)/^(~|\^|@.*?\(\))$/u.test(e[i])||(r+="/"+e[i].toString().replaceAll("~","~0").replaceAll("/","~1"));return r},u.toPathArray=function(e){let{cache:t}=u;if(t[e])return t[e].concat();let r=[],i=e.replaceAll(/@(?:null|boolean|number|string|integer|undefined|nonFinite|scalar|array|object|function|other)\(\)/gu,";$&;").replaceAll(/[['](\??\(.*?\))[\]'](?!.\])/gu,function(e,t){return"[#"+(r.push(t)-1)+"]"}).replaceAll(/\[['"]([^'\]]*)['"]\]/gu,function(e,t){return"['"+t.replaceAll(".","%@%").replaceAll("~","%%@@%%")+"']"}).replaceAll("~",";~;").replaceAll(/['"]?\.['"]?(?![^[]*\])|\[['"]?/gu,";").replaceAll("%@%",".").replaceAll("%%@@%%","~").replaceAll(/(?:;)?(\^+)(?:;)?/gu,function(e,t){return";"+t.split("").join(";")+";"}).replaceAll(/;;;|;;/gu,";..;").replaceAll(/;$|'?\]|'$/gu,"").split(";").map(function(e){let t=e.match(/#(\d+)/u);return t&&t[1]?r[t[1]]:e});return t[e]=i,t[e].concat()},u.prototype.safeVm={Script:class{constructor(e){this.code=e,this.ast=s(this.code)}runInNewContext(e){let t=Object.assign(Object.create(null),e);return l.evalAst(this.ast,t)}}},u.prototype.vm=r,module.exports=t})();