|
1 | 1 | import { basename, dirname, StackLineParser } from '@sentry/utils';
|
2 | 2 |
|
3 | 3 | /** Gets the module */
|
4 |
| -function getModule(nodeRequire: NodeRequire | undefined, filename: string | undefined): string | undefined { |
5 |
| - if (!filename || !nodeRequire) { |
| 4 | +function getModule(filename: string | undefined): string | undefined { |
| 5 | + if (!filename) { |
6 | 6 | return;
|
7 | 7 | }
|
8 | 8 |
|
9 | 9 | const base = `${
|
10 |
| - (nodeRequire.main && nodeRequire.main.filename && dirname(nodeRequire.main.filename)) || global.process.cwd() |
| 10 | + (require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd() |
11 | 11 | }/`;
|
12 | 12 |
|
13 | 13 | // It's specifically a module
|
@@ -37,83 +37,81 @@ function getModule(nodeRequire: NodeRequire | undefined, filename: string | unde
|
37 | 37 | const FILENAME_MATCH = /^\s*[-]{4,}$/;
|
38 | 38 | const FULL_MATCH = /at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
|
39 | 39 |
|
40 |
| -export const node = (nodeRequire: NodeRequire | undefined): StackLineParser => { |
41 |
| - return (line: string) => { |
42 |
| - if (line.match(FILENAME_MATCH)) { |
43 |
| - return { |
44 |
| - filename: line, |
45 |
| - }; |
46 |
| - } |
47 |
| - |
48 |
| - const lineMatch = line.match(FULL_MATCH); |
49 |
| - if (!lineMatch) { |
50 |
| - return undefined; |
51 |
| - } |
| 40 | +export const node: StackLineParser = (line: string) => { |
| 41 | + if (line.match(FILENAME_MATCH)) { |
| 42 | + return { |
| 43 | + filename: line, |
| 44 | + }; |
| 45 | + } |
52 | 46 |
|
53 |
| - let object: string | undefined; |
54 |
| - let method: string | undefined; |
55 |
| - let functionName: string | undefined; |
56 |
| - let typeName: string | undefined; |
57 |
| - let methodName: string | undefined; |
| 47 | + const lineMatch = line.match(FULL_MATCH); |
| 48 | + if (!lineMatch) { |
| 49 | + return undefined; |
| 50 | + } |
58 | 51 |
|
59 |
| - if (lineMatch[1]) { |
60 |
| - functionName = lineMatch[1]; |
| 52 | + let object: string | undefined; |
| 53 | + let method: string | undefined; |
| 54 | + let functionName: string | undefined; |
| 55 | + let typeName: string | undefined; |
| 56 | + let methodName: string | undefined; |
61 | 57 |
|
62 |
| - let methodStart = functionName.lastIndexOf('.'); |
63 |
| - if (functionName[methodStart - 1] === '.') { |
64 |
| - // eslint-disable-next-line no-plusplus |
65 |
| - methodStart--; |
66 |
| - } |
| 58 | + if (lineMatch[1]) { |
| 59 | + functionName = lineMatch[1]; |
67 | 60 |
|
68 |
| - if (methodStart > 0) { |
69 |
| - object = functionName.substr(0, methodStart); |
70 |
| - method = functionName.substr(methodStart + 1); |
71 |
| - const objectEnd = object.indexOf('.Module'); |
72 |
| - if (objectEnd > 0) { |
73 |
| - functionName = functionName.substr(objectEnd + 1); |
74 |
| - object = object.substr(0, objectEnd); |
75 |
| - } |
76 |
| - } |
77 |
| - typeName = undefined; |
| 61 | + let methodStart = functionName.lastIndexOf('.'); |
| 62 | + if (functionName[methodStart - 1] === '.') { |
| 63 | + // eslint-disable-next-line no-plusplus |
| 64 | + methodStart--; |
78 | 65 | }
|
79 | 66 |
|
80 |
| - if (method) { |
81 |
| - typeName = object; |
82 |
| - methodName = method; |
83 |
| - } |
84 |
| - |
85 |
| - if (method === '<anonymous>') { |
86 |
| - methodName = undefined; |
87 |
| - functionName = undefined; |
| 67 | + if (methodStart > 0) { |
| 68 | + object = functionName.substr(0, methodStart); |
| 69 | + method = functionName.substr(methodStart + 1); |
| 70 | + const objectEnd = object.indexOf('.Module'); |
| 71 | + if (objectEnd > 0) { |
| 72 | + functionName = functionName.substr(objectEnd + 1); |
| 73 | + object = object.substr(0, objectEnd); |
| 74 | + } |
88 | 75 | }
|
| 76 | + typeName = undefined; |
| 77 | + } |
89 | 78 |
|
90 |
| - let fn; |
91 |
| - try { |
92 |
| - fn = functionName || `${typeName}.${methodName || '<anonymous>'}`; |
93 |
| - } catch (_) { |
94 |
| - // This seems to happen sometimes when using 'use strict', |
95 |
| - // stemming from `getTypeName`. |
96 |
| - // [TypeError: Cannot read property 'constructor' of undefined] |
97 |
| - fn = '<anonymous>'; |
98 |
| - } |
| 79 | + if (method) { |
| 80 | + typeName = object; |
| 81 | + methodName = method; |
| 82 | + } |
99 | 83 |
|
100 |
| - const filename = lineMatch[2]; |
101 |
| - const isNative = lineMatch[5] === 'native'; |
102 |
| - const isInternal = |
103 |
| - isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1); |
| 84 | + if (method === '<anonymous>') { |
| 85 | + methodName = undefined; |
| 86 | + functionName = undefined; |
| 87 | + } |
104 | 88 |
|
105 |
| - // in_app is all that's not an internal Node function or a module within node_modules |
106 |
| - // note that isNative appears to return true even for node core libraries |
107 |
| - // see https://github.com/getsentry/raven-node/issues/176 |
108 |
| - const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/'); |
| 89 | + let fn; |
| 90 | + try { |
| 91 | + fn = functionName || `${typeName}.${methodName || '<anonymous>'}`; |
| 92 | + } catch (_) { |
| 93 | + // This seems to happen sometimes when using 'use strict', |
| 94 | + // stemming from `getTypeName`. |
| 95 | + // [TypeError: Cannot read property 'constructor' of undefined] |
| 96 | + fn = '<anonymous>'; |
| 97 | + } |
109 | 98 |
|
110 |
| - return { |
111 |
| - filename, |
112 |
| - module: getModule(nodeRequire, filename), |
113 |
| - function: fn, |
114 |
| - lineno: parseInt(lineMatch[3], 10) || undefined, |
115 |
| - colno: parseInt(lineMatch[4], 10) || undefined, |
116 |
| - in_app, |
117 |
| - }; |
| 99 | + const filename = lineMatch[2]; |
| 100 | + const isNative = lineMatch[5] === 'native'; |
| 101 | + const isInternal = |
| 102 | + isNative || (filename && !filename.startsWith('/') && !filename.startsWith('.') && filename.indexOf(':\\') !== 1); |
| 103 | + |
| 104 | + // in_app is all that's not an internal Node function or a module within node_modules |
| 105 | + // note that isNative appears to return true even for node core libraries |
| 106 | + // see https://github.com/getsentry/raven-node/issues/176 |
| 107 | + const in_app = !isInternal && filename !== undefined && !filename.includes('node_modules/'); |
| 108 | + |
| 109 | + return { |
| 110 | + filename, |
| 111 | + module: getModule(filename), |
| 112 | + function: fn, |
| 113 | + lineno: parseInt(lineMatch[3], 10) || undefined, |
| 114 | + colno: parseInt(lineMatch[4], 10) || undefined, |
| 115 | + in_app, |
118 | 116 | };
|
119 | 117 | };
|
0 commit comments