@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/runtime.js
CHANGED
|
@@ -58,6 +58,11 @@ export function withRequestSite (_siteId , fn ) {
|
|
|
58
58
|
return fn();
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/** RFC-0043 §1.6: the `workerd` build's per-request site; `cloudhal dev` keeps none. */
|
|
62
|
+
export function loadedSite() {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
61
66
|
/**
|
|
62
67
|
* RFC-0036 §4: on workerd a playground runs each request over its session's store; `cloudhal dev`
|
|
63
68
|
* renders from the project's local store, so this only runs `fn`.
|
|
@@ -158,5 +163,24 @@ export async function commentsBackend(
|
|
|
158
163
|
return { store: new SqlCommentStore((await localStore(config.dataDir)).sql) };
|
|
159
164
|
}
|
|
160
165
|
|
|
166
|
+
/** RFC-0043 §2.5: the `workerd` build's verdict shape, for code importing either build. */
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* RFC-0043 §1.8: `cloudhal dev` renders the working copy, drafts included, so a request on Node
|
|
179
|
+
* is always served; the status pages are drawn on demand at `/__cloudhal/status/<state>`.
|
|
180
|
+
*/
|
|
181
|
+
export async function siteState(_request , _siteId ) {
|
|
182
|
+
return { kind: "serve" };
|
|
183
|
+
}
|
|
184
|
+
|
|
161
185
|
/** Shared with the `workerd` runtime so code importing either build sees the same names. */
|
|
162
186
|
export { CACHE_TAG_PREFIX } from "./cache-tags.js";
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Site Worker entry's status step, `error` page, health endpoint and the answer when the
|
|
3
|
+
* hostname cannot be mapped to a site (RFC-0043 §1.2, §1.8, §1.9, §2.5), apart from
|
|
4
|
+
* `cloudflare-entry.ts` so unit tests run them: no bindings, no Astro, no `cloudflare:` imports.
|
|
5
|
+
* Worker-safe.
|
|
6
|
+
*/
|
|
7
|
+
import type { Site } from "@cloudhal/core-contracts";
|
|
8
|
+
import type { SiteStateResult } from "./runtime-workerd.js";
|
|
9
|
+
/** A state the entry answers itself. */
|
|
10
|
+
export type StatusState = Exclude<SiteStateResult, {
|
|
11
|
+
kind: "serve";
|
|
12
|
+
}>;
|
|
13
|
+
type Log = (message: string, error?: unknown) => void;
|
|
14
|
+
/** §1.3: the `error` page's reference — Cloudflare's ray id, else a random one. */
|
|
15
|
+
export declare function referenceId(request: Request): string;
|
|
16
|
+
export interface ErrorPageOptions {
|
|
17
|
+
/** The site node this request already read (`loadedSite()`); never read here (§1.6). */
|
|
18
|
+
readonly site?: Site | undefined;
|
|
19
|
+
readonly log?: Log | undefined;
|
|
20
|
+
readonly now?: number | undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The `error` page (§1.2): never the theme's hook, nothing read from the store. In the site's
|
|
24
|
+
* locale and with its name when this request already read the site (§1.4, §1.6), else English.
|
|
25
|
+
*/
|
|
26
|
+
export declare function errorPage(request: Request, error: unknown, options?: ErrorPageOptions): Response;
|
|
27
|
+
/**
|
|
28
|
+
* §1.8: Astro's own 500 (a render that threw) becomes the `error` page — HTML, or Astro's empty
|
|
29
|
+
* fallback (`new Response(null, { status: 500 })` when no `500.astro` renders: the starter's
|
|
30
|
+
* catch-all route matches `/500` and throws again). What the renderer or the theme answered on
|
|
31
|
+
* purpose passes unchanged and logs nothing: a status page (`x-cloudhal-state`, e.g. the
|
|
32
|
+
* starter's `not-published` 503 for a request that reaches Astro), a theme's `500.astro`
|
|
33
|
+
* (`x-cloudhal-error-page: theme`), a 500 with an explicit non-HTML type, and any other 5xx.
|
|
34
|
+
*/
|
|
35
|
+
export declare function withErrorPage(response: Response, request: Request, options?: ErrorPageOptions): Response;
|
|
36
|
+
export interface StatusStepOptions {
|
|
37
|
+
/** The request's verdict (the runtime's `siteState`); a throw is a store read failure. */
|
|
38
|
+
readonly state: () => Promise<SiteStateResult>;
|
|
39
|
+
/** Draws the status page for a page path (theme hook, render event). */
|
|
40
|
+
readonly page: (state: StatusState) => Promise<Response>;
|
|
41
|
+
readonly log?: Log | undefined;
|
|
42
|
+
readonly now?: (() => number) | undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* §2.5 step 6, for `GET`/`HEAD`: the status answer for this request, or `undefined` to serve the
|
|
46
|
+
* site. Page paths get the page of every state. Machine paths (§2.3) get a short plain or JSON
|
|
47
|
+
* answer for `preview-expired` (decision 12: never the published content) and `unavailable`;
|
|
48
|
+
* before the first publish they keep the theme's own `statusPlain`. A store that cannot be read
|
|
49
|
+
* is `unavailable` (503, `Retry-After: 300`), as `/__cloudhal/health` reports it.
|
|
50
|
+
*/
|
|
51
|
+
export declare function statusStep(request: Request, options: StatusStepOptions): Promise<Response | undefined>;
|
|
52
|
+
export declare const HEALTH_PATH = "/__cloudhal/health";
|
|
53
|
+
/**
|
|
54
|
+
* §1.9: `{ ok, state }`, the admin's `/api/health` shape plus the site's state, so one monitor
|
|
55
|
+
* rule reads both. Public, never cached, never indexed; reads only the memoised pointer.
|
|
56
|
+
*/
|
|
57
|
+
export declare function health(request: Request, state: () => Promise<SiteStateResult>, log?: Log): Promise<Response>;
|
|
58
|
+
/**
|
|
59
|
+
* RFC-0034 §4 with §1.2: on a multi-site Worker the hostname map could not be read (the store
|
|
60
|
+
* failed past the map's TTL), so no site is known. Treated as a store failure, never as `error`:
|
|
61
|
+
* health answers `503 { ok: false }`; `GET`/`HEAD` page paths the `unavailable` page and machine
|
|
62
|
+
* paths its plain answer (503, `Retry-After: 300`, English, no site name); anything else a
|
|
63
|
+
* plain 503.
|
|
64
|
+
*/
|
|
65
|
+
export declare function hostLookupFailed(request: Request, error: unknown, options: Pick<StatusStepOptions, "page" | "log" | "now">): Promise<Response>;
|
|
66
|
+
export {};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Site Worker entry's status step, `error` page, health endpoint and the answer when the
|
|
3
|
+
* hostname cannot be mapped to a site (RFC-0043 §1.2, §1.8, §1.9, §2.5), apart from
|
|
4
|
+
* `cloudflare-entry.ts` so unit tests run them: no bindings, no Astro, no `cloudflare:` imports.
|
|
5
|
+
* Worker-safe.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
import { statusPage, statusPlain } from "./status-page.js";
|
|
10
|
+
import { statusInput, statusPathKind } from "./status-serve.js";
|
|
11
|
+
|
|
12
|
+
/** A state the entry answers itself. */
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
/** §1.3: the `error` page's reference — Cloudflare's ray id, else a random one. */
|
|
18
|
+
export function referenceId(request ) {
|
|
19
|
+
return (
|
|
20
|
+
request.headers.get("cf-ray") ??
|
|
21
|
+
Array.from(crypto.getRandomValues(new Uint8Array(8)), (b) =>
|
|
22
|
+
b.toString(16).padStart(2, "0"),
|
|
23
|
+
).join("")
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The `error` page (§1.2): never the theme's hook, nothing read from the store. In the site's
|
|
36
|
+
* locale and with its name when this request already read the site (§1.4, §1.6), else English.
|
|
37
|
+
*/
|
|
38
|
+
export function errorPage(
|
|
39
|
+
request ,
|
|
40
|
+
error ,
|
|
41
|
+
options = {},
|
|
42
|
+
) {
|
|
43
|
+
const rayId = referenceId(request);
|
|
44
|
+
(options.log ?? console.error)(`cloudhal: request failed (reference ${rayId})`, error);
|
|
45
|
+
const now = options.now ?? Date.now();
|
|
46
|
+
let input = { locale: "en", now };
|
|
47
|
+
try {
|
|
48
|
+
const url = new URL(request.url);
|
|
49
|
+
input = statusInput(
|
|
50
|
+
"error",
|
|
51
|
+
{ live: options.site },
|
|
52
|
+
{ host: url.host, pathname: url.pathname },
|
|
53
|
+
now,
|
|
54
|
+
);
|
|
55
|
+
} catch {
|
|
56
|
+
// a malformed site node: the English page without a name
|
|
57
|
+
}
|
|
58
|
+
return statusPage("error", { ...input, rayId }, { method: request.method });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* §1.8: Astro's own 500 (a render that threw) becomes the `error` page — HTML, or Astro's empty
|
|
63
|
+
* fallback (`new Response(null, { status: 500 })` when no `500.astro` renders: the starter's
|
|
64
|
+
* catch-all route matches `/500` and throws again). What the renderer or the theme answered on
|
|
65
|
+
* purpose passes unchanged and logs nothing: a status page (`x-cloudhal-state`, e.g. the
|
|
66
|
+
* starter's `not-published` 503 for a request that reaches Astro), a theme's `500.astro`
|
|
67
|
+
* (`x-cloudhal-error-page: theme`), a 500 with an explicit non-HTML type, and any other 5xx.
|
|
68
|
+
*/
|
|
69
|
+
export function withErrorPage(
|
|
70
|
+
response ,
|
|
71
|
+
request ,
|
|
72
|
+
options = {},
|
|
73
|
+
) {
|
|
74
|
+
const type = response.headers.get("content-type") ?? "";
|
|
75
|
+
if (
|
|
76
|
+
response.status !== 500 ||
|
|
77
|
+
(type !== "" && !type.includes("text/html")) ||
|
|
78
|
+
response.headers.has("x-cloudhal-state") ||
|
|
79
|
+
response.headers.get("x-cloudhal-error-page") === "theme"
|
|
80
|
+
)
|
|
81
|
+
return response;
|
|
82
|
+
return errorPage(request, new Error("Astro answered 500"), options);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* §2.5 step 6, for `GET`/`HEAD`: the status answer for this request, or `undefined` to serve the
|
|
96
|
+
* site. Page paths get the page of every state. Machine paths (§2.3) get a short plain or JSON
|
|
97
|
+
* answer for `preview-expired` (decision 12: never the published content) and `unavailable`;
|
|
98
|
+
* before the first publish they keep the theme's own `statusPlain`. A store that cannot be read
|
|
99
|
+
* is `unavailable` (503, `Retry-After: 300`), as `/__cloudhal/health` reports it.
|
|
100
|
+
*/
|
|
101
|
+
export async function statusStep(
|
|
102
|
+
request ,
|
|
103
|
+
options ,
|
|
104
|
+
) {
|
|
105
|
+
if (request.method !== "GET" && request.method !== "HEAD") return undefined;
|
|
106
|
+
const url = new URL(request.url);
|
|
107
|
+
const kind = statusPathKind(url.pathname);
|
|
108
|
+
if (!kind) return undefined;
|
|
109
|
+
let state ;
|
|
110
|
+
try {
|
|
111
|
+
state = await options.state();
|
|
112
|
+
} catch (error) {
|
|
113
|
+
(options.log ?? console.error)("cloudhal: the store could not be read before rendering", error);
|
|
114
|
+
state = { kind: "unavailable" };
|
|
115
|
+
}
|
|
116
|
+
if (state.kind === "serve") return undefined;
|
|
117
|
+
if (kind === "page") return options.page(state);
|
|
118
|
+
if (state.kind === "not-published") return undefined;
|
|
119
|
+
const sites =
|
|
120
|
+
state.kind === "unavailable" ? {} : { live: state.site, published: state.published };
|
|
121
|
+
const input = statusInput(
|
|
122
|
+
state.kind,
|
|
123
|
+
sites,
|
|
124
|
+
{ host: url.host, pathname: url.pathname },
|
|
125
|
+
(options.now ?? Date.now)(),
|
|
126
|
+
);
|
|
127
|
+
const as = url.pathname.toLowerCase().endsWith(".json") ? "json" : "text";
|
|
128
|
+
return statusPlain(state.kind, input, as, { method: request.method });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const HEALTH_PATH = "/__cloudhal/health";
|
|
132
|
+
|
|
133
|
+
const HEALTH_HEADERS = {
|
|
134
|
+
"cache-control": "no-store",
|
|
135
|
+
"cloudflare-cdn-cache-control": "no-store",
|
|
136
|
+
"x-robots-tag": "noindex",
|
|
137
|
+
} ;
|
|
138
|
+
|
|
139
|
+
/** §1.9: the store cannot be read, so the site is down for monitors. */
|
|
140
|
+
function healthDown() {
|
|
141
|
+
return Response.json({ ok: false }, { status: 503, headers: HEALTH_HEADERS });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* §1.9: `{ ok, state }`, the admin's `/api/health` shape plus the site's state, so one monitor
|
|
146
|
+
* rule reads both. Public, never cached, never indexed; reads only the memoised pointer.
|
|
147
|
+
*/
|
|
148
|
+
export async function health(
|
|
149
|
+
request ,
|
|
150
|
+
state ,
|
|
151
|
+
log = console.error,
|
|
152
|
+
) {
|
|
153
|
+
if (request.method !== "GET" && request.method !== "HEAD")
|
|
154
|
+
return Response.json(
|
|
155
|
+
{ error: "method_not_allowed" },
|
|
156
|
+
{ status: 405, headers: { ...HEALTH_HEADERS, allow: "GET" } },
|
|
157
|
+
);
|
|
158
|
+
try {
|
|
159
|
+
const result = await state();
|
|
160
|
+
if (result.kind === "unavailable") return healthDown();
|
|
161
|
+
return Response.json(
|
|
162
|
+
{ ok: true, state: result.kind === "not-published" ? "not-published" : "published" },
|
|
163
|
+
{ headers: HEALTH_HEADERS },
|
|
164
|
+
);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
log("cloudhal: health check could not read the store", error);
|
|
167
|
+
return healthDown();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* RFC-0034 §4 with §1.2: on a multi-site Worker the hostname map could not be read (the store
|
|
173
|
+
* failed past the map's TTL), so no site is known. Treated as a store failure, never as `error`:
|
|
174
|
+
* health answers `503 { ok: false }`; `GET`/`HEAD` page paths the `unavailable` page and machine
|
|
175
|
+
* paths its plain answer (503, `Retry-After: 300`, English, no site name); anything else a
|
|
176
|
+
* plain 503.
|
|
177
|
+
*/
|
|
178
|
+
export async function hostLookupFailed(
|
|
179
|
+
request ,
|
|
180
|
+
error ,
|
|
181
|
+
options ,
|
|
182
|
+
) {
|
|
183
|
+
(options.log ?? console.error)("cloudhal: the site's hostnames could not be read", error);
|
|
184
|
+
if (new URL(request.url).pathname === HEALTH_PATH) return healthDown();
|
|
185
|
+
const answer = await statusStep(request, {
|
|
186
|
+
state: () => Promise.reject(error),
|
|
187
|
+
page: options.page,
|
|
188
|
+
log: () => {},
|
|
189
|
+
now: options.now,
|
|
190
|
+
});
|
|
191
|
+
const input = { locale: "en", now: (options.now ?? Date.now)() };
|
|
192
|
+
return answer ?? statusPlain("unavailable", input, "text", { method: request.method });
|
|
193
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type StatusDir } from "./status-html.js";
|
|
2
|
+
import { type SiteState, type StatusPageInput, type StatusStrings } from "./status-page.js";
|
|
3
|
+
/** The states a theme may draw. */
|
|
4
|
+
export type HookState = Exclude<SiteState, "error">;
|
|
5
|
+
export interface StatusContext extends StatusPageInput {
|
|
6
|
+
readonly status: 503 | 410;
|
|
7
|
+
readonly dir: StatusDir;
|
|
8
|
+
/** The built-in words, already in the page's locale. */
|
|
9
|
+
readonly strings: StatusStrings;
|
|
10
|
+
/** The owner's message, escaped, line breaks as `<br>` (never use `message.text` raw). */
|
|
11
|
+
readonly messageHtml?: string | undefined;
|
|
12
|
+
/** "Back by …", formatted. */
|
|
13
|
+
readonly untilText?: string | undefined;
|
|
14
|
+
/** The built-in document, with extra CSS appended. */
|
|
15
|
+
defaultHtml(options?: {
|
|
16
|
+
css?: string | undefined;
|
|
17
|
+
}): string;
|
|
18
|
+
}
|
|
19
|
+
export type StatusPageHook = (state: HookState, ctx: StatusContext) => string | undefined | Promise<string | undefined>;
|
|
20
|
+
/** How long a hook may take before the built-in page answers instead. */
|
|
21
|
+
export declare const HOOK_TIMEOUT_MS = 50;
|
|
22
|
+
/** Loads `src/theme.ts` lazily (the integration's `virtual:cloudhal/theme-hooks`) and memoises the hook. */
|
|
23
|
+
export declare function themeStatusHook(loadTheme: () => Promise<unknown>, onError?: (message: string, error: unknown) => void): () => Promise<StatusPageHook | undefined>;
|
|
24
|
+
export interface RenderStatusOptions {
|
|
25
|
+
readonly hook?: StatusPageHook | undefined;
|
|
26
|
+
readonly method?: string | undefined;
|
|
27
|
+
readonly onFailure?: ((message: string, error?: unknown) => void) | undefined;
|
|
28
|
+
/** The site's policy for a theme's document (default `SITE_CSP`). */
|
|
29
|
+
readonly csp?: string | undefined;
|
|
30
|
+
/** Test hook: the hook's time budget. */
|
|
31
|
+
readonly timeoutMs?: number | undefined;
|
|
32
|
+
}
|
|
33
|
+
/** Test hook: forget which states' hook failures were logged. */
|
|
34
|
+
export declare function resetHookFailures(): void;
|
|
35
|
+
/** The status response for `state`: the theme's document when the hook gives one, else the built-in page. */
|
|
36
|
+
export declare function renderStatus(state: SiteState, input: StatusPageInput, options?: RenderStatusOptions): Promise<Response>;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theme hook for status pages (RFC-0043 §1.7): a theme may export `statusPage(state, ctx)`
|
|
3
|
+
* from `src/theme.ts` and return its own document. The renderer keeps the status code, the
|
|
4
|
+
* headers and the CSP (invariant 9); the built-in page is used when the hook returns
|
|
5
|
+
* `undefined`, throws, answers with something other than a string, exceeds 64 KB, or takes more
|
|
6
|
+
* than 50 ms — logged once per state and isolate — and always for `error`, which must not depend
|
|
7
|
+
* on code that may be the cause. Worker-safe.
|
|
8
|
+
*/
|
|
9
|
+
import { SITE_CSP } from "./csp.js";
|
|
10
|
+
import { messageHtml, } from "./status-html.js";
|
|
11
|
+
import {
|
|
12
|
+
HOOK_HTML_LIMIT,
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
statusCode,
|
|
17
|
+
statusPage,
|
|
18
|
+
statusPageHtml,
|
|
19
|
+
statusStrings,
|
|
20
|
+
} from "./status-page.js";
|
|
21
|
+
|
|
22
|
+
/** The states a theme may draw. */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/** How long a hook may take before the built-in page answers instead. */
|
|
44
|
+
export const HOOK_TIMEOUT_MS = 50;
|
|
45
|
+
|
|
46
|
+
const failed = new Set ();
|
|
47
|
+
|
|
48
|
+
/** Loads `src/theme.ts` lazily (the integration's `virtual:cloudhal/theme-hooks`) and memoises the hook. */
|
|
49
|
+
export function themeStatusHook(
|
|
50
|
+
loadTheme ,
|
|
51
|
+
onError = () => {},
|
|
52
|
+
) {
|
|
53
|
+
let pending ;
|
|
54
|
+
return () => {
|
|
55
|
+
pending ??= loadTheme().then(
|
|
56
|
+
(mod) => {
|
|
57
|
+
const hook = (mod )?.statusPage;
|
|
58
|
+
return typeof hook === "function" ? (hook ) : undefined;
|
|
59
|
+
},
|
|
60
|
+
(error ) => {
|
|
61
|
+
onError("cloudhal: the theme module could not be loaded for the status page", error);
|
|
62
|
+
return undefined;
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
return pending;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
/** Runs the hook within its budget; `undefined` means the built-in page. */
|
|
80
|
+
async function hookHtml(
|
|
81
|
+
state ,
|
|
82
|
+
input ,
|
|
83
|
+
hook ,
|
|
84
|
+
options ,
|
|
85
|
+
) {
|
|
86
|
+
const strings = statusStrings(state, input);
|
|
87
|
+
const ctx = {
|
|
88
|
+
...input,
|
|
89
|
+
status: statusCode(state) ,
|
|
90
|
+
dir: strings.dir,
|
|
91
|
+
strings,
|
|
92
|
+
...(input.message && state === "maintenance"
|
|
93
|
+
? { messageHtml: messageHtml(input.message.text) }
|
|
94
|
+
: {}),
|
|
95
|
+
untilText: strings.untilText,
|
|
96
|
+
defaultHtml: (o = {}) => statusPageHtml(state, input, o),
|
|
97
|
+
};
|
|
98
|
+
let timer ;
|
|
99
|
+
const timeout = new Promise ((_, reject) => {
|
|
100
|
+
timer = setTimeout(
|
|
101
|
+
() => reject(new Error(`took more than ${options.timeoutMs ?? HOOK_TIMEOUT_MS} ms`)),
|
|
102
|
+
options.timeoutMs ?? HOOK_TIMEOUT_MS,
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
try {
|
|
106
|
+
const result = await Promise.race([Promise.resolve().then(() => hook(state, ctx)), timeout]);
|
|
107
|
+
if (result === undefined) return undefined;
|
|
108
|
+
if (typeof result !== "string") throw new Error("returned something other than a string");
|
|
109
|
+
if (result.length > HOOK_HTML_LIMIT)
|
|
110
|
+
throw new Error(`returned more than ${HOOK_HTML_LIMIT} bytes`);
|
|
111
|
+
return result;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (!failed.has(state)) {
|
|
114
|
+
failed.add(state);
|
|
115
|
+
options.onFailure?.(`cloudhal: the theme's statusPage hook failed for ${state}`, error);
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
} finally {
|
|
119
|
+
clearTimeout(timer);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Test hook: forget which states' hook failures were logged. */
|
|
124
|
+
export function resetHookFailures() {
|
|
125
|
+
failed.clear();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The status response for `state`: the theme's document when the hook gives one, else the built-in page. */
|
|
129
|
+
export async function renderStatus(
|
|
130
|
+
state ,
|
|
131
|
+
input ,
|
|
132
|
+
options = {},
|
|
133
|
+
) {
|
|
134
|
+
if (state === "error" || !options.hook)
|
|
135
|
+
return statusPage(state, input, { method: options.method });
|
|
136
|
+
const html = await hookHtml(state, input, options.hook, options);
|
|
137
|
+
return statusPage(state, input, {
|
|
138
|
+
method: options.method,
|
|
139
|
+
...(html !== undefined ? { html, csp: options.csp ?? SITE_CSP } : {}),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The built-in status document (RFC-0043 §1.3): one self-contained HTML page under 8 KB with
|
|
3
|
+
* inline CSS, the system font stack, no scripts and no requests except the site's own logo.
|
|
4
|
+
* Light and dark through `prefers-color-scheme`, logical properties only (ADR-0033), and the
|
|
5
|
+
* published site's tokens (§1.3) as four custom properties when the state allows them.
|
|
6
|
+
*/
|
|
7
|
+
import type { StatusMessages } from "./status-strings/en.js";
|
|
8
|
+
export type StatusDir = "ltr" | "rtl";
|
|
9
|
+
/** The published site's look, read from `Site.theme.config` (§1.3); every field optional. */
|
|
10
|
+
export interface StatusTheme {
|
|
11
|
+
/** `#rrggbb`: a decorative accent (never text), so it needs no contrast check. */
|
|
12
|
+
readonly brand?: string | undefined;
|
|
13
|
+
/** `#rrggbb`: the page background; used only together with `text` and when they contrast. */
|
|
14
|
+
readonly surface?: string | undefined;
|
|
15
|
+
/** `#rrggbb`: the text color; used only together with `surface` and when they contrast. */
|
|
16
|
+
readonly text?: string | undefined;
|
|
17
|
+
readonly font?: "sans" | "serif" | "mono" | undefined;
|
|
18
|
+
}
|
|
19
|
+
/** What the template draws; every piece already chosen and, where it is text, still unescaped. */
|
|
20
|
+
export interface StatusDocument {
|
|
21
|
+
readonly state: string;
|
|
22
|
+
readonly locale: string;
|
|
23
|
+
readonly dir: StatusDir;
|
|
24
|
+
readonly noindex: boolean;
|
|
25
|
+
readonly words: Pick<StatusMessages, "builtWith"> & {
|
|
26
|
+
title: string;
|
|
27
|
+
body: string;
|
|
28
|
+
};
|
|
29
|
+
readonly siteName?: string | undefined;
|
|
30
|
+
/** A same-origin `/media/…` URL only (the page's CSP allows `img-src 'self'`). */
|
|
31
|
+
readonly logo?: string | undefined;
|
|
32
|
+
readonly message?: {
|
|
33
|
+
readonly text: string;
|
|
34
|
+
readonly locale: string;
|
|
35
|
+
readonly dir: StatusDir;
|
|
36
|
+
};
|
|
37
|
+
/** "Back by …", already formatted. */
|
|
38
|
+
readonly until?: string | undefined;
|
|
39
|
+
/** "Reference: …", already formatted. */
|
|
40
|
+
readonly reference?: string | undefined;
|
|
41
|
+
readonly theme?: StatusTheme | undefined;
|
|
42
|
+
/** Extra CSS from a theme hook's `defaultHtml({ css })`, appended after the built-in rules. */
|
|
43
|
+
readonly css?: string | undefined;
|
|
44
|
+
}
|
|
45
|
+
export declare function escapeHtml(s: string): string;
|
|
46
|
+
/** The owner's message: escaped, line breaks as `<br>`; never markup or links (§2.10). */
|
|
47
|
+
export declare function messageHtml(text: string): string;
|
|
48
|
+
/** WCAG 2.x contrast ratio of two `#rrggbb` colors. */
|
|
49
|
+
export declare function contrastRatio(a: string, b: string): number;
|
|
50
|
+
/** The document for `doc`, as a string. */
|
|
51
|
+
export declare function statusDocument(doc: StatusDocument): string;
|