@tokenoftrust/storefront-runner 6.12.10 → 6.12.12
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.
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* `tot_preview` cookies), shared by every middleware layer that consults it: the
|
|
4
4
|
* front door's static passthrough and mirror stage, the control-plane gate, and the
|
|
5
5
|
* render chain. A presented context is verified by `requireValidPreviewToken` and
|
|
6
|
-
* never degrades to "absent"; the control plane and the render chain refuse it with
|
|
6
|
+
* never degrades to "absent" (except a cookie-only expired token, below); the control plane and the render chain refuse it with
|
|
7
7
|
* `previewContextRefusalResponse` before any publication, page-cache, rewrite-target
|
|
8
8
|
* or source resolution.
|
|
9
9
|
*
|
|
@@ -61,6 +61,13 @@ export async function resolveRequestPreviewContext(
|
|
|
61
61
|
secret: input.secret,
|
|
62
62
|
nowSeconds: input.nowSeconds,
|
|
63
63
|
});
|
|
64
|
+
// A `tot_preview` cookie alone that has merely expired grants nothing and proves no
|
|
65
|
+
// tampering: it reads as absent, so the viewer falls back to session or public
|
|
66
|
+
// access instead of a dead-end 403. Any presented `__preview` query value, and every
|
|
67
|
+
// other refusal (bad signature, wrong tenant, ambiguity), still refuses.
|
|
68
|
+
if (context.status === "refused" && context.reason === "expired" && queryValues.length === 0) {
|
|
69
|
+
return { context: { status: "absent" }, presented, cookieValue: null };
|
|
70
|
+
}
|
|
64
71
|
if (
|
|
65
72
|
context.status === "refused" &&
|
|
66
73
|
(isPreviewDispatcherPath(input.url.pathname) || isAuthPath(input.renderPath))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/storefront-runner",
|
|
3
|
-
"version": "6.12.
|
|
3
|
+
"version": "6.12.12",
|
|
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",
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* The tenant sitemap contract, shared by the Worker that renders it and the publish tooling
|
|
4
|
+
* that materializes and validates it on both serving substrates.
|
|
5
|
+
*
|
|
6
|
+
* `/sitemap.xml` is a `<sitemapindex>`. Its entries are typed, paged child sitemaps at
|
|
7
|
+
* `/sitemap-<group>-<page>.xml`, each a `<urlset>` of at most {@link SITEMAP_MAX_URLS_PER_CHILD_V1}
|
|
8
|
+
* URLs. A child path ends in `.xml`, so S3/CloudFront and the Cloudflare static bundle both serve
|
|
9
|
+
* it as a plain file with no rewrite, and the materializer writes it at the same bundle path.
|
|
10
|
+
*
|
|
11
|
+
* Pure: no I/O, no platform imports.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** @typedef {{loc: string, lastmod?: string}} SitemapUrl */
|
|
15
|
+
|
|
16
|
+
/** The one discovery entry point robots.txt advertises. */
|
|
17
|
+
export const SITEMAP_INDEX_PATH_V1 = "/sitemap.xml";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* URLs per child sitemap. The protocol allows 50,000; 5,000 keeps each child a small, fast,
|
|
21
|
+
* individually cacheable document and matches the paging large commerce platforms use.
|
|
22
|
+
*/
|
|
23
|
+
export const SITEMAP_MAX_URLS_PER_CHILD_V1 = 5000;
|
|
24
|
+
|
|
25
|
+
/** Child sitemap groups, in the order the index lists them. */
|
|
26
|
+
export const SITEMAP_GROUPS_V1 = Object.freeze(["pages", "collections", "products", "blogs", "build", "spaces"]);
|
|
27
|
+
|
|
28
|
+
const CHILD_PATH = /^\/sitemap-([a-z]+)-([1-9][0-9]{0,5})\.xml$/;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* `/sitemap-<group>-<page>.xml` for a declared group and a 1-based page.
|
|
32
|
+
* @param {string} group @param {number} page @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
export function sitemapChildPathV1(group, page) {
|
|
35
|
+
if (!SITEMAP_GROUPS_V1.includes(group)) throw new Error(`unknown sitemap group ${JSON.stringify(group)}`);
|
|
36
|
+
if (!Number.isInteger(page) || page < 1) throw new Error(`sitemap page must be a positive integer, got ${page}`);
|
|
37
|
+
return `/sitemap-${group}-${page}.xml`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The group and page a child sitemap path names, or null when it names none.
|
|
42
|
+
* @param {unknown} path @returns {{group: string, page: number} | null}
|
|
43
|
+
*/
|
|
44
|
+
export function parseSitemapChildPathV1(path) {
|
|
45
|
+
if (typeof path !== "string") return null;
|
|
46
|
+
const match = CHILD_PATH.exec(path);
|
|
47
|
+
const group = match?.[1];
|
|
48
|
+
if (!match || group === undefined || !SITEMAP_GROUPS_V1.includes(group)) return null;
|
|
49
|
+
return { group, page: Number(match[2]) };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** True for `/sitemap.xml` and for every well-formed child sitemap path. @param {string} path */
|
|
53
|
+
export function isSitemapDocumentPathV1(path) {
|
|
54
|
+
return path === SITEMAP_INDEX_PATH_V1 || parseSitemapChildPathV1(path) !== null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Page each group into children of at most {@link SITEMAP_MAX_URLS_PER_CHILD_V1} URLs, in
|
|
59
|
+
* {@link SITEMAP_GROUPS_V1} order. An empty group yields no child.
|
|
60
|
+
* @param {ReadonlyArray<{group: string, urls: readonly SitemapUrl[]}>} groups
|
|
61
|
+
* @param {number} [maxPerChild]
|
|
62
|
+
* @returns {{group: string, page: number, path: string, urls: SitemapUrl[]}[]}
|
|
63
|
+
*/
|
|
64
|
+
export function chunkSitemapGroupsV1(groups, maxPerChild = SITEMAP_MAX_URLS_PER_CHILD_V1) {
|
|
65
|
+
/** @type {Map<string, SitemapUrl[]>} */
|
|
66
|
+
const byGroup = new Map();
|
|
67
|
+
for (const { group, urls } of groups) {
|
|
68
|
+
if (!SITEMAP_GROUPS_V1.includes(group)) throw new Error(`unknown sitemap group ${JSON.stringify(group)}`);
|
|
69
|
+
byGroup.set(group, [...(byGroup.get(group) ?? []), ...urls]);
|
|
70
|
+
}
|
|
71
|
+
/** @type {{group: string, page: number, path: string, urls: SitemapUrl[]}[]} */
|
|
72
|
+
const children = [];
|
|
73
|
+
for (const group of SITEMAP_GROUPS_V1) {
|
|
74
|
+
const urls = byGroup.get(group) ?? [];
|
|
75
|
+
for (let start = 0, page = 1; start < urls.length; start += maxPerChild, page += 1) {
|
|
76
|
+
children.push({ group, page, path: sitemapChildPathV1(group, page), urls: urls.slice(start, start + maxPerChild) });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return children;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @param {string} value */
|
|
83
|
+
function escapeXml(value) {
|
|
84
|
+
return String(value)
|
|
85
|
+
.replace(/&/g, "&")
|
|
86
|
+
.replace(/</g, "<")
|
|
87
|
+
.replace(/>/g, ">")
|
|
88
|
+
.replace(/"/g, """)
|
|
89
|
+
.replace(/'/g, "'");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The newest `lastmod` among `urls`, spelled as its source spelled it; undefined when none parses.
|
|
94
|
+
* @param {readonly SitemapUrl[]} urls @returns {string | undefined}
|
|
95
|
+
*/
|
|
96
|
+
export function newestLastmodV1(urls) {
|
|
97
|
+
/** @type {string | undefined} */
|
|
98
|
+
let newest;
|
|
99
|
+
let newestTime = -Infinity;
|
|
100
|
+
for (const { lastmod } of urls) {
|
|
101
|
+
const time = lastmod ? Date.parse(lastmod) : NaN;
|
|
102
|
+
if (Number.isFinite(time) && time > newestTime) {
|
|
103
|
+
newest = lastmod;
|
|
104
|
+
newestTime = time;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return newest;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A `<urlset>` document. Locs are written exactly as given. @param {readonly SitemapUrl[]} urls */
|
|
111
|
+
export function renderSitemapUrlsetV1(urls) {
|
|
112
|
+
const rows = urls.map((u) =>
|
|
113
|
+
` <url>\n <loc>${escapeXml(u.loc)}</loc>${u.lastmod ? `\n <lastmod>${escapeXml(u.lastmod)}</lastmod>` : ""}\n </url>`);
|
|
114
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${rows.join("\n")}\n</urlset>\n`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A `<sitemapindex>` naming each child at `base` + its path. `base` is the tenant's canonical
|
|
119
|
+
* base without a trailing slash, path prefix included, so every loc keeps the prefix the page
|
|
120
|
+
* locs carry. Each entry's lastmod is the newest lastmod inside that child, when it has one.
|
|
121
|
+
* @param {string} base @param {readonly {path: string, urls: readonly SitemapUrl[]}[]} children
|
|
122
|
+
*/
|
|
123
|
+
export function renderSitemapIndexV1(base, children) {
|
|
124
|
+
const root = base.replace(/\/+$/, "");
|
|
125
|
+
const rows = children.map((child) => {
|
|
126
|
+
const lastmod = newestLastmodV1(child.urls);
|
|
127
|
+
return ` <sitemap>\n <loc>${escapeXml(`${root}${child.path}`)}</loc>${lastmod ? `\n <lastmod>${escapeXml(lastmod)}</lastmod>` : ""}\n </sitemap>`;
|
|
128
|
+
});
|
|
129
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${rows.join("\n")}\n</sitemapindex>\n`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** True when `xml` is a `<sitemapindex>` rather than a `<urlset>`. @param {string} xml */
|
|
133
|
+
export function isSitemapIndexXmlV1(xml) {
|
|
134
|
+
return /<sitemapindex[\s>]/.test(xml);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The child sitemap locs a `<sitemapindex>` names, exactly as spelled; empty for a `<urlset>`.
|
|
139
|
+
* @param {string} xml @returns {string[]}
|
|
140
|
+
*/
|
|
141
|
+
export function sitemapIndexLocsV1(xml) {
|
|
142
|
+
if (!isSitemapIndexXmlV1(xml)) return [];
|
|
143
|
+
return [...xml.matchAll(/<sitemap>\s*<loc>([^<]*)<\/loc>/g)].map((match) => (match[1] ?? "").trim());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Page URLs a sitemap lists, following an index to its children. `readChild` returns a child's
|
|
148
|
+
* body, or null when it cannot be read (which contributes nothing).
|
|
149
|
+
* @param {string} xml @param {(loc: string) => Promise<string | null>} readChild @returns {Promise<number>}
|
|
150
|
+
*/
|
|
151
|
+
export async function countSitemapUrlsV1(xml, readChild) {
|
|
152
|
+
/** @param {string} body */
|
|
153
|
+
const urls = (body) => (body.match(/<url>/g) ?? []).length;
|
|
154
|
+
if (!isSitemapIndexXmlV1(xml)) return urls(xml);
|
|
155
|
+
const bodies = await Promise.all(sitemapIndexLocsV1(xml).map(readChild));
|
|
156
|
+
return bodies.reduce((count, body) => count + urls(body ?? ""), 0);
|
|
157
|
+
}
|