Repository navigation
Expand file tree
/
Copy pathconsentStore.ts
More file actions
177 lines (152 loc) · 6.25 KB
/
Copy pathconsentStore.ts
File metadata and controls
177 lines (152 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/** Optional purposes a visitor can consent to; essential processing needs no consent and has no flag. */
export interface ConsentChoices {
/**
* Google Firebase Analytics and Performance Monitoring, Vercel Speed Insights, and Sentry's browser error reporting.
*/
analytics: boolean;
/** YouTube previews embedded in project cards. */
media: boolean;
/** The service worker that saves pages on the device for offline use. */
offline: boolean;
/** Each linked site's own icon beside a link, fetched through DuckDuckGo's icon proxy. */
linkIcons: boolean;
}
/** Name of the first-party cookie recording the visitor's choices. */
export const CONSENT_COOKIE = 'cookie-consent';
/** How long a choice is kept before the banner asks again: 6 months, the re-consent interval the Irish DPC expects. */
export const CONSENT_MAX_AGE_DAYS = 180;
/** Version of the purposes and vendors a choice was made against; raising it asks every visitor again. */
export const CONSENT_VERSION = 2;
/**
* The one-letter code of each purpose in the stored cookie, in the order they are written. Adding a purpose here adds
* it to the cookie format, the parser, and both preset choices below.
*/
export const PURPOSE_CODES: Readonly<Record<keyof ConsentChoices, string>> = {
analytics: 'a',
media: 'm',
offline: 'o',
linkIcons: 'l',
};
/** The purposes, in cookie order. */
export const PURPOSE_KEYS = Object.keys(PURPOSE_CODES) as (keyof ConsentChoices)[];
/**
* Builds a choice with every purpose set to one value.
* @param granted Whether every purpose is allowed
* @returns The frozen choice
*/
function allPurposes(granted: boolean): Readonly<ConsentChoices> {
return Object.freeze(Object.fromEntries(PURPOSE_KEYS.map((key) => [key, granted])) as unknown as ConsentChoices);
}
/** Choices with every optional purpose refused, as saved by "Essential only". */
export const ESSENTIAL_ONLY = allPurposes(false);
/** Choices with every optional purpose granted, as saved by "Accept all". */
export const ACCEPT_ALL = allPurposes(true);
/**
* Serialised cookie value, such as `v2.a1.m0.o1.l0.t1791490457`: version, one flag per purpose, and the Unix time the
* choice was made.
*/
const COOKIE_PATTERN = new RegExp(
`^v(\\d+)\\.${PURPOSE_KEYS.map((key) => `${PURPOSE_CODES[key]}([01])`).join('\\.')}\\.t(\\d+)$`,
);
/** Listeners notified when the stored choice changes, in this tab or another. */
const consentListeners = new Set<() => void>();
/** Channel to the site's other open tabs; created on first use, and `null` where `BroadcastChannel` is unavailable. */
let channel: BroadcastChannel | null | undefined;
/** Notifies every subscriber that the stored choice may have changed. */
function notifyListeners(): void {
consentListeners.forEach((listener) => listener());
}
/**
* Opens the channel that carries a change made in one tab to the others, so withdrawing consent in one tab stops
* analytics in all of them. Created lazily in the browser, since a channel opened during server rendering would keep
* the Node process alive.
* @returns The channel, or `null` where unavailable
*/
function getChannel(): BroadcastChannel | null {
if (channel === undefined) {
channel =
typeof window === 'undefined' || typeof BroadcastChannel === 'undefined'
? null
: new BroadcastChannel(CONSENT_COOKIE);
channel?.addEventListener('message', notifyListeners);
}
return channel;
}
/** Raw cookie value the cached snapshot was parsed from; `undefined` until the first read. */
let cachedRaw: string | null | undefined;
/** Snapshot parsed from {@link cachedRaw}, returned again while the cookie is unchanged. */
let cachedChoices: Readonly<ConsentChoices> | null = null;
/**
* Reads the raw consent cookie value.
* @returns The value, or `null` when the cookie is absent
*/
function readCookie(): string | null {
for (const pair of document.cookie.split(';')) {
const [key, ...value] = pair.trim().split('=');
if (key === CONSENT_COOKIE) {
return value.join('=');
}
}
return null;
}
/**
* Parses a raw cookie value written by {@link saveConsent} for the current {@link CONSENT_VERSION}.
* @param raw Raw cookie value
* @returns The choices, or `null` for a missing, malformed, legacy, or outdated value
*/
function parseCookie(raw: string | null): Readonly<ConsentChoices> | null {
const match = raw === null ? null : COOKIE_PATTERN.exec(raw);
if (match === null || Number(match[1]) !== CONSENT_VERSION) {
return null;
}
return Object.freeze(
Object.fromEntries(
PURPOSE_KEYS.map((key, index) => [key, match[index + 2] === '1']),
) as unknown as ConsentChoices,
);
}
/**
* Reads the visitor's consent choices.
*
* The result is the same object for as long as the cookie is unchanged, which `useSyncExternalStore` requires. A Global
* Privacy Control or Do Not Track signal is not a choice: it leaves the visitor undecided, so nothing optional runs and
* the banner still asks.
* @returns The choices, or `null` while the visitor has not decided
*/
export function readConsent(): Readonly<ConsentChoices> | null {
if (typeof document === 'undefined') {
return null;
}
const raw = readCookie();
if (raw !== cachedRaw) {
cachedRaw = raw;
cachedChoices = parseCookie(raw);
}
return cachedChoices;
}
/**
* Stores the visitor's choices for {@link CONSENT_MAX_AGE_DAYS} and notifies subscribers in this tab and in every
* other open tab of the site.
* @param choices Choices to store
*/
export function saveConsent(choices: Readonly<ConsentChoices>): void {
const flags = PURPOSE_KEYS.map((key) => `${PURPOSE_CODES[key]}${Number(choices[key])}`).join('.');
const timestamp = Math.floor(Date.now() / 1000);
const secure = window.location.protocol === 'https:' ? '; Secure' : '';
const maxAge = CONSENT_MAX_AGE_DAYS * 24 * 60 * 60;
document.cookie = `${CONSENT_COOKIE}=v${CONSENT_VERSION}.${flags}.t${timestamp}; max-age=${maxAge}; path=/; SameSite=Lax${secure}`;
notifyListeners();
getChannel()?.postMessage('changed');
}
/**
* Subscribes to changes of the stored choice, in the shape `useSyncExternalStore` expects.
* @param listener Called after each change
* @returns A function that removes the subscription
*/
export function subscribeConsent(listener: () => void): () => void {
getChannel();
consentListeners.add(listener);
return () => {
consentListeners.delete(listener);
};
}