Files
apps.apple.com/shared/logger/node_modules/@sentry/browser/esm/client.js
2025-11-04 05:03:50 +08:00

140 lines
4.2 KiB
JavaScript

import { BaseClient, SDK_VERSION } from '@sentry/core';
import { getSDKSource, logger, createClientReportEnvelope, dsnToString } from '@sentry/utils';
import { eventFromException, eventFromMessage } from './eventbuilder.js';
import { WINDOW } from './helpers.js';
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs.js';
import { createUserFeedbackEnvelope } from './userfeedback.js';
/**
* Configuration options for the Sentry Browser SDK.
* @see @sentry/types Options for more information.
*/
/**
* The Sentry Browser SDK Client.
*
* @see BrowserOptions for documentation on configuration options.
* @see SentryClient for usage documentation.
*/
class BrowserClient extends BaseClient {
/**
* Creates a new Browser SDK instance.
*
* @param options Configuration options for this SDK.
*/
constructor(options) {
const sdkSource = WINDOW.SENTRY_SDK_SOURCE || getSDKSource();
options._metadata = options._metadata || {};
options._metadata.sdk = options._metadata.sdk || {
name: 'sentry.javascript.browser',
packages: [
{
name: `${sdkSource}:@sentry/browser`,
version: SDK_VERSION,
},
],
version: SDK_VERSION,
};
super(options);
if (options.sendClientReports && WINDOW.document) {
WINDOW.document.addEventListener('visibilitychange', () => {
if (WINDOW.document.visibilityState === 'hidden') {
this._flushOutcomes();
}
});
}
}
/**
* @inheritDoc
*/
eventFromException(exception, hint) {
return eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace);
}
/**
* @inheritDoc
*/
eventFromMessage(
message,
// eslint-disable-next-line deprecation/deprecation
level = 'info',
hint,
) {
return eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace);
}
/**
* @inheritDoc
*/
sendEvent(event, hint) {
// We only want to add the sentry event breadcrumb when the user has the breadcrumb integration installed and
// activated its `sentry` option.
// We also do not want to use the `Breadcrumbs` class here directly, because we do not want it to be included in
// bundles, if it is not used by the SDK.
// 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
// way for now.
const breadcrumbIntegration = this.getIntegrationById(BREADCRUMB_INTEGRATION_ID) ;
// We check for definedness of `addSentryBreadcrumb` in case users provided their own integration with id
// "Breadcrumbs" that does not have this function.
if (breadcrumbIntegration && breadcrumbIntegration.addSentryBreadcrumb) {
breadcrumbIntegration.addSentryBreadcrumb(event);
}
super.sendEvent(event, hint);
}
/**
* Sends user feedback to Sentry.
*/
captureUserFeedback(feedback) {
if (!this._isEnabled()) {
(typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.warn('SDK not enabled, will not capture user feedback.');
return;
}
const envelope = createUserFeedbackEnvelope(feedback, {
metadata: this.getSdkMetadata(),
dsn: this.getDsn(),
tunnel: this.getOptions().tunnel,
});
void this._sendEnvelope(envelope);
}
/**
* @inheritDoc
*/
_prepareEvent(event, hint, scope) {
event.platform = event.platform || 'javascript';
return super._prepareEvent(event, hint, scope);
}
/**
* Sends client reports as an envelope.
*/
_flushOutcomes() {
const outcomes = this._clearOutcomes();
if (outcomes.length === 0) {
(typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('No outcomes to send');
return;
}
if (!this._dsn) {
(typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('No dsn provided, will not send outcomes');
return;
}
(typeof __SENTRY_DEBUG__ === 'undefined' || __SENTRY_DEBUG__) && logger.log('Sending outcomes:', outcomes);
const envelope = createClientReportEnvelope(outcomes, this._options.tunnel && dsnToString(this._dsn));
void this._sendEnvelope(envelope);
}
}
export { BrowserClient };
//# sourceMappingURL=client.js.map