Skip to content

Commit

Permalink
chore: remove circular dependencies (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra authored Dec 18, 2024
1 parent 6da785b commit c2acbc9
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 127 deletions.
4 changes: 3 additions & 1 deletion src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
* currently not supported in the browser lib).
*/

import { _copyAndTruncateStrings, isCrossDomainCookie, _base64Encode } from '../utils'
import { _copyAndTruncateStrings, isCrossDomainCookie } from '../utils'
import { Info } from '../utils/event-utils'
import { isLikelyBot, DEFAULT_BLOCKED_UA_STRS, isBlockedUA, NavigatorUAData } from '../utils/blocked-uas'
import { expect } from '@jest/globals'

import { _base64Encode } from '../utils/encode-utils'

function userAgentFor(botString: string) {
const randOne = (Math.random() + 1).toString(36).substring(7)
const randTwo = (Math.random() + 1).toString(36).substring(7)
Expand Down
3 changes: 2 additions & 1 deletion src/autocapture-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AutocaptureConfig, Properties } from './types'
import { each, entries, includes, trim } from './utils'
import { each, entries } from './utils'

import { isArray, isNullish, isString, isUndefined } from './utils/type-utils'
import { logger } from './utils/logger'
import { window } from './utils/globals'
import { isDocumentFragment, isElementNode, isTag, isTextNode } from './utils/element-utils'
import { includes, trim } from './utils/string-utils'

export function splitClassString(s: string): string[] {
return s ? trim(s).split(/\s+/) : []
Expand Down
3 changes: 2 additions & 1 deletion src/autocapture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { each, extend, includes, registerEvent } from './utils'
import { each, extend, registerEvent } from './utils'
import {
autocaptureCompatibleElements,
getClassNames,
Expand All @@ -24,6 +24,7 @@ import { createLogger } from './utils/logger'
import { document, window } from './utils/globals'
import { convertToURL } from './utils/request-utils'
import { isDocumentFragment, isElementNode, isTag, isTextNode } from './utils/element-utils'
import { includes } from './utils/string-utils'

const logger = createLogger('[AutoCapture]')

Expand Down
3 changes: 2 additions & 1 deletion src/consent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PostHog } from './posthog-core'
import { find, includes } from './utils'
import { find } from './utils'
import { assignableWindow, navigator } from './utils/globals'
import { cookieStore, localStore } from './storage'
import { PersistentStore } from './types'
import { includes } from './utils/string-utils'

const OPT_OUT_PREFIX = '__ph_opt_in_out_'

Expand Down
2 changes: 1 addition & 1 deletion src/customizations/before-send.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clampToRange } from '../utils/number-utils'
import { BeforeSendFn, CaptureResult, KnownEventName } from '../types'
import { includes } from '../utils'
import { isArray, isUndefined } from '../utils/type-utils'
import { includes } from '../utils/string-utils'

function appendArray(currentValue: string[] | undefined, sampleType: string | string[]): string[] {
return [...(currentValue ? currentValue : []), ...(isArray(sampleType) ? sampleType : [sampleType])]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PostHog } from '../posthog-core'
import { CAMPAIGN_PARAMS, EVENT_TO_PERSON_PROPERTIES, Info } from '../utils/event-utils'
import { each, extend, includes } from '../utils'
import { each, extend } from '../utils'
import { includes } from '../utils/string-utils'

export const setAllPersonProfilePropertiesAsPersonPropertiesForFlags = (posthog: PostHog): void => {
const allProperties = extend({}, Info.properties(), Info.campaignParams(), Info.referrerInfo())
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import { isLocalhost } from '../../utils/request-utils'
import { MutationRateLimiter } from './mutation-rate-limiter'
import { gzipSync, strFromU8, strToU8 } from 'fflate'
import { clampToRange } from '../../utils/number-utils'
import { includes } from '../../utils'
import Config from '../../config'
import { includes } from '../../utils/string-utils'

const LOGGER_PREFIX = '[SessionRecording]'
const logger = createLogger(LOGGER_PREFIX)
Expand Down
3 changes: 2 additions & 1 deletion src/heatmaps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { includes, registerEvent } from './utils'
import { registerEvent } from './utils'
import RageClick from './extensions/rageclick'
import { DeadClickCandidate, Properties, RemoteConfig } from './types'
import { PostHog } from './posthog-core'
Expand All @@ -10,6 +10,7 @@ import { isEmptyObject, isObject, isUndefined } from './utils/type-utils'
import { createLogger } from './utils/logger'
import { isElementInToolbar, isElementNode, isTag } from './utils/element-utils'
import { DeadClicksAutocapture, isDeadClicksEnabledForHeatmaps } from './extensions/dead-clicks-autocapture'
import { includes } from './utils/string-utils'

const DEFAULT_FLUSH_INTERVAL = 5000

Expand Down
3 changes: 1 addition & 2 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import {
each,
eachArray,
extend,
includes,
registerEvent,
safewrapClass,
isCrossDomainCookie,
isDistinctIdStringLike,
} from './utils'
import { assignableWindow, document, location, navigator, userAgent, window } from './utils/globals'
import { PostHogFeatureFlags } from './posthog-featureflags'
Expand Down Expand Up @@ -84,6 +82,7 @@ import { WebExperiments } from './web-experiments'
import { PostHogExceptions } from './posthog-exceptions'
import { SiteApps } from './site-apps'
import { DeadClicksAutocapture, isDeadClicksEnabledForAutocapture } from './extensions/dead-clicks-autocapture'
import { includes, isDistinctIdStringLike } from './utils/string-utils'

/*
SIMPLE STYLE GUIDE:
Expand Down
3 changes: 2 additions & 1 deletion src/posthog-persistence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint camelcase: "off" */

import { each, extend, include, stripEmptyProperties, stripLeadingDollar } from './utils'
import { each, extend, include, stripEmptyProperties } from './utils'
import { cookieStore, localPlusCookieStore, localStore, memoryStore, sessionStore } from './storage'
import { PersistentStore, PostHogConfig, Properties } from './types'
import {
Expand All @@ -15,6 +15,7 @@ import {
import { isEmptyObject, isObject, isUndefined } from './utils/type-utils'
import { Info } from './utils/event-utils'
import { logger } from './utils/logger'
import { stripLeadingDollar } from './utils/string-utils'

const CASE_INSENSITIVE_PERSISTENCE_TYPES: readonly Lowercase<PostHogConfig['persistence']>[] = [
'cookie',
Expand Down
4 changes: 3 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _base64Encode, each, find } from './utils'
import { each, find } from './utils'
import Config from './config'
import { Compression, RequestOptions, RequestResponse } from './types'
import { formDataToQuery } from './utils/request-utils'
Expand All @@ -7,6 +7,8 @@ import { logger } from './utils/logger'
import { AbortController, fetch, navigator, XMLHttpRequest } from './utils/globals'
import { gzipSync, strToU8 } from 'fflate'

import { _base64Encode } from './utils/encode-utils'

// eslint-disable-next-line compat/compat
export const SUPPORTS_REQUEST = !!XMLHttpRequest || !!fetch

Expand Down
95 changes: 95 additions & 0 deletions src/utils/encode-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { isNull } from './type-utils'

export function _base64Encode(data: null): null
export function _base64Encode(data: undefined): undefined
export function _base64Encode(data: string): string
export function _base64Encode(data: string | null | undefined): string | null | undefined {
const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
let o1,
o2,
o3,
h1,
h2,
h3,
h4,
bits,
i = 0,
ac = 0,
enc = ''
const tmp_arr: string[] = []

if (!data) {
return data
}

data = utf8Encode(data)

do {
// pack three octets into four hexets
o1 = data.charCodeAt(i++)
o2 = data.charCodeAt(i++)
o3 = data.charCodeAt(i++)

bits = (o1 << 16) | (o2 << 8) | o3

h1 = (bits >> 18) & 0x3f
h2 = (bits >> 12) & 0x3f
h3 = (bits >> 6) & 0x3f
h4 = bits & 0x3f

// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4)
} while (i < data.length)

enc = tmp_arr.join('')

switch (data.length % 3) {
case 1:
enc = enc.slice(0, -2) + '=='
break
case 2:
enc = enc.slice(0, -1) + '='
break
}

return enc
}

export const utf8Encode = function (string: string): string {
string = (string + '').replace(/\r\n/g, '\n').replace(/\r/g, '\n')

let utftext = '',
start,
end
let stringl = 0,
n

start = end = 0
stringl = string.length

for (n = 0; n < stringl; n++) {
const c1 = string.charCodeAt(n)
let enc = null

if (c1 < 128) {
end++
} else if (c1 > 127 && c1 < 2048) {
enc = String.fromCharCode((c1 >> 6) | 192, (c1 & 63) | 128)
} else {
enc = String.fromCharCode((c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1 & 63) | 128)
}
if (!isNull(enc)) {
if (end > start) {
utftext += string.substring(start, end)
}
utftext += enc
start = end = n + 1
}
}

if (end > start) {
utftext += string.substring(start, string.length)
}

return utftext
}
3 changes: 2 additions & 1 deletion src/utils/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { getQueryParam, convertToURL } from './request-utils'
import { isNull } from './type-utils'
import { Properties } from '../types'
import Config from '../config'
import { each, extend, stripEmptyProperties, stripLeadingDollar } from './index'
import { each, extend, stripEmptyProperties } from './index'
import { document, location, userAgent, window } from './globals'
import { detectBrowser, detectBrowserVersion, detectDevice, detectDeviceType, detectOS } from './user-agent-utils'
import { stripLeadingDollar } from './string-utils'

const URL_REGEX_PREFIX = 'https?://(.*)'

Expand Down
Loading

0 comments on commit c2acbc9

Please sign in to comment.