@luwiostack/cookie-consent 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -31
- package/dist/index.cjs +257 -128
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -53
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +10 -53
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +227 -96
- package/dist/index.mjs.map +1 -1
- package/dist/machine-names--nw4DSRE.cjs +138 -0
- package/dist/machine-names--nw4DSRE.cjs.map +1 -0
- package/dist/machine-names-DX8eCM2Q.mjs +103 -0
- package/dist/machine-names-DX8eCM2Q.mjs.map +1 -0
- package/dist/react.cjs +1 -1
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.cts +1 -1
- package/dist/react.d.mts +1 -1
- package/dist/react.mjs +1 -1
- package/dist/react.mjs.map +1 -1
- package/dist/{types-BrxRJL4I.d.cts → types-WpJKTgRa.d.cts} +92 -16
- package/dist/types-WpJKTgRa.d.cts.map +1 -0
- package/dist/{types-BrxRJL4I.d.mts → types-WpJKTgRa.d.mts} +92 -16
- package/dist/types-WpJKTgRa.d.mts.map +1 -0
- package/dist/vendors.cjs +216 -46
- package/dist/vendors.cjs.map +1 -1
- package/dist/vendors.d.cts +20 -12
- package/dist/vendors.d.cts.map +1 -1
- package/dist/vendors.d.mts +20 -12
- package/dist/vendors.d.mts.map +1 -1
- package/dist/vendors.mjs +204 -34
- package/dist/vendors.mjs.map +1 -1
- package/package.json +3 -4
- package/dist/errors-BqpghFQf.cjs +0 -55
- package/dist/errors-BqpghFQf.cjs.map +0 -1
- package/dist/errors-D2ZIcEYK.mjs +0 -32
- package/dist/errors-D2ZIcEYK.mjs.map +0 -1
- package/dist/types-BrxRJL4I.d.cts.map +0 -1
- package/dist/types-BrxRJL4I.d.mts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_machine_names = require("./machine-names--nw4DSRE.cjs");
|
|
3
3
|
const require_used_vendors = require("./used-vendors-C8uSNIkd.cjs");
|
|
4
4
|
let _luwiostack_storage = require("@luwiostack/storage");
|
|
5
5
|
//#region src/domain/consent-decision.ts
|
|
@@ -24,9 +24,9 @@ var ConsentDecision = class ConsentDecision {
|
|
|
24
24
|
}
|
|
25
25
|
/** @throws {ConsentError} on an empty `consent_id` / `policy_version` or a non-finite `decided_at`. */
|
|
26
26
|
static new(input) {
|
|
27
|
-
if (typeof input.consent_id !== "string" || input.consent_id === "") throw new
|
|
28
|
-
if (typeof input.policy_version !== "string" || input.policy_version === "") throw new
|
|
29
|
-
if (typeof input.decided_at !== "number" || !Number.isFinite(input.decided_at)) throw new
|
|
27
|
+
if (typeof input.consent_id !== "string" || input.consent_id === "") throw new require_machine_names.ConsentError("A consent decision needs a non-empty consent_id.");
|
|
28
|
+
if (typeof input.policy_version !== "string" || input.policy_version === "") throw new require_machine_names.ConsentError("A consent decision needs a non-empty policy_version.");
|
|
29
|
+
if (typeof input.decided_at !== "number" || !Number.isFinite(input.decided_at)) throw new require_machine_names.ConsentError("A consent decision needs decided_at as finite epoch milliseconds.");
|
|
30
30
|
return new ConsentDecision(input);
|
|
31
31
|
}
|
|
32
32
|
/** Like {@link ConsentDecision.new}, but returns `null` instead of throwing. */
|
|
@@ -80,18 +80,43 @@ const COOKIE_TYPES = /* @__PURE__ */ new Set([
|
|
|
80
80
|
]);
|
|
81
81
|
/** Checks one vendor declaration: an id, unique cookie names, valid categories and lifetimes. */
|
|
82
82
|
function validateVendor(vendor, listedIn) {
|
|
83
|
-
if (typeof vendor?.id !== "string" || vendor.id === "") throw new
|
|
83
|
+
if (typeof vendor?.id !== "string" || vendor.id === "") throw new require_machine_names.ConsentError(`Category "${listedIn}": a vendor id must be non-empty.`);
|
|
84
84
|
const where = `Vendor "${vendor.id}"`;
|
|
85
|
-
if (!Array.isArray(vendor.cookies)) throw new
|
|
85
|
+
if (!Array.isArray(vendor.cookies)) throw new require_machine_names.ConsentError(`${where}: cookies must be an array.`);
|
|
86
|
+
validateTransfers(vendor.transfers, where);
|
|
86
87
|
const names = /* @__PURE__ */ new Set();
|
|
87
88
|
for (const cookie of vendor.cookies) {
|
|
88
|
-
if (typeof cookie
|
|
89
|
-
if (
|
|
89
|
+
if (typeof cookie !== "object" || cookie === null) throw new require_machine_names.ConsentError(`${where}: every cookie must be an object.`);
|
|
90
|
+
if (typeof cookie.name !== "string" || cookie.name === "") throw new require_machine_names.ConsentError(`${where}: a cookie name must be non-empty.`);
|
|
91
|
+
if (names.has(cookie.name)) throw new require_machine_names.ConsentError(`${where}: duplicate cookie "${cookie.name}".`);
|
|
90
92
|
names.add(cookie.name);
|
|
91
|
-
if (!
|
|
92
|
-
if (cookie.type !== void 0 && !COOKIE_TYPES.has(cookie.type)) throw new
|
|
93
|
+
if (!require_machine_names.isConsentCategoryId(cookie.category)) throw new require_machine_names.ConsentError(`${where}: cookie "${cookie.name}" needs a category (${require_machine_names.CONSENT_CATEGORIES.join(", ")}).`);
|
|
94
|
+
if (cookie.type !== void 0 && !COOKIE_TYPES.has(cookie.type)) throw new require_machine_names.ConsentError(`${where}: cookie "${cookie.name}" has an unknown type "${cookie.type}".`);
|
|
93
95
|
const { max_age } = cookie;
|
|
94
|
-
if (!(max_age === "session" || max_age === "persistent" || typeof max_age === "number" && Number.isFinite(max_age) && max_age > 0)) throw new
|
|
96
|
+
if (!(max_age === "session" || max_age === "persistent" || typeof max_age === "number" && Number.isFinite(max_age) && max_age > 0)) throw new require_machine_names.ConsentError(`${where}: cookie "${cookie.name}" needs max_age as positive milliseconds, 'session' or 'persistent'.`);
|
|
97
|
+
if (cookie.domain !== void 0 && (typeof cookie.domain !== "string" || !DOMAIN.test(cookie.domain))) throw new require_machine_names.ConsentError(`${where}: cookie "${cookie.name}" needs domain as a host name like ".youtube.com", or none for the site's own domain.`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** A cookie domain: a host name, optionally with a leading dot, no scheme, port or path. */
|
|
101
|
+
const DOMAIN = /^\.?[a-z0-9-]+(\.[a-z0-9-]+)+$/i;
|
|
102
|
+
const SAFEGUARDS = /* @__PURE__ */ new Set([
|
|
103
|
+
"adequacy",
|
|
104
|
+
"dpf",
|
|
105
|
+
"scc",
|
|
106
|
+
"bcr"
|
|
107
|
+
]);
|
|
108
|
+
/** Transfers: absent, or per unique ISO alpha-2 country at least one known safeguard. */
|
|
109
|
+
function validateTransfers(transfers, where) {
|
|
110
|
+
if (transfers === void 0) return;
|
|
111
|
+
if (!Array.isArray(transfers)) throw new require_machine_names.ConsentError(`${where}: transfers must be an array.`);
|
|
112
|
+
const countries = /* @__PURE__ */ new Set();
|
|
113
|
+
for (const transfer of transfers) {
|
|
114
|
+
const country = transfer?.country;
|
|
115
|
+
if (typeof country !== "string" || !/^[A-Z]{2}$/.test(country)) throw new require_machine_names.ConsentError(`${where}: a transfer needs country as ISO alpha-2, e.g. "US".`);
|
|
116
|
+
if (countries.has(country)) throw new require_machine_names.ConsentError(`${where}: duplicate transfer "${country}".`);
|
|
117
|
+
countries.add(country);
|
|
118
|
+
const { safeguards } = transfer;
|
|
119
|
+
if (!Array.isArray(safeguards) || safeguards.length === 0 || !safeguards.every((s) => SAFEGUARDS.has(s))) throw new require_machine_names.ConsentError(`${where}: transfer "${country}" needs safeguards from ${[...SAFEGUARDS].join(", ")}.`);
|
|
95
120
|
}
|
|
96
121
|
}
|
|
97
122
|
/**
|
|
@@ -100,38 +125,89 @@ function validateVendor(vendor, listedIn) {
|
|
|
100
125
|
* of any use of the service). A vendor listed under several categories is merged into one.
|
|
101
126
|
*/
|
|
102
127
|
function resolveVendors(config) {
|
|
103
|
-
if (typeof config !== "object" || config === null || Array.isArray(config)) throw new
|
|
104
|
-
for (const key of Object.keys(config)) if (!
|
|
128
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) throw new require_machine_names.ConsentError("categories must be an object of category id → vendors.");
|
|
129
|
+
for (const key of Object.keys(config)) if (!require_machine_names.isConsentCategoryId(key)) throw new require_machine_names.ConsentError(`Unknown consent category "${key}" — use ${require_machine_names.CONSENT_CATEGORIES.join(", ")}.`);
|
|
105
130
|
const merged = /* @__PURE__ */ new Map();
|
|
106
|
-
const
|
|
107
|
-
|
|
131
|
+
const idByMachineName = /* @__PURE__ */ new Map();
|
|
132
|
+
const listed = Object.fromEntries(require_machine_names.CONSENT_CATEGORIES.map((id) => [id, []]));
|
|
133
|
+
for (const category of require_machine_names.CONSENT_CATEGORIES) {
|
|
108
134
|
const inCategory = config[category];
|
|
109
135
|
if (inCategory === void 0) continue;
|
|
110
|
-
if (!Array.isArray(inCategory)) throw new
|
|
136
|
+
if (!Array.isArray(inCategory)) throw new require_machine_names.ConsentError(`Category "${category}": vendors must be an array.`);
|
|
111
137
|
const seen = /* @__PURE__ */ new Set();
|
|
112
|
-
for (const
|
|
113
|
-
validateVendor(
|
|
138
|
+
for (const declaration of inCategory) {
|
|
139
|
+
validateVendor(declaration, category);
|
|
140
|
+
const vendor = require_machine_names.toConsentVendor(declaration);
|
|
141
|
+
const other = idByMachineName.get(vendor.machine_name);
|
|
142
|
+
if (other !== void 0 && other !== vendor.id) throw new require_machine_names.ConsentError(`Vendors "${other}" and "${vendor.id}" have the same machine name "${vendor.machine_name}".`);
|
|
143
|
+
idByMachineName.set(vendor.machine_name, vendor.id);
|
|
114
144
|
const where = `Category "${category}", vendor "${vendor.id}"`;
|
|
115
|
-
if (seen.has(vendor.id)) throw new
|
|
145
|
+
if (seen.has(vendor.id)) throw new require_machine_names.ConsentError(`${where}: duplicate vendor.`);
|
|
116
146
|
seen.add(vendor.id);
|
|
117
147
|
listed[category].push(vendor.id);
|
|
118
|
-
if (vendor.cookies.filter((c) => c.category === category).length === 0) throw new
|
|
148
|
+
if (vendor.cookies.filter((c) => c.category === category).length === 0) throw new require_machine_names.ConsentError(`${where}: the vendor has no ${category} cookies.`);
|
|
119
149
|
const entry = merged.get(vendor.id) ?? {
|
|
120
150
|
vendor,
|
|
121
151
|
cookies: /* @__PURE__ */ new Map()
|
|
122
152
|
};
|
|
123
|
-
|
|
153
|
+
if (entry.vendor !== vendor && !sameVendorInfo(entry.vendor, vendor)) throw new require_machine_names.ConsentError(`Vendor "${vendor.id}" is declared differently in two categories (privacy_policy_url or transfers).`);
|
|
154
|
+
for (const cookie of vendor.cookies) {
|
|
155
|
+
if (cookie.category !== category && cookie.category !== "necessary") continue;
|
|
156
|
+
const known = entry.cookies.get(cookie.name);
|
|
157
|
+
if (known !== void 0 && !sameCookie(known, cookie)) throw new require_machine_names.ConsentError(`Vendor "${vendor.id}": cookie "${cookie.name}" is declared differently in two categories.`);
|
|
158
|
+
const clash = [...entry.cookies.values()].find((c) => c.name !== cookie.name && c.machine_name === cookie.machine_name);
|
|
159
|
+
if (clash) throw new require_machine_names.ConsentError(`Vendor "${vendor.id}": cookies "${clash.name}" and "${cookie.name}" have the same machine name "${cookie.machine_name}".`);
|
|
160
|
+
entry.cookies.set(cookie.name, cookie);
|
|
161
|
+
}
|
|
124
162
|
merged.set(vendor.id, entry);
|
|
125
163
|
}
|
|
126
164
|
}
|
|
127
|
-
|
|
128
|
-
|
|
165
|
+
const vendors = [...merged.values()].map(({ vendor, cookies }) => {
|
|
166
|
+
const order = vendor.cookies.map((c) => c.name).filter((name) => cookies.has(name));
|
|
167
|
+
const ordered = new Set(order);
|
|
168
|
+
for (const name of cookies.keys()) if (!ordered.has(name)) order.push(name);
|
|
169
|
+
return {
|
|
129
170
|
...vendor,
|
|
130
|
-
cookies:
|
|
131
|
-
}
|
|
171
|
+
cookies: order.flatMap((name) => cookies.get(name) ?? [])
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
rejectSharedMachineNames(vendors);
|
|
175
|
+
return {
|
|
176
|
+
vendors,
|
|
132
177
|
listed
|
|
133
178
|
};
|
|
134
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Every vendor and cookie key must name one thing: `google` + cookie `analytics` comes out
|
|
182
|
+
* `google_analytics`, the key of the Google Analytics vendor — both would render the same text.
|
|
183
|
+
* (Within one vendor, and between vendor ids, this is already checked as they're declared.)
|
|
184
|
+
*/
|
|
185
|
+
function rejectSharedMachineNames(vendors) {
|
|
186
|
+
const owners = /* @__PURE__ */ new Map();
|
|
187
|
+
const claim = (key, owner) => {
|
|
188
|
+
const other = owners.get(key);
|
|
189
|
+
if (other !== void 0 && other !== owner) {
|
|
190
|
+
const first = other.charAt(0).toUpperCase() + other.slice(1);
|
|
191
|
+
throw new require_machine_names.ConsentError(`${first} and ${owner} have the same machine name "${key}".`);
|
|
192
|
+
}
|
|
193
|
+
owners.set(key, owner);
|
|
194
|
+
};
|
|
195
|
+
for (const vendor of vendors) {
|
|
196
|
+
claim(vendor.machine_name, `vendor "${vendor.id}"`);
|
|
197
|
+
for (const cookie of vendor.cookies) claim(cookie.machine_name, `vendor "${vendor.id}" cookie "${cookie.name}"`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function sameVendorInfo(a, b) {
|
|
201
|
+
return a.privacy_policy_url === b.privacy_policy_url && transfersKey(a.transfers) === transfersKey(b.transfers);
|
|
202
|
+
}
|
|
203
|
+
/** Transfers in a canonical form: the same countries and safeguards in any order compare equal. */
|
|
204
|
+
function transfersKey(transfers) {
|
|
205
|
+
if (transfers === void 0) return "none";
|
|
206
|
+
return transfers.map((t) => `${t.country}:${[...t.safeguards].sort().join(",")}`).sort().join(";");
|
|
207
|
+
}
|
|
208
|
+
function sameCookie(a, b) {
|
|
209
|
+
return a.category === b.category && (a.type ?? "http") === (b.type ?? "http") && a.max_age === b.max_age && a.domain === b.domain;
|
|
210
|
+
}
|
|
135
211
|
function reusableId(record) {
|
|
136
212
|
const id = record?.consent_id;
|
|
137
213
|
return typeof id === "string" && id !== "" ? id : null;
|
|
@@ -169,11 +245,13 @@ function randomId() {
|
|
|
169
245
|
function createConsent(config = {}) {
|
|
170
246
|
const { vendors, listed } = resolveVendors(config.categories ?? {});
|
|
171
247
|
const byId = new Map(vendors.map((v) => [v.id, v]));
|
|
172
|
-
const categories =
|
|
248
|
+
const categories = require_machine_names.CONSENT_CATEGORIES.map((id) => {
|
|
173
249
|
const ids = [.../* @__PURE__ */ new Set([...listed[id], ...vendors.map((v) => v.id)])];
|
|
174
250
|
return {
|
|
175
251
|
id,
|
|
176
|
-
|
|
252
|
+
machine_name: require_machine_names.categoryMachineName(id),
|
|
253
|
+
description_machine_name: `${require_machine_names.categoryMachineName(id)}_description`,
|
|
254
|
+
required: id === require_machine_names.REQUIRED_CATEGORY,
|
|
177
255
|
vendors: ids.flatMap((vendorId) => {
|
|
178
256
|
const vendor = byId.get(vendorId);
|
|
179
257
|
const cookies = vendor.cookies.filter((c) => c.category === id);
|
|
@@ -186,24 +264,33 @@ function createConsent(config = {}) {
|
|
|
186
264
|
});
|
|
187
265
|
const usedIn = require_used_vendors.usedVendors(categories);
|
|
188
266
|
const maxAge = config.maxAge ?? 31536e6;
|
|
189
|
-
if (typeof maxAge !== "number" || Number.isNaN(maxAge) || maxAge <= 0) throw new
|
|
190
|
-
const
|
|
267
|
+
if (typeof maxAge !== "number" || Number.isNaN(maxAge) || maxAge <= 0) throw new require_machine_names.ConsentError("maxAge must be a positive number of milliseconds.");
|
|
268
|
+
const rawVersion = config.policyVersion ?? 1;
|
|
269
|
+
if (!(typeof rawVersion === "string" && rawVersion !== "" || typeof rawVersion === "number" && Number.isFinite(rawVersion))) throw new require_machine_names.ConsentError("policyVersion must be a non-empty string or a finite number.");
|
|
270
|
+
const policyVersion = String(rawVersion);
|
|
191
271
|
const store = config.store ?? (0, _luwiostack_storage.localStore)("luwio.consent", { onError: "ignore" });
|
|
192
272
|
const now = config.now ?? Date.now;
|
|
193
273
|
let unsaved;
|
|
274
|
+
let unsavedOver = null;
|
|
194
275
|
const listeners = /* @__PURE__ */ new Set();
|
|
195
276
|
const notify = () => {
|
|
196
277
|
for (const fn of [...listeners]) fn();
|
|
197
278
|
};
|
|
198
|
-
const read = () =>
|
|
279
|
+
const read = () => {
|
|
280
|
+
const stored = store.read();
|
|
281
|
+
if (unsaved === void 0) return stored;
|
|
282
|
+
if (stored === unsavedOver) return unsaved;
|
|
283
|
+
unsaved = void 0;
|
|
284
|
+
return stored;
|
|
285
|
+
};
|
|
199
286
|
let lastRecord;
|
|
200
287
|
let lastExpired = false;
|
|
201
288
|
let lastSnapshot = PENDING;
|
|
202
289
|
function toDecision(record) {
|
|
203
290
|
if (record.policy_version !== policyVersion) return null;
|
|
204
291
|
if (!Array.isArray(record.granted)) return null;
|
|
205
|
-
const granted = /* @__PURE__ */ new Set([
|
|
206
|
-
for (const id of record.granted) if (
|
|
292
|
+
const granted = /* @__PURE__ */ new Set([require_machine_names.REQUIRED_CATEGORY]);
|
|
293
|
+
for (const id of record.granted) if (require_machine_names.isConsentCategoryId(id)) granted.add(id);
|
|
207
294
|
return ConsentDecision.tryNew({
|
|
208
295
|
...record,
|
|
209
296
|
granted
|
|
@@ -228,41 +315,52 @@ function createConsent(config = {}) {
|
|
|
228
315
|
}
|
|
229
316
|
function commit(granted) {
|
|
230
317
|
const previous = current();
|
|
231
|
-
const set = /* @__PURE__ */ new Set([
|
|
232
|
-
for (const id of granted) if (
|
|
318
|
+
const set = /* @__PURE__ */ new Set([require_machine_names.REQUIRED_CATEGORY]);
|
|
319
|
+
for (const id of granted) if (require_machine_names.isConsentCategoryId(id)) set.add(id);
|
|
233
320
|
const next = ConsentDecision.new({
|
|
234
321
|
consent_id: previous?.consent_id ?? reusableId(read()) ?? randomId(),
|
|
235
322
|
policy_version: policyVersion,
|
|
236
|
-
granted:
|
|
323
|
+
granted: require_machine_names.CONSENT_CATEGORIES.filter((id) => set.has(id)),
|
|
237
324
|
decided_at: now()
|
|
238
325
|
});
|
|
239
326
|
const record = next.toJSON();
|
|
240
|
-
store.
|
|
327
|
+
const before = store.read();
|
|
328
|
+
let announced = false;
|
|
329
|
+
const stopWatching = store.subscribe(() => {
|
|
330
|
+
announced = true;
|
|
331
|
+
});
|
|
332
|
+
try {
|
|
333
|
+
store.write(record);
|
|
334
|
+
} finally {
|
|
335
|
+
stopWatching();
|
|
336
|
+
}
|
|
241
337
|
const kept = store.read();
|
|
242
|
-
const landed =
|
|
338
|
+
const landed = announced || kept !== before;
|
|
243
339
|
unsaved = landed ? void 0 : record;
|
|
340
|
+
unsavedOver = kept;
|
|
244
341
|
if (!landed) notify();
|
|
245
342
|
config.onChange?.(next, previous);
|
|
246
343
|
return next;
|
|
247
344
|
}
|
|
248
|
-
const isRequired = (id) => id ===
|
|
249
|
-
const isGranted = (id, vendorId) => (vendorId === void 0 || usedIn[id]
|
|
345
|
+
const isRequired = (id) => id === require_machine_names.REQUIRED_CATEGORY;
|
|
346
|
+
const isGranted = (id, vendorId) => (vendorId === void 0 || usedIn[id]?.has(vendorId) === true) && (isRequired(id) || (current()?.isGranted(id) ?? false));
|
|
250
347
|
return {
|
|
251
348
|
categories,
|
|
252
349
|
vendors,
|
|
253
350
|
policyVersion,
|
|
254
351
|
getSnapshot,
|
|
255
352
|
subscribe: (onChange) => {
|
|
256
|
-
|
|
257
|
-
|
|
353
|
+
const listener = () => onChange();
|
|
354
|
+
listeners.add(listener);
|
|
355
|
+
const unsubscribe = store.subscribe(listener);
|
|
258
356
|
return () => {
|
|
259
|
-
listeners.delete(
|
|
357
|
+
listeners.delete(listener);
|
|
260
358
|
unsubscribe();
|
|
261
359
|
};
|
|
262
360
|
},
|
|
263
361
|
isGranted,
|
|
264
362
|
isRequired,
|
|
265
|
-
acceptAll: () => commit(
|
|
363
|
+
acceptAll: () => commit(require_machine_names.CONSENT_CATEGORIES),
|
|
266
364
|
rejectAll: () => commit([]),
|
|
267
365
|
save: (granted) => commit(granted),
|
|
268
366
|
grant: (...grant) => commit([...current()?.granted() ?? [], ...grant]),
|
|
@@ -300,8 +398,8 @@ var ConsentCookieFormatter = class {
|
|
|
300
398
|
/**
|
|
301
399
|
* A cookie's lifetime in `locale`, in the largest unit that divides it evenly: 2 years, 3 months,
|
|
302
400
|
* 400 days, 30 minutes — falling back to whole days (or the largest unit that fits) when nothing
|
|
303
|
-
* divides evenly. Returns `null` for `'session'` and `'persistent'`: those are words, so
|
|
304
|
-
* them
|
|
401
|
+
* divides evenly. Returns `null` for `'session'` and `'persistent'`: those are words, so translate
|
|
402
|
+
* them — `formatLocale(cookie, locale) ?? t(cookie.max_age_machine_name)`.
|
|
305
403
|
*/
|
|
306
404
|
static formatLocale(cookie, locale, options = {}) {
|
|
307
405
|
const { max_age } = cookie;
|
|
@@ -338,19 +436,36 @@ function domainCandidates(hostname) {
|
|
|
338
436
|
for (let i = 0; i < parts.length - 1; i++) parents.push(parts.slice(i).join("."));
|
|
339
437
|
return [void 0, ...parents];
|
|
340
438
|
}
|
|
341
|
-
/**
|
|
439
|
+
/**
|
|
440
|
+
* The paths a cookie may have been set for, from the root down to this page — each parent with and
|
|
441
|
+
* without a trailing `/` (`Path=/a/` is sent on `/a/b` too): `/a/b` → `/`, `/a`, `/a/`, `/a/b`. The
|
|
442
|
+
* page's own path gets its `/` variant only when the URL has one.
|
|
443
|
+
* @internal
|
|
444
|
+
*/
|
|
342
445
|
function pathCandidates(pathname) {
|
|
343
446
|
const segments = pathname.split("/").filter(Boolean);
|
|
344
447
|
const paths = ["/"];
|
|
345
|
-
for (let i = 1; i <= segments.length; i++)
|
|
448
|
+
for (let i = 1; i <= segments.length; i++) {
|
|
449
|
+
const path = `/${segments.slice(0, i).join("/")}`;
|
|
450
|
+
paths.push(path);
|
|
451
|
+
if (i < segments.length || pathname.endsWith("/")) paths.push(`${path}/`);
|
|
452
|
+
}
|
|
346
453
|
return paths;
|
|
347
454
|
}
|
|
348
455
|
function cookieNames(doc) {
|
|
349
456
|
return doc.cookie.split(";").map((pair) => pair.slice(0, pair.indexOf("=") === -1 ? void 0 : pair.indexOf("=")).trim()).filter(Boolean);
|
|
350
457
|
}
|
|
351
458
|
function expireCookie(doc, name) {
|
|
352
|
-
const { hostname, pathname } = doc.location;
|
|
353
|
-
|
|
459
|
+
const { hostname, pathname, protocol } = doc.location;
|
|
460
|
+
const variants = !(doc.defaultView?.isSecureContext ?? protocol === "https:") ? [""] : protocol === "https:" ? ["; Secure"] : ["", "; Secure"];
|
|
461
|
+
const expired = "Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT";
|
|
462
|
+
for (const secure of variants) {
|
|
463
|
+
if (name.startsWith("__Host-")) {
|
|
464
|
+
doc.cookie = `${name}=; Path=/${secure}; ${expired}`;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
for (const domain of domainCandidates(hostname)) for (const path of pathCandidates(pathname)) doc.cookie = `${name}=; Path=${path}${domain ? `; Domain=${domain}` : ""}${secure}; ${expired}`;
|
|
468
|
+
}
|
|
354
469
|
}
|
|
355
470
|
function storageKeys(storage) {
|
|
356
471
|
const keys = [];
|
|
@@ -368,8 +483,9 @@ function storageKeys(storage) {
|
|
|
368
483
|
*
|
|
369
484
|
* Only **first-party** storage can be reached: cookies on your own domain (tried with every parent
|
|
370
485
|
* domain and path, since tags set them broadly), `localStorage` / `sessionStorage` items and
|
|
371
|
-
* IndexedDB databases. A third-party cookie (
|
|
372
|
-
*
|
|
486
|
+
* IndexedDB databases. A third-party cookie (declared with a `domain`, like `IDE` on
|
|
487
|
+
* doubleclick.net) lives on another site: it's skipped, so a first-party item that shares its name
|
|
488
|
+
* is never removed. `HttpOnly` cookies can only be removed by your server. Scripts already running keep
|
|
373
489
|
* running — reload the page (e.g. from `onClear`) to stop them.
|
|
374
490
|
*
|
|
375
491
|
* Returns a stop function. A no-op on the server.
|
|
@@ -382,7 +498,7 @@ function clearRevokedCookies(consent, options = {}) {
|
|
|
382
498
|
for (const category of consent.categories) {
|
|
383
499
|
const granted = category.id === "necessary" || consent.isGranted(category.id);
|
|
384
500
|
for (const vendor of category.vendors) for (const cookie of vendor.cookies) if (granted) kept.push(matcher(cookie.name));
|
|
385
|
-
else revoked.push(cookie);
|
|
501
|
+
else if (cookie.domain === void 0) revoked.push(cookie);
|
|
386
502
|
}
|
|
387
503
|
if (revoked.length === 0) return;
|
|
388
504
|
const isKept = (actual) => kept.some((matches) => matches(actual));
|
|
@@ -414,7 +530,7 @@ function clearRevokedCookies(consent, options = {}) {
|
|
|
414
530
|
indexedDB.deleteDatabase(name);
|
|
415
531
|
cleared(cookie, name);
|
|
416
532
|
}
|
|
417
|
-
});
|
|
533
|
+
}).catch(() => {});
|
|
418
534
|
else if (!cookie.name.includes("*") && !isKept(cookie.name)) indexedDB.deleteDatabase(cookie.name);
|
|
419
535
|
}
|
|
420
536
|
}
|
|
@@ -423,66 +539,32 @@ function clearRevokedCookies(consent, options = {}) {
|
|
|
423
539
|
return consent.subscribe(clear);
|
|
424
540
|
}
|
|
425
541
|
//#endregion
|
|
426
|
-
//#region src/integrations/
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
};
|
|
437
|
-
function resolveGtag() {
|
|
438
|
-
if (typeof window === "undefined") return void 0;
|
|
439
|
-
const w = window;
|
|
440
|
-
if (typeof w.gtag === "function") return w.gtag;
|
|
441
|
-
w.dataLayer = w.dataLayer ?? [];
|
|
442
|
-
const dataLayer = w.dataLayer;
|
|
443
|
-
w.gtag = function gtag() {
|
|
444
|
-
dataLayer.push(arguments);
|
|
445
|
-
};
|
|
446
|
-
return w.gtag;
|
|
542
|
+
//#region src/integrations/unblock-scripts.ts
|
|
543
|
+
const loading = /* @__PURE__ */ new Set();
|
|
544
|
+
/** A script still loading in `doc` (one taken out of the page holds nothing up, and is forgotten). */
|
|
545
|
+
function loadingIn(doc) {
|
|
546
|
+
for (const script of loading) if (!script.isConnected) loading.delete(script);
|
|
547
|
+
else if (script.ownerDocument === doc) return script;
|
|
548
|
+
}
|
|
549
|
+
/** Call `fn` once when `script` has loaded or failed. */
|
|
550
|
+
function onSettled(script, fn) {
|
|
551
|
+
script.addEventListener("load", fn, { once: true });
|
|
552
|
+
script.addEventListener("error", fn, { once: true });
|
|
447
553
|
}
|
|
554
|
+
const JS_TYPE = /^(?:(?:application|text)\/(?:x-)?(?:java|ecma)script|text\/(?:jscript|livescript|javascript1\.[0-5]))$/i;
|
|
448
555
|
/**
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
*
|
|
452
|
-
*
|
|
453
|
-
* Call it **before** the Google tag (gtag.js / GTM) loads, so the default is in place for its first
|
|
454
|
-
* hit. Returns an unsubscribe. A no-op on the server.
|
|
556
|
+
* Whether an external script holds up the inline ones after it, and runs in order with the external
|
|
557
|
+
* ones (`async = false`): the browser fires `load` or `error` for it.
|
|
558
|
+
* An `async` one doesn't wait; one the browser won't run (a `nomodule` script, a type that isn't
|
|
559
|
+
* JavaScript) fires neither event, and waiting on it would block everything after it for good.
|
|
455
560
|
*/
|
|
456
|
-
function
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
for (const [signal, cat] of entries) out[signal] = (typeof cat === "string" ? [cat] : cat).every(isGranted) ? "granted" : "denied";
|
|
463
|
-
return out;
|
|
464
|
-
};
|
|
465
|
-
if (options.adsDataRedaction) gtag("set", "ads_data_redaction", true);
|
|
466
|
-
if (options.urlPassthrough) gtag("set", "url_passthrough", true);
|
|
467
|
-
gtag("consent", "default", {
|
|
468
|
-
...signals((id) => consent.isRequired(id)),
|
|
469
|
-
wait_for_update: options.waitForUpdate ?? 500,
|
|
470
|
-
...options.region ? { region: [...options.region] } : {}
|
|
471
|
-
});
|
|
472
|
-
let last = "";
|
|
473
|
-
const sync = () => {
|
|
474
|
-
if (consent.getSnapshot().status !== "decided" && last === "") return;
|
|
475
|
-
const next = signals((id) => consent.isGranted(id));
|
|
476
|
-
const key = JSON.stringify(next);
|
|
477
|
-
if (key === last) return;
|
|
478
|
-
last = key;
|
|
479
|
-
gtag("consent", "update", next);
|
|
480
|
-
};
|
|
481
|
-
sync();
|
|
482
|
-
return consent.subscribe(sync);
|
|
561
|
+
function holdsUpOthers(el, type) {
|
|
562
|
+
if (el.hasAttribute("async")) return false;
|
|
563
|
+
const t = (type ?? "").trim();
|
|
564
|
+
if (t.toLowerCase() === "module") return true;
|
|
565
|
+
if (t !== "" && !JS_TYPE.test(t)) return false;
|
|
566
|
+
return !el.hasAttribute("nomodule");
|
|
483
567
|
}
|
|
484
|
-
//#endregion
|
|
485
|
-
//#region src/integrations/unblock-scripts.ts
|
|
486
568
|
/**
|
|
487
569
|
* Hold third-party scripts and embeds back until their category is granted — mark them up blocked and
|
|
488
570
|
* this activates them the moment consent allows (on load, and on every later change):
|
|
@@ -500,7 +582,9 @@ function googleConsentMode(consent, options = {}) {
|
|
|
500
582
|
* ```
|
|
501
583
|
*
|
|
502
584
|
* A script is replaced by a fresh `<script>` copy (the only way to make the browser run it), its
|
|
503
|
-
* CSP `nonce` included
|
|
585
|
+
* CSP `nonce` included. Scripts run in document order, as the parser would run them: consecutive
|
|
586
|
+
* external ones download in parallel and run in order, and an inline one waits until every external script above it
|
|
587
|
+
* has loaded (or failed). An `async` script holds nothing up. An embed gets its `src` from `data-src`. Activation is one-way: a revoked category can't un-run a
|
|
504
588
|
* script — reload the page from `onChange` when something is withdrawn. Returns a stop function.
|
|
505
589
|
*/
|
|
506
590
|
function unblockScripts(consent, options = {}) {
|
|
@@ -513,21 +597,66 @@ function unblockScripts(consent, options = {}) {
|
|
|
513
597
|
const vendor = el.getAttribute(vendorAttr) ?? void 0;
|
|
514
598
|
return ids.length > 0 && ids.every((id) => consent.isGranted(id, vendor));
|
|
515
599
|
};
|
|
600
|
+
const queue = [];
|
|
601
|
+
const queued = /* @__PURE__ */ new WeakSet();
|
|
602
|
+
let waitingOn = null;
|
|
603
|
+
let stopped = false;
|
|
604
|
+
const insert = (el) => {
|
|
605
|
+
const script = document.createElement("script");
|
|
606
|
+
for (const { name, value } of Array.from(el.attributes)) {
|
|
607
|
+
if (name === "type" || name === "data-type") continue;
|
|
608
|
+
script.setAttribute(name, value);
|
|
609
|
+
}
|
|
610
|
+
const type = el.getAttribute("data-type");
|
|
611
|
+
if (type) script.setAttribute("type", type);
|
|
612
|
+
const { nonce } = el;
|
|
613
|
+
if (nonce) script.nonce = nonce;
|
|
614
|
+
if (!el.hasAttribute("src")) script.text = el.textContent ?? "";
|
|
615
|
+
else if (holdsUpOthers(el, type)) {
|
|
616
|
+
script.async = false;
|
|
617
|
+
loading.add(script);
|
|
618
|
+
onSettled(script, () => loading.delete(script));
|
|
619
|
+
}
|
|
620
|
+
el.replaceWith(script);
|
|
621
|
+
};
|
|
622
|
+
const waitFor = (script) => {
|
|
623
|
+
waitingOn = script;
|
|
624
|
+
onSettled(script, () => {
|
|
625
|
+
if (waitingOn !== script) return;
|
|
626
|
+
waitingOn = null;
|
|
627
|
+
drain();
|
|
628
|
+
});
|
|
629
|
+
};
|
|
630
|
+
/** A script waited on but taken out of the page never settles: stop waiting, it holds nothing up. */
|
|
631
|
+
const dropRemovedBlocker = () => {
|
|
632
|
+
if (waitingOn?.isConnected !== false) return;
|
|
633
|
+
waitingOn = null;
|
|
634
|
+
drain();
|
|
635
|
+
};
|
|
636
|
+
const drain = () => {
|
|
637
|
+
while (waitingOn === null && !stopped && queue.length > 0) {
|
|
638
|
+
const el = queue[0];
|
|
639
|
+
if (!el.isConnected || !allowed(el)) {
|
|
640
|
+
queue.shift();
|
|
641
|
+
queued.delete(el);
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
if (!el.hasAttribute("src")) {
|
|
645
|
+
const blocker = loadingIn(el.ownerDocument);
|
|
646
|
+
if (blocker) return waitFor(blocker);
|
|
647
|
+
}
|
|
648
|
+
queue.shift();
|
|
649
|
+
insert(el);
|
|
650
|
+
}
|
|
651
|
+
};
|
|
516
652
|
const activate = () => {
|
|
653
|
+
dropRemovedBlocker();
|
|
517
654
|
for (const el of Array.from(root.querySelectorAll(`script[type="text/plain"][${attr}]`))) {
|
|
518
|
-
if (!allowed(el)) continue;
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
if (name === "type" || name === "data-type") continue;
|
|
522
|
-
script.setAttribute(name, value);
|
|
523
|
-
}
|
|
524
|
-
const type = el.getAttribute("data-type");
|
|
525
|
-
if (type) script.setAttribute("type", type);
|
|
526
|
-
const { nonce } = el;
|
|
527
|
-
if (nonce) script.nonce = nonce;
|
|
528
|
-
if (!el.hasAttribute("src")) script.text = el.textContent ?? "";
|
|
529
|
-
el.replaceWith(script);
|
|
655
|
+
if (queued.has(el) || !allowed(el)) continue;
|
|
656
|
+
queued.add(el);
|
|
657
|
+
queue.push(el);
|
|
530
658
|
}
|
|
659
|
+
drain();
|
|
531
660
|
for (const el of Array.from(root.querySelectorAll(`[${attr}][data-src]:not(script)`))) {
|
|
532
661
|
if (!allowed(el)) continue;
|
|
533
662
|
const src = el.getAttribute("data-src");
|
|
@@ -541,6 +670,7 @@ function unblockScripts(consent, options = {}) {
|
|
|
541
670
|
if ((options.observe ?? true) && typeof MutationObserver !== "undefined") {
|
|
542
671
|
observer = new MutationObserver((records) => {
|
|
543
672
|
if (records.some((r) => r.addedNodes.length > 0)) activate();
|
|
673
|
+
else dropRemovedBlocker();
|
|
544
674
|
});
|
|
545
675
|
const target = root instanceof Document ? root.documentElement : root;
|
|
546
676
|
observer.observe(target, {
|
|
@@ -549,23 +679,22 @@ function unblockScripts(consent, options = {}) {
|
|
|
549
679
|
});
|
|
550
680
|
}
|
|
551
681
|
return () => {
|
|
682
|
+
stopped = true;
|
|
552
683
|
unsubscribe();
|
|
553
684
|
observer?.disconnect();
|
|
554
685
|
};
|
|
555
686
|
}
|
|
556
687
|
//#endregion
|
|
557
|
-
exports.CONSENT_CATEGORIES =
|
|
688
|
+
exports.CONSENT_CATEGORIES = require_machine_names.CONSENT_CATEGORIES;
|
|
558
689
|
exports.ConsentCookieFormatter = ConsentCookieFormatter;
|
|
559
690
|
exports.ConsentDecision = ConsentDecision;
|
|
560
|
-
exports.ConsentError =
|
|
561
|
-
exports.DEFAULT_GOOGLE_CONSENT_MAPPING = DEFAULT_GOOGLE_CONSENT_MAPPING;
|
|
691
|
+
exports.ConsentError = require_machine_names.ConsentError;
|
|
562
692
|
exports.DEFAULT_MAX_AGE = DEFAULT_MAX_AGE;
|
|
563
693
|
exports.DEFAULT_STORAGE_KEY = DEFAULT_STORAGE_KEY;
|
|
564
|
-
exports.REQUIRED_CATEGORY =
|
|
694
|
+
exports.REQUIRED_CATEGORY = require_machine_names.REQUIRED_CATEGORY;
|
|
565
695
|
exports.clearRevokedCookies = clearRevokedCookies;
|
|
566
696
|
exports.createConsent = createConsent;
|
|
567
|
-
exports.
|
|
568
|
-
exports.isConsentCategoryId = require_errors.isConsentCategoryId;
|
|
697
|
+
exports.isConsentCategoryId = require_machine_names.isConsentCategoryId;
|
|
569
698
|
exports.unblockScripts = unblockScripts;
|
|
570
699
|
|
|
571
700
|
//# sourceMappingURL=index.cjs.map
|