Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): Modern implementation of getGlobalObject #5809

Merged
merged 3 commits into from
Sep 26, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 42 additions & 13 deletions packages/utils/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

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

import { isNodeEnv } from './node';

/** Internal */
interface SentryGlobal {
Sentry?: {
Expand All @@ -26,23 +24,54 @@ interface SentryGlobal {
};
}

const fallbackGlobalObject = {};
// The code below for 'check' and 'GLOBAL' was copied from core-js before modification
// https://github.com/zloirock/core-js/blob/1b944df55282cdc99c90db5f49eb0b6eda2cc0a3/packages/core-js/internals/global.js
// core-js has the following licence:
//
// Copyright (c) 2014-2022 Denis Pushkarev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/** Returns 'it' if it's the global object */
timfish marked this conversation as resolved.
Show resolved Hide resolved
function check(it: any): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return it && it.Math == Math && it;
timfish marked this conversation as resolved.
Show resolved Hide resolved
}

const GLOBAL =
check(typeof globalThis == 'object' && globalThis) ||
// eslint-disable-next-line no-restricted-globals
check(typeof window == 'object' && window) ||
check(typeof self == 'object' && self) ||
check(typeof global == 'object' && global) ||
(function (this: any) {
return this;
})() ||
{};
timfish marked this conversation as resolved.
Show resolved Hide resolved

/**
* Safely get global scope object
*
* @returns Global scope object
*/
export function getGlobalObject<T>(): T & SentryGlobal {
return (
isNodeEnv()
? global
: typeof window !== 'undefined' // eslint-disable-line no-restricted-globals
? window // eslint-disable-line no-restricted-globals
: typeof self !== 'undefined'
? self
: fallbackGlobalObject
) as T & SentryGlobal;
return GLOBAL as T & SentryGlobal;
}

/**
Expand All @@ -57,7 +86,7 @@ export function getGlobalObject<T>(): T & SentryGlobal {
* @returns the singleton
*/
export function getGlobalSingleton<T>(name: keyof SentryGlobal['__SENTRY__'], creator: () => T, obj?: unknown): T {
const global = (obj || getGlobalObject()) as SentryGlobal;
const global = (obj || GLOBAL) as SentryGlobal;
const __SENTRY__ = (global.__SENTRY__ = global.__SENTRY__ || {});
const singleton = __SENTRY__[name] || (__SENTRY__[name] = creator());
return singleton;
Expand Down