-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathworldwide.ts
109 lines (99 loc) · 3.96 KB
/
worldwide.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* NOTE: In order to avoid circular dependencies, if you add a function to this module and it needs to print something,
* you must either a) use `console.log` rather than the logger, or b) put your function elsewhere.
*
* Note: This file was originally called `global.ts`, but was changed to unblock users which might be doing
* string replaces with bundlers like Vite for `global` (would break imports that rely on importing from utils/src/global).
*
* Why worldwide?
*
* Why not?
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Client, MetricsAggregator, Scope } from '@sentry/types';
import type { SdkSource } from './env';
import type { logger } from './logger';
import { SDK_VERSION } from './version';
interface SentryCarrier {
acs?: any;
stack?: any;
globalScope?: Scope;
defaultIsolationScope?: Scope;
defaultCurrentScope?: Scope;
globalMetricsAggregators?: WeakMap<Client, MetricsAggregator> | undefined;
logger?: typeof logger;
/** Overwrites TextEncoder used in `@sentry/utils`, need for `react-native@0.73` and older */
encodePolyfill?: (input: string) => Uint8Array;
/** Overwrites TextDecoder used in `@sentry/utils`, need for `react-native@0.73` and older */
decodePolyfill?: (input: Uint8Array) => string;
}
// TODO(v9): Clean up or remove this type
type BackwardsCompatibleSentryCarrier = SentryCarrier & {
// pre-v7 hub (replaced by .stack)
hub: any;
integrations?: any[];
logger: any;
extensions?: {
/** Extension methods for the hub, which are bound to the current Hub instance */
// eslint-disable-next-line @typescript-eslint/ban-types
[key: string]: Function;
};
};
/** Internal global with common properties and Sentry extensions */
export type InternalGlobal = {
navigator?: { userAgent?: string };
console: Console;
PerformanceObserver?: any;
Sentry?: any;
onerror?: {
(event: object | string, source?: string, lineno?: number, colno?: number, error?: Error): any;
__SENTRY_INSTRUMENTED__?: true;
__SENTRY_LOADER__?: true;
};
onunhandledrejection?: {
(event: unknown): boolean;
__SENTRY_INSTRUMENTED__?: true;
__SENTRY_LOADER__?: true;
};
SENTRY_ENVIRONMENT?: string;
SENTRY_DSN?: string;
SENTRY_RELEASE?: {
id?: string;
};
SENTRY_SDK_SOURCE?: SdkSource;
/**
* Debug IDs are indirectly injected by Sentry CLI or bundler plugins to directly reference a particular source map
* for resolving of a source file. The injected code will place an entry into the record for each loaded bundle/JS
* file.
*/
_sentryDebugIds?: Record<string, string>;
__SENTRY__: Record<Exclude<string, 'version'>, SentryCarrier> & {
version?: string;
} & BackwardsCompatibleSentryCarrier;
/**
* Raw module metadata that is injected by bundler plugins.
*
* Keys are `error.stack` strings, values are the metadata.
*/
_sentryModuleMetadata?: Record<string, any>;
_sentryEsmLoaderHookRegistered?: boolean;
};
/** Get's the global object for the current JavaScript runtime */
export const GLOBAL_OBJ = globalThis as unknown as InternalGlobal;
/**
* Returns a global singleton contained in the global `__SENTRY__[]` object.
*
* If the singleton doesn't already exist in `__SENTRY__`, it will be created using the given factory
* function and added to the `__SENTRY__` object.
*
* @param name name of the global singleton on __SENTRY__
* @param creator creator Factory function to create the singleton if it doesn't already exist on `__SENTRY__`
* @param obj (Optional) The global object on which to look for `__SENTRY__`, if not `GLOBAL_OBJ`'s return value
* @returns the singleton
*/
export function getGlobalSingleton<T>(name: keyof SentryCarrier, creator: () => T, obj?: unknown): T {
const gbl = (obj || GLOBAL_OBJ) as InternalGlobal;
const __SENTRY__ = (gbl.__SENTRY__ = gbl.__SENTRY__ || {});
const versionedCarrier = (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {});
return versionedCarrier[name] || (versionedCarrier[name] = creator());
}