aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
3 lines (2 loc) • 2.6 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.captureStackTrace=captureStackTrace,exports.captureCallStack=captureCallStack,exports.parseErrorStack=parseErrorStack,exports.renderCallStackJustMyCode=renderCallStackJustMyCode;var debug_1=()=>{var tmp=require("./debug");return debug_1=()=>tmp,tmp};function captureStackTrace(below=captureStackTrace,limit=Number.MAX_SAFE_INTEGER){if(!(0,debug_1().debugModeEnabled)())return["stack traces disabled"];const previousLimit=Error.stackTraceLimit;try{return Error.stackTraceLimit=limit,renderCallStackJustMyCode(captureCallStack(below),!1)}finally{Error.stackTraceLimit=previousLimit}}function captureCallStack(upTo){const obj={};Error.captureStackTrace(obj,upTo);let trace=parseErrorStack(obj.stack);return trace.length===0&&(Error.captureStackTrace(obj),trace=parseErrorStack(obj.stack)),trace}function parseErrorStack(stack){const lines=stack.split(`
`),framePrefix=" at ";return lines.filter(line=>line.startsWith(framePrefix)).map(line=>{line=line.slice(framePrefix.length);let fileName,functionName,sourceLocation;const paren=line.indexOf("(");paren?(functionName=line.slice(0,paren-1),line=line.slice(paren+1,-1)):functionName="<entry>",functionName==="Object.<anonymous>"&&(functionName="<anonymous>");let asI=functionName.indexOf(" [as ");asI>-1&&(functionName=functionName.slice(0,asI));const m=line.match(/(:[0-9]+){0,2}$/);return fileName=m?line.slice(0,-m[0].length):line,sourceLocation=m?m[0].slice(1):"",{fileName,functionName,sourceLocation}})}function renderCallStackJustMyCode(stack,indent=!0){const moduleRe=/(\/|\\)node_modules(\/|\\)([^/\\]+)/,lines=[];let skipped=new Array,i=0;for(;i<stack.length;){const frame=stack[i++],pat=frame.fileName.match(moduleRe);if(pat){for(;i<stack.length&&stack[i].fileName.includes(pat[0]);)i++;skip({functionName:stack[i-1].functionName,fileName:pat[3]})}else if(frame.fileName.includes("node:"))for(skip({fileName:"node internals"});i<stack.length&&stack[i].fileName.includes("node:");)i++;else{reportSkipped(!0);const prefix=indent?" at ":"";lines.push(`${prefix}${frame.functionName} (${frame.fileName}:${frame.sourceLocation})`)}}return reportSkipped(!1),lines;function skip(what){skipped.find(x=>x.fileName===what.fileName&&x.functionName===what.functionName)||skipped.push(what)}function reportSkipped(includeFunction){if(skipped.length>0){const rendered=skipped.map((s,j)=>j===skipped.length-1&&s.functionName&&includeFunction?`${s.functionName} in ${s.fileName}`:s.fileName),prefix=indent?" ":"";lines.push(`${prefix}...${rendered.join(", ")}...`)}skipped=[]}}