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
- const base = `${
10
- ( nodeRequire . main && nodeRequire . main . filename && dirname ( nodeRequire . main . filename ) ) || global . process . cwd ( )
11
- } /`;
9
+ const base = `${ ( require ?. main ?. filename && dirname ( require . main . filename ) ) || global . process . cwd ( ) } /` ;
12
10
13
11
// It's specifically a module
14
12
const file = basename ( filename , '.js' ) ;
15
- // eslint-disable-next-line no-param-reassign
16
- filename = dirname ( filename ) ;
17
- let n = filename . lastIndexOf ( '/node_modules/' ) ;
13
+
14
+ const path = dirname ( filename ) ;
15
+ let n = path . lastIndexOf ( '/node_modules/' ) ;
18
16
if ( n > - 1 ) {
19
17
// /node_modules/ is 14 chars
20
- return `${ filename . substr ( n + 14 ) . replace ( / \/ / g, '.' ) } :${ file } ` ;
18
+ return `${ path . substr ( n + 14 ) . replace ( / \/ / g, '.' ) } :${ file } ` ;
21
19
}
22
20
// Let's see if it's a part of the main module
23
21
// To be a part of main module, it has to share the same base
24
- n = `${ filename } /` . lastIndexOf ( base , 0 ) ;
22
+ n = `${ path } /` . lastIndexOf ( base , 0 ) ;
25
23
26
24
if ( n === 0 ) {
27
- let moduleName = filename . substr ( base . length ) . replace ( / \/ / g, '.' ) ;
25
+ let moduleName = path . substr ( base . length ) . replace ( / \/ / g, '.' ) ;
28
26
if ( moduleName ) {
29
27
moduleName += ':' ;
30
28
}
@@ -37,83 +35,81 @@ function getModule(nodeRequire: NodeRequire | undefined, filename: string | unde
37
35
const FILENAME_MATCH = / ^ \s * [ - ] { 4 , } $ / ;
38
36
const FULL_MATCH = / a t (?: ( .+ ?) \s + \( ) ? (?: ( .+ ?) : ( \d + ) (?: : ( \d + ) ) ? | ( [ ^ ) ] + ) ) \) ? / ;
39
37
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
- }
52
-
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 ;
38
+ export const node : StackLineParser = ( line : string ) => {
39
+ if ( line . match ( FILENAME_MATCH ) ) {
40
+ return {
41
+ filename : line ,
42
+ } ;
43
+ }
58
44
59
- if ( lineMatch [ 1 ] ) {
60
- functionName = lineMatch [ 1 ] ;
45
+ const lineMatch = line . match ( FULL_MATCH ) ;
46
+ if ( ! lineMatch ) {
47
+ return undefined ;
48
+ }
61
49
62
- let methodStart = functionName . lastIndexOf ( '.' ) ;
63
- if ( functionName [ methodStart - 1 ] === '.' ) {
64
- // eslint-disable-next-line no-plusplus
65
- methodStart -- ;
66
- }
50
+ let object : string | undefined ;
51
+ let method : string | undefined ;
52
+ let functionName : string | undefined ;
53
+ let typeName : string | undefined ;
54
+ let methodName : string | undefined ;
67
55
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 ;
78
- }
56
+ if ( lineMatch [ 1 ] ) {
57
+ functionName = lineMatch [ 1 ] ;
79
58
80
- if ( method ) {
81
- typeName = object ;
82
- methodName = method ;
59
+ let methodStart = functionName . lastIndexOf ( '.' ) ;
60
+ if ( functionName [ methodStart - 1 ] === '.' ) {
61
+ // eslint-disable-next-line no-plusplus
62
+ methodStart -- ;
83
63
}
84
64
85
- if ( method === '<anonymous>' ) {
86
- methodName = undefined ;
87
- functionName = undefined ;
65
+ if ( methodStart > 0 ) {
66
+ object = functionName . substr ( 0 , methodStart ) ;
67
+ method = functionName . substr ( methodStart + 1 ) ;
68
+ const objectEnd = object . indexOf ( '.Module' ) ;
69
+ if ( objectEnd > 0 ) {
70
+ functionName = functionName . substr ( objectEnd + 1 ) ;
71
+ object = object . substr ( 0 , objectEnd ) ;
72
+ }
88
73
}
74
+ typeName = undefined ;
75
+ }
89
76
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
- }
77
+ if ( method ) {
78
+ typeName = object ;
79
+ methodName = method ;
80
+ }
99
81
100
- const filename = lineMatch [ 2 ] ;
101
- const isNative = lineMatch [ 5 ] === 'native' ;
102
- const isInternal =
103
- isNative || ( filename && ! filename . startsWith ( '/' ) && ! filename . startsWith ( '.' ) && filename . indexOf ( ':\\' ) !== 1 ) ;
82
+ if ( method === '<anonymous>' ) {
83
+ methodName = undefined ;
84
+ functionName = undefined ;
85
+ }
104
86
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/' ) ;
87
+ let fn ;
88
+ try {
89
+ fn = functionName || `${ typeName } .${ methodName || '<anonymous>' } ` ;
90
+ } catch ( _ ) {
91
+ // This seems to happen sometimes when using 'use strict',
92
+ // stemming from `getTypeName`.
93
+ // [TypeError: Cannot read property 'constructor' of undefined]
94
+ fn = '<anonymous>' ;
95
+ }
109
96
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
- } ;
97
+ const filename = lineMatch [ 2 ] ;
98
+ const isNative = lineMatch [ 5 ] === 'native' ;
99
+ const isInternal =
100
+ isNative || ( filename && ! filename . startsWith ( '/' ) && ! filename . startsWith ( '.' ) && filename . indexOf ( ':\\' ) !== 1 ) ;
101
+
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/' ) ;
106
+
107
+ return {
108
+ filename,
109
+ module : getModule ( filename ) ,
110
+ function : fn ,
111
+ lineno : parseInt ( lineMatch [ 3 ] , 10 ) || undefined ,
112
+ colno : parseInt ( lineMatch [ 4 ] , 10 ) || undefined ,
113
+ in_app,
118
114
} ;
119
115
} ;
0 commit comments