UNPKG

4.44 kBJavaScriptView Raw
1Object.defineProperty(exports, '__esModule', { value: true });
2
3const core = require('@sentry/core');
4const utils = require('@sentry/utils');
5const eventbuilder = require('./eventbuilder.js');
6const helpers = require('./helpers.js');
7const breadcrumbs = require('./integrations/breadcrumbs.js');
8const userfeedback = require('./userfeedback.js');
9
10/**
11 * Configuration options for the Sentry Browser SDK.
12 * @see @sentry/types Options for more information.
13 */
14
15/**
16 * The Sentry Browser SDK Client.
17 *
18 * @see BrowserOptions for documentation on configuration options.
19 * @see SentryClient for usage documentation.
20 */
21class BrowserClient extends core.BaseClient {
22 /**
23 * Creates a new Browser SDK instance.
24 *
25 * @param options Configuration options for this SDK.
26 */
27 constructor(options) {
28 const sdkSource = helpers.WINDOW.SENTRY_SDK_SOURCE || utils.getSDKSource();
29
30 options._metadata = options._metadata || {};
31 options._metadata.sdk = options._metadata.sdk || {
32 name: 'sentry.javascript.browser',
33 packages: [
34 {
35 name: `${sdkSource}:@sentry/browser`,
36 version: core.SDK_VERSION,
37 },
38 ],
39 version: core.SDK_VERSION,
40 };
41
42 super(options);
43
44 if (options.sendClientReports && helpers.WINDOW.document) {
45 helpers.WINDOW.document.addEventListener('visibilitychange', () => {
46 if (helpers.WINDOW.document.visibilityState === 'hidden') {
47 this._flushOutcomes();
48 }
49 });
50 }
51 }
52
53 /**
54 * @inheritDoc
55 */
56 eventFromException(exception, hint) {
57 return eventbuilder.eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace);
58 }
59
60 /**
61 * @inheritDoc
62 */
63 eventFromMessage(
64 message,
65 // eslint-disable-next-line deprecation/deprecation
66 level = 'info',
67 hint,
68 ) {
69 return eventbuilder.eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace);
70 }
71
72 /**
73 * @inheritDoc
74 */
75 sendEvent(event, hint) {
76 // We only want to add the sentry event breadcrumb when the user has the breadcrumb integration installed and
77 // activated its `sentry` option.
78 // We also do not want to use the `Breadcrumbs` class here directly, because we do not want it to be included in
79 // bundles, if it is not used by the SDK.
80 // This all sadly is a bit ugly, but we currently don't have a "pre-send" hook on the integrations so we do it this
81 // way for now.
82 const breadcrumbIntegration = this.getIntegrationById(breadcrumbs.BREADCRUMB_INTEGRATION_ID) ;
83 // We check for definedness of `addSentryBreadcrumb` in case users provided their own integration with id
84 // "Breadcrumbs" that does not have this function.
85 if (breadcrumbIntegration && breadcrumbIntegration.addSentryBreadcrumb) {
86 breadcrumbIntegration.addSentryBreadcrumb(event);
87 }
88
89 super.sendEvent(event, hint);
90 }
91
92 /**
93 * Sends user feedback to Sentry.
94 */
95 captureUserFeedback(feedback) {
96 if (!this._isEnabled()) {
97 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.warn('SDK not enabled, will not capture user feedback.');
98 return;
99 }
100
101 const envelope = userfeedback.createUserFeedbackEnvelope(feedback, {
102 metadata: this.getSdkMetadata(),
103 dsn: this.getDsn(),
104 tunnel: this.getOptions().tunnel,
105 });
106 void this._sendEnvelope(envelope);
107 }
108
109 /**
110 * @inheritDoc
111 */
112 _prepareEvent(event, hint, scope) {
113 event.platform = event.platform || 'javascript';
114 return super._prepareEvent(event, hint, scope);
115 }
116
117 /**
118 * Sends client reports as an envelope.
119 */
120 _flushOutcomes() {
121 const outcomes = this._clearOutcomes();
122
123 if (outcomes.length === 0) {
124 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log('No outcomes to send');
125 return;
126 }
127
128 if (!this._dsn) {
129 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log('No dsn provided, will not send outcomes');
130 return;
131 }
132
133 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && utils.logger.log('Sending outcomes:', outcomes);
134
135 const envelope = utils.createClientReportEnvelope(outcomes, this._options.tunnel && utils.dsnToString(this._dsn));
136 void this._sendEnvelope(envelope);
137 }
138}
139
140exports.BrowserClient = BrowserClient;
141//# sourceMappingURL=client.js.map