metadata.js 2.8 KB

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