UNPKG

7.21 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
3import { addInstrumentationHandler, getGlobalObject, logger, resolvedSyncPromise } from '@sentry/utils';
4import { BrowserClient } from './client';
5import { IS_DEBUG_BUILD } from './flags';
6import { wrap as internalWrap } from './helpers';
7import { Breadcrumbs, Dedupe, GlobalHandlers, LinkedErrors, TryCatch, UserAgent } from './integrations';
8export var defaultIntegrations = [
9 new CoreIntegrations.InboundFilters(),
10 new CoreIntegrations.FunctionToString(),
11 new TryCatch(),
12 new Breadcrumbs(),
13 new GlobalHandlers(),
14 new LinkedErrors(),
15 new Dedupe(),
16 new UserAgent(),
17];
18/**
19 * The Sentry Browser SDK Client.
20 *
21 * To use this SDK, call the {@link init} function as early as possible when
22 * loading the web page. To set context information or send manual events, use
23 * the provided methods.
24 *
25 * @example
26 *
27 * ```
28 *
29 * import { init } from '@sentry/browser';
30 *
31 * init({
32 * dsn: '__DSN__',
33 * // ...
34 * });
35 * ```
36 *
37 * @example
38 * ```
39 *
40 * import { configureScope } from '@sentry/browser';
41 * configureScope((scope: Scope) => {
42 * scope.setExtra({ battery: 0.7 });
43 * scope.setTag({ user_mode: 'admin' });
44 * scope.setUser({ id: '4711' });
45 * });
46 * ```
47 *
48 * @example
49 * ```
50 *
51 * import { addBreadcrumb } from '@sentry/browser';
52 * addBreadcrumb({
53 * message: 'My Breadcrumb',
54 * // ...
55 * });
56 * ```
57 *
58 * @example
59 *
60 * ```
61 *
62 * import * as Sentry from '@sentry/browser';
63 * Sentry.captureMessage('Hello, world!');
64 * Sentry.captureException(new Error('Good bye'));
65 * Sentry.captureEvent({
66 * message: 'Manual',
67 * stacktrace: [
68 * // ...
69 * ],
70 * });
71 * ```
72 *
73 * @see {@link BrowserOptions} for documentation on configuration options.
74 */
75export function init(options) {
76 if (options === void 0) { options = {}; }
77 if (options.defaultIntegrations === undefined) {
78 options.defaultIntegrations = defaultIntegrations;
79 }
80 if (options.release === undefined) {
81 var window_1 = getGlobalObject();
82 // This supports the variable that sentry-webpack-plugin injects
83 if (window_1.SENTRY_RELEASE && window_1.SENTRY_RELEASE.id) {
84 options.release = window_1.SENTRY_RELEASE.id;
85 }
86 }
87 if (options.autoSessionTracking === undefined) {
88 options.autoSessionTracking = true;
89 }
90 if (options.sendClientReports === undefined) {
91 options.sendClientReports = true;
92 }
93 initAndBind(BrowserClient, options);
94 if (options.autoSessionTracking) {
95 startSessionTracking();
96 }
97}
98/**
99 * Present the user with a report dialog.
100 *
101 * @param options Everything is optional, we try to fetch all info need from the global scope.
102 */
103export function showReportDialog(options) {
104 if (options === void 0) { options = {}; }
105 var hub = getCurrentHub();
106 var scope = hub.getScope();
107 if (scope) {
108 options.user = __assign(__assign({}, scope.getUser()), options.user);
109 }
110 if (!options.eventId) {
111 options.eventId = hub.lastEventId();
112 }
113 var client = hub.getClient();
114 if (client) {
115 client.showReportDialog(options);
116 }
117}
118/**
119 * This is the getter for lastEventId.
120 *
121 * @returns The last event id of a captured event.
122 */
123export function lastEventId() {
124 return getCurrentHub().lastEventId();
125}
126/**
127 * This function is here to be API compatible with the loader.
128 * @hidden
129 */
130export function forceLoad() {
131 // Noop
132}
133/**
134 * This function is here to be API compatible with the loader.
135 * @hidden
136 */
137export function onLoad(callback) {
138 callback();
139}
140/**
141 * Call `flush()` on the current client, if there is one. See {@link Client.flush}.
142 *
143 * @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause
144 * the client to wait until all events are sent before resolving the promise.
145 * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
146 * doesn't (or if there's no client defined).
147 */
148export function flush(timeout) {
149 var client = getCurrentHub().getClient();
150 if (client) {
151 return client.flush(timeout);
152 }
153 IS_DEBUG_BUILD && logger.warn('Cannot flush events. No client defined.');
154 return resolvedSyncPromise(false);
155}
156/**
157 * Call `close()` on the current client, if there is one. See {@link Client.close}.
158 *
159 * @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this
160 * parameter will cause the client to wait until all events are sent before disabling itself.
161 * @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
162 * doesn't (or if there's no client defined).
163 */
164export function close(timeout) {
165 var client = getCurrentHub().getClient();
166 if (client) {
167 return client.close(timeout);
168 }
169 IS_DEBUG_BUILD && logger.warn('Cannot flush events and disable SDK. No client defined.');
170 return resolvedSyncPromise(false);
171}
172/**
173 * Wrap code within a try/catch block so the SDK is able to capture errors.
174 *
175 * @param fn A function to wrap.
176 *
177 * @returns The result of wrapped function call.
178 */
179// eslint-disable-next-line @typescript-eslint/no-explicit-any
180export function wrap(fn) {
181 return internalWrap(fn)();
182}
183function startSessionOnHub(hub) {
184 hub.startSession({ ignoreDuration: true });
185 hub.captureSession();
186}
187/**
188 * Enable automatic Session Tracking for the initial page load.
189 */
190function startSessionTracking() {
191 var window = getGlobalObject();
192 var document = window.document;
193 if (typeof document === 'undefined') {
194 IS_DEBUG_BUILD && logger.warn('Session tracking in non-browser environment with @sentry/browser is not supported.');
195 return;
196 }
197 var hub = getCurrentHub();
198 // The only way for this to be false is for there to be a version mismatch between @sentry/browser (>= 6.0.0) and
199 // @sentry/hub (< 5.27.0). In the simple case, there won't ever be such a mismatch, because the two packages are
200 // pinned at the same version in package.json, but there are edge cases where it's possible. See
201 // https://github.com/getsentry/sentry-javascript/issues/3207 and
202 // https://github.com/getsentry/sentry-javascript/issues/3234 and
203 // https://github.com/getsentry/sentry-javascript/issues/3278.
204 if (!hub.captureSession) {
205 return;
206 }
207 // The session duration for browser sessions does not track a meaningful
208 // concept that can be used as a metric.
209 // Automatically captured sessions are akin to page views, and thus we
210 // discard their duration.
211 startSessionOnHub(hub);
212 // We want to create a session for every navigation as well
213 addInstrumentationHandler('history', function (_a) {
214 var from = _a.from, to = _a.to;
215 // Don't create an additional session for the initial route or if the location did not change
216 if (!(from === undefined || from === to)) {
217 startSessionOnHub(getCurrentHub());
218 }
219 });
220}
221//# sourceMappingURL=sdk.js.map
\No newline at end of file