|
| 1 | +// This code was originally forked from https://github.com/felixge/node-stack-trace |
| 2 | +// Since then it has been highly modified to fit our needs. |
| 3 | + |
| 4 | +// Copyright (c) 2011 Felix Geisendörfer ([email protected])// |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions:// |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software.// |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | + |
| 24 | +import type { StackLineParserFn } from '@sentry/types'; |
| 25 | + |
| 26 | +export type GetModuleFn = (filename: string | undefined) => string | undefined; |
| 27 | + |
| 28 | +/** Node Stack line parser */ |
| 29 | +// eslint-disable-next-line complexity |
| 30 | +export function node(getModule?: GetModuleFn): StackLineParserFn { |
| 31 | + const FILENAME_MATCH = /^\s*[-]{4,}$/; |
| 32 | + const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+):(\d+):(\d+)?|([^)]+))\)?/; |
| 33 | + |
| 34 | + // eslint-disable-next-line complexity |
| 35 | + return (line: string) => { |
| 36 | + const lineMatch = line.match(FULL_MATCH); |
| 37 | + |
| 38 | + if (lineMatch) { |
| 39 | + let object: string | undefined; |
| 40 | + let method: string | undefined; |
| 41 | + let functionName: string | undefined; |
| 42 | + let typeName: string | undefined; |
| 43 | + let methodName: string | undefined; |
| 44 | + |
| 45 | + if (lineMatch[1]) { |
| 46 | + functionName = lineMatch[1]; |
| 47 | + |
| 48 | + let methodStart = functionName.lastIndexOf('.'); |
| 49 | + if (functionName[methodStart - 1] === '.') { |
| 50 | + methodStart--; |
| 51 | + } |
| 52 | + |
| 53 | + if (methodStart > 0) { |
| 54 | + object = functionName.slice(0, methodStart); |
| 55 | + method = functionName.slice(methodStart + 1); |
| 56 | + const objectEnd = object.indexOf('.Module'); |
| 57 | + if (objectEnd > 0) { |
| 58 | + functionName = functionName.slice(objectEnd + 1); |
| 59 | + object = object.slice(0, objectEnd); |
| 60 | + } |
| 61 | + } |
| 62 | + typeName = undefined; |
| 63 | + } |
| 64 | + |
| 65 | + if (method) { |
| 66 | + typeName = object; |
| 67 | + methodName = method; |
| 68 | + } |
| 69 | + |
| 70 | + if (method === '<anonymous>') { |
| 71 | + methodName = undefined; |
| 72 | + functionName = undefined; |
| 73 | + } |
| 74 | + |
| 75 | + if (functionName === undefined) { |
| 76 | + methodName = methodName || '<anonymous>'; |
| 77 | + functionName = typeName ? `${typeName}.${methodName}` : methodName; |
| 78 | + } |
| 79 | + |
| 80 | + let filename = lineMatch[2] && lineMatch[2].startsWith('file://') ? lineMatch[2].slice(7) : lineMatch[2]; |
| 81 | + const isNative = lineMatch[5] === 'native'; |
| 82 | + |
| 83 | + if (!filename && lineMatch[5] && !isNative) { |
| 84 | + filename = lineMatch[5]; |
| 85 | + } |
| 86 | + |
| 87 | + const isInternal = |
| 88 | + isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && !filename.includes(':\\')); |
| 89 | + |
| 90 | + // in_app is all that's not an internal Node function or a module within node_modules |
| 91 | + // note that isNative appears to return true even for node core libraries |
| 92 | + // see https://github.com/getsentry/raven-node/issues/176 |
| 93 | + |
| 94 | + const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/'); |
| 95 | + |
| 96 | + return { |
| 97 | + filename, |
| 98 | + module: getModule ? getModule(filename) : undefined, |
| 99 | + function: functionName, |
| 100 | + lineno: parseInt(lineMatch[3], 10) || undefined, |
| 101 | + colno: parseInt(lineMatch[4], 10) || undefined, |
| 102 | + in_app, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + if (line.match(FILENAME_MATCH)) { |
| 107 | + return { |
| 108 | + filename: line, |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + return undefined; |
| 113 | + }; |
| 114 | +} |
0 commit comments