@cloudhal/astro-renderer 0.0.0-stage → 0.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/LICENSE +202 -0
- package/README.md +36 -2
- package/dist/src/ambient-client.d.ts +17 -0
- package/dist/src/ambient-client.js +55 -0
- package/dist/src/analytics-client.d.ts +17 -0
- package/dist/src/analytics-client.js +96 -0
- package/dist/src/analytics-middleware.d.ts +6 -0
- package/dist/src/analytics-middleware.js +43 -0
- package/dist/src/analytics-node.d.ts +46 -0
- package/dist/src/analytics-node.js +134 -0
- package/dist/src/analytics.d.ts +117 -0
- package/dist/src/analytics.js +372 -0
- package/dist/src/archive.d.ts +106 -0
- package/dist/src/archive.js +207 -0
- package/dist/src/astro-env.d.ts +21 -0
- package/dist/src/block-html.d.ts +14 -0
- package/dist/src/block-html.js +103 -0
- package/dist/src/blocks.d.ts +24 -0
- package/dist/src/blocks.js +66 -0
- package/dist/src/cache-tags.d.ts +25 -0
- package/dist/src/cache-tags.js +59 -0
- package/dist/src/carousel-client.d.ts +28 -0
- package/dist/src/carousel-client.js +67 -0
- package/dist/src/chart.d.ts +34 -0
- package/dist/src/chart.js +182 -0
- package/dist/src/chrome-client.d.ts +27 -0
- package/dist/src/chrome-client.js +64 -0
- package/dist/src/cloudflare-entry.d.ts +87 -0
- package/dist/src/cloudflare-entry.js +548 -0
- package/dist/src/comments-client.d.ts +11 -0
- package/dist/src/comments-client.js +58 -0
- package/dist/src/comments-html.d.ts +42 -0
- package/dist/src/comments-html.js +303 -0
- package/dist/src/comments-middleware.d.ts +6 -0
- package/dist/src/comments-middleware.js +38 -0
- package/dist/src/comments-node.d.ts +42 -0
- package/dist/src/comments-node.js +112 -0
- package/dist/src/comments-view.d.ts +68 -0
- package/dist/src/comments-view.js +174 -0
- package/dist/src/comments.d.ts +66 -0
- package/dist/src/comments.js +258 -0
- package/dist/src/components/Block.astro +39 -0
- package/dist/src/components/Body.astro +43 -0
- package/dist/src/components/Comments.astro +19 -0
- package/dist/src/components/ContentList.astro +31 -0
- package/dist/src/components/EntryArticle.astro +31 -0
- package/dist/src/components/Hero.astro +36 -0
- package/dist/src/components/Image.astro +36 -0
- package/dist/src/components/RichText.astro +19 -0
- package/dist/src/components/Sections.astro +66 -0
- package/dist/src/components/SeoHead.astro +22 -0
- package/dist/src/components/Surface.astro +36 -0
- package/dist/src/components/SurfaceNode.astro +82 -0
- package/dist/src/components/index.d.ts +12 -0
- package/dist/src/components/index.js +12 -0
- package/dist/src/csp.d.ts +20 -0
- package/dist/src/csp.js +28 -0
- package/dist/src/embed-client.d.ts +38 -0
- package/dist/src/embed-client.js +98 -0
- package/dist/src/embed-middleware.d.ts +9 -0
- package/dist/src/embed-middleware.js +49 -0
- package/dist/src/feeds.d.ts +72 -0
- package/dist/src/feeds.js +246 -0
- package/dist/src/gates.d.ts +25 -0
- package/dist/src/gates.js +63 -0
- package/dist/src/index.d.ts +17 -0
- package/dist/src/index.js +17 -0
- package/dist/src/integration.d.ts +55 -0
- package/dist/src/integration.js +244 -0
- package/dist/src/kit.d.ts +48 -0
- package/dist/src/kit.js +83 -0
- package/dist/src/locale-feeds.d.ts +29 -0
- package/dist/src/locale-feeds.js +152 -0
- package/dist/src/locale-middleware.d.ts +5 -0
- package/dist/src/locale-middleware.js +26 -0
- package/dist/src/locales.d.ts +87 -0
- package/dist/src/locales.js +336 -0
- package/dist/src/markdown.d.ts +83 -0
- package/dist/src/markdown.js +533 -0
- package/dist/src/media-middleware.d.ts +5 -0
- package/dist/src/media-middleware.js +18 -0
- package/dist/src/media-route.d.ts +47 -0
- package/dist/src/media-route.js +238 -0
- package/dist/src/media.d.ts +109 -0
- package/dist/src/media.js +298 -0
- package/dist/src/motion-client.d.ts +39 -0
- package/dist/src/motion-client.js +115 -0
- package/dist/src/navigation.d.ts +127 -0
- package/dist/src/navigation.js +382 -0
- package/dist/src/not-found.d.ts +16 -0
- package/dist/src/not-found.js +41 -0
- package/dist/src/overlay-client.d.ts +20 -0
- package/dist/src/overlay-client.js +49 -0
- package/dist/src/prefs-client.d.ts +14 -0
- package/dist/src/prefs-client.js +18 -0
- package/dist/src/preview.d.ts +70 -0
- package/dist/src/preview.js +551 -0
- package/dist/src/resolver.d.ts +71 -0
- package/dist/src/resolver.js +206 -0
- package/dist/src/routing.d.ts +108 -0
- package/dist/src/routing.js +285 -0
- package/dist/src/runtime-workerd.d.ts +87 -0
- package/dist/src/runtime-workerd.js +275 -0
- package/dist/src/runtime.d.ts +59 -0
- package/dist/src/runtime.js +153 -0
- package/dist/src/safe-urls.d.ts +13 -0
- package/dist/src/safe-urls.js +35 -0
- package/dist/src/sanitize.d.ts +7 -0
- package/dist/src/sanitize.js +217 -0
- package/dist/src/search-client.d.ts +29 -0
- package/dist/src/search-client.js +92 -0
- package/dist/src/search-limit.d.ts +36 -0
- package/dist/src/search-limit.js +66 -0
- package/dist/src/search.d.ts +64 -0
- package/dist/src/search.js +155 -0
- package/dist/src/seo.d.ts +85 -0
- package/dist/src/seo.js +468 -0
- package/dist/src/site-dispatch.d.ts +37 -0
- package/dist/src/site-dispatch.js +86 -0
- package/dist/src/site-hosts.d.ts +35 -0
- package/dist/src/site-hosts.js +110 -0
- package/dist/src/source.d.ts +227 -0
- package/dist/src/source.js +794 -0
- package/dist/src/static-gates.d.ts +3 -0
- package/dist/src/static-gates.js +28 -0
- package/dist/src/static-headers.d.ts +30 -0
- package/dist/src/static-headers.js +140 -0
- package/dist/src/static-media.d.ts +4 -0
- package/dist/src/static-media.js +65 -0
- package/dist/src/static-redirects.d.ts +4 -0
- package/dist/src/static-redirects.js +40 -0
- package/dist/src/surface-client.d.ts +34 -0
- package/dist/src/surface-client.js +146 -0
- package/dist/src/surfaces/html.d.ts +35 -0
- package/dist/src/surfaces/html.js +184 -0
- package/dist/src/surfaces/markup.d.ts +57 -0
- package/dist/src/surfaces/markup.js +135 -0
- package/dist/src/surfaces/motion.d.ts +33 -0
- package/dist/src/surfaces/motion.js +132 -0
- package/dist/src/surfaces/primitives/carousel.d.ts +2 -0
- package/dist/src/surfaces/primitives/carousel.js +95 -0
- package/dist/src/surfaces/primitives/content.d.ts +13 -0
- package/dist/src/surfaces/primitives/content.js +159 -0
- package/dist/src/surfaces/primitives/data.d.ts +5 -0
- package/dist/src/surfaces/primitives/data.js +80 -0
- package/dist/src/surfaces/primitives/index.d.ts +54 -0
- package/dist/src/surfaces/primitives/index.js +106 -0
- package/dist/src/surfaces/primitives/inputs.d.ts +8 -0
- package/dist/src/surfaces/primitives/inputs.js +68 -0
- package/dist/src/surfaces/primitives/layout.d.ts +14 -0
- package/dist/src/surfaces/primitives/layout.js +61 -0
- package/dist/src/surfaces/primitives/motion.d.ts +9 -0
- package/dist/src/surfaces/primitives/motion.js +45 -0
- package/dist/src/surfaces/primitives/overlays.d.ts +6 -0
- package/dist/src/surfaces/primitives/overlays.js +79 -0
- package/dist/src/surfaces/tree.d.ts +107 -0
- package/dist/src/surfaces/tree.js +321 -0
- package/dist/src/surfaces.d.ts +14 -0
- package/dist/src/surfaces.js +33 -0
- package/dist/src/taxonomy.d.ts +86 -0
- package/dist/src/taxonomy.js +268 -0
- package/dist/src/theme-settings.d.ts +24 -0
- package/dist/src/theme-settings.js +111 -0
- package/package.json +219 -3
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The media route (RFC-0004 §7.1–§7.3): `GET`/`HEAD /media/<sha16>/<slug>.<ext>[.sha256]`, answered
|
|
3
|
+
* before page routing by the site Worker (and by the dev middleware). Pure over its dependencies, so
|
|
4
|
+
* Workers, Node and tests share it. Worker-safe.
|
|
5
|
+
*
|
|
6
|
+
* 1. The asset is found by its URL prefix in the source being served: the published snapshot, or
|
|
7
|
+
* the preview target behind a valid token (invariant 15). Unknown → 404.
|
|
8
|
+
* 2. Taken-down content answers 410 (451 when legal) from every snapshot (invariant 11).
|
|
9
|
+
* 3. A slug or extension that is not the asset's current one → 301 to the canonical URL.
|
|
10
|
+
* 4. External files → 302 to their https URL.
|
|
11
|
+
* 5. Blob content streams with Range (206), conditional requests (`If-None-Match` on the SHA-256),
|
|
12
|
+
* `nosniff`, a type-specific CSP, `Repr-Digest`, and immutable caching when published.
|
|
13
|
+
* 6. `.sha256` → `<sha256> <slug>.<ext>` (GNU `sha256sum -c` format).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
MEDIA_ROUTE,
|
|
18
|
+
MEDIA_TYPES,
|
|
19
|
+
mediaExtension,
|
|
20
|
+
mediaPath,
|
|
21
|
+
servedContentType,
|
|
22
|
+
} from "@cloudhal/core-ts/media";
|
|
23
|
+
import { resolveBlobRange } from "@cloudhal/storage";
|
|
24
|
+
import { assetCacheTag, mediaCacheTag } from "./cache-tags.js";
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
export function matchMediaRoute(pathname ) {
|
|
49
|
+
const m = MEDIA_ROUTE.exec(pathname);
|
|
50
|
+
if (!m) return undefined;
|
|
51
|
+
return {
|
|
52
|
+
prefix: m[1] ,
|
|
53
|
+
slug: m[2] ,
|
|
54
|
+
ext: m[3] ,
|
|
55
|
+
checksum: m[4] !== undefined,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const SVG_CSP = "default-src 'none'; img-src data:; style-src 'unsafe-inline'; sandbox";
|
|
60
|
+
const DEFAULT_CSP = "default-src 'none'";
|
|
61
|
+
|
|
62
|
+
function hexToBase64(hex ) {
|
|
63
|
+
let bin = "";
|
|
64
|
+
for (let i = 0; i < hex.length; i += 2)
|
|
65
|
+
bin += String.fromCharCode(Number.parseInt(hex.slice(i, i + 2), 16));
|
|
66
|
+
return btoa(bin);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** One `bytes=` range (RFC 9110 §14.1.2); several ranges, or garbage, are ignored (full body). */
|
|
70
|
+
export function parseRangeHeader(header ) {
|
|
71
|
+
const m = header ? /^bytes=(\d*)-(\d*)$/.exec(header.trim()) : null;
|
|
72
|
+
if (!m) return undefined;
|
|
73
|
+
const [, a, b] = m;
|
|
74
|
+
if (a === "" && b === "") return undefined;
|
|
75
|
+
if (a === "") return { suffix: Number(b) };
|
|
76
|
+
const offset = Number(a);
|
|
77
|
+
if (b === "") return { offset };
|
|
78
|
+
const end = Number(b);
|
|
79
|
+
if (end < offset) return undefined;
|
|
80
|
+
return { offset, length: end - offset + 1 };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function cacheHeaders(deps , asset ) {
|
|
84
|
+
if (deps.mode !== "published") return { "cache-control": "private, no-store" };
|
|
85
|
+
return {
|
|
86
|
+
"cache-control": "public, max-age=31536000, immutable",
|
|
87
|
+
"cloudflare-cdn-cache-control": "public, max-age=31536000",
|
|
88
|
+
// Not the site tag: publishing pages never evicts media (§7.4).
|
|
89
|
+
"cache-tag": [mediaCacheTag(deps.siteId), ...(asset ? [assetCacheTag(asset.id)] : [])].join(
|
|
90
|
+
",",
|
|
91
|
+
),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function plain(status , text , extra = {}) {
|
|
96
|
+
return new Response(text, {
|
|
97
|
+
status,
|
|
98
|
+
headers: {
|
|
99
|
+
"content-type": "text/plain; charset=utf-8",
|
|
100
|
+
"x-content-type-options": "nosniff",
|
|
101
|
+
"cache-control": "no-store",
|
|
102
|
+
"cloudflare-cdn-cache-control": "no-store",
|
|
103
|
+
...extra,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Answers a media request, or returns `undefined` when the path is not a media URL (it falls
|
|
110
|
+
* through to the site's own routes: `/media` and other paths under it stay usable, invariant 12).
|
|
111
|
+
*/
|
|
112
|
+
export async function serveMediaRequest(
|
|
113
|
+
request ,
|
|
114
|
+
deps ,
|
|
115
|
+
) {
|
|
116
|
+
const url = new URL(request.url);
|
|
117
|
+
const match = matchMediaRoute(url.pathname);
|
|
118
|
+
if (!match) return undefined;
|
|
119
|
+
if (request.method !== "GET" && request.method !== "HEAD")
|
|
120
|
+
return plain(405, "method not allowed", { allow: "GET, HEAD" });
|
|
121
|
+
const asset = await deps.assetByPrefix(match.prefix);
|
|
122
|
+
if (!asset) return plain(404, "not found");
|
|
123
|
+
const head = request.method === "HEAD";
|
|
124
|
+
const canonical = mediaPath(asset);
|
|
125
|
+
const ext = mediaExtension(asset.mediaType);
|
|
126
|
+
if (asset.storage.kind === "blob") {
|
|
127
|
+
const state = await deps.blobState(asset.sha256);
|
|
128
|
+
if (state?.state === "purged")
|
|
129
|
+
return state.purge?.legal
|
|
130
|
+
? plain(451, "unavailable for legal reasons")
|
|
131
|
+
: plain(410, "this file was removed");
|
|
132
|
+
}
|
|
133
|
+
if (match.slug !== asset.slug || match.ext !== ext) {
|
|
134
|
+
const query = deps.keepQuery ? `?${deps.keepQuery}` : "";
|
|
135
|
+
return new Response(null, {
|
|
136
|
+
status: 301,
|
|
137
|
+
headers: {
|
|
138
|
+
location: `${canonical}${match.checksum ? ".sha256" : ""}${query}`,
|
|
139
|
+
"cache-control": "no-store",
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
if (asset.storage.kind === "external") {
|
|
144
|
+
return new Response(null, {
|
|
145
|
+
status: 302,
|
|
146
|
+
headers: {
|
|
147
|
+
location: asset.storage.url,
|
|
148
|
+
"cache-control": deps.mode === "published" ? "public, max-age=300" : "private, no-store",
|
|
149
|
+
...(asset.storage.checksum === "declared" ? { "cloudhal-checksum": "declared" } : {}),
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
const etag = `"${asset.sha256}"`;
|
|
154
|
+
const common = {
|
|
155
|
+
etag,
|
|
156
|
+
"x-content-type-options": "nosniff",
|
|
157
|
+
...cacheHeaders(deps, asset),
|
|
158
|
+
};
|
|
159
|
+
if (match.checksum) {
|
|
160
|
+
const body = `${asset.sha256} ${asset.slug}.${ext}\n`;
|
|
161
|
+
return new Response(head ? null : body, {
|
|
162
|
+
status: 200,
|
|
163
|
+
headers: {
|
|
164
|
+
...common,
|
|
165
|
+
etag: `"${asset.sha256}.sha256"`,
|
|
166
|
+
"content-type": "text/plain; charset=utf-8",
|
|
167
|
+
"content-length": String(new TextEncoder().encode(body).byteLength),
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
const info = MEDIA_TYPES[asset.mediaType];
|
|
172
|
+
const headers = {
|
|
173
|
+
...common,
|
|
174
|
+
"content-type": servedContentType(asset.mediaType),
|
|
175
|
+
"content-disposition":
|
|
176
|
+
info?.disposition === "inline" ? "inline" : `attachment; filename="${asset.slug}.${ext}"`,
|
|
177
|
+
"accept-ranges": "bytes",
|
|
178
|
+
"repr-digest": `sha-256=:${hexToBase64(asset.sha256)}:`,
|
|
179
|
+
};
|
|
180
|
+
// Built-in PDF viewers refuse to render under `sandbox` (answer 4); everything else is inert.
|
|
181
|
+
if (asset.mediaType === "image/svg+xml") headers["content-security-policy"] = SVG_CSP;
|
|
182
|
+
else if (asset.mediaType !== "application/pdf") headers["content-security-policy"] = DEFAULT_CSP;
|
|
183
|
+
const inm = request.headers.get("if-none-match");
|
|
184
|
+
if (inm?.split(",").some((t) => t.trim().replace(/^W\//, "") === etag))
|
|
185
|
+
return new Response(null, { status: 304, headers: common });
|
|
186
|
+
const key = `media/${deps.siteId}/${asset.sha256}`;
|
|
187
|
+
const rangeHeader = request.headers.get("range");
|
|
188
|
+
const ifRange = request.headers.get("if-range");
|
|
189
|
+
const range = !ifRange || ifRange === etag ? parseRangeHeader(rangeHeader) : undefined;
|
|
190
|
+
if (range) {
|
|
191
|
+
const resolved = resolveBlobRange(range, asset.bytes);
|
|
192
|
+
if (!resolved)
|
|
193
|
+
return new Response(null, {
|
|
194
|
+
status: 416,
|
|
195
|
+
headers: { "content-range": `bytes */${asset.bytes}`, "cache-control": "no-store" },
|
|
196
|
+
});
|
|
197
|
+
const stream = deps.blobs.getStream
|
|
198
|
+
? await deps.blobs.getStream(key, { range: resolved })
|
|
199
|
+
: undefined;
|
|
200
|
+
let body ;
|
|
201
|
+
if (stream) body = stream.body;
|
|
202
|
+
else {
|
|
203
|
+
const all = await deps.blobs.get(key);
|
|
204
|
+
if (!all) return plain(404, "not found");
|
|
205
|
+
body = all.slice(resolved.offset, resolved.offset + resolved.length);
|
|
206
|
+
}
|
|
207
|
+
if (head && stream) await stream.body.cancel();
|
|
208
|
+
return new Response(head ? null : body, {
|
|
209
|
+
status: 206,
|
|
210
|
+
headers: {
|
|
211
|
+
...headers,
|
|
212
|
+
"content-range": `bytes ${resolved.offset}-${resolved.offset + resolved.length - 1}/${asset.bytes}`,
|
|
213
|
+
"content-length": String(resolved.length),
|
|
214
|
+
// Partial bodies never enter the edge cache under the whole file's URL.
|
|
215
|
+
...(deps.mode === "published" ? { "cloudflare-cdn-cache-control": "no-store" } : {}),
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
if (head) {
|
|
220
|
+
if (!(await deps.blobs.head(key))) return plain(404, "not found");
|
|
221
|
+
return new Response(null, {
|
|
222
|
+
status: 200,
|
|
223
|
+
headers: { ...headers, "content-length": String(asset.bytes) },
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
const stream = deps.blobs.getStream ? await deps.blobs.getStream(key) : undefined;
|
|
227
|
+
let body ;
|
|
228
|
+
if (stream) body = stream.body;
|
|
229
|
+
else {
|
|
230
|
+
const all = await deps.blobs.get(key);
|
|
231
|
+
if (!all) return plain(404, "not found");
|
|
232
|
+
body = all;
|
|
233
|
+
}
|
|
234
|
+
return new Response(body, {
|
|
235
|
+
status: 200,
|
|
236
|
+
headers: { ...headers, "content-length": String(asset.bytes) },
|
|
237
|
+
});
|
|
238
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Showing assets (RFC-0004 §7.5–§7.7): the one place that knows about image transformations.
|
|
3
|
+
* Starters and section components call `imageAttributes`; markdown, SEO and feeds use the helpers
|
|
4
|
+
* here. Pure functions; Worker-safe.
|
|
5
|
+
*
|
|
6
|
+
* `transform: "cdn-cgi"` emits Cloudflare URL transformations from the same zone (a fixed width
|
|
7
|
+
* ladder, never wider than the original); it is opt-in (`media.transform` in `cloudhal.json`, answer
|
|
8
|
+
* 13) because it needs Transformations enabled on a real zone. `"none"` (the default; offline,
|
|
9
|
+
* static builds, previews, workers.dev) is a passthrough: the original URL, no `srcset`.
|
|
10
|
+
*/
|
|
11
|
+
import type { Asset, ImageRef } from "@cloudhal/core-contracts";
|
|
12
|
+
import type { BlockRegistry } from "@cloudhal/core-ts/blocks";
|
|
13
|
+
import type { IconSet } from "@cloudhal/core-ts/icons";
|
|
14
|
+
export type ImageTransform = "cdn-cgi" | "none";
|
|
15
|
+
/** Fixed widths: bounded cache fragmentation and a bounded number of unique transformations. */
|
|
16
|
+
export declare const WIDTH_LADDER: readonly number[];
|
|
17
|
+
/** Assets by id (what a page needs to resolve `assetId` and `asset:<id>`). */
|
|
18
|
+
export type AssetIndex = ReadonlyMap<string, Asset>;
|
|
19
|
+
export interface MediaContext {
|
|
20
|
+
readonly assets?: AssetIndex | undefined;
|
|
21
|
+
readonly locale?: string | undefined;
|
|
22
|
+
readonly defaultLocale?: string | undefined;
|
|
23
|
+
readonly transform?: ImageTransform | undefined;
|
|
24
|
+
/** Preview responses never use transformations (§7.3). */
|
|
25
|
+
readonly preview?: boolean | undefined;
|
|
26
|
+
/** Called once per unresolved `asset:` reference (logged by the caller). */
|
|
27
|
+
readonly onMiss?: ((id: string) => void) | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* The deployment's block registry (RFC-0015 §6), for previews: a theme's type is then told
|
|
30
|
+
* apart from an unknown one. The site renders theme types with the theme's components.
|
|
31
|
+
*/
|
|
32
|
+
readonly blocks?: BlockRegistry | undefined;
|
|
33
|
+
/** RFC-0026: the site's icon weight (26c) and the theme's own drawings (26d). */
|
|
34
|
+
readonly icons?: {
|
|
35
|
+
readonly weight?: string | undefined;
|
|
36
|
+
readonly set?: IconSet | undefined;
|
|
37
|
+
} | undefined;
|
|
38
|
+
/** RFC-0028 §4: the page's placeholder allowance, shared by every image it draws. */
|
|
39
|
+
readonly budget?: PlaceholderBudget | undefined;
|
|
40
|
+
}
|
|
41
|
+
export interface ImageAttributeOptions {
|
|
42
|
+
locale: string;
|
|
43
|
+
defaultLocale: string;
|
|
44
|
+
transform: ImageTransform;
|
|
45
|
+
sizes: string;
|
|
46
|
+
widths?: readonly number[];
|
|
47
|
+
quality?: number;
|
|
48
|
+
priority?: boolean;
|
|
49
|
+
preview?: boolean;
|
|
50
|
+
/** RFC-0028 §4: the page's placeholder bytes left; spent as LQIPs are drawn (colour only after). */
|
|
51
|
+
budget?: PlaceholderBudget;
|
|
52
|
+
/** RFC-0028 §3: how the image meets its frame (the theme sets `object-fit` to match). */
|
|
53
|
+
fit?: ImageFit;
|
|
54
|
+
}
|
|
55
|
+
/** RFC-0028 §3. `cover` is the default. */
|
|
56
|
+
export type ImageFit = "cover" | "contain" | "blur-fill" | "color-fill" | "natural";
|
|
57
|
+
export declare const IMAGE_FITS: readonly ImageFit[];
|
|
58
|
+
/** A page's placeholder allowance (RFC-0028 §4), shared by every image the page draws. */
|
|
59
|
+
export interface PlaceholderBudget {
|
|
60
|
+
left: number;
|
|
61
|
+
}
|
|
62
|
+
/** About 24 KB of LQIPs per page; beyond it images get their colour only. */
|
|
63
|
+
export declare const PLACEHOLDER_BUDGET: number;
|
|
64
|
+
export declare const placeholderBudget: () => PlaceholderBudget;
|
|
65
|
+
/**
|
|
66
|
+
* The placeholder painted behind an image while it loads (RFC-0028 §2): the dominant colour, and
|
|
67
|
+
* the LQIP blurred by an SVG filter (no script, nothing hidden). By fit (§3):
|
|
68
|
+
* - `cover` (default) and `blur-fill`: the blurred LQIP covers the box; under `object-fit:
|
|
69
|
+
* contain` (blur-fill) it stays visible in the bars. A cover crop leans towards the focal point.
|
|
70
|
+
* - `contain`: the blurred LQIP only where the picture will be; the bars stay the page's.
|
|
71
|
+
* - `color-fill`: the colour fills the box, the blurred LQIP where the picture will be.
|
|
72
|
+
* A transparent image gets none (its pixels would show the placeholder after it loads), except
|
|
73
|
+
* `color-fill`'s colour, which is the point of it.
|
|
74
|
+
*/
|
|
75
|
+
export declare function placeholderStyle(asset: Asset, budget?: PlaceholderBudget, fit?: ImageFit): string | undefined;
|
|
76
|
+
export interface ImageAttributes {
|
|
77
|
+
src: string;
|
|
78
|
+
srcset?: string;
|
|
79
|
+
sizes?: string;
|
|
80
|
+
width: number;
|
|
81
|
+
height: number;
|
|
82
|
+
alt: string;
|
|
83
|
+
loading: "lazy" | "eager";
|
|
84
|
+
decoding: "async";
|
|
85
|
+
fetchpriority?: "high";
|
|
86
|
+
style?: string;
|
|
87
|
+
}
|
|
88
|
+
/** Alt text: this use's alt when given, "" for decorative images, else the asset's (§7.6). */
|
|
89
|
+
export declare function assetAlt(asset: Asset, refAlt: string | undefined, locale: string, defaultLocale: string): string;
|
|
90
|
+
/** The asset's caption followed by its credit (§7.7), or undefined. */
|
|
91
|
+
export declare function assetCaption(asset: Asset, locale: string, defaultLocale: string, own?: string): string | undefined;
|
|
92
|
+
/** A transformation URL of a blob image (same zone, `/cdn-cgi/image/…`). */
|
|
93
|
+
export declare function transformedUrl(asset: Asset, width: number, quality?: number): string;
|
|
94
|
+
/** `<img>` attributes for an asset (RFC-0004 §7.5): size always, `srcset` when transforming. */
|
|
95
|
+
export declare function imageAttributes(asset: Asset, ref: Pick<ImageRef, "alt" | "width" | "height"> | undefined, o: ImageAttributeOptions): ImageAttributes;
|
|
96
|
+
/** `<img …>` HTML for attributes (every value escaped). */
|
|
97
|
+
export declare function imgHtml(a: ImageAttributes, extra?: string): string;
|
|
98
|
+
/** The URL social cards use: absolute, the 1200-wide variant when transforming (§7.7). */
|
|
99
|
+
export declare function socialImageUrl(origin: string, asset: Asset, transform?: ImageTransform): string;
|
|
100
|
+
/** schema.org ImageObject for JSON-LD (§7.7); SPDX ids link to spdx.org. */
|
|
101
|
+
export declare function imageObject(origin: string, asset: Asset, locale: string, defaultLocale: string): Record<string, unknown>;
|
|
102
|
+
/** "CSV · 1.2 MB" and the checksum link for downloads (§7.6). */
|
|
103
|
+
export declare function downloadMeta(asset: Asset): string;
|
|
104
|
+
/** Whether a download should carry `download` (attachments: data and other). */
|
|
105
|
+
export declare function isAttachment(asset: Asset): boolean;
|
|
106
|
+
/** Every asset of a source, by id (`source.assets()`; empty for sources without media). */
|
|
107
|
+
export declare function loadAssetIndex(source: {
|
|
108
|
+
assets?: () => Promise<Asset[]>;
|
|
109
|
+
}): Promise<Map<string, Asset>>;
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Showing assets (RFC-0004 §7.5–§7.7): the one place that knows about image transformations.
|
|
3
|
+
* Starters and section components call `imageAttributes`; markdown, SEO and feeds use the helpers
|
|
4
|
+
* here. Pure functions; Worker-safe.
|
|
5
|
+
*
|
|
6
|
+
* `transform: "cdn-cgi"` emits Cloudflare URL transformations from the same zone (a fixed width
|
|
7
|
+
* ladder, never wider than the original); it is opt-in (`media.transform` in `cloudhal.json`, answer
|
|
8
|
+
* 13) because it needs Transformations enabled on a real zone. `"none"` (the default; offline,
|
|
9
|
+
* static builds, previews, workers.dev) is a passthrough: the original URL, no `srcset`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
import { assetUrl, formatBytes, MEDIA_TYPES, mediaPath } from "@cloudhal/core-ts/media";
|
|
16
|
+
|
|
17
|
+
const escapeHtml = (s ) =>
|
|
18
|
+
s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/** Fixed widths: bounded cache fragmentation and a bounded number of unique transformations. */
|
|
23
|
+
export const WIDTH_LADDER = [320, 640, 960, 1280, 1600, 1920, 2560];
|
|
24
|
+
|
|
25
|
+
/** Assets by id (what a page needs to resolve `assetId` and `asset:<id>`). */
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
/** RFC-0028 §3. `cover` is the default. */
|
|
66
|
+
|
|
67
|
+
export const IMAGE_FITS = [
|
|
68
|
+
"cover",
|
|
69
|
+
"contain",
|
|
70
|
+
"blur-fill",
|
|
71
|
+
"color-fill",
|
|
72
|
+
"natural",
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
/** A page's placeholder allowance (RFC-0028 §4), shared by every image the page draws. */
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
/** About 24 KB of LQIPs per page; beyond it images get their colour only. */
|
|
80
|
+
export const PLACEHOLDER_BUDGET = 24 * 1024;
|
|
81
|
+
export const placeholderBudget = () => ({ left: PLACEHOLDER_BUDGET });
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The placeholder painted behind an image while it loads (RFC-0028 §2): the dominant colour, and
|
|
85
|
+
* the LQIP blurred by an SVG filter (no script, nothing hidden). By fit (§3):
|
|
86
|
+
* - `cover` (default) and `blur-fill`: the blurred LQIP covers the box; under `object-fit:
|
|
87
|
+
* contain` (blur-fill) it stays visible in the bars. A cover crop leans towards the focal point.
|
|
88
|
+
* - `contain`: the blurred LQIP only where the picture will be; the bars stay the page's.
|
|
89
|
+
* - `color-fill`: the colour fills the box, the blurred LQIP where the picture will be.
|
|
90
|
+
* A transparent image gets none (its pixels would show the placeholder after it loads), except
|
|
91
|
+
* `color-fill`'s colour, which is the point of it.
|
|
92
|
+
*/
|
|
93
|
+
export function placeholderStyle(
|
|
94
|
+
asset ,
|
|
95
|
+
budget ,
|
|
96
|
+
fit = "cover",
|
|
97
|
+
) {
|
|
98
|
+
const img = asset.image;
|
|
99
|
+
if (!img) return undefined;
|
|
100
|
+
if (img.alpha)
|
|
101
|
+
return fit === "color-fill" && img.color ? `background-color:${img.color}` : undefined;
|
|
102
|
+
const color = img.color && fit !== "contain" ? `background-color:${img.color}` : "";
|
|
103
|
+
let lqip = img.lqip;
|
|
104
|
+
if (lqip && budget) {
|
|
105
|
+
if (budget.left < lqip.length) lqip = undefined;
|
|
106
|
+
else budget.left -= lqip.length;
|
|
107
|
+
}
|
|
108
|
+
if (!lqip) return color || undefined;
|
|
109
|
+
// The LQIP's own pixel grid (≤ 24 px a side): a blur of ~1 of its pixels scales with the box.
|
|
110
|
+
const long = Math.max(img.width, img.height);
|
|
111
|
+
const w = Math.max(1, Math.round((24 * img.width) / long));
|
|
112
|
+
const h = Math.max(1, Math.round((24 * img.height) / long));
|
|
113
|
+
const third = (v ) =>
|
|
114
|
+
v === undefined || (v > 1 / 3 && v < 2 / 3) ? "Mid" : v <= 1 / 3 ? "Min" : "Max";
|
|
115
|
+
const align =
|
|
116
|
+
fit === "contain" || fit === "color-fill"
|
|
117
|
+
? "xMidYMid meet"
|
|
118
|
+
: fit === "cover"
|
|
119
|
+
? `x${third(img.focal?.x)}Y${third(img.focal?.y)} slice`
|
|
120
|
+
: "xMidYMid slice";
|
|
121
|
+
const svg = `<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 ${w} ${h}' preserveAspectRatio='${align}'><filter id='b' x='0' y='0' width='100%' height='100%' color-interpolation-filters='sRGB'><feGaussianBlur stdDeviation='1' edgeMode='duplicate'/><feComponentTransfer><feFuncA type='discrete' tableValues='1 1'/></feComponentTransfer></filter><image width='100%' height='100%' preserveAspectRatio='none' filter='url(#b)' href='${lqip}'/></svg>`;
|
|
122
|
+
const url = `data:image/svg+xml,${svg.replaceAll("#", "%23").replaceAll("<", "%3C").replaceAll(">", "%3E")}`;
|
|
123
|
+
return `${color ? `${color};` : ""}background-image:url("${url}");background-size:100% 100%;background-repeat:no-repeat`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
const TRANSFORMABLE = ["image/jpeg", "image/png", "image/webp", "image/avif", "image/gif"];
|
|
140
|
+
|
|
141
|
+
function localized(
|
|
142
|
+
map ,
|
|
143
|
+
locale ,
|
|
144
|
+
fallback ,
|
|
145
|
+
) {
|
|
146
|
+
return map?.[locale] ?? map?.[fallback];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Alt text: this use's alt when given, "" for decorative images, else the asset's (§7.6). */
|
|
150
|
+
export function assetAlt(
|
|
151
|
+
asset ,
|
|
152
|
+
refAlt ,
|
|
153
|
+
locale ,
|
|
154
|
+
defaultLocale ,
|
|
155
|
+
) {
|
|
156
|
+
if (refAlt?.trim()) return refAlt;
|
|
157
|
+
if (asset.decorative) return "";
|
|
158
|
+
return localized(asset.alt, locale, defaultLocale) ?? "";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The asset's caption followed by its credit (§7.7), or undefined. */
|
|
162
|
+
export function assetCaption(
|
|
163
|
+
asset ,
|
|
164
|
+
locale ,
|
|
165
|
+
defaultLocale ,
|
|
166
|
+
own ,
|
|
167
|
+
) {
|
|
168
|
+
const caption = own?.trim() || localized(asset.caption, locale, defaultLocale);
|
|
169
|
+
const parts = [caption, asset.credit].filter(Boolean);
|
|
170
|
+
return parts.length ? parts.join(" · ") : undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A transformation URL of a blob image (same zone, `/cdn-cgi/image/…`). */
|
|
174
|
+
export function transformedUrl(asset , width , quality = 80) {
|
|
175
|
+
return `/cdn-cgi/image/width=${width},fit=scale-down,format=auto,quality=${quality}${mediaPath(asset)}`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function canTransform(asset , o ) {
|
|
179
|
+
return (
|
|
180
|
+
o.transform === "cdn-cgi" &&
|
|
181
|
+
!o.preview &&
|
|
182
|
+
asset.storage.kind === "blob" &&
|
|
183
|
+
TRANSFORMABLE.includes(asset.mediaType) &&
|
|
184
|
+
!asset.image?.animated &&
|
|
185
|
+
(asset.image?.width ?? 0) >= 320
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** `<img>` attributes for an asset (RFC-0004 §7.5): size always, `srcset` when transforming. */
|
|
190
|
+
export function imageAttributes(
|
|
191
|
+
asset ,
|
|
192
|
+
ref ,
|
|
193
|
+
o ,
|
|
194
|
+
) {
|
|
195
|
+
const width = asset.image?.width ?? ref?.width ?? 0;
|
|
196
|
+
const height = asset.image?.height ?? ref?.height ?? 0;
|
|
197
|
+
const base = {
|
|
198
|
+
src: assetUrl(asset),
|
|
199
|
+
width,
|
|
200
|
+
height,
|
|
201
|
+
alt: assetAlt(asset, ref?.alt, o.locale, o.defaultLocale),
|
|
202
|
+
loading: o.priority ? "eager" : "lazy",
|
|
203
|
+
decoding: "async",
|
|
204
|
+
...(o.priority ? { fetchpriority: "high" } : {}),
|
|
205
|
+
};
|
|
206
|
+
const fit = o.fit ?? "cover";
|
|
207
|
+
// A cover crop keeps the subject in view (RFC-0028 §3).
|
|
208
|
+
const focal =
|
|
209
|
+
fit === "cover" && asset.image?.focal
|
|
210
|
+
? `object-position:${pct(asset.image.focal.x)} ${pct(asset.image.focal.y)}`
|
|
211
|
+
: "";
|
|
212
|
+
const style = [placeholderStyle(asset, o.budget, fit), focal].filter(Boolean).join(";");
|
|
213
|
+
if (style) base.style = style;
|
|
214
|
+
if (!canTransform(asset, o)) return base;
|
|
215
|
+
const quality = o.quality ?? 80;
|
|
216
|
+
const ladder = (o.widths ?? WIDTH_LADDER).filter((w) => w <= width);
|
|
217
|
+
const candidates = ladder.map((w) => `${transformedUrl(asset, w, quality)} ${w}w`);
|
|
218
|
+
// The original closes the set when it is wider than the ladder's last step.
|
|
219
|
+
if ((ladder.at(-1) ?? 0) < width) candidates.push(`${mediaPath(asset)} ${width}w`);
|
|
220
|
+
return {
|
|
221
|
+
...base,
|
|
222
|
+
src: width > 1280 ? transformedUrl(asset, 1280, quality) : mediaPath(asset),
|
|
223
|
+
srcset: candidates.join(", "),
|
|
224
|
+
sizes: o.sizes,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const pct = (v ) => `${Math.round(v * 1000) / 10}%`;
|
|
229
|
+
|
|
230
|
+
/** `<img …>` HTML for attributes (every value escaped). */
|
|
231
|
+
export function imgHtml(a , extra = "") {
|
|
232
|
+
const attr = (k , v ) =>
|
|
233
|
+
v === undefined || v === "" || v === 0 ? "" : ` ${k}="${escapeHtml(String(v))}"`;
|
|
234
|
+
return `<img src="${escapeHtml(a.src)}"${attr("srcset", a.srcset)}${attr("sizes", a.sizes)} alt="${escapeHtml(a.alt)}"${attr("width", a.width)}${attr("height", a.height)} loading="${a.loading}" decoding="${a.decoding}"${attr("fetchpriority", a.fetchpriority)}${attr("style", a.style)}${extra}>`;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The URL social cards use: absolute, the 1200-wide variant when transforming (§7.7). */
|
|
238
|
+
export function socialImageUrl(
|
|
239
|
+
origin ,
|
|
240
|
+
asset ,
|
|
241
|
+
transform = "none",
|
|
242
|
+
) {
|
|
243
|
+
if (asset.storage.kind === "external") return asset.storage.url;
|
|
244
|
+
const path =
|
|
245
|
+
canTransform(asset, { transform }) && (asset.image?.width ?? 0) > 1200
|
|
246
|
+
? transformedUrl(asset, 1200)
|
|
247
|
+
: mediaPath(asset);
|
|
248
|
+
return `${origin}${path}`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** schema.org ImageObject for JSON-LD (§7.7); SPDX ids link to spdx.org. */
|
|
252
|
+
export function imageObject(
|
|
253
|
+
origin ,
|
|
254
|
+
asset ,
|
|
255
|
+
locale ,
|
|
256
|
+
defaultLocale ,
|
|
257
|
+
) {
|
|
258
|
+
const caption = localized(asset.caption, locale, defaultLocale);
|
|
259
|
+
const license =
|
|
260
|
+
asset.license?.kind === "spdx"
|
|
261
|
+
? `https://spdx.org/licenses/${asset.license.id.split(" ")[0]}.html`
|
|
262
|
+
: asset.license?.url;
|
|
263
|
+
return {
|
|
264
|
+
"@type": "ImageObject",
|
|
265
|
+
contentUrl:
|
|
266
|
+
asset.storage.kind === "external" ? asset.storage.url : `${origin}${mediaPath(asset)}`,
|
|
267
|
+
...(asset.image ? { width: asset.image.width, height: asset.image.height } : {}),
|
|
268
|
+
...(caption ? { caption } : {}),
|
|
269
|
+
...(asset.credit ? { creditText: asset.credit } : {}),
|
|
270
|
+
...(license ? { license } : {}),
|
|
271
|
+
encodingFormat: asset.mediaType,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** "CSV · 1.2 MB" and the checksum link for downloads (§7.6). */
|
|
276
|
+
export function downloadMeta(asset ) {
|
|
277
|
+
const label = MEDIA_TYPES[asset.mediaType]?.label ?? asset.mediaType;
|
|
278
|
+
const sum =
|
|
279
|
+
asset.storage.kind === "blob"
|
|
280
|
+
? ` · <a href="${escapeHtml(`${mediaPath(asset)}.sha256`)}">SHA-256</a>`
|
|
281
|
+
: asset.storage.checksum === "declared"
|
|
282
|
+
? " · SHA-256 (not verified)"
|
|
283
|
+
: "";
|
|
284
|
+
return `<small class="ch-asset-meta">${escapeHtml(label)} · ${escapeHtml(formatBytes(asset.bytes))}${sum}</small>`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Whether a download should carry `download` (attachments: data and other). */
|
|
288
|
+
export function isAttachment(asset ) {
|
|
289
|
+
return (MEDIA_TYPES[asset.mediaType]?.disposition ?? "attachment") === "attachment";
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Every asset of a source, by id (`source.assets()`; empty for sources without media). */
|
|
293
|
+
export async function loadAssetIndex(source
|
|
294
|
+
|
|
295
|
+
) {
|
|
296
|
+
const list = source.assets ? await source.assets() : [];
|
|
297
|
+
return new Map(list.map((a) => [a.id, a]));
|
|
298
|
+
}
|