diff --git a/packages/core/src/carrier.ts b/packages/core/src/carrier.ts index 5136121cb8ae..4ecd8838fec8 100644 --- a/packages/core/src/carrier.ts +++ b/packages/core/src/carrier.ts @@ -2,7 +2,6 @@ import type { AsyncContextStack } from './asyncContext/stackStrategy'; import type { AsyncContextStrategy } from './asyncContext/types'; import type { Scope } from './scope'; import type { Logger } from './utils-hoist/logger'; -import { SDK_VERSION } from './utils-hoist/version'; import { GLOBAL_OBJ } from './utils-hoist/worldwide'; /** @@ -17,6 +16,11 @@ type VersionedCarrier = { version?: string; } & Record, SentryCarrier>; +/** + * IMPORTANT - This must be updated if any breaking changes are made to the 'SentryCarrier' interface. + */ +export const CARRIER_VERSION = '9'; + export interface SentryCarrier { acs?: AsyncContextStrategy; stack?: AsyncContextStack; @@ -34,10 +38,6 @@ export interface SentryCarrier { /** * Returns the global shim registry. - * - * FIXME: This function is problematic, because despite always returning a valid Carrier, - * it has an optional `__SENTRY__` property, which then in turn requires us to always perform an unnecessary check - * at the call-site. We always access the carrier through this function, so we can guarantee that `__SENTRY__` is there. **/ export function getMainCarrier(): Carrier { // This ensures a Sentry carrier exists @@ -50,11 +50,11 @@ export function getSentryCarrier(carrier: Carrier): SentryCarrier { const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {}); // For now: First SDK that sets the .version property wins - __SENTRY__.version = __SENTRY__.version || SDK_VERSION; + __SENTRY__.version = __SENTRY__.version || CARRIER_VERSION; // Intentionally populating and returning the version of "this" SDK instance // rather than what's set in .version so that "this" SDK always gets its carrier - return (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {}); + return (__SENTRY__[CARRIER_VERSION] = __SENTRY__[CARRIER_VERSION] || {}); } /** @@ -74,7 +74,7 @@ export function getGlobalSingleton( obj = GLOBAL_OBJ, ): NonNullable { const __SENTRY__ = (obj.__SENTRY__ = obj.__SENTRY__ || {}); - const carrier = (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {}); + const carrier = (__SENTRY__[CARRIER_VERSION] = __SENTRY__[CARRIER_VERSION] || {}); // Note: We do not want to set `carrier.version` here, as this may be called before any `init` is called, e.g. for the default scopes return carrier[name] || (carrier[name] = creator()); } diff --git a/packages/core/test/lib/carrier.test.ts b/packages/core/test/lib/carrier.test.ts index 19ebc8117ca4..f3fbacef37ed 100644 --- a/packages/core/test/lib/carrier.test.ts +++ b/packages/core/test/lib/carrier.test.ts @@ -1,5 +1,5 @@ import { getSentryCarrier } from '../../src/carrier'; -import { SDK_VERSION } from '../../src/utils-hoist/version'; +import { CARRIER_VERSION } from '../../src/carrier'; describe('getSentryCarrier', () => { describe('base case (one SDK)', () => { @@ -11,8 +11,8 @@ describe('getSentryCarrier', () => { expect(globalObject).toEqual({ __SENTRY__: { - version: SDK_VERSION, - [SDK_VERSION]: {}, + version: CARRIER_VERSION, + [CARRIER_VERSION]: {}, }, }); }); @@ -20,8 +20,8 @@ describe('getSentryCarrier', () => { it('returns the existing sentry carrier object if it already exists', () => { const originalGlobalObject = { __SENTRY__: { - version: SDK_VERSION, - [SDK_VERSION]: { + version: CARRIER_VERSION, + [CARRIER_VERSION]: { acs: {}, }, }, @@ -42,13 +42,14 @@ describe('getSentryCarrier', () => { describe('multiple (older) SDKs', () => { it("returns the version of the sentry carrier object of the SDK's version rather than the one set in .version", () => { const sentryCarrier = getSentryCarrier({ - // @ts-expect-error - this is just a test object __SENTRY__: { version: '8.0.0', // another SDK set this '8.0.0': { + // @ts-expect-error - this is just a test object stack: {}, }, - [SDK_VERSION]: { + [CARRIER_VERSION]: { + // @ts-expect-error - this is just a test object acs: {}, }, hub: {}, @@ -82,7 +83,7 @@ describe('getSentryCarrier', () => { '8.0.0': { acs: {}, }, - [SDK_VERSION]: {}, + [CARRIER_VERSION]: {}, }, }); });