@codefast/tracking 0.5.0-canary.5 → 1.0.0-canary.7

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 (276) hide show
  1. package/CHANGELOG.md +220 -0
  2. package/README.md +176 -126
  3. package/dist/adapters/tanstack-start.d.ts +80 -0
  4. package/dist/adapters/tanstack-start.d.ts.map +1 -0
  5. package/dist/adapters/tanstack-start.js +74 -0
  6. package/dist/adapters/tanstack-start.js.map +1 -0
  7. package/dist/client/ad-framework-consent.d.ts +62 -0
  8. package/dist/client/ad-framework-consent.d.ts.map +1 -0
  9. package/dist/client/ad-framework-consent.js +40 -0
  10. package/dist/client/ad-framework-consent.js.map +1 -0
  11. package/dist/client/browser-cookie.d.ts +9 -0
  12. package/dist/client/browser-cookie.d.ts.map +1 -0
  13. package/dist/client/browser-cookie.js +15 -0
  14. package/dist/client/browser-cookie.js.map +1 -0
  15. package/dist/client/consent-runtime.d.ts +53 -0
  16. package/dist/client/consent-runtime.d.ts.map +1 -0
  17. package/dist/client/consent-runtime.js +33 -0
  18. package/dist/client/consent-runtime.js.map +1 -0
  19. package/dist/client/{consent-storage.d.mts → consent-storage.d.ts} +8 -6
  20. package/dist/client/consent-storage.d.ts.map +1 -0
  21. package/dist/client/consent-storage.js +109 -0
  22. package/dist/client/consent-storage.js.map +1 -0
  23. package/dist/client/consent-withdrawal.d.ts +18 -0
  24. package/dist/client/consent-withdrawal.d.ts.map +1 -0
  25. package/dist/client/consent-withdrawal.js +17 -0
  26. package/dist/client/consent-withdrawal.js.map +1 -0
  27. package/dist/client/cookie-anonymous-id.d.ts +41 -0
  28. package/dist/client/cookie-anonymous-id.d.ts.map +1 -0
  29. package/dist/client/cookie-anonymous-id.js +65 -0
  30. package/dist/client/cookie-anonymous-id.js.map +1 -0
  31. package/dist/client/create-client-tracker.d.ts +60 -0
  32. package/dist/client/create-client-tracker.d.ts.map +1 -0
  33. package/dist/client/create-client-tracker.js +91 -0
  34. package/dist/client/create-client-tracker.js.map +1 -0
  35. package/dist/client/gpc.d.ts +10 -0
  36. package/dist/client/gpc.d.ts.map +1 -0
  37. package/dist/client/gpc.js +12 -0
  38. package/dist/client/gpc.js.map +1 -0
  39. package/dist/client/initial-consent-store.d.ts +63 -0
  40. package/dist/client/initial-consent-store.d.ts.map +1 -0
  41. package/dist/client/initial-consent-store.js +138 -0
  42. package/dist/client/initial-consent-store.js.map +1 -0
  43. package/dist/client/is-analytics-allowed.d.ts +26 -0
  44. package/dist/client/is-analytics-allowed.d.ts.map +1 -0
  45. package/dist/client/is-analytics-allowed.js +22 -0
  46. package/dist/client/is-analytics-allowed.js.map +1 -0
  47. package/dist/client/server-persisted-anonymous-id.d.ts +30 -0
  48. package/dist/client/server-persisted-anonymous-id.d.ts.map +1 -0
  49. package/dist/client/server-persisted-anonymous-id.js +72 -0
  50. package/dist/client/server-persisted-anonymous-id.js.map +1 -0
  51. package/dist/core/consent-config.d.ts +24 -0
  52. package/dist/core/consent-config.d.ts.map +1 -0
  53. package/dist/core/consent-config.js +9 -0
  54. package/dist/core/consent-config.js.map +1 -0
  55. package/dist/core/consent-receipt.d.ts +71 -0
  56. package/dist/core/consent-receipt.d.ts.map +1 -0
  57. package/dist/core/consent-receipt.js +39 -0
  58. package/dist/core/consent-receipt.js.map +1 -0
  59. package/dist/core/consent.d.ts +182 -0
  60. package/dist/core/consent.d.ts.map +1 -0
  61. package/dist/core/consent.js +159 -0
  62. package/dist/core/consent.js.map +1 -0
  63. package/dist/core/cookie.d.ts +15 -0
  64. package/dist/core/cookie.d.ts.map +1 -0
  65. package/dist/core/cookie.js +26 -0
  66. package/dist/core/cookie.js.map +1 -0
  67. package/dist/core/destination.d.ts +29 -0
  68. package/dist/core/destination.d.ts.map +1 -0
  69. package/dist/core/destination.js +2 -0
  70. package/dist/core/destination.js.map +1 -0
  71. package/dist/core/event-catalog.d.ts +35 -0
  72. package/dist/core/event-catalog.d.ts.map +1 -0
  73. package/dist/core/event-catalog.js +30 -0
  74. package/dist/core/event-catalog.js.map +1 -0
  75. package/dist/core/event-id.d.ts +7 -0
  76. package/dist/core/event-id.d.ts.map +1 -0
  77. package/dist/core/event-id.js +9 -0
  78. package/dist/core/event-id.js.map +1 -0
  79. package/dist/core/tracked-event.d.ts +15 -0
  80. package/dist/core/tracked-event.d.ts.map +1 -0
  81. package/dist/core/tracked-event.js +2 -0
  82. package/dist/core/tracked-event.js.map +1 -0
  83. package/dist/destinations/ad-consent.d.ts +23 -0
  84. package/dist/destinations/ad-consent.d.ts.map +1 -0
  85. package/dist/destinations/ad-consent.js +12 -0
  86. package/dist/destinations/ad-consent.js.map +1 -0
  87. package/dist/destinations/google-analytics.d.ts +148 -0
  88. package/dist/destinations/google-analytics.d.ts.map +1 -0
  89. package/dist/destinations/google-analytics.js +156 -0
  90. package/dist/destinations/google-analytics.js.map +1 -0
  91. package/dist/destinations/google-consent.d.ts +62 -0
  92. package/dist/destinations/google-consent.d.ts.map +1 -0
  93. package/dist/destinations/google-consent.js +109 -0
  94. package/dist/destinations/google-consent.js.map +1 -0
  95. package/dist/destinations/meta.d.ts +63 -0
  96. package/dist/destinations/meta.d.ts.map +1 -0
  97. package/dist/destinations/meta.js +44 -0
  98. package/dist/destinations/meta.js.map +1 -0
  99. package/dist/destinations/microsoft-uet.d.ts +56 -0
  100. package/dist/destinations/microsoft-uet.d.ts.map +1 -0
  101. package/dist/destinations/microsoft-uet.js +41 -0
  102. package/dist/destinations/microsoft-uet.js.map +1 -0
  103. package/dist/destinations/shared.d.ts +18 -0
  104. package/dist/destinations/shared.d.ts.map +1 -0
  105. package/dist/destinations/shared.js +19 -0
  106. package/dist/destinations/shared.js.map +1 -0
  107. package/dist/destinations/tiktok.d.ts +55 -0
  108. package/dist/destinations/tiktok.d.ts.map +1 -0
  109. package/dist/destinations/tiktok.js +40 -0
  110. package/dist/destinations/tiktok.js.map +1 -0
  111. package/dist/destinations/vercel-analytics.d.ts +25 -0
  112. package/dist/destinations/vercel-analytics.d.ts.map +1 -0
  113. package/dist/destinations/vercel-analytics.js +23 -0
  114. package/dist/destinations/vercel-analytics.js.map +1 -0
  115. package/dist/index.d.ts +64 -0
  116. package/dist/index.d.ts.map +1 -0
  117. package/dist/index.js +41 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/react/consent-banner.d.ts +155 -0
  120. package/dist/react/consent-banner.d.ts.map +1 -0
  121. package/dist/react/consent-banner.js +171 -0
  122. package/dist/react/consent-banner.js.map +1 -0
  123. package/dist/react/gtag-consent-bootstrap.d.ts +17 -0
  124. package/dist/react/gtag-consent-bootstrap.d.ts.map +1 -0
  125. package/dist/react/gtag-consent-bootstrap.js +22 -0
  126. package/dist/react/gtag-consent-bootstrap.js.map +1 -0
  127. package/dist/react/use-consent.d.ts +42 -0
  128. package/dist/react/use-consent.d.ts.map +1 -0
  129. package/dist/react/use-consent.js +60 -0
  130. package/dist/react/use-consent.js.map +1 -0
  131. package/dist/react/use-google-consent-sync.d.ts +20 -0
  132. package/dist/react/use-google-consent-sync.d.ts.map +1 -0
  133. package/dist/react/use-google-consent-sync.js +22 -0
  134. package/dist/react/use-google-consent-sync.js.map +1 -0
  135. package/dist/react/use-initial-consent.d.ts +11 -0
  136. package/dist/react/use-initial-consent.d.ts.map +1 -0
  137. package/dist/react/use-initial-consent.js +18 -0
  138. package/dist/react/use-initial-consent.js.map +1 -0
  139. package/dist/server/anonymous-id-cookie.d.ts +67 -0
  140. package/dist/server/anonymous-id-cookie.d.ts.map +1 -0
  141. package/dist/server/anonymous-id-cookie.js +71 -0
  142. package/dist/server/anonymous-id-cookie.js.map +1 -0
  143. package/dist/server/consent-receipt-store.d.ts +56 -0
  144. package/dist/server/consent-receipt-store.d.ts.map +1 -0
  145. package/dist/server/consent-receipt-store.js +44 -0
  146. package/dist/server/consent-receipt-store.js.map +1 -0
  147. package/dist/server/consent-receipt.d.ts +41 -0
  148. package/dist/server/consent-receipt.d.ts.map +1 -0
  149. package/dist/server/consent-receipt.js +60 -0
  150. package/dist/server/consent-receipt.js.map +1 -0
  151. package/dist/server/initial-consent.d.ts +26 -0
  152. package/dist/server/initial-consent.d.ts.map +1 -0
  153. package/dist/server/initial-consent.js +31 -0
  154. package/dist/server/initial-consent.js.map +1 -0
  155. package/dist/server/measurement-protocol.d.ts +69 -0
  156. package/dist/server/measurement-protocol.d.ts.map +1 -0
  157. package/dist/server/measurement-protocol.js +61 -0
  158. package/dist/server/measurement-protocol.js.map +1 -0
  159. package/dist/server/region.d.ts +27 -0
  160. package/dist/server/region.d.ts.map +1 -0
  161. package/dist/server/region.js +69 -0
  162. package/dist/server/region.js.map +1 -0
  163. package/dist/server/user-deletion.d.ts +64 -0
  164. package/dist/server/user-deletion.d.ts.map +1 -0
  165. package/dist/server/user-deletion.js +54 -0
  166. package/dist/server/user-deletion.js.map +1 -0
  167. package/dist/tooling/import-protection.d.ts +19 -0
  168. package/dist/tooling/import-protection.d.ts.map +1 -0
  169. package/dist/tooling/import-protection.js +23 -0
  170. package/dist/tooling/import-protection.js.map +1 -0
  171. package/package.json +203 -108
  172. package/src/adapters/tanstack-start.ts +135 -0
  173. package/src/client/ad-framework-consent.ts +86 -0
  174. package/src/client/browser-cookie.ts +16 -0
  175. package/src/client/consent-runtime.ts +80 -0
  176. package/src/client/consent-storage.ts +31 -1
  177. package/src/client/consent-withdrawal.ts +32 -0
  178. package/src/client/cookie-anonymous-id.ts +112 -0
  179. package/src/client/create-client-tracker.ts +128 -86
  180. package/src/client/gpc.ts +4 -2
  181. package/src/client/initial-consent-store.ts +218 -0
  182. package/src/client/is-analytics-allowed.ts +43 -0
  183. package/src/client/server-persisted-anonymous-id.ts +102 -0
  184. package/src/core/consent-config.ts +27 -0
  185. package/src/core/consent-receipt.ts +115 -0
  186. package/src/core/consent.ts +164 -10
  187. package/src/core/cookie.ts +29 -0
  188. package/src/core/destination.ts +17 -8
  189. package/src/core/event-catalog.ts +29 -15
  190. package/src/core/event-id.ts +1 -2
  191. package/src/core/tracked-event.ts +4 -4
  192. package/src/destinations/ad-consent.ts +29 -0
  193. package/src/destinations/google-analytics.ts +196 -133
  194. package/src/destinations/google-consent.ts +164 -0
  195. package/src/destinations/meta.ts +96 -0
  196. package/src/destinations/microsoft-uet.ts +86 -0
  197. package/src/destinations/shared.ts +45 -0
  198. package/src/destinations/tiktok.ts +85 -0
  199. package/src/destinations/vercel-analytics.ts +23 -24
  200. package/src/index.ts +165 -8
  201. package/src/react/consent-banner.tsx +40 -14
  202. package/src/react/gtag-consent-bootstrap.tsx +48 -0
  203. package/src/react/use-consent.ts +53 -45
  204. package/src/react/use-google-consent-sync.ts +38 -0
  205. package/src/react/use-initial-consent.ts +22 -0
  206. package/src/server/anonymous-id-cookie.ts +103 -0
  207. package/src/server/consent-receipt-store.ts +79 -0
  208. package/src/server/consent-receipt.ts +89 -0
  209. package/src/server/initial-consent.ts +46 -0
  210. package/src/server/measurement-protocol.ts +114 -0
  211. package/src/server/region.ts +22 -15
  212. package/src/server/user-deletion.ts +93 -0
  213. package/src/tooling/import-protection.ts +22 -0
  214. package/dist/client/consent-storage.mjs +0 -63
  215. package/dist/client/create-client-tracker.d.mts +0 -37
  216. package/dist/client/create-client-tracker.mjs +0 -66
  217. package/dist/client/gpc.d.mts +0 -10
  218. package/dist/client/gpc.mjs +0 -12
  219. package/dist/client/index.d.mts +0 -8
  220. package/dist/client/index.mjs +0 -8
  221. package/dist/client/lifecycle.d.mts +0 -23
  222. package/dist/client/lifecycle.mjs +0 -30
  223. package/dist/client/local-storage.d.mts +0 -13
  224. package/dist/client/local-storage.mjs +0 -31
  225. package/dist/client/queue.d.mts +0 -49
  226. package/dist/client/queue.mjs +0 -71
  227. package/dist/client/router.d.mts +0 -30
  228. package/dist/client/router.mjs +0 -17
  229. package/dist/core/consent.d.mts +0 -89
  230. package/dist/core/consent.mjs +0 -57
  231. package/dist/core/destination.d.mts +0 -23
  232. package/dist/core/destination.mjs +0 -1
  233. package/dist/core/event-catalog.d.mts +0 -36
  234. package/dist/core/event-catalog.mjs +0 -12
  235. package/dist/core/event-id.d.mts +0 -10
  236. package/dist/core/event-id.mjs +0 -12
  237. package/dist/core/index.d.mts +0 -6
  238. package/dist/core/index.mjs +0 -4
  239. package/dist/core/tracked-event.d.mts +0 -17
  240. package/dist/core/tracked-event.mjs +0 -1
  241. package/dist/destinations/ga4-measurement-protocol.d.mts +0 -54
  242. package/dist/destinations/ga4-measurement-protocol.mjs +0 -89
  243. package/dist/destinations/google-analytics.d.mts +0 -98
  244. package/dist/destinations/google-analytics.mjs +0 -126
  245. package/dist/destinations/http-destination.d.mts +0 -20
  246. package/dist/destinations/http-destination.mjs +0 -25
  247. package/dist/destinations/index.d.mts +0 -5
  248. package/dist/destinations/index.mjs +0 -5
  249. package/dist/destinations/vercel-analytics.d.mts +0 -13
  250. package/dist/destinations/vercel-analytics.mjs +0 -30
  251. package/dist/index.d.mts +0 -6
  252. package/dist/index.mjs +0 -4
  253. package/dist/react/consent-banner.d.mts +0 -163
  254. package/dist/react/consent-banner.mjs +0 -209
  255. package/dist/react/index.d.mts +0 -3
  256. package/dist/react/index.mjs +0 -3
  257. package/dist/react/use-consent.d.mts +0 -49
  258. package/dist/react/use-consent.mjs +0 -54
  259. package/dist/server/create-server-tracker.d.mts +0 -42
  260. package/dist/server/create-server-tracker.mjs +0 -67
  261. package/dist/server/index.d.mts +0 -3
  262. package/dist/server/index.mjs +0 -3
  263. package/dist/server/region.d.mts +0 -20
  264. package/dist/server/region.mjs +0 -56
  265. package/src/client/index.ts +0 -17
  266. package/src/client/lifecycle.ts +0 -50
  267. package/src/client/local-storage.ts +0 -44
  268. package/src/client/queue.ts +0 -117
  269. package/src/client/router.ts +0 -37
  270. package/src/core/index.ts +0 -24
  271. package/src/destinations/ga4-measurement-protocol.ts +0 -166
  272. package/src/destinations/http-destination.ts +0 -33
  273. package/src/destinations/index.ts +0 -20
  274. package/src/react/index.ts +0 -29
  275. package/src/server/create-server-tracker.ts +0 -128
  276. package/src/server/index.ts +0 -4
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Guards the cookie name against anything outside the RFC 6265 token set.
3
+ *
4
+ * @throws Error when `cookieName` is not a valid token.
5
+ *
6
+ * @since 1.0.0-canary.7
7
+ */
8
+ export declare function assertValidAnonymousIdCookieName(cookieName: string): void;
9
+ /**
10
+ * Whether a value is safe to persist as an anonymous id. The persist endpoint is public and
11
+ * writes its input into a `Set-Cookie`, so only an exactly-UUID-shaped value may pass —
12
+ * anything else is a header-injection attempt or corruption, never a valid id.
13
+ *
14
+ * @since 1.0.0-canary.6
15
+ */
16
+ export declare function isValidAnonymousId(value: string): boolean;
17
+ /**
18
+ * @since 1.0.0-canary.6
19
+ */
20
+ export interface AnonymousIdCookieOptions {
21
+ /** Cookie name — must match what the client tracker reads. */
22
+ cookieName: string;
23
+ /** The client-minted id to persist — throws unless it is exactly UUID-shaped. */
24
+ id: string;
25
+ /** How long the id survives a return visit. Defaults to one year. */
26
+ maxAgeSeconds?: number | undefined;
27
+ }
28
+ /**
29
+ * The validated name/value plus cookie attributes for persisting a client-minted anonymous
30
+ * id from the server — feed straight into the framework's cookie helper (`setCookie`).
31
+ *
32
+ * @remarks
33
+ * Always `Secure` — on a plain-HTTP dev origin a browser may drop it, and the
34
+ * client-written cookie (`createServerPersistedAnonymousId`'s optimistic write) still
35
+ * covers that session. Not `httpOnly`: the client tracker must read the id to stamp it onto
36
+ * events. A server-set cookie escapes Safari ITP's 7-day cap on `document.cookie` writes, so
37
+ * the id actually lives its full max-age. The server only ever re-sets an id the client
38
+ * hands it — minting stays client-side, after consent, by design.
39
+ *
40
+ * @throws Error when `id` is not UUID-shaped or `cookieName` is not a valid token.
41
+ *
42
+ * @since 1.0.0-canary.6
43
+ */
44
+ export declare function resolveAnonymousIdCookie(options: AnonymousIdCookieOptions): {
45
+ maxAge: number;
46
+ name: string;
47
+ path: string;
48
+ sameSite: "lax";
49
+ secure: true;
50
+ value: string;
51
+ };
52
+ /**
53
+ * Name plus attributes for expiring the anonymous-id cookie — the server half of a consent
54
+ * withdrawal. Attributes must match {@link resolveAnonymousIdCookie} so the browser targets
55
+ * the same cookie. Feed into the framework's `deleteCookie`.
56
+ *
57
+ * @throws Error when `cookieName` is not a valid token.
58
+ *
59
+ * @since 1.0.0-canary.6
60
+ */
61
+ export declare function resolveClearAnonymousIdCookie(cookieName: string): {
62
+ name: string;
63
+ path: string;
64
+ sameSite: "lax";
65
+ secure: true;
66
+ };
67
+ //# sourceMappingURL=anonymous-id-cookie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anonymous-id-cookie.d.ts","sourceRoot":"","sources":["../../src/server/anonymous-id-cookie.ts"],"names":[],"mappings":"AAQA;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAIzE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEzD;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,8DAA8D;IAC9D,UAAU,EAAE,MAAM,CAAC;IACnB,iFAAiF;IACjF,EAAE,EAAE,MAAM,CAAC;IACX,qEAAqE;IACrE,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,wBAAwB,GAAG;IAC3E,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,KAAK,CAAC;IAChB,MAAM,EAAE,IAAI,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CAeA;AAED;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,MAAM,GAAG;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,KAAK,CAAC;IAChB,MAAM,EAAE,IAAI,CAAC;CACd,CAIA"}
@@ -0,0 +1,71 @@
1
+ import { ONE_YEAR_IN_SECONDS } from "#/core/cookie";
2
+ // The only shape the client ever mints (crypto.randomUUID) — anything else is rejected.
3
+ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
4
+ // Conservative subset of the RFC 6265 cookie-name token.
5
+ const COOKIE_NAME_PATTERN = /^[\w-]+$/;
6
+ /**
7
+ * Guards the cookie name against anything outside the RFC 6265 token set.
8
+ *
9
+ * @throws Error when `cookieName` is not a valid token.
10
+ *
11
+ * @since 1.0.0-canary.7
12
+ */
13
+ export function assertValidAnonymousIdCookieName(cookieName) {
14
+ if (!COOKIE_NAME_PATTERN.test(cookieName)) {
15
+ throw new Error(`Invalid anonymous-id cookie name: ${JSON.stringify(cookieName)}`);
16
+ }
17
+ }
18
+ /**
19
+ * Whether a value is safe to persist as an anonymous id. The persist endpoint is public and
20
+ * writes its input into a `Set-Cookie`, so only an exactly-UUID-shaped value may pass —
21
+ * anything else is a header-injection attempt or corruption, never a valid id.
22
+ *
23
+ * @since 1.0.0-canary.6
24
+ */
25
+ export function isValidAnonymousId(value) {
26
+ return UUID_PATTERN.test(value);
27
+ }
28
+ /**
29
+ * The validated name/value plus cookie attributes for persisting a client-minted anonymous
30
+ * id from the server — feed straight into the framework's cookie helper (`setCookie`).
31
+ *
32
+ * @remarks
33
+ * Always `Secure` — on a plain-HTTP dev origin a browser may drop it, and the
34
+ * client-written cookie (`createServerPersistedAnonymousId`'s optimistic write) still
35
+ * covers that session. Not `httpOnly`: the client tracker must read the id to stamp it onto
36
+ * events. A server-set cookie escapes Safari ITP's 7-day cap on `document.cookie` writes, so
37
+ * the id actually lives its full max-age. The server only ever re-sets an id the client
38
+ * hands it — minting stays client-side, after consent, by design.
39
+ *
40
+ * @throws Error when `id` is not UUID-shaped or `cookieName` is not a valid token.
41
+ *
42
+ * @since 1.0.0-canary.6
43
+ */
44
+ export function resolveAnonymousIdCookie(options) {
45
+ assertValidAnonymousIdCookieName(options.cookieName);
46
+ if (!isValidAnonymousId(options.id)) {
47
+ throw new Error("Invalid anonymous id: expected a UUID-shaped value");
48
+ }
49
+ return {
50
+ maxAge: options.maxAgeSeconds ?? ONE_YEAR_IN_SECONDS,
51
+ name: options.cookieName,
52
+ path: "/",
53
+ sameSite: "lax",
54
+ secure: true,
55
+ value: options.id,
56
+ };
57
+ }
58
+ /**
59
+ * Name plus attributes for expiring the anonymous-id cookie — the server half of a consent
60
+ * withdrawal. Attributes must match {@link resolveAnonymousIdCookie} so the browser targets
61
+ * the same cookie. Feed into the framework's `deleteCookie`.
62
+ *
63
+ * @throws Error when `cookieName` is not a valid token.
64
+ *
65
+ * @since 1.0.0-canary.6
66
+ */
67
+ export function resolveClearAnonymousIdCookie(cookieName) {
68
+ assertValidAnonymousIdCookieName(cookieName);
69
+ return { name: cookieName, path: "/", sameSite: "lax", secure: true };
70
+ }
71
+ //# sourceMappingURL=anonymous-id-cookie.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anonymous-id-cookie.js","sourceRoot":"","sources":["../../src/server/anonymous-id-cookie.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD,wFAAwF;AACxF,MAAM,YAAY,GAAG,iEAAiE,CAAC;AAEvF,yDAAyD;AACzD,MAAM,mBAAmB,GAAG,UAAU,CAAC;AAEvC;;;;;;GAMG;AACH,MAAM,UAAU,gCAAgC,CAAC,UAAkB;IACjE,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,qCAAqC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACrF,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAa;IAC9C,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC;AAcD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,wBAAwB,CAAC,OAAiC;IAQxE,gCAAgC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAErD,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;IACxE,CAAC;IAED,OAAO;QACL,MAAM,EAAE,OAAO,CAAC,aAAa,IAAI,mBAAmB;QACpD,IAAI,EAAE,OAAO,CAAC,UAAU;QACxB,IAAI,EAAE,GAAG;QACT,QAAQ,EAAE,KAAK;QACf,MAAM,EAAE,IAAI;QACZ,KAAK,EAAE,OAAO,CAAC,EAAE;KAClB,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,6BAA6B,CAAC,UAAkB;IAM9D,gCAAgC,CAAC,UAAU,CAAC,CAAC;IAE7C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AACxE,CAAC"}
@@ -0,0 +1,56 @@
1
+ import type { ConsentReceipt } from "#/core/consent-receipt";
2
+ /**
3
+ * Persistence backend for consent receipts — the durable store is the consumer's to supply
4
+ * (a database, an append-only log). The store is **append-only**: `append` never overwrites,
5
+ * and an update/withdrawal is a new receipt referencing the prior one (spec-consent-receipts §3).
6
+ * May be async so a real backend can await its write.
7
+ *
8
+ * @since 1.0.0-canary.7
9
+ */
10
+ export interface ReceiptStore {
11
+ append: (receipt: ConsentReceipt) => Promise<void> | void;
12
+ get: (receiptId: string) => ConsentReceipt | undefined | Promise<ConsentReceipt | undefined>;
13
+ }
14
+ /**
15
+ * An in-memory {@link ReceiptStore} for development and tests — **not** durable across a
16
+ * process restart, and not a lawful production store on its own. A real deployment supplies
17
+ * a database-backed implementation; this exists so the endpoint is runnable end-to-end
18
+ * without standing up storage first.
19
+ *
20
+ * @remarks
21
+ * Append is idempotent-by-id: re-appending the same `receiptId` is ignored, preserving the
22
+ * append-only guarantee even if a client retries.
23
+ *
24
+ * @since 1.0.0-canary.7
25
+ */
26
+ export declare function createInMemoryReceiptStore(): ReceiptStore;
27
+ /**
28
+ * The minimal id-keyed primitive a durable backend must expose for {@link createDurableReceiptStore}.
29
+ * Keeping it this small lets an integrator back receipts with any store (Vercel KV, Postgres,
30
+ * an append-only log) by implementing two methods rather than the full {@link ReceiptStore}.
31
+ *
32
+ * @since 1.0.0-canary.7
33
+ */
34
+ export interface ReceiptStoreBackend {
35
+ get: (receiptId: string) => Promise<ConsentReceipt | undefined>;
36
+ /**
37
+ * Persist one receipt under its id. MUST be idempotent-by-id and never overwrite — a
38
+ * duplicate `receiptId` is a no-op — so the append-only guarantee holds atomically even
39
+ * under a client retry or concurrent write (e.g. KV set-if-absent, Postgres
40
+ * `INSERT … ON CONFLICT DO NOTHING`). The frame delegates rather than doing a racy
41
+ * get-then-put itself.
42
+ */
43
+ put: (receiptId: string, receipt: ConsentReceipt) => Promise<void>;
44
+ }
45
+ /**
46
+ * A durable {@link ReceiptStore} over an injected {@link ReceiptStoreBackend} — the package
47
+ * supplies the append-only contract and adaptation, the deployment supplies the backend
48
+ * client (no database dependency is baked in). Pair it with a real backend in production,
49
+ * where {@link createInMemoryReceiptStore} is not a lawful store on its own.
50
+ *
51
+ * @since 1.0.0-canary.7
52
+ */
53
+ export declare function createDurableReceiptStore(options: {
54
+ backend: ReceiptStoreBackend;
55
+ }): ReceiptStore;
56
+ //# sourceMappingURL=consent-receipt-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-receipt-store.d.ts","sourceRoot":"","sources":["../../src/server/consent-receipt-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAE7D;;;;;;;GAOG;AACH,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC1D,GAAG,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,cAAc,GAAG,SAAS,GAAG,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAAC;CAC9F;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,0BAA0B,IAAI,YAAY,CAazD;AAED;;;;;;GAMG;AACH,MAAM,WAAW,mBAAmB;IAClC,GAAG,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAAC;IAChE;;;;;;OAMG;IACH,GAAG,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACpE;AAED;;;;;;;GAOG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE;IAAE,OAAO,EAAE,mBAAmB,CAAA;CAAE,GAAG,YAAY,CASjG"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * An in-memory {@link ReceiptStore} for development and tests — **not** durable across a
3
+ * process restart, and not a lawful production store on its own. A real deployment supplies
4
+ * a database-backed implementation; this exists so the endpoint is runnable end-to-end
5
+ * without standing up storage first.
6
+ *
7
+ * @remarks
8
+ * Append is idempotent-by-id: re-appending the same `receiptId` is ignored, preserving the
9
+ * append-only guarantee even if a client retries.
10
+ *
11
+ * @since 1.0.0-canary.7
12
+ */
13
+ export function createInMemoryReceiptStore() {
14
+ const receipts = new Map();
15
+ return {
16
+ append(receipt) {
17
+ if (!receipts.has(receipt.receiptId)) {
18
+ receipts.set(receipt.receiptId, receipt);
19
+ }
20
+ },
21
+ get(receiptId) {
22
+ return receipts.get(receiptId);
23
+ },
24
+ };
25
+ }
26
+ /**
27
+ * A durable {@link ReceiptStore} over an injected {@link ReceiptStoreBackend} — the package
28
+ * supplies the append-only contract and adaptation, the deployment supplies the backend
29
+ * client (no database dependency is baked in). Pair it with a real backend in production,
30
+ * where {@link createInMemoryReceiptStore} is not a lawful store on its own.
31
+ *
32
+ * @since 1.0.0-canary.7
33
+ */
34
+ export function createDurableReceiptStore(options) {
35
+ return {
36
+ async append(receipt) {
37
+ await options.backend.put(receipt.receiptId, receipt);
38
+ },
39
+ async get(receiptId) {
40
+ return options.backend.get(receiptId);
41
+ },
42
+ };
43
+ }
44
+ //# sourceMappingURL=consent-receipt-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-receipt-store.js","sourceRoot":"","sources":["../../src/server/consent-receipt-store.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,0BAA0B;IACxC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAA0B,CAAC;IAEnD,OAAO;QACL,MAAM,CAAC,OAAuB;YAC5B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;gBACrC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAC3C,CAAC;QACH,CAAC;QACD,GAAG,CAAC,SAAiB;YACnB,OAAO,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACjC,CAAC;KACF,CAAC;AACJ,CAAC;AAqBD;;;;;;;GAOG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAyC;IACjF,OAAO;QACL,KAAK,CAAC,MAAM,CAAC,OAAuB;YAClC,MAAM,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACxD,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,SAAiB;YACzB,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACxC,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,41 @@
1
+ import type { ConsentReceipt, ConsentReceiptInput } from "#/core/consent-receipt";
2
+ /**
3
+ * The receipt information-structure version this builder emits (ISO/IEC TS 27560-shaped).
4
+ * Bump when the receipt field set changes; stored receipts carry it so a reader can migrate.
5
+ *
6
+ * @since 1.0.0-canary.7
7
+ */
8
+ export declare const RECEIPT_SCHEMA_VERSION = "1.0.0";
9
+ /**
10
+ * Coarsens an IP for storage — a full address MUST never be persisted (spec-consent-receipts
11
+ * §4, R5). IPv4 drops the last two octets; IPv6 keeps only the first two hextets. This mirrors
12
+ * the Cookiebot-style anonymization cited in the spec and is the maximally-private default;
13
+ * a controller may widen it under counsel, but the builder never returns a full address.
14
+ * Returns `undefined` for a missing or malformed input rather than storing a bad value.
15
+ *
16
+ * @since 1.0.0-canary.7
17
+ */
18
+ export declare function coarsenIp(ip: string | null | undefined): string | undefined;
19
+ /**
20
+ * @remarks
21
+ * `sign` is optional so the package never bakes a crypto secret: supply an HMAC/signature
22
+ * function to stamp an `integrityKey`, letting a client prove its state matches the stored
23
+ * original (spec-consent-receipts §5). Omit it and receipts carry no integrity key.
24
+ *
25
+ * @since 1.0.0-canary.7
26
+ */
27
+ export interface BuildConsentReceiptOptions {
28
+ input: ConsentReceiptInput;
29
+ /** The connection IP, read server-side — never accepted from the request body. */
30
+ rawIp?: string | null | undefined;
31
+ sign?: ((receipt: Omit<ConsentReceipt, "integrityKey">) => string) | undefined;
32
+ }
33
+ /**
34
+ * Builds a stored consent receipt from caller input plus server-derived fields
35
+ * (`receiptId`, `timestamp`, coarsened IP, optional `integrityKey`). Pure except for the
36
+ * id/clock it stamps — pass those in via a seam if a deterministic build is needed.
37
+ *
38
+ * @since 1.0.0-canary.7
39
+ */
40
+ export declare function buildConsentReceipt(options: BuildConsentReceiptOptions): ConsentReceipt;
41
+ //# sourceMappingURL=consent-receipt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-receipt.d.ts","sourceRoot":"","sources":["../../src/server/consent-receipt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAElF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,UAAU,CAAC;AAI9C;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CA+B3E;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,0BAA0B;IACzC,KAAK,EAAE,mBAAmB,CAAC;IAC3B,kFAAkF;IAClF,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAClC,IAAI,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,cAAc,CAAC,KAAK,MAAM,CAAC,GAAG,SAAS,CAAC;CAChF;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,0BAA0B,GAAG,cAAc,CAYvF"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The receipt information-structure version this builder emits (ISO/IEC TS 27560-shaped).
3
+ * Bump when the receipt field set changes; stored receipts carry it so a reader can migrate.
4
+ *
5
+ * @since 1.0.0-canary.7
6
+ */
7
+ export const RECEIPT_SCHEMA_VERSION = "1.0.0";
8
+ const IPV4_PATTERN = /^(\d{1,3}\.){3}\d{1,3}$/;
9
+ /**
10
+ * Coarsens an IP for storage — a full address MUST never be persisted (spec-consent-receipts
11
+ * §4, R5). IPv4 drops the last two octets; IPv6 keeps only the first two hextets. This mirrors
12
+ * the Cookiebot-style anonymization cited in the spec and is the maximally-private default;
13
+ * a controller may widen it under counsel, but the builder never returns a full address.
14
+ * Returns `undefined` for a missing or malformed input rather than storing a bad value.
15
+ *
16
+ * @since 1.0.0-canary.7
17
+ */
18
+ export function coarsenIp(ip) {
19
+ if (!ip) {
20
+ return undefined;
21
+ }
22
+ const trimmed = ip.trim();
23
+ if (IPV4_PATTERN.test(trimmed)) {
24
+ const octets = trimmed.split(".");
25
+ // Reject out-of-range octets (e.g. "999.1.1.1") rather than storing a malformed coarse value.
26
+ if (octets.some((octet) => Number(octet) > 255)) {
27
+ return undefined;
28
+ }
29
+ return `${octets[0]}.${octets[1]}.0.0`;
30
+ }
31
+ if (trimmed.includes(":")) {
32
+ const [first, second] = trimmed.split(":");
33
+ // Require two explicit leading hextets — a compressed-leading address ("::1", "fe80::1")
34
+ // can't be coarsened without full expansion, so degrade to no stored value.
35
+ if (!first || !second) {
36
+ return undefined;
37
+ }
38
+ return `${first}:${second}::`;
39
+ }
40
+ return undefined;
41
+ }
42
+ /**
43
+ * Builds a stored consent receipt from caller input plus server-derived fields
44
+ * (`receiptId`, `timestamp`, coarsened IP, optional `integrityKey`). Pure except for the
45
+ * id/clock it stamps — pass those in via a seam if a deterministic build is needed.
46
+ *
47
+ * @since 1.0.0-canary.7
48
+ */
49
+ export function buildConsentReceipt(options) {
50
+ const base = {
51
+ ...options.input,
52
+ ipCoarse: coarsenIp(options.rawIp),
53
+ receiptId: crypto.randomUUID(),
54
+ schemaVersion: RECEIPT_SCHEMA_VERSION,
55
+ timestamp: Date.now(),
56
+ };
57
+ const integrityKey = options.sign?.(base);
58
+ return integrityKey === undefined ? base : { ...base, integrityKey };
59
+ }
60
+ //# sourceMappingURL=consent-receipt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-receipt.js","sourceRoot":"","sources":["../../src/server/consent-receipt.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,OAAO,CAAC;AAE9C,MAAM,YAAY,GAAG,yBAAyB,CAAC;AAE/C;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CAAC,EAA6B;IACrD,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,OAAO,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC;IAE1B,IAAI,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;QAC/B,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAElC,8FAA8F;QAC9F,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,EAAE,CAAC;YAChD,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;IACzC,CAAC;IAED,IAAI,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1B,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAE3C,yFAAyF;QACzF,4EAA4E;QAC5E,IAAI,CAAC,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YACtB,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,OAAO,GAAG,KAAK,IAAI,MAAM,IAAI,CAAC;IAChC,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAiBD;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAmC;IACrE,MAAM,IAAI,GAAyC;QACjD,GAAG,OAAO,CAAC,KAAK;QAChB,QAAQ,EAAE,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC;QAClC,SAAS,EAAE,MAAM,CAAC,UAAU,EAAE;QAC9B,aAAa,EAAE,sBAAsB;QACrC,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;KACtB,CAAC;IAEF,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC;IAE1C,OAAO,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,YAAY,EAAE,CAAC;AACvE,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { ConsentCategory, InitialConsent } from "#/core/consent";
2
+ /**
3
+ * @since 1.0.0-canary.6
4
+ */
5
+ export interface InitialConsentOptions {
6
+ /** ISO 3166-1 alpha-2 from the geo header; missing → the strictest opt-in default. */
7
+ countryCode: string | undefined;
8
+ /** Honored as an ads-only opt-out on the default decision. */
9
+ hasGlobalPrivacyControlSignal?: boolean | undefined;
10
+ /** Categories the app's prompt asks about — opt-out regions grant exactly these by default. */
11
+ requestedCategories: ReadonlyArray<ConsentCategory>;
12
+ }
13
+ /**
14
+ * Pure region → mode → default-consent resolution for server functions (or a fail-closed
15
+ * bake when country is unknown). Named with the `resolve*` family it composes.
16
+ *
17
+ * @remarks
18
+ * A missing country code means an unknown visitor (a prerender crawl, a host without a
19
+ * geo header) — not a known non-EU visitor — so it fails closed to
20
+ * {@link STRICTEST_INITIAL_CONSENT} instead of `"other"`'s opt-out. Conflating the two
21
+ * would grant analytics by default to every visitor, EU included, on a geo-less host.
22
+ *
23
+ * @since 1.0.0-canary.6
24
+ */
25
+ export declare function resolveInitialConsent(options: InitialConsentOptions): InitialConsent;
26
+ //# sourceMappingURL=initial-consent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"initial-consent.d.ts","sourceRoot":"","sources":["../../src/server/initial-consent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAItE;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,sFAAsF;IACtF,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,8DAA8D;IAC9D,6BAA6B,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACpD,+FAA+F;IAC/F,mBAAmB,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;CACrD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAiBpF"}
@@ -0,0 +1,31 @@
1
+ import { resolveConsentMode, resolveDefaultConsent, STRICTEST_INITIAL_CONSENT } from "#/core/consent";
2
+ import { resolveRegionFromCountryCode } from "#/server/region";
3
+ /**
4
+ * Pure region → mode → default-consent resolution for server functions (or a fail-closed
5
+ * bake when country is unknown). Named with the `resolve*` family it composes.
6
+ *
7
+ * @remarks
8
+ * A missing country code means an unknown visitor (a prerender crawl, a host without a
9
+ * geo header) — not a known non-EU visitor — so it fails closed to
10
+ * {@link STRICTEST_INITIAL_CONSENT} instead of `"other"`'s opt-out. Conflating the two
11
+ * would grant analytics by default to every visitor, EU included, on a geo-less host.
12
+ *
13
+ * @since 1.0.0-canary.6
14
+ */
15
+ export function resolveInitialConsent(options) {
16
+ if (!options.countryCode) {
17
+ return STRICTEST_INITIAL_CONSENT;
18
+ }
19
+ const region = resolveRegionFromCountryCode(options.countryCode);
20
+ const mode = resolveConsentMode(region);
21
+ return {
22
+ defaultConsent: resolveDefaultConsent({
23
+ hasGlobalPrivacyControlSignal: options.hasGlobalPrivacyControlSignal ?? false,
24
+ mode,
25
+ requestedCategories: options.requestedCategories,
26
+ }),
27
+ mode,
28
+ region,
29
+ };
30
+ }
31
+ //# sourceMappingURL=initial-consent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"initial-consent.js","sourceRoot":"","sources":["../../src/server/initial-consent.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AACtG,OAAO,EAAE,4BAA4B,EAAE,MAAM,iBAAiB,CAAC;AAc/D;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAA8B;IAClE,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;QACzB,OAAO,yBAAyB,CAAC;IACnC,CAAC;IAED,MAAM,MAAM,GAAG,4BAA4B,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACjE,MAAM,IAAI,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAExC,OAAO;QACL,cAAc,EAAE,qBAAqB,CAAC;YACpC,6BAA6B,EAAE,OAAO,CAAC,6BAA6B,IAAI,KAAK;YAC7E,IAAI;YACJ,mBAAmB,EAAE,OAAO,CAAC,mBAAmB;SACjD,CAAC;QACF,IAAI;QACJ,MAAM;KACP,CAAC;AACJ,CAAC"}
@@ -0,0 +1,69 @@
1
+ import type { ConsentDecision } from "#/core/consent";
2
+ /**
3
+ * A GA4 event for the Measurement Protocol. `params` are flat scalars — GA4 rejects nested values and booleans.
4
+ *
5
+ * @since 1.0.0-canary.7
6
+ */
7
+ export interface MeasurementProtocolEvent {
8
+ name: string;
9
+ params?: Record<string, number | string> | undefined;
10
+ }
11
+ /**
12
+ * The GA4 MP `consent` object — the two ad-consent signals the payload accepts.
13
+ *
14
+ * @since 1.0.0-canary.7
15
+ */
16
+ export interface MeasurementProtocolConsent {
17
+ ad_personalization: "DENIED" | "GRANTED";
18
+ ad_user_data: "DENIED" | "GRANTED";
19
+ }
20
+ /**
21
+ * Maps the package `ConsentDecision` to GA4 MP consent signals — `ads` drives both ad fields.
22
+ *
23
+ * @since 1.0.0-canary.7
24
+ */
25
+ export declare function toMeasurementProtocolConsent(decision: ConsentDecision): MeasurementProtocolConsent;
26
+ /**
27
+ * Extracts the GA4 `client_id` from a `_ga` cookie value, e.g.
28
+ * `GA1.1.1809596761.1693829043` → `1809596761.1693829043` (everything past the
29
+ * `GA1.<version>.` prefix). The `_ga` layout is community-established, not part of the
30
+ * Measurement Protocol reference, so anything that doesn't parse returns `undefined` rather
31
+ * than sending a malformed id.
32
+ *
33
+ * @since 1.0.0-canary.7
34
+ */
35
+ export declare function extractGaClientId(gaCookieValue: string | undefined): string | undefined;
36
+ /**
37
+ * The one network primitive the sender needs — injected so the package ships no HTTP client.
38
+ *
39
+ * @since 1.0.0-canary.7
40
+ */
41
+ export type MeasurementProtocolTransport = (request: {
42
+ body: string;
43
+ url: string;
44
+ }) => Promise<void>;
45
+ /**
46
+ * @since 1.0.0-canary.7
47
+ */
48
+ export interface SendMeasurementProtocolEventsOptions {
49
+ /** GA4 **API Secret** — read server-side from config, never a client-exposed var. */
50
+ apiSecret: string;
51
+ /** The visitor's GA4 client id (see {@link extractGaClientId}). */
52
+ clientId: string;
53
+ consent?: MeasurementProtocolConsent | undefined;
54
+ endpointOrigin?: string | undefined;
55
+ events: ReadonlyArray<MeasurementProtocolEvent>;
56
+ measurementId: string;
57
+ transport?: MeasurementProtocolTransport | undefined;
58
+ }
59
+ /**
60
+ * POSTs one or more events to the GA4 Measurement Protocol for a known `client_id` — the
61
+ * server-side counterpart to gtag.js. The caller owns credentials (never bake them) and the
62
+ * consent gate: send only for a server-owned event the visitor's consent permits.
63
+ *
64
+ * @see https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events
65
+ *
66
+ * @since 1.0.0-canary.7
67
+ */
68
+ export declare function sendMeasurementProtocolEvents(options: SendMeasurementProtocolEventsOptions): Promise<void>;
69
+ //# sourceMappingURL=measurement-protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"measurement-protocol.d.ts","sourceRoot":"","sources":["../../src/server/measurement-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD;;;;GAIG;AACH,MAAM,WAAW,wBAAwB;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,SAAS,CAAC;CACtD;AAED;;;;GAIG;AACH,MAAM,WAAW,0BAA0B;IACzC,kBAAkB,EAAE,QAAQ,GAAG,SAAS,CAAC;IACzC,YAAY,EAAE,QAAQ,GAAG,SAAS,CAAC;CACpC;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,eAAe,GAAG,0BAA0B,CAIlG;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,aAAa,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAYvF;AAED;;;;GAIG;AACH,MAAM,MAAM,4BAA4B,GAAG,CAAC,OAAO,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAkBrG;;GAEG;AACH,MAAM,WAAW,oCAAoC;IACnD,qFAAqF;IACrF,SAAS,EAAE,MAAM,CAAC;IAClB,mEAAmE;IACnE,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,0BAA0B,GAAG,SAAS,CAAC;IACjD,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,MAAM,EAAE,aAAa,CAAC,wBAAwB,CAAC,CAAC;IAChD,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,4BAA4B,GAAG,SAAS,CAAC;CACtD;AAED;;;;;;;;GAQG;AACH,wBAAsB,6BAA6B,CAAC,OAAO,EAAE,oCAAoC,GAAG,OAAO,CAAC,IAAI,CAAC,CAUhH"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Maps the package `ConsentDecision` to GA4 MP consent signals — `ads` drives both ad fields.
3
+ *
4
+ * @since 1.0.0-canary.7
5
+ */
6
+ export function toMeasurementProtocolConsent(decision) {
7
+ const state = decision.ads ? "GRANTED" : "DENIED";
8
+ return { ad_personalization: state, ad_user_data: state };
9
+ }
10
+ /**
11
+ * Extracts the GA4 `client_id` from a `_ga` cookie value, e.g.
12
+ * `GA1.1.1809596761.1693829043` → `1809596761.1693829043` (everything past the
13
+ * `GA1.<version>.` prefix). The `_ga` layout is community-established, not part of the
14
+ * Measurement Protocol reference, so anything that doesn't parse returns `undefined` rather
15
+ * than sending a malformed id.
16
+ *
17
+ * @since 1.0.0-canary.7
18
+ */
19
+ export function extractGaClientId(gaCookieValue) {
20
+ if (!gaCookieValue) {
21
+ return undefined;
22
+ }
23
+ const segments = gaCookieValue.split(".");
24
+ if (segments.length < 4 || segments[0] !== "GA1") {
25
+ return undefined;
26
+ }
27
+ return segments.slice(2).join(".");
28
+ }
29
+ /** Default GA4 collection origin; use `https://region1.google-analytics.com` for EU data residency. */
30
+ const DEFAULT_ENDPOINT_ORIGIN = "https://www.google-analytics.com";
31
+ async function defaultTransport(request) {
32
+ const response = await fetch(request.url, {
33
+ body: request.body,
34
+ headers: { "content-type": "application/json" },
35
+ method: "POST",
36
+ });
37
+ // MP returns 2xx with no useful body; a non-2xx is a config error worth surfacing.
38
+ if (!response.ok) {
39
+ throw new Error(`Measurement Protocol responded ${String(response.status)}`);
40
+ }
41
+ }
42
+ /**
43
+ * POSTs one or more events to the GA4 Measurement Protocol for a known `client_id` — the
44
+ * server-side counterpart to gtag.js. The caller owns credentials (never bake them) and the
45
+ * consent gate: send only for a server-owned event the visitor's consent permits.
46
+ *
47
+ * @see https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events
48
+ *
49
+ * @since 1.0.0-canary.7
50
+ */
51
+ export async function sendMeasurementProtocolEvents(options) {
52
+ const origin = options.endpointOrigin ?? DEFAULT_ENDPOINT_ORIGIN;
53
+ const url = `${origin}/mp/collect?measurement_id=${encodeURIComponent(options.measurementId)}&api_secret=${encodeURIComponent(options.apiSecret)}`;
54
+ const body = JSON.stringify({
55
+ client_id: options.clientId,
56
+ events: options.events,
57
+ ...(options.consent ? { consent: options.consent } : {}),
58
+ });
59
+ await (options.transport ?? defaultTransport)({ body, url });
60
+ }
61
+ //# sourceMappingURL=measurement-protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"measurement-protocol.js","sourceRoot":"","sources":["../../src/server/measurement-protocol.ts"],"names":[],"mappings":"AAsBA;;;;GAIG;AACH,MAAM,UAAU,4BAA4B,CAAC,QAAyB;IACpE,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC;IAElD,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC;AAC5D,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,iBAAiB,CAAC,aAAiC;IACjE,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,aAAa,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAE1C,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE,CAAC;QACjD,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACrC,CAAC;AASD,uGAAuG;AACvG,MAAM,uBAAuB,GAAG,kCAAkC,CAAC;AAEnE,KAAK,UAAU,gBAAgB,CAAC,OAAsC;IACpE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;QACxC,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;QAC/C,MAAM,EAAE,MAAM;KACf,CAAC,CAAC;IAEH,mFAAmF;IACnF,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,kCAAkC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC/E,CAAC;AACH,CAAC;AAiBD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAAC,OAA6C;IAC/F,MAAM,MAAM,GAAG,OAAO,CAAC,cAAc,IAAI,uBAAuB,CAAC;IACjE,MAAM,GAAG,GAAG,GAAG,MAAM,8BAA8B,kBAAkB,CAAC,OAAO,CAAC,aAAa,CAAC,eAAe,kBAAkB,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;IACnJ,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC;QAC1B,SAAS,EAAE,OAAO,CAAC,QAAQ;QAC3B,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACzD,CAAC,CAAC;IAEH,MAAM,CAAC,OAAO,CAAC,SAAS,IAAI,gBAAgB,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;AAC/D,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { ConsentRegion } from "#/core/consent";
2
+ /**
3
+ * EU member states — GDPR opt-in via `resolveConsentMode("eu")`.
4
+ *
5
+ * @since 1.0.0-canary.6
6
+ */
7
+ export declare const EU_COUNTRY_CODES: ReadonlySet<string>;
8
+ /**
9
+ * UK GDPR (post-Brexit) and EEA/EFTA states that apply GDPR-equivalent rules — mapped to
10
+ * the same `"eu"` consent region so they get opt-in, not the `"other"` opt-out default.
11
+ *
12
+ * Restricted edge runtimes that cannot import this package should duplicate these codes
13
+ * and keep a sync test against this export.
14
+ *
15
+ * @since 1.0.0-canary.6
16
+ */
17
+ export declare const OPT_IN_EQUIVALENT_COUNTRY_CODES: ReadonlySet<string>;
18
+ /**
19
+ * Unrecognized codes fall back to "other", which `resolveConsentMode` treats as
20
+ * opt-out — safe only because opt-in regions (EU/VN plus UK/EEA/EFTA above) are matched
21
+ * explicitly. A *missing* code also maps to "other" here, but `resolveInitialConsent`
22
+ * intercepts that case first and fails closed — unknown is not known-elsewhere.
23
+ *
24
+ * @since 0.5.0-canary.4
25
+ */
26
+ export declare function resolveRegionFromCountryCode(countryCode: string | undefined): ConsentRegion;
27
+ //# sourceMappingURL=region.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"region.d.ts","sourceRoot":"","sources":["../../src/server/region.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAEpD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,EAAE,WAAW,CAAC,MAAM,CA4B/C,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,+BAA+B,EAAE,WAAW,CAAC,MAAM,CAAqC,CAAC;AAEtG;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,GAAG,aAAa,CAoB3F"}