1- import type { DebugImage , StackFrame , StackParser } from '@sentry/types' ;
1+ import type { DebugImage , StackParser } from '@sentry/types' ;
22import { GLOBAL_OBJ } from './worldwide' ;
33
4- const debugIdStackParserCache = new WeakMap < StackParser , Map < string , StackFrame [ ] > > ( ) ;
4+ type StackString = string ;
5+ type CachedResult = [ string , string ] ;
6+
7+ let parsedStackResults : Record < StackString , CachedResult > | undefined ;
8+ let lastKeysCount : number | undefined ;
9+ let cachedFilenameDebugIds : Record < string , string > | undefined ;
510
611/**
712 * Returns a map of filenames to debug identifiers.
@@ -12,38 +17,46 @@ export function getFilenameToDebugIdMap(stackParser: StackParser): Record<string
1217 return { } ;
1318 }
1419
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 ) ;
20+ const debugIdKeys = Object . keys ( debugIdMap ) ;
21+
22+ // If the count of registered globals hasn't changed since the last call, we
23+ // can just return the cached result.
24+ if ( cachedFilenameDebugIds && debugIdKeys . length === lastKeysCount ) {
25+ return cachedFilenameDebugIds ;
2226 }
2327
28+ lastKeysCount = debugIdKeys . length ;
29+
2430 // Build a map of filename -> debug_id.
25- return Object . keys ( debugIdMap ) . reduce < Record < string , string > > ( ( acc , debugIdStackTrace ) => {
26- let parsedStack : StackFrame [ ] ;
31+ cachedFilenameDebugIds = debugIdKeys . reduce < Record < string , string > > ( ( acc , stackKey ) => {
32+ if ( ! parsedStackResults ) {
33+ parsedStackResults = { } ;
34+ }
35+
36+ const result = parsedStackResults [ stackKey ] ;
2737
28- const cachedParsedStack = debugIdStackFramesCache . get ( debugIdStackTrace ) ;
29- if ( cachedParsedStack ) {
30- parsedStack = cachedParsedStack ;
38+ if ( result ) {
39+ acc [ result [ 0 ] ] = result [ 1 ] ;
3140 } else {
32- parsedStack = stackParser ( debugIdStackTrace ) ;
33- debugIdStackFramesCache . set ( debugIdStackTrace , parsedStack ) ;
34- }
41+ const parsedStack = stackParser ( stackKey ) ;
3542
36- for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
37- const stackFrame = parsedStack [ i ] ;
38- const file = stackFrame && stackFrame . filename ;
43+ for ( let i = parsedStack . length - 1 ; i >= 0 ; i -- ) {
44+ const stackFrame = parsedStack [ i ] ;
45+ const filename = stackFrame && stackFrame . filename ;
46+ const debugId = debugIdMap [ stackKey ] ;
3947
40- if ( stackFrame && file ) {
41- acc [ file ] = debugIdMap [ debugIdStackTrace ] as string ;
42- break ;
48+ if ( filename && debugId ) {
49+ acc [ filename ] = debugId ;
50+ parsedStackResults [ stackKey ] = [ filename , debugId ] ;
51+ break ;
52+ }
4353 }
4454 }
55+
4556 return acc ;
4657 } , { } ) ;
58+
59+ return cachedFilenameDebugIds ;
4760}
4861
4962/**
@@ -55,6 +68,10 @@ export function getDebugImagesForResources(
5568) : DebugImage [ ] {
5669 const filenameDebugIdMap = getFilenameToDebugIdMap ( stackParser ) ;
5770
71+ if ( ! filenameDebugIdMap ) {
72+ return [ ] ;
73+ }
74+
5875 const images : DebugImage [ ] = [ ] ;
5976 for ( const path of resource_paths ) {
6077 if ( path && filenameDebugIdMap [ path ] ) {
0 commit comments