-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): Single implementation to fetch debug ids (#14199)
In the process of fixing getsentry/sentry-electron#1011 I was looking for a way to get a `Map<Filename, DebugId>`. I found that there was already some code duplication for parsing and caching of debug ids from the polyfilled globals. This PR moves the common code to `@sentry/utils` which will be useful for fixing the above.
- Loading branch information
Showing
5 changed files
with
92 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { DebugImage, StackFrame, StackParser } from '@sentry/types'; | ||
import { GLOBAL_OBJ } from './worldwide'; | ||
|
||
const debugIdStackParserCache = new WeakMap<StackParser, Map<string, StackFrame[]>>(); | ||
|
||
/** | ||
* Returns a map of filenames to debug identifiers. | ||
*/ | ||
export function getFilenameToDebugIdMap(stackParser: StackParser): Record<string, string> { | ||
const debugIdMap = GLOBAL_OBJ._sentryDebugIds; | ||
if (!debugIdMap) { | ||
return {}; | ||
} | ||
|
||
let debugIdStackFramesCache: Map<string, StackFrame[]>; | ||
const cachedDebugIdStackFrameCache = debugIdStackParserCache.get(stackParser); | ||
if (cachedDebugIdStackFrameCache) { | ||
debugIdStackFramesCache = cachedDebugIdStackFrameCache; | ||
} else { | ||
debugIdStackFramesCache = new Map<string, StackFrame[]>(); | ||
debugIdStackParserCache.set(stackParser, debugIdStackFramesCache); | ||
} | ||
|
||
// Build a map of filename -> debug_id. | ||
return Object.keys(debugIdMap).reduce<Record<string, string>>((acc, debugIdStackTrace) => { | ||
let parsedStack: StackFrame[]; | ||
|
||
const cachedParsedStack = debugIdStackFramesCache.get(debugIdStackTrace); | ||
if (cachedParsedStack) { | ||
parsedStack = cachedParsedStack; | ||
} else { | ||
parsedStack = stackParser(debugIdStackTrace); | ||
debugIdStackFramesCache.set(debugIdStackTrace, parsedStack); | ||
} | ||
|
||
for (let i = parsedStack.length - 1; i >= 0; i--) { | ||
const stackFrame = parsedStack[i]; | ||
const file = stackFrame && stackFrame.filename; | ||
|
||
if (stackFrame && file) { | ||
acc[file] = debugIdMap[debugIdStackTrace] as string; | ||
break; | ||
} | ||
} | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
/** | ||
* Returns a list of debug images for the given resources. | ||
*/ | ||
export function getDebugImagesForResources( | ||
stackParser: StackParser, | ||
resource_paths: ReadonlyArray<string>, | ||
): DebugImage[] { | ||
const filenameDebugIdMap = getFilenameToDebugIdMap(stackParser); | ||
|
||
const images: DebugImage[] = []; | ||
for (const path of resource_paths) { | ||
if (path && filenameDebugIdMap[path]) { | ||
images.push({ | ||
type: 'sourcemap', | ||
code_file: path, | ||
debug_id: filenameDebugIdMap[path] as string, | ||
}); | ||
} | ||
} | ||
|
||
return images; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters