@cloudhal/astro-renderer 0.6.3 → 0.6.5
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/dist/src/astro-env.d.ts +9 -0
- package/dist/src/cloudflare-entry.d.ts +10 -0
- package/dist/src/cloudflare-entry.js +117 -8
- package/dist/src/feeds.js +2 -1
- package/dist/src/integration.js +34 -5
- package/dist/src/locales.js +2 -1
- package/dist/src/markdown.js +9 -11
- package/dist/src/resolver.d.ts +7 -0
- package/dist/src/resolver.js +12 -0
- package/dist/src/runtime-workerd.d.ts +32 -0
- package/dist/src/runtime-workerd.js +92 -16
- package/dist/src/runtime.d.ts +20 -0
- package/dist/src/runtime.js +24 -0
- package/dist/src/status-entry.d.ts +66 -0
- package/dist/src/status-entry.js +193 -0
- package/dist/src/status-hook.d.ts +36 -0
- package/dist/src/status-hook.js +141 -0
- package/dist/src/status-html.d.ts +51 -0
- package/dist/src/status-html.js +157 -0
- package/dist/src/status-middleware.d.ts +6 -0
- package/dist/src/status-middleware.js +61 -0
- package/dist/src/status-page.d.ts +94 -0
- package/dist/src/status-page.js +323 -0
- package/dist/src/status-serve.d.ts +56 -0
- package/dist/src/status-serve.js +140 -0
- package/dist/src/status-strings/ar.d.ts +2 -0
- package/dist/src/status-strings/ar.js +34 -0
- package/dist/src/status-strings/catalog.d.ts +8 -0
- package/dist/src/status-strings/catalog.js +52 -0
- package/dist/src/status-strings/de.d.ts +2 -0
- package/dist/src/status-strings/de.js +34 -0
- package/dist/src/status-strings/en.d.ts +46 -0
- package/dist/src/status-strings/en.js +47 -0
- package/dist/src/status-strings/es.d.ts +2 -0
- package/dist/src/status-strings/es.js +34 -0
- package/dist/src/status-strings/fa.d.ts +2 -0
- package/dist/src/status-strings/fa.js +34 -0
- package/dist/src/status-strings/fr.d.ts +2 -0
- package/dist/src/status-strings/fr.js +34 -0
- package/dist/src/status-strings/he.d.ts +2 -0
- package/dist/src/status-strings/he.js +34 -0
- package/dist/src/status-strings/hi.d.ts +2 -0
- package/dist/src/status-strings/hi.js +34 -0
- package/dist/src/status-strings/id.d.ts +2 -0
- package/dist/src/status-strings/id.js +34 -0
- package/dist/src/status-strings/it.d.ts +2 -0
- package/dist/src/status-strings/it.js +34 -0
- package/dist/src/status-strings/ja.d.ts +2 -0
- package/dist/src/status-strings/ja.js +34 -0
- package/dist/src/status-strings/ko.d.ts +2 -0
- package/dist/src/status-strings/ko.js +34 -0
- package/dist/src/status-strings/nl.d.ts +2 -0
- package/dist/src/status-strings/nl.js +34 -0
- package/dist/src/status-strings/pl.d.ts +2 -0
- package/dist/src/status-strings/pl.js +34 -0
- package/dist/src/status-strings/pt-BR.d.ts +2 -0
- package/dist/src/status-strings/pt-BR.js +34 -0
- package/dist/src/status-strings/ru.d.ts +2 -0
- package/dist/src/status-strings/ru.js +34 -0
- package/dist/src/status-strings/tr.d.ts +2 -0
- package/dist/src/status-strings/tr.js +34 -0
- package/dist/src/status-strings/uk.d.ts +2 -0
- package/dist/src/status-strings/uk.js +34 -0
- package/dist/src/status-strings/zh-Hans.d.ts +2 -0
- package/dist/src/status-strings/zh-Hans.js +34 -0
- package/package.json +23 -7
package/dist/src/astro-env.d.ts
CHANGED
|
@@ -19,3 +19,12 @@ declare module "virtual:cloudhal/comments-client" {
|
|
|
19
19
|
const client: import("./comments-client.js").CommentsClient | null;
|
|
20
20
|
export default client;
|
|
21
21
|
}
|
|
22
|
+
|
|
23
|
+
declare module "virtual:cloudhal/theme-hooks" {
|
|
24
|
+
/**
|
|
25
|
+
* RFC-0043 §1.7: loads the project's `src/theme.ts` lazily (an empty module when the project
|
|
26
|
+
* has none), so a status page can ask for the theme's `statusPage` hook without evaluating
|
|
27
|
+
* the theme on the normal path.
|
|
28
|
+
*/
|
|
29
|
+
export const loadTheme: () => Promise<Record<string, unknown>>;
|
|
30
|
+
}
|
|
@@ -32,6 +32,16 @@
|
|
|
32
32
|
* render the section themselves (`loadComments`, `<Comments />`) and set their own CSP when an
|
|
33
33
|
* open form uses Turnstile; `withSecurityHeaders` keeps it. All of it is off without a usable
|
|
34
34
|
* `CLOUDHAL_COMMENTS_KEY` (or when the build has no comments module).
|
|
35
|
+
* - Status pages (RFC-0043 §1): before Astro, the entry decides the site's state — `not-published`
|
|
36
|
+
* before the first publish, `preview-expired` for a preview token that does not verify,
|
|
37
|
+
* `unavailable` for a Worker that serves no site — and answers page paths with the renderer's
|
|
38
|
+
* own page (`status-page.ts`: 503/410, `Retry-After`, `no-store`, its own CSP), restyled by the
|
|
39
|
+
* theme's optional `statusPage` hook. Machine paths get a short plain answer for an invalid
|
|
40
|
+
* preview token or an unreadable store (`status-entry.ts`). A store that cannot be read before
|
|
41
|
+
* rendering is `unavailable` (503, `Retry-After: 300`); a thrown error or Astro's own 500 page
|
|
42
|
+
* becomes the `error` page (500) with the `cf-ray` as its reference, in the site's language
|
|
43
|
+
* once the request has read the site; any 5xx the theme or renderer answers on purpose passes.
|
|
44
|
+
* `GET /__cloudhal/health` answers `{ ok, state }` for monitors.
|
|
35
45
|
*/
|
|
36
46
|
import { type CommentsRateLimit } from "./comments.js";
|
|
37
47
|
import { type RedirectEnv } from "./host-redirect.js";
|
|
@@ -32,17 +32,30 @@
|
|
|
32
32
|
* render the section themselves (`loadComments`, `<Comments />`) and set their own CSP when an
|
|
33
33
|
* open form uses Turnstile; `withSecurityHeaders` keeps it. All of it is off without a usable
|
|
34
34
|
* `CLOUDHAL_COMMENTS_KEY` (or when the build has no comments module).
|
|
35
|
+
* - Status pages (RFC-0043 §1): before Astro, the entry decides the site's state — `not-published`
|
|
36
|
+
* before the first publish, `preview-expired` for a preview token that does not verify,
|
|
37
|
+
* `unavailable` for a Worker that serves no site — and answers page paths with the renderer's
|
|
38
|
+
* own page (`status-page.ts`: 503/410, `Retry-After`, `no-store`, its own CSP), restyled by the
|
|
39
|
+
* theme's optional `statusPage` hook. Machine paths get a short plain answer for an invalid
|
|
40
|
+
* preview token or an unreadable store (`status-entry.ts`). A store that cannot be read before
|
|
41
|
+
* rendering is `unavailable` (503, `Retry-After: 300`); a thrown error or Astro's own 500 page
|
|
42
|
+
* becomes the `error` page (500) with the `cf-ray` as its reference, in the site's language
|
|
43
|
+
* once the request has read the site; any 5xx the theme or renderer answers on purpose passes.
|
|
44
|
+
* `GET /__cloudhal/health` answers `{ ok, state }` for monitors.
|
|
35
45
|
*/
|
|
36
46
|
|
|
37
47
|
import { cache } from "cloudflare:workers";
|
|
38
48
|
import config from "virtual:cloudhal/config";
|
|
49
|
+
import { loadTheme } from "virtual:cloudhal/theme-hooks";
|
|
39
50
|
import { handle } from "@astrojs/cloudflare/handler";
|
|
40
51
|
import { WaeWriter } from "@cloudhal/analytics/wae";
|
|
41
52
|
import {
|
|
42
53
|
analyticsBackend,
|
|
43
54
|
CACHE_TAG_PREFIX,
|
|
55
|
+
loadedSite,
|
|
44
56
|
resetSource,
|
|
45
57
|
serveMedia,
|
|
58
|
+
siteState,
|
|
46
59
|
withRequestSite,
|
|
47
60
|
} from "@cloudhal/astro-renderer/runtime";
|
|
48
61
|
import { SqlCommentStore } from "@cloudhal/comments/sql";
|
|
@@ -62,7 +75,19 @@ import { EMBED_HEADER, withEmbedFrames } from "./embed-client.js";
|
|
|
62
75
|
import { hostRedirect, } from "./host-redirect.js";
|
|
63
76
|
import { PREVIEW_PARAM } from "./resolver.js";
|
|
64
77
|
import { SearchLimiter, searchCost } from "./search-limit.js";
|
|
65
|
-
import {
|
|
78
|
+
import { SiteDispatch, unknownHost, withNoindex } from "./site-dispatch.js";
|
|
79
|
+
import {
|
|
80
|
+
errorPage,
|
|
81
|
+
HEALTH_PATH,
|
|
82
|
+
health,
|
|
83
|
+
hostLookupFailed,
|
|
84
|
+
|
|
85
|
+
statusStep,
|
|
86
|
+
withErrorPage,
|
|
87
|
+
} from "./status-entry.js";
|
|
88
|
+
import { renderStatus, themeStatusHook } from "./status-hook.js";
|
|
89
|
+
|
|
90
|
+
import { statusInput } from "./status-serve.js";
|
|
66
91
|
|
|
67
92
|
|
|
68
93
|
|
|
@@ -142,6 +167,8 @@ const pendingWrites = new PendingWrites();
|
|
|
142
167
|
const analyticsBySite = new Map ();
|
|
143
168
|
const commentsBySite = new Map ();
|
|
144
169
|
let dispatch ;
|
|
170
|
+
/** RFC-0043 §1.7: the theme's `statusPage` hook, loaded the first time a status page is drawn. */
|
|
171
|
+
const statusHook = themeStatusHook(loadTheme, (m, e) => console.error(m, e));
|
|
145
172
|
|
|
146
173
|
/** One per isolate: which site a request is for (RFC-0034 §4). */
|
|
147
174
|
function siteDispatch(env , siteIdFromBuild ) {
|
|
@@ -504,8 +531,12 @@ async function serveSite(
|
|
|
504
531
|
const media = siteId ? await serveMedia(request, { siteId }) : undefined;
|
|
505
532
|
const rendered =
|
|
506
533
|
media ??
|
|
507
|
-
|
|
508
|
-
|
|
534
|
+
withErrorPage(
|
|
535
|
+
withSecurityHeaders(
|
|
536
|
+
cacheOnlySuccess((await handle(request, env , ctx)) ),
|
|
537
|
+
),
|
|
538
|
+
request,
|
|
539
|
+
{ site: loadedSite() },
|
|
509
540
|
);
|
|
510
541
|
// Server-side render events (§5.4), off the response path: published HTML 200s (cache
|
|
511
542
|
// misses), redirects, 404, 410, 451 and 5xx; never previews or HEAD.
|
|
@@ -529,6 +560,58 @@ async function serveSite(
|
|
|
529
560
|
: stripped;
|
|
530
561
|
}
|
|
531
562
|
|
|
563
|
+
/**
|
|
564
|
+
* RFC-0043 §2.5: the status page for a request the site cannot serve, drawn with the site data
|
|
565
|
+
* the state allows (§1.6) and the theme's hook; counted as a render event like any 5xx (§1.8).
|
|
566
|
+
*/
|
|
567
|
+
async function statusResponse(
|
|
568
|
+
call ,
|
|
569
|
+
state ,
|
|
570
|
+
siteId ,
|
|
571
|
+
) {
|
|
572
|
+
const { request, env, ctx } = call;
|
|
573
|
+
const url = new URL(request.url);
|
|
574
|
+
const kind = state.kind;
|
|
575
|
+
const sites =
|
|
576
|
+
state.kind === "unavailable"
|
|
577
|
+
? {}
|
|
578
|
+
: {
|
|
579
|
+
live: state.site,
|
|
580
|
+
published: state.kind === "preview-expired" ? state.published : undefined,
|
|
581
|
+
};
|
|
582
|
+
const started = Date.now();
|
|
583
|
+
const response = await renderStatus(
|
|
584
|
+
kind,
|
|
585
|
+
statusInput(kind, sites, { host: url.host, pathname: url.pathname }, started),
|
|
586
|
+
{
|
|
587
|
+
hook: await statusHook(),
|
|
588
|
+
method: request.method,
|
|
589
|
+
csp: SITE_CSP,
|
|
590
|
+
onFailure: (m, e) => console.error(m, e),
|
|
591
|
+
},
|
|
592
|
+
);
|
|
593
|
+
const a = state.kind !== "unavailable" ? await siteAnalytics(env, siteId) : undefined;
|
|
594
|
+
if (a?.available)
|
|
595
|
+
ctx.waitUntil(
|
|
596
|
+
a.render(request, response, Date.now() - started).then(() => pendingWrites.settled()),
|
|
597
|
+
);
|
|
598
|
+
return response;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/** A status page before any site is known (the hostname map failed): English, no site data. */
|
|
602
|
+
async function siteless(request , state ) {
|
|
603
|
+
return renderStatus(
|
|
604
|
+
state.kind,
|
|
605
|
+
{ locale: "en", now: Date.now() },
|
|
606
|
+
{
|
|
607
|
+
hook: await statusHook(),
|
|
608
|
+
method: request.method,
|
|
609
|
+
csp: SITE_CSP,
|
|
610
|
+
onFailure: (m, e) => console.error(m, e),
|
|
611
|
+
},
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
|
|
532
615
|
export function createSiteWorker(options = {}) {
|
|
533
616
|
const searchLimiter = new SearchLimiter();
|
|
534
617
|
async function route(request , env , ctx ) {
|
|
@@ -539,17 +622,43 @@ export function createSiteWorker(options = {}) {
|
|
|
539
622
|
if (moved) return moved;
|
|
540
623
|
// RFC-0034 §4: the hostname picks the site on a multi-site Worker; everything the request
|
|
541
624
|
// runs (Astro's render included) then sees that site.
|
|
542
|
-
|
|
625
|
+
let choice ;
|
|
626
|
+
try {
|
|
627
|
+
choice = await siteDispatch(env, options.siteId).choose(request);
|
|
628
|
+
} catch (error) {
|
|
629
|
+
// A store that cannot be read is `unavailable` (503), as health says, never `error`.
|
|
630
|
+
return hostLookupFailed(request, error, { page: (state) => siteless(request, state) });
|
|
631
|
+
}
|
|
543
632
|
if (choice.kind === "unknown") return unknownHost();
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
const
|
|
633
|
+
// Without `CLOUDHAL_SITE_ID` a single-site Worker serves the site it was built for, as
|
|
634
|
+
// Astro's `getSource` always did; `unavailable` is for a site row that is gone (§2.5).
|
|
635
|
+
const siteId = choice.siteId ?? config.siteId;
|
|
636
|
+
const call = { request, env, ctx };
|
|
637
|
+
const run = async () => {
|
|
638
|
+
try {
|
|
639
|
+
if (url.pathname === HEALTH_PATH) return await health(request, () => siteState(request));
|
|
640
|
+
const status = await statusStep(request, {
|
|
641
|
+
state: () => siteState(request),
|
|
642
|
+
page: (state) => statusResponse(call, state, siteId),
|
|
643
|
+
});
|
|
644
|
+
return status ?? (await serveSite(call, siteId, searchLimiter));
|
|
645
|
+
} catch (error) {
|
|
646
|
+
// RFC-0043 §1.6: inside the request's site, so the page can use what it already read.
|
|
647
|
+
return errorPage(request, error, { site: loadedSite() });
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
const response = await withRequestSite(siteId, run);
|
|
547
651
|
return choice.noindex ? withNoindex(response) : response;
|
|
548
652
|
}
|
|
549
653
|
return {
|
|
550
654
|
async fetch(request , env , ctx ) {
|
|
551
655
|
// #62: the edge cache is shared by every host this Worker answers on, unless told to vary.
|
|
552
|
-
|
|
656
|
+
try {
|
|
657
|
+
return varyByHost(await route(request, env, ctx));
|
|
658
|
+
} catch (error) {
|
|
659
|
+
// RFC-0043 §1.8: whatever failed, the visitor gets a page and a reference to quote.
|
|
660
|
+
return varyByHost(errorPage(request, error));
|
|
661
|
+
}
|
|
553
662
|
},
|
|
554
663
|
};
|
|
555
664
|
}
|
package/dist/src/feeds.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { collectionSettings, entryPath } from "@cloudhal/core-ts";
|
|
8
|
+
import { trimEndChar } from "@cloudhal/core-ts/names";
|
|
8
9
|
|
|
9
10
|
import { markdownText, renderBody } from "./markdown.js";
|
|
10
11
|
|
|
@@ -214,7 +215,7 @@ export function redirectsFile(redirects ) {
|
|
|
214
215
|
.sort((a, b) => b.from.length - a.from.length || a.from.localeCompare(b.from));
|
|
215
216
|
const splat = (to ) => {
|
|
216
217
|
const i = to.search(/[?#]/);
|
|
217
|
-
const head = (i < 0 ? to : to.slice(0, i)
|
|
218
|
+
const head = trimEndChar(i < 0 ? to : to.slice(0, i), "/");
|
|
218
219
|
return `${head}/:splat${i < 0 ? "" : to.slice(i)}`;
|
|
219
220
|
};
|
|
220
221
|
const lines = [
|
package/dist/src/integration.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
1
3
|
|
|
2
4
|
|
|
3
5
|
|
|
@@ -61,6 +63,14 @@ const RESOLVED_CLIENT_ID = `\0${CLIENT_ID}`;
|
|
|
61
63
|
/** Dev only: the built comments module and worker, served from memory by the comments middleware. */
|
|
62
64
|
const COMMENTS_CLIENT_ID = "virtual:cloudhal/comments-client";
|
|
63
65
|
const RESOLVED_COMMENTS_CLIENT_ID = `\0${COMMENTS_CLIENT_ID}`;
|
|
66
|
+
/**
|
|
67
|
+
* RFC-0043 §1.7: a lazy loader of the project's `src/theme.ts` for the status pages' theme hook
|
|
68
|
+
* (root-relative, so Vite resolves it in dev and in the Site Worker build), or of an empty module
|
|
69
|
+
* when the project has no theme module. Evaluated only when a status page is drawn.
|
|
70
|
+
*/
|
|
71
|
+
const THEME_HOOKS_ID = "virtual:cloudhal/theme-hooks";
|
|
72
|
+
const RESOLVED_THEME_HOOKS_ID = `\0${THEME_HOOKS_ID}`;
|
|
73
|
+
const THEME_MODULE = "src/theme.ts";
|
|
64
74
|
|
|
65
75
|
/** The virtual modules: the project config, and in dev the built analytics and comments clients. */
|
|
66
76
|
function configPlugin(modules ) {
|
|
@@ -68,27 +78,46 @@ function configPlugin(modules
|
|
|
68
78
|
[VIRTUAL_ID]: RESOLVED_ID,
|
|
69
79
|
[CLIENT_ID]: RESOLVED_CLIENT_ID,
|
|
70
80
|
[COMMENTS_CLIENT_ID]: RESOLVED_COMMENTS_CLIENT_ID,
|
|
81
|
+
[THEME_HOOKS_ID]: RESOLVED_THEME_HOOKS_ID,
|
|
71
82
|
};
|
|
72
83
|
const bodies = {
|
|
73
84
|
[RESOLVED_ID]: modules.config,
|
|
74
85
|
[RESOLVED_CLIENT_ID]: modules.client,
|
|
75
86
|
[RESOLVED_COMMENTS_CLIENT_ID]: modules.comments,
|
|
76
87
|
};
|
|
88
|
+
let root = process.cwd();
|
|
77
89
|
return {
|
|
78
90
|
name: "cloudhal-config",
|
|
91
|
+
configResolved: (config ) => {
|
|
92
|
+
root = config.root;
|
|
93
|
+
},
|
|
79
94
|
resolveId: (id ) => (Object.hasOwn(ids, id) ? ids[id] : undefined),
|
|
80
|
-
load: (id ) =>
|
|
81
|
-
|
|
95
|
+
load: (id ) => {
|
|
96
|
+
if (id === RESOLVED_THEME_HOOKS_ID)
|
|
97
|
+
return existsSync(path.join(root, THEME_MODULE))
|
|
98
|
+
? `export const loadTheme = () => import("/${THEME_MODULE}");`
|
|
99
|
+
: "export const loadTheme = () => Promise.resolve({});";
|
|
100
|
+
return Object.hasOwn(bodies, id)
|
|
101
|
+
? `export default ${JSON.stringify(bodies[id])};`
|
|
102
|
+
: undefined;
|
|
103
|
+
},
|
|
82
104
|
};
|
|
83
105
|
}
|
|
84
106
|
|
|
85
107
|
/**
|
|
86
108
|
* What only `cloudhal dev` runs as middleware (the Site Worker's entry and builds do these jobs):
|
|
87
109
|
* the analytics beacon (RFC-0006 §5.1); the click-to-load video player (RFC-0015 §4) and the
|
|
88
|
-
* interaction runtime (RFC-0020 §4), which builds write as files;
|
|
89
|
-
* report-only, so the console shows what a deploy would block
|
|
110
|
+
* interaction runtime (RFC-0020 §4), which builds write as files; the Site Worker's CSP sent
|
|
111
|
+
* report-only, so the console shows what a deploy would block; and the status pages' preview
|
|
112
|
+
* route (RFC-0043 §1.8).
|
|
90
113
|
*/
|
|
91
|
-
const DEV_MIDDLEWARE = [
|
|
114
|
+
const DEV_MIDDLEWARE = [
|
|
115
|
+
"analytics-middleware",
|
|
116
|
+
"embed-middleware",
|
|
117
|
+
"csp-middleware",
|
|
118
|
+
// RFC-0043 §1.8: the status pages drawn on demand at `/__cloudhal/status/<state>`.
|
|
119
|
+
"status-middleware",
|
|
120
|
+
].map((m) => ({
|
|
92
121
|
entrypoint: `@cloudhal/astro-renderer/${m}`,
|
|
93
122
|
order: "pre" ,
|
|
94
123
|
}));
|
package/dist/src/locales.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { entryPath, termSlug } from "@cloudhal/core-ts";
|
|
11
11
|
import { localeRouting, localeUrl, localizedPath } from "@cloudhal/core-ts/locale-paths";
|
|
12
|
+
import { trimEndChar } from "@cloudhal/core-ts/names";
|
|
12
13
|
import { routableEntries } from "./entry-views.js";
|
|
13
14
|
|
|
14
15
|
|
|
@@ -247,7 +248,7 @@ export function localizeLinks(html , localize )
|
|
|
247
248
|
const cut = url.search(/[?#]/);
|
|
248
249
|
const path = cut < 0 ? url : url.slice(0, cut);
|
|
249
250
|
const suffix = cut < 0 ? "" : url.slice(cut);
|
|
250
|
-
return `${head}${localize(path.length > 1 ? path
|
|
251
|
+
return `${head}${localize(path.length > 1 ? trimEndChar(path, "/") : path)}${suffix}${tail}`;
|
|
251
252
|
};
|
|
252
253
|
return html
|
|
253
254
|
.replace(/(<(?:a|form)\b[^>]*?\s(?:href|action)=")(\/[^"]*)(")/gi, swap)
|
package/dist/src/markdown.js
CHANGED
|
@@ -3,6 +3,7 @@ import { createBlockRegistry } from "@cloudhal/core-ts/block
|
|
|
3
3
|
import { directiveExtension } from "@cloudhal/core-ts/body-blocks";
|
|
4
4
|
import { checkDirective, directiveProps } from "@cloudhal/core-ts/directives";
|
|
5
5
|
import { ASSET_HREF, assetUrl, mediaPath } from "@cloudhal/core-ts/media";
|
|
6
|
+
import { trimChar } from "@cloudhal/core-ts/names";
|
|
6
7
|
import { parseSurfaceFence } from "@cloudhal/core-ts/surfaces";
|
|
7
8
|
import { Marked } from "marked";
|
|
8
9
|
import { builtInBlockHtml, isBuiltInBlock, missingBlockHtml } from "./block-html.js";
|
|
@@ -48,17 +49,14 @@ const RESERVED_ID = /^(?:comment|cloudhal|ch-)/;
|
|
|
48
49
|
* Latin letters or digits falls back to `section`; repeats get `-2`, `-3`, ….
|
|
49
50
|
*/
|
|
50
51
|
export function headingSlug(text , taken ) {
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
.replace(/^-+|-+$/g, "")
|
|
60
|
-
.slice(0, 64)
|
|
61
|
-
.replace(/-+$/, "") || "section";
|
|
52
|
+
const dashed = text
|
|
53
|
+
.replace(/[ıİşŞğĞ]/g, (c) => TURKISH[c] ?? c)
|
|
54
|
+
.normalize("NFKD")
|
|
55
|
+
.replace(/[̀-ͯ]/g, "")
|
|
56
|
+
.toLowerCase()
|
|
57
|
+
.replace(/ß/g, "ss")
|
|
58
|
+
.replace(/[^a-z0-9]+/g, "-");
|
|
59
|
+
const base = trimChar(trimChar(dashed, "-").slice(0, 64), "-") || "section";
|
|
62
60
|
const slug = RESERVED_ID.test(base) || /^\d/.test(base) ? `h-${base}` : base;
|
|
63
61
|
if (!taken) return slug;
|
|
64
62
|
const seen = taken.get(slug) ?? 0;
|
package/dist/src/resolver.d.ts
CHANGED
|
@@ -66,6 +66,13 @@ export declare class SiteResolver {
|
|
|
66
66
|
/** Forget the cached pointer (e.g. after a publish handled in the same isolate). */
|
|
67
67
|
invalidate(): void;
|
|
68
68
|
site(): Promise<Site>;
|
|
69
|
+
/**
|
|
70
|
+
* RFC-0043 §1.6: the site node a public page may name for `resolution`, never a new store
|
|
71
|
+
* read: the published snapshot's (already loaded, or loaded for the render anyway) for a
|
|
72
|
+
* published site, so an unpublished rename or locale never shows; the live row `resolve` just
|
|
73
|
+
* read before the first publish; nothing for a preview. `undefined` when it cannot be read.
|
|
74
|
+
*/
|
|
75
|
+
publicSite(resolution: Resolution): Promise<Site | undefined>;
|
|
69
76
|
snapshot(id: string): Promise<SnapshotSource>;
|
|
70
77
|
resolve(request: Request): Promise<Resolution>;
|
|
71
78
|
}
|
package/dist/src/resolver.js
CHANGED
|
@@ -115,6 +115,18 @@ export class SiteResolver {
|
|
|
115
115
|
return this.#pointer.site;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
/**
|
|
119
|
+
* RFC-0043 §1.6: the site node a public page may name for `resolution`, never a new store
|
|
120
|
+
* read: the published snapshot's (already loaded, or loaded for the render anyway) for a
|
|
121
|
+
* published site, so an unpublished rename or locale never shows; the live row `resolve` just
|
|
122
|
+
* read before the first publish; nothing for a preview. `undefined` when it cannot be read.
|
|
123
|
+
*/
|
|
124
|
+
async publicSite(resolution ) {
|
|
125
|
+
if (resolution.kind === "preview") return undefined;
|
|
126
|
+
if (resolution.kind === "unpublished") return this.#pointer?.site;
|
|
127
|
+
return resolution.source.site().catch(() => undefined);
|
|
128
|
+
}
|
|
129
|
+
|
|
118
130
|
snapshot(id ) {
|
|
119
131
|
let entry = this.#snapshots.get(id);
|
|
120
132
|
if (entry) {
|
|
@@ -36,10 +36,42 @@ export interface AstroLike {
|
|
|
36
36
|
}
|
|
37
37
|
/** Runs `fn` (and everything it awaits) as a request for `siteId`. */
|
|
38
38
|
export declare function withRequestSite<T>(siteId: string, fn: () => T): T;
|
|
39
|
+
/**
|
|
40
|
+
* RFC-0043 §1.6: the site node this request already read (its name and locales), for the
|
|
41
|
+
* `error` page; `undefined` before a successful read. Never reads the store: it may be what
|
|
42
|
+
* failed. Only inside `withRequestSite`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function loadedSite(): Site | undefined;
|
|
39
45
|
/** Runs `fn` (and everything it awaits) as a request for `siteId` over `store`. */
|
|
40
46
|
export declare function withRequestStore<T>(store: SiteGraphStore, siteId: string, fn: () => T): T;
|
|
41
47
|
/** Test/purge hook: forget cached state in this isolate, for one site or all of them. */
|
|
42
48
|
export declare function resetSource(siteId?: string): void;
|
|
49
|
+
/** RFC-0043 §2.5: what the entry answers for this request, decided before Astro runs. */
|
|
50
|
+
export type SiteStateResult = {
|
|
51
|
+
readonly kind: "serve";
|
|
52
|
+
}
|
|
53
|
+
/** The site row is missing (deleted, RFC-0034). */
|
|
54
|
+
| {
|
|
55
|
+
readonly kind: "unavailable";
|
|
56
|
+
}
|
|
57
|
+
/** No published snapshot and no valid preview: `site` is the live node (its name, locales). */
|
|
58
|
+
| {
|
|
59
|
+
readonly kind: "not-published";
|
|
60
|
+
readonly site: Site;
|
|
61
|
+
}
|
|
62
|
+
/** A preview token in the URL that does not verify; `published` is the published snapshot's site node, if any. */
|
|
63
|
+
| {
|
|
64
|
+
readonly kind: "preview-expired";
|
|
65
|
+
readonly site: Site;
|
|
66
|
+
readonly published?: Site | undefined;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* The state of the request's site (RFC-0043 §2.5): `serve` for a published site or a valid
|
|
70
|
+
* preview, `not-published` before the first publish, `preview-expired` for a token in the URL
|
|
71
|
+
* that does not verify (§1.2: it no longer falls back to the published page). Shares the
|
|
72
|
+
* request's resolution with `getSource`.
|
|
73
|
+
*/
|
|
74
|
+
export declare function siteState(request: Request, siteId?: string): Promise<SiteStateResult>;
|
|
43
75
|
/**
|
|
44
76
|
* What site analytics read on the Site Worker (`SiteAnalytics`, RFC-0006 §5.2): D1 for settings and
|
|
45
77
|
* the daily salt, the site row (published pointer, domains) and the isolate's snapshot cache.
|
|
@@ -23,8 +23,9 @@ import { usableCommentsKey } from "./comments.js";
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
import { matchMediaRoute, serveMediaRequest } from "./media-route.js";
|
|
26
|
-
import { PREVIEW_PARAM,
|
|
26
|
+
import { PREVIEW_PARAM, SiteResolver } from "./resolver.js";
|
|
27
27
|
|
|
28
|
+
import { markInvalidPreview } from "./status-serve.js";
|
|
28
29
|
|
|
29
30
|
/** RFC-0029 §3: `AI_SEARCH`, `SEARCH_BUCKET` and `CLOUDHAL_SEARCH_*` turn on semantic search. */
|
|
30
31
|
|
|
@@ -49,11 +50,20 @@ import { PREVIEW_PARAM, SiteResolver } from "./resolver.js";
|
|
|
49
50
|
* Worker) around everything the request runs, Astro's render included. Module state, not a header:
|
|
50
51
|
* a client cannot choose it.
|
|
51
52
|
*/
|
|
52
|
-
const requestSite = new AsyncLocalStorage
|
|
53
|
+
const requestSite = new AsyncLocalStorage ();
|
|
53
54
|
|
|
54
55
|
/** Runs `fn` (and everything it awaits) as a request for `siteId`. */
|
|
55
56
|
export function withRequestSite (siteId , fn ) {
|
|
56
|
-
return requestSite.run(siteId, fn);
|
|
57
|
+
return requestSite.run({ siteId }, fn);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* RFC-0043 §1.6: the site node this request already read (its name and locales), for the
|
|
62
|
+
* `error` page; `undefined` before a successful read. Never reads the store: it may be what
|
|
63
|
+
* failed. Only inside `withRequestSite`.
|
|
64
|
+
*/
|
|
65
|
+
export function loadedSite() {
|
|
66
|
+
return requestSite.getStore()?.site;
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
/**
|
|
@@ -73,7 +83,7 @@ export function withRequestStore (store , siteId , fn
|
|
|
73
83
|
function siteOf(fallback ) {
|
|
74
84
|
return (
|
|
75
85
|
requestStore.getStore()?.siteId ??
|
|
76
|
-
requestSite.getStore() ??
|
|
86
|
+
requestSite.getStore()?.siteId ??
|
|
77
87
|
(env ).CLOUDHAL_SITE_ID ??
|
|
78
88
|
fallback
|
|
79
89
|
);
|
|
@@ -143,6 +153,79 @@ export function resetSource(siteId ) {
|
|
|
143
153
|
for (const [id, r] of resolvers) if (!siteId || id === siteId) r.invalidate();
|
|
144
154
|
}
|
|
145
155
|
|
|
156
|
+
/**
|
|
157
|
+
* RFC-0043 §1.8: one resolution per request. The entry decides the site's state before Astro
|
|
158
|
+
* runs and Astro's `getSource` resolves the same request, so the verdict (pointer read, preview
|
|
159
|
+
* token) is memoised on the request object; a request Astro re-created simply resolves again.
|
|
160
|
+
*/
|
|
161
|
+
const resolutions = new WeakMap ();
|
|
162
|
+
|
|
163
|
+
function resolveRequest(r , request ) {
|
|
164
|
+
let pending = resolutions.get(request);
|
|
165
|
+
if (!pending) {
|
|
166
|
+
pending = r.resolve(request);
|
|
167
|
+
resolutions.set(request, pending);
|
|
168
|
+
const context = requestSite.getStore();
|
|
169
|
+
// RFC-0043 §1.6: the `error` page's site, filled in the background, never awaited here (a
|
|
170
|
+
// published site's node comes with the snapshot load, which a render awaits anyway, so it is
|
|
171
|
+
// known by the time one fails). Health renders no page and reads only the pointer.
|
|
172
|
+
if (context && new URL(request.url).pathname !== "/__cloudhal/health")
|
|
173
|
+
pending
|
|
174
|
+
.then((resolution) => r.publicSite(resolution))
|
|
175
|
+
.then(
|
|
176
|
+
(site) => {
|
|
177
|
+
if (site?.id === context.siteId) context.site = site;
|
|
178
|
+
},
|
|
179
|
+
() => {},
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
return pending;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** RFC-0043 §2.5: what the entry answers for this request, decided before Astro runs. */
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The state of the request's site (RFC-0043 §2.5): `serve` for a published site or a valid
|
|
201
|
+
* preview, `not-published` before the first publish, `preview-expired` for a token in the URL
|
|
202
|
+
* that does not verify (§1.2: it no longer falls back to the published page). Shares the
|
|
203
|
+
* request's resolution with `getSource`.
|
|
204
|
+
*/
|
|
205
|
+
export async function siteState(request , siteId ) {
|
|
206
|
+
const id = siteOf(siteId);
|
|
207
|
+
if (!id) return { kind: "unavailable" };
|
|
208
|
+
const r = getResolver(id);
|
|
209
|
+
let resolution ;
|
|
210
|
+
try {
|
|
211
|
+
resolution = await resolveRequest(r, request);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
if (error instanceof Error && /^site .* not found$/.test(error.message))
|
|
214
|
+
return { kind: "unavailable" };
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
if (resolution.kind === "preview") return { kind: "serve" };
|
|
218
|
+
const fromUrl = new URL(request.url).searchParams.get(PREVIEW_PARAM);
|
|
219
|
+
if (resolution.previewError && fromUrl && fromUrl !== "exit")
|
|
220
|
+
return {
|
|
221
|
+
kind: "preview-expired",
|
|
222
|
+
site: await r.site(),
|
|
223
|
+
published: resolution.kind === "published" ? await resolution.source.site() : undefined,
|
|
224
|
+
};
|
|
225
|
+
if (resolution.kind === "unpublished") return { kind: "not-published", site: await r.site() };
|
|
226
|
+
return { kind: "serve" };
|
|
227
|
+
}
|
|
228
|
+
|
|
146
229
|
/**
|
|
147
230
|
* What site analytics read on the Site Worker (`SiteAnalytics`, RFC-0006 §5.2): D1 for settings and
|
|
148
231
|
* the daily salt, the site row (published pointer, domains) and the isolate's snapshot cache.
|
|
@@ -237,7 +320,7 @@ export async function getSource(
|
|
|
237
320
|
if (!astro) throw new Error("on Cloudflare, pass the Astro global: getSource(config, Astro)");
|
|
238
321
|
const siteId = siteOf(config.siteId) ;
|
|
239
322
|
const r = getResolver(siteId);
|
|
240
|
-
const resolution = await r
|
|
323
|
+
const resolution = await resolveRequest(r, astro.request);
|
|
241
324
|
const h = astro.response.headers;
|
|
242
325
|
if (resolution.kind === "preview") {
|
|
243
326
|
h.set("cache-control", "private, no-store");
|
|
@@ -275,11 +358,7 @@ export async function getSource(
|
|
|
275
358
|
h.set("cache-tag", siteCacheTag(siteId));
|
|
276
359
|
h.set("x-cloudhal-mode", "published");
|
|
277
360
|
h.set("x-cloudhal-snapshot", resolution.snapshotId);
|
|
278
|
-
if (resolution.previewError)
|
|
279
|
-
// An invalid preview link must not poison the cache entry for this URL.
|
|
280
|
-
h.set("cloudflare-cdn-cache-control", "no-store");
|
|
281
|
-
h.set("x-cloudhal-preview-error", resolution.previewError);
|
|
282
|
-
}
|
|
361
|
+
if (resolution.previewError) markInvalidPreview(h);
|
|
283
362
|
return resolution.source;
|
|
284
363
|
}
|
|
285
364
|
|
|
@@ -296,7 +375,7 @@ export async function serveMedia(
|
|
|
296
375
|
const siteId = siteOf(options.siteId);
|
|
297
376
|
if (!siteId) return undefined;
|
|
298
377
|
const r = getResolver(siteId);
|
|
299
|
-
const resolution = await r
|
|
378
|
+
const resolution = await resolveRequest(r, request);
|
|
300
379
|
if (resolution.kind === "unpublished")
|
|
301
380
|
return new Response("not found", {
|
|
302
381
|
status: 404,
|
|
@@ -314,10 +393,7 @@ export async function serveMedia(
|
|
|
314
393
|
blobs: store.blobs,
|
|
315
394
|
...(preview && token ? { keepQuery: `${PREVIEW_PARAM}=${encodeURIComponent(token)}` } : {}),
|
|
316
395
|
});
|
|
317
|
-
if (response && resolution.kind === "published" && resolution.previewError)
|
|
318
|
-
|
|
319
|
-
response.headers.set("cloudflare-cdn-cache-control", "no-store");
|
|
320
|
-
response.headers.set("x-cloudhal-preview-error", resolution.previewError);
|
|
321
|
-
}
|
|
396
|
+
if (response && resolution.kind === "published" && resolution.previewError)
|
|
397
|
+
markInvalidPreview(response.headers);
|
|
322
398
|
return response;
|
|
323
399
|
}
|
package/dist/src/runtime.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export declare function resetSource(_siteId?: string): void;
|
|
|
25
25
|
* serves the project's one site, so this only runs `fn`.
|
|
26
26
|
*/
|
|
27
27
|
export declare function withRequestSite<T>(_siteId: string, fn: () => T): T;
|
|
28
|
+
/** RFC-0043 §1.6: the `workerd` build's per-request site; `cloudhal dev` keeps none. */
|
|
29
|
+
export declare function loadedSite(): Site | undefined;
|
|
28
30
|
/**
|
|
29
31
|
* RFC-0036 §4: on workerd a playground runs each request over its session's store; `cloudhal dev`
|
|
30
32
|
* renders from the project's local store, so this only runs `fn`.
|
|
@@ -61,5 +63,23 @@ export declare function analyticsBackend(siteId: string, config?: Partial<Cloudh
|
|
|
61
63
|
export declare function commentsBackend(config: Partial<CloudhalRendererConfig>): Promise<{
|
|
62
64
|
store: SqlCommentStore;
|
|
63
65
|
} | undefined>;
|
|
66
|
+
/** RFC-0043 §2.5: the `workerd` build's verdict shape, for code importing either build. */
|
|
67
|
+
export type SiteStateResult = {
|
|
68
|
+
readonly kind: "serve";
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: "unavailable";
|
|
71
|
+
} | {
|
|
72
|
+
readonly kind: "not-published";
|
|
73
|
+
readonly site: Site;
|
|
74
|
+
} | {
|
|
75
|
+
readonly kind: "preview-expired";
|
|
76
|
+
readonly site: Site;
|
|
77
|
+
readonly published?: Site | undefined;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* RFC-0043 §1.8: `cloudhal dev` renders the working copy, drafts included, so a request on Node
|
|
81
|
+
* is always served; the status pages are drawn on demand at `/__cloudhal/status/<state>`.
|
|
82
|
+
*/
|
|
83
|
+
export declare function siteState(_request: Request, _siteId?: string): Promise<SiteStateResult>;
|
|
64
84
|
/** Shared with the `workerd` runtime so code importing either build sees the same names. */
|
|
65
85
|
export { CACHE_TAG_PREFIX } from "./cache-tags.js";
|