|
| 1 | +import type { Event, StackParser } from '@sentry/types'; |
| 2 | +import { GLOBAL_OBJ } from '@sentry/utils'; |
| 3 | + |
| 4 | +/** Keys are source filename/url, values are metadata objects. */ |
| 5 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 6 | +const filenameMetadataMap = new Map<string, any>(); |
| 7 | +/** Set of stack strings that have already been parsed. */ |
| 8 | +const parsedStacks = new Set<string>(); |
| 9 | + |
| 10 | +function ensureMetadataStacksAreParsed(parser: StackParser): void { |
| 11 | + if (!GLOBAL_OBJ._sentryModuleMetadata) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + for (const stack of Object.keys(GLOBAL_OBJ._sentryModuleMetadata)) { |
| 16 | + const metadata = GLOBAL_OBJ._sentryModuleMetadata[stack]; |
| 17 | + |
| 18 | + if (parsedStacks.has(stack)) { |
| 19 | + continue; |
| 20 | + } |
| 21 | + |
| 22 | + // Ensure this stack doesn't get parsed again |
| 23 | + parsedStacks.add(stack); |
| 24 | + |
| 25 | + const frames = parser(stack); |
| 26 | + |
| 27 | + // Go through the frames starting from the top of the stack and find the first one with a filename |
| 28 | + for (const frame of frames.reverse()) { |
| 29 | + if (frame.filename) { |
| 30 | + // Save the metadata for this filename |
| 31 | + filenameMetadataMap.set(frame.filename, metadata); |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Retrieve metadata for a specific JavaScript file URL. |
| 40 | + * |
| 41 | + * Metadata is injected by the Sentry bundler plugins using the `_experiments.moduleMetadata` config option. |
| 42 | + */ |
| 43 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 44 | +export function getMetadataForUrl(parser: StackParser, filename: string): any | undefined { |
| 45 | + ensureMetadataStacksAreParsed(parser); |
| 46 | + return filenameMetadataMap.get(filename); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Adds metadata to stack frames. |
| 51 | + * |
| 52 | + * Metadata is injected by the Sentry bundler plugins using the `_experiments.moduleMetadata` config option. |
| 53 | + */ |
| 54 | +export function addMetadataToStackFrames(parser: StackParser, event: Event): void { |
| 55 | + try { |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 57 | + event.exception!.values!.forEach(exception => { |
| 58 | + if (!exception.stacktrace) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + for (const frame of exception.stacktrace.frames || []) { |
| 63 | + if (!frame.filename) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + const metadata = getMetadataForUrl(parser, frame.filename); |
| 68 | + |
| 69 | + if (metadata) { |
| 70 | + frame.module_metadata = metadata; |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + } catch (_) { |
| 75 | + // To save bundle size we're just try catching here instead of checking for the existence of all the different objects. |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Strips metadata from stack frames. |
| 81 | + */ |
| 82 | +export function stripMetadataFromStackFrames(event: Event): void { |
| 83 | + try { |
| 84 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 85 | + event.exception!.values!.forEach(exception => { |
| 86 | + if (!exception.stacktrace) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + for (const frame of exception.stacktrace.frames || []) { |
| 91 | + delete frame.module_metadata; |
| 92 | + } |
| 93 | + }); |
| 94 | + } catch (_) { |
| 95 | + // To save bundle size we're just try catching here instead of checking for the existence of all the different objects. |
| 96 | + } |
| 97 | +} |
0 commit comments