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