From 8bd0e2ac6e0a339775d321bd0892adfcca308f88 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Mon, 30 Oct 2023 09:30:29 +0000 Subject: [PATCH] fix --- .../exception-observer.test.ts | 2 +- src/__tests__/page-view.ts | 4 ++-- src/__tests__/posthog-core.js | 2 +- src/__tests__/rate-limiter.test.ts | 2 +- src/extensions/exception-autocapture/index.ts | 2 +- src/extensions/toolbar.ts | 3 ++- src/gdpr-utils.ts | 3 ++- src/page-view.ts | 2 +- src/posthog-core.ts | 5 ++--- src/sessionid.ts | 2 +- src/utils/event-utils.ts | 3 ++- src/utils/globals.ts | 13 +++++++++++++ src/utils/index.ts | 14 +------------- src/utils/logger.ts | 2 +- src/uuidv7.ts | 2 +- 15 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 src/utils/globals.ts diff --git a/src/__tests__/extensions/exception-autocapture/exception-observer.test.ts b/src/__tests__/extensions/exception-autocapture/exception-observer.test.ts index a85ac3118..e2bd50be9 100644 --- a/src/__tests__/extensions/exception-autocapture/exception-observer.test.ts +++ b/src/__tests__/extensions/exception-autocapture/exception-observer.test.ts @@ -2,7 +2,7 @@ import { PostHog } from '../../../posthog-core' import { DecideResponse, PostHogConfig } from '../../../types' import { ExceptionObserver } from '../../../extensions/exception-autocapture' -import { window } from '../../../utils' +import { window } from '../../../utils/globals' describe('Exception Observer', () => { let exceptionObserver: ExceptionObserver diff --git a/src/__tests__/page-view.ts b/src/__tests__/page-view.ts index fbd7bf3b7..2dbce5c11 100644 --- a/src/__tests__/page-view.ts +++ b/src/__tests__/page-view.ts @@ -1,8 +1,8 @@ import { PageViewManager } from '../page-view' const mockWindowGetter = jest.fn() -jest.mock('../utils', () => ({ - ...jest.requireActual('../utils'), +jest.mock('../utils/globals', () => ({ + ...jest.requireActual('../utils/globals'), get window() { return mockWindowGetter() }, diff --git a/src/__tests__/posthog-core.js b/src/__tests__/posthog-core.js index 3d71d2b4d..73ef340ee 100644 --- a/src/__tests__/posthog-core.js +++ b/src/__tests__/posthog-core.js @@ -2,10 +2,10 @@ import { init_as_module, PostHog } from '../posthog-core' import { PostHogPersistence } from '../posthog-persistence' import { Decide } from '../decide' import { autocapture } from '../autocapture' -import { window, document } from '../utils' import { truth } from './helpers/truth' import { _info } from '../utils/event-utils' +import { document, window } from '../utils/globals' jest.mock('../gdpr-utils', () => ({ ...jest.requireActual('../gdpr-utils'), diff --git a/src/__tests__/rate-limiter.test.ts b/src/__tests__/rate-limiter.test.ts index 61c5b9137..9c4b27e10 100644 --- a/src/__tests__/rate-limiter.test.ts +++ b/src/__tests__/rate-limiter.test.ts @@ -1,4 +1,4 @@ -import { window } from '../../src/utils' +import { window } from '../../src/utils/globals' import { RateLimiter } from '../rate-limiter' import { logger } from '../utils/logger' diff --git a/src/extensions/exception-autocapture/index.ts b/src/extensions/exception-autocapture/index.ts index 97433c9b9..f0d093d23 100644 --- a/src/extensions/exception-autocapture/index.ts +++ b/src/extensions/exception-autocapture/index.ts @@ -1,4 +1,4 @@ -import { window } from '../../utils' +import { window } from '../../utils/globals' import { PostHog } from '../../posthog-core' import { DecideResponse, Properties } from '../../types' import { ErrorEventArgs, ErrorProperties, errorToProperties, unhandledRejectionToProperties } from './error-conversion' diff --git a/src/extensions/toolbar.ts b/src/extensions/toolbar.ts index 40bc681f9..bf91cf914 100644 --- a/src/extensions/toolbar.ts +++ b/src/extensions/toolbar.ts @@ -1,9 +1,10 @@ -import { _register_event, _try, loadScript, window } from '../utils' +import { _register_event, _try, loadScript } from '../utils' import { PostHog } from '../posthog-core' import { DecideResponse, ToolbarParams } from '../types' import { POSTHOG_MANAGED_HOSTS } from './cloud' import { _getHashParam } from '../utils/request-utils' import { logger } from '../utils/logger' +import { window } from '../utils/globals' // TRICKY: Many web frameworks will modify the route on load, potentially before posthog is initialized. // To get ahead of this we grab it as soon as the posthog-js is parsed diff --git a/src/gdpr-utils.ts b/src/gdpr-utils.ts index 2e3ac07be..1904d3d58 100644 --- a/src/gdpr-utils.ts +++ b/src/gdpr-utils.ts @@ -11,7 +11,8 @@ * These functions are used internally by the SDK and are not intended to be publicly exposed. */ -import { _each, _includes, window } from './utils' +import { _each, _includes } from './utils' +import { window } from './utils/globals' import { cookieStore, localStore, localPlusCookieStore } from './storage' import { GDPROptions, PersistentStore } from './types' import { PostHog } from './posthog-core' diff --git a/src/page-view.ts b/src/page-view.ts index d2beb9248..35de57395 100644 --- a/src/page-view.ts +++ b/src/page-view.ts @@ -1,4 +1,4 @@ -import { window } from './utils' +import { window } from './utils/globals' interface PageViewData { pathname: string diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 56dec273a..db65306ae 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -7,10 +7,9 @@ import { _isBlockedUA, _register_event, _safewrap_class, - userAgent, - window, isCrossDomainCookie, } from './utils' +import { window } from './utils/globals' import { autocapture } from './autocapture' import { PostHogFeatureFlags } from './posthog-featureflags' import { PostHogPersistence } from './posthog-persistence' @@ -54,10 +53,10 @@ import { PostHogSurveys } from './posthog-surveys' import { RateLimiter } from './rate-limiter' import { uuidv7 } from './uuidv7' import { SurveyCallback } from './posthog-surveys-types' -import { document } from './utils' import { _isArray, _isEmptyObject, _isFunction, _isObject, _isString, _isUndefined } from './utils/type-utils' import { _info } from './utils/event-utils' import { logger } from './utils/logger' +import { document, userAgent } from './utils/globals' /* SIMPLE STYLE GUIDE: diff --git a/src/sessionid.ts b/src/sessionid.ts index bd029b263..fca582a16 100644 --- a/src/sessionid.ts +++ b/src/sessionid.ts @@ -3,7 +3,7 @@ import { SESSION_ID } from './constants' import { sessionStore } from './storage' import { PostHogConfig, SessionIdChangedCallback } from './types' import { uuidv7 } from './uuidv7' -import { window } from './utils' +import { window } from './utils/globals' import { _isArray, _isNumber, _isUndefined } from './utils/type-utils' import { logger } from './utils/logger' diff --git a/src/utils/event-utils.ts b/src/utils/event-utils.ts index 0bd142d9e..2fed693a9 100644 --- a/src/utils/event-utils.ts +++ b/src/utils/event-utils.ts @@ -2,7 +2,8 @@ import { _getQueryParam } from './request-utils' import { _isNull, _isUndefined } from './type-utils' import { Properties } from '../types' import Config from '../config' -import { _each, _extend, _includes, _strip_empty_properties, _timestamp, document, userAgent } from './index' +import { _each, _extend, _includes, _strip_empty_properties, _timestamp } from './index' +import { document, userAgent } from './globals' export const _info = { campaignParams: function (customParams?: string[]): Record { diff --git a/src/utils/globals.ts b/src/utils/globals.ts new file mode 100644 index 000000000..1fce48be4 --- /dev/null +++ b/src/utils/globals.ts @@ -0,0 +1,13 @@ +/* + * Saved references to long variable names, so that closure compiler can + * minimize file size. + */ +export const ArrayProto = Array.prototype +export const nativeForEach = ArrayProto.forEach +export const nativeIndexOf = ArrayProto.indexOf +export const win: Window & typeof globalThis = typeof window !== 'undefined' ? window : ({} as typeof window) +const navigator = win.navigator || { userAgent: '' } +const document = win.document || {} +const userAgent = navigator.userAgent + +export { win as window, userAgent, document } diff --git a/src/utils/index.ts b/src/utils/index.ts index 2197d44d9..99a5d237e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -10,18 +10,8 @@ import { hasOwnProperty, } from './type-utils' import { logger } from './logger' +import { document, nativeForEach, nativeIndexOf } from './globals' -/* - * Saved references to long variable names, so that closure compiler can - * minimize file size. - */ -export const ArrayProto = Array.prototype -export const nativeForEach = ArrayProto.forEach -export const nativeIndexOf = ArrayProto.indexOf -const win: Window & typeof globalThis = typeof window !== 'undefined' ? window : ({} as typeof window) -const navigator = win.navigator || { userAgent: '' } -const document = win.document || {} -const userAgent = navigator.userAgent const breaker: Breaker = {} // UNDERSCORE @@ -555,5 +545,3 @@ export function isCrossDomainCookie(documentLocation: Location | undefined) { // for the hostname return hostname.split('.').slice(-2).join('.') !== 'herokuapp.com' } - -export { win as window, userAgent, document } diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 275ea95cb..48a52f89e 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -1,6 +1,6 @@ import Config from '../config' import { _isUndefined } from './type-utils' -import { window } from './index' +import { window } from './globals' const LOGGER_PREFIX = '[PostHog.js]' export const logger = { diff --git a/src/uuidv7.ts b/src/uuidv7.ts index be86385fd..c1d6e3bb9 100644 --- a/src/uuidv7.ts +++ b/src/uuidv7.ts @@ -9,7 +9,7 @@ */ // polyfill for IE11 -import { window } from './utils' +import { window } from './utils/globals' import { _isNumber, _isUndefined } from './utils/type-utils'