contextlines.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. const WINDOW = utils.GLOBAL_OBJ ;
  5. const DEFAULT_LINES_OF_CONTEXT = 7;
  6. const INTEGRATION_NAME = 'ContextLines';
  7. const _contextLinesIntegration = ((options = {}) => {
  8. const contextLines = options.frameContextLines != null ? options.frameContextLines : DEFAULT_LINES_OF_CONTEXT;
  9. return {
  10. name: INTEGRATION_NAME,
  11. // TODO v8: Remove this
  12. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  13. processEvent(event) {
  14. return addSourceContext(event, contextLines);
  15. },
  16. };
  17. }) ;
  18. const contextLinesIntegration = core.defineIntegration(_contextLinesIntegration);
  19. /**
  20. * Collects source context lines around the lines of stackframes pointing to JS embedded in
  21. * the current page's HTML.
  22. *
  23. * This integration DOES NOT work for stack frames pointing to JS files that are loaded by the browser.
  24. * For frames pointing to files, context lines are added during ingestion and symbolication
  25. * by attempting to download the JS files to the Sentry backend.
  26. *
  27. * Use this integration if you have inline JS code in HTML pages that can't be accessed
  28. * by our backend (e.g. due to a login-protected page).
  29. *
  30. * @deprecated Use `contextLinesIntegration()` instead.
  31. */
  32. // eslint-disable-next-line deprecation/deprecation
  33. const ContextLines = core.convertIntegrationFnToClass(INTEGRATION_NAME, contextLinesIntegration)
  34. ;
  35. /**
  36. * Processes an event and adds context lines.
  37. */
  38. function addSourceContext(event, contextLines) {
  39. const doc = WINDOW.document;
  40. const htmlFilename = WINDOW.location && utils.stripUrlQueryAndFragment(WINDOW.location.href);
  41. if (!doc || !htmlFilename) {
  42. return event;
  43. }
  44. const exceptions = event.exception && event.exception.values;
  45. if (!exceptions || !exceptions.length) {
  46. return event;
  47. }
  48. const html = doc.documentElement.innerHTML;
  49. if (!html) {
  50. return event;
  51. }
  52. const htmlLines = ['<!DOCTYPE html>', '<html>', ...html.split('\n'), '</html>'];
  53. exceptions.forEach(exception => {
  54. const stacktrace = exception.stacktrace;
  55. if (stacktrace && stacktrace.frames) {
  56. stacktrace.frames = stacktrace.frames.map(frame =>
  57. applySourceContextToFrame(frame, htmlLines, htmlFilename, contextLines),
  58. );
  59. }
  60. });
  61. return event;
  62. }
  63. /**
  64. * Only exported for testing
  65. */
  66. function applySourceContextToFrame(
  67. frame,
  68. htmlLines,
  69. htmlFilename,
  70. linesOfContext,
  71. ) {
  72. if (frame.filename !== htmlFilename || !frame.lineno || !htmlLines.length) {
  73. return frame;
  74. }
  75. utils.addContextToFrame(htmlLines, frame, linesOfContext);
  76. return frame;
  77. }
  78. exports.ContextLines = ContextLines;
  79. exports.applySourceContextToFrame = applySourceContextToFrame;
  80. exports.contextLinesIntegration = contextLinesIntegration;
  81. //# sourceMappingURL=contextlines.js.map