UNPKG

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