@asstio-dev/storefront 0.46.0 → 0.47.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.
@@ -0,0 +1,36 @@
1
+ import { type Purpose } from "./codec.js";
2
+ /** Next.js's `cookies()` store, or anything with the same `get`. */
3
+ export type CookieReader = {
4
+ get(name: string): {
5
+ value: string;
6
+ } | undefined;
7
+ };
8
+ export interface ConsentReading {
9
+ /** The S30 device half: the locked cookie says `granted`, under the current refusal mark, decided after the refusal time. */
10
+ granted(purpose: Purpose): boolean;
11
+ /** The device key of a parsable cookie (locked or fallback), else null. */
12
+ device: string | null;
13
+ /** The notice reference the device recorded, else null. */
14
+ ref: string | null;
15
+ }
16
+ /**
17
+ * The device state for SSR from `__Host-sf_consent` and `__Host-sf_consent_off`. A missing, unparsable or fallback cookie is not
18
+ * granted for any purpose, nor is a grant decided under another refusal mark or not later than the refusal time. `origin` is the frontend's own
19
+ * origin (the `__Host-` prefix is dropped on http://localhost); without it the prefixed names are read.
20
+ */
21
+ export declare function readConsentCookie(cookies: CookieReader, opts?: {
22
+ origin?: string;
23
+ }): ConsentReading;
24
+ /** `t.` + the first 16 hex of `sha256(token)`: the owner key of a guest shopper token (PL9, S36 owner binding). */
25
+ export declare function tokenOwner(token: string): string;
26
+ /** `a.` + the account id as 32 lowercase hex (a uuid's dashes dropped). */
27
+ export declare function accountOwner(accountId: string): string;
28
+ /**
29
+ * The consent owner the page hands the coordinator (PL9): `a.<account id>` under a session (pass the session's account id), else
30
+ * `t.<sha256 prefix>` when `__Host-sf_shopper` exists, else `none`.
31
+ */
32
+ export declare function consentOwner(cookies: CookieReader, session?: {
33
+ accountId: string;
34
+ } | null, opts?: {
35
+ origin?: string;
36
+ }): string;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * `@asstio-dev/storefront/consent/server` — server-side consent readers (shopper-state spec §7.4, S36, PL9). Server code only
3
+ * **reads** the consent cookies; the browser coordinator is their one writer. Built on the codec only, so SSR and the browser give
4
+ * the same answer for the same cookies. Node runtime only: `tokenOwner`/`consentOwner` use `node:crypto` (not the Edge runtime).
5
+ */
6
+ import { createHash } from "node:crypto";
7
+ import { cookieSecurity, shopperCookie } from "../account/cookies.js";
8
+ import { consentCookieNames, grantedFor, isFallback, parseConsent, parseRefusal } from "./codec.js";
9
+ /**
10
+ * The device state for SSR from `__Host-sf_consent` and `__Host-sf_consent_off`. A missing, unparsable or fallback cookie is not
11
+ * granted for any purpose, nor is a grant decided under another refusal mark or not later than the refusal time. `origin` is the frontend's own
12
+ * origin (the `__Host-` prefix is dropped on http://localhost); without it the prefixed names are read.
13
+ */
14
+ export function readConsentCookie(cookies, opts = {}) {
15
+ const names = consentCookieNames(opts.origin);
16
+ const cookie = parseConsent(cookies.get(names.consent)?.value);
17
+ const off = parseRefusal(cookies.get(names.off)?.value);
18
+ return {
19
+ granted: (purpose) => grantedFor(cookie, off, purpose),
20
+ device: cookie?.device ?? null,
21
+ ref: cookie && !isFallback(cookie) ? cookie.ref : null,
22
+ };
23
+ }
24
+ /** `t.` + the first 16 hex of `sha256(token)`: the owner key of a guest shopper token (PL9, S36 owner binding). */
25
+ export function tokenOwner(token) {
26
+ return "t." + createHash("sha256").update(token, "utf8").digest("hex").slice(0, 16);
27
+ }
28
+ /** `a.` + the account id as 32 lowercase hex (a uuid's dashes dropped). */
29
+ export function accountOwner(accountId) {
30
+ const hex = accountId.replace(/-/g, "").toLowerCase();
31
+ if (!/^[0-9a-f]{32}$/.test(hex))
32
+ throw new RangeError("An account id is a uuid (32 hex digits).");
33
+ return "a." + hex;
34
+ }
35
+ /**
36
+ * The consent owner the page hands the coordinator (PL9): `a.<account id>` under a session (pass the session's account id), else
37
+ * `t.<sha256 prefix>` when `__Host-sf_shopper` exists, else `none`.
38
+ */
39
+ export function consentOwner(cookies, session, opts = {}) {
40
+ if (session)
41
+ return accountOwner(session.accountId);
42
+ const security = opts.origin === undefined ? { secure: true } : cookieSecurity(opts.origin);
43
+ const token = cookies.get(shopperCookie.cookieName(security))?.value;
44
+ return token ? tokenOwner(token) : "none";
45
+ }
package/dist/index.d.ts CHANGED
@@ -62,6 +62,8 @@ export { jsonLdScript } from "./seo/serialize.js";
62
62
  export { productFacts } from "./analytics/facts.js";
63
63
  export { longestCategory, toGa4Ecommerce, toGa4Item } from "./analytics/ga4.js";
64
64
  export { trackSearchEvents, type SearchEvent, type SearchEventsResponse } from "./analytics/events.js";
65
+ export { consentNotice, type ConsentNoticeDto } from "./consent/notice.js";
66
+ export type { ConsentCookie, Purpose, PurposeState, UnsyncedEntry } from "./consent/codec.js";
65
67
  export { AnalyticsError, type Ga4Ecommerce, type Ga4Item, type Ga4ItemOptions, type Ga4ItemSource, type Ga4Purchase, type Ga4SharedOptions, type ProductFacts, type SelectedVariant, } from "./analytics/types.js";
66
68
  export { SeoError, type BreadcrumbLink, type CollectionHeadOptions, type HeadOptions, type JsonLd, type NextMetadataLike, type ProductJsonLdOptions, type SchemaProperty, type SeoContext, type SeoHead, type SeoMeta, type VariantAvailability, } from "./seo/types.js";
67
69
  export { readVariantParam, redirectTarget, VARIANT_PARAM, variantQueryForApi, variantUrl, type QueryInput, type VariantParam, } from "./variant.js";
package/dist/index.js CHANGED
@@ -53,6 +53,7 @@ export { jsonLdScript } from "./seo/serialize.js";
53
53
  export { productFacts } from "./analytics/facts.js";
54
54
  export { longestCategory, toGa4Ecommerce, toGa4Item } from "./analytics/ga4.js";
55
55
  export { trackSearchEvents } from "./analytics/events.js";
56
+ export { consentNotice } from "./consent/notice.js";
56
57
  export { AnalyticsError, } from "./analytics/types.js";
57
58
  export { SeoError, } from "./seo/types.js";
58
59
  export { readVariantParam, redirectTarget, VARIANT_PARAM, variantQueryForApi, variantUrl, } from "./variant.js";