@aifeatures/react 0.7.1 → 0.8.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 +33 -0
- package/dist/consent-reader.cjs +1 -0
- package/dist/consent-reader.d.cts +130 -0
- package/dist/consent-reader.d.ts +130 -0
- package/dist/consent-reader.js +1 -0
- package/dist/index.cjs +8 -1
- package/dist/index.d.cts +630 -3
- package/dist/index.d.ts +630 -3
- package/dist/index.js +8 -1
- package/package.json +20 -4
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @aifeatures/react consent runtime
|
|
2
|
+
|
|
3
|
+
`ConsentProvider` requests `/_ls/v1/geo` and `/_ls/v1/prefs/{configId}` on the current site's origin by default. Published sites, shared/editor previews, and local development must serve those two paths through site-services. No hostname is needed in generated site code:
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
<ConsentProvider configId={consentConfigId}>
|
|
7
|
+
<App />
|
|
8
|
+
<ConsentBanner />
|
|
9
|
+
</ConsentProvider>
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
An explicit `apiUrl` still selects another base URL, including the existing public API and Storybook's mock hosts. Other components retain their existing defaults. Response bodies must match the consent contract, including `country`, `region`, and `shape` on geo responses. The legacy country-only geo response is invalid and causes a strict fallback. Additive response headers are diagnostic information; missing or unfamiliar versions do not override a valid body.
|
|
13
|
+
|
|
14
|
+
## Inspect a decision in DevTools
|
|
15
|
+
|
|
16
|
+
Run this in the browser console after the provider mounts:
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
window.__LS_CONSENT_DEBUG__()
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The function returns copies of current snapshots for up to ten active providers, identified by the site's `configId`. It accepts no commands, does not change consent, and removes a provider's snapshot on unmount. Nothing is persisted or sent anywhere. Snapshots contain no country, region, IP, saved grants, visitor identifier, or request/response body.
|
|
23
|
+
|
|
24
|
+
Each snapshot includes:
|
|
25
|
+
|
|
26
|
+
- `mode` and final `shape`. Both are `null` on a returning visitor's zero-request path because the saved choice applies without resolving geography or owner mode.
|
|
27
|
+
- `reason`: `unresolved`, `stored-choice`, `renewal-required`, `geo`, `unknown-geo`, `strict-everywhere`, `off`, `prefs-fallback`, or `geo-fallback`. Saved choices and renewal take precedence over a previously resolved verdict.
|
|
28
|
+
- `storedChoice`: `unread`, `none`, `valid`, `revision-mismatch`, `taxonomy-version`, `expired`, `unverified-mirror`, or `malformed`; `renewalRequired` indicates when an unreadable or outdated choice forces opt-in. Genuine expiry uses the existing first-visit policy.
|
|
29
|
+
- `requests.prefs` and `requests.geo`: `outcome` is `not-requested`, `pending`, `success`, `timeout`, `http-error`, `network-error`, or `invalid-contract`. Each reports `httpStatus`, `version` (`X-LS-Consent-Version`), `requestId` (`X-LS-Request-Id`), and `geoSource` (`cloudflare`, `unknown`, or `null`). Missing headers are `null`; version and request ID values are limited to 128 characters.
|
|
30
|
+
|
|
31
|
+
Requests have a two-second deadline covering headers and body. Failures produce a console warning with the safe request diagnostics. Config failure uses the conservative strict fallback; geo failure uses the existing conservative verdict under the resolved owner setting. A valid saved choice still makes zero automatic requests. Opening preferences on a legacy installation without a baked policy path may fetch display config; that request is also reported.
|
|
32
|
+
|
|
33
|
+
This mechanism is for troubleshooting in DevTools. Application integrations should continue to use `useConsent()` and the shared consent components.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var d=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var R=(n,t)=>{for(var r in t)d(n,r,{get:t[r],enumerable:!0})},b=(n,t,r,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of w(t))!S.call(n,o)&&o!==r&&d(n,o,{get:()=>t[o],enumerable:!(e=y(t,o))||e.enumerable});return n};var h=n=>b(d({},"__esModule",{value:!0}),n);var X={};R(X,{CONSENT_COOKIE_PREFIX:()=>a,readSharedConsentRecord:()=>G,readSharedConsentStatus:()=>m,subscribeToConsentRecord:()=>H});module.exports=h(X);var k=["functional","analytics","advertising"];var a="af-consent-",x=/^[A-Za-z0-9_-]+$/;function $(n){if(!x.test(n))throw new TypeError(`aifeatures consent: invalid config id ${JSON.stringify(n)} \u2014 expected only A-Z a-z 0-9 _ - (the id returned by setup_cookie_consent).`);return`${a}${n}`}var O=1,E=365*24*60*60,_=E*1e3,N=1440*60*1e3,u="aifeatures:consent-storage";var T=["landingsite.dev","agncy.dev","agcy.dev","getlandingsite.com"];var c;function l(n){return n.endsWith(".")?n.slice(0,-1):n}function A(){return c!==void 0?c:typeof window<"u"?l(window.location.hostname):""}function I(n){let t=l(n);return T.some(r=>t===r||t.endsWith(`.${r}`))}function L(n){if(typeof document>"u")return[];let t;try{t=document.cookie}catch{return[]}let r=[];for(let e of t.split(";")){let o=e.indexOf("=");o!==-1&&e.slice(0,o).trim()===n&&r.push(e.slice(o+1).trim())}return r}function P(){if(typeof window>"u")return[];let n=[];for(let t of["localStorage","sessionStorage"])try{let r=window[t];r&&n.push(r)}catch{}return n}function D(n){let t=[];for(let r of P())try{let e=r.getItem(n);e!==null&&t.push(e)}catch{}return t}function i(n){return typeof n=="boolean"}function F(n,t){let r;try{r=JSON.parse(decodeURIComponent(n))}catch{return{kind:"invalid",reason:"malformed"}}if(typeof r!="object"||r===null)return{kind:"invalid",reason:"malformed"};let e=r;if(e.v!==O)return typeof e.v=="number"&&Number.isInteger(e.v)&&e.v>0?{kind:"absent-in-place",reason:"taxonomy-version"}:{kind:"invalid",reason:"malformed"};if(typeof e.ts!="number"||!Number.isFinite(e.ts))return{kind:"invalid",reason:"malformed"};if(e.ts-Date.now()>N)return{kind:"invalid",reason:"malformed"};if(typeof e.cid!="string"||e.cid.length===0)return{kind:"invalid",reason:"malformed"};if(typeof e.grants!="object"||e.grants===null)return{kind:"invalid",reason:"malformed"};let o=e.grants;return!i(o.functional)||!i(o.analytics)||!i(o.advertising)?{kind:"invalid",reason:"malformed"}:e.cid!==t?{kind:"absent-in-place",reason:"malformed"}:e.revision!==void 0&&(typeof e.revision!="string"||e.revision.trim()==="")||e.gpcOverride!==void 0&&!i(e.gpcOverride)?{kind:"invalid",reason:"malformed"}:Date.now()-e.ts>_?{kind:"invalid",reason:"expired"}:{kind:"valid",record:{v:e.v,ts:e.ts,cid:e.cid,grants:{functional:o.functional,analytics:o.analytics,advertising:o.advertising},...e.revision!==void 0?{revision:e.revision}:{},...e.gpcOverride===!0?{gpcOverride:!0}:{}}}}function U(n){let t=$(n);if(I(A()))return{values:D(t),mirror:!1};let r=L(t);if(r.length>0)return{values:r,mirror:!1};let e=M(t);return{values:e===null?[]:[e],mirror:!0}}function M(n){if(typeof window>"u")return null;try{return window.localStorage.getItem(n)}catch{return null}}function V(n){let{values:t,mirror:r}=U(n);if(t.length===0)return{status:{status:"no-record"},clearable:!1};let e=null,o=!0;for(let v of t){let s=F(v,n);if(s.kind==="valid")return r&&k.some(C=>s.record.grants[C])?{status:{status:"unreadable",reason:"unverified-mirror"},clearable:!1}:{status:{status:"answered",record:s.record},clearable:!1};s.reason!=="expired"&&(o=!1),(e===null||e==="expired")&&(e=s.reason)}return{status:{status:"unreadable",reason:e??"malformed"},clearable:o}}function f(n){return V(n).status}function g(n){if(typeof window>"u")return()=>{};let t=e=>{(e.key===null||e.key.startsWith(a))&&n()},r=()=>n();return window.addEventListener("storage",t),window.addEventListener(u,r),()=>{window.removeEventListener("storage",t),window.removeEventListener(u,r)}}function m({acceptCids:n}){let t=null;for(let r of n){let e;try{e=f(r)}catch{p||(p=!0,console.error(`aifeatures consent: invalid config id ${JSON.stringify(r)} in acceptCids \u2014 nothing can be honored for it. Check the value against the config id setup_cookie_consent returned.`)),e={status:"unreadable",reason:"malformed"}}if(e.status==="answered")return typeof navigator<"u"&&!!navigator.globalPrivacyControl&&e.record.gpcOverride!==!0&&e.record.grants.advertising?{status:"answered",record:{...e.record,grants:{...e.record.grants,advertising:!1}}}:e;e.status==="unreadable"&&(t??(t=e))}return t??{status:"no-record"}}var p=!1;function G(n){let t=m(n);return t.status==="answered"?t.record:null}var H=g;0&&(module.exports={CONSENT_COOKIE_PREFIX,readSharedConsentRecord,readSharedConsentStatus,subscribeToConsentRecord});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
interface ConsentGrants {
|
|
2
|
+
functional: boolean;
|
|
3
|
+
analytics: boolean;
|
|
4
|
+
advertising: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface ConsentRecord {
|
|
7
|
+
/** Taxonomy version the grants were recorded under. */
|
|
8
|
+
v: number;
|
|
9
|
+
/** Epoch ms of the LAST choice action — one timestamp covers all
|
|
10
|
+
* categories; any write resets it (R3). */
|
|
11
|
+
ts: number;
|
|
12
|
+
/** The consent config id the choice was made under. Redundant with the
|
|
13
|
+
* per-cid storage name, kept as belt-and-braces: a record whose cid does
|
|
14
|
+
* not match the name it was read from is never honored. */
|
|
15
|
+
cid: string;
|
|
16
|
+
grants: ConsentGrants;
|
|
17
|
+
/** Site disclosure revision shown when the visitor chose. */
|
|
18
|
+
revision?: string;
|
|
19
|
+
/** An explicit, informed Preferences action overridden an active GPC signal. */
|
|
20
|
+
gpcOverride?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** Why a stored value exists but cannot be honored. `taxonomy-version` is the
|
|
23
|
+
* skew case (a newer package wrote it); `expired` is a choice past its
|
|
24
|
+
* 12-month life; `malformed` covers garbage, wrong types, an impossibly
|
|
25
|
+
* future timestamp, and a record carrying a foreign cid. */
|
|
26
|
+
type ConsentUnreadableReason = 'taxonomy-version' | 'expired' | 'unverified-mirror' | 'malformed';
|
|
27
|
+
/** The full outcome of a stored-record read: the distinction classification
|
|
28
|
+
* already computes, kept instead of collapsed to record-or-null, so a
|
|
29
|
+
* consumer can tell "the visitor declined" from "we could not read what they
|
|
30
|
+
* chose" (both gate identically — only one is worth alerting on). */
|
|
31
|
+
type ConsentReadStatus = {
|
|
32
|
+
status: 'answered';
|
|
33
|
+
record: ConsentRecord;
|
|
34
|
+
} | {
|
|
35
|
+
status: 'no-record';
|
|
36
|
+
} | {
|
|
37
|
+
status: 'unreadable';
|
|
38
|
+
reason: ConsentUnreadableReason;
|
|
39
|
+
};
|
|
40
|
+
declare const CONSENT_COOKIE_PREFIX = "af-consent-";
|
|
41
|
+
/**
|
|
42
|
+
* Listen for consent-record changes from ANY writer: another tab (`storage`
|
|
43
|
+
* event on the localStorage value) or this tab (our CustomEvent). The
|
|
44
|
+
* listener should re-read via readConsentRecord(). Returns an unsubscribe.
|
|
45
|
+
* No-op outside the browser.
|
|
46
|
+
*
|
|
47
|
+
* SAME-ORIGIN notification only: the `storage` event fires per origin, so a
|
|
48
|
+
* choice made on another subdomain (www vs app) arrives silently in the
|
|
49
|
+
* cookie — consumers that care must re-read on visibilitychange/focus.
|
|
50
|
+
*/
|
|
51
|
+
declare function subscribeToConsentRecord$1(listener: () => void): () => void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* React-free shared consent reader — the `@aifeatures/react/consent-reader`
|
|
55
|
+
* subpath entry, for first-party platform surfaces that need to HONOR a
|
|
56
|
+
* consent choice made on a sibling subdomain (the www → app chain: the
|
|
57
|
+
* marketing site's ConsentProvider writes the registrable-domain cookie, the
|
|
58
|
+
* app reads it) without mounting the React consent engine.
|
|
59
|
+
*
|
|
60
|
+
* Read-only by contract: whatever it finds — garbage, foreign, expired — it
|
|
61
|
+
* NEVER clears, writes, or mirrors anything. It shares storage classification
|
|
62
|
+
* and honors the current GPC signal. Site disclosure-revision validation is
|
|
63
|
+
* owned by the provider with the published manifest.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/** At least one config id — `acceptCids: []` can never read anything, so it
|
|
67
|
+
* is a compile error rather than a silent permanent null. */
|
|
68
|
+
interface ReadSharedConsentOptions {
|
|
69
|
+
/** Every config id whose choice this surface accepts (e.g. the www site's
|
|
70
|
+
* and the app's own), MOST-PREFERRED FIRST. */
|
|
71
|
+
acceptCids: readonly [string, ...string[]];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* THE reader: what the visitor chose, or why nothing is being honored.
|
|
75
|
+
*
|
|
76
|
+
* - `answered` — a valid stored record (which may grant nothing: an explicit
|
|
77
|
+
* rejection is an answer).
|
|
78
|
+
* - `no-record` — nothing stored for any of `acceptCids`. A visitor who has
|
|
79
|
+
* not chosen yet, a visitor who cleared their cookies, and an
|
|
80
|
+
* unknown/misspelled cid are indistinguishable here: verify the cid
|
|
81
|
+
* against what `setup_cookie_consent` returned.
|
|
82
|
+
* - `unreadable` — something IS stored but cannot be honored: `reason`
|
|
83
|
+
* `'taxonomy-version'` (a newer package wrote it — a coordinated release
|
|
84
|
+
* is mid-flight and this surface is blacked out until it catches up),
|
|
85
|
+
* `'expired'` (past the 12-month life), `'unverified-mirror'` (the shared
|
|
86
|
+
* cookie disappeared, leaving an origin's unverifiable grants), or `'malformed'` (garbage, or a
|
|
87
|
+
* record carrying a foreign cid).
|
|
88
|
+
*
|
|
89
|
+
* All three gate identically — nothing but `answered` may run a tracker — but
|
|
90
|
+
* only `unreadable` is worth alerting on: it is the difference between a
|
|
91
|
+
* visitor who declined and a deployment that has gone silently blind. Each
|
|
92
|
+
* config id maps to its own `af-consent-${cid}` storage name, checked at
|
|
93
|
+
* every scope it may exist at. Never clears or writes anything under any
|
|
94
|
+
* input, and never throws: a config id outside nanoid's alphabet — an unset
|
|
95
|
+
* or empty env var being the realistic case — reports `unreadable` /
|
|
96
|
+
* `'malformed'` and logs once, so a misconfigured deploy degrades to
|
|
97
|
+
* no-tracking with a signal instead of breaking the consuming page.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const consent = readSharedConsentStatus({ acceptCids: [WWW_CONSENT_CID] });
|
|
102
|
+
* if (consent.status === 'answered' && consent.record.grants.analytics) initAnalytics();
|
|
103
|
+
* else if (consent.status === 'unreadable') console.error('consent unreadable', consent.reason);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
declare function readSharedConsentStatus({ acceptCids, }: ReadSharedConsentOptions): ConsentReadStatus;
|
|
107
|
+
/**
|
|
108
|
+
* Convenience wrapper over readSharedConsentStatus: the first valid stored
|
|
109
|
+
* record among `acceptCids`, or null. A `null` covers BOTH "no choice stored"
|
|
110
|
+
* and "stored but unreadable" — use readSharedConsentStatus when a surface
|
|
111
|
+
* needs to alert on the difference (a taxonomy-version skew is an analytics
|
|
112
|
+
* blackout that reports as a visitor who never answered).
|
|
113
|
+
*
|
|
114
|
+
* An UNKNOWN OR MISSPELLED cid returns exactly the same `null` as a visitor
|
|
115
|
+
* who has never answered — nothing here can tell them apart, and a wrong cid
|
|
116
|
+
* therefore looks like a site where nobody ever consents. Verify the value
|
|
117
|
+
* against the config id `setup_cookie_consent` returned. (A visitor who
|
|
118
|
+
* DECLINED is not null: that is an `answered` record with every grant false.)
|
|
119
|
+
*/
|
|
120
|
+
declare function readSharedConsentRecord(options: ReadSharedConsentOptions): ConsentRecord | null;
|
|
121
|
+
/**
|
|
122
|
+
* Re-exported change subscription. SAME-ORIGIN notification only: the
|
|
123
|
+
* `storage` event fires per origin, so a choice made on another subdomain
|
|
124
|
+
* (a www choice, in an app tab) arrives silently in the cookie and fires
|
|
125
|
+
* NOTHING here — cross-subdomain consumers must re-read via
|
|
126
|
+
* readSharedConsentStatus() on `visibilitychange`/`focus`.
|
|
127
|
+
*/
|
|
128
|
+
declare const subscribeToConsentRecord: typeof subscribeToConsentRecord$1;
|
|
129
|
+
|
|
130
|
+
export { CONSENT_COOKIE_PREFIX, type ConsentGrants, type ConsentReadStatus, type ConsentRecord, type ConsentUnreadableReason, type ReadSharedConsentOptions, readSharedConsentRecord, readSharedConsentStatus, subscribeToConsentRecord };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
interface ConsentGrants {
|
|
2
|
+
functional: boolean;
|
|
3
|
+
analytics: boolean;
|
|
4
|
+
advertising: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface ConsentRecord {
|
|
7
|
+
/** Taxonomy version the grants were recorded under. */
|
|
8
|
+
v: number;
|
|
9
|
+
/** Epoch ms of the LAST choice action — one timestamp covers all
|
|
10
|
+
* categories; any write resets it (R3). */
|
|
11
|
+
ts: number;
|
|
12
|
+
/** The consent config id the choice was made under. Redundant with the
|
|
13
|
+
* per-cid storage name, kept as belt-and-braces: a record whose cid does
|
|
14
|
+
* not match the name it was read from is never honored. */
|
|
15
|
+
cid: string;
|
|
16
|
+
grants: ConsentGrants;
|
|
17
|
+
/** Site disclosure revision shown when the visitor chose. */
|
|
18
|
+
revision?: string;
|
|
19
|
+
/** An explicit, informed Preferences action overridden an active GPC signal. */
|
|
20
|
+
gpcOverride?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** Why a stored value exists but cannot be honored. `taxonomy-version` is the
|
|
23
|
+
* skew case (a newer package wrote it); `expired` is a choice past its
|
|
24
|
+
* 12-month life; `malformed` covers garbage, wrong types, an impossibly
|
|
25
|
+
* future timestamp, and a record carrying a foreign cid. */
|
|
26
|
+
type ConsentUnreadableReason = 'taxonomy-version' | 'expired' | 'unverified-mirror' | 'malformed';
|
|
27
|
+
/** The full outcome of a stored-record read: the distinction classification
|
|
28
|
+
* already computes, kept instead of collapsed to record-or-null, so a
|
|
29
|
+
* consumer can tell "the visitor declined" from "we could not read what they
|
|
30
|
+
* chose" (both gate identically — only one is worth alerting on). */
|
|
31
|
+
type ConsentReadStatus = {
|
|
32
|
+
status: 'answered';
|
|
33
|
+
record: ConsentRecord;
|
|
34
|
+
} | {
|
|
35
|
+
status: 'no-record';
|
|
36
|
+
} | {
|
|
37
|
+
status: 'unreadable';
|
|
38
|
+
reason: ConsentUnreadableReason;
|
|
39
|
+
};
|
|
40
|
+
declare const CONSENT_COOKIE_PREFIX = "af-consent-";
|
|
41
|
+
/**
|
|
42
|
+
* Listen for consent-record changes from ANY writer: another tab (`storage`
|
|
43
|
+
* event on the localStorage value) or this tab (our CustomEvent). The
|
|
44
|
+
* listener should re-read via readConsentRecord(). Returns an unsubscribe.
|
|
45
|
+
* No-op outside the browser.
|
|
46
|
+
*
|
|
47
|
+
* SAME-ORIGIN notification only: the `storage` event fires per origin, so a
|
|
48
|
+
* choice made on another subdomain (www vs app) arrives silently in the
|
|
49
|
+
* cookie — consumers that care must re-read on visibilitychange/focus.
|
|
50
|
+
*/
|
|
51
|
+
declare function subscribeToConsentRecord$1(listener: () => void): () => void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* React-free shared consent reader — the `@aifeatures/react/consent-reader`
|
|
55
|
+
* subpath entry, for first-party platform surfaces that need to HONOR a
|
|
56
|
+
* consent choice made on a sibling subdomain (the www → app chain: the
|
|
57
|
+
* marketing site's ConsentProvider writes the registrable-domain cookie, the
|
|
58
|
+
* app reads it) without mounting the React consent engine.
|
|
59
|
+
*
|
|
60
|
+
* Read-only by contract: whatever it finds — garbage, foreign, expired — it
|
|
61
|
+
* NEVER clears, writes, or mirrors anything. It shares storage classification
|
|
62
|
+
* and honors the current GPC signal. Site disclosure-revision validation is
|
|
63
|
+
* owned by the provider with the published manifest.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/** At least one config id — `acceptCids: []` can never read anything, so it
|
|
67
|
+
* is a compile error rather than a silent permanent null. */
|
|
68
|
+
interface ReadSharedConsentOptions {
|
|
69
|
+
/** Every config id whose choice this surface accepts (e.g. the www site's
|
|
70
|
+
* and the app's own), MOST-PREFERRED FIRST. */
|
|
71
|
+
acceptCids: readonly [string, ...string[]];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* THE reader: what the visitor chose, or why nothing is being honored.
|
|
75
|
+
*
|
|
76
|
+
* - `answered` — a valid stored record (which may grant nothing: an explicit
|
|
77
|
+
* rejection is an answer).
|
|
78
|
+
* - `no-record` — nothing stored for any of `acceptCids`. A visitor who has
|
|
79
|
+
* not chosen yet, a visitor who cleared their cookies, and an
|
|
80
|
+
* unknown/misspelled cid are indistinguishable here: verify the cid
|
|
81
|
+
* against what `setup_cookie_consent` returned.
|
|
82
|
+
* - `unreadable` — something IS stored but cannot be honored: `reason`
|
|
83
|
+
* `'taxonomy-version'` (a newer package wrote it — a coordinated release
|
|
84
|
+
* is mid-flight and this surface is blacked out until it catches up),
|
|
85
|
+
* `'expired'` (past the 12-month life), `'unverified-mirror'` (the shared
|
|
86
|
+
* cookie disappeared, leaving an origin's unverifiable grants), or `'malformed'` (garbage, or a
|
|
87
|
+
* record carrying a foreign cid).
|
|
88
|
+
*
|
|
89
|
+
* All three gate identically — nothing but `answered` may run a tracker — but
|
|
90
|
+
* only `unreadable` is worth alerting on: it is the difference between a
|
|
91
|
+
* visitor who declined and a deployment that has gone silently blind. Each
|
|
92
|
+
* config id maps to its own `af-consent-${cid}` storage name, checked at
|
|
93
|
+
* every scope it may exist at. Never clears or writes anything under any
|
|
94
|
+
* input, and never throws: a config id outside nanoid's alphabet — an unset
|
|
95
|
+
* or empty env var being the realistic case — reports `unreadable` /
|
|
96
|
+
* `'malformed'` and logs once, so a misconfigured deploy degrades to
|
|
97
|
+
* no-tracking with a signal instead of breaking the consuming page.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const consent = readSharedConsentStatus({ acceptCids: [WWW_CONSENT_CID] });
|
|
102
|
+
* if (consent.status === 'answered' && consent.record.grants.analytics) initAnalytics();
|
|
103
|
+
* else if (consent.status === 'unreadable') console.error('consent unreadable', consent.reason);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
declare function readSharedConsentStatus({ acceptCids, }: ReadSharedConsentOptions): ConsentReadStatus;
|
|
107
|
+
/**
|
|
108
|
+
* Convenience wrapper over readSharedConsentStatus: the first valid stored
|
|
109
|
+
* record among `acceptCids`, or null. A `null` covers BOTH "no choice stored"
|
|
110
|
+
* and "stored but unreadable" — use readSharedConsentStatus when a surface
|
|
111
|
+
* needs to alert on the difference (a taxonomy-version skew is an analytics
|
|
112
|
+
* blackout that reports as a visitor who never answered).
|
|
113
|
+
*
|
|
114
|
+
* An UNKNOWN OR MISSPELLED cid returns exactly the same `null` as a visitor
|
|
115
|
+
* who has never answered — nothing here can tell them apart, and a wrong cid
|
|
116
|
+
* therefore looks like a site where nobody ever consents. Verify the value
|
|
117
|
+
* against the config id `setup_cookie_consent` returned. (A visitor who
|
|
118
|
+
* DECLINED is not null: that is an `answered` record with every grant false.)
|
|
119
|
+
*/
|
|
120
|
+
declare function readSharedConsentRecord(options: ReadSharedConsentOptions): ConsentRecord | null;
|
|
121
|
+
/**
|
|
122
|
+
* Re-exported change subscription. SAME-ORIGIN notification only: the
|
|
123
|
+
* `storage` event fires per origin, so a choice made on another subdomain
|
|
124
|
+
* (a www choice, in an app tab) arrives silently in the cookie and fires
|
|
125
|
+
* NOTHING here — cross-subdomain consumers must re-read via
|
|
126
|
+
* readSharedConsentStatus() on `visibilitychange`/`focus`.
|
|
127
|
+
*/
|
|
128
|
+
declare const subscribeToConsentRecord: typeof subscribeToConsentRecord$1;
|
|
129
|
+
|
|
130
|
+
export { CONSENT_COOKIE_PREFIX, type ConsentGrants, type ConsentReadStatus, type ConsentRecord, type ConsentUnreadableReason, type ReadSharedConsentOptions, readSharedConsentRecord, readSharedConsentStatus, subscribeToConsentRecord };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var v=["functional","analytics","advertising"];var a="af-consent-",C=/^[A-Za-z0-9_-]+$/;function y(n){if(!C.test(n))throw new TypeError(`aifeatures consent: invalid config id ${JSON.stringify(n)} \u2014 expected only A-Z a-z 0-9 _ - (the id returned by setup_cookie_consent).`);return`${a}${n}`}var w=1,S=365*24*60*60,R=S*1e3,b=1440*60*1e3,d="aifeatures:consent-storage";var h=["landingsite.dev","agncy.dev","agcy.dev","getlandingsite.com"];var u;function c(n){return n.endsWith(".")?n.slice(0,-1):n}function k(){return u!==void 0?u:typeof window<"u"?c(window.location.hostname):""}function x(n){let t=c(n);return h.some(r=>t===r||t.endsWith(`.${r}`))}function $(n){if(typeof document>"u")return[];let t;try{t=document.cookie}catch{return[]}let r=[];for(let e of t.split(";")){let o=e.indexOf("=");o!==-1&&e.slice(0,o).trim()===n&&r.push(e.slice(o+1).trim())}return r}function O(){if(typeof window>"u")return[];let n=[];for(let t of["localStorage","sessionStorage"])try{let r=window[t];r&&n.push(r)}catch{}return n}function E(n){let t=[];for(let r of O())try{let e=r.getItem(n);e!==null&&t.push(e)}catch{}return t}function i(n){return typeof n=="boolean"}function _(n,t){let r;try{r=JSON.parse(decodeURIComponent(n))}catch{return{kind:"invalid",reason:"malformed"}}if(typeof r!="object"||r===null)return{kind:"invalid",reason:"malformed"};let e=r;if(e.v!==w)return typeof e.v=="number"&&Number.isInteger(e.v)&&e.v>0?{kind:"absent-in-place",reason:"taxonomy-version"}:{kind:"invalid",reason:"malformed"};if(typeof e.ts!="number"||!Number.isFinite(e.ts))return{kind:"invalid",reason:"malformed"};if(e.ts-Date.now()>b)return{kind:"invalid",reason:"malformed"};if(typeof e.cid!="string"||e.cid.length===0)return{kind:"invalid",reason:"malformed"};if(typeof e.grants!="object"||e.grants===null)return{kind:"invalid",reason:"malformed"};let o=e.grants;return!i(o.functional)||!i(o.analytics)||!i(o.advertising)?{kind:"invalid",reason:"malformed"}:e.cid!==t?{kind:"absent-in-place",reason:"malformed"}:e.revision!==void 0&&(typeof e.revision!="string"||e.revision.trim()==="")||e.gpcOverride!==void 0&&!i(e.gpcOverride)?{kind:"invalid",reason:"malformed"}:Date.now()-e.ts>R?{kind:"invalid",reason:"expired"}:{kind:"valid",record:{v:e.v,ts:e.ts,cid:e.cid,grants:{functional:o.functional,analytics:o.analytics,advertising:o.advertising},...e.revision!==void 0?{revision:e.revision}:{},...e.gpcOverride===!0?{gpcOverride:!0}:{}}}}function N(n){let t=y(n);if(x(k()))return{values:E(t),mirror:!1};let r=$(t);if(r.length>0)return{values:r,mirror:!1};let e=T(t);return{values:e===null?[]:[e],mirror:!0}}function T(n){if(typeof window>"u")return null;try{return window.localStorage.getItem(n)}catch{return null}}function A(n){let{values:t,mirror:r}=N(n);if(t.length===0)return{status:{status:"no-record"},clearable:!1};let e=null,o=!0;for(let p of t){let s=_(p,n);if(s.kind==="valid")return r&&v.some(m=>s.record.grants[m])?{status:{status:"unreadable",reason:"unverified-mirror"},clearable:!1}:{status:{status:"answered",record:s.record},clearable:!1};s.reason!=="expired"&&(o=!1),(e===null||e==="expired")&&(e=s.reason)}return{status:{status:"unreadable",reason:e??"malformed"},clearable:o}}function l(n){return A(n).status}function f(n){if(typeof window>"u")return()=>{};let t=e=>{(e.key===null||e.key.startsWith(a))&&n()},r=()=>n();return window.addEventListener("storage",t),window.addEventListener(d,r),()=>{window.removeEventListener("storage",t),window.removeEventListener(d,r)}}function I({acceptCids:n}){let t=null;for(let r of n){let e;try{e=l(r)}catch{g||(g=!0,console.error(`aifeatures consent: invalid config id ${JSON.stringify(r)} in acceptCids \u2014 nothing can be honored for it. Check the value against the config id setup_cookie_consent returned.`)),e={status:"unreadable",reason:"malformed"}}if(e.status==="answered")return typeof navigator<"u"&&!!navigator.globalPrivacyControl&&e.record.gpcOverride!==!0&&e.record.grants.advertising?{status:"answered",record:{...e.record,grants:{...e.record.grants,advertising:!1}}}:e;e.status==="unreadable"&&(t??(t=e))}return t??{status:"no-record"}}var g=!1;function D(n){let t=I(n);return t.status==="answered"?t.record:null}var F=f;export{a as CONSENT_COOKIE_PREFIX,D as readSharedConsentRecord,I as readSharedConsentStatus,F as subscribeToConsentRecord};
|