@sentry/lynx-react
Version:
Official Sentry SDK for Lynx React
112 lines (109 loc) • 4.15 kB
JavaScript
import { Client, makeDsn, logger, GLOBAL_OBJ, addAutoIpAddressToUser, addAutoIpAddressToSession } from '@sentry/core';
import { eventFromException, eventFromMessage } from '@sentry/browser';
import { SDK_VERSION } from './version.mjs';
import { getEnvelopeEndpointWithUrlEncodedAuth } from './utils/api.mjs';
const WINDOW = GLOBAL_OBJ;
/**
* The Sentry Lynx SDK Client.
*
* @see LynxOptions for documentation on configuration options.
* @see SentryClient for usage documentation.
*/
class LynxClient extends Client {
/**
* Creates a new Lynx SDK instance.
*
* @param options Configuration options for this SDK.
*/
constructor(options) {
const opts = {
// We default this to true, as it is the safer scenario
parentSpanIsAlwaysRootSpan: true,
...options,
// TODO: Temporary fix for URLSearchParams not defined in Lynx
dsn: undefined,
};
applySdkMetadata(opts, 'lynx', ['lynx']);
super(opts);
// TODO: Temporary fix for URLSearchParams not defined in Lynx
if (options.dsn) {
// @ts-ignore _dsn is read-only
this._dsn = makeDsn(options.dsn);
}
else {
logger.warn('No DSN provided, client will not send events.');
}
if (this._dsn) {
const url = getEnvelopeEndpointWithUrlEncodedAuth(this._dsn, undefined, opts._metadata ? opts._metadata.sdk : undefined);
// @ts-ignore _transport is read-only
this._transport = options.transport({
recordDroppedEvent: this.recordDroppedEvent.bind(this),
...opts.transportOptions,
url,
});
}
// TODO: End Of Temporary fix for URLSearchParams not defined in Lynx
// eslint-disable-next-line @typescript-eslint/no-this-alias
const client = this;
const { sendDefaultPii, } = client._options;
if (opts.sendClientReports && WINDOW.document) {
WINDOW.document.addEventListener('visibilitychange', () => {
if (WINDOW.document.visibilityState === 'hidden') {
this._flushOutcomes();
}
});
}
if (sendDefaultPii) {
client.on('postprocessEvent', addAutoIpAddressToUser);
client.on('beforeSendSession', addAutoIpAddressToSession);
}
}
/**
* @inheritDoc
*/
eventFromException(exception, hint) {
return eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace);
}
/**
* @inheritDoc
*/
eventFromMessage(message, level = 'info', hint) {
return 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);
}
}
/**
* A builder for the SDK metadata in the options for the SDK initialization.
*
* Note: This function is identical to `buildMetadata` in Remix and NextJS and SvelteKit.
* We don't extract it for bundle size reasons.
* @see https://github.com/getsentry/sentry-javascript/pull/7404
* @see https://github.com/getsentry/sentry-javascript/pull/4196
*
* If you make changes to this function consider updating the others as well.
*
* @param options SDK options object that gets mutated
* @param names list of package names
*/
function applySdkMetadata(options, name, names = [name], source = 'npm') {
const metadata = options._metadata || {};
if (!metadata.sdk) {
metadata.sdk = {
name: `sentry.javascript.${name}`,
packages: names.map(name => ({
name: `${source}:@sentry/${name}`,
version: SDK_VERSION,
})),
version: SDK_VERSION,
};
}
options._metadata = metadata;
}
export { LynxClient, WINDOW, applySdkMetadata };
//# sourceMappingURL=client.mjs.map