1 | import { BaseClient, SDK_VERSION } from '@sentry/core';
|
2 | import { getSDKSource, logger, createClientReportEnvelope, dsnToString } from '@sentry/utils';
|
3 | import { eventFromException, eventFromMessage } from './eventbuilder.js';
|
4 | import { WINDOW } from './helpers.js';
|
5 | import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs.js';
|
6 | import { createUserFeedbackEnvelope } from './userfeedback.js';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | class BrowserClient extends BaseClient {
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 | constructor(options) {
|
26 | const sdkSource = WINDOW.SENTRY_SDK_SOURCE || getSDKSource();
|
27 |
|
28 | options._metadata = options._metadata || {};
|
29 | options._metadata.sdk = options._metadata.sdk || {
|
30 | name: 'sentry.javascript.browser',
|
31 | packages: [
|
32 | {
|
33 | name: `${sdkSource}:@sentry/browser`,
|
34 | version: SDK_VERSION,
|
35 | },
|
36 | ],
|
37 | version: SDK_VERSION,
|
38 | };
|
39 |
|
40 | super(options);
|
41 |
|
42 | if (options.sendClientReports && WINDOW.document) {
|
43 | WINDOW.document.addEventListener('visibilitychange', () => {
|
44 | if (WINDOW.document.visibilityState === 'hidden') {
|
45 | this._flushOutcomes();
|
46 | }
|
47 | });
|
48 | }
|
49 | }
|
50 |
|
51 | |
52 |
|
53 |
|
54 | eventFromException(exception, hint) {
|
55 | return eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace);
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 | eventFromMessage(
|
62 | message,
|
63 |
|
64 | level = 'info',
|
65 | hint,
|
66 | ) {
|
67 | return eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace);
|
68 | }
|
69 |
|
70 | |
71 |
|
72 |
|
73 | sendEvent(event, hint) {
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | const breadcrumbIntegration = this.getIntegrationById(BREADCRUMB_INTEGRATION_ID) ;
|
81 |
|
82 |
|
83 | if (breadcrumbIntegration && breadcrumbIntegration.addSentryBreadcrumb) {
|
84 | breadcrumbIntegration.addSentryBreadcrumb(event);
|
85 | }
|
86 |
|
87 | super.sendEvent(event, hint);
|
88 | }
|
89 |
|
90 | |
91 |
|
92 |
|
93 | captureUserFeedback(feedback) {
|
94 | if (!this._isEnabled()) {
|
95 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('SDK not enabled, will not capture user feedback.');
|
96 | return;
|
97 | }
|
98 |
|
99 | const envelope = createUserFeedbackEnvelope(feedback, {
|
100 | metadata: this.getSdkMetadata(),
|
101 | dsn: this.getDsn(),
|
102 | tunnel: this.getOptions().tunnel,
|
103 | });
|
104 | void this._sendEnvelope(envelope);
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 | _prepareEvent(event, hint, scope) {
|
111 | event.platform = event.platform || 'javascript';
|
112 | return super._prepareEvent(event, hint, scope);
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 | _flushOutcomes() {
|
119 | const outcomes = this._clearOutcomes();
|
120 |
|
121 | if (outcomes.length === 0) {
|
122 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('No outcomes to send');
|
123 | return;
|
124 | }
|
125 |
|
126 | if (!this._dsn) {
|
127 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('No dsn provided, will not send outcomes');
|
128 | return;
|
129 | }
|
130 |
|
131 | (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('Sending outcomes:', outcomes);
|
132 |
|
133 | const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn));
|
134 | void this._sendEnvelope(envelope);
|
135 | }
|
136 | }
|
137 |
|
138 | export { BrowserClient };
|
139 |
|