@rcooler/consent 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +56 -2
- package/dist/consent.css +1 -0
- package/dist/headless.d.ts +18 -0
- package/dist/headless.js +734 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/initialize.d.ts +13 -0
- package/dist/initialize.js +44 -0
- package/dist/integrations.d.ts +19 -0
- package/dist/integrations.js +233 -0
- package/dist/network.d.ts +3 -0
- package/dist/network.js +58 -0
- package/dist/regional.d.ts +14 -0
- package/dist/regional.js +73 -0
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.js +19 -0
- package/dist/types.d.ts +132 -0
- package/dist/types.js +11 -0
- package/dist/ui.d.ts +51 -0
- package/dist/ui.js +348 -0
- package/dist/validation.d.ts +10 -0
- package/dist/validation.js +318 -0
- package/package.json +34 -4
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ConsentOptions } from "./headless.js";
|
|
2
|
+
import type { ConsentController, PublicConfig } from "./types.js";
|
|
3
|
+
import type { ConsentUI, UIOptions } from "./ui.js";
|
|
4
|
+
export interface ConsentInstallation {
|
|
5
|
+
controller: ConsentController;
|
|
6
|
+
ui: ConsentUI;
|
|
7
|
+
destroy(): void;
|
|
8
|
+
}
|
|
9
|
+
export interface InitializeOptions extends Omit<ConsentOptions, "config">, UIOptions {
|
|
10
|
+
config?: PublicConfig;
|
|
11
|
+
}
|
|
12
|
+
/** One installation per document, even when multiple host template slots include the script. */
|
|
13
|
+
export declare function initializeConsent(options: InitializeOptions): Promise<ConsentInstallation>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createConsent } from "./headless.js";
|
|
2
|
+
import { registerGA4 } from "./integrations.js";
|
|
3
|
+
import { fetchJSON, sameOriginURL } from "./network.js";
|
|
4
|
+
import { mountConsent } from "./ui.js";
|
|
5
|
+
import { validateConfig } from "./validation.js";
|
|
6
|
+
let installation;
|
|
7
|
+
/** One installation per document, even when multiple host template slots include the script. */
|
|
8
|
+
export function initializeConsent(options) {
|
|
9
|
+
if (installation)
|
|
10
|
+
return installation;
|
|
11
|
+
installation = (async () => {
|
|
12
|
+
const { config: inline, ...rest } = options;
|
|
13
|
+
let config = inline;
|
|
14
|
+
let configURL = options.configURL;
|
|
15
|
+
if (configURL) {
|
|
16
|
+
const url = sameOriginURL(configURL, options.origin ?? location.origin);
|
|
17
|
+
url.searchParams.set("lang", options.locale ?? (document.documentElement.lang || "en"));
|
|
18
|
+
url.searchParams.set("path", location.pathname);
|
|
19
|
+
configURL = url.href;
|
|
20
|
+
config = await fetchJSON(url, {}, options.fetcher);
|
|
21
|
+
}
|
|
22
|
+
if (!validateConfig(config))
|
|
23
|
+
throw new Error("Invalid consent configuration");
|
|
24
|
+
const controller = createConsent({ ...rest, ...(configURL ? { configURL } : {}), config });
|
|
25
|
+
const ui = mountConsent(controller, options);
|
|
26
|
+
const cleanups = config.policy.services
|
|
27
|
+
.filter((service) => service.kind === "ga4")
|
|
28
|
+
.map((service) => registerGA4(controller, service.id));
|
|
29
|
+
return {
|
|
30
|
+
controller,
|
|
31
|
+
ui,
|
|
32
|
+
destroy() {
|
|
33
|
+
cleanups.forEach((cleanup) => cleanup());
|
|
34
|
+
ui.destroy();
|
|
35
|
+
controller.destroy();
|
|
36
|
+
installation = undefined;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
})().catch((error) => {
|
|
40
|
+
installation = undefined;
|
|
41
|
+
throw error;
|
|
42
|
+
});
|
|
43
|
+
return installation;
|
|
44
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ConsentController, ServiceAdapter } from "./types.js";
|
|
2
|
+
import type { ConsentUI } from "./ui.js";
|
|
3
|
+
/** Explicit deferred scripts must never also appear as active script tags in the host HTML. */
|
|
4
|
+
export declare function deferredScript(source: string, options?: {
|
|
5
|
+
nonce?: string;
|
|
6
|
+
reloadOnRevoke?: boolean;
|
|
7
|
+
}): ServiceAdapter;
|
|
8
|
+
export interface IframeOptions {
|
|
9
|
+
source: string;
|
|
10
|
+
title: string;
|
|
11
|
+
message?: string;
|
|
12
|
+
buttonLabel?: string;
|
|
13
|
+
sandbox?: string;
|
|
14
|
+
ui: ConsentUI;
|
|
15
|
+
}
|
|
16
|
+
/** A placeholder is inert until both category permission and host page eligibility permit its service. */
|
|
17
|
+
export declare function mountIframePlaceholder(controller: ConsentController, serviceID: string, container: HTMLElement, options: IframeOptions): () => void;
|
|
18
|
+
/** Strict basic consent mode: no Google load, preconnect, queued event or page URL before permission. */
|
|
19
|
+
export declare function registerGA4(controller: ConsentController, serviceID: string): () => void;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { safeURL } from "./validation.js";
|
|
2
|
+
function matchesDestination(context, source) {
|
|
3
|
+
return Boolean(context.service.destination &&
|
|
4
|
+
new URL(context.service.destination).href === new URL(source).href);
|
|
5
|
+
}
|
|
6
|
+
/** Explicit deferred scripts must never also appear as active script tags in the host HTML. */
|
|
7
|
+
export function deferredScript(source, options = {}) {
|
|
8
|
+
if (!safeURL(source))
|
|
9
|
+
throw new Error("Deferred scripts require an HTTPS URL");
|
|
10
|
+
return {
|
|
11
|
+
reloadOnRevoke: options.reloadOnRevoke ?? true,
|
|
12
|
+
activate(context) {
|
|
13
|
+
if (!context.isAllowed() || !matchesDestination(context, source))
|
|
14
|
+
return;
|
|
15
|
+
const script = document.createElement("script");
|
|
16
|
+
script.async = true;
|
|
17
|
+
script.src = source;
|
|
18
|
+
script.referrerPolicy = "no-referrer";
|
|
19
|
+
if (options.nonce)
|
|
20
|
+
script.nonce = options.nonce;
|
|
21
|
+
context.signal.addEventListener("abort", () => script.remove(), { once: true });
|
|
22
|
+
if (!context.isAllowed())
|
|
23
|
+
return;
|
|
24
|
+
document.head.append(script);
|
|
25
|
+
return () => script.remove();
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const frameGroups = new WeakMap();
|
|
30
|
+
/** A placeholder is inert until both category permission and host page eligibility permit its service. */
|
|
31
|
+
export function mountIframePlaceholder(controller, serviceID, container, options) {
|
|
32
|
+
if (!safeURL(options.source))
|
|
33
|
+
throw new Error("Frames require an HTTPS URL");
|
|
34
|
+
const placeholder = document.createElement("div");
|
|
35
|
+
placeholder.className = "consent-placeholder";
|
|
36
|
+
const message = document.createElement("p");
|
|
37
|
+
message.textContent = options.message ?? "This content requires your permission.";
|
|
38
|
+
const button = document.createElement("button");
|
|
39
|
+
button.type = "button";
|
|
40
|
+
button.textContent = options.buttonLabel ?? "Privacy preferences";
|
|
41
|
+
button.addEventListener("click", () => options.ui.open(button));
|
|
42
|
+
placeholder.append(message, button);
|
|
43
|
+
container.append(placeholder);
|
|
44
|
+
const groups = frameGroups.get(controller) ?? new Map();
|
|
45
|
+
frameGroups.set(controller, groups);
|
|
46
|
+
let frame;
|
|
47
|
+
const mount = {
|
|
48
|
+
cleanup() {
|
|
49
|
+
frame?.remove();
|
|
50
|
+
frame = undefined;
|
|
51
|
+
placeholder.hidden = false;
|
|
52
|
+
},
|
|
53
|
+
activate(context) {
|
|
54
|
+
mount.cleanup();
|
|
55
|
+
if (context.service.kind !== "iframe" ||
|
|
56
|
+
!context.isAllowed() ||
|
|
57
|
+
!matchesDestination(context, options.source))
|
|
58
|
+
return;
|
|
59
|
+
frame = document.createElement("iframe");
|
|
60
|
+
frame.title = options.title;
|
|
61
|
+
frame.referrerPolicy = "no-referrer";
|
|
62
|
+
// A host can explicitly add capabilities, but the safe default grants none.
|
|
63
|
+
frame.setAttribute("sandbox", options.sandbox ?? "");
|
|
64
|
+
frame.src = options.source;
|
|
65
|
+
if (!context.isAllowed())
|
|
66
|
+
return;
|
|
67
|
+
placeholder.hidden = true;
|
|
68
|
+
container.append(frame);
|
|
69
|
+
context.signal.addEventListener("abort", () => mount.cleanup(), { once: true });
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
let group = groups.get(serviceID);
|
|
73
|
+
if (!group) {
|
|
74
|
+
group = {
|
|
75
|
+
mounts: new Set([mount]),
|
|
76
|
+
unregister() {
|
|
77
|
+
/* Replaced by the registration below. */
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
groups.set(serviceID, group);
|
|
81
|
+
const current = group;
|
|
82
|
+
try {
|
|
83
|
+
group.unregister = controller.register(serviceID, {
|
|
84
|
+
activate(context) {
|
|
85
|
+
current.context = context;
|
|
86
|
+
for (const item of current.mounts)
|
|
87
|
+
item.activate(context);
|
|
88
|
+
return () => {
|
|
89
|
+
if (current.context !== context)
|
|
90
|
+
return;
|
|
91
|
+
delete current.context;
|
|
92
|
+
for (const item of current.mounts)
|
|
93
|
+
item.cleanup();
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
groups.delete(serviceID);
|
|
100
|
+
placeholder.remove();
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
group.mounts.add(mount);
|
|
106
|
+
if (group.context && !group.context.signal.aborted)
|
|
107
|
+
mount.activate(group.context);
|
|
108
|
+
}
|
|
109
|
+
const current = group;
|
|
110
|
+
let disposed = false;
|
|
111
|
+
return () => {
|
|
112
|
+
// A repeated disposer must not forget a group which a later mount has created.
|
|
113
|
+
if (disposed)
|
|
114
|
+
return;
|
|
115
|
+
disposed = true;
|
|
116
|
+
mount.cleanup();
|
|
117
|
+
current.mounts.delete(mount);
|
|
118
|
+
if (current.mounts.size === 0) {
|
|
119
|
+
current.unregister();
|
|
120
|
+
groups.delete(serviceID);
|
|
121
|
+
}
|
|
122
|
+
placeholder.remove();
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
const denied = {
|
|
126
|
+
analytics_storage: "denied",
|
|
127
|
+
ad_storage: "denied",
|
|
128
|
+
ad_user_data: "denied",
|
|
129
|
+
ad_personalization: "denied",
|
|
130
|
+
};
|
|
131
|
+
/** Strict basic consent mode: no Google load, preconnect, queued event or page URL before permission. */
|
|
132
|
+
export function registerGA4(controller, serviceID) {
|
|
133
|
+
const service = controller.state.config.policy.services.find((item) => item.id === serviceID);
|
|
134
|
+
if (!service || service.kind !== "ga4")
|
|
135
|
+
throw new Error("GA4 adapter requires a declared GA4 service");
|
|
136
|
+
if (window.top !== window.self || location.search || location.hash)
|
|
137
|
+
return () => undefined;
|
|
138
|
+
const page = location.origin + location.pathname;
|
|
139
|
+
let invalidated = false;
|
|
140
|
+
let loaded = false;
|
|
141
|
+
let queue;
|
|
142
|
+
const eligible = () => !invalidated &&
|
|
143
|
+
location.origin + location.pathname === page &&
|
|
144
|
+
!location.search &&
|
|
145
|
+
!location.hash;
|
|
146
|
+
const invalidate = () => {
|
|
147
|
+
if (eligible())
|
|
148
|
+
return;
|
|
149
|
+
// With nothing loaded and an address which cannot be measured, such as after an in-page
|
|
150
|
+
// anchor, a reload would achieve nothing.
|
|
151
|
+
if (!loaded && (location.search || location.hash))
|
|
152
|
+
return;
|
|
153
|
+
invalidated = true;
|
|
154
|
+
queue?.("consent", "update", denied);
|
|
155
|
+
// Keep the original document URL contract even if a router changes location before opt-in.
|
|
156
|
+
location.reload();
|
|
157
|
+
};
|
|
158
|
+
// Preserve the original methods for restoration; the wrappers below apply their History receiver.
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
160
|
+
const originalPush = history.pushState;
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
162
|
+
const originalReplace = history.replaceState;
|
|
163
|
+
const push = function (...args) {
|
|
164
|
+
originalPush.apply(this, args);
|
|
165
|
+
invalidate();
|
|
166
|
+
};
|
|
167
|
+
const replace = function (...args) {
|
|
168
|
+
originalReplace.apply(this, args);
|
|
169
|
+
invalidate();
|
|
170
|
+
};
|
|
171
|
+
history.pushState = push;
|
|
172
|
+
history.replaceState = replace;
|
|
173
|
+
window.addEventListener("hashchange", invalidate);
|
|
174
|
+
window.addEventListener("popstate", invalidate);
|
|
175
|
+
const unregister = controller.register(serviceID, {
|
|
176
|
+
reloadOnRevoke: true,
|
|
177
|
+
async activate(context) {
|
|
178
|
+
if (loaded || !eligible() || !context.isAllowed())
|
|
179
|
+
return;
|
|
180
|
+
if (!(await controller.recheck()) || !eligible() || !context.isAllowed())
|
|
181
|
+
return;
|
|
182
|
+
const target = window;
|
|
183
|
+
if (Object.prototype.hasOwnProperty.call(target, "rcoolerConsentLayer"))
|
|
184
|
+
return;
|
|
185
|
+
target.rcoolerConsentLayer = [];
|
|
186
|
+
queue = function () {
|
|
187
|
+
// gtag.js runs commands pushed as Arguments objects only; it ignores plain arrays.
|
|
188
|
+
// eslint-disable-next-line prefer-rest-params
|
|
189
|
+
target.rcoolerConsentLayer?.push(arguments);
|
|
190
|
+
};
|
|
191
|
+
loaded = true;
|
|
192
|
+
queue("consent", "default", denied);
|
|
193
|
+
queue("consent", "update", { ...denied, analytics_storage: "granted" });
|
|
194
|
+
queue("js", new Date());
|
|
195
|
+
queue("config", context.service.measurement_id, {
|
|
196
|
+
send_page_view: false,
|
|
197
|
+
allow_google_signals: false,
|
|
198
|
+
allow_ad_personalization_signals: false,
|
|
199
|
+
page_location: page,
|
|
200
|
+
page_referrer: "",
|
|
201
|
+
});
|
|
202
|
+
queue("event", "page_view", {
|
|
203
|
+
send_to: context.service.measurement_id,
|
|
204
|
+
page_location: page,
|
|
205
|
+
page_referrer: "",
|
|
206
|
+
});
|
|
207
|
+
const script = document.createElement("script");
|
|
208
|
+
script.async = true;
|
|
209
|
+
script.referrerPolicy = "no-referrer";
|
|
210
|
+
script.src = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(context.service.measurement_id ?? "")}&l=rcoolerConsentLayer`;
|
|
211
|
+
const cleanup = () => {
|
|
212
|
+
queue?.("consent", "update", denied);
|
|
213
|
+
script.remove();
|
|
214
|
+
};
|
|
215
|
+
context.signal.addEventListener("abort", cleanup, { once: true });
|
|
216
|
+
if (!context.isAllowed() || !eligible()) {
|
|
217
|
+
cleanup();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
document.head.append(script);
|
|
221
|
+
return cleanup;
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
return () => {
|
|
225
|
+
unregister();
|
|
226
|
+
if (history.pushState === push)
|
|
227
|
+
history.pushState = originalPush;
|
|
228
|
+
if (history.replaceState === replace)
|
|
229
|
+
history.replaceState = originalReplace;
|
|
230
|
+
window.removeEventListener("hashchange", invalidate);
|
|
231
|
+
window.removeEventListener("popstate", invalidate);
|
|
232
|
+
};
|
|
233
|
+
}
|
package/dist/network.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** A configuration wraps a policy of up to 256 KiB in a small envelope. */
|
|
2
|
+
const maxResponseBytes = (256 + 16) * 1024;
|
|
3
|
+
/** Public consent endpoints never send ambient cookies or follow redirects. */
|
|
4
|
+
export function sameOriginURL(source, origin = location.origin) {
|
|
5
|
+
const url = new URL(source, origin);
|
|
6
|
+
if (url.origin !== origin ||
|
|
7
|
+
url.username ||
|
|
8
|
+
url.password ||
|
|
9
|
+
url.hash ||
|
|
10
|
+
!["https:", "http:"].includes(url.protocol)) {
|
|
11
|
+
throw new Error("Consent endpoint must be same-origin");
|
|
12
|
+
}
|
|
13
|
+
return url;
|
|
14
|
+
}
|
|
15
|
+
export async function fetchJSON(url, init = {}, fetcher = fetch) {
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
const timeout = setTimeout(() => controller.abort(), 4000);
|
|
18
|
+
try {
|
|
19
|
+
const response = await fetcher(url.href, {
|
|
20
|
+
...init,
|
|
21
|
+
credentials: "omit",
|
|
22
|
+
cache: "no-store",
|
|
23
|
+
redirect: "error",
|
|
24
|
+
signal: controller.signal,
|
|
25
|
+
headers: { Accept: "application/json", ...init.headers },
|
|
26
|
+
});
|
|
27
|
+
if (!response.ok ||
|
|
28
|
+
response.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase() !==
|
|
29
|
+
"application/json" ||
|
|
30
|
+
!response.body) {
|
|
31
|
+
throw new Error("Consent endpoint unavailable");
|
|
32
|
+
}
|
|
33
|
+
const reader = response.body.getReader();
|
|
34
|
+
const chunks = [];
|
|
35
|
+
let length = 0;
|
|
36
|
+
while (true) {
|
|
37
|
+
const chunk = await reader.read();
|
|
38
|
+
if (chunk.done)
|
|
39
|
+
break;
|
|
40
|
+
length += chunk.value.length;
|
|
41
|
+
if (length > maxResponseBytes) {
|
|
42
|
+
await reader.cancel();
|
|
43
|
+
throw new Error("Consent response too large");
|
|
44
|
+
}
|
|
45
|
+
chunks.push(chunk.value);
|
|
46
|
+
}
|
|
47
|
+
const bytes = new Uint8Array(length);
|
|
48
|
+
let offset = 0;
|
|
49
|
+
for (const chunk of chunks) {
|
|
50
|
+
bytes.set(chunk, offset);
|
|
51
|
+
offset += chunk.length;
|
|
52
|
+
}
|
|
53
|
+
return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
clearTimeout(timeout);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Notice, PrivacyRestrictions, PublicConfig, RightsLinks, Service } from "./types.js";
|
|
2
|
+
export declare const regionalRulesVersion = "2026-12-03";
|
|
3
|
+
/** Location identifiers, not a claim that every jurisdiction has the same law. */
|
|
4
|
+
export declare const regionalProfiles: readonly ["global-strict", "eea", "uk", "ch", "br", "ca", "ca-qc", "us", ...string[], "au", "nz", "jp", "kr", "za", "in"];
|
|
5
|
+
export declare const restrictionNames: readonly ["sale", "sharing", "targeted_advertising", "sensitive_data"];
|
|
6
|
+
export declare function validRestrictions(value: unknown): value is PrivacyRestrictions;
|
|
7
|
+
export declare function serviceRestricted(service: Service, restrictions: PrivacyRestrictions, gpc?: boolean): boolean;
|
|
8
|
+
/** Regional text supplements the immutable base notice; extra applicable profiles remain visible. */
|
|
9
|
+
export declare function regionalNotices(config: PublicConfig, requested: string): {
|
|
10
|
+
locale: string;
|
|
11
|
+
notice: Notice;
|
|
12
|
+
profile?: string;
|
|
13
|
+
}[];
|
|
14
|
+
export declare function regionalRights(config: PublicConfig, requested: string): RightsLinks | undefined;
|
package/dist/regional.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export const regionalRulesVersion = "2026-12-03";
|
|
2
|
+
const states = "AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI WY DC".split(" ");
|
|
3
|
+
/** Location identifiers, not a claim that every jurisdiction has the same law. */
|
|
4
|
+
export const regionalProfiles = [
|
|
5
|
+
"global-strict",
|
|
6
|
+
"eea",
|
|
7
|
+
"uk",
|
|
8
|
+
"ch",
|
|
9
|
+
"br",
|
|
10
|
+
"ca",
|
|
11
|
+
"ca-qc",
|
|
12
|
+
"us",
|
|
13
|
+
...states.map((state) => `us-${state.toLowerCase()}`),
|
|
14
|
+
"au",
|
|
15
|
+
"nz",
|
|
16
|
+
"jp",
|
|
17
|
+
"kr",
|
|
18
|
+
"za",
|
|
19
|
+
"in",
|
|
20
|
+
];
|
|
21
|
+
export const restrictionNames = [
|
|
22
|
+
"sale",
|
|
23
|
+
"sharing",
|
|
24
|
+
"targeted_advertising",
|
|
25
|
+
"sensitive_data",
|
|
26
|
+
];
|
|
27
|
+
export function validRestrictions(value) {
|
|
28
|
+
return (typeof value === "object" &&
|
|
29
|
+
value !== null &&
|
|
30
|
+
!Array.isArray(value) &&
|
|
31
|
+
Object.keys(value).length === 4 &&
|
|
32
|
+
restrictionNames.every((name) => typeof value[name] === "boolean"));
|
|
33
|
+
}
|
|
34
|
+
export function serviceRestricted(service, restrictions, gpc = false) {
|
|
35
|
+
if (service.category === "necessary")
|
|
36
|
+
return false;
|
|
37
|
+
if (gpc && service.category === "marketing")
|
|
38
|
+
return true;
|
|
39
|
+
return restrictionNames.some((name) => (restrictions[name] || (gpc && name !== "sensitive_data")) &&
|
|
40
|
+
(!service.processing || service.processing[name]));
|
|
41
|
+
}
|
|
42
|
+
function localized(values, requested) {
|
|
43
|
+
const key = [requested, requested.split("-")[0] ?? "en", "en", ...Object.keys(values)].find((candidate) => Object.hasOwn(values, candidate));
|
|
44
|
+
return key ? { locale: key, value: values[key] } : undefined;
|
|
45
|
+
}
|
|
46
|
+
/** Regional text supplements the immutable base notice; extra applicable profiles remain visible. */
|
|
47
|
+
export function regionalNotices(config, requested) {
|
|
48
|
+
const result = [];
|
|
49
|
+
const overrides = config.policy.regional?.notices;
|
|
50
|
+
const selected = config.region?.profile_id ?? "global-strict";
|
|
51
|
+
const parent = selected.startsWith("us-") ? "us" : selected === "ca-qc" ? "ca" : undefined;
|
|
52
|
+
const profiles = [
|
|
53
|
+
...new Set([
|
|
54
|
+
selected,
|
|
55
|
+
...(parent ? [parent] : []),
|
|
56
|
+
...(config.region?.applicable_profiles ?? config.policy.regional?.applicable_profiles ?? []),
|
|
57
|
+
"global-strict",
|
|
58
|
+
]),
|
|
59
|
+
].filter((profile) => profile !== "global-strict");
|
|
60
|
+
profiles.push("global-strict");
|
|
61
|
+
for (const profile of profiles) {
|
|
62
|
+
const selected = localized(overrides?.[profile] ?? {}, requested);
|
|
63
|
+
if (selected)
|
|
64
|
+
result.push({ locale: selected.locale, notice: selected.value, profile });
|
|
65
|
+
}
|
|
66
|
+
const base = localized(config.policy.notices, requested);
|
|
67
|
+
if (base)
|
|
68
|
+
result.push({ locale: base.locale, notice: base.value });
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
export function regionalRights(config, requested) {
|
|
72
|
+
return localized(config.policy.regional?.rights ?? {}, requested)?.value;
|
|
73
|
+
}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { initializeConsent } from "./initialize.js";
|
|
2
|
+
const script = document.currentScript;
|
|
3
|
+
const source = script?.dataset.consentConfig;
|
|
4
|
+
if (source && !window.rcoolerConsent) {
|
|
5
|
+
const start = () => initializeConsent({
|
|
6
|
+
configURL: source,
|
|
7
|
+
locale: script?.dataset.consentLocale ?? (document.documentElement.lang || "en"),
|
|
8
|
+
});
|
|
9
|
+
window.rcoolerConsent =
|
|
10
|
+
document.readyState === "loading"
|
|
11
|
+
? new Promise((resolve, reject) => document.addEventListener("DOMContentLoaded", () => {
|
|
12
|
+
void start().then(resolve, reject);
|
|
13
|
+
}, { once: true }))
|
|
14
|
+
: start();
|
|
15
|
+
// A failed configuration leaves optional resources absent and public content usable.
|
|
16
|
+
void window.rcoolerConsent.catch(() => {
|
|
17
|
+
delete window.rcoolerConsent;
|
|
18
|
+
});
|
|
19
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
export type Category = "necessary" | "preferences" | "analytics" | "marketing";
|
|
2
|
+
export type Choices = Record<Exclude<Category, "necessary">, boolean>;
|
|
3
|
+
export interface PrivacyRestrictions {
|
|
4
|
+
sale: boolean;
|
|
5
|
+
sharing: boolean;
|
|
6
|
+
targeted_advertising: boolean;
|
|
7
|
+
sensitive_data: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface PrivacySignals {
|
|
10
|
+
gpc: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface RightsLinks {
|
|
13
|
+
request_url?: string;
|
|
14
|
+
appeal_url?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface RegionalPolicy {
|
|
17
|
+
rules_version: string;
|
|
18
|
+
applicable_profiles: string[];
|
|
19
|
+
notices?: Record<string, Record<string, Notice>>;
|
|
20
|
+
rights?: Record<string, RightsLinks>;
|
|
21
|
+
}
|
|
22
|
+
export interface RegionSelection {
|
|
23
|
+
country?: string;
|
|
24
|
+
subdivision?: string;
|
|
25
|
+
source: "unknown" | "host" | "trusted_proxy";
|
|
26
|
+
profile_id: string;
|
|
27
|
+
applicable_profiles: string[];
|
|
28
|
+
rules_version: string;
|
|
29
|
+
selection_id: string;
|
|
30
|
+
resolution_version?: string;
|
|
31
|
+
}
|
|
32
|
+
export interface StorageDeclaration {
|
|
33
|
+
name: string;
|
|
34
|
+
type: "cookie" | "localStorage";
|
|
35
|
+
domain?: string;
|
|
36
|
+
path?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface Service {
|
|
39
|
+
id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
description: string;
|
|
42
|
+
provider: string;
|
|
43
|
+
category: Category;
|
|
44
|
+
kind: "ga4" | "iframe" | "script" | "custom";
|
|
45
|
+
destination?: string;
|
|
46
|
+
measurement_id?: string;
|
|
47
|
+
policy_url: string;
|
|
48
|
+
storage: StorageDeclaration[];
|
|
49
|
+
processing?: PrivacyRestrictions;
|
|
50
|
+
}
|
|
51
|
+
export interface Notice {
|
|
52
|
+
title: string;
|
|
53
|
+
description: string;
|
|
54
|
+
privacy_url: string;
|
|
55
|
+
}
|
|
56
|
+
export interface Policy {
|
|
57
|
+
schema_version: 1;
|
|
58
|
+
site: string;
|
|
59
|
+
epoch: string;
|
|
60
|
+
version: number;
|
|
61
|
+
expires_after_days: number;
|
|
62
|
+
retention_days: number;
|
|
63
|
+
notices: Record<string, Notice>;
|
|
64
|
+
services: Service[];
|
|
65
|
+
regional?: RegionalPolicy;
|
|
66
|
+
}
|
|
67
|
+
export interface PublicConfig {
|
|
68
|
+
enabled: boolean;
|
|
69
|
+
policy: Policy;
|
|
70
|
+
allowed_services: string[];
|
|
71
|
+
receipt_url?: string;
|
|
72
|
+
recheck_after_seconds: number;
|
|
73
|
+
region?: RegionSelection;
|
|
74
|
+
privacy_signals?: PrivacySignals;
|
|
75
|
+
}
|
|
76
|
+
export interface ReceiptRequest {
|
|
77
|
+
event_id: string;
|
|
78
|
+
previous_receipt_id?: string;
|
|
79
|
+
site: string;
|
|
80
|
+
epoch: string;
|
|
81
|
+
policy_version: number;
|
|
82
|
+
locale: string;
|
|
83
|
+
choices: Choices;
|
|
84
|
+
decided_at: string;
|
|
85
|
+
sequence: number;
|
|
86
|
+
region?: RegionSelection;
|
|
87
|
+
privacy_signals?: PrivacySignals;
|
|
88
|
+
restrictions?: PrivacyRestrictions;
|
|
89
|
+
}
|
|
90
|
+
export interface ReceiptResponse {
|
|
91
|
+
receipt_id: string;
|
|
92
|
+
event_id: string;
|
|
93
|
+
received_at: string;
|
|
94
|
+
expires_at: string;
|
|
95
|
+
}
|
|
96
|
+
export interface Decision extends ReceiptRequest {
|
|
97
|
+
source?: "privacy_signal";
|
|
98
|
+
expires_at: number;
|
|
99
|
+
receipt_id?: string;
|
|
100
|
+
acknowledged: boolean;
|
|
101
|
+
}
|
|
102
|
+
export interface ConsentState {
|
|
103
|
+
config: PublicConfig;
|
|
104
|
+
choices: Choices;
|
|
105
|
+
decided: boolean;
|
|
106
|
+
pending: boolean;
|
|
107
|
+
error: string | null;
|
|
108
|
+
restrictions: PrivacyRestrictions;
|
|
109
|
+
privacy_signals: PrivacySignals;
|
|
110
|
+
}
|
|
111
|
+
export interface ServiceContext {
|
|
112
|
+
service: Service;
|
|
113
|
+
signal: AbortSignal;
|
|
114
|
+
isAllowed(): boolean;
|
|
115
|
+
}
|
|
116
|
+
export interface ServiceAdapter {
|
|
117
|
+
activate(context: ServiceContext): void | (() => void) | Promise<void | (() => void)>;
|
|
118
|
+
/** Loaded SDKs which cannot be reliably stopped must reload on withdrawal. */
|
|
119
|
+
reloadOnRevoke?: boolean;
|
|
120
|
+
}
|
|
121
|
+
export interface ConsentController {
|
|
122
|
+
readonly state: ConsentState;
|
|
123
|
+
subscribe(listener: (state: ConsentState) => void): () => void;
|
|
124
|
+
decide(choices: Choices, locale?: string, restrictions?: PrivacyRestrictions): Promise<boolean>;
|
|
125
|
+
restrict(restrictions: PrivacyRestrictions, locale?: string): Promise<boolean>;
|
|
126
|
+
isAllowed(serviceID: string): boolean;
|
|
127
|
+
register(serviceID: string, adapter: ServiceAdapter): () => void;
|
|
128
|
+
recheck(): Promise<boolean>;
|
|
129
|
+
destroy(): void;
|
|
130
|
+
}
|
|
131
|
+
export declare const deniedChoices: () => Choices;
|
|
132
|
+
export declare const noRestrictions: () => PrivacyRestrictions;
|
package/dist/types.js
ADDED