1
- import type { DebugImage , StackFrame , StackParser } from '@sentry/types' ;
1
+ import type { DebugImage , StackParser } from '@sentry/types' ;
2
2
import { GLOBAL_OBJ } from './worldwide' ;
3
3
4
- const debugIdStackParserCache = new WeakMap < StackParser , Map < string , StackFrame [ ] > > ( ) ;
4
+ type StackString = string ;
5
+
6
+ interface CachedResult {
7
+ filename : string ;
8
+ debugId : string ;
9
+ }
10
+
11
+ let debugIdStackParserCache : WeakMap < StackParser , Map < StackString , CachedResult > > | undefined ;
12
+
13
+ function getCacheForStackParser ( stackParser : StackParser ) : Map < StackString , CachedResult > {
14
+ if ( ! debugIdStackParserCache ) {
15
+ debugIdStackParserCache = new WeakMap ( ) ;
16
+ }
17
+
18
+ let result = debugIdStackParserCache . get ( stackParser ) ;
19
+
20
+ if ( ! result ) {
21
+ result = new Map ( ) ;
22
+ debugIdStackParserCache . set ( stackParser , result ) ;
23
+ }
24
+
25
+ return result ;
26
+ }
27
+
28
+ let lastDebugIdKeyCount = 0 ;
29
+ let cachedFilenameToDebugId : Map < string , string > | undefined ;
5
30
6
31
/**
7
32
* Returns a map of filenames to debug identifiers.
8
33
*/
9
- export function getFilenameToDebugIdMap ( stackParser : StackParser ) : Record < string , string > {
34
+ export function getFilenameToDebugIdMap ( stackParser : StackParser ) : Map < string , string > {
10
35
const debugIdMap = GLOBAL_OBJ . _sentryDebugIds ;
11
36
if ( ! debugIdMap ) {
12
- return { } ;
37
+ return new Map ( ) ;
13
38
}
14
39
15
- let debugIdStackFramesCache : Map < string , StackFrame [ ] > ;
16
- const cachedDebugIdStackFrameCache = debugIdStackParserCache . get ( stackParser ) ;
17
- if ( cachedDebugIdStackFrameCache ) {
18
- debugIdStackFramesCache = cachedDebugIdStackFrameCache ;
19
- } else {
20
- debugIdStackFramesCache = new Map < string , StackFrame [ ] > ( ) ;
21
- debugIdStackParserCache . set ( stackParser , debugIdStackFramesCache ) ;
40
+ const debugIdKeys = Object . keys ( debugIdMap ) ;
41
+
42
+ // If the count of registered globals hasn't changed since the last call, we
43
+ // can just return the cached result.
44
+ if ( debugIdKeys . length === lastDebugIdKeyCount && cachedFilenameToDebugId ) {
45
+ return cachedFilenameToDebugId ;
22
46
}
23
47
48
+ const debugIdStackFramesCache = getCacheForStackParser ( stackParser ) ;
49
+
24
50
// Build a map of filename -> debug_id.
25
- return Object . keys ( debugIdMap ) . reduce < Record < string , string > > ( ( acc , debugIdStackTrace ) => {
26
- let parsedStack : StackFrame [ ] ;
27
-
28
- const cachedParsedStack = debugIdStackFramesCache . get ( debugIdStackTrace ) ;
29
- if ( cachedParsedStack ) {
30
- parsedStack = cachedParsedStack ;
31
- } else {
32
- parsedStack = stackParser ( debugIdStackTrace ) ;
33
- debugIdStackFramesCache . set ( debugIdStackTrace , parsedStack ) ;
34
- }
51
+ const output = debugIdKeys . reduce < Map < string , string > > ( ( acc , debugIdStackTrace ) => {
52
+ let result = debugIdStackFramesCache . get ( debugIdStackTrace ) ;
53
+
54
+ if ( ! result ) {
55
+ const parsedStack = stackParser ( debugIdStackTrace ) ;
35
56
36
- for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
37
- const stackFrame = parsedStack [ i ] ;
38
- const file = stackFrame && stackFrame . filename ;
57
+ for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
58
+ const stackFrame = parsedStack [ i ] ;
59
+ const filename = stackFrame && stackFrame . filename ;
60
+ const debugId = debugIdMap [ debugIdStackTrace ] ;
39
61
40
- if ( stackFrame && file ) {
41
- acc [ file ] = debugIdMap [ debugIdStackTrace ] as string ;
42
- break ;
62
+ if ( filename && debugId ) {
63
+ result = { filename, debugId } ;
64
+ debugIdStackFramesCache . set ( debugIdStackTrace , result ) ;
65
+ break ;
66
+ }
43
67
}
44
68
}
69
+
70
+ if ( result ) {
71
+ acc . set ( result . filename , result . debugId ) ;
72
+ }
73
+
45
74
return acc ;
46
- } , { } ) ;
75
+ } , new Map ( ) ) ;
76
+
77
+ lastDebugIdKeyCount = Object . keys ( debugIdMap ) . length ;
78
+ cachedFilenameToDebugId = output ;
79
+
80
+ return output ;
47
81
}
48
82
49
83
/**
@@ -57,11 +91,12 @@ export function getDebugImagesForResources(
57
91
58
92
const images : DebugImage [ ] = [ ] ;
59
93
for ( const path of resource_paths ) {
60
- if ( path && filenameDebugIdMap [ path ] ) {
94
+ const debug_id = filenameDebugIdMap . get ( path ) ;
95
+ if ( path && debug_id ) {
61
96
images . push ( {
62
97
type : 'sourcemap' ,
63
98
code_file : path ,
64
- debug_id : filenameDebugIdMap [ path ] as string ,
99
+ debug_id,
65
100
} ) ;
66
101
}
67
102
}
0 commit comments