UNPKG

cross-caller

Version:

Gets the caller function, file and line number in all browsers and server environments

1 lines 3.24 kB
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["export interface CallSite {\r\n /** The caller's function name */\r\n function: string;\r\n\r\n /** The caller's file uri */\r\n file: string;\r\n\r\n /** The caller's line number */\r\n line: string;\r\n\r\n /** The caller's position in the line (column number) */\r\n position: string;\r\n}\r\n\r\n// V8, CharkaCore, GraalJS, Hermes, QuickJS-NG, LibJS, Rhino-V8, Espruino - with function names\r\n// \"at {function} ({file}:{line}:{position})\"\r\nconst stackRegex1 = /at\\s+(.*?)\\s+\\((.*):(\\d*):(\\d*)\\)/i;\r\n\r\n// V8, CharkaCore, GraalJS, LibJS, Rhino - without function names\r\n// \"at {file}:{line}:{position}\"\r\nconst stackRegex2 = /at\\s+()(.*):(\\d*):(\\d*)/i;\r\n\r\n// JavaScriptCore, SpiderMonkey, Rhino-Mozilla and V4 - both with and without function names and positions\r\n// \"{function}@{file}:{line}:{position}\"\r\nconst stackRegex3 = /(.*?)(?:\\(\\))?@(?:(.*?):(\\d+)(?::(\\d+))?)?/i;\r\n\r\n// QuickJS, XS, Duktape and Nashorn - without position\r\n// \"at {function} ({file}:{line})\"\r\nconst stackRegex4 = /at\\s*(.*)\\s+\\((.*):(\\d*)()\\)/i;\r\n\r\n/**\r\n * Parses the call site at the specified depth from a stack trace.\r\n * @param stack The stack trace string, typically from an Error object.\r\n * @param callerDepth The depth of the caller function in the stack. Zero means the immediate caller.\r\n */\r\nexport function parseCallSite(stack: string, callerDepth: number): CallSite | null {\r\n const list = stack.trim().split('\\n');\r\n\r\n for (let i = 0, depth = 0; i < list.length; i++) {\r\n const line = list[i];\r\n const match = stackRegex1.exec(line)\r\n || stackRegex2.exec(line)\r\n || stackRegex3.exec(line)\r\n || stackRegex4.exec(line);\r\n\r\n if (!match || match.length !== 5) {\r\n continue;\r\n }\r\n\r\n if (depth++ === callerDepth) {\r\n return {\r\n function: match[1] || '',\r\n file: match[2] || '',\r\n line: match[3] || '',\r\n position: match[4] || '',\r\n };\r\n }\r\n }\r\n\r\n return null;\r\n}\r\n\r\n/**\r\n * Gets the call site of the caller function.\r\n * @param callerDepth The depth of the caller function in the stack. Default is 0, which means the immediate caller.\r\n */\r\nexport function getCaller(callerDepth: number = 0): CallSite | null {\r\n return parseCallSite(new Error().stack || '', callerDepth + 2);\r\n}\r\n"],"names":["stackRegex1","stackRegex2","stackRegex3","stackRegex4","parseCallSite","stack","callerDepth","list","trim","split","i","depth","length","line","match","exec","function","file","position","getCaller","Error"],"mappings":"AAgBA,IAAMA,EAAc,qCAIdC,EAAc,2BAIdC,EAAc,8CAIdC,EAAc,gCAOJ,SAAAC,EAAcC,EAAeC,GAG3C,IAFA,IAAMC,EAAOF,EAAMG,OAAOC,MAAM,MAEvBC,EAAI,EAAGC,EAAQ,EAAGD,EAAIH,EAAKK,OAAQF,IAAK,CAC/C,IAAMG,EAAON,EAAKG,GACZI,EAAQd,EAAYe,KAAKF,IAC1BZ,EAAYc,KAAKF,IACjBX,EAAYa,KAAKF,IACjBV,EAAYY,KAAKF,GAEtB,GAAKC,GAA0B,IAAjBA,EAAMF,QAIhBD,MAAYL,EACd,MAAO,CACLU,SAAUF,EAAM,IAAM,GACtBG,KAAMH,EAAM,IAAM,GAClBD,KAAMC,EAAM,IAAM,GAClBI,SAAUJ,EAAM,IAAM,GAG5B,CAEA,OACF,IAAA,CAMgB,SAAAK,EAAUb,GACxB,YADwB,IAAAA,IAAAA,EAAsB,GACvCF,GAAc,IAAIgB,OAAQf,OAAS,GAAIC,EAAc,EAC9D"}