init commit for iPAL-kit plugin
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import type { ConsentState } from './categories.js'
|
||||
|
||||
import { CONSENT_CATEGORIES, DEFAULT_CONSENT } from './categories.js'
|
||||
|
||||
/**
|
||||
* Consent persistence in a cookie. Stored with a version so we can invalidate
|
||||
* old consents when the policy changes, and a timestamp (GDPR: consent must be
|
||||
* dated). Readable both client-side (banner) and server-side (script gating).
|
||||
*/
|
||||
export const CONSENT_COOKIE = 'cookie-consent'
|
||||
export const CONSENT_VERSION = 1
|
||||
export const CONSENT_MAX_AGE = 60 * 60 * 24 * 180 // 180 days
|
||||
|
||||
type StoredConsent = {
|
||||
state: ConsentState
|
||||
timestamp: string
|
||||
version: number
|
||||
}
|
||||
|
||||
export function serializeConsent(state: ConsentState): string {
|
||||
const payload: StoredConsent = {
|
||||
state,
|
||||
timestamp: new Date().toISOString(),
|
||||
version: CONSENT_VERSION,
|
||||
}
|
||||
return encodeURIComponent(JSON.stringify(payload))
|
||||
}
|
||||
|
||||
export function parseConsent(raw: string | undefined): ConsentState | null {
|
||||
if (!raw) {return null}
|
||||
try {
|
||||
const parsed = JSON.parse(decodeURIComponent(raw)) as StoredConsent
|
||||
if (parsed.version !== CONSENT_VERSION) {return null}
|
||||
const state = { ...DEFAULT_CONSENT }
|
||||
for (const cat of CONSENT_CATEGORIES) {
|
||||
if (typeof parsed.state?.[cat] === 'boolean') {state[cat] = parsed.state[cat]}
|
||||
}
|
||||
state.necessary = true
|
||||
return state
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user