reportingobserver.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 INTEGRATION_NAME = 'ReportingObserver';
  6. const SETUP_CLIENTS = new WeakMap();
  7. const _reportingObserverIntegration = ((options = {}) => {
  8. const types = options.types || ['crash', 'deprecation', 'intervention'];
  9. /** Handler for the reporting observer. */
  10. function handler(reports) {
  11. if (!SETUP_CLIENTS.has(core.getClient() )) {
  12. return;
  13. }
  14. for (const report of reports) {
  15. core.withScope(scope => {
  16. scope.setExtra('url', report.url);
  17. const label = `ReportingObserver [${report.type}]`;
  18. let details = 'No details available';
  19. if (report.body) {
  20. // Object.keys doesn't work on ReportBody, as all properties are inheirted
  21. const plainBody
  22. = {};
  23. // eslint-disable-next-line guard-for-in
  24. for (const prop in report.body) {
  25. plainBody[prop] = report.body[prop];
  26. }
  27. scope.setExtra('body', plainBody);
  28. if (report.type === 'crash') {
  29. const body = report.body ;
  30. // A fancy way to create a message out of crashId OR reason OR both OR fallback
  31. details = [body.crashId || '', body.reason || ''].join(' ').trim() || details;
  32. } else {
  33. const body = report.body ;
  34. details = body.message || details;
  35. }
  36. }
  37. core.captureMessage(`${label}: ${details}`);
  38. });
  39. }
  40. }
  41. return {
  42. name: INTEGRATION_NAME,
  43. setupOnce() {
  44. if (!utils.supportsReportingObserver()) {
  45. return;
  46. }
  47. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
  48. const observer = new (WINDOW ).ReportingObserver(handler, {
  49. buffered: true,
  50. types,
  51. });
  52. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  53. observer.observe();
  54. },
  55. setup(client) {
  56. SETUP_CLIENTS.set(client, true);
  57. },
  58. };
  59. }) ;
  60. const reportingObserverIntegration = core.defineIntegration(_reportingObserverIntegration);
  61. /**
  62. * Reporting API integration - https://w3c.github.io/reporting/
  63. * @deprecated Use `reportingObserverIntegration()` instead.
  64. */
  65. // eslint-disable-next-line deprecation/deprecation
  66. const ReportingObserver = core.convertIntegrationFnToClass(
  67. INTEGRATION_NAME,
  68. reportingObserverIntegration,
  69. )
  70. ;
  71. exports.ReportingObserver = ReportingObserver;
  72. exports.reportingObserverIntegration = reportingObserverIntegration;
  73. //# sourceMappingURL=reportingobserver.js.map