@tokenoftrust/storefront-runner 8.0.0 → 8.1.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/apps/storefront/src/components/catalog/storefront/ProductCard.astro +15 -7
- package/apps/storefront/src/components/publication/storefront/islands/CatalogQueryView.tsx +6 -1
- package/apps/storefront/src/layouts/Layout.astro +39 -1
- package/apps/storefront/src/lib/checkout/commerce/finalCartAuthorization.ts +4 -4
- package/apps/storefront/src/lib/compliance/shopper/statePickerClient.ts +622 -67
- package/apps/storefront/src/lib/compliance/shopper/staticEligibilityArtifact.ts +182 -0
- package/apps/storefront/src/lib/compliance/shopper/staticEligibilityPointerGate.ts +13 -0
- package/apps/storefront/src/middleware/routeBranch.ts +1 -0
- package/apps/storefront/src/pages/api/commerce/final-cart-authorization.ts +45 -28
- package/apps/storefront/src/pages/api/compliance/preflight.ts +162 -66
- package/apps/storefront/src/pages/eligibility.json.ts +23 -0
- package/package.json +1 -1
- package/packages/cli/src/publication-spaces.mjs +1 -1
- package/packages/public-runtime/package.json +1 -0
- package/packages/public-runtime/schemas/publication-space/v1/manifest.schema.json +1 -1
- package/packages/public-runtime/schemas/publication-space/v1/normalized-mount.schema.json +1 -1
- package/packages/public-runtime/src/compliance/evaluate.ts +13 -0
- package/packages/public-runtime/src/compliance/state-directory.ts +16 -2
- package/packages/public-runtime/src/compliance/static-eligibility.d.mts +92 -0
- package/packages/public-runtime/src/compliance/static-eligibility.mjs +394 -0
- package/packages/public-runtime/src/declared-tenant-config.mjs +1 -1
- package/packages/public-runtime/src/publication-space.ts +1 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static shopper-eligibility artifact (lib/compliance/shopper/staticEligibilityArtifact.ts,
|
|
3
|
+
* `@tot/public-runtime/compliance/static-eligibility`). The published pages filter their catalog
|
|
4
|
+
* cards and gate add-to-cart in the browser over this document, so it is rendered here once and the
|
|
5
|
+
* publish step pins it to a content address (`/_a/<sha256>.json`); a static host never calls this
|
|
6
|
+
* route. Public-only by schema: no prices, costs, B2B, draft or secret data.
|
|
7
|
+
*/
|
|
8
|
+
import type { APIContext } from "astro";
|
|
9
|
+
import { serializeStaticEligibilityArtifact } from "@tot/public-runtime/compliance/static-eligibility";
|
|
10
|
+
import { buildTenantStaticEligibility } from "@/lib/compliance/shopper/staticEligibilityArtifact";
|
|
11
|
+
|
|
12
|
+
export async function GET(context: APIContext): Promise<Response> {
|
|
13
|
+
const { tot, tenant } = context.locals;
|
|
14
|
+
const products = await tot.getAllActiveProducts();
|
|
15
|
+
const artifact = await buildTenantStaticEligibility(tenant, products);
|
|
16
|
+
if (!artifact) return new Response(null, { status: 404 });
|
|
17
|
+
return new Response(serializeStaticEligibilityArtifact(artifact), {
|
|
18
|
+
headers: {
|
|
19
|
+
"content-type": "application/json; charset=utf-8",
|
|
20
|
+
"cache-control": "public, max-age=300, s-maxage=3600",
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.1.0",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"description": "World-shareable storefront runner: multi-tenant renderer on Astro/Cloudflare. No control plane.",
|
|
6
6
|
"packageManager": "pnpm@11.9.0",
|
|
@@ -36,7 +36,7 @@ export const PLATFORM_PATHS = Object.freeze([
|
|
|
36
36
|
"/apps", "/assets", "/auth", "/brand", "/capabilities", "/cockpit", "/dashboard", "/dev",
|
|
37
37
|
"/favicon.svg", "/health", "/js", "/feed.json", "/llms.txt", "/publication-discovery",
|
|
38
38
|
"/search-index.json", "/sitemap.xml", "/platform", "/preview", "/products", "/collections",
|
|
39
|
-
"/search", "/catalog-index.json", "/saved", "/robots.txt", "/shared", "/style-guide", "/tenants",
|
|
39
|
+
"/search", "/catalog-index.json", "/eligibility.json", "/saved", "/robots.txt", "/shared", "/style-guide", "/tenants",
|
|
40
40
|
]);
|
|
41
41
|
|
|
42
42
|
const SPACE_ID = /^[a-z0-9](?:[a-z0-9._-]{0,126}[a-z0-9])?$/;
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
"./theme-stylesheets": "./src/theme-stylesheets.mjs",
|
|
51
51
|
"./compliance-coherence": "./src/compliance-coherence.mjs",
|
|
52
52
|
"./compliance/state-directory": "./src/compliance/state-directory.ts",
|
|
53
|
+
"./compliance/static-eligibility": "./src/compliance/static-eligibility.mjs",
|
|
53
54
|
"./integration/lifecycle-host-asset": "./src/integration/lifecycle-host-asset.mjs",
|
|
54
55
|
"./integration/native-ga4-sandbox-asset": "./src/integration/native-ga4-sandbox-asset.mjs",
|
|
55
56
|
"./integration/visitor-region": "./src/integration/visitor-region.mjs"
|
|
@@ -207,7 +207,7 @@
|
|
|
207
207
|
"mountPath": {
|
|
208
208
|
"allOf": [
|
|
209
209
|
{ "$ref": "#/$defs/path" },
|
|
210
|
-
{ "type": "string", "pattern": "^/(?!(?:\\.[wW][eE][lL][lL]-[kK][nN][oO][wW][nN]|__[eE][vV][iI][dD][eE][nN][cC][eE]|__[pP][rR][eE][vV][iI][eE][wW]|_[aA]|_[aA][vV][aA][iI][lL][aA][bB][iI][lL][iI][tT][yY]|_[aA][sS][tT][rR][oO]|_[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[aA][cC][cC][oO][uU][nN][tT]|[aA][dD][mM][iI][nN]|[aA][pP][iI]|[aA][pP][pP][sS]|[aA][sS][sS][eE][tT][sS]|[aA][uU][tT][hH]|[bB][rR][aA][nN][dD]|[cC][aA][pP][aA][bB][iI][lL][iI][tT][iI][eE][sS]|[cC][aA][tT][aA][lL][oO][gG]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[cC][oO][cC][kK][pP][iI][tT]|[cC][oO][lL][lL][eE][cC][tT][iI][oO][nN][sS]|[dD][aA][sS][hH][bB][oO][aA][rR][dD]|[dD][eE][vV]|[fF][aA][vV][iI][cC][oO][nN]\\.[sS][vV][gG]|[fF][eE][eE][dD]\\.[jJ][sS][oO][nN]|[hH][eE][aA][lL][tT][hH]|[jJ][sS]|[lL][lL][mM][sS]\\.[tT][xX][tT]|[pP][lL][aA][tT][fF][oO][rR][mM]|[pP][rR][eE][vV][iI][eE][wW]|[pP][rR][oO][dD][uU][cC][tT][sS]|[pP][uU][bB][lL][iI][cC][aA][tT][iI][oO][nN]-[dD][iI][sS][cC][oO][vV][eE][rR][yY]|[rR][oO][bB][oO][tT][sS]\\.[tT][xX][tT]|[sS][aA][vV][eE][dD]|[sS][eE][aA][rR][cC][hH]|[sS][eE][aA][rR][cC][hH]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[sS][hH][aA][rR][eE][dD]|[sS][iI][tT][eE][mM][aA][pP]\\.[xX][mM][lL]|[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[tT][eE][nN][aA][nN][tT][sS])(?:/|$))" }
|
|
210
|
+
{ "type": "string", "pattern": "^/(?!(?:\\.[wW][eE][lL][lL]-[kK][nN][oO][wW][nN]|__[eE][vV][iI][dD][eE][nN][cC][eE]|__[pP][rR][eE][vV][iI][eE][wW]|_[aA]|_[aA][vV][aA][iI][lL][aA][bB][iI][lL][iI][tT][yY]|_[aA][sS][tT][rR][oO]|_[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[aA][cC][cC][oO][uU][nN][tT]|[aA][dD][mM][iI][nN]|[aA][pP][iI]|[aA][pP][pP][sS]|[aA][sS][sS][eE][tT][sS]|[aA][uU][tT][hH]|[bB][rR][aA][nN][dD]|[cC][aA][pP][aA][bB][iI][lL][iI][tT][iI][eE][sS]|[cC][aA][tT][aA][lL][oO][gG]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[cC][oO][cC][kK][pP][iI][tT]|[cC][oO][lL][lL][eE][cC][tT][iI][oO][nN][sS]|[dD][aA][sS][hH][bB][oO][aA][rR][dD]|[dD][eE][vV]|[eE][lL][iI][gG][iI][bB][iI][lL][iI][tT][yY]\\.[jJ][sS][oO][nN]|[fF][aA][vV][iI][cC][oO][nN]\\.[sS][vV][gG]|[fF][eE][eE][dD]\\.[jJ][sS][oO][nN]|[hH][eE][aA][lL][tT][hH]|[jJ][sS]|[lL][lL][mM][sS]\\.[tT][xX][tT]|[pP][lL][aA][tT][fF][oO][rR][mM]|[pP][rR][eE][vV][iI][eE][wW]|[pP][rR][oO][dD][uU][cC][tT][sS]|[pP][uU][bB][lL][iI][cC][aA][tT][iI][oO][nN]-[dD][iI][sS][cC][oO][vV][eE][rR][yY]|[rR][oO][bB][oO][tT][sS]\\.[tT][xX][tT]|[sS][aA][vV][eE][dD]|[sS][eE][aA][rR][cC][hH]|[sS][eE][aA][rR][cC][hH]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[sS][hH][aA][rR][eE][dD]|[sS][iI][tT][eE][mM][aA][pP]\\.[xX][mM][lL]|[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[tT][eE][nN][aA][nN][tT][sS])(?:/|$))" }
|
|
211
211
|
]
|
|
212
212
|
},
|
|
213
213
|
"aliasPath": {
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"mountPath": {
|
|
68
68
|
"allOf": [
|
|
69
69
|
{ "$ref": "#/$defs/normalizedPath" },
|
|
70
|
-
{ "type": "string", "pattern": "^/(?!(?:\\.[wW][eE][lL][lL]-[kK][nN][oO][wW][nN]|__[eE][vV][iI][dD][eE][nN][cC][eE]|__[pP][rR][eE][vV][iI][eE][wW]|_[aA]|_[aA][vV][aA][iI][lL][aA][bB][iI][lL][iI][tT][yY]|_[aA][sS][tT][rR][oO]|_[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[aA][cC][cC][oO][uU][nN][tT]|[aA][dD][mM][iI][nN]|[aA][pP][iI]|[aA][pP][pP][sS]|[aA][sS][sS][eE][tT][sS]|[aA][uU][tT][hH]|[bB][rR][aA][nN][dD]|[cC][aA][pP][aA][bB][iI][lL][iI][tT][iI][eE][sS]|[cC][aA][tT][aA][lL][oO][gG]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[cC][oO][cC][kK][pP][iI][tT]|[cC][oO][lL][lL][eE][cC][tT][iI][oO][nN][sS]|[dD][aA][sS][hH][bB][oO][aA][rR][dD]|[dD][eE][vV]|[fF][aA][vV][iI][cC][oO][nN]\\.[sS][vV][gG]|[fF][eE][eE][dD]\\.[jJ][sS][oO][nN]|[hH][eE][aA][lL][tT][hH]|[jJ][sS]|[lL][lL][mM][sS]\\.[tT][xX][tT]|[pP][lL][aA][tT][fF][oO][rR][mM]|[pP][rR][eE][vV][iI][eE][wW]|[pP][rR][oO][dD][uU][cC][tT][sS]|[pP][uU][bB][lL][iI][cC][aA][tT][iI][oO][nN]-[dD][iI][sS][cC][oO][vV][eE][rR][yY]|[rR][oO][bB][oO][tT][sS]\\.[tT][xX][tT]|[sS][aA][vV][eE][dD]|[sS][eE][aA][rR][cC][hH]|[sS][eE][aA][rR][cC][hH]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[sS][hH][aA][rR][eE][dD]|[sS][iI][tT][eE][mM][aA][pP]\\.[xX][mM][lL]|[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[tT][eE][nN][aA][nN][tT][sS])(?:/|$))" }
|
|
70
|
+
{ "type": "string", "pattern": "^/(?!(?:\\.[wW][eE][lL][lL]-[kK][nN][oO][wW][nN]|__[eE][vV][iI][dD][eE][nN][cC][eE]|__[pP][rR][eE][vV][iI][eE][wW]|_[aA]|_[aA][vV][aA][iI][lL][aA][bB][iI][lL][iI][tT][yY]|_[aA][sS][tT][rR][oO]|_[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[aA][cC][cC][oO][uU][nN][tT]|[aA][dD][mM][iI][nN]|[aA][pP][iI]|[aA][pP][pP][sS]|[aA][sS][sS][eE][tT][sS]|[aA][uU][tT][hH]|[bB][rR][aA][nN][dD]|[cC][aA][pP][aA][bB][iI][lL][iI][tT][iI][eE][sS]|[cC][aA][tT][aA][lL][oO][gG]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[cC][oO][cC][kK][pP][iI][tT]|[cC][oO][lL][lL][eE][cC][tT][iI][oO][nN][sS]|[dD][aA][sS][hH][bB][oO][aA][rR][dD]|[dD][eE][vV]|[eE][lL][iI][gG][iI][bB][iI][lL][iI][tT][yY]\\.[jJ][sS][oO][nN]|[fF][aA][vV][iI][cC][oO][nN]\\.[sS][vV][gG]|[fF][eE][eE][dD]\\.[jJ][sS][oO][nN]|[hH][eE][aA][lL][tT][hH]|[jJ][sS]|[lL][lL][mM][sS]\\.[tT][xX][tT]|[pP][lL][aA][tT][fF][oO][rR][mM]|[pP][rR][eE][vV][iI][eE][wW]|[pP][rR][oO][dD][uU][cC][tT][sS]|[pP][uU][bB][lL][iI][cC][aA][tT][iI][oO][nN]-[dD][iI][sS][cC][oO][vV][eE][rR][yY]|[rR][oO][bB][oO][tT][sS]\\.[tT][xX][tT]|[sS][aA][vV][eE][dD]|[sS][eE][aA][rR][cC][hH]|[sS][eE][aA][rR][cC][hH]-[iI][nN][dD][eE][xX]\\.[jJ][sS][oO][nN]|[sS][hH][aA][rR][eE][dD]|[sS][iI][tT][eE][mM][aA][pP]\\.[xX][mM][lL]|[sS][tT][yY][lL][eE]-[gG][uU][iI][dD][eE]|[tT][eE][nN][aA][nN][tT][sS])(?:/|$))" }
|
|
71
71
|
]
|
|
72
72
|
},
|
|
73
73
|
"aliasPath": {
|
|
@@ -327,6 +327,19 @@ function matchesSubject(
|
|
|
327
327
|
return evalPredicate(rule.when.subjectMatch, line);
|
|
328
328
|
}
|
|
329
329
|
|
|
330
|
+
/**
|
|
331
|
+
* Whether `rule`'s `subjectMatch` selects a product with this `profile`: `"true"` / `"false"`, or
|
|
332
|
+
* `"indeterminate"` when a fact it reads is unknown. A rule with no `subjectMatch` selects every
|
|
333
|
+
* product. Exposed so the static eligibility projection scopes a registry rule to the products the
|
|
334
|
+
* engine itself would hold to it, rather than restating the predicate.
|
|
335
|
+
*/
|
|
336
|
+
export function ruleSubjectOutcome(
|
|
337
|
+
rule: ComplianceRule,
|
|
338
|
+
profile: ProductComplianceProfile,
|
|
339
|
+
): "true" | "false" | "indeterminate" {
|
|
340
|
+
return matchesSubject(rule, { sku: "", quantity: 1, profile });
|
|
341
|
+
}
|
|
342
|
+
|
|
330
343
|
export function evaluateCartCompliance(
|
|
331
344
|
input: CartComplianceInput,
|
|
332
345
|
): CartComplianceDecision {
|
|
@@ -235,6 +235,11 @@ export interface StateDirectoryRule {
|
|
|
235
235
|
export interface ProductStateEligibilityInput {
|
|
236
236
|
/** Explicit per-product ship-to allow-list (when non-empty, only listed states are eligible). */
|
|
237
237
|
allowedStates?: readonly string[] | null;
|
|
238
|
+
/**
|
|
239
|
+
* The product declares an allow-list that names no valid state (`allowedRegions === []`): it
|
|
240
|
+
* ships nowhere. Distinct from an absent allow-list, which restricts nothing.
|
|
241
|
+
*/
|
|
242
|
+
shipsNowhere?: boolean;
|
|
238
243
|
/** Explicit per-product no-ship list. */
|
|
239
244
|
restrictedStates?: readonly string[] | null;
|
|
240
245
|
/** Product tags; `directory:<ST>`, `state-directory:<ST>`, and `state:<ST>` grant directory eligibility. */
|
|
@@ -243,6 +248,11 @@ export interface ProductStateEligibilityInput {
|
|
|
243
248
|
registryListings?: CatalogReading<RegistryListingMap> | null;
|
|
244
249
|
/** City registries the ship-to falls under (e.g. `["SF"]`). */
|
|
245
250
|
localJurisdictions?: readonly string[] | null;
|
|
251
|
+
/**
|
|
252
|
+
* States whose registry listing this product must hold, in addition to the store-wide
|
|
253
|
+
* `config.directoryStates` (a registry rule that selects this product, not every product).
|
|
254
|
+
*/
|
|
255
|
+
registryStates?: readonly string[] | null;
|
|
246
256
|
}
|
|
247
257
|
|
|
248
258
|
export type ProductStateEligibilityReason =
|
|
@@ -280,6 +290,9 @@ export function evaluateProductStateEligibility(
|
|
|
280
290
|
config?: StateDirectoryRule | null,
|
|
281
291
|
): ProductStateEligibilityDecision {
|
|
282
292
|
const normalized = normalizeUsStateCode(stateCode);
|
|
293
|
+
if (product.shipsNowhere === true) {
|
|
294
|
+
return { eligible: false, reason: "state-restricted", stateCode: normalized };
|
|
295
|
+
}
|
|
283
296
|
if (!normalized) {
|
|
284
297
|
return { eligible: true, reason: "no-state-selected", stateCode: null };
|
|
285
298
|
}
|
|
@@ -297,6 +310,7 @@ export function evaluateProductStateEligibility(
|
|
|
297
310
|
|
|
298
311
|
const productAllowed = normalizeStateSet(product.allowedStates);
|
|
299
312
|
const directoryStates = normalizeStateSet(config?.directoryStates);
|
|
313
|
+
for (const code of normalizeStateSet(product.registryStates)) directoryStates.add(code);
|
|
300
314
|
if (directoryStates.has(normalized)) {
|
|
301
315
|
const registry = resolveRegistryEligibility(product.registryListings, normalized, product.localJurisdictions);
|
|
302
316
|
if (registry.refused) {
|
|
@@ -377,7 +391,7 @@ export function revalidateCartForState<TLine extends CartRevalidationLine>(
|
|
|
377
391
|
config?: StateDirectoryRule | null,
|
|
378
392
|
): CartStateRevalidationResult<TLine> {
|
|
379
393
|
const normalized = normalizeUsStateCode(stateCode);
|
|
380
|
-
if (!normalized) {
|
|
394
|
+
if (!normalized && !lines.some((line) => resolveProduct(line)?.shipsNowhere === true)) {
|
|
381
395
|
return {
|
|
382
396
|
stateCode: null,
|
|
383
397
|
kept: [...lines],
|
|
@@ -403,6 +417,6 @@ export function revalidateCartForState<TLine extends CartRevalidationLine>(
|
|
|
403
417
|
stateCode: normalized,
|
|
404
418
|
kept,
|
|
405
419
|
removed,
|
|
406
|
-
notice: formatRemovalNotice(removed, normalized),
|
|
420
|
+
notice: formatRemovalNotice(removed, normalized ?? "your destination"),
|
|
407
421
|
};
|
|
408
422
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export const STATIC_ELIGIBILITY_SCHEMA: "tot.static-eligibility/v1";
|
|
2
|
+
export const STATIC_ELIGIBILITY_ROUTE: "/eligibility.json";
|
|
3
|
+
export const STATIC_ELIGIBILITY_META_NAME: "tot-static-eligibility";
|
|
4
|
+
export const STATIC_ELIGIBILITY_MAX_BYTES: number;
|
|
5
|
+
|
|
6
|
+
export interface StaticEligibilityProvenance {
|
|
7
|
+
tenant: string;
|
|
8
|
+
catalogRevision: string;
|
|
9
|
+
rulesetVersion: string;
|
|
10
|
+
}
|
|
11
|
+
export interface StaticEligibilityZipScope {
|
|
12
|
+
region: string;
|
|
13
|
+
jurisdiction: string | null;
|
|
14
|
+
zipPrefixes: string[];
|
|
15
|
+
}
|
|
16
|
+
export interface StaticEligibilityProduct {
|
|
17
|
+
/** `null` = no allow-list; `[]` = ships nowhere. */
|
|
18
|
+
allowedStates: string[] | null;
|
|
19
|
+
restrictedStates: string[];
|
|
20
|
+
directoryStates: string[];
|
|
21
|
+
/** States whose registry listing this product must hold (the rule pack's registry rules that select it). */
|
|
22
|
+
registryStates: string[];
|
|
23
|
+
registryListings: Record<string, string> | null;
|
|
24
|
+
}
|
|
25
|
+
export interface StaticEligibilityArtifact {
|
|
26
|
+
schema: "tot.static-eligibility/v1";
|
|
27
|
+
provenance: StaticEligibilityProvenance;
|
|
28
|
+
rules: {
|
|
29
|
+
directoryStates: string[];
|
|
30
|
+
restrictedStates: string[];
|
|
31
|
+
eligibleStates: string[];
|
|
32
|
+
zipScopes: StaticEligibilityZipScope[];
|
|
33
|
+
};
|
|
34
|
+
products: Record<string, StaticEligibilityProduct>;
|
|
35
|
+
}
|
|
36
|
+
export type StaticEligibilityVerdict =
|
|
37
|
+
| { ok: true; artifact: StaticEligibilityArtifact }
|
|
38
|
+
| { ok: false; reason: string };
|
|
39
|
+
export interface StaticEligibilityPointer {
|
|
40
|
+
url: string;
|
|
41
|
+
digest: string | null;
|
|
42
|
+
tenant: string;
|
|
43
|
+
rulesetVersion: string;
|
|
44
|
+
catalogRevision: string | null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function canonicalJson(value: unknown): string;
|
|
48
|
+
export function buildStaticEligibilityArtifact(input: {
|
|
49
|
+
provenance: StaticEligibilityProvenance;
|
|
50
|
+
rules?: {
|
|
51
|
+
directoryStates?: unknown;
|
|
52
|
+
restrictedStates?: unknown;
|
|
53
|
+
eligibleStates?: unknown;
|
|
54
|
+
zipScopes?: unknown;
|
|
55
|
+
};
|
|
56
|
+
products: ReadonlyArray<{
|
|
57
|
+
handle: string;
|
|
58
|
+
allowedStates?: readonly string[] | null;
|
|
59
|
+
restrictedStates?: readonly string[] | null;
|
|
60
|
+
directoryStates?: readonly string[] | null;
|
|
61
|
+
registryStates?: readonly string[] | null;
|
|
62
|
+
registryListings?: Record<string, string> | null;
|
|
63
|
+
}>;
|
|
64
|
+
}): StaticEligibilityArtifact;
|
|
65
|
+
export function staticEligibilityRevisionSource(
|
|
66
|
+
body: Pick<StaticEligibilityArtifact, "rules" | "products">,
|
|
67
|
+
): string;
|
|
68
|
+
export function serializeStaticEligibilityArtifact(artifact: StaticEligibilityArtifact): string;
|
|
69
|
+
export function validateStaticEligibilityArtifact(
|
|
70
|
+
raw: unknown,
|
|
71
|
+
expected?: Partial<StaticEligibilityProvenance>,
|
|
72
|
+
): StaticEligibilityVerdict;
|
|
73
|
+
export function parseStaticEligibilityArtifact(
|
|
74
|
+
bytes: string | Uint8Array,
|
|
75
|
+
expected?: Partial<StaticEligibilityProvenance>,
|
|
76
|
+
): StaticEligibilityVerdict;
|
|
77
|
+
export function staticEligibilityAddress(digest: string): string;
|
|
78
|
+
export function isStaticEligibilityDigest(value: unknown): value is string;
|
|
79
|
+
export function findStaticEligibilityPointers(
|
|
80
|
+
html: string,
|
|
81
|
+
): Array<{ tag: string; attrs: Record<string, string> }>;
|
|
82
|
+
export function staticEligibilityPointerTag(pointer: {
|
|
83
|
+
url: string;
|
|
84
|
+
tenant: string;
|
|
85
|
+
rulesetVersion: string;
|
|
86
|
+
catalogRevision?: string;
|
|
87
|
+
digest?: string;
|
|
88
|
+
}): string;
|
|
89
|
+
export function pinStaticEligibilityPointer(
|
|
90
|
+
html: string,
|
|
91
|
+
pin: { digest: string; provenance: StaticEligibilityProvenance },
|
|
92
|
+
): { html: string; rewritten: number };
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Static shopper-eligibility artifact (ADR 0013 static serving + ADR 0047 rules engine).
|
|
4
|
+
*
|
|
5
|
+
* A published page filters its catalog cards and gates its add-to-cart forms in the browser, with no
|
|
6
|
+
* origin compute. The data it filters on is ONE public, content-addressed JSON artifact per release,
|
|
7
|
+
* emitted at publish time and discovered from the page through a `<meta name="tot-static-eligibility">`
|
|
8
|
+
* pointer. This module is the single contract for that artifact, shared by the Worker route that
|
|
9
|
+
* renders it, the publish scripts that pin and validate it, and the browser client that consumes it.
|
|
10
|
+
*
|
|
11
|
+
* It is plain `.mjs` (like `declared-tenant-config.mjs`) so the node publish scripts and the Worker
|
|
12
|
+
* bundle import the same bytes. It has no runtime dependency and no I/O.
|
|
13
|
+
*
|
|
14
|
+
* PROVENANCE: `{ tenant, catalogRevision, rulesetVersion }` lives inside the artifact and is repeated
|
|
15
|
+
* on the page pointer; the artifact's `digest` is its sha256, carried by the content address
|
|
16
|
+
* (`/_a/<digest>.json`) and the pointer. A consumer accepts bytes only when the digest and all three
|
|
17
|
+
* provenance fields agree.
|
|
18
|
+
*
|
|
19
|
+
* PUBLIC-ONLY: the schema is a strict allowlist. An unknown key anywhere is a refusal, and key names
|
|
20
|
+
* that denote private data (prices, costs, B2B, drafts, secrets) are refused by name as well, so a
|
|
21
|
+
* future field cannot leak by being added to the producer alone.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const STATIC_ELIGIBILITY_SCHEMA = "tot.static-eligibility/v1";
|
|
25
|
+
/** The Worker route that renders the artifact; the publish step pins it to `/_a/<digest>.json`. */
|
|
26
|
+
export const STATIC_ELIGIBILITY_ROUTE = "/eligibility.json";
|
|
27
|
+
/** `<meta name>` of the page pointer. */
|
|
28
|
+
export const STATIC_ELIGIBILITY_META_NAME = "tot-static-eligibility";
|
|
29
|
+
/** Hard ceiling on artifact bytes (client and publisher both refuse above it). */
|
|
30
|
+
export const STATIC_ELIGIBILITY_MAX_BYTES = 8 * 1024 * 1024;
|
|
31
|
+
|
|
32
|
+
const STATE_CODE = /^[A-Z]{2}$/;
|
|
33
|
+
const HANDLE = /^[A-Za-z0-9][A-Za-z0-9._~-]{0,254}$/;
|
|
34
|
+
const RESERVED_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
35
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/;
|
|
36
|
+
const SAFE_TOKEN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
|
|
37
|
+
const ZIP_PREFIX = /^[0-9]{1,5}$/;
|
|
38
|
+
const JURISDICTION = /^[A-Z]{2}(?:-[A-Z]{2,4})?$/;
|
|
39
|
+
const LISTING_STATUSES = new Set(["listed", "unlisted", "unknown", "conflict", "invalid"]);
|
|
40
|
+
|
|
41
|
+
/** Key names that denote private data; refused anywhere in an artifact regardless of position. */
|
|
42
|
+
const PRIVATE_KEY_PATTERN = /(?:price|cost|margin|wholesale|b2b|draft|secret|token|password|credential|api[_-]?key|email|phone|internal|private|payout|salary)/i;
|
|
43
|
+
|
|
44
|
+
const ARTIFACT_KEYS = ["schema", "provenance", "rules", "products"];
|
|
45
|
+
const PROVENANCE_KEYS = ["tenant", "catalogRevision", "rulesetVersion"];
|
|
46
|
+
const RULES_KEYS = ["directoryStates", "restrictedStates", "eligibleStates", "zipScopes"];
|
|
47
|
+
const ZIP_SCOPE_KEYS = ["region", "jurisdiction", "zipPrefixes"];
|
|
48
|
+
const PRODUCT_KEYS = ["allowedStates", "restrictedStates", "directoryStates", "registryStates", "registryListings"];
|
|
49
|
+
|
|
50
|
+
/** @typedef {{ tenant: string, catalogRevision: string, rulesetVersion: string }} StaticEligibilityProvenance */
|
|
51
|
+
/** @typedef {{ region: string, jurisdiction: string | null, zipPrefixes: string[] }} StaticEligibilityZipScope */
|
|
52
|
+
/**
|
|
53
|
+
* @typedef {{
|
|
54
|
+
* allowedStates: string[] | null,
|
|
55
|
+
* restrictedStates: string[],
|
|
56
|
+
* directoryStates: string[],
|
|
57
|
+
* registryStates: string[],
|
|
58
|
+
* registryListings: Record<string, string> | null,
|
|
59
|
+
* }} StaticEligibilityProduct
|
|
60
|
+
*/
|
|
61
|
+
/**
|
|
62
|
+
* @typedef {{
|
|
63
|
+
* schema: typeof STATIC_ELIGIBILITY_SCHEMA,
|
|
64
|
+
* provenance: StaticEligibilityProvenance,
|
|
65
|
+
* rules: {
|
|
66
|
+
* directoryStates: string[],
|
|
67
|
+
* restrictedStates: string[],
|
|
68
|
+
* eligibleStates: string[],
|
|
69
|
+
* zipScopes: StaticEligibilityZipScope[],
|
|
70
|
+
* },
|
|
71
|
+
* products: Record<string, StaticEligibilityProduct>,
|
|
72
|
+
* }} StaticEligibilityArtifact
|
|
73
|
+
*/
|
|
74
|
+
/** @typedef {{ ok: true, artifact: StaticEligibilityArtifact } | { ok: false, reason: string }} StaticEligibilityVerdict */
|
|
75
|
+
|
|
76
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
77
|
+
function isPlainObject(value) {
|
|
78
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) &&
|
|
79
|
+
(Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @param {unknown} value @returns {string | null} */
|
|
83
|
+
function stateCodeOrNull(value) {
|
|
84
|
+
if (typeof value !== "string") return null;
|
|
85
|
+
const code = value.trim().toUpperCase().replace(/^US-/, "");
|
|
86
|
+
return STATE_CODE.test(code) ? code : null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @param {unknown} list @returns {string[]} */
|
|
90
|
+
function normalizeStateList(list) {
|
|
91
|
+
if (!Array.isArray(list)) return [];
|
|
92
|
+
const out = new Set();
|
|
93
|
+
for (const raw of list) {
|
|
94
|
+
const code = stateCodeOrNull(raw);
|
|
95
|
+
if (code) out.add(code);
|
|
96
|
+
}
|
|
97
|
+
return [...out].sort();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Canonical JSON: every object's keys sorted, no whitespace. Two producers building the same artifact
|
|
102
|
+
* produce the same bytes, so the digest is a property of the content.
|
|
103
|
+
* @param {unknown} value
|
|
104
|
+
* @returns {string}
|
|
105
|
+
*/
|
|
106
|
+
export function canonicalJson(value) {
|
|
107
|
+
if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
|
|
108
|
+
if (value !== null && typeof value === "object") {
|
|
109
|
+
const entries = Object.keys(value).sort()
|
|
110
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalJson(/** @type {any} */ (value)[key])}`);
|
|
111
|
+
return `{${entries.join(",")}}`;
|
|
112
|
+
}
|
|
113
|
+
return JSON.stringify(value);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Build a normalized artifact from producer input. Region lists are uppercased, deduplicated and
|
|
118
|
+
* sorted; `allowedStates` keeps `null` (no allow-list) distinct from `[]` (ships nowhere) — the same
|
|
119
|
+
* distinction `ProductComplianceProfile.allowedRegions` draws, which a lossy encoding would collapse
|
|
120
|
+
* into "no restriction".
|
|
121
|
+
* @param {{
|
|
122
|
+
* provenance: StaticEligibilityProvenance,
|
|
123
|
+
* rules?: { directoryStates?: unknown, restrictedStates?: unknown, eligibleStates?: unknown, zipScopes?: unknown },
|
|
124
|
+
* products: ReadonlyArray<{ handle: string, allowedStates?: readonly string[] | null, restrictedStates?: readonly string[] | null, directoryStates?: readonly string[] | null, registryStates?: readonly string[] | null, registryListings?: Record<string, string> | null }>,
|
|
125
|
+
* }} input
|
|
126
|
+
* @returns {StaticEligibilityArtifact}
|
|
127
|
+
*/
|
|
128
|
+
export function buildStaticEligibilityArtifact(input) {
|
|
129
|
+
/** @type {Record<string, StaticEligibilityProduct>} */
|
|
130
|
+
const products = {};
|
|
131
|
+
for (const product of [...input.products].sort((a, b) => a.handle < b.handle ? -1 : a.handle > b.handle ? 1 : 0)) {
|
|
132
|
+
/** @type {Record<string, string> | null} */
|
|
133
|
+
let registryListings = null;
|
|
134
|
+
if (isPlainObject(product.registryListings)) {
|
|
135
|
+
registryListings = {};
|
|
136
|
+
for (const jurisdiction of Object.keys(product.registryListings).sort()) {
|
|
137
|
+
const status = product.registryListings[jurisdiction];
|
|
138
|
+
if (JURISDICTION.test(jurisdiction) && typeof status === "string" && LISTING_STATUSES.has(status)) {
|
|
139
|
+
registryListings[jurisdiction] = status;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
products[product.handle] = {
|
|
144
|
+
allowedStates: product.allowedStates == null ? null : normalizeStateList(product.allowedStates),
|
|
145
|
+
restrictedStates: normalizeStateList(product.restrictedStates),
|
|
146
|
+
directoryStates: normalizeStateList(product.directoryStates),
|
|
147
|
+
registryStates: normalizeStateList(product.registryStates),
|
|
148
|
+
registryListings,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const zipScopes = Array.isArray(input.rules?.zipScopes) ? input.rules.zipScopes : [];
|
|
152
|
+
return {
|
|
153
|
+
schema: STATIC_ELIGIBILITY_SCHEMA,
|
|
154
|
+
provenance: {
|
|
155
|
+
tenant: input.provenance.tenant,
|
|
156
|
+
catalogRevision: input.provenance.catalogRevision,
|
|
157
|
+
rulesetVersion: input.provenance.rulesetVersion,
|
|
158
|
+
},
|
|
159
|
+
rules: {
|
|
160
|
+
directoryStates: normalizeStateList(input.rules?.directoryStates),
|
|
161
|
+
restrictedStates: normalizeStateList(input.rules?.restrictedStates),
|
|
162
|
+
eligibleStates: normalizeStateList(input.rules?.eligibleStates),
|
|
163
|
+
zipScopes: zipScopes.flatMap((scope) => {
|
|
164
|
+
const region = stateCodeOrNull(scope?.region);
|
|
165
|
+
const prefixes = Array.isArray(scope?.zipPrefixes)
|
|
166
|
+
? [...new Set(scope.zipPrefixes.filter((/** @type {unknown} */ p) => typeof p === "string" && ZIP_PREFIX.test(p)))].sort()
|
|
167
|
+
: [];
|
|
168
|
+
if (!region || prefixes.length === 0) return [];
|
|
169
|
+
const jurisdiction = typeof scope.jurisdiction === "string" && JURISDICTION.test(scope.jurisdiction)
|
|
170
|
+
? scope.jurisdiction : null;
|
|
171
|
+
return [{ region, jurisdiction, zipPrefixes: /** @type {string[]} */ (prefixes) }];
|
|
172
|
+
}).sort((a, b) => canonicalJson(a) < canonicalJson(b) ? -1 : 1),
|
|
173
|
+
},
|
|
174
|
+
products,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The canonical text a producer hashes (sha256, first 32 hex characters) to stamp `catalogRevision`:
|
|
180
|
+
* the eligibility inputs only (rules + products), so a catalog or rules change always changes the
|
|
181
|
+
* provenance and an unrelated change never does. The caller hashes it with its own sha256, because
|
|
182
|
+
* the Worker's is asynchronous and node's is not.
|
|
183
|
+
* @param {Pick<StaticEligibilityArtifact, "rules" | "products">} body
|
|
184
|
+
* @returns {string}
|
|
185
|
+
*/
|
|
186
|
+
export function staticEligibilityRevisionSource(body) {
|
|
187
|
+
return canonicalJson({ rules: body.rules, products: body.products });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** @param {StaticEligibilityArtifact} artifact @returns {string} */
|
|
191
|
+
export function serializeStaticEligibilityArtifact(artifact) {
|
|
192
|
+
return canonicalJson(artifact);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @param {unknown} node
|
|
197
|
+
* @param {string} path
|
|
198
|
+
* @returns {string | null} the offending path, or null
|
|
199
|
+
*/
|
|
200
|
+
function privateKeyPath(node, path = "$") {
|
|
201
|
+
if (Array.isArray(node)) {
|
|
202
|
+
for (let i = 0; i < node.length; i++) {
|
|
203
|
+
const hit = privateKeyPath(node[i], `${path}[${i}]`);
|
|
204
|
+
if (hit) return hit;
|
|
205
|
+
}
|
|
206
|
+
} else if (node !== null && typeof node === "object") {
|
|
207
|
+
for (const [key, value] of Object.entries(node)) {
|
|
208
|
+
if (PRIVATE_KEY_PATTERN.test(key)) return `${path}.${key}`;
|
|
209
|
+
const hit = privateKeyPath(value, `${path}.${key}`);
|
|
210
|
+
if (hit) return hit;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @param {Record<string, unknown>} object
|
|
218
|
+
* @param {string[]} allowed
|
|
219
|
+
* @returns {string | null} the first key outside `allowed`
|
|
220
|
+
*/
|
|
221
|
+
function unknownKey(object, allowed) {
|
|
222
|
+
for (const key of Object.keys(object)) if (!allowed.includes(key)) return key;
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** @param {unknown} list @param {RegExp} pattern @returns {boolean} */
|
|
227
|
+
function isCodeList(list, pattern) {
|
|
228
|
+
return Array.isArray(list) && list.every((item) => typeof item === "string" && pattern.test(item));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Validate a parsed artifact against the strict public-only schema. Returns the SAME object on
|
|
233
|
+
* success (nothing is coerced), or the first reason it is refused. `expected` additionally requires
|
|
234
|
+
* the provenance to match what the page pointer announced.
|
|
235
|
+
* @param {unknown} raw
|
|
236
|
+
* @param {Partial<StaticEligibilityProvenance>} [expected]
|
|
237
|
+
* @returns {StaticEligibilityVerdict}
|
|
238
|
+
*/
|
|
239
|
+
export function validateStaticEligibilityArtifact(raw, expected) {
|
|
240
|
+
if (!isPlainObject(raw)) return { ok: false, reason: "artifact-not-object" };
|
|
241
|
+
const extra = unknownKey(raw, ARTIFACT_KEYS);
|
|
242
|
+
if (extra) return { ok: false, reason: `unknown-key:${extra}` };
|
|
243
|
+
if (raw.schema !== STATIC_ELIGIBILITY_SCHEMA) return { ok: false, reason: "schema-mismatch" };
|
|
244
|
+
const privatePath = privateKeyPath(raw);
|
|
245
|
+
if (privatePath) return { ok: false, reason: `private-key:${privatePath}` };
|
|
246
|
+
|
|
247
|
+
const provenance = raw.provenance;
|
|
248
|
+
if (!isPlainObject(provenance)) return { ok: false, reason: "provenance-missing" };
|
|
249
|
+
const provExtra = unknownKey(provenance, PROVENANCE_KEYS);
|
|
250
|
+
if (provExtra) return { ok: false, reason: `unknown-key:provenance.${provExtra}` };
|
|
251
|
+
for (const key of PROVENANCE_KEYS) {
|
|
252
|
+
const value = provenance[key];
|
|
253
|
+
if (typeof value !== "string" || !SAFE_TOKEN.test(value)) return { ok: false, reason: `provenance-invalid:${key}` };
|
|
254
|
+
}
|
|
255
|
+
for (const key of PROVENANCE_KEYS) {
|
|
256
|
+
const want = expected?.[/** @type {keyof StaticEligibilityProvenance} */ (key)];
|
|
257
|
+
if (want !== undefined && provenance[key] !== want) return { ok: false, reason: `provenance-mismatch:${key}` };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const rules = raw.rules;
|
|
261
|
+
if (!isPlainObject(rules)) return { ok: false, reason: "rules-missing" };
|
|
262
|
+
const rulesExtra = unknownKey(rules, RULES_KEYS);
|
|
263
|
+
if (rulesExtra) return { ok: false, reason: `unknown-key:rules.${rulesExtra}` };
|
|
264
|
+
for (const key of ["directoryStates", "restrictedStates", "eligibleStates"]) {
|
|
265
|
+
if (!isCodeList(rules[key], STATE_CODE)) return { ok: false, reason: `rules-invalid:${key}` };
|
|
266
|
+
}
|
|
267
|
+
if (!Array.isArray(rules.zipScopes)) return { ok: false, reason: "rules-invalid:zipScopes" };
|
|
268
|
+
for (const scope of rules.zipScopes) {
|
|
269
|
+
if (!isPlainObject(scope)) return { ok: false, reason: "rules-invalid:zipScopes" };
|
|
270
|
+
const scopeExtra = unknownKey(scope, ZIP_SCOPE_KEYS);
|
|
271
|
+
if (scopeExtra) return { ok: false, reason: `unknown-key:zipScopes.${scopeExtra}` };
|
|
272
|
+
if (typeof scope.region !== "string" || !STATE_CODE.test(scope.region) ||
|
|
273
|
+
!(scope.jurisdiction === null || (typeof scope.jurisdiction === "string" && JURISDICTION.test(scope.jurisdiction))) ||
|
|
274
|
+
!isCodeList(scope.zipPrefixes, ZIP_PREFIX) || /** @type {unknown[]} */ (scope.zipPrefixes).length === 0) {
|
|
275
|
+
return { ok: false, reason: "rules-invalid:zipScopes" };
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const products = raw.products;
|
|
280
|
+
if (!isPlainObject(products)) return { ok: false, reason: "products-missing" };
|
|
281
|
+
for (const [handle, product] of Object.entries(products)) {
|
|
282
|
+
if (RESERVED_KEYS.has(handle) || !HANDLE.test(handle)) return { ok: false, reason: "products-invalid:handle" };
|
|
283
|
+
if (!isPlainObject(product)) return { ok: false, reason: `products-invalid:${handle}` };
|
|
284
|
+
const productExtra = unknownKey(product, PRODUCT_KEYS);
|
|
285
|
+
if (productExtra) return { ok: false, reason: `unknown-key:products.${productExtra}` };
|
|
286
|
+
// `null` = no allow-list; `[]` = ships nowhere. A missing key is neither: refuse it.
|
|
287
|
+
if (!(product.allowedStates === null || isCodeList(product.allowedStates, STATE_CODE))) {
|
|
288
|
+
return { ok: false, reason: `products-invalid:${handle}.allowedStates` };
|
|
289
|
+
}
|
|
290
|
+
if (!isCodeList(product.restrictedStates, STATE_CODE)) return { ok: false, reason: `products-invalid:${handle}.restrictedStates` };
|
|
291
|
+
if (!isCodeList(product.directoryStates, STATE_CODE)) return { ok: false, reason: `products-invalid:${handle}.directoryStates` };
|
|
292
|
+
if (!isCodeList(product.registryStates, STATE_CODE)) return { ok: false, reason: `products-invalid:${handle}.registryStates` };
|
|
293
|
+
const listings = product.registryListings;
|
|
294
|
+
if (listings !== null) {
|
|
295
|
+
if (!isPlainObject(listings)) return { ok: false, reason: `products-invalid:${handle}.registryListings` };
|
|
296
|
+
for (const [jurisdiction, status] of Object.entries(listings)) {
|
|
297
|
+
if (!JURISDICTION.test(jurisdiction) || typeof status !== "string" || !LISTING_STATUSES.has(status)) {
|
|
298
|
+
return { ok: false, reason: `products-invalid:${handle}.registryListings` };
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return { ok: true, artifact: /** @type {StaticEligibilityArtifact} */ (raw) };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Parse and validate artifact bytes: size ceiling, UTF-8 JSON, strict schema.
|
|
308
|
+
* @param {string | Uint8Array} bytes
|
|
309
|
+
* @param {Partial<StaticEligibilityProvenance>} [expected]
|
|
310
|
+
* @returns {StaticEligibilityVerdict}
|
|
311
|
+
*/
|
|
312
|
+
export function parseStaticEligibilityArtifact(bytes, expected) {
|
|
313
|
+
const length = typeof bytes === "string" ? bytes.length : bytes.byteLength;
|
|
314
|
+
if (length > STATIC_ELIGIBILITY_MAX_BYTES) return { ok: false, reason: "artifact-too-large" };
|
|
315
|
+
let parsed;
|
|
316
|
+
try {
|
|
317
|
+
const text = typeof bytes === "string" ? bytes : new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
318
|
+
parsed = JSON.parse(text);
|
|
319
|
+
} catch {
|
|
320
|
+
return { ok: false, reason: "artifact-not-json" };
|
|
321
|
+
}
|
|
322
|
+
return validateStaticEligibilityArtifact(parsed, expected);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** The pinned, content-addressed URL path of an artifact. */
|
|
326
|
+
export function staticEligibilityAddress(/** @type {string} */ digest) {
|
|
327
|
+
return `/_a/${digest}.json`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** @param {unknown} value */
|
|
331
|
+
export function isStaticEligibilityDigest(value) {
|
|
332
|
+
return typeof value === "string" && SHA256_HEX.test(value);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* @typedef {{ url: string, digest: string | null, tenant: string, rulesetVersion: string, catalogRevision: string | null }} StaticEligibilityPointer
|
|
337
|
+
*/
|
|
338
|
+
|
|
339
|
+
const POINTER_TAG = new RegExp(`<meta\\b[^>]*\\bname=["']${STATIC_ELIGIBILITY_META_NAME}["'][^>]*>`, "gi");
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The attributes of every pointer tag in `html`, in document order. A page with more than one is
|
|
343
|
+
* ambiguous and a caller refuses it.
|
|
344
|
+
* @param {string} html
|
|
345
|
+
* @returns {Array<{ tag: string, attrs: Record<string, string> }>}
|
|
346
|
+
*/
|
|
347
|
+
export function findStaticEligibilityPointers(html) {
|
|
348
|
+
const out = [];
|
|
349
|
+
for (const match of html.matchAll(POINTER_TAG)) {
|
|
350
|
+
/** @type {Record<string, string>} */
|
|
351
|
+
const attrs = {};
|
|
352
|
+
for (const attr of match[0].matchAll(/\s([a-z][a-z0-9:-]*)=(?:"([^"]*)"|'([^']*)')/gi)) {
|
|
353
|
+
attrs[attr[1].toLowerCase()] = attr[2] ?? attr[3] ?? "";
|
|
354
|
+
}
|
|
355
|
+
out.push({ tag: match[0], attrs });
|
|
356
|
+
}
|
|
357
|
+
return out;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Render the pointer tag. At render time `url` is the live Worker route and the digest and revision
|
|
362
|
+
* are empty; `pinStaticEligibilityPointer` fills them in when the release is materialized.
|
|
363
|
+
* @param {{ url: string, tenant: string, rulesetVersion: string, catalogRevision?: string, digest?: string }} pointer
|
|
364
|
+
* @returns {string}
|
|
365
|
+
*/
|
|
366
|
+
export function staticEligibilityPointerTag(pointer) {
|
|
367
|
+
const escape = (/** @type {string} */ value) => value.replace(/[&<>"]/g, (c) =>
|
|
368
|
+
({ "&": "&", "<": "<", ">": ">", '"': """ })[/** @type {"&"|"<"|">"|'"'} */ (c)]);
|
|
369
|
+
return `<meta name="${STATIC_ELIGIBILITY_META_NAME}" content="${escape(pointer.url)}"` +
|
|
370
|
+
` data-tenant="${escape(pointer.tenant)}" data-ruleset-version="${escape(pointer.rulesetVersion)}"` +
|
|
371
|
+
` data-catalog-revision="${escape(pointer.catalogRevision ?? "")}" data-sha256="${escape(pointer.digest ?? "")}">`;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Rewrite every pointer tag in `html` to the pinned address, bound to the artifact's digest and
|
|
376
|
+
* provenance. Returns the html and how many tags were rewritten.
|
|
377
|
+
* @param {string} html
|
|
378
|
+
* @param {{ digest: string, provenance: StaticEligibilityProvenance }} pin
|
|
379
|
+
* @returns {{ html: string, rewritten: number }}
|
|
380
|
+
*/
|
|
381
|
+
export function pinStaticEligibilityPointer(html, pin) {
|
|
382
|
+
let rewritten = 0;
|
|
383
|
+
const next = html.replace(POINTER_TAG, () => {
|
|
384
|
+
rewritten += 1;
|
|
385
|
+
return staticEligibilityPointerTag({
|
|
386
|
+
url: staticEligibilityAddress(pin.digest),
|
|
387
|
+
digest: pin.digest,
|
|
388
|
+
tenant: pin.provenance.tenant,
|
|
389
|
+
rulesetVersion: pin.provenance.rulesetVersion,
|
|
390
|
+
catalogRevision: pin.provenance.catalogRevision,
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
return { html: next, rewritten };
|
|
394
|
+
}
|
|
@@ -551,7 +551,7 @@ export function closedCatalogPath(uri) {
|
|
|
551
551
|
if (/^\/(?:versions|live|assets)(?:\/|$)/.test(path)) return "deny";
|
|
552
552
|
if (path === "/manifest.json" && uri !== path) return "deny";
|
|
553
553
|
if (/^\/(?:products|collections|search|saved|_availability|publication-discovery|tenants)(?:\/|$)/.test(path)) return "admit";
|
|
554
|
-
if (/^\/(?:catalog-index\.json|search-index\.json|llms\.txt|feed\.json|sitemap-(?:products|collections)-[0-9]+\.xml)(?:\/|$)/.test(path)) return "admit";
|
|
554
|
+
if (/^\/(?:catalog-index\.json|eligibility\.json|search-index\.json|llms\.txt|feed\.json|sitemap-(?:products|collections)-[0-9]+\.xml)(?:\/|$)/.test(path)) return "admit";
|
|
555
555
|
if (path.startsWith("/_a/") && !/\.(?:css|js|mjs|woff2?)$/.test(path)) return "admit";
|
|
556
556
|
return "allow";
|
|
557
557
|
}
|