-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdebug-ids.ts
70 lines (60 loc) · 2.09 KB
/
debug-ids.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
}