@cloudhal/astro-renderer 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-request source selection for a deployed site (Cloudflare Worker or a Node server):
|
|
3
|
+
*
|
|
4
|
+
* - **Published** (default): the site's `publishedSnapshotId` pointer decides what renders, so
|
|
5
|
+
* publish and rollback are pointer flips — no rebuild, no redeploy. Snapshots are immutable,
|
|
6
|
+
* so their sources are cached per isolate; the pointer itself is re-read after `pointerTtlMs`.
|
|
7
|
+
* - **Time gates** (RFC-0008 §7): the published source is the snapshot as seen at the request's
|
|
8
|
+
* instant (`SnapshotSource.at`), so embargoed and expired pages and entries are absent.
|
|
9
|
+
* - **Preview**: a signed token (`?cloudhal-preview=<token>`, then a cookie) shows drafts (`live`)
|
|
10
|
+
* or any snapshot, gated nodes included. Tokens are minted by the admin API (`mintPreviewToken`). `?cloudhal-preview=exit`
|
|
11
|
+
* leaves preview mode.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { verifyPreviewToken } from "@cloudhal/core-ts";
|
|
15
|
+
import { LiveSource, SnapshotSource } from "./source.js";
|
|
16
|
+
|
|
17
|
+
export const PREVIEW_PARAM = "cloudhal-preview";
|
|
18
|
+
export const PREVIEW_COOKIE = "cloudhal_preview";
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
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
|
+
function readCookie(header , name ) {
|
|
63
|
+
if (!header) return undefined;
|
|
64
|
+
for (const part of header.split(";")) {
|
|
65
|
+
const [k, ...v] = part.trim().split("=");
|
|
66
|
+
if (k === name) return decodeURIComponent(v.join("="));
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class SiteResolver {
|
|
72
|
+
#o
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
;
|
|
82
|
+
#pointer ;
|
|
83
|
+
#snapshots = new Map ();
|
|
84
|
+
|
|
85
|
+
constructor(options ) {
|
|
86
|
+
this.#o = {
|
|
87
|
+
store: options.store,
|
|
88
|
+
siteId: options.siteId,
|
|
89
|
+
pointerTtlMs: options.pointerTtlMs ?? 2000,
|
|
90
|
+
snapshotCacheSize: options.snapshotCacheSize ?? 8,
|
|
91
|
+
previewCookie: options.previewCookie ?? true,
|
|
92
|
+
now: options.now ?? Date.now,
|
|
93
|
+
...(options.previewSecret ? { previewSecret: options.previewSecret } : {}),
|
|
94
|
+
...(options.persistBundle ? { persistBundle: options.persistBundle } : {}),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The store sources read from (the media route needs its blobs and ledger). */
|
|
99
|
+
get store() {
|
|
100
|
+
return this.#o.store;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Forget the cached pointer (e.g. after a publish handled in the same isolate). */
|
|
104
|
+
invalidate() {
|
|
105
|
+
this.#pointer = undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async site() {
|
|
109
|
+
const now = this.#o.now();
|
|
110
|
+
if (!this.#pointer || now - this.#pointer.at > this.#o.pointerTtlMs) {
|
|
111
|
+
const site = await this.#o.store.getSite(this.#o.siteId);
|
|
112
|
+
if (!site) throw new Error(`site ${this.#o.siteId} not found`);
|
|
113
|
+
this.#pointer = { site, at: now };
|
|
114
|
+
}
|
|
115
|
+
return this.#pointer.site;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
snapshot(id ) {
|
|
119
|
+
let entry = this.#snapshots.get(id);
|
|
120
|
+
if (entry) {
|
|
121
|
+
// refresh LRU position
|
|
122
|
+
this.#snapshots.delete(id);
|
|
123
|
+
this.#snapshots.set(id, entry);
|
|
124
|
+
return entry;
|
|
125
|
+
}
|
|
126
|
+
entry = SnapshotSource.open(this.#o.store, id);
|
|
127
|
+
entry.catch(() => this.#snapshots.delete(id));
|
|
128
|
+
this.#snapshots.set(id, entry);
|
|
129
|
+
while (this.#snapshots.size > this.#o.snapshotCacheSize) {
|
|
130
|
+
const oldest = this.#snapshots.keys().next().value ;
|
|
131
|
+
this.#snapshots.delete(oldest);
|
|
132
|
+
}
|
|
133
|
+
return entry;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async resolve(request ) {
|
|
137
|
+
const url = new URL(request.url);
|
|
138
|
+
const fromUrl = url.searchParams.get(PREVIEW_PARAM);
|
|
139
|
+
const secure = url.protocol === "https:";
|
|
140
|
+
const cookie = (value , maxAge ) =>
|
|
141
|
+
`${PREVIEW_COOKIE}=${encodeURIComponent(value)}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${maxAge}${secure ? "; Secure" : ""}`;
|
|
142
|
+
|
|
143
|
+
let previewError ;
|
|
144
|
+
let clearCookie ;
|
|
145
|
+
if (fromUrl === "exit") {
|
|
146
|
+
clearCookie = cookie("", 0);
|
|
147
|
+
} else {
|
|
148
|
+
const token =
|
|
149
|
+
fromUrl ??
|
|
150
|
+
(this.#o.previewCookie
|
|
151
|
+
? readCookie(request.headers.get("cookie"), PREVIEW_COOKIE)
|
|
152
|
+
: undefined);
|
|
153
|
+
if (token && this.#o.previewSecret) {
|
|
154
|
+
try {
|
|
155
|
+
const claims = await verifyPreviewToken(
|
|
156
|
+
this.#o.previewSecret,
|
|
157
|
+
token,
|
|
158
|
+
this.#o.siteId,
|
|
159
|
+
this.#o.now(),
|
|
160
|
+
);
|
|
161
|
+
// Previews show drafts and gated nodes alike (RFC-0008 §7.3); they are never cached.
|
|
162
|
+
const source =
|
|
163
|
+
claims.target === "live"
|
|
164
|
+
? new LiveSource(this.#o.store, this.#o.siteId, { includeDrafts: true })
|
|
165
|
+
: (await this.snapshot(claims.target.snapshot)).ungated();
|
|
166
|
+
if (source instanceof SnapshotSource && source.snapshot.siteId !== this.#o.siteId)
|
|
167
|
+
throw new Error("snapshot belongs to another site");
|
|
168
|
+
const maxAge = Math.max(0, claims.exp - Math.floor(this.#o.now() / 1000));
|
|
169
|
+
return {
|
|
170
|
+
kind: "preview",
|
|
171
|
+
source,
|
|
172
|
+
target: claims.target,
|
|
173
|
+
...(fromUrl && this.#o.previewCookie ? { setCookie: cookie(token, maxAge) } : {}),
|
|
174
|
+
};
|
|
175
|
+
} catch (e) {
|
|
176
|
+
previewError = e instanceof Error ? e.message : String(e);
|
|
177
|
+
if (!fromUrl) clearCookie = cookie("", 0);
|
|
178
|
+
}
|
|
179
|
+
} else if (token) {
|
|
180
|
+
previewError = "preview is not enabled on this deployment";
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const site = await this.site();
|
|
185
|
+
if (!site.publishedSnapshotId)
|
|
186
|
+
return {
|
|
187
|
+
kind: "unpublished",
|
|
188
|
+
...(previewError ? { previewError } : {}),
|
|
189
|
+
...(clearCookie ? { setCookie: clearCookie } : {}),
|
|
190
|
+
};
|
|
191
|
+
// One instant for the whole request (RFC-0008 §7.1): a page and the lists on it agree.
|
|
192
|
+
const at = this.#o.now();
|
|
193
|
+
const live = await this.snapshot(site.publishedSnapshotId);
|
|
194
|
+
if (this.#o.persistBundle) live.persistBundle(this.#o.persistBundle);
|
|
195
|
+
const source = live.at(at);
|
|
196
|
+
const published = {
|
|
197
|
+
kind: "published",
|
|
198
|
+
source,
|
|
199
|
+
snapshotId: site.publishedSnapshotId,
|
|
200
|
+
at,
|
|
201
|
+
...(previewError ? { previewError } : {}),
|
|
202
|
+
};
|
|
203
|
+
// Leaving preview: the cookie must be cleared on a published response too.
|
|
204
|
+
return clearCookie ? { ...published, setCookie: clearCookie } : published;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How URLs map onto the graph, shared by the starter's routes, sitemaps, feeds and previews: a
|
|
3
|
+
* Page owns its `path`; an entry lives at `/<collection>/<slug>`; a term archive at
|
|
4
|
+
* `<basePath>/<slug>[/page/<n>|/rss.xml]` (RFC-0002 §7). Pages win when several could answer, so a
|
|
5
|
+
* page at `/posts/x` is never shadowed by an article. Anything else asks the site's redirects
|
|
6
|
+
* before it is a 404 (or a 410 for a retired term).
|
|
7
|
+
*/
|
|
8
|
+
import type { ContentEntry, Page, Redirect, Site } from "@cloudhal/core-contracts";
|
|
9
|
+
import { type ArchiveSummary, type ArchiveView } from "./archive.js";
|
|
10
|
+
import { type SearchView } from "./search.js";
|
|
11
|
+
import type { GraphSource } from "./source.js";
|
|
12
|
+
import { type TaxonomyIndex } from "./taxonomy.js";
|
|
13
|
+
export declare function entryPath(entry: Pick<ContentEntry, "collection" | "slug">): string;
|
|
14
|
+
export type Resolved = {
|
|
15
|
+
kind: "page";
|
|
16
|
+
page: Page;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "entry";
|
|
19
|
+
entry: ContentEntry;
|
|
20
|
+
};
|
|
21
|
+
/** The page or entry at `path` (leading slash, no trailing slash except "/") in `locale`, if any. */
|
|
22
|
+
export declare function resolvePath(source: GraphSource, path: string, locale?: string): Promise<Resolved | undefined>;
|
|
23
|
+
/** Every page and entry path of a locale (default: the site's), as graph paths without prefix. */
|
|
24
|
+
export declare function routablePaths(source: GraphSource, locale?: string): Promise<string[]>;
|
|
25
|
+
export interface NavLink {
|
|
26
|
+
href: string;
|
|
27
|
+
label: string;
|
|
28
|
+
/** The current page or one of its children. */
|
|
29
|
+
current: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The header navigation until a navigation editor exists: the site's top-level pages in the
|
|
33
|
+
* site's page order (home excluded), in the default locale.
|
|
34
|
+
*/
|
|
35
|
+
export declare function navLinks(site: Site, pages: Page[], currentPath: string): NavLink[];
|
|
36
|
+
/**
|
|
37
|
+
* Top-level categories with a served archive, by `position` then name (themes choose whether to
|
|
38
|
+
* show them; the blog starter appends them to `navLinks`).
|
|
39
|
+
*/
|
|
40
|
+
export declare function categoryNavLinks(archives: readonly ArchiveSummary[], currentPath: string, locale: string): NavLink[];
|
|
41
|
+
export type RouteResult = {
|
|
42
|
+
readonly kind: "page";
|
|
43
|
+
readonly page: Page;
|
|
44
|
+
} | {
|
|
45
|
+
readonly kind: "entry";
|
|
46
|
+
readonly entry: ContentEntry;
|
|
47
|
+
} | {
|
|
48
|
+
readonly kind: "archive";
|
|
49
|
+
readonly archive: ArchiveView;
|
|
50
|
+
readonly index: TaxonomyIndex;
|
|
51
|
+
} | {
|
|
52
|
+
readonly kind: "feed";
|
|
53
|
+
readonly archive: ArchiveView;
|
|
54
|
+
readonly index: TaxonomyIndex;
|
|
55
|
+
} | {
|
|
56
|
+
readonly kind: "search";
|
|
57
|
+
readonly search: SearchView;
|
|
58
|
+
} | {
|
|
59
|
+
readonly kind: "redirect";
|
|
60
|
+
readonly status: 301 | 302;
|
|
61
|
+
readonly location: string;
|
|
62
|
+
} | {
|
|
63
|
+
readonly kind: "gone";
|
|
64
|
+
} | {
|
|
65
|
+
readonly kind: "not-found";
|
|
66
|
+
};
|
|
67
|
+
export interface RouteOptions {
|
|
68
|
+
/** Entries per archive page. Default `DEFAULT_ARCHIVE_PAGE_SIZE`. */
|
|
69
|
+
readonly perPage?: number;
|
|
70
|
+
/** The request's query string (`?a=b`), kept on redirects. */
|
|
71
|
+
readonly search?: string;
|
|
72
|
+
/** The locale the request is for (RFC-0033; `localeRequest`). Default: the site's. */
|
|
73
|
+
readonly locale?: string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A redirect's `Location`: the matched location (core-ts `joinTarget` already appended the rest
|
|
77
|
+
* of a prefix match) plus the request's query string, merged into a query the target already has
|
|
78
|
+
* and placed before its fragment. A preview token never leaves the site.
|
|
79
|
+
*/
|
|
80
|
+
export declare function redirectLocation(location: string, search?: string): string;
|
|
81
|
+
/**
|
|
82
|
+
* Resolves a request path in the order of RFC-0002 §7: page → entry → archive (and its feed) →
|
|
83
|
+
* redirect → 410 for a retired term → 404. A trailing slash is ignored. `/search` (RFC-0019 §3)
|
|
84
|
+
* answers after pages, when the source can search: `?q=…&page=n`, past the last page a 404.
|
|
85
|
+
*/
|
|
86
|
+
export declare function resolveRoute(source: GraphSource, requestPath: string, options?: RouteOptions): Promise<RouteResult>;
|
|
87
|
+
/**
|
|
88
|
+
* Request paths that a redirect answers and a static build can write a page for: every `from`,
|
|
89
|
+
* and for prefix redirects the old twin of every served path under the target
|
|
90
|
+
* (`/tag/old/page/2` for `/tag/new/page/2`). Feed paths (`…/rss.xml`) are left out: a
|
|
91
|
+
* meta-refresh page cannot stand in for XML (`redirectsFile` covers them).
|
|
92
|
+
*/
|
|
93
|
+
export declare function redirectPaths(redirects: readonly Redirect[], served: readonly string[]): string[];
|
|
94
|
+
export interface StaticPaths {
|
|
95
|
+
/** For the catch-all page route: pages, entries, archive pages, then paths that redirect. */
|
|
96
|
+
readonly pages: string[];
|
|
97
|
+
/** For the term feed route (`<archive>/rss.xml`). */
|
|
98
|
+
readonly feeds: string[];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Everything a static build writes (`getStaticPaths`): routable pages and entries, every page of
|
|
102
|
+
* every served archive and its feed, and the paths redirects answer (Astro writes those as
|
|
103
|
+
* meta-refresh pages; `redirectsFile` gives hosts the real 301s). 404s and 410s are not built.
|
|
104
|
+
*/
|
|
105
|
+
export declare function staticPaths(source: GraphSource, options?: {
|
|
106
|
+
perPage?: number;
|
|
107
|
+
locale?: string;
|
|
108
|
+
}): Promise<StaticPaths>;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How URLs map onto the graph, shared by the starter's routes, sitemaps, feeds and previews: a
|
|
3
|
+
* Page owns its `path`; an entry lives at `/<collection>/<slug>`; a term archive at
|
|
4
|
+
* `<basePath>/<slug>[/page/<n>|/rss.xml]` (RFC-0002 §7). Pages win when several could answer, so a
|
|
5
|
+
* page at `/posts/x` is never shadowed by an article. Anything else asks the site's redirects
|
|
6
|
+
* before it is a 404 (or a 410 for a retired term).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { normalisePath, SEARCH_PATH, termName } from "@cloudhal/core-ts";
|
|
10
|
+
import {
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
archiveState,
|
|
14
|
+
archiveSummaries,
|
|
15
|
+
DEFAULT_ARCHIVE_PAGE_SIZE,
|
|
16
|
+
pageCount,
|
|
17
|
+
parseArchivePath,
|
|
18
|
+
termForSlug,
|
|
19
|
+
} from "./archive.js";
|
|
20
|
+
import { PREVIEW_PARAM } from "./resolver.js";
|
|
21
|
+
import { searchRequest } from "./search.js";
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
archiveFeedPath,
|
|
25
|
+
archivePagePath,
|
|
26
|
+
isPublicTerm,
|
|
27
|
+
loadTaxonomyIndex,
|
|
28
|
+
publicTrail,
|
|
29
|
+
shownTerm,
|
|
30
|
+
|
|
31
|
+
taxonomyIndex,
|
|
32
|
+
} from "./taxonomy.js";
|
|
33
|
+
|
|
34
|
+
export function entryPath(entry ) {
|
|
35
|
+
return `/${entry.collection}/${entry.slug}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
/** The page or entry at `path` (leading slash, no trailing slash except "/") in `locale`, if any. */
|
|
41
|
+
export async function resolvePath(
|
|
42
|
+
source ,
|
|
43
|
+
path ,
|
|
44
|
+
locale ,
|
|
45
|
+
) {
|
|
46
|
+
const page = await source.pageByPath(path, locale);
|
|
47
|
+
if (page) return { kind: "page", page };
|
|
48
|
+
const m = /^\/([a-z0-9]+(?:-[a-z0-9]+)*)\/([a-z0-9]+(?:-[a-z0-9]+)*)$/.exec(path);
|
|
49
|
+
if (!m) return undefined;
|
|
50
|
+
const entry = await source.entryBySlug(m[1] , m[2] , locale);
|
|
51
|
+
return entry ? { kind: "entry", entry } : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Every page and entry path of a locale (default: the site's), as graph paths without prefix. */
|
|
55
|
+
export async function routablePaths(source , locale ) {
|
|
56
|
+
const pages = await source.pages();
|
|
57
|
+
const loc = locale ?? (await source.site()).defaultLocale;
|
|
58
|
+
const pagePaths = pages.filter((p) => p.locale === loc).map((p) => p.path);
|
|
59
|
+
const taken = new Set(pagePaths);
|
|
60
|
+
const entryPaths = (await source.allEntries(loc)).map(entryPath).filter((p) => !taken.has(p));
|
|
61
|
+
return [...pagePaths, ...entryPaths];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The header navigation until a navigation editor exists: the site's top-level pages in the
|
|
73
|
+
* site's page order (home excluded), in the default locale.
|
|
74
|
+
*/
|
|
75
|
+
export function navLinks(site , pages , currentPath ) {
|
|
76
|
+
return pages
|
|
77
|
+
.filter((p) => p.locale === site.defaultLocale && p.path !== "/" && /^\/[^/]+$/.test(p.path))
|
|
78
|
+
.map((p) => ({
|
|
79
|
+
href: p.path,
|
|
80
|
+
label: p.title,
|
|
81
|
+
current: currentPath === p.path || currentPath.startsWith(`${p.path}/`),
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Top-level categories with a served archive, by `position` then name (themes choose whether to
|
|
87
|
+
* show them; the blog starter appends them to `navLinks`).
|
|
88
|
+
*/
|
|
89
|
+
export function categoryNavLinks(
|
|
90
|
+
archives ,
|
|
91
|
+
currentPath ,
|
|
92
|
+
locale ,
|
|
93
|
+
) {
|
|
94
|
+
return archives
|
|
95
|
+
.filter((a) => a.taxonomy.key === "category" && !a.term.parentId)
|
|
96
|
+
.map((a) => ({
|
|
97
|
+
href: a.path,
|
|
98
|
+
label: termName(a.term, locale),
|
|
99
|
+
current: currentPath === a.path || currentPath.startsWith(`${a.path}/`),
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A redirect's `Location`: the matched location (core-ts `joinTarget` already appended the rest
|
|
124
|
+
* of a prefix match) plus the request's query string, merged into a query the target already has
|
|
125
|
+
* and placed before its fragment. A preview token never leaves the site.
|
|
126
|
+
*/
|
|
127
|
+
export function redirectLocation(location , search = "") {
|
|
128
|
+
const params = new URLSearchParams(search);
|
|
129
|
+
if (!location.startsWith("/")) params.delete(PREVIEW_PARAM);
|
|
130
|
+
const query = params.toString();
|
|
131
|
+
if (!query) return location;
|
|
132
|
+
const hash = location.indexOf("#");
|
|
133
|
+
const head = hash < 0 ? location : location.slice(0, hash);
|
|
134
|
+
const tail = hash < 0 ? "" : location.slice(hash);
|
|
135
|
+
return `${head}${head.includes("?") ? "&" : "?"}${query}${tail}`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Resolves a request path in the order of RFC-0002 §7: page → entry → archive (and its feed) →
|
|
140
|
+
* redirect → 410 for a retired term → 404. A trailing slash is ignored. `/search` (RFC-0019 §3)
|
|
141
|
+
* answers after pages, when the source can search: `?q=…&page=n`, past the last page a 404.
|
|
142
|
+
*/
|
|
143
|
+
export async function resolveRoute(
|
|
144
|
+
source ,
|
|
145
|
+
requestPath ,
|
|
146
|
+
options = {},
|
|
147
|
+
) {
|
|
148
|
+
const path = normalisePath(requestPath);
|
|
149
|
+
const site = await source.site();
|
|
150
|
+
const locale = options.locale ?? site.defaultLocale;
|
|
151
|
+
const resolved = await resolvePath(source, path, locale);
|
|
152
|
+
if (resolved) return resolved;
|
|
153
|
+
if (path === SEARCH_PATH && source.search) {
|
|
154
|
+
const { query, page } = searchRequest(options.search ?? "");
|
|
155
|
+
const search = await source.search(query, { page, locale });
|
|
156
|
+
if (page > 1 && page > search.pages) return { kind: "not-found" };
|
|
157
|
+
return { kind: "search", search };
|
|
158
|
+
}
|
|
159
|
+
let gone = false;
|
|
160
|
+
const taxonomies = await source.taxonomies();
|
|
161
|
+
const request = parseArchivePath(taxonomies, path);
|
|
162
|
+
if (request) {
|
|
163
|
+
const vocabulary = await source.vocabulary();
|
|
164
|
+
const term = termForSlug(
|
|
165
|
+
vocabulary.filter((t) => t.taxonomyId === request.taxonomy.id),
|
|
166
|
+
request.slug,
|
|
167
|
+
{ locale, defaultLocale: site.defaultLocale },
|
|
168
|
+
);
|
|
169
|
+
const listed =
|
|
170
|
+
term && isPublicTerm(term)
|
|
171
|
+
? await source.entriesByTerm(request.taxonomy.key, term.id, {
|
|
172
|
+
locale,
|
|
173
|
+
descendants: request.taxonomy.hierarchical,
|
|
174
|
+
})
|
|
175
|
+
: [];
|
|
176
|
+
const state = archiveState(request.taxonomy, term, listed.length, locale);
|
|
177
|
+
gone = state.status === 410;
|
|
178
|
+
if (state.status === 200 && term) {
|
|
179
|
+
if (request.explicitFirstPage)
|
|
180
|
+
return {
|
|
181
|
+
kind: "redirect",
|
|
182
|
+
status: 301,
|
|
183
|
+
location: redirectLocation(request.root, options.search),
|
|
184
|
+
};
|
|
185
|
+
const perPage = options.perPage ?? DEFAULT_ARCHIVE_PAGE_SIZE;
|
|
186
|
+
const pages = pageCount(listed.length, perPage);
|
|
187
|
+
const index = taxonomyIndex(taxonomies, vocabulary, locale, site.defaultLocale);
|
|
188
|
+
const shown = shownTerm(index, term);
|
|
189
|
+
const servesFeed = request.kind === "feed" && request.taxonomy.feed;
|
|
190
|
+
if (shown && request.page <= pages && (request.kind === "page" || servesFeed)) {
|
|
191
|
+
const archive = {
|
|
192
|
+
taxonomy: request.taxonomy,
|
|
193
|
+
term,
|
|
194
|
+
shown,
|
|
195
|
+
trail: publicTrail(index, term),
|
|
196
|
+
locale,
|
|
197
|
+
root: request.root,
|
|
198
|
+
path: archivePagePath(request.root, request.page),
|
|
199
|
+
...(request.taxonomy.feed ? { feedPath: archiveFeedPath(request.root) } : {}),
|
|
200
|
+
page: request.page,
|
|
201
|
+
pages,
|
|
202
|
+
perPage,
|
|
203
|
+
total: listed.length,
|
|
204
|
+
entries:
|
|
205
|
+
request.kind === "feed"
|
|
206
|
+
? listed
|
|
207
|
+
: listed.slice((request.page - 1) * perPage, request.page * perPage),
|
|
208
|
+
indexable: state.indexable,
|
|
209
|
+
};
|
|
210
|
+
return { kind: request.kind === "feed" ? "feed" : "archive", archive, index };
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const redirect = await source.redirectFor(path, locale);
|
|
215
|
+
if (redirect)
|
|
216
|
+
return {
|
|
217
|
+
kind: "redirect",
|
|
218
|
+
status: redirect.redirect.status,
|
|
219
|
+
location: redirectLocation(redirect.location, options.search),
|
|
220
|
+
};
|
|
221
|
+
return gone ? { kind: "gone" } : { kind: "not-found" };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Request paths that a redirect answers and a static build can write a page for: every `from`,
|
|
226
|
+
* and for prefix redirects the old twin of every served path under the target
|
|
227
|
+
* (`/tag/old/page/2` for `/tag/new/page/2`). Feed paths (`…/rss.xml`) are left out: a
|
|
228
|
+
* meta-refresh page cannot stand in for XML (`redirectsFile` covers them).
|
|
229
|
+
*/
|
|
230
|
+
export function redirectPaths(redirects , served ) {
|
|
231
|
+
const out = new Set ();
|
|
232
|
+
for (const r of redirects) {
|
|
233
|
+
out.add(r.from);
|
|
234
|
+
if (r.match !== "prefix" || !r.to.startsWith("/")) continue;
|
|
235
|
+
const target = r.to.split(/[?#]/, 1)[0] ?? r.to;
|
|
236
|
+
for (const path of served)
|
|
237
|
+
if (path.startsWith(`${target}/`)) out.add(`${r.from}${path.slice(target.length)}`);
|
|
238
|
+
}
|
|
239
|
+
const taken = new Set(served);
|
|
240
|
+
return [...out].filter((p) => !taken.has(p) && p !== "/" && !/\.[a-z0-9]+$/i.test(p));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Everything a static build writes (`getStaticPaths`): routable pages and entries, every page of
|
|
252
|
+
* every served archive and its feed, and the paths redirects answer (Astro writes those as
|
|
253
|
+
* meta-refresh pages; `redirectsFile` gives hosts the real 301s). 404s and 410s are not built.
|
|
254
|
+
*/
|
|
255
|
+
export async function staticPaths(
|
|
256
|
+
source ,
|
|
257
|
+
options = {},
|
|
258
|
+
) {
|
|
259
|
+
const perPage = options.perPage ?? DEFAULT_ARCHIVE_PAGE_SIZE;
|
|
260
|
+
const site = await source.site();
|
|
261
|
+
const locale = options.locale ?? site.defaultLocale;
|
|
262
|
+
const tagged = locale === site.defaultLocale ? undefined : locale;
|
|
263
|
+
const [base, index, entries, redirects] = await Promise.all([
|
|
264
|
+
routablePaths(source, locale),
|
|
265
|
+
loadTaxonomyIndex(source, locale),
|
|
266
|
+
source.allEntries(locale),
|
|
267
|
+
source.redirects().then((all) => all.filter((r) => r.locale === tagged)),
|
|
268
|
+
]);
|
|
269
|
+
// The entries the source shows (a live dev source includes drafts), as `resolveRoute` counts them.
|
|
270
|
+
const archives = archiveSummaries(index, entries);
|
|
271
|
+
const taken = new Set(base);
|
|
272
|
+
const archivePages = archives
|
|
273
|
+
.flatMap((a) =>
|
|
274
|
+
Array.from({ length: pageCount(a.total, perPage) }, (_, i) => archivePagePath(a.path, i + 1)),
|
|
275
|
+
)
|
|
276
|
+
.filter((p) => !taken.has(p));
|
|
277
|
+
const served = [...base, ...archivePages];
|
|
278
|
+
const feeds = archives.flatMap((a) => (a.feedPath ? [a.feedPath] : []));
|
|
279
|
+
// `…/page/1` answers with a 301 to the archive root (a meta-refresh page in static builds).
|
|
280
|
+
const firstPages = archives.map((a) => `${a.path}/page/1`);
|
|
281
|
+
return {
|
|
282
|
+
pages: [...served, ...firstPages, ...redirectPaths(redirects, [...served, ...feeds])],
|
|
283
|
+
feeds,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `workerd` build of `@cloudhal/astro-renderer/runtime` (selected by the `workerd` export
|
|
3
|
+
* condition): the deployed Site Worker. Storage comes from bindings — D1 `DB` for nodes and the
|
|
4
|
+
* published pointer, R2 `BLOBS` for snapshot bodies — and every request is resolved on its own:
|
|
5
|
+
* published snapshot by default, drafts/any snapshot behind a signed preview link.
|
|
6
|
+
*
|
|
7
|
+
* Response headers set here drive the Workers Cache (ADR-0012): published pages are cached at the
|
|
8
|
+
* edge under `Cache-Tag: cloudhal-site-<siteId>` until a publish/rollback purges that tag, and
|
|
9
|
+
* never past the snapshot's next time gate (RFC-0008 §7.4); previews are never cached.
|
|
10
|
+
*/
|
|
11
|
+
import { SqlCommentStore } from "@cloudhal/comments/sql";
|
|
12
|
+
import type { Site } from "@cloudhal/core-contracts";
|
|
13
|
+
import { type AiSearchEnv } from "@cloudhal/core-ts";
|
|
14
|
+
import type { SqlDriver } from "@cloudhal/storage";
|
|
15
|
+
import type { CloudhalRendererConfig } from "./integration.js";
|
|
16
|
+
import type { PeerEdition } from "./locales.js";
|
|
17
|
+
import type { GraphSource } from "./source.js";
|
|
18
|
+
/** RFC-0029 §3: `AI_SEARCH`, `SEARCH_BUCKET` and `CLOUDHAL_SEARCH_*` turn on semantic search. */
|
|
19
|
+
export interface SiteWorkerEnv extends AiSearchEnv {
|
|
20
|
+
DB: D1Database;
|
|
21
|
+
BLOBS: R2Bucket;
|
|
22
|
+
/** Overrides the site id baked into the build (one Worker can be re-pointed without a rebuild). */
|
|
23
|
+
CLOUDHAL_SITE_ID?: string;
|
|
24
|
+
/** Shared with the admin Worker; enables signed preview links. */
|
|
25
|
+
CLOUDHAL_PREVIEW_SECRET?: string;
|
|
26
|
+
/** Reader comments (RFC-0007 §15.4): without a usable key, comments are off. */
|
|
27
|
+
CLOUDHAL_COMMENTS_KEY?: string;
|
|
28
|
+
}
|
|
29
|
+
/** Minimal shape of the Astro global the page passes in. */
|
|
30
|
+
export interface AstroLike {
|
|
31
|
+
request: Request;
|
|
32
|
+
response: {
|
|
33
|
+
headers: Headers;
|
|
34
|
+
status?: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** Runs `fn` (and everything it awaits) as a request for `siteId`. */
|
|
38
|
+
export declare function withRequestSite<T>(siteId: string, fn: () => T): T;
|
|
39
|
+
/** Test/purge hook: forget cached state in this isolate, for one site or all of them. */
|
|
40
|
+
export declare function resetSource(siteId?: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* What site analytics read on the Site Worker (`SiteAnalytics`, RFC-0006 §5.2): D1 for settings and
|
|
43
|
+
* the daily salt, the site row (published pointer, domains) and the isolate's snapshot cache.
|
|
44
|
+
*/
|
|
45
|
+
export declare function analyticsBackend(siteId: string,
|
|
46
|
+
/** Ignored here (bindings decide); the Node build reads the local store from `dataDir`. */
|
|
47
|
+
_config?: Partial<CloudhalRendererConfig>): {
|
|
48
|
+
sql: () => SqlDriver;
|
|
49
|
+
site: () => Promise<Site | undefined>;
|
|
50
|
+
snapshot: (id: string) => Promise<GraphSource>;
|
|
51
|
+
open: () => Promise<SqlDriver>;
|
|
52
|
+
};
|
|
53
|
+
export { CACHE_TAG_PREFIX } from "./cache-tags.js";
|
|
54
|
+
/**
|
|
55
|
+
* RFC-0034 §6: the other sites of the project that publish a version of `node`'s work (language
|
|
56
|
+
* editions), for `hreflang` and the language switcher. Only what those sites have published
|
|
57
|
+
* counts: their published snapshot is read, never their drafts. A site without a hostname is
|
|
58
|
+
* left out (there is no address to point to).
|
|
59
|
+
*/
|
|
60
|
+
export declare function peerEditions(config: CloudhalRendererConfig, node: {
|
|
61
|
+
id: string;
|
|
62
|
+
translation?: {
|
|
63
|
+
group: string;
|
|
64
|
+
} | undefined;
|
|
65
|
+
path?: string;
|
|
66
|
+
}): Promise<PeerEdition[]>;
|
|
67
|
+
/**
|
|
68
|
+
* The comment store for pages on the Site Worker (RFC-0007 §8.1): D1. `undefined` when the build
|
|
69
|
+
* has no comments module (`comments.available: false`) or the Worker has no usable
|
|
70
|
+
* `CLOUDHAL_COMMENTS_KEY` — comments are then off, and the entry answers their endpoints `404`.
|
|
71
|
+
*/
|
|
72
|
+
export declare function commentsBackend(config: Partial<CloudhalRendererConfig>): Promise<{
|
|
73
|
+
store: SqlCommentStore;
|
|
74
|
+
} | undefined>;
|
|
75
|
+
/**
|
|
76
|
+
* Resolves the source for this request and sets caching/preview headers on the response.
|
|
77
|
+
* Returns `undefined` when the site has never been published (the page should answer 503).
|
|
78
|
+
*/
|
|
79
|
+
export declare function getSource(config: CloudhalRendererConfig, astro?: AstroLike): Promise<GraphSource | undefined>;
|
|
80
|
+
/**
|
|
81
|
+
* The media route (RFC-0004 §7.1), answered by the Site Worker entry before Astro: the asset comes
|
|
82
|
+
* from the published snapshot, or from the preview target behind a valid token (invariant 15).
|
|
83
|
+
* Returns `undefined` for paths that are not media URLs.
|
|
84
|
+
*/
|
|
85
|
+
export declare function serveMedia(request: Request, options?: {
|
|
86
|
+
siteId?: string | undefined;
|
|
87
|
+
}): Promise<Response | undefined>;
|