Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 30, 2023
1 parent 0385bf6 commit 8bd0e2a
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/page-view.ts
Original file line number Diff line number Diff line change
@@ -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()
},
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/rate-limiter.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
2 changes: 1 addition & 1 deletion src/extensions/exception-autocapture/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/toolbar.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/gdpr-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/page-view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window } from './utils'
import { window } from './utils/globals'

interface PageViewData {
pathname: string
Expand Down
5 changes: 2 additions & 3 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/sessionid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/utils/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/globals.ts
Original file line number Diff line number Diff line change
@@ -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 }
14 changes: 1 addition & 13 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/uuidv7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

// polyfill for IE11
import { window } from './utils'
import { window } from './utils/globals'

import { _isNumber, _isUndefined } from './utils/type-utils'

Expand Down

0 comments on commit 8bd0e2a

Please sign in to comment.