UNPKG

@sentry/browser

Version:
107 lines (103 loc) • 4.04 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const browserUtils = require('@sentry/browser-utils'); const core = require('@sentry/core'); const eventbuilder = require('./eventbuilder.js'); const helpers = require('./helpers.js'); class BrowserClient extends core.Client { /** * Creates a new Browser SDK instance. * * @param options Configuration options for this SDK. */ constructor(options) { const opts = applyDefaultOptions(options); const sdkSource = helpers.WINDOW.SENTRY_SDK_SOURCE || core.getSDKSource(); core.applySdkMetadata(opts, "browser", ["browser"], sdkSource); super(opts); if (opts.routeProvider) { browserUtils.setRouteProvider(opts.routeProvider, this); } this._unhandledSessionStatus = "unhandled"; const { userInfo } = this.getDataCollectionOptions(); if (opts._metadata?.sdk) { opts._metadata.sdk.settings = { // Only allow IP inferral by Relay if the user opted in via dataCollection infer_ip: userInfo ? "auto" : "never", // purposefully allowing already passed settings to override the default ...opts._metadata.sdk.settings }; } const { sendClientReports } = this._options; const checkDrift = () => { core.timestampInSeconds(); }; const listeners = [ // Flush buffered data when the page becomes hidden (e.g. tab switch, navigation, or the page // being discarded). `flush()` emits the `flush` hook, which drains the span (streaming), log and // metric buffers and hands the resulting envelopes to the transport (which uses `keepalive`). // Client report outcomes don't listen to the `flush` hook, so we flush them separately. [ helpers.WINDOW.document, "visibilitychange", () => { checkDrift(); if (helpers.WINDOW.document.visibilityState === "hidden") { if (sendClientReports) { this._flushOutcomes(); } queueMicrotask(() => { void this.flush(); }); } } ], // `freeze` and `resume` are only fired by Chromium browsers. Other browsers never fire them, so these listeners // are no-ops there. [helpers.WINDOW.document, "freeze", checkDrift], [helpers.WINDOW.document, "resume", checkDrift], // Pages restored from the back/forward cache were paused while they were cached. [helpers.WINDOW, "pagehide", checkDrift], [helpers.WINDOW, "pageshow", checkDrift] ]; listeners.forEach(([target, name, listener]) => target?.addEventListener?.(name, listener)); this.on( "close", () => listeners.forEach(([target, name, listener]) => target?.removeEventListener?.(name, listener)) ); if (userInfo) { this.on("beforeSendSession", core.addAutoIpAddressToSession); } } /** * @inheritDoc */ eventFromException(exception, hint) { return eventbuilder.eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace); } /** * @inheritDoc */ eventFromMessage(message, level = "info", hint) { return eventbuilder.eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace); } /** * @inheritDoc */ _prepareEvent(event, hint, currentScope, isolationScope) { event.platform = event.platform || "javascript"; return super._prepareEvent(event, hint, currentScope, isolationScope); } } function applyDefaultOptions(optionsArg) { return { release: typeof __SENTRY_RELEASE__ === "string" ? __SENTRY_RELEASE__ : helpers.WINDOW.SENTRY_RELEASE?.id, // This supports the variable that sentry-webpack-plugin injects sendClientReports: true, // We default this to true, as it is the safer scenario parentSpanIsAlwaysRootSpan: true, ...optionsArg }; } exports.BrowserClient = BrowserClient; exports.applyDefaultOptions = applyDefaultOptions; //# sourceMappingURL=client.js.map