@tokenoftrust/storefront-runner 8.0.1 → 8.1.1
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/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/renderChain.ts +44 -25
- package/apps/storefront/src/middleware/routeBranch.ts +1 -0
- package/apps/storefront/src/middleware/staticBundle.ts +3 -0
- 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,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
|
}
|