metadata.js 3.0 KB

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