From bd6f08c10c9814e4f098c37810332c6881b4f424 Mon Sep 17 00:00:00 2001 From: Zac Bennett Date: Tue, 18 Jun 2024 17:01:28 -0700 Subject: [PATCH 1/4] fix: adding ampIntegrationsContext to getGlobalScope --- packages/analytics-client-common/src/global-scope.ts | 3 +++ packages/analytics-client-common/src/types/global.d.ts | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 packages/analytics-client-common/src/types/global.d.ts diff --git a/packages/analytics-client-common/src/global-scope.ts b/packages/analytics-client-common/src/global-scope.ts index 1e8106772..d783835e4 100644 --- a/packages/analytics-client-common/src/global-scope.ts +++ b/packages/analytics-client-common/src/global-scope.ts @@ -2,6 +2,9 @@ /* Only file allowed to access to globalThis, window, self */ export const getGlobalScope = (): typeof globalThis | undefined => { + if (typeof ampIntegrationContext !== 'undefined') { + return ampIntegrationContext; + } if (typeof globalThis !== 'undefined') { return globalThis; } diff --git a/packages/analytics-client-common/src/types/global.d.ts b/packages/analytics-client-common/src/types/global.d.ts new file mode 100644 index 000000000..18216d9f6 --- /dev/null +++ b/packages/analytics-client-common/src/types/global.d.ts @@ -0,0 +1,2 @@ +/* eslint-disable no-restricted-globals */ +declare let ampIntegrationContext: typeof globalThis; From 1af86fa8900d8755848d0dece9658cb1f136ed48 Mon Sep 17 00:00:00 2001 From: Zac Bennett Date: Thu, 20 Jun 2024 12:02:27 -0700 Subject: [PATCH 2/4] fix: fix reference error --- packages/analytics-client-common/src/global-scope.ts | 2 ++ packages/analytics-client-common/src/types/global.d.ts | 2 -- packages/analytics-client-common/src/types/global.ts | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) delete mode 100644 packages/analytics-client-common/src/types/global.d.ts create mode 100644 packages/analytics-client-common/src/types/global.ts diff --git a/packages/analytics-client-common/src/global-scope.ts b/packages/analytics-client-common/src/global-scope.ts index d783835e4..971b18572 100644 --- a/packages/analytics-client-common/src/global-scope.ts +++ b/packages/analytics-client-common/src/global-scope.ts @@ -1,6 +1,8 @@ /* eslint-disable no-restricted-globals */ /* Only file allowed to access to globalThis, window, self */ +import { ampIntegrationContext } from './types/global'; + export const getGlobalScope = (): typeof globalThis | undefined => { if (typeof ampIntegrationContext !== 'undefined') { return ampIntegrationContext; diff --git a/packages/analytics-client-common/src/types/global.d.ts b/packages/analytics-client-common/src/types/global.d.ts deleted file mode 100644 index 18216d9f6..000000000 --- a/packages/analytics-client-common/src/types/global.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -/* eslint-disable no-restricted-globals */ -declare let ampIntegrationContext: typeof globalThis; diff --git a/packages/analytics-client-common/src/types/global.ts b/packages/analytics-client-common/src/types/global.ts new file mode 100644 index 000000000..c32301f99 --- /dev/null +++ b/packages/analytics-client-common/src/types/global.ts @@ -0,0 +1,3 @@ +/* eslint-disable no-restricted-globals */ +// eslint-disable-next-line no-var +export var ampIntegrationContext: typeof globalThis; From 09b733bcb0dfcb4da2969c5332fdb9b24b060633 Mon Sep 17 00:00:00 2001 From: Zac Bennett Date: Fri, 21 Jun 2024 14:37:34 -0700 Subject: [PATCH 3/4] fix: fixed minifing the global variable name --- packages/analytics-client-common/src/global-scope.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/analytics-client-common/src/global-scope.ts b/packages/analytics-client-common/src/global-scope.ts index 971b18572..3410bea3a 100644 --- a/packages/analytics-client-common/src/global-scope.ts +++ b/packages/analytics-client-common/src/global-scope.ts @@ -1,11 +1,12 @@ /* eslint-disable no-restricted-globals */ /* Only file allowed to access to globalThis, window, self */ -import { ampIntegrationContext } from './types/global'; - export const getGlobalScope = (): typeof globalThis | undefined => { - if (typeof ampIntegrationContext !== 'undefined') { - return ampIntegrationContext; + // This should only be used for integrations with Amplitude that are not running in a browser environment + // We need to specify the name of the global variable as a string to prevent it from being minified + const ampIntegrationContextName = 'ampIntegrationContext' as keyof typeof globalThis; + if (typeof globalThis[ampIntegrationContextName] !== 'undefined') { + return globalThis[ampIntegrationContextName] as typeof globalThis; } if (typeof globalThis !== 'undefined') { return globalThis; From 4176f5f0571dbd535761f92c997fb825253807a5 Mon Sep 17 00:00:00 2001 From: Zac Bennett Date: Mon, 24 Jun 2024 09:48:35 -0700 Subject: [PATCH 4/4] fix: checking for undefined and added tests --- .../src/global-scope.ts | 2 +- .../test/global-scope.test.ts | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 packages/analytics-client-common/test/global-scope.test.ts diff --git a/packages/analytics-client-common/src/global-scope.ts b/packages/analytics-client-common/src/global-scope.ts index 3410bea3a..183d0caaf 100644 --- a/packages/analytics-client-common/src/global-scope.ts +++ b/packages/analytics-client-common/src/global-scope.ts @@ -5,7 +5,7 @@ export const getGlobalScope = (): typeof globalThis | undefined => { // This should only be used for integrations with Amplitude that are not running in a browser environment // We need to specify the name of the global variable as a string to prevent it from being minified const ampIntegrationContextName = 'ampIntegrationContext' as keyof typeof globalThis; - if (typeof globalThis[ampIntegrationContextName] !== 'undefined') { + if (typeof globalThis !== 'undefined' && typeof globalThis[ampIntegrationContextName] !== 'undefined') { return globalThis[ampIntegrationContextName] as typeof globalThis; } if (typeof globalThis !== 'undefined') { diff --git a/packages/analytics-client-common/test/global-scope.test.ts b/packages/analytics-client-common/test/global-scope.test.ts new file mode 100644 index 000000000..9b96c5b56 --- /dev/null +++ b/packages/analytics-client-common/test/global-scope.test.ts @@ -0,0 +1,45 @@ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +import { getGlobalScope } from '../src/global-scope'; + +describe('getGlobalScope', () => { + let originalGlobalThis: any; + + beforeEach(() => { + originalGlobalThis = globalThis; + }); + + afterEach(() => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + globalThis = originalGlobalThis; + delete (globalThis as any).ampIntegrationContext; + }); + + test('returns ampIntegrationContext if it exists', () => { + (globalThis as any).ampIntegrationContext = { someKey: 'someValue' }; + expect(getGlobalScope()).toBe((globalThis as any).ampIntegrationContext); + }); + + test('returns globalThis if ampIntegrationContext does not exist', () => { + const scope = getGlobalScope(); + // Need to use Object.is because expect(scope).toBe(globalThis) will throw an error + expect(Object.is(scope, globalThis)).toBeTruthy(); + }); + + test('should return window if globalThis is undefined and window is defined', () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + globalThis = undefined; + + const scope = getGlobalScope(); + + // Note: We NEED to reassign globalThis to its original state because the jest expect function requires it + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + globalThis = originalGlobalThis; + // Need to use Object.is because expect(scope).toBe(globalThis) will throw an error + expect(Object.is(scope, window)).toBeTruthy(); + }); +});