UNPKG

2.47 kBJavaScriptView Raw
1import { supportsReportingObserver, GLOBAL_OBJ } from '@sentry/utils';
2
3const WINDOW = GLOBAL_OBJ ;
4
5/** Reporting API integration - https://w3c.github.io/reporting/ */
6class ReportingObserver {
7 /**
8 * @inheritDoc
9 */
10 static __initStatic() {this.id = 'ReportingObserver';}
11
12 /**
13 * @inheritDoc
14 */
15 __init() {this.name = ReportingObserver.id;}
16
17 /**
18 * Returns current hub.
19 */
20
21 /**
22 * @inheritDoc
23 */
24 constructor(
25 _options
26
27 = {
28 types: ['crash', 'deprecation', 'intervention'],
29 },
30 ) {this._options = _options;ReportingObserver.prototype.__init.call(this);}
31
32 /**
33 * @inheritDoc
34 */
35 setupOnce(_, getCurrentHub) {
36 if (!supportsReportingObserver()) {
37 return;
38 }
39
40 this._getCurrentHub = getCurrentHub;
41
42 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
43 const observer = new (WINDOW ).ReportingObserver(this.handler.bind(this), {
44 buffered: true,
45 types: this._options.types,
46 });
47
48 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
49 observer.observe();
50 }
51
52 /**
53 * @inheritDoc
54 */
55 handler(reports) {
56 const hub = this._getCurrentHub && this._getCurrentHub();
57 if (!hub || !hub.getIntegration(ReportingObserver)) {
58 return;
59 }
60 for (const report of reports) {
61 hub.withScope(scope => {
62 scope.setExtra('url', report.url);
63
64 const label = `ReportingObserver [${report.type}]`;
65 let details = 'No details available';
66
67 if (report.body) {
68 // Object.keys doesn't work on ReportBody, as all properties are inheirted
69 const plainBody
70
71 = {};
72
73 // eslint-disable-next-line guard-for-in
74 for (const prop in report.body) {
75 plainBody[prop] = report.body[prop];
76 }
77
78 scope.setExtra('body', plainBody);
79
80 if (report.type === 'crash') {
81 const body = report.body ;
82 // A fancy way to create a message out of crashId OR reason OR both OR fallback
83 details = [body.crashId || '', body.reason || ''].join(' ').trim() || details;
84 } else {
85 const body = report.body ;
86 details = body.message || details;
87 }
88 }
89
90 hub.captureMessage(`${label}: ${details}`);
91 });
92 }
93 }
94} ReportingObserver.__initStatic();
95
96export { ReportingObserver };
97//# sourceMappingURL=reportingobserver.js.map