|
1 | 1 | import { StackLineParser, StackLineParserFn } from '@sentry/types'; |
2 | | -import { basename, createStackParser, dirname } from '@sentry/utils'; |
3 | | - |
4 | | -/** Gets the module */ |
5 | | -function getModule(filename: string | undefined): string | undefined { |
6 | | - if (!filename) { |
7 | | - return; |
8 | | - } |
9 | | - |
10 | | - // We could use optional chaining here but webpack does like that mixed with require |
11 | | - const base = `${ |
12 | | - (require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd() |
13 | | - }/`; |
14 | | - |
15 | | - // It's specifically a module |
16 | | - const file = basename(filename, '.js'); |
17 | | - |
18 | | - const path = dirname(filename); |
19 | | - let n = path.lastIndexOf('/node_modules/'); |
20 | | - if (n > -1) { |
21 | | - // /node_modules/ is 14 chars |
22 | | - return `${path.substr(n + 14).replace(/\//g, '.')}:${file}`; |
23 | | - } |
24 | | - // Let's see if it's a part of the main module |
25 | | - // To be a part of main module, it has to share the same base |
26 | | - n = `${path}/`.lastIndexOf(base, 0); |
27 | | - |
28 | | - if (n === 0) { |
29 | | - let moduleName = path.substr(base.length).replace(/\//g, '.'); |
30 | | - if (moduleName) { |
31 | | - moduleName += ':'; |
32 | | - } |
33 | | - moduleName += file; |
34 | | - return moduleName; |
35 | | - } |
36 | | - return file; |
37 | | -} |
38 | 2 |
|
39 | 3 | const FILENAME_MATCH = /^\s*[-]{4,}$/; |
40 | 4 | const FULL_MATCH = /at (?:async )?(?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/; |
41 | 5 |
|
| 6 | +type GetModuleFn = (filename: string | undefined) => string | undefined; |
| 7 | + |
42 | 8 | // eslint-disable-next-line complexity |
43 | | -const node: StackLineParserFn = (line: string) => { |
44 | | - if (line.match(FILENAME_MATCH)) { |
45 | | - return { |
46 | | - filename: line, |
47 | | - }; |
48 | | - } |
| 9 | +function node(getModule?: GetModuleFn): StackLineParserFn { |
| 10 | + // eslint-disable-next-line complexity |
| 11 | + return (line: string) => { |
| 12 | + if (line.match(FILENAME_MATCH)) { |
| 13 | + return { |
| 14 | + filename: line, |
| 15 | + }; |
| 16 | + } |
49 | 17 |
|
50 | | - const lineMatch = line.match(FULL_MATCH); |
51 | | - if (!lineMatch) { |
52 | | - return undefined; |
53 | | - } |
| 18 | + const lineMatch = line.match(FULL_MATCH); |
| 19 | + if (!lineMatch) { |
| 20 | + return undefined; |
| 21 | + } |
54 | 22 |
|
55 | | - let object: string | undefined; |
56 | | - let method: string | undefined; |
57 | | - let functionName: string | undefined; |
58 | | - let typeName: string | undefined; |
59 | | - let methodName: string | undefined; |
| 23 | + let object: string | undefined; |
| 24 | + let method: string | undefined; |
| 25 | + let functionName: string | undefined; |
| 26 | + let typeName: string | undefined; |
| 27 | + let methodName: string | undefined; |
60 | 28 |
|
61 | | - if (lineMatch[1]) { |
62 | | - functionName = lineMatch[1]; |
| 29 | + if (lineMatch[1]) { |
| 30 | + functionName = lineMatch[1]; |
63 | 31 |
|
64 | | - let methodStart = functionName.lastIndexOf('.'); |
65 | | - if (functionName[methodStart - 1] === '.') { |
66 | | - // eslint-disable-next-line no-plusplus |
67 | | - methodStart--; |
68 | | - } |
| 32 | + let methodStart = functionName.lastIndexOf('.'); |
| 33 | + if (functionName[methodStart - 1] === '.') { |
| 34 | + // eslint-disable-next-line no-plusplus |
| 35 | + methodStart--; |
| 36 | + } |
69 | 37 |
|
70 | | - if (methodStart > 0) { |
71 | | - object = functionName.substr(0, methodStart); |
72 | | - method = functionName.substr(methodStart + 1); |
73 | | - const objectEnd = object.indexOf('.Module'); |
74 | | - if (objectEnd > 0) { |
75 | | - functionName = functionName.substr(objectEnd + 1); |
76 | | - object = object.substr(0, objectEnd); |
| 38 | + if (methodStart > 0) { |
| 39 | + object = functionName.substr(0, methodStart); |
| 40 | + method = functionName.substr(methodStart + 1); |
| 41 | + const objectEnd = object.indexOf('.Module'); |
| 42 | + if (objectEnd > 0) { |
| 43 | + functionName = functionName.substr(objectEnd + 1); |
| 44 | + object = object.substr(0, objectEnd); |
| 45 | + } |
77 | 46 | } |
| 47 | + typeName = undefined; |
78 | 48 | } |
79 | | - typeName = undefined; |
80 | | - } |
81 | 49 |
|
82 | | - if (method) { |
83 | | - typeName = object; |
84 | | - methodName = method; |
85 | | - } |
| 50 | + if (method) { |
| 51 | + typeName = object; |
| 52 | + methodName = method; |
| 53 | + } |
86 | 54 |
|
87 | | - if (method === '<anonymous>') { |
88 | | - methodName = undefined; |
89 | | - functionName = undefined; |
90 | | - } |
| 55 | + if (method === '<anonymous>') { |
| 56 | + methodName = undefined; |
| 57 | + functionName = undefined; |
| 58 | + } |
91 | 59 |
|
92 | | - if (functionName === undefined) { |
93 | | - methodName = methodName || '<anonymous>'; |
94 | | - functionName = typeName ? `${typeName}.${methodName}` : methodName; |
95 | | - } |
| 60 | + if (functionName === undefined) { |
| 61 | + methodName = methodName || '<anonymous>'; |
| 62 | + functionName = typeName ? `${typeName}.${methodName}` : methodName; |
| 63 | + } |
96 | 64 |
|
97 | | - const filename = lineMatch[2]?.startsWith('file://') ? lineMatch[2].substr(7) : lineMatch[2]; |
98 | | - const isNative = lineMatch[5] === 'native'; |
99 | | - const isInternal = |
100 | | - isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1); |
| 65 | + const filename = lineMatch[2]?.startsWith('file://') ? lineMatch[2].substr(7) : lineMatch[2]; |
| 66 | + const isNative = lineMatch[5] === 'native'; |
| 67 | + const isInternal = |
| 68 | + isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1); |
101 | 69 |
|
102 | | - // in_app is all that's not an internal Node function or a module within node_modules |
103 | | - // note that isNative appears to return true even for node core libraries |
104 | | - // see https://github.com/getsentry/raven-node/issues/176 |
105 | | - const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/'); |
| 70 | + // in_app is all that's not an internal Node function or a module within node_modules |
| 71 | + // note that isNative appears to return true even for node core libraries |
| 72 | + // see https://github.com/getsentry/raven-node/issues/176 |
| 73 | + const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/'); |
106 | 74 |
|
107 | | - return { |
108 | | - filename, |
109 | | - module: getModule(filename), |
110 | | - function: functionName, |
111 | | - lineno: parseInt(lineMatch[3], 10) || undefined, |
112 | | - colno: parseInt(lineMatch[4], 10) || undefined, |
113 | | - in_app, |
| 75 | + return { |
| 76 | + filename, |
| 77 | + module: getModule?.(filename), |
| 78 | + function: functionName, |
| 79 | + lineno: parseInt(lineMatch[3], 10) || undefined, |
| 80 | + colno: parseInt(lineMatch[4], 10) || undefined, |
| 81 | + in_app, |
| 82 | + }; |
114 | 83 | }; |
115 | | -}; |
116 | | - |
117 | | -export const nodeStackLineParser: StackLineParser = [90, node]; |
| 84 | +} |
118 | 85 |
|
119 | | -export const defaultStackParser = createStackParser(nodeStackLineParser); |
| 86 | +/** Node.js stack line parser */ |
| 87 | +export function nodeStackLineParser(getModule?: GetModuleFn): StackLineParser { |
| 88 | + return [90, node(getModule)]; |
| 89 | +} |
0 commit comments