UNPKG

9.11 kBJavaScriptView Raw
1import { Integrations, getIntegrationsToSetup, initAndBind, getReportDialogEndpoint, getCurrentHub } from '@sentry/core';
2import { stackParserFromStackParserOptions, supportsFetch, logger, resolvedSyncPromise, addInstrumentationHandler } from '@sentry/utils';
3import { BrowserClient } from './client.js';
4import { WINDOW, wrap as wrap$1 } from './helpers.js';
5import { GlobalHandlers } from './integrations/globalhandlers.js';
6import { TryCatch } from './integrations/trycatch.js';
7import { Breadcrumbs } from './integrations/breadcrumbs.js';
8import { LinkedErrors } from './integrations/linkederrors.js';
9import { HttpContext } from './integrations/httpcontext.js';
10import { Dedupe } from './integrations/dedupe.js';
11import { defaultStackParser } from './stack-parsers.js';
12import { makeFetchTransport } from './transports/fetch.js';
13import { makeXHRTransport } from './transports/xhr.js';
14
15const defaultIntegrations = [
16 new Integrations.InboundFilters(),
17 new Integrations.FunctionToString(),
18 new TryCatch(),
19 new Breadcrumbs(),
20 new GlobalHandlers(),
21 new LinkedErrors(),
22 new Dedupe(),
23 new HttpContext(),
24];
25
26/**
27 * A magic string that build tooling can leverage in order to inject a release value into the SDK.
28 */
29
30/**
31 * The Sentry Browser SDK Client.
32 *
33 * To use this SDK, call the {@link init} function as early as possible when
34 * loading the web page. To set context information or send manual events, use
35 * the provided methods.
36 *
37 * @example
38 *
39 * ```
40 *
41 * import { init } from '@sentry/browser';
42 *
43 * init({
44 * dsn: '__DSN__',
45 * // ...
46 * });
47 * ```
48 *
49 * @example
50 * ```
51 *
52 * import { configureScope } from '@sentry/browser';
53 * configureScope((scope: Scope) => {
54 * scope.setExtra({ battery: 0.7 });
55 * scope.setTag({ user_mode: 'admin' });
56 * scope.setUser({ id: '4711' });
57 * });
58 * ```
59 *
60 * @example
61 * ```
62 *
63 * import { addBreadcrumb } from '@sentry/browser';
64 * addBreadcrumb({
65 * message: 'My Breadcrumb',
66 * // ...
67 * });
68 * ```
69 *
70 * @example
71 *
72 * ```
73 *
74 * import * as Sentry from '@sentry/browser';
75 * Sentry.captureMessage('Hello, world!');
76 * Sentry.captureException(new Error('Good bye'));
77 * Sentry.captureEvent({
78 * message: 'Manual',
79 * stacktrace: [
80 * // ...
81 * ],
82 * });
83 * ```
84 *
85 * @see {@link BrowserOptions} for documentation on configuration options.
86 */
87function init(options = {}) {
88 if (options.defaultIntegrations === undefined) {
89 options.defaultIntegrations = defaultIntegrations;
90 }
91 if (options.release === undefined) {
92 // This allows build tooling to find-and-replace __SENTRY_RELEASE__ to inject a release value
93 if (typeof __SENTRY_RELEASE__ === 'string') {
94 options.release = __SENTRY_RELEASE__;
95 }
96
97 // This supports the variable that sentry-webpack-plugin injects
98 if (WINDOW.SENTRY_RELEASE && WINDOW.SENTRY_RELEASE.id) {
99 options.release = WINDOW.SENTRY_RELEASE.id;
100 }
101 }
102 if (options.autoSessionTracking === undefined) {
103 options.autoSessionTracking = true;
104 }
105 if (options.sendClientReports === undefined) {
106 options.sendClientReports = true;
107 }
108
109 const clientOptions = {
110 ...options,
111 stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
112 integrations: getIntegrationsToSetup(options),
113 transport: options.transport || (supportsFetch() ? makeFetchTransport : makeXHRTransport),
114 };
115
116 initAndBind(BrowserClient, clientOptions);
117
118 if (options.autoSessionTracking) {
119 startSessionTracking();
120 }
121}
122
123/**
124 * Present the user with a report dialog.
125 *
126 * @param options Everything is optional, we try to fetch all info need from the global scope.
127 */
128function showReportDialog(options = {}, hub = getCurrentHub()) {
129 // doesn't work without a document (React Native)
130 if (!WINDOW.document) {
131 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('Global document not defined in showReportDialog call');
132 return;
133 }
134
135 const { client, scope } = hub.getStackTop();
136 const dsn = options.dsn || (client && client.getDsn());
137 if (!dsn) {
138 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('DSN not configured for showReportDialog call');
139 return;
140 }
141
142 if (scope) {
143 options.user = {
144 ...scope.getUser(),
145 ...options.user,
146 };
147 }
148
149 if (!options.eventId) {
150 options.eventId = hub.lastEventId();
151 }
152
153 const script = WINDOW.document.createElement('script');
154 script.async = true;
155 script.src = getReportDialogEndpoint(dsn, options);
156
157 if (options.onLoad) {
158 script.onload = options.onLoad;
159 }
160
161 const injectionPoint = WINDOW.document.head || WINDOW.document.body;
162 if (injectionPoint) {
163 injectionPoint.appendChild(script);
164 } else {
165 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.error('Not injecting report dialog. No injection point found in HTML');
166 }
167}
168
169/**
170 * This is the getter for lastEventId.
171 *
172 * @returns The last event id of a captured event.
173 */
174function lastEventId() {
175 return getCurrentHub().lastEventId();
176}
177
178/**
179 * This function is here to be API compatible with the loader.
180 * @hidden
181 */
182function forceLoad() {
183 // Noop
184}
185
186/**
187 * This function is here to be API compatible with the loader.
188 * @hidden
189 */
190function onLoad(callback) {
191 callback();
192}
193
194/**
195 * Call `flush()` on the current client, if there is one. See {@link Client.flush}.
196 *
197 * @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause
198 * the client to wait until all events are sent before resolving the promise.
199 * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
200 * doesn't (or if there's no client defined).
201 */
202function flush(timeout) {
203 const client = getCurrentHub().getClient();
204 if (client) {
205 return client.flush(timeout);
206 }
207 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Cannot flush events. No client defined.');
208 return resolvedSyncPromise(false);
209}
210
211/**
212 * Call `close()` on the current client, if there is one. See {@link Client.close}.
213 *
214 * @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this
215 * parameter will cause the client to wait until all events are sent before disabling itself.
216 * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
217 * doesn't (or if there's no client defined).
218 */
219function close(timeout) {
220 const client = getCurrentHub().getClient();
221 if (client) {
222 return client.close(timeout);
223 }
224 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('Cannot flush events and disable SDK. No client defined.');
225 return resolvedSyncPromise(false);
226}
227
228/**
229 * Wrap code within a try/catch block so the SDK is able to capture errors.
230 *
231 * @param fn A function to wrap.
232 *
233 * @returns The result of wrapped function call.
234 */
235// eslint-disable-next-line @typescript-eslint/no-explicit-any
236function wrap(fn) {
237 return wrap$1(fn)();
238}
239
240function startSessionOnHub(hub) {
241 hub.startSession({ ignoreDuration: true });
242 hub.captureSession();
243}
244
245/**
246 * Enable automatic Session Tracking for the initial page load.
247 */
248function startSessionTracking() {
249 if (typeof WINDOW.document === 'undefined') {
250 (typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) &&
251 logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
252 return;
253 }
254
255 const hub = getCurrentHub();
256
257 // The only way for this to be false is for there to be a version mismatch between @sentry/browser (>= 6.0.0) and
258 // @sentry/hub (< 5.27.0). In the simple case, there won't ever be such a mismatch, because the two packages are
259 // pinned at the same version in package.json, but there are edge cases where it's possible. See
260 // https://github.com/getsentry/sentry-javascript/issues/3207 and
261 // https://github.com/getsentry/sentry-javascript/issues/3234 and
262 // https://github.com/getsentry/sentry-javascript/issues/3278.
263 if (!hub.captureSession) {
264 return;
265 }
266
267 // The session duration for browser sessions does not track a meaningful
268 // concept that can be used as a metric.
269 // Automatically captured sessions are akin to page views, and thus we
270 // discard their duration.
271 startSessionOnHub(hub);
272
273 // We want to create a session for every navigation as well
274 addInstrumentationHandler('history', ({ from, to }) => {
275 // Don't create an additional session for the initial route or if the location did not change
276 if (!(from === undefined || from === to)) {
277 startSessionOnHub(getCurrentHub());
278 }
279 });
280}
281
282/**
283 * Captures user feedback and sends it to Sentry.
284 */
285function captureUserFeedback(feedback) {
286 const client = getCurrentHub().getClient();
287 if (client) {
288 client.captureUserFeedback(feedback);
289 }
290}
291
292export { captureUserFeedback, close, defaultIntegrations, flush, forceLoad, init, lastEventId, onLoad, showReportDialog, wrap };
293//# sourceMappingURL=sdk.js.map