Skip to content

Commit c9c5646

Browse files
committed
moare fixes
1 parent 74a6858 commit c9c5646

File tree

5 files changed

+33
-44
lines changed

5 files changed

+33
-44
lines changed

packages/core/src/carrier.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { AsyncContextStack } from './asyncContext/stackStrategy';
22
import type { AsyncContextStrategy } from './asyncContext/types';
3-
import type { Client, Integration, MetricsAggregator, Scope } from './types-hoist';
3+
import type { Client, MetricsAggregator, Scope } from './types-hoist';
4+
import type { Logger } from './utils-hoist/logger';
45
import { SDK_VERSION } from './utils-hoist/version';
56
import { GLOBAL_OBJ } from './utils-hoist/worldwide';
67

@@ -12,25 +13,26 @@ export interface Carrier {
1213
__SENTRY__?: VersionedCarrier;
1314
}
1415

15-
type VersionedCarrier = {
16+
export type VersionedCarrier = {
1617
version?: string;
1718
} & Record<Exclude<string, 'version'>, SentryCarrier>;
1819

19-
interface SentryCarrier {
20+
export interface SentryCarrier {
2021
acs?: AsyncContextStrategy;
2122
stack?: AsyncContextStack;
2223

2324
globalScope?: Scope;
2425
defaultIsolationScope?: Scope;
2526
defaultCurrentScope?: Scope;
2627
globalMetricsAggregators?: WeakMap<Client, MetricsAggregator> | undefined;
27-
28-
// TODO(v9): Remove these properties - they are no longer used and were left over in v8
29-
integrations?: Integration[];
30-
extensions?: {
31-
// eslint-disable-next-line @typescript-eslint/ban-types
32-
[key: string]: Function;
33-
};
28+
logger?: Logger;
29+
30+
// TODO(v9): Do we still need those?
31+
// --> Check with RN
32+
/** Overwrites TextEncoder used in `@sentry/core`, need for `react-native@0.73` and older */
33+
encodePolyfill?: (input: string) => Uint8Array;
34+
/** Overwrites TextDecoder used in `@sentry/core`, need for `react-native@0.73` and older */
35+
decodePolyfill?: (input: Uint8Array) => string;
3436
}
3537

3638
/**

packages/core/src/metrics/exports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ function getMetricsAggregatorForClient(
2323
client: Client,
2424
Aggregator: MetricsAggregatorConstructor,
2525
): MetricsAggregatorInterface {
26-
const globalMetricsAggregators = getGlobalSingleton<WeakMap<Client, MetricsAggregatorInterface>>(
26+
const globalMetricsAggregators = getGlobalSingleton(
2727
'globalMetricsAggregators',
28-
() => new WeakMap(),
28+
() => new WeakMap<Client, MetricsAggregatorInterface>(),
2929
);
3030

3131
const aggregator = globalMetricsAggregators.get(client);

packages/core/src/utils-hoist/envelope.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getSentryCarrier } from '../carrier';
12
import type {
23
Attachment,
34
AttachmentItem,
@@ -74,18 +75,20 @@ export function envelopeContainsItemType(envelope: Envelope, types: EnvelopeItem
7475
* Encode a string to UTF8 array.
7576
*/
7677
function encodeUTF8(input: string): Uint8Array {
77-
return GLOBAL_OBJ.__SENTRY__ && GLOBAL_OBJ.__SENTRY__.encodePolyfill
78-
? GLOBAL_OBJ.__SENTRY__.encodePolyfill(input)
79-
: new TextEncoder().encode(input);
78+
// TODO(v9): Do we actually still need this?
79+
// --> Evaluate if RN still needs this
80+
const carrier = getSentryCarrier(GLOBAL_OBJ);
81+
return carrier.encodePolyfill ? carrier.encodePolyfill(input) : new TextEncoder().encode(input);
8082
}
8183

8284
/**
8385
* Decode a UTF8 array to string.
8486
*/
8587
function decodeUTF8(input: Uint8Array): string {
86-
return GLOBAL_OBJ.__SENTRY__ && GLOBAL_OBJ.__SENTRY__.decodePolyfill
87-
? GLOBAL_OBJ.__SENTRY__.decodePolyfill(input)
88-
: new TextDecoder().decode(input);
88+
// TODO(v9): Do we actually still need this?
89+
// --> Evaluate if RN still needs this
90+
const carrier = getSentryCarrier(GLOBAL_OBJ);
91+
return carrier.decodePolyfill ? carrier.decodePolyfill(input) : new TextDecoder().decode(input);
8992
}
9093

9194
/**

packages/core/src/utils-hoist/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const originalConsoleMethods: {
2424
[key in ConsoleLevel]?: (...args: unknown[]) => void;
2525
} = {};
2626

27-
/** JSDoc */
27+
/** A Sentry Logger instance. */
2828
export interface Logger extends LoggerConsoleMethods {
2929
disable(): void;
3030
enable(): void;

packages/core/src/utils-hoist/worldwide.ts

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,10 @@
1212

1313
/* eslint-disable @typescript-eslint/no-explicit-any */
1414

15-
import type { Client, MetricsAggregator, Scope } from '../types-hoist';
16-
15+
import type { Carrier, SentryCarrier, VersionedCarrier } from '../carrier';
1716
import type { SdkSource } from './env';
18-
import type { logger } from './logger';
1917
import { SDK_VERSION } from './version';
2018

21-
interface SentryCarrier {
22-
acs?: any;
23-
stack?: any;
24-
25-
globalScope?: Scope;
26-
defaultIsolationScope?: Scope;
27-
defaultCurrentScope?: Scope;
28-
globalMetricsAggregators?: WeakMap<Client, MetricsAggregator> | undefined;
29-
logger?: typeof logger;
30-
31-
/** Overwrites TextEncoder used in `@sentry/core`, need for `react-native@0.73` and older */
32-
encodePolyfill?: (input: string) => Uint8Array;
33-
/** Overwrites TextDecoder used in `@sentry/core`, need for `react-native@0.73` and older */
34-
decodePolyfill?: (input: Uint8Array) => string;
35-
}
36-
3719
/** Internal global with common properties and Sentry extensions */
3820
export type InternalGlobal = {
3921
navigator?: { userAgent?: string };
@@ -60,17 +42,14 @@ export type InternalGlobal = {
6042
* file.
6143
*/
6244
_sentryDebugIds?: Record<string, string>;
63-
__SENTRY__: Record<Exclude<string, 'version'>, SentryCarrier> & {
64-
version?: string;
65-
};
6645
/**
6746
* Raw module metadata that is injected by bundler plugins.
6847
*
6948
* Keys are `error.stack` strings, values are the metadata.
7049
*/
7150
_sentryModuleMetadata?: Record<string, any>;
7251
_sentryEsmLoaderHookRegistered?: boolean;
73-
};
52+
} & Carrier;
7453

7554
/** Get's the global object for the current JavaScript runtime */
7655
export const GLOBAL_OBJ = globalThis as unknown as InternalGlobal;
@@ -86,15 +65,20 @@ export const GLOBAL_OBJ = globalThis as unknown as InternalGlobal;
8665
* @param obj (Optional) The global object on which to look for `__SENTRY__`, if not `GLOBAL_OBJ`'s return value
8766
* @returns the singleton
8867
*/
89-
export function getGlobalSingleton<T>(name: keyof SentryCarrier, creator: () => T, obj = GLOBAL_OBJ): T {
68+
export function getGlobalSingleton<Prop extends keyof SentryCarrier>(
69+
name: Prop,
70+
creator: () => NonNullable<SentryCarrier[Prop]>,
71+
obj = GLOBAL_OBJ,
72+
): NonNullable<SentryCarrier[Prop]> {
9073
const __SENTRY__ = getSentryCarrierObj(obj);
9174
const versionedCarrier = (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {});
75+
9276
return versionedCarrier[name] || (versionedCarrier[name] = creator());
9377
}
9478

9579
function getSentryCarrierObj(
9680
obj: Omit<InternalGlobal, '__SENTRY__'> & Partial<Pick<InternalGlobal, '__SENTRY__'>>,
97-
): InternalGlobal['__SENTRY__'] {
81+
): VersionedCarrier {
9882
// Set the Sentry carrier, if it does not exist yet
9983
return obj.__SENTRY__ || (obj.__SENTRY__ = {});
10084
}

0 commit comments

Comments
 (0)