Skip to content

Commit efe3229

Browse files
committed
cleanup types, reduce bundle size a bit
1 parent 71fe56a commit efe3229

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed

packages/core/src/carrier.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Integration, Scope, VersionString } from '@sentry/types';
1+
import type { Client, Integration, MetricsAggregator, Scope, VersionString } from '@sentry/types';
22
import { GLOBAL_OBJ } from '@sentry/utils';
33
import { SDK_VERSION } from '@sentry/utils';
44
import type { AsyncContextStack } from './asyncContext/stackStrategy';
@@ -19,20 +19,19 @@ type VersionedCarrier = {
1919

2020
interface SentryCarrier {
2121
acs?: AsyncContextStrategy;
22-
/**
23-
* Extra Hub properties injected by various SDKs
24-
*/
25-
integrations?: Integration[];
26-
extensions?: {
27-
/** Extension methods for the hub, which are bound to the current Hub instance */
28-
// eslint-disable-next-line @typescript-eslint/ban-types
29-
[key: string]: Function;
30-
};
3122
stack?: AsyncContextStack;
3223

3324
globalScope?: Scope;
3425
defaultIsolationScope?: Scope;
3526
defaultCurrentScope?: Scope;
27+
globalMetricsAggregators?: WeakMap<Client, MetricsAggregator> | undefined;
28+
29+
// TODO(v9): Remove these properties - they are no longer used and were left over in v8
30+
integrations?: Integration[];
31+
extensions?: {
32+
// eslint-disable-next-line @typescript-eslint/ban-types
33+
[key: string]: Function;
34+
};
3635
}
3736

3837
/**
@@ -50,21 +49,12 @@ export function getMainCarrier(): Carrier {
5049

5150
/** Will either get the existing sentry carrier, or create a new one. */
5251
export function getSentryCarrier(carrier: Carrier): SentryCarrier {
53-
if (!carrier.__SENTRY__) {
54-
carrier.__SENTRY__ = {};
55-
}
52+
const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});
5653

5754
// For now: First SDK that sets the .version property wins
58-
if (!carrier.__SENTRY__.version) {
59-
carrier.__SENTRY__.version = SDK_VERSION;
60-
}
55+
__SENTRY__.version = __SENTRY__.version || SDK_VERSION;
6156

6257
// Intentionally populating and returning the version of "this" SDK instance
6358
// rather than what's set in .version so that "this" SDK always gets its carrier
64-
if (!carrier.__SENTRY__[SDK_VERSION]) {
65-
carrier.__SENTRY__[SDK_VERSION] = {
66-
extensions: {},
67-
};
68-
}
69-
return carrier.__SENTRY__[SDK_VERSION];
59+
return (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {});
7060
}

packages/core/test/lib/carrier.test.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1+
import { SDK_VERSION } from '@sentry/utils';
12
import { getSentryCarrier } from '../../src/carrier';
2-
import { SDK_VERSION } from '../../src/version';
33

44
describe('getSentryCarrier', () => {
55
describe('base case (one SDK)', () => {
66
it('populates the default sentry carrier object if it does not exist', () => {
77
const globalObject = {};
88
const sentryCarrier = getSentryCarrier(globalObject);
99

10-
expect(sentryCarrier).toEqual({
11-
extensions: {},
12-
});
10+
expect(sentryCarrier).toEqual({});
1311

1412
expect(globalObject).toEqual({
1513
__SENTRY__: {
1614
version: SDK_VERSION,
17-
[SDK_VERSION]: {
18-
extensions: {},
19-
},
15+
[SDK_VERSION]: {},
2016
},
2117
});
2218
});
@@ -71,26 +67,23 @@ describe('getSentryCarrier', () => {
7167
version: '8.0.0' as const,
7268
'8.0.0': {
7369
// and this object
74-
integrations: [() => {}],
70+
acs: {},
7571
},
7672
},
7773
};
7874

75+
// @ts-expect-error - this is just a test object, no need to pass a hub
7976
const sentryCarrier = getSentryCarrier(globalObject);
8077

81-
expect(sentryCarrier).toEqual({
82-
extensions: {},
83-
});
78+
expect(sentryCarrier).toEqual({});
8479

8580
expect(globalObject).toEqual({
8681
__SENTRY__: {
8782
version: '8.0.0',
8883
'8.0.0': {
89-
integrations: [expect.any(Function)],
90-
},
91-
[SDK_VERSION]: {
92-
extensions: {},
84+
acs: {},
9385
},
86+
[SDK_VERSION]: {},
9487
},
9588
});
9689
});

packages/utils/src/worldwide.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,23 @@ import type { Client, MetricsAggregator, Scope, VersionString } from '@sentry/ty
1717
import type { SdkSource } from './env';
1818
import { SDK_VERSION } from './version';
1919

20-
type BackwardsCompatibleSentryCarrier = {
21-
// v8 scope stack (replaces .hub)
22-
stack?: any;
20+
interface SentryCarrier {
2321
acs?: any;
22+
stack?: any;
23+
24+
globalScope?: Scope;
25+
defaultIsolationScope?: Scope;
26+
defaultCurrentScope?: Scope;
27+
globalMetricsAggregators: WeakMap<Client, MetricsAggregator> | undefined;
28+
29+
/** Overwrites TextEncoder used in `@sentry/utils`, need for `[email protected]` and older */
30+
encodePolyfill?: (input: string) => Uint8Array;
31+
/** Overwrites TextDecoder used in `@sentry/utils`, need for `[email protected]` and older */
32+
decodePolyfill?: (input: Uint8Array) => string;
33+
}
34+
35+
// TODO(v9): Clean up or remove this type
36+
type BackwardsCompatibleSentryCarrier = SentryCarrier & {
2437
// pre-v7 hub (replaced by .stack)
2538
hub: any;
2639
integrations?: any[];
@@ -30,14 +43,6 @@ type BackwardsCompatibleSentryCarrier = {
3043
// eslint-disable-next-line @typescript-eslint/ban-types
3144
[key: string]: Function;
3245
};
33-
globalScope: Scope | undefined;
34-
defaultCurrentScope: Scope | undefined;
35-
defaultIsolationScope: Scope | undefined;
36-
globalMetricsAggregators: WeakMap<Client, MetricsAggregator> | undefined;
37-
/** Overwrites TextEncoder used in `@sentry/utils`, need for `[email protected]` and older */
38-
encodePolyfill?: (input: string) => Uint8Array;
39-
/** Overwrites TextDecoder used in `@sentry/utils`, need for `[email protected]` and older */
40-
decodePolyfill?: (input: Uint8Array) => string;
4146
};
4247

4348
/** Internal global with common properties and Sentry extensions */
@@ -68,7 +73,7 @@ export type InternalGlobal = {
6873
*/
6974
_sentryDebugIds?: Record<string, string>;
7075
__SENTRY__: {
71-
[key: VersionString]: BackwardsCompatibleSentryCarrier;
76+
[key: VersionString]: SentryCarrier;
7277
version: VersionString;
7378
} & BackwardsCompatibleSentryCarrier;
7479
/**
@@ -83,10 +88,8 @@ export type InternalGlobal = {
8388
/** Get's the global object for the current JavaScript runtime */
8489
export const GLOBAL_OBJ = globalThis as unknown as InternalGlobal;
8590

86-
type SingletonKeys = Exclude<keyof InternalGlobal['__SENTRY__'], 'version' | VersionString>;
87-
8891
/**
89-
* Returns a global singleton contained in the global `__SENTRY__` object.
92+
* Returns a global singleton contained in the global `__SENTRY__[]` object.
9093
*
9194
* If the singleton doesn't already exist in `__SENTRY__`, it will be created using the given factory
9295
* function and added to the `__SENTRY__` object.
@@ -96,7 +99,7 @@ type SingletonKeys = Exclude<keyof InternalGlobal['__SENTRY__'], 'version' | Ver
9699
* @param obj (Optional) The global object on which to look for `__SENTRY__`, if not `GLOBAL_OBJ`'s return value
97100
* @returns the singleton
98101
*/
99-
export function getGlobalSingleton<T>(name: SingletonKeys, creator: () => T, obj?: unknown): T {
102+
export function getGlobalSingleton<T>(name: keyof SentryCarrier, creator: () => T, obj?: unknown): T {
100103
const gbl = (obj || GLOBAL_OBJ) as InternalGlobal;
101104
const __SENTRY__ = (gbl.__SENTRY__ = gbl.__SENTRY__ || {});
102105
const versionedCarrier = (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {});

0 commit comments

Comments
 (0)