Skip to content

Commit a5bfa80

Browse files
authored
feat(utils): Modern implementation of getGlobalObject (#5809)
- Replaces `getGlobalObject` global object detection with a modern implementation taken from `core-js` which is then modified/simplified a little - Prioritises `globalThis` which was previously removed due to requiring a TypeScript version which was required in a no longer supported version of Angular (#2978) - This is a requirement for future support of other js runtimes (#5611) - No longer uses `isNodeEnv` 🎉 - Caches the global so it doesn't need to be evaluated for every invocation of `getGlobalObject`
1 parent 630ed9c commit a5bfa80

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

packages/utils/src/global.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import { Integration } from '@sentry/types';
99

10-
import { isNodeEnv } from './node';
11-
1210
/** Internal */
1311
interface SentryGlobal {
1412
Sentry?: {
@@ -26,23 +24,53 @@ interface SentryGlobal {
2624
};
2725
}
2826

29-
const fallbackGlobalObject = {};
27+
// The code below for 'isGlobalObj' and 'GLOBAL' was copied from core-js before modification
28+
// https://github.com/zloirock/core-js/blob/1b944df55282cdc99c90db5f49eb0b6eda2cc0a3/packages/core-js/internals/global.js
29+
// core-js has the following licence:
30+
//
31+
// Copyright (c) 2014-2022 Denis Pushkarev
32+
//
33+
// Permission is hereby granted, free of charge, to any person obtaining a copy
34+
// of this software and associated documentation files (the "Software"), to deal
35+
// in the Software without restriction, including without limitation the rights
36+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37+
// copies of the Software, and to permit persons to whom the Software is
38+
// furnished to do so, subject to the following conditions:
39+
//
40+
// The above copyright notice and this permission notice shall be included in
41+
// all copies or substantial portions of the Software.
42+
//
43+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
49+
// THE SOFTWARE.
50+
51+
/** Returns 'obj' if it's the global object, otherwise returns undefined */
52+
function isGlobalObj(obj: { Math?: Math }): any | undefined {
53+
return obj && obj.Math == Math ? obj : undefined;
54+
}
55+
56+
const GLOBAL =
57+
(typeof globalThis == 'object' && isGlobalObj(globalThis)) ||
58+
// eslint-disable-next-line no-restricted-globals
59+
(typeof window == 'object' && isGlobalObj(window)) ||
60+
(typeof self == 'object' && isGlobalObj(self)) ||
61+
(typeof global == 'object' && isGlobalObj(global)) ||
62+
(function (this: any) {
63+
return this;
64+
})() ||
65+
{};
3066

3167
/**
3268
* Safely get global scope object
3369
*
3470
* @returns Global scope object
3571
*/
3672
export function getGlobalObject<T>(): T & SentryGlobal {
37-
return (
38-
isNodeEnv()
39-
? global
40-
: typeof window !== 'undefined' // eslint-disable-line no-restricted-globals
41-
? window // eslint-disable-line no-restricted-globals
42-
: typeof self !== 'undefined'
43-
? self
44-
: fallbackGlobalObject
45-
) as T & SentryGlobal;
73+
return GLOBAL as T & SentryGlobal;
4674
}
4775

4876
/**
@@ -57,7 +85,7 @@ export function getGlobalObject<T>(): T & SentryGlobal {
5785
* @returns the singleton
5886
*/
5987
export function getGlobalSingleton<T>(name: keyof SentryGlobal['__SENTRY__'], creator: () => T, obj?: unknown): T {
60-
const global = (obj || getGlobalObject()) as SentryGlobal;
88+
const global = (obj || GLOBAL) as SentryGlobal;
6189
const __SENTRY__ = (global.__SENTRY__ = global.__SENTRY__ || {});
6290
const singleton = __SENTRY__[name] || (__SENTRY__[name] = creator());
6391
return singleton;

0 commit comments

Comments
 (0)