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