@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,794 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
matchRedirect,
|
|
16
|
+
redirectLocale,
|
|
17
|
+
searchDocuments,
|
|
18
|
+
searchRows,
|
|
19
|
+
termName,
|
|
20
|
+
} from "@cloudhal/core-ts";
|
|
21
|
+
import {
|
|
22
|
+
gateInstants,
|
|
23
|
+
hasGates,
|
|
24
|
+
inheritedGate,
|
|
25
|
+
isAvailable,
|
|
26
|
+
nextInstant,
|
|
27
|
+
} from "@cloudhal/core-ts/availability";
|
|
28
|
+
import { matches, parseFilter, resolveFilter } from "@cloudhal/core-ts/filter";
|
|
29
|
+
import { assetRefsOf } from "@cloudhal/core-ts/media";
|
|
30
|
+
import { navigationFor } from "./navigation.js";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* What a renderer needs from a site, independent of whether it reads live nodes (dev) or a
|
|
34
|
+
* published snapshot (build/production).
|
|
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
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
function byNewest(a , b ) {
|
|
127
|
+
return (b.publishedAt ?? b.createdAt).localeCompare(a.publishedAt ?? a.createdAt);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function byPosition(locale ) {
|
|
131
|
+
return (a , b ) =>
|
|
132
|
+
(a.position ?? Number.MAX_SAFE_INTEGER) - (b.position ?? Number.MAX_SAFE_INTEGER) ||
|
|
133
|
+
termName(a, locale).localeCompare(termName(b, locale)) ||
|
|
134
|
+
a.slug.localeCompare(b.slug);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function shown(term , includeDrafts ) {
|
|
138
|
+
return includeDrafts
|
|
139
|
+
? term.status !== "retired"
|
|
140
|
+
: term.status === "active" && term.visibility === "public";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function termFilter(key , slug , descendants ) {
|
|
144
|
+
return `${key}:${slug}${descendants ? ">*" : ""}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Resolves a filter against a vocabulary; unknown terms match nothing and are logged. */
|
|
148
|
+
function compile(filter , taxonomies , terms ) {
|
|
149
|
+
const resolved = resolveFilter(
|
|
150
|
+
parseFilter(filter),
|
|
151
|
+
{ taxonomies, terms },
|
|
152
|
+
{ unknown: "match-nothing" },
|
|
153
|
+
);
|
|
154
|
+
if (resolved.unknown.length > 0)
|
|
155
|
+
console.warn(
|
|
156
|
+
`cloudhal: filter "${filter}" names unknown terms: ${resolved.unknown.join(", ")}`,
|
|
157
|
+
);
|
|
158
|
+
return resolved;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function visible (nodes , includeDrafts ) {
|
|
162
|
+
return includeDrafts
|
|
163
|
+
? nodes.filter((n) => n.status !== "archived")
|
|
164
|
+
: nodes.filter((n) => n.status === "published");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Reads the current nodes on every call. Drafts are included so authors can preview them in dev,
|
|
169
|
+
* and so are gated nodes (a preview shows embargoed and expired content, RFC-0008 §7.3). Without
|
|
170
|
+
* drafts it serves what the public would see: published nodes whose gates are open at `now`.
|
|
171
|
+
*/
|
|
172
|
+
export class LiveSource {
|
|
173
|
+
mode = "live";
|
|
174
|
+
#store ;
|
|
175
|
+
#siteId ;
|
|
176
|
+
#includeDrafts ;
|
|
177
|
+
#now ;
|
|
178
|
+
|
|
179
|
+
constructor(
|
|
180
|
+
store ,
|
|
181
|
+
siteId ,
|
|
182
|
+
options = {},
|
|
183
|
+
) {
|
|
184
|
+
this.#store = store;
|
|
185
|
+
this.#siteId = siteId;
|
|
186
|
+
this.#includeDrafts = options.includeDrafts ?? true;
|
|
187
|
+
this.#now = options.now ?? Date.now;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Drafts and gated nodes, or what the public sees now. */
|
|
191
|
+
#visible (nodes ) {
|
|
192
|
+
if (this.#includeDrafts) return visible(nodes, true);
|
|
193
|
+
const now = this.#now();
|
|
194
|
+
return nodes.filter((n) => isAvailable(n, now));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async nextGate() {
|
|
198
|
+
if (this.#includeDrafts) return undefined;
|
|
199
|
+
const [pages, entries] = await Promise.all([
|
|
200
|
+
this.#store.listPages(this.#siteId),
|
|
201
|
+
this.#store.listContentEntries(this.#siteId),
|
|
202
|
+
]);
|
|
203
|
+
const published = [...pages, ...entries].filter((n) => n.status === "published");
|
|
204
|
+
return nextInstant(gateInstants(published), this.#now());
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async site() {
|
|
208
|
+
const site = await this.#store.getSite(this.#siteId);
|
|
209
|
+
if (!site) throw new Error(`site ${this.#siteId} not found`);
|
|
210
|
+
return site;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async pages() {
|
|
214
|
+
const site = await this.site();
|
|
215
|
+
const pages = this.#visible(await this.#store.listPages(this.#siteId));
|
|
216
|
+
const order = new Map(site.pageIds.map((id, i) => [id, i]));
|
|
217
|
+
return pages.sort((a, b) => (order.get(a.id) ?? 1e9) - (order.get(b.id) ?? 1e9));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async pageByPath(path , locale ) {
|
|
221
|
+
const loc = locale ?? (await this.site()).defaultLocale;
|
|
222
|
+
return (await this.pages()).find((p) => p.path === path && p.locale === loc);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async entries(collection , locale ) {
|
|
226
|
+
const loc = locale ?? (await this.site()).defaultLocale;
|
|
227
|
+
const all = this.#visible(await this.#store.listContentEntries(this.#siteId, collection));
|
|
228
|
+
return all.filter((e) => e.locale === loc).sort(byNewest);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async entryBySlug(
|
|
232
|
+
collection ,
|
|
233
|
+
slug ,
|
|
234
|
+
locale ,
|
|
235
|
+
) {
|
|
236
|
+
return (await this.entries(collection, locale)).find((e) => e.slug === slug);
|
|
237
|
+
}
|
|
238
|
+
async allEntries(locale ) {
|
|
239
|
+
const loc = locale ?? (await this.site()).defaultLocale;
|
|
240
|
+
return this.#visible(await this.#store.listContentEntries(this.#siteId))
|
|
241
|
+
.filter((e) => e.locale === loc)
|
|
242
|
+
.sort(byNewest);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async search(query , options = {}) {
|
|
246
|
+
const site = await this.site();
|
|
247
|
+
const locale = options.locale ?? site.defaultLocale;
|
|
248
|
+
// What this source shows decided visibility and gates already (drafts included).
|
|
249
|
+
const [entries, pages, taxonomies, terms] = await Promise.all([
|
|
250
|
+
this.allEntries(locale),
|
|
251
|
+
this.pages(),
|
|
252
|
+
this.taxonomies(),
|
|
253
|
+
this.vocabulary(),
|
|
254
|
+
]);
|
|
255
|
+
const rows = searchDocuments({
|
|
256
|
+
locales: [locale],
|
|
257
|
+
defaultLocale: site.defaultLocale,
|
|
258
|
+
entries,
|
|
259
|
+
pages: pages.filter((p) => p.locale === locale),
|
|
260
|
+
taxonomies,
|
|
261
|
+
terms,
|
|
262
|
+
drafts: true,
|
|
263
|
+
}).map((r) => ({ ...r, gateFrom: undefined, gateUntil: undefined }));
|
|
264
|
+
return searchRows(rows, query, { ...options, locale, now: this.#now() });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async entryById(id ) {
|
|
268
|
+
const entry = await this.#store.getContentEntry(id);
|
|
269
|
+
return entry && entry.siteId === this.#siteId && this.#visible([entry]).length
|
|
270
|
+
? entry
|
|
271
|
+
: undefined;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
taxonomies() {
|
|
275
|
+
return this.#store.listTaxonomies(this.#siteId);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async terms(key ) {
|
|
279
|
+
const loc = (await this.site()).defaultLocale;
|
|
280
|
+
return (await this.#store.listTerms(this.#siteId, { taxonomy: key }))
|
|
281
|
+
.filter((t) => shown(t, this.#includeDrafts))
|
|
282
|
+
.sort(byPosition(loc));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async termBySlug(key , slug ) {
|
|
286
|
+
return (await this.terms(key)).find((t) => t.slug === slug);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
vocabulary(key ) {
|
|
290
|
+
return this.#store.listTerms(this.#siteId, key === undefined ? {} : { taxonomy: key });
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async entriesByFilter(
|
|
294
|
+
filter ,
|
|
295
|
+
options = {},
|
|
296
|
+
) {
|
|
297
|
+
const loc = options.locale ?? (await this.site()).defaultLocale;
|
|
298
|
+
const [taxonomies, terms, entries] = await Promise.all([
|
|
299
|
+
this.#store.listTaxonomies(this.#siteId),
|
|
300
|
+
this.#store.listTerms(this.#siteId),
|
|
301
|
+
this.#store.listContentEntries(this.#siteId, { locale: loc }),
|
|
302
|
+
]);
|
|
303
|
+
const f = compile(filter, taxonomies, terms);
|
|
304
|
+
const out = this.#visible(entries)
|
|
305
|
+
.filter((e) => matches(f, e))
|
|
306
|
+
.sort(byNewest);
|
|
307
|
+
return options.limit === undefined ? out : out.slice(0, options.limit);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
entriesByTerm(
|
|
311
|
+
key ,
|
|
312
|
+
slug ,
|
|
313
|
+
options = {},
|
|
314
|
+
) {
|
|
315
|
+
return this.entriesByFilter(termFilter(key, slug, options.descendants === true), options);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
redirectFor(path , locale ) {
|
|
319
|
+
return this.#store.redirectFor(this.#siteId, path, locale);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
redirects() {
|
|
323
|
+
return this.#store.listRedirects(this.#siteId);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
menu(key ) {
|
|
327
|
+
return this.#store.getMenuByKey(this.#siteId, key);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async navigation(
|
|
331
|
+
key ,
|
|
332
|
+
locale ,
|
|
333
|
+
options = {},
|
|
334
|
+
) {
|
|
335
|
+
const menu = await this.menu(key);
|
|
336
|
+
if (!menu) return undefined;
|
|
337
|
+
const site = await this.site();
|
|
338
|
+
const [pages, entries, taxonomies, terms] = await Promise.all([
|
|
339
|
+
this.#store.listPages(this.#siteId),
|
|
340
|
+
this.#store.listContentEntries(this.#siteId),
|
|
341
|
+
this.#store.listTaxonomies(this.#siteId),
|
|
342
|
+
this.#store.listTerms(this.#siteId),
|
|
343
|
+
]);
|
|
344
|
+
return navigationFor(
|
|
345
|
+
menu,
|
|
346
|
+
{
|
|
347
|
+
site,
|
|
348
|
+
// drafts: everything but archived (the lookup decides); public: what is available now
|
|
349
|
+
pages: this.#includeDrafts ? pages : this.#visible(pages),
|
|
350
|
+
entries: this.#includeDrafts ? entries : this.#visible(entries),
|
|
351
|
+
taxonomies,
|
|
352
|
+
terms,
|
|
353
|
+
includeDrafts: this.#includeDrafts,
|
|
354
|
+
...(options.collectionIndex ? { collectionIndex: options.collectionIndex } : {}),
|
|
355
|
+
},
|
|
356
|
+
locale ?? site.defaultLocale,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
async assets() {
|
|
361
|
+
const out = [];
|
|
362
|
+
let cursor ;
|
|
363
|
+
do {
|
|
364
|
+
const page = await this.#store.listAssets(this.#siteId, {
|
|
365
|
+
limit: 200,
|
|
366
|
+
...(cursor ? { cursor } : {}),
|
|
367
|
+
});
|
|
368
|
+
out.push(...page.assets);
|
|
369
|
+
cursor = page.nextCursor;
|
|
370
|
+
} while (cursor);
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async asset(id ) {
|
|
375
|
+
const a = await this.#store.getAsset(id);
|
|
376
|
+
return a?.siteId === this.#siteId ? a : undefined;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
assetByPrefix(prefix16 ) {
|
|
380
|
+
return this.#store.getAssetByPrefix(this.#siteId, prefix16);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** Everything a snapshot shows before gates are applied; built once per snapshot and isolate. */
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
/** What a snapshot shows at one instant (or, for previews, regardless of gates). */
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
/** State shared by every view of one snapshot: the loaded nodes and the asset gates. */
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
/** Who may see the nodes of a snapshot: the clock gates are checked against, or nobody (previews). */
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The embargo instant each asset inherits (RFC-0008 §7.2): an asset whose every live referrer —
|
|
432
|
+
* published pages and entries, shown terms — has an embargo is hidden until the earliest one
|
|
433
|
+
* opens. Only assets used by a gated node can inherit anything, so only their referrers are read.
|
|
434
|
+
*/
|
|
435
|
+
function assetGates(loaded ) {
|
|
436
|
+
const out = new Map ();
|
|
437
|
+
const gated = [...loaded.pages, ...loaded.entries].filter(hasGates);
|
|
438
|
+
const kindOf = (n ) => ("collection" in n ? "contentEntry" : "page");
|
|
439
|
+
const candidates = new Set(gated.flatMap((n) => assetRefsOf(kindOf(n), n).map((r) => r.assetId)));
|
|
440
|
+
if (candidates.size === 0) return out;
|
|
441
|
+
const referrers = new Map ();
|
|
442
|
+
const add = (kind , node , gate ) => {
|
|
443
|
+
for (const ref of assetRefsOf(kind, node)) {
|
|
444
|
+
if (!candidates.has(ref.assetId)) continue;
|
|
445
|
+
const list = referrers.get(ref.assetId) ?? [];
|
|
446
|
+
list.push(gate === undefined ? {} : { availability: gate });
|
|
447
|
+
referrers.set(ref.assetId, list);
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
for (const p of loaded.pages) add("page", p, p.availability);
|
|
451
|
+
for (const e of loaded.entries) add("contentEntry", e, e.availability);
|
|
452
|
+
for (const t of loaded.terms) if (shown(t, false)) add("term", t, undefined);
|
|
453
|
+
for (const [id, list] of referrers) {
|
|
454
|
+
const from = inheritedGate(list)?.from;
|
|
455
|
+
if (from !== undefined) out.set(id, Date.parse(from));
|
|
456
|
+
}
|
|
457
|
+
return out;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Reads exactly the node bodies captured in a snapshot (immutable; only published nodes are
|
|
462
|
+
* rendered). Time gates (RFC-0008 §7) apply on top: by default against the wall clock at each
|
|
463
|
+
* call; `at(now)` fixes the instant for one request (the Site Worker does, so a page and the
|
|
464
|
+
* lists on it agree); `ungated()` is the preview view, which shows gated nodes.
|
|
465
|
+
*/
|
|
466
|
+
export class SnapshotSource {
|
|
467
|
+
mode = "snapshot";
|
|
468
|
+
#store ;
|
|
469
|
+
#snapshot ;
|
|
470
|
+
#shared ;
|
|
471
|
+
#clock ;
|
|
472
|
+
#view ;
|
|
473
|
+
|
|
474
|
+
constructor(
|
|
475
|
+
store ,
|
|
476
|
+
snapshot ,
|
|
477
|
+
options = {},
|
|
478
|
+
) {
|
|
479
|
+
this.#store = store;
|
|
480
|
+
this.#snapshot = snapshot;
|
|
481
|
+
this.#shared = options.shared ?? {};
|
|
482
|
+
this.#clock = options.ungated ? null : (options.now ?? Date.now);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
static async open(store , snapshotId ) {
|
|
486
|
+
const snapshot = await store.getSnapshot(snapshotId);
|
|
487
|
+
if (!snapshot) throw new Error(`snapshot ${snapshotId} not found`);
|
|
488
|
+
return new SnapshotSource(store, snapshot);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
get snapshot() {
|
|
492
|
+
return this.#snapshot;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** This snapshot as the public sees it at `now` (one instant for a whole request). */
|
|
496
|
+
at(now ) {
|
|
497
|
+
return new SnapshotSource(this.#store, this.#snapshot, {
|
|
498
|
+
now: () => now,
|
|
499
|
+
shared: this.#shared,
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** This snapshot with gates ignored: previews show embargoed and expired nodes (§7.3). */
|
|
504
|
+
ungated() {
|
|
505
|
+
return new SnapshotSource(this.#store, this.#snapshot, {
|
|
506
|
+
shared: this.#shared,
|
|
507
|
+
ungated: true,
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** True when this view ignores gates (a preview). */
|
|
512
|
+
get gatesIgnored() {
|
|
513
|
+
return this.#clock === null;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* The next instant (ms) at which anything this snapshot shows changes because of a gate, after
|
|
518
|
+
* this view's `now`; undefined when no gate lies ahead (or gates are ignored). The Site Worker
|
|
519
|
+
* caps the edge cache with it (§7.4).
|
|
520
|
+
*/
|
|
521
|
+
async nextGate() {
|
|
522
|
+
if (this.#clock === null) return undefined;
|
|
523
|
+
const { gates } = await this.#load();
|
|
524
|
+
return gates.length === 0 ? undefined : nextInstant(gates, this.#clock());
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** Reads the snapshot's nodes once; kinds this renderer does not know are ignored. */
|
|
528
|
+
#load() {
|
|
529
|
+
this.#shared.loaded ??= (async () => {
|
|
530
|
+
// RFC-0010: one round of shard reads when the snapshot has a bundle; node by node otherwise.
|
|
531
|
+
const bundle = await this.#store.readSnapshotBundle(this.#snapshot).catch(() => undefined);
|
|
532
|
+
this.#shared.bundle = bundle ? "present" : "missing";
|
|
533
|
+
const fetched = bundle ? undefined : new Map ();
|
|
534
|
+
const read = (refs ) =>
|
|
535
|
+
Promise.all(
|
|
536
|
+
refs.map(async (r) => {
|
|
537
|
+
const hit = bundle?.get(r.hash);
|
|
538
|
+
if (hit !== undefined) return hit ;
|
|
539
|
+
const body = await this.#store.readSnapshotNode (r);
|
|
540
|
+
// as stored, before anything downstream could touch the object
|
|
541
|
+
fetched?.set(r.hash, JSON.stringify(body));
|
|
542
|
+
return body;
|
|
543
|
+
}),
|
|
544
|
+
);
|
|
545
|
+
const siteRef = this.#snapshot.nodes.find((n) => n.kind === "site");
|
|
546
|
+
if (!siteRef) throw new Error(`snapshot ${this.#snapshot.id} has no site node`);
|
|
547
|
+
const [site] = await read ([siteRef]);
|
|
548
|
+
const pages = (await read (
|
|
549
|
+
this.#snapshot.nodes.filter((n) => n.kind === "page"),
|
|
550
|
+
)) ;
|
|
551
|
+
const entries = (await read (
|
|
552
|
+
this.#snapshot.nodes.filter((n) => n.kind === "contentEntry"),
|
|
553
|
+
)) ;
|
|
554
|
+
const byKind = async (kind ) =>
|
|
555
|
+
(await read (
|
|
556
|
+
this.#snapshot.nodes.filter((n) => (n.kind ) === kind),
|
|
557
|
+
)) ;
|
|
558
|
+
const [taxonomies, terms, redirects, menus, assets] = await Promise.all([
|
|
559
|
+
byKind ("taxonomy"),
|
|
560
|
+
byKind ("term"),
|
|
561
|
+
byKind ("redirect"),
|
|
562
|
+
byKind ("menu"),
|
|
563
|
+
byKind ("asset"),
|
|
564
|
+
]);
|
|
565
|
+
const assetIndex = new Map ();
|
|
566
|
+
for (const a of assets) {
|
|
567
|
+
assetIndex.set(a.id, a);
|
|
568
|
+
assetIndex.set(a.sha256.slice(0, 16), a);
|
|
569
|
+
}
|
|
570
|
+
const order = new Map((site ).pageIds.map((id, i) => [id, i]));
|
|
571
|
+
const shownPages = visible(pages, false).sort(
|
|
572
|
+
(a, b) => (order.get(a.id) ?? 1e9) - (order.get(b.id) ?? 1e9),
|
|
573
|
+
);
|
|
574
|
+
const shownEntries = visible(entries, false);
|
|
575
|
+
if (fetched) this.#shared.fetched = fetched;
|
|
576
|
+
return {
|
|
577
|
+
site: site ,
|
|
578
|
+
pages: shownPages,
|
|
579
|
+
entries: shownEntries,
|
|
580
|
+
taxonomies,
|
|
581
|
+
terms,
|
|
582
|
+
redirects,
|
|
583
|
+
menus,
|
|
584
|
+
assets,
|
|
585
|
+
assetIndex,
|
|
586
|
+
gates: gateInstants([...shownPages, ...shownEntries]),
|
|
587
|
+
};
|
|
588
|
+
})();
|
|
589
|
+
this.#shared.loaded.catch(() => {
|
|
590
|
+
this.#shared.loaded = undefined;
|
|
591
|
+
});
|
|
592
|
+
return this.#shared.loaded;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* RFC-0010 read-through: when this snapshot loaded without a bundle, write one in the background
|
|
597
|
+
* (`defer` keeps the task alive, e.g. the Worker's `waitUntil`). The resolver calls it for the
|
|
598
|
+
* published snapshot only: previews and history never get a bundle. Never throws; a failed write
|
|
599
|
+
* leaves the snapshot loading node by node, and the next request tries again.
|
|
600
|
+
*/
|
|
601
|
+
persistBundle(defer ) {
|
|
602
|
+
if (this.#shared.bundle === "present" || this.#shared.bundle === "persisting") return;
|
|
603
|
+
const task = this.#load()
|
|
604
|
+
.then(async () => {
|
|
605
|
+
if (this.#shared.bundle !== "missing") return;
|
|
606
|
+
this.#shared.bundle = "persisting";
|
|
607
|
+
await this.#store.writeSnapshotBundle(this.#snapshot, this.#shared.fetched ?? new Map());
|
|
608
|
+
this.#shared.bundle = "present";
|
|
609
|
+
this.#shared.fetched = undefined;
|
|
610
|
+
})
|
|
611
|
+
.catch(() => {
|
|
612
|
+
if (this.#shared.bundle === "persisting") this.#shared.bundle = "missing";
|
|
613
|
+
});
|
|
614
|
+
defer(task);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
#assetGates(loaded ) {
|
|
618
|
+
this.#shared.assetGates ??= Promise.resolve().then(() => assetGates(loaded));
|
|
619
|
+
return this.#shared.assetGates;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* What this view shows: every published node when gates are ignored or the snapshot has none;
|
|
624
|
+
* otherwise the nodes (and inherited assets) whose gates are open at this view's instant.
|
|
625
|
+
*/
|
|
626
|
+
#current() {
|
|
627
|
+
const now = this.#clock === null ? null : this.#clock();
|
|
628
|
+
if (this.#view?.now === now) return this.#view.view;
|
|
629
|
+
const view = (async () => {
|
|
630
|
+
const loaded = await this.#load();
|
|
631
|
+
if (now === null || loaded.gates.length === 0) return loaded;
|
|
632
|
+
const gates = await this.#assetGates(loaded);
|
|
633
|
+
const hidden = new Set([...gates].filter(([, from]) => !(from <= now)).map(([id]) => id));
|
|
634
|
+
const assets =
|
|
635
|
+
hidden.size === 0 ? loaded.assets : loaded.assets.filter((a) => !hidden.has(a.id));
|
|
636
|
+
const assetIndex =
|
|
637
|
+
hidden.size === 0
|
|
638
|
+
? loaded.assetIndex
|
|
639
|
+
: new Map([...loaded.assetIndex].filter(([, a]) => !hidden.has(a.id)));
|
|
640
|
+
return {
|
|
641
|
+
...loaded,
|
|
642
|
+
pages: loaded.pages.filter((p) => isAvailable(p, now)),
|
|
643
|
+
entries: loaded.entries.filter((e) => isAvailable(e, now)),
|
|
644
|
+
assets,
|
|
645
|
+
assetIndex,
|
|
646
|
+
};
|
|
647
|
+
})();
|
|
648
|
+
this.#view = { now, view };
|
|
649
|
+
return view;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
async site() {
|
|
653
|
+
return (await this.#current()).site;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
async pages() {
|
|
657
|
+
return (await this.#current()).pages;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
async pageByPath(path , locale ) {
|
|
661
|
+
const { site, pages } = await this.#current();
|
|
662
|
+
const loc = locale ?? site.defaultLocale;
|
|
663
|
+
return pages.find((p) => p.path === path && p.locale === loc);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
async entries(collection , locale ) {
|
|
667
|
+
const { site, entries } = await this.#current();
|
|
668
|
+
const loc = locale ?? site.defaultLocale;
|
|
669
|
+
return entries.filter((e) => e.collection === collection && e.locale === loc).sort(byNewest);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
async entryBySlug(
|
|
673
|
+
collection ,
|
|
674
|
+
slug ,
|
|
675
|
+
locale ,
|
|
676
|
+
) {
|
|
677
|
+
return (await this.entries(collection, locale)).find((e) => e.slug === slug);
|
|
678
|
+
}
|
|
679
|
+
async allEntries(locale ) {
|
|
680
|
+
const { site, entries } = await this.#current();
|
|
681
|
+
const loc = locale ?? site.defaultLocale;
|
|
682
|
+
return entries.filter((e) => e.locale === loc).sort(byNewest);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
async search(query , options = {}) {
|
|
686
|
+
const locale = options.locale ?? (await this.site()).defaultLocale;
|
|
687
|
+
return this.#store.search(this.#snapshot.siteId, query, {
|
|
688
|
+
...options,
|
|
689
|
+
locale,
|
|
690
|
+
now: this.#clock?.() ?? Date.now(),
|
|
691
|
+
snapshotId: this.#snapshot.id,
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** A published entry of this snapshot by id (any locale), if its gates are open. */
|
|
696
|
+
async entryById(id ) {
|
|
697
|
+
return (await this.#current()).entries.find((e) => e.id === id);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
async taxonomies() {
|
|
701
|
+
return (await this.#current()).taxonomies;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
async terms(key ) {
|
|
705
|
+
const { site, taxonomies, terms } = await this.#current();
|
|
706
|
+
const tax = taxonomies.find((t) => t.key === key);
|
|
707
|
+
if (!tax) return [];
|
|
708
|
+
return terms
|
|
709
|
+
.filter((t) => t.taxonomyId === tax.id && shown(t, false))
|
|
710
|
+
.sort(byPosition(site.defaultLocale));
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
async termBySlug(key , slug ) {
|
|
714
|
+
return (await this.terms(key)).find((t) => t.slug === slug);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
async vocabulary(key ) {
|
|
718
|
+
const { site, taxonomies, terms } = await this.#current();
|
|
719
|
+
if (key === undefined) return [...terms].sort(byPosition(site.defaultLocale));
|
|
720
|
+
const tax = taxonomies.find((t) => t.key === key);
|
|
721
|
+
if (!tax) return [];
|
|
722
|
+
return terms.filter((t) => t.taxonomyId === tax.id).sort(byPosition(site.defaultLocale));
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
async entriesByFilter(
|
|
726
|
+
filter ,
|
|
727
|
+
options = {},
|
|
728
|
+
) {
|
|
729
|
+
const { site, entries, taxonomies, terms } = await this.#current();
|
|
730
|
+
const loc = options.locale ?? site.defaultLocale;
|
|
731
|
+
const f = compile(filter, taxonomies, terms);
|
|
732
|
+
const out = entries.filter((e) => e.locale === loc && matches(f, e)).sort(byNewest);
|
|
733
|
+
return options.limit === undefined ? out : out.slice(0, options.limit);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
entriesByTerm(
|
|
737
|
+
key ,
|
|
738
|
+
slug ,
|
|
739
|
+
options = {},
|
|
740
|
+
) {
|
|
741
|
+
return this.entriesByFilter(termFilter(key, slug, options.descendants === true), options);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
async redirectFor(path , locale ) {
|
|
745
|
+
const { site, redirects } = await this.#current();
|
|
746
|
+
return matchRedirect(redirects, path, locale && redirectLocale(locale, site.defaultLocale));
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
async redirects() {
|
|
750
|
+
return (await this.#current()).redirects;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
async menu(key ) {
|
|
754
|
+
return (await this.#current()).menus.find((m) => m.key === key);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
async navigation(
|
|
758
|
+
key ,
|
|
759
|
+
locale ,
|
|
760
|
+
options = {},
|
|
761
|
+
) {
|
|
762
|
+
const view = await this.#current();
|
|
763
|
+
const menu = view.menus.find((m) => m.key === key);
|
|
764
|
+
if (!menu) return undefined;
|
|
765
|
+
// Only published pages and entries whose gates are open are here: anything else resolves as
|
|
766
|
+
// missing, so its menu item is hidden (RFC-0003 §7.1, RFC-0008 §7.2).
|
|
767
|
+
return navigationFor(
|
|
768
|
+
menu,
|
|
769
|
+
{
|
|
770
|
+
site: view.site,
|
|
771
|
+
pages: view.pages,
|
|
772
|
+
entries: view.entries,
|
|
773
|
+
taxonomies: view.taxonomies,
|
|
774
|
+
terms: view.terms,
|
|
775
|
+
...(options.collectionIndex ? { collectionIndex: options.collectionIndex } : {}),
|
|
776
|
+
},
|
|
777
|
+
locale ?? view.site.defaultLocale,
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
async assets() {
|
|
782
|
+
return (await this.#current()).assets;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
async asset(id ) {
|
|
786
|
+
return id.startsWith("asset_") ? (await this.#current()).assetIndex.get(id) : undefined;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
async assetByPrefix(prefix16 ) {
|
|
790
|
+
return /^[0-9a-f]{16}$/.test(prefix16)
|
|
791
|
+
? (await this.#current()).assetIndex.get(prefix16)
|
|
792
|
+
: undefined;
|
|
793
|
+
}
|
|
794
|
+
}
|