Skip to content

Commit

Permalink
refactor state wiring
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Sep 15, 2023
1 parent 0c9c9cd commit 24099e1
Show file tree
Hide file tree
Showing 36 changed files with 738 additions and 682 deletions.
Binary file removed live-connect-js-6.0.3-alpha-8f0b28c.0.tgz
Binary file not shown.
16 changes: 1 addition & 15 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strEqualsIgnoreCase, expiresInHours } from 'live-connect-common'
import { WrappedStorageHandler } from './handlers/storage-handler'
import { StorageStrategies, StorageStrategy } from './model/storage-strategy'
import { StorageStrategies } from './model/storage-strategy'

export type CacheRecord = {
data: string
Expand All @@ -19,20 +19,6 @@ export type StorageHandlerBackedCacheOpts = {
defaultExpirationHours?: number
}

// export type MakeCacheOpts = StorageHandlerBackedCacheOpts & {
// strategy: StorageStrategy,
// }

// export function makeCache(opts: MakeCacheOpts): DurableCache {
// if (!strEqualsIgnoreCase(opts.strategy, StorageStrategies.cookie) && strEqualsIgnoreCase(opts.strategy, StorageStrategies.none)) {
// return NoOpCache
// } else {
// // TODO: Remove once we validate config properly
// const strategyWithDefault = opts.strategy ?? StorageStrategies.cookie
// return new StorageHandlerBackedCache({ ...opts, strategy: strategyWithDefault })
// }
// }

export class StorageHandlerBackedCache implements DurableCache {
private handler
private storageStrategy
Expand Down
14 changes: 7 additions & 7 deletions src/enrichers/cache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { strEqualsIgnoreCase } from "live-connect-common";
import { DurableCache, NoOpCache, StorageHandlerBackedCache } from "../cache";
import { WrappedStorageHandler } from "../handlers/storage-handler";
import { StorageStrategies, StorageStrategy } from "../model/storage-strategy";
import { Enricher } from "../types";
import { strEqualsIgnoreCase } from 'live-connect-common'
import { DurableCache, NoOpCache, StorageHandlerBackedCache } from '../cache'
import { WrappedStorageHandler } from '../handlers/storage-handler'
import { StorageStrategies, StorageStrategy } from '../model/storage-strategy'
import { Enricher } from '../types'

type Input = { domain: string, storageHandler: WrappedStorageHandler, storageStrategy: StorageStrategy }
type Output = { cache: DurableCache }
Expand All @@ -16,13 +16,13 @@ export const enrichCache: Enricher<Input, Output> = state => {
cache = new StorageHandlerBackedCache({
strategy: 'ls',
storageHandler: state.storageHandler,
domain: state.domain,
domain: state.domain
})
} else {
cache = new StorageHandlerBackedCache({
strategy: 'cookie',
storageHandler: state.storageHandler,
domain: state.domain,
domain: state.domain
})
}
return { ...state, cache }
Expand Down
6 changes: 3 additions & 3 deletions src/enrichers/call-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CallHandler, EventBus } from "live-connect-common"
import { WrappedCallHandler } from "../handlers/call-handler"
import { Enricher } from "../types"
import { CallHandler, EventBus } from 'live-connect-common'
import { WrappedCallHandler } from '../handlers/call-handler'
import { Enricher } from '../types'

type Input = { callHandler: CallHandler, eventBus: EventBus }
type Output = { callHandler: WrappedCallHandler }
Expand Down
67 changes: 4 additions & 63 deletions src/enrichers/decisions.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,11 @@
import { getQueryParameter, ParsedParam } from '../utils/url'
import { trim, isUUID, expiresInDays } from 'live-connect-common'
import { Enricher, EventBus, State } from '../types'
import { Enricher, EventBus } from '../types'
import { WrappedStorageHandler } from '../handlers/storage-handler'

const DEFAULT_DECISION_ID_COOKIE_EXPIRES = expiresInDays(30)
const DECISION_ID_QUERY_PARAM_NAME = 'li_did'
const DECISION_ID_COOKIE_NAMESPACE = 'lidids.'

const _onlyUnique = (value: string, index: number, self: string[]) => self.indexOf(value) === index
const _nonEmpty = (value: string) => value && trim(value).length > 0
import { resolve } from '../manager/decisions'

type Input = { storageHandler: WrappedStorageHandler, eventBus: EventBus, pageUrl?: string, domain: string }
type Output = { decisionIds: string[] }

export const enrichDecisionIds: Enricher<Input, Output> = state => {
const { storageHandler, eventBus, pageUrl, domain } = state
function _addDecisionId(key: string, cookieDomain?: string) {
if (key) {
storageHandler.setCookie(
`${DECISION_ID_COOKIE_NAMESPACE}${key}`,
key,
DEFAULT_DECISION_ID_COOKIE_EXPIRES,
'Lax',
cookieDomain)
}
}

function _orElseEmtpy<A>(errorDescription: string, f: () => A[]): A[] {
try {
return f()
} catch (e) {
eventBus.emitErrorWithMessage('DecisionsResolve', errorDescription, e)
return []
}
}

const freshDecisions = _orElseEmtpy(
'Error while extracting new decision ids',
() => {
const extractedFreshDecisions = ([] as ParsedParam[]).concat((pageUrl && getQueryParameter(pageUrl, DECISION_ID_QUERY_PARAM_NAME)) || [])
return extractedFreshDecisions
.map(trim)
.filter(_nonEmpty)
.filter(isUUID)
.filter(_onlyUnique)
}
)

const storedDecisions = _orElseEmtpy(
'Error while retrieving stored decision ids',
() => {
const extractedStoredDecisions = storageHandler.findSimilarCookies(DECISION_ID_COOKIE_NAMESPACE)
return extractedStoredDecisions.map(trim)
.filter(_nonEmpty)
.filter(isUUID)
.filter(_onlyUnique)
}
)

freshDecisions.forEach(decision => {
try {
_addDecisionId(decision, domain)
} catch (e) {
eventBus.emitErrorWithMessage('DecisionsResolve', 'Error while storing new decision id', e)
}
})

return { ...state, decisionIds: freshDecisions.concat(storedDecisions).filter(_onlyUnique) }
const { storageHandler, eventBus } = state
return { ...state, ...resolve(state, storageHandler, eventBus) }
}
6 changes: 3 additions & 3 deletions src/enrichers/domain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WrappedStorageHandler } from "../handlers/storage-handler"
import { Enricher } from "../types"
import { determineHighestAccessibleDomain } from "../utils/domain"
import { WrappedStorageHandler } from '../handlers/storage-handler'
import { Enricher } from '../types'
import { determineHighestAccessibleDomain } from '../utils/domain'

type Input = { storageHandler: WrappedStorageHandler }
type Output = { domain: string }
Expand Down
10 changes: 0 additions & 10 deletions src/enrichers/error-pixel.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/enrichers/identifiers-nohash.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { containsEmailField, isEmail } from '../utils/email'
import { safeToString, isArray, trim } from 'live-connect-common'
import { Enricher, EventBus, RetrievedIdentifier, State } from '../types'
import { Enricher, EventBus, RetrievedIdentifier } from '../types'
import { WrappedReadOnlyStorageHandler } from '../handlers/storage-handler'

type Input = {identifiersToResolve: string | string[], storageHandler: WrappedReadOnlyStorageHandler, eventBus: EventBus}
type Output = {retrievedIdentifiers: RetrievedIdentifier[]}
type Input = { identifiersToResolve: string | string[], storageHandler: WrappedReadOnlyStorageHandler, eventBus: EventBus }
type Output = { retrievedIdentifiers: RetrievedIdentifier[] }

export const enrichIdentifiers: Enricher<Input, Output> = state => {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/enrichers/identifiers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { replaceEmailsWithHashes } from '../utils/email'
import { safeToString, isString, isArray } from 'live-connect-common'
import { EventBus, HashedEmail, State, RetrievedIdentifier, Enricher } from '../types'
import { EventBus, HashedEmail, RetrievedIdentifier, Enricher } from '../types'
import { WrappedReadOnlyStorageHandler } from '../handlers/storage-handler'

type Input = { identifiersToResolve: string | string[], storageHandler: WrappedReadOnlyStorageHandler, eventBus: EventBus }
Expand Down
7 changes: 3 additions & 4 deletions src/enrichers/live-connect-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ulid } from '../utils/ulid'
import { domainHash } from '../utils/hash'
import { expiresInDays } from 'live-connect-common'
import { PEOPLE_VERIFIED_LS_ENTRY } from '../utils/consts'
import { Enricher, EventBus } from '../types'
import { Enricher } from '../types'
import { WrappedStorageHandler } from '../handlers/storage-handler'
import { DurableCache } from '../cache'

Expand All @@ -13,16 +13,15 @@ type Input = { expirationDays?: number, domain: string, cache: DurableCache, sto
type Output = { liveConnectId?: string, peopleVerifiedId?: string }

export const enrichLiveConnectId: Enricher<Input, Output> = state => {
const {expirationDays, domain, storageHandler, cache } = state
const { expirationDays, domain, storageHandler, cache } = state

const expiry = expirationDays || DEFAULT_EXPIRATION_DAYS
const oldValue = cache.get(NEXT_GEN_FP_NAME)?.data

if (oldValue) {
cache.set(NEXT_GEN_FP_NAME, oldValue, expiresInDays(expiry))
} else {
const newValue = `${domainHash(domain)}--${ulid()}`

const newValue = `${domainHash(domain)}--${ulid()}`.toLocaleLowerCase()
cache.set(NEXT_GEN_FP_NAME, newValue, expiresInDays(expiry))
}

Expand Down
1 change: 0 additions & 1 deletion src/enrichers/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ export const enrichPage: Enricher<Input, Output> = state => {
contextElements: getContextElements(state.privacyMode, state.contextSelectors, state.contextElementsLength)
}
}

1 change: 0 additions & 1 deletion src/enrichers/people-verified-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ export const enrichPeopleVerifiedId: Enricher<Input, Output> = state => {
return state
}
}

12 changes: 6 additions & 6 deletions src/enrichers/storage-handler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { EventBus, ReadOnlyStorageHandler, StorageHandler } from "live-connect-common";
import { StorageStrategy } from "../model/storage-strategy";
import { Enricher } from "../types";
import { WrappedReadOnlyStorageHandler, WrappedStorageHandler } from "../handlers/storage-handler";
import { EventBus, ReadOnlyStorageHandler, StorageHandler } from 'live-connect-common'
import { StorageStrategy } from '../model/storage-strategy'
import { Enricher } from '../types'
import { WrappedReadOnlyStorageHandler, WrappedStorageHandler } from '../handlers/storage-handler'

type InputReadOnly = { storageStrategy: StorageStrategy, storageHandler: ReadOnlyStorageHandler, eventBus: EventBus }
type InputReadOnly = { storageStrategy: StorageStrategy, storageHandler: ReadOnlyStorageHandler, eventBus: EventBus }
type OutputReadOnly = { storageHandler: WrappedReadOnlyStorageHandler }

export const enrichReadOnlyStorageHandler: Enricher<InputReadOnly, OutputReadOnly> = state => {
const storageHandler = WrappedReadOnlyStorageHandler.make(state.storageStrategy, state.storageHandler, state.eventBus)
return { ...state, storageHandler }
}

type Input = { storageStrategy: StorageStrategy, storageHandler: StorageHandler, eventBus: EventBus }
type Input = { storageStrategy: StorageStrategy, storageHandler: StorageHandler, eventBus: EventBus }
type Output = { storageHandler: WrappedStorageHandler }

export const enrichStorageHandler: Enricher<Input, Output> = state => {
Expand Down
4 changes: 2 additions & 2 deletions src/enrichers/storage-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StorageStrategies, StorageStrategy } from "../model/storage-strategy";
import { Enricher } from "../types";
import { StorageStrategies, StorageStrategy } from '../model/storage-strategy'
import { Enricher } from '../types'

type Input = { privacyMode: boolean, storageStrategy?: StorageStrategy }
type Output = { storageStrategy: StorageStrategy }
Expand Down
8 changes: 2 additions & 6 deletions src/events/error-pixel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { PixelSender } from '../pixel/sender'
import { StateWrapper } from '../pixel/state'
import * as page from '../enrichers/page'
import { ERRORS_CHANNEL, isRecord, isString } from 'live-connect-common'
import { EventBus, State } from '../types'
import { WrappedCallHandler } from '../handlers/call-handler'

const MAX_ERROR_FIELD_LENGTH = 120

Expand Down Expand Up @@ -51,12 +49,10 @@ export function asErrorDetails(e: unknown): State {
}
}

export function register(state: {}, callHandler: WrappedCallHandler, eventBus: EventBus): void {
export function register(state: State, sender: PixelSender, eventBus: EventBus): void {
try {
const pixelSender = new PixelSender(state, callHandler, eventBus)

eventBus.on(ERRORS_CHANNEL, (error) => {
pixelSender.sendPixel(new StateWrapper(asErrorDetails(error), eventBus).combineWith(state || {}))
sender.sendPixel(new StateWrapper(asErrorDetails(error), eventBus).combineWith(state || {}))
})
} catch (e) {
console.error('handlers.error.register', e)
Expand Down
Loading

0 comments on commit 24099e1

Please sign in to comment.