@owlmeans/consent 0.1.18-rc.9 → 0.1.18

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.
Files changed (115) hide show
  1. package/README.md +229 -0
  2. package/build/consts.d.ts +78 -3
  3. package/build/consts.d.ts.map +1 -1
  4. package/build/consts.js +104 -0
  5. package/build/consts.js.map +1 -1
  6. package/build/consts.local.d.ts +12 -0
  7. package/build/consts.local.d.ts.map +1 -0
  8. package/build/consts.local.js +12 -0
  9. package/build/consts.local.js.map +1 -0
  10. package/build/geo/types.d.ts +47 -0
  11. package/build/geo/types.d.ts.map +1 -0
  12. package/build/geo/types.js +2 -0
  13. package/build/geo/types.js.map +1 -0
  14. package/build/geo.d.ts +4 -0
  15. package/build/geo.d.ts.map +1 -0
  16. package/build/geo.js +96 -0
  17. package/build/geo.js.map +1 -0
  18. package/build/gtobserver/types.d.ts +75 -0
  19. package/build/gtobserver/types.d.ts.map +1 -0
  20. package/build/gtobserver/types.js +2 -0
  21. package/build/gtobserver/types.js.map +1 -0
  22. package/build/gtobserver.d.ts +7 -0
  23. package/build/gtobserver.d.ts.map +1 -0
  24. package/build/gtobserver.js +142 -0
  25. package/build/gtobserver.js.map +1 -0
  26. package/build/i18n/be.json +8 -1
  27. package/build/i18n/de.json +8 -1
  28. package/build/i18n/en.json +8 -1
  29. package/build/i18n/es.json +8 -1
  30. package/build/i18n/fr.json +8 -1
  31. package/build/i18n/pl.json +8 -1
  32. package/build/i18n/ru.json +8 -1
  33. package/build/i18n/types.d.ts +11 -0
  34. package/build/i18n/types.d.ts.map +1 -0
  35. package/build/i18n/types.js +2 -0
  36. package/build/i18n/types.js.map +1 -0
  37. package/build/i18n/uk.json +8 -1
  38. package/build/i18n.d.ts +4 -14
  39. package/build/i18n.d.ts.map +1 -1
  40. package/build/i18n.js +12 -27
  41. package/build/i18n.js.map +1 -1
  42. package/build/index.d.ts +13 -5
  43. package/build/index.d.ts.map +1 -1
  44. package/build/index.js +5 -3
  45. package/build/index.js.map +1 -1
  46. package/build/linker/types.d.ts +59 -0
  47. package/build/linker/types.d.ts.map +1 -0
  48. package/build/linker/types.js +2 -0
  49. package/build/linker/types.js.map +1 -0
  50. package/build/linker.d.ts +5 -74
  51. package/build/linker.d.ts.map +1 -1
  52. package/build/linker.js +290 -324
  53. package/build/linker.js.map +1 -1
  54. package/build/plugins/types.d.ts +42 -0
  55. package/build/plugins/types.d.ts.map +1 -0
  56. package/build/plugins/types.js +2 -0
  57. package/build/plugins/types.js.map +1 -0
  58. package/build/plugins.d.ts +4 -68
  59. package/build/plugins.d.ts.map +1 -1
  60. package/build/plugins.js +73 -61
  61. package/build/plugins.js.map +1 -1
  62. package/build/storage/types.d.ts +31 -0
  63. package/build/storage/types.d.ts.map +1 -0
  64. package/build/storage/types.js +2 -0
  65. package/build/storage/types.js.map +1 -0
  66. package/build/storage.d.ts +6 -22
  67. package/build/storage.d.ts.map +1 -1
  68. package/build/storage.js +72 -81
  69. package/build/storage.js.map +1 -1
  70. package/build/store.d.ts +2 -2
  71. package/build/store.d.ts.map +1 -1
  72. package/build/store.js +115 -31
  73. package/build/store.js.map +1 -1
  74. package/build/types.d.ts +141 -5
  75. package/build/types.d.ts.map +1 -1
  76. package/build/types.local.d.ts +9 -0
  77. package/build/types.local.d.ts.map +1 -0
  78. package/build/types.local.js +2 -0
  79. package/build/types.local.js.map +1 -0
  80. package/package.json +3 -3
  81. package/src/consts.local.ts +16 -0
  82. package/src/consts.ts +120 -3
  83. package/src/geo/types.ts +49 -0
  84. package/src/geo.ts +122 -0
  85. package/src/gtobserver/types.ts +73 -0
  86. package/src/gtobserver.ts +182 -0
  87. package/src/i18n/be.json +8 -1
  88. package/src/i18n/de.json +8 -1
  89. package/src/i18n/en.json +8 -1
  90. package/src/i18n/es.json +8 -1
  91. package/src/i18n/fr.json +8 -1
  92. package/src/i18n/pl.json +8 -1
  93. package/src/i18n/ru.json +8 -1
  94. package/src/i18n/types.ts +11 -0
  95. package/src/i18n/uk.json +8 -1
  96. package/src/i18n.ts +20 -32
  97. package/src/index.ts +13 -12
  98. package/src/linker/types.ts +59 -0
  99. package/src/linker.ts +301 -349
  100. package/src/plugins/types.ts +42 -0
  101. package/src/plugins.ts +82 -105
  102. package/src/storage/types.ts +31 -0
  103. package/src/storage.ts +75 -77
  104. package/src/store.ts +125 -31
  105. package/src/types.local.ts +9 -0
  106. package/src/types.ts +148 -5
  107. package/tests/consent.spec.ts +26 -28
  108. package/tests/geo.spec.ts +474 -0
  109. package/tests/linker.spec.ts +68 -74
  110. package/tests/tracking.spec.ts +14 -14
  111. package/build/gtm.d.ts +0 -72
  112. package/build/gtm.d.ts.map +0 -1
  113. package/build/gtm.js +0 -182
  114. package/build/gtm.js.map +0 -1
  115. package/src/gtm.ts +0 -220
package/src/store.ts CHANGED
@@ -1,11 +1,16 @@
1
- import { CONSENT_KEY, DEFAULT_CONSENT_CATEGORIES } from './consts.js'
2
- import { applyConsent, pushConsentDefaults } from './gtm.js'
3
- import { consentLinker, stripConsentLinkParam, writeConsentLanguage } from './linker.js'
4
- import { adoptConsent, adoptConsentLanguage, registerConsentPlugin, startConsentPlugins } from './plugins.js'
5
- import { readConsent, writeConsent } from './storage.js'
1
+ import {
2
+ CONSENT_IDLE_STATE, CONSENT_KEY, CONSENT_STATE_ATTRIBUTE, DEFAULT_CONSENT_CATEGORIES,
3
+ } from './consts.js'
4
+ import { CLOUDFLARE_LOCATOR_ALIAS } from './consts.local.js'
5
+ import { consentModeHelper } from './gtobserver.js'
6
+ import { consentGeoHelper } from './geo.js'
7
+ import { consentLinkHelper } from './linker.js'
8
+ import { consentPluginHelper } from './plugins.js'
9
+ import { consentStorageHelper } from './storage.js'
6
10
  import type {
7
11
  ConsentListener, ConsentOptions, ConsentReason, ConsentRecord, ConsentState, ConsentStore,
8
12
  } from './types.js'
13
+ import type { ConsentGeoVerdict } from './geo/types.js'
9
14
 
10
15
  /**
11
16
  * The consent state of this DOCUMENT.
@@ -17,12 +22,34 @@ import type {
17
22
  */
18
23
  export const makeConsentStore = (): ConsentStore => {
19
24
  const listeners = new Set<ConsentListener>()
20
- let state: ConsentState = { record: null, open: false, reason: null }
25
+ const waiting = new Set<(state: ConsentState) => void>()
26
+ let state: ConsentState = CONSENT_IDLE_STATE
21
27
  let options: ConsentOptions = {}
28
+ /** The one country lookup of this document, while it runs. */
29
+ let lookup: Promise<void> | null = null
30
+ /** What the last lookup decided — a later `init` never locates the same visitor twice. */
31
+ let verdict: ConsentGeoVerdict | null = null
32
+
33
+ /** `<html data-consent>`: what a test waits on and a stylesheet may key on. */
34
+ const mark = (): void => {
35
+ const root = typeof document !== 'undefined' ? document.documentElement : undefined
36
+ if (root == null || typeof root.setAttribute !== 'function') {
37
+ return
38
+ }
39
+ root.setAttribute(CONSENT_STATE_ATTRIBUTE, state.open ? 'open'
40
+ : state.locating != null ? 'locating'
41
+ : state.record != null ? 'decided' : 'idle')
42
+ }
22
43
 
23
44
  const publish = (next: Partial<ConsentState>): void => {
24
45
  state = { ...state, ...next }
46
+ mark()
25
47
  listeners.forEach(listener => listener(state))
48
+ if (state.locating == null && lookup == null && waiting.size > 0) {
49
+ const settled = [...waiting]
50
+ waiting.clear()
51
+ settled.forEach(resolve => resolve(state))
52
+ }
26
53
  }
27
54
 
28
55
  const resolved = (): ConsentOptions & { categories: typeof DEFAULT_CONSENT_CATEGORIES, storageKey: string } => ({
@@ -31,6 +58,37 @@ export const makeConsentStore = (): ConsentStore => {
31
58
  storageKey: options.storageKey ?? CONSENT_KEY,
32
59
  })
33
60
 
61
+ const ask = (): void => {
62
+ publish({ record: null, open: true, reason: 'initial', locating: null })
63
+ }
64
+
65
+ /**
66
+ * What a finished lookup does — unless the visitor got there first: a decision saved meanwhile, or
67
+ * the window opened for a reason of its own (signing in), always wins over the location.
68
+ */
69
+ const settle = (result: ConsentGeoVerdict, pending: ConsentRecord | null): void => {
70
+ verdict = result
71
+ lookup = null
72
+ if (state.record != null || (state.open && state.reason !== 'initial')) {
73
+ publish({ locating: null })
74
+
75
+ return
76
+ }
77
+ if (result === 'auto') {
78
+ const record = consentGeoHelper.automaticRecord(resolved())
79
+ consentStorageHelper.writeConsent(record, options)
80
+ consentModeHelper.applyConsent(record, options)
81
+ publish({ record, open: false, reason: null, locating: null })
82
+
83
+ return
84
+ }
85
+ if (pending != null) {
86
+ // A grant derived somewhere nobody had to be asked does not survive arriving where they must.
87
+ consentStorageHelper.clearConsent(options)
88
+ }
89
+ ask()
90
+ }
91
+
34
92
  const store: ConsentStore = {
35
93
  get: () => state,
36
94
 
@@ -43,52 +101,82 @@ export const makeConsentStore = (): ConsentStore => {
43
101
  init: opts => {
44
102
  options = { ...options, ...opts }
45
103
  // Before anything is read, so a page with no stored answer still declares what is denied.
46
- pushConsentDefaults(options)
104
+ consentModeHelper.pushConsentDefaults(options)
47
105
 
48
106
  if (options.linker != null) {
49
- // Replace-by-alias (`registerConsentPlugin`), so a second `init` (a re-mounted provider,
50
- // StrictMode) never double-installs the click listener — `consentLinker().start` also
51
- // guards itself with its own closured flag, belt and braces.
52
- registerConsentPlugin(consentLinker())
53
- startConsentPlugins(options)
107
+ // Registered once: `start` installs the click listener of THAT instance, and a second
108
+ // instance (a second `init` — a re-mounted provider, StrictMode, `loadGtm`) would install
109
+ // its own beside it.
110
+ if (!consentPluginHelper.consentPlugins().some(plugin => plugin.alias === 'linker')) {
111
+ consentPluginHelper.registerConsentPlugin(consentLinkHelper.consentLinker())
112
+ }
113
+ consentPluginHelper.startConsentPlugins(options)
114
+ }
115
+ if (options.geo?.cloudflare != null && options.geo.cloudflare !== false
116
+ && !consentPluginHelper.consentPlugins().some(plugin => plugin.alias === CLOUDFLARE_LOCATOR_ALIAS)) {
117
+ consentPluginHelper.registerConsentPlugin(consentGeoHelper.cloudflareLocator())
54
118
  }
55
119
 
56
- let record = readConsent(options)
57
- // Only when THIS document has no decision yet — an existing one always wins, exactly as the
58
- // ordinary "ask" path would never overwrite a stored record either.
59
- if (record == null && options.linker != null) {
60
- record = adoptConsent(options)
61
- if (record != null) {
62
- writeConsent(record, options)
120
+ let record = consentStorageHelper.readConsent(options)
121
+ const automatic = consentGeoHelper.autoState(record)
122
+ // An automatic decision past its age is held back — applied nowhere — until the visitor is
123
+ // located again, exactly as the head scripts already skipped it.
124
+ const pending = automatic === 'stale' ? record : null
125
+ if (pending != null) {
126
+ record = null
127
+ }
128
+ // An explicit decision carried from another domain always beats none — and beats an automatic
129
+ // one, which nobody chose. An explicit stored one always wins, as the ordinary "ask" path
130
+ // would never overwrite it either.
131
+ if ((record == null || automatic != null) && options.linker != null) {
132
+ const adopted = consentPluginHelper.adoptConsent(options)
133
+ if (adopted != null) {
134
+ consentStorageHelper.writeConsent(adopted, options)
135
+ record = adopted
63
136
  }
64
137
  }
65
138
  if (options.linker != null) {
66
139
  // The language the link carried, stored at once — the interface language is strictly
67
140
  // necessary storage, so no decision is consulted. A page that stamped the inline head
68
141
  // fragment has already done this and stripped the parameter, which leaves nothing here.
69
- const carried = adoptConsentLanguage(options)
142
+ const carried = consentPluginHelper.adoptConsentLanguage(options)
70
143
  if (carried != null) {
71
- writeConsentLanguage(carried, options)
144
+ consentLinkHelper.writeConsentLanguage(carried, options)
72
145
  }
73
146
  // Always, whether or not anything was adopted — a stale or foreign parameter is exactly as
74
147
  // much noise in the visible URL as an adopted one, and a page that already had its own
75
148
  // decision may still have arrived with one attached.
76
- stripConsentLinkParam(options)
149
+ consentLinkHelper.stripConsentLinkParam(options)
77
150
  }
78
151
 
79
152
  if (record != null) {
80
- applyConsent(record, options)
81
- publish({ record, open: false, reason: null })
153
+ consentModeHelper.applyConsent(record, options)
154
+ publish({ record, open: false, reason: null, locating: null })
82
155
 
83
156
  return
84
157
  }
85
- publish({ record: null, open: true, reason: 'initial' })
158
+ if (lookup != null) {
159
+ return
160
+ }
161
+ // The window is already open for a reason of its own (signing in): the visitor is answering.
162
+ if (state.open && state.reason !== 'initial') {
163
+ return
164
+ }
165
+ if (verdict === 'ask' || !consentGeoHelper.enabled(options)) {
166
+ ask()
167
+
168
+ return
169
+ }
170
+ // A first visit waits behind the spinner; a re-check runs silently, nothing applied meanwhile.
171
+ publish({ record: null, open: false, reason: null, locating: pending != null ? 'recheck' : 'first' })
172
+ lookup = consentGeoHelper.decide(options).then(result => settle(result, pending))
86
173
  },
87
174
 
88
175
  save: record => {
89
- writeConsent(record, options)
90
- applyConsent(record, options)
91
- publish({ record, open: false, reason: null })
176
+ const { auto: _auto, ...explicit } = record
177
+ consentStorageHelper.writeConsent(explicit, options)
178
+ consentModeHelper.applyConsent(explicit, options)
179
+ publish({ record: explicit, open: false, reason: null, locating: null })
92
180
  },
93
181
 
94
182
  acceptAll: () => {
@@ -98,7 +186,9 @@ export const makeConsentStore = (): ConsentStore => {
98
186
  store.save(record)
99
187
  },
100
188
 
101
- open: (reason?: ConsentReason) => { publish({ open: true, reason: reason ?? 'reopen' }) },
189
+ open: (reason?: ConsentReason) => {
190
+ publish({ open: true, reason: reason ?? 'reopen', locating: null })
191
+ },
102
192
 
103
193
  close: () => { publish({ open: false, reason: null }) },
104
194
 
@@ -112,6 +202,10 @@ export const makeConsentStore = (): ConsentStore => {
112
202
  },
113
203
 
114
204
  options: resolved,
205
+
206
+ settled: () => state.locating == null && lookup == null
207
+ ? Promise.resolve(state)
208
+ : new Promise(resolve => { waiting.add(resolve) }),
115
209
  }
116
210
 
117
211
  return store
@@ -119,8 +213,8 @@ export const makeConsentStore = (): ConsentStore => {
119
213
 
120
214
  export const consentStore: ConsentStore = makeConsentStore()
121
215
 
122
- /** Open the preferences dialog from anywhere — a footer link, a policy page, a login gate. */
216
+ /** @deprecated compat:factory-refactor — use `consentStore.open(…)` */
123
217
  export const openConsent = (reason?: ConsentReason): void => consentStore.open(reason)
124
218
 
125
- /** Whether a category is granted, for the callers that are not components. */
219
+ /** @deprecated compat:factory-refactor — use `consentStore.granted(…)` */
126
220
  export const isConsented = (key: string): boolean => consentStore.granted(key)
@@ -0,0 +1,9 @@
1
+ export interface ConsentWindow {
2
+ dataLayer?: unknown[]
3
+ [flag: string]: unknown
4
+ }
5
+
6
+ /** The one navigator field the automatic decision reads: Global Privacy Control. */
7
+ export interface ConsentNavigator {
8
+ globalPrivacyControl?: boolean
9
+ }
package/src/types.ts CHANGED
@@ -5,6 +5,8 @@
5
5
  * manager simply never sees the update, and the only symptom is analytics that stay switched off
6
6
  * for everyone who agreed to them.
7
7
  */
8
+ import { CONSENT_LINK_VERSION } from './consts.local.js'
9
+
8
10
  export type ConsentSignal =
9
11
  | 'ad_storage' | 'ad_user_data' | 'ad_personalization'
10
12
  | 'analytics_storage' | 'functionality_storage'
@@ -68,12 +70,60 @@ export interface ConsentService {
68
70
  privacyHref?: string
69
71
  }
70
72
 
71
- /** `v` is the schema version; every other key is a category key. */
73
+ /**
74
+ * `v` is the schema version and `auto` marks an automatic decision; every other key is a category
75
+ * key — so neither `v` nor `auto` may ever name a category.
76
+ */
72
77
  export interface ConsentRecord {
73
78
  v?: number
79
+ /**
80
+ * Unix seconds at which this record was DERIVED rather than chosen: the visitor was located
81
+ * outside every consent-requiring country (`ConsentGeoOptions`) and nobody asked them. Present
82
+ * only on an automatic decision — an explicit save drops it. An automatic decision is trusted for
83
+ * `CONSENT_AUTO_MAX_AGE` seconds and then counts as undecided until the visitor is located again,
84
+ * so a traveller who crosses into a consent country is asked there instead of carrying a grant
85
+ * nobody gave. It never travels on a linker link as the visitor's own choice.
86
+ */
87
+ auto?: number
74
88
  [category: string]: boolean | number | undefined
75
89
  }
76
90
 
91
+ /** Where a locator places the visitor. */
92
+ export interface ConsentGeoLocation {
93
+ /** ISO 3166-1 alpha-2, upper case — `PL`, `US`. Anything else counts as "not located". */
94
+ country: string
95
+ }
96
+
97
+ export interface ConsentCloudflareOptions {
98
+ /** The same-origin path of Cloudflare's trace endpoint. Defaults to `CONSENT_TRACE_PATH`. */
99
+ path?: string
100
+ }
101
+
102
+ /**
103
+ * Ask only where the law requires it: locate the visitor first, and decide automatically wherever
104
+ * no consent is required.
105
+ *
106
+ * Its presence turns the gate on. The country comes from the registered locators
107
+ * (`ConsentGeoPlugin`, highest priority first, the first usable answer wins); `cloudflare` adds the
108
+ * built-in one. A visitor nobody can locate — no locator, every one of them failing, an unusable
109
+ * code, `timeout` passing — is asked, exactly as without the gate.
110
+ */
111
+ export interface ConsentGeoOptions {
112
+ /**
113
+ * The built-in locator: a same-origin GET of Cloudflare's `/cdn-cgi/trace`, reading `loc=`. It
114
+ * answers only where the host is proxied by Cloudflare — anywhere else (a local dev server, a
115
+ * self-hosted export) it fails, and the visitor is asked.
116
+ */
117
+ cloudflare?: boolean | ConsentCloudflareOptions
118
+ /** The countries where consent is asked. Defaults to `CONSENT_REQUIRED_COUNTRIES`. */
119
+ countries?: readonly string[]
120
+ /** How long locating may take before the visitor is asked anyway, in ms. `CONSENT_GEO_TIMEOUT`. */
121
+ timeout?: number
122
+ }
123
+
124
+ /** Which lookup is running: the first one (the page waits behind a spinner) or a silent re-check. */
125
+ export type ConsentLocating = 'first' | 'recheck'
126
+
77
127
  export interface ConsentOptions {
78
128
  categories?: ConsentCategory[]
79
129
  storageKey?: string
@@ -91,6 +141,8 @@ export interface ConsentOptions {
91
141
  * `consentLinker`/`ConsentPlugin` in `./plugins.js` and `./linker.js`.
92
142
  */
93
143
  linker?: ConsentLinkerOptions
144
+ /** Ask only in consent-requiring countries — see `ConsentGeoOptions`. Off when absent. */
145
+ geo?: ConsentGeoOptions
94
146
  }
95
147
 
96
148
  export interface ConsentLinkerOptions {
@@ -130,27 +182,118 @@ export interface ConsentLinkerLanguage {
130
182
  storageKey?: string
131
183
  }
132
184
 
133
- /** Why the dialog is open. `login` is what the sign-in precondition raises. */
134
- export type ConsentReason = 'initial' | 'reopen' | 'login' | string
185
+ /**
186
+ * Why the consent UI is open. `initial` is the first ask (the bar, in bar mode); `preferences` is the
187
+ * bar's own "Cookie preferences"; `reopen` a footer link or the corner button; `login` what the
188
+ * sign-in precondition raises. Every reason but `initial` opens the preferences window.
189
+ */
190
+ export type ConsentReason = 'initial' | 'reopen' | 'login' | 'preferences' | string
135
191
 
136
192
  export interface ConsentState {
137
193
  record: ConsentRecord | null
138
194
  open: boolean
139
195
  reason: ConsentReason | null
196
+ /** A country lookup is running (`ConsentGeoOptions`); `null` once it settled, or when there is none. */
197
+ locating: ConsentLocating | null
140
198
  }
141
199
 
142
- export type ConsentListener = (state: ConsentState) => void
200
+ export interface ConsentListener { (state: ConsentState): void }
143
201
 
144
202
  export interface ConsentStore {
145
203
  get: () => ConsentState
146
204
  subscribe: (listener: ConsentListener) => () => void
147
- /** Push the defaults, load and migrate any stored record, apply it, and open when there is none. */
205
+ /**
206
+ * Push the defaults, load and migrate any stored record, apply it — and, when there is none,
207
+ * either ask at once or, with `geo` set, locate the visitor first and ask or decide automatically.
208
+ * Synchronous: a lookup runs in the background, `locating` says so, and `settled()` waits for it.
209
+ */
148
210
  init: (opts?: ConsentOptions) => void
211
+ /** Record an explicit decision. Always explicit: an `auto` key on `record` is dropped. */
149
212
  save: (record: ConsentRecord) => void
150
213
  acceptAll: () => void
214
+ /** Open the consent UI from anywhere — a footer link, a policy page, a login gate. */
151
215
  open: (reason?: ConsentReason) => void
152
216
  close: () => void
153
217
  /** Imperative reader, for the callers that are not React. */
154
218
  granted: (key: string) => boolean
155
219
  options: () => ConsentOptions & { categories: ConsentCategory[], storageKey: string }
220
+ /** The state once no country lookup is running — at once when none is. */
221
+ settled: () => Promise<ConsentState>
222
+ }
223
+
224
+ /** A language the dialog's built-in copy is carried in (`CONSENT_LOCALES`). */
225
+ export type ConsentLocale = 'en' | 'pl' | 'ru' | 'be' | 'uk' | 'es' | 'de' | 'fr'
226
+
227
+ /** The decoded, not-yet-validated payload a decorated link's parameter carries. */
228
+ export interface ConsentLinkPayload {
229
+ v: typeof CONSENT_LINK_VERSION
230
+ /** One entry per OPTIONAL category this site knows, `1` granted / `0` denied. */
231
+ c: Record<string, 0 | 1>
232
+ /** Unix seconds the link was decorated at. */
233
+ t: number
234
+ /**
235
+ * The interface language of the page the link was on (a BCP 47 tag, lower-cased) — present only
236
+ * when the sender has `linker.language` set and its `<html lang>` names one. Optional on the
237
+ * wire, so a receiver that predates it reads the same `v: 2` payload and ignores the field.
238
+ */
239
+ l?: string
240
+ }
241
+
242
+ /**
243
+ * An extension seam for `@owlmeans/consent`: something that reacts to this document's consent
244
+ * decision, or supplies one from elsewhere, without the core store knowing anything about where
245
+ * that decision came from.
246
+ *
247
+ * `consentLinker` (`./linker.js`) is the one built-in plugin — cross-domain consent through
248
+ * decorated links — but nothing here is specific to it. A registry rather than a fixed option on
249
+ * `ConsentOptions` because the store has zero dependencies today and stays that way: a plugin is
250
+ * opt-in code an application imports for its own reason, never a bundled feature this package
251
+ * pulls in whether or not anything uses it.
252
+ */
253
+ export interface ConsentPlugin {
254
+ /** Stable identity. Registering the same alias again REPLACES the earlier plugin. */
255
+ alias: string
256
+ /** Run order when more than one plugin implements the same hook. Higher first. Defaults to 0. */
257
+ priority?: number
258
+ /** Installed once, the first time `consentStore.init` runs with this plugin registered. */
259
+ start?: (opts: ConsentOptions) => void
260
+ /**
261
+ * Offer a decision from outside this document's own storage — e.g. one carried on the URL that
262
+ * led here. Returns a full record to adopt, or `null` to defer to the next plugin / the ordinary
263
+ * "ask" path. Never called once a stored record already exists.
264
+ */
265
+ adopt?: (opts: ConsentOptions) => ConsentRecord | null
266
+ /**
267
+ * Offer an interface language from outside this document's own storage — e.g. the one the page
268
+ * that led here was read in. Returns a language the application supports, or `null` to keep
269
+ * choosing its own. Only a CANDIDATE: it says what the link carried, and `writeConsentLanguage`
270
+ * stores it — unconditionally, since the interface language is strictly necessary storage.
271
+ */
272
+ adoptLanguage?: (opts: ConsentOptions) => string | null
273
+ /**
274
+ * Rewrite an outgoing URL to carry this document's current decision — and its language — or
275
+ * `null` to leave it untouched (a foreign host, a link that opts out, nothing to carry yet).
276
+ * `record` is `null` while the visitor has not decided: a plugin that also carries something
277
+ * else (the language) still has a reason to decorate then.
278
+ */
279
+ decorate?: (url: URL, record: ConsentRecord | null, opts: ConsentOptions) => URL | null
280
+ /** Extra domains this decision is understood to apply to, for disclosure — never deduplicated here. */
281
+ domains?: (opts: ConsentOptions) => string[]
282
+ /**
283
+ * Find the visitor's country, or throw when it cannot tell. Asked only while `opts.geo` is set
284
+ * and nothing is stored; locators run highest priority first and the first usable country wins,
285
+ * so a throw (or an unusable code) hands over to the next one — the built-in Cloudflare locator
286
+ * sits at the very bottom.
287
+ */
288
+ locate?: (opts: ConsentOptions) => Promise<ConsentGeoLocation>
289
+ }
290
+
291
+ /**
292
+ * The country-detection plugin: one async method that resolves the visitor's country or throws.
293
+ * Register it with `consentPluginHelper.registerConsentPlugin` — or, in an OwlMeans application,
294
+ * with `@owlmeans/web-panel/consent`'s `appendConsentGeoPlugin(context, plugin)` while the context is
295
+ * configured.
296
+ */
297
+ export interface ConsentGeoPlugin extends ConsentPlugin {
298
+ locate: (opts: ConsentOptions) => Promise<ConsentGeoLocation>
156
299
  }
@@ -1,14 +1,12 @@
1
1
  import { describe, test, expect, beforeEach } from 'bun:test'
2
2
  import {
3
3
  CONSENT_ANALYTICS, CONSENT_ESSENTIAL, CONSENT_EVENT, CONSENT_KEY, CONSENT_LOCALES,
4
- CONSENT_MARKETING, CONSENT_SCHEMA_VERSION, DEFAULT_CONSENT_CATEGORIES,
4
+ CONSENT_MARKETING, CONSENT_SCHEMA_VERSION, DEFAULT_CONSENT_CATEGORIES, DEFAULT_CONSENT_MESSAGES,
5
5
  } from '../src/consts.js'
6
- import { migrateConsent, readConsent, writeConsent, clearConsent } from '../src/storage.js'
7
- import { applyConsent, consentDefaults, consentUpdate, consentBootstrapScript } from '../src/gtm.js'
6
+ import { consentStorageHelper } from '../src/storage.js'
7
+ import { consentModeHelper } from '../src/gtobserver.js'
8
8
  import { makeConsentStore } from '../src/store.js'
9
- import {
10
- DEFAULT_CONSENT_MESSAGES, defaultConsentTranslate, interpolate, normalizeLocale,
11
- } from '../src/i18n.js'
9
+ import { consentI18nHelper } from '../src/i18n.js'
12
10
  import type { ConsentCategory } from '../src/types.js'
13
11
 
14
12
  /** A minimal browser: just the two stores this package writes to. */
@@ -41,33 +39,33 @@ beforeEach(() => {
41
39
 
42
40
  describe('storage', () => {
43
41
  test('a record round-trips through localStorage', () => {
44
- writeConsent({ [CONSENT_ANALYTICS]: true, [CONSENT_MARKETING]: false })
42
+ consentStorageHelper.writeConsent({ [CONSENT_ANALYTICS]: true, [CONSENT_MARKETING]: false })
45
43
 
46
- expect(readConsent()?.[CONSENT_ANALYTICS]).toBe(true)
47
- expect(readConsent()?.[CONSENT_MARKETING]).toBe(false)
44
+ expect(consentStorageHelper.readConsent()?.[CONSENT_ANALYTICS]).toBe(true)
45
+ expect(consentStorageHelper.readConsent()?.[CONSENT_MARKETING]).toBe(false)
48
46
  })
49
47
 
50
48
  test('the cookie answers when localStorage does not', () => {
51
- writeConsent({ [CONSENT_ANALYTICS]: true })
49
+ consentStorageHelper.writeConsent({ [CONSENT_ANALYTICS]: true })
52
50
  env.store.clear()
53
51
 
54
- expect(readConsent()?.[CONSENT_ANALYTICS]).toBe(true)
52
+ expect(consentStorageHelper.readConsent()?.[CONSENT_ANALYTICS]).toBe(true)
55
53
  })
56
54
 
57
55
  test('the cookie is path-wide, SameSite=Lax and long-lived', () => {
58
- writeConsent({ [CONSENT_ANALYTICS]: true })
56
+ consentStorageHelper.writeConsent({ [CONSENT_ANALYTICS]: true })
59
57
 
60
58
  expect(env.raw()).toContain(`${CONSENT_KEY}=`)
61
59
  // The cookie fragment the browser stub keeps is just the pair, so the attributes are asserted
62
60
  // on what `writeConsent` produced rather than on what a real browser would retain.
63
- expect(readConsent()).not.toBeNull()
61
+ expect(consentStorageHelper.readConsent()).not.toBeNull()
64
62
  })
65
63
 
66
64
  test('a legacy record — the owlmeans.com visitor — migrates instead of being discarded', () => {
67
65
  // Exactly what the previous widget wrote: two categories, no version, essential implicit.
68
66
  const legacy = { analytics: true, marketing: false }
69
67
 
70
- const migrated = migrateConsent(legacy)
68
+ const migrated = consentStorageHelper.migrateConsent(legacy)
71
69
 
72
70
  expect(migrated?.[CONSENT_ESSENTIAL]).toBe(true)
73
71
  expect(migrated?.[CONSENT_ANALYTICS]).toBe(true)
@@ -77,19 +75,19 @@ describe('storage', () => {
77
75
  test('a current record is returned unchanged', () => {
78
76
  const current = { essential: true, analytics: false, v: CONSENT_SCHEMA_VERSION }
79
77
 
80
- expect(migrateConsent(current)).toBe(current)
78
+ expect(consentStorageHelper.migrateConsent(current)).toBe(current)
81
79
  })
82
80
 
83
81
  test('nothing stored reads as null', () => {
84
- clearConsent()
82
+ consentStorageHelper.clearConsent()
85
83
 
86
- expect(readConsent()).toBeNull()
84
+ expect(consentStorageHelper.readConsent()).toBeNull()
87
85
  })
88
86
  })
89
87
 
90
88
  describe('consent mode signals', () => {
91
89
  test('defaults deny everything except security storage', () => {
92
- const defaults = consentDefaults()
90
+ const defaults = consentModeHelper.consentDefaults()
93
91
 
94
92
  expect(defaults.analytics_storage).toBe('denied')
95
93
  expect(defaults.ad_storage).toBe('denied')
@@ -99,7 +97,7 @@ describe('consent mode signals', () => {
99
97
  })
100
98
 
101
99
  test('an update maps each category onto the signals it drives', () => {
102
- const update = consentUpdate({ [CONSENT_ANALYTICS]: true, [CONSENT_MARKETING]: false })
100
+ const update = consentModeHelper.consentUpdate({ [CONSENT_ANALYTICS]: true, [CONSENT_MARKETING]: false })
103
101
 
104
102
  expect(update.analytics_storage).toBe('granted')
105
103
  expect(update.ad_storage).toBe('denied')
@@ -112,8 +110,8 @@ describe('consent mode signals', () => {
112
110
  { key: 'ads', labelKey: 'a', descriptionKey: 'b', signals: ['ad_storage'] },
113
111
  ]
114
112
 
115
- expect(Object.keys(consentDefaults(categories))).toEqual(['ad_storage'])
116
- expect(consentUpdate({ ads: true }, categories)).toEqual({ ad_storage: 'granted' })
113
+ expect(Object.keys(consentModeHelper.consentDefaults(categories))).toEqual(['ad_storage'])
114
+ expect(consentModeHelper.consentUpdate({ ads: true }, categories)).toEqual({ ad_storage: 'granted' })
117
115
  })
118
116
  })
119
117
 
@@ -124,7 +122,7 @@ describe('applyConsent dispatches a DOM event', () => {
124
122
  ;(globalThis as any).addEventListener(CONSENT_EVENT, listener)
125
123
 
126
124
  try {
127
- applyConsent({ [CONSENT_ANALYTICS]: true })
125
+ consentModeHelper.applyConsent({ [CONSENT_ANALYTICS]: true })
128
126
 
129
127
  expect(seen).toEqual([{ record: { [CONSENT_ANALYTICS]: true } }])
130
128
  } finally {
@@ -138,7 +136,7 @@ describe('applyConsent dispatches a DOM event', () => {
138
136
  ;(globalThis as any).addEventListener(CONSENT_EVENT, listener)
139
137
 
140
138
  try {
141
- applyConsent({ [CONSENT_ANALYTICS]: true }, { silent: true })
139
+ consentModeHelper.applyConsent({ [CONSENT_ANALYTICS]: true }, { silent: true })
142
140
 
143
141
  expect(seen).toEqual([])
144
142
  } finally {
@@ -148,7 +146,7 @@ describe('applyConsent dispatches a DOM event', () => {
148
146
  })
149
147
 
150
148
  describe('the bootstrap script', () => {
151
- const script = consentBootstrapScript()
149
+ const script = consentModeHelper.consentBootstrapScript()
152
150
 
153
151
  test('declares the defaults before anything else can', () => {
154
152
  expect(script).toContain("'consent','default'")
@@ -242,15 +240,15 @@ describe('the built-in translations', () => {
242
240
  })
243
241
 
244
242
  test('an unknown locale falls back to English rather than to a raw key', () => {
245
- expect(normalizeLocale('kl')).toBe('en')
246
- expect(defaultConsentTranslate('kl')('title', 'fallback')).toBe('Cookie Preferences')
243
+ expect(consentI18nHelper.normalizeLocale('kl')).toBe('en')
244
+ expect(consentI18nHelper.defaultConsentTranslate('kl')('title', 'fallback')).toBe('Cookie Preferences')
247
245
  })
248
246
 
249
247
  test('a key nobody translated returns the supplied default', () => {
250
- expect(defaultConsentTranslate('en')('nope', 'the default')).toBe('the default')
248
+ expect(consentI18nHelper.defaultConsentTranslate('en')('nope', 'the default')).toBe('the default')
251
249
  })
252
250
 
253
251
  test('placeholders interpolate', () => {
254
- expect(interpolate('kept for {{days}} days', { days: 365 })).toBe('kept for 365 days')
252
+ expect(consentI18nHelper.interpolate('kept for {{days}} days', { days: 365 })).toBe('kept for 365 days')
255
253
  })
256
254
  })