This repository has been archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1abf1b7
commit 1de9941
Showing
5 changed files
with
81 additions
and
117 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,74 @@ | ||
import { Enricher, EventBus } from '../types' | ||
import { getQueryParameter, ParsedParam } from '../utils/url' | ||
import { trim, isUUID, expiresInDays } from 'live-connect-common' | ||
import { WrappedStorageHandler } from '../handlers/storage-handler' | ||
import { resolve } from '../manager/decisions' | ||
|
||
type Input = { pageUrl?: string, domain: string } | ||
type Output = { decisionIds: string[] } | ||
|
||
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 | ||
|
||
export function enrichDecisionIds( | ||
storageHandler: WrappedStorageHandler, | ||
eventBus: EventBus | ||
): Enricher<Input, Output> { | ||
return state => ({ ...state, ...resolve(state, storageHandler, eventBus) }) | ||
return 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 _orElseEmpty<A>(errorDescription: string, f: () => A[]): A[] { | ||
try { | ||
return f() | ||
} catch (e) { | ||
eventBus.emitErrorWithMessage('DecisionsResolve', errorDescription, e) | ||
return [] | ||
} | ||
} | ||
|
||
const freshDecisions = _orElseEmpty( | ||
'Error while extracting new decision ids', | ||
() => { | ||
const extractedFreshDecisions = ([] as ParsedParam[]).concat((state.pageUrl && getQueryParameter(state.pageUrl, DECISION_ID_QUERY_PARAM_NAME)) || []) | ||
return extractedFreshDecisions | ||
.map(trim) | ||
.filter(_nonEmpty) | ||
.filter(isUUID) | ||
.filter(_onlyUnique) | ||
} | ||
) | ||
|
||
const storedDecisions = _orElseEmpty( | ||
'Error while retrieving stored decision ids', | ||
() => { | ||
const extractedStoredDecisions = storageHandler.findSimilarCookies(DECISION_ID_COOKIE_NAMESPACE) | ||
return extractedStoredDecisions | ||
.map(trim) | ||
.filter(_nonEmpty) | ||
.filter(isUUID) | ||
} | ||
) | ||
|
||
freshDecisions.forEach(decision => { | ||
try { | ||
_addDecisionId(decision, state.domain) | ||
} catch (e) { | ||
eventBus.emitErrorWithMessage('DecisionsResolve', 'Error while storing new decision id', e) | ||
} | ||
}) | ||
|
||
return { ...state, decisionIds: freshDecisions.concat(storedDecisions).filter(_onlyUnique) } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters