@cloudhal/astro-renderer 0.6.2 → 0.6.4

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.
Files changed (59) hide show
  1. package/dist/src/astro-env.d.ts +9 -0
  2. package/dist/src/cloudflare-entry.d.ts +6 -0
  3. package/dist/src/cloudflare-entry.js +135 -6
  4. package/dist/src/integration.js +34 -5
  5. package/dist/src/runtime-workerd.d.ts +26 -0
  6. package/dist/src/runtime-workerd.js +63 -3
  7. package/dist/src/runtime.d.ts +18 -0
  8. package/dist/src/runtime.js +19 -0
  9. package/dist/src/status-hook.d.ts +36 -0
  10. package/dist/src/status-hook.js +141 -0
  11. package/dist/src/status-html.d.ts +51 -0
  12. package/dist/src/status-html.js +157 -0
  13. package/dist/src/status-middleware.d.ts +6 -0
  14. package/dist/src/status-middleware.js +61 -0
  15. package/dist/src/status-page.d.ts +94 -0
  16. package/dist/src/status-page.js +323 -0
  17. package/dist/src/status-serve.d.ts +44 -0
  18. package/dist/src/status-serve.js +112 -0
  19. package/dist/src/status-strings/ar.d.ts +2 -0
  20. package/dist/src/status-strings/ar.js +34 -0
  21. package/dist/src/status-strings/catalog.d.ts +8 -0
  22. package/dist/src/status-strings/catalog.js +52 -0
  23. package/dist/src/status-strings/de.d.ts +2 -0
  24. package/dist/src/status-strings/de.js +34 -0
  25. package/dist/src/status-strings/en.d.ts +46 -0
  26. package/dist/src/status-strings/en.js +47 -0
  27. package/dist/src/status-strings/es.d.ts +2 -0
  28. package/dist/src/status-strings/es.js +34 -0
  29. package/dist/src/status-strings/fa.d.ts +2 -0
  30. package/dist/src/status-strings/fa.js +34 -0
  31. package/dist/src/status-strings/fr.d.ts +2 -0
  32. package/dist/src/status-strings/fr.js +34 -0
  33. package/dist/src/status-strings/he.d.ts +2 -0
  34. package/dist/src/status-strings/he.js +34 -0
  35. package/dist/src/status-strings/hi.d.ts +2 -0
  36. package/dist/src/status-strings/hi.js +34 -0
  37. package/dist/src/status-strings/id.d.ts +2 -0
  38. package/dist/src/status-strings/id.js +34 -0
  39. package/dist/src/status-strings/it.d.ts +2 -0
  40. package/dist/src/status-strings/it.js +34 -0
  41. package/dist/src/status-strings/ja.d.ts +2 -0
  42. package/dist/src/status-strings/ja.js +34 -0
  43. package/dist/src/status-strings/ko.d.ts +2 -0
  44. package/dist/src/status-strings/ko.js +34 -0
  45. package/dist/src/status-strings/nl.d.ts +2 -0
  46. package/dist/src/status-strings/nl.js +34 -0
  47. package/dist/src/status-strings/pl.d.ts +2 -0
  48. package/dist/src/status-strings/pl.js +34 -0
  49. package/dist/src/status-strings/pt-BR.d.ts +2 -0
  50. package/dist/src/status-strings/pt-BR.js +34 -0
  51. package/dist/src/status-strings/ru.d.ts +2 -0
  52. package/dist/src/status-strings/ru.js +34 -0
  53. package/dist/src/status-strings/tr.d.ts +2 -0
  54. package/dist/src/status-strings/tr.js +34 -0
  55. package/dist/src/status-strings/uk.d.ts +2 -0
  56. package/dist/src/status-strings/uk.js +34 -0
  57. package/dist/src/status-strings/zh-Hans.d.ts +2 -0
  58. package/dist/src/status-strings/zh-Hans.js +34 -0
  59. package/package.json +27 -11
@@ -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;
@@ -0,0 +1,157 @@
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
+
8
+
9
+
10
+
11
+ /** The published site's look, read from `Site.theme.config` (§1.3); every field optional. */
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ /** What the template draws; every piece already chosen and, where it is text, still unescaped. */
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+  
31
+
32
+
33
+  
34
+
35
+  
36
+
37
+
38
+
39
+
40
+
41
+
42
+ export function escapeHtml(s ) {
43
+ return s.replace(
44
+ /[&<>"']/g,
45
+ (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c] ,
46
+ );
47
+ }
48
+
49
+ /** The owner's message: escaped, line breaks as `<br>`; never markup or links (§2.10). */
50
+ export function messageHtml(text ) {
51
+ return escapeHtml(text.replace(/\r\n?/g, "\n"))
52
+ .split("\n")
53
+ .map((line) => line.trim())
54
+ .filter(Boolean)
55
+ .join("<br>");
56
+ }
57
+
58
+ const HEX = /^#[0-9a-f]{6}$/i;
59
+
60
+ function luminance(hex ) {
61
+ const channel = (i ) => {
62
+ const c = Number.parseInt(hex.slice(i, i + 2), 16) / 255;
63
+ return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
64
+ };
65
+ return 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5);
66
+ }
67
+
68
+ /** WCAG 2.x contrast ratio of two `#rrggbb` colors. */
69
+ export function contrastRatio(a , b ) {
70
+ const [l1, l2] = [luminance(a), luminance(b)].sort((x, y) => y - x) ;
71
+ return (l1 + 0.05) / (l2 + 0.05);
72
+ }
73
+
74
+ /** Only typed values reach the stylesheet: hex colors (a readable pair) and a font keyword. */
75
+ function themeCss(theme ) {
76
+ if (!theme) return "";
77
+ const decl = [];
78
+ if (theme.brand && HEX.test(theme.brand)) decl.push(`--ch-status-brand:${theme.brand}`);
79
+ if (
80
+ theme.surface &&
81
+ theme.text &&
82
+ HEX.test(theme.surface) &&
83
+ HEX.test(theme.text) &&
84
+ contrastRatio(theme.surface, theme.text) >= 4.5
85
+ )
86
+ decl.push(`--ch-status-surface:${theme.surface}`, `--ch-status-text:${theme.text}`);
87
+ const stacks = {
88
+ sans: "system-ui, sans-serif",
89
+ serif: "ui-serif, Georgia, serif",
90
+ mono: "ui-monospace, Menlo, monospace",
91
+ };
92
+ if (theme.font && theme.font in stacks) decl.push(`--ch-status-font:${stacks[theme.font]}`);
93
+ return decl.length ? `:root{${decl.join(";")}}` : "";
94
+ }
95
+
96
+ /** The built-in rules: about 1.2 KB, logical properties only. */
97
+ const CSS = [
98
+ ":root{--ch-status-brand:#2563eb;--ch-status-surface:#f6f7f9;--ch-status-text:#1a1d21;",
99
+ "--ch-status-muted:#5b6470;--ch-status-card:#ffffff;--ch-status-font:system-ui,sans-serif;",
100
+ "color-scheme:light dark}",
101
+ "@media(prefers-color-scheme:dark){:root{--ch-status-surface:#111418;--ch-status-text:#e8eaed;",
102
+ "--ch-status-muted:#a3abb5;--ch-status-card:#1b1f25}}",
103
+ "*{box-sizing:border-box}",
104
+ "html{background:var(--ch-status-surface);color:var(--ch-status-text);",
105
+ "font:16px/1.5 var(--ch-status-font)}",
106
+ "body{margin:0;min-height:100vh;display:grid;grid-template-rows:1fr auto;",
107
+ "padding-block:1rem;padding-inline:1rem}",
108
+ ".ch-status{align-self:center;justify-self:center;inline-size:100%;max-inline-size:32rem;",
109
+ "background:var(--ch-status-card);border-block-start:4px solid var(--ch-status-brand);",
110
+ "border-radius:.75rem;padding-block:2rem;padding-inline:1.75rem;",
111
+ "box-shadow:0 1px 2px rgba(0,0,0,.06),0 8px 24px rgba(0,0,0,.08)}",
112
+ ".ch-status__logo{max-block-size:3rem;max-inline-size:12rem;margin-block-end:1rem}",
113
+ ".ch-status__site{margin:0 0 .5rem;font-size:.875rem;letter-spacing:.04em;",
114
+ "text-transform:uppercase;color:var(--ch-status-muted)}",
115
+ "h1{margin:0 0 .75rem;font-size:1.625rem;line-height:1.25}",
116
+ "p{margin:0 0 .75rem}",
117
+ ".ch-status__message{padding-block:.75rem;padding-inline:1rem;",
118
+ "border-inline-start:3px solid var(--ch-status-brand);",
119
+ "background:color-mix(in srgb,var(--ch-status-brand) 8%,transparent);border-radius:.25rem}",
120
+ ".ch-status__until,.ch-status__ref{color:var(--ch-status-muted);font-size:.9375rem}",
121
+ "code{font-family:ui-monospace,Menlo,monospace;font-size:.9em}",
122
+ "footer{justify-self:center;margin-block-start:1.5rem;font-size:.8125rem;",
123
+ "color:var(--ch-status-muted)}",
124
+ ].join("");
125
+
126
+ /** The document for `doc`, as a string. */
127
+ export function statusDocument(doc ) {
128
+ const e = escapeHtml;
129
+ const title = doc.siteName ? `${doc.words.title} · ${doc.siteName}` : doc.words.title;
130
+ const parts = [
131
+ "<!doctype html>",
132
+ `<html lang="${e(doc.locale)}" dir="${doc.dir}">`,
133
+ "<head>",
134
+ '<meta charset="utf-8">',
135
+ '<meta name="viewport" content="width=device-width, initial-scale=1">',
136
+ doc.noindex ? '<meta name="robots" content="noindex">' : "",
137
+ `<title>${e(title)}</title>`,
138
+ `<style>${CSS}${themeCss(doc.theme)}${doc.css ?? ""}</style>`,
139
+ "</head>",
140
+ "<body>",
141
+ `<main class="ch-status" data-state="${e(doc.state)}">`,
142
+ doc.logo ? `<img class="ch-status__logo" src="${e(doc.logo)}" alt="">` : "",
143
+ doc.siteName ? `<p class="ch-status__site">${e(doc.siteName)}</p>` : "",
144
+ `<h1>${e(doc.words.title)}</h1>`,
145
+ `<p>${e(doc.words.body)}</p>`,
146
+ doc.message
147
+ ? `<p class="ch-status__message" lang="${e(doc.message.locale)}" dir="${doc.message.dir}">${messageHtml(doc.message.text)}</p>`
148
+ : "",
149
+ doc.until ? `<p class="ch-status__until">${e(doc.until)}</p>` : "",
150
+ doc.reference ? `<p class="ch-status__ref"><code>${e(doc.reference)}</code></p>` : "",
151
+ "</main>",
152
+ `<footer>${e(doc.words.builtWith)}</footer>`,
153
+ "</body>",
154
+ "</html>",
155
+ ];
156
+ return `${parts.filter(Boolean).join("\n")}\n`;
157
+ }
@@ -0,0 +1,6 @@
1
+ interface MiddlewareContext {
2
+ request: Request;
3
+ }
4
+ export declare const STATUS_PREVIEW_PATH: RegExp;
5
+ export declare function onRequest(context: MiddlewareContext, next: () => Promise<Response>): Promise<Response>;
6
+ export {};
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Dev middleware (added by the `cloudhal()` integration for Node targets, RFC-0043 §1.8):
3
+ * `cloudhal dev` renders the working copy, drafts included, so it never answers a real page with
4
+ * a status page. For parity it draws each page on demand, through the same module and the
5
+ * project's theme hook, at `/__cloudhal/status/<state>` (`?locale=tr`, `?page=coming-soon`,
6
+ * `?message=…`, `?until=<RFC 3339>`), so a theme author can see and screenshot their override.
7
+ */
8
+ import config from "virtual:cloudhal/config";
9
+ import { loadTheme } from "virtual:cloudhal/theme-hooks";
10
+ import { getSource } from "@cloudhal/astro-renderer/runtime";
11
+ import { renderStatus, themeStatusHook } from "./status-hook.js";
12
+ import { isSiteState, } from "./status-page.js";
13
+ import { statusInput } from "./status-serve.js";
14
+
15
+
16
+
17
+
18
+
19
+ export const STATUS_PREVIEW_PATH = /^\/__cloudhal\/status\/([a-z-]+)\/?$/;
20
+
21
+ const hook = themeStatusHook(loadTheme, (m, e) => console.error(m, e));
22
+
23
+ export async function onRequest(
24
+ context ,
25
+ next ,
26
+ ) {
27
+ const url = new URL(context.request.url);
28
+ const m = STATUS_PREVIEW_PATH.exec(url.pathname);
29
+ if (!m) return next();
30
+ const state = m[1] ;
31
+ if (!isSiteState(state))
32
+ return new Response(`unknown state "${state}"`, {
33
+ status: 404,
34
+ headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" },
35
+ });
36
+ const site = await (await getSource(config)).site();
37
+ const now = Date.now();
38
+ // Dev shows the working copy: the live node stands in for the published one here.
39
+ const base = statusInput(
40
+ state,
41
+ { live: site, published: site },
42
+ { host: url.host, pathname: "/" },
43
+ now,
44
+ );
45
+ const locale = url.searchParams.get("locale");
46
+ const message = url.searchParams.get("message");
47
+ const until = url.searchParams.get("until");
48
+ const input = {
49
+ ...base,
50
+ ...(locale ? { locale } : {}),
51
+ page: url.searchParams.get("page") === "coming-soon" ? "coming-soon" : "maintenance",
52
+ ...(message ? { message: { text: message, locale: locale ?? site.defaultLocale } } : {}),
53
+ ...(until ? { until: { at: until, timeZone: site.timeZone } } : {}),
54
+ ...(state === "error" ? { rayId: "dev-preview" } : {}),
55
+ };
56
+ return renderStatus(state, input, {
57
+ hook: await hook(),
58
+ method: context.request.method,
59
+ onFailure: (msg, e) => console.error(msg, e),
60
+ });
61
+ }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Status pages (RFC-0043 §1): the one page a site answers with when it cannot show its pages —
3
+ * not published yet, in maintenance, unavailable, failed, or behind an expired preview link —
4
+ * with the right status code, `Retry-After`, `no-store` and its own CSP. Pure and Worker-safe:
5
+ * it imports only the catalog and the template, takes an injected clock, and never throws (a
6
+ * broken input degrades to the English page). Where it is served: `cloudflare-entry.ts` before
7
+ * Astro, `status-middleware.ts` in `cloudhal dev`, and the starter's routes as their fallback.
8
+ */
9
+ import { type StatusDir, type StatusTheme } from "./status-html.js";
10
+ export type { HookState, StatusContext, StatusPageHook } from "./status-hook.js";
11
+ export type { StatusTheme } from "./status-html.js";
12
+ export type SiteState = "not-published" | "maintenance" | "unavailable" | "error" | "preview-expired";
13
+ export declare const SITE_STATES: readonly SiteState[];
14
+ export declare const isSiteState: (value: string) => value is SiteState;
15
+ export interface StatusPageInput {
16
+ /** §1.6 decides what is passed per state; `logo` is a same-origin `/media/…` URL. */
17
+ readonly site?: {
18
+ readonly name: string;
19
+ readonly logo?: string | undefined;
20
+ } | undefined;
21
+ /** Already chosen (§1.4: host or path prefix, the site default, then English). */
22
+ readonly locale: string;
23
+ /** Maintenance wording (§2.1 `page`); `maintenance` when absent. */
24
+ readonly page?: "maintenance" | "coming-soon" | undefined;
25
+ /** Maintenance only: the owner's plain-text message and its language. */
26
+ readonly message?: {
27
+ readonly text: string;
28
+ readonly locale: string;
29
+ } | undefined;
30
+ /** Maintenance only: when it ends, for "Back by …" and `Retry-After`. */
31
+ readonly until?: {
32
+ readonly at: string;
33
+ readonly timeZone?: string | undefined;
34
+ } | undefined;
35
+ /** The published site's tokens (§1.3); the caller passes them only where the state allows. */
36
+ readonly theme?: StatusTheme | undefined;
37
+ /** `error` only: the request id a visitor can quote (`cf-ray`). */
38
+ readonly rayId?: string | undefined;
39
+ /** Injected clock (RFC-0008 §9.4). */
40
+ readonly now: number;
41
+ }
42
+ /** The built-in page's own policy (§1.3): inline style, same-origin images, nothing else. */
43
+ export declare const STATUS_CSP = "default-src 'none'; style-src 'unsafe-inline'; img-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'";
44
+ /** The built-in document never exceeds this (§1.3); a test draws every state in every language. */
45
+ export declare const STATUS_PAGE_LIMIT = 8192;
46
+ /** A theme hook's document may be this large before the built-in page is used instead (§1.7). */
47
+ export declare const HOOK_HTML_LIMIT = 65536;
48
+ export declare function statusCode(state: SiteState): 503 | 500 | 410;
49
+ /** Delay-seconds (§1.2), or nothing for `error` and `preview-expired`. */
50
+ export declare function retryAfter(state: SiteState, input: Pick<StatusPageInput, "until" | "now">): string | undefined;
51
+ /**
52
+ * The catalog locale for a site locale (§1.4): exact, then the primary language subtag
53
+ * (`pt-PT` → `pt-BR`, `zh-HK` → `zh-Hans`), then English.
54
+ */
55
+ export declare function catalogLocale(locale: string): string;
56
+ /** `<html dir>` for a locale: the catalog's entry, else by script. */
57
+ export declare function statusDirection(locale: string): StatusDir;
58
+ /** ICU simple arguments only (§1.5): `{name}` is replaced, anything else is left as it is. */
59
+ export declare function fillMessage(message: string, params: Readonly<Record<string, string>>): string;
60
+ /** The words of a page in its language: what the theme hook gets as `strings` (§1.7). */
61
+ export interface StatusStrings {
62
+ readonly locale: string;
63
+ readonly dir: StatusDir;
64
+ readonly title: string;
65
+ readonly body: string;
66
+ readonly builtWith: string;
67
+ readonly untilText?: string | undefined;
68
+ readonly referenceText?: string | undefined;
69
+ }
70
+ export declare function statusStrings(state: SiteState, input: StatusPageInput): StatusStrings;
71
+ /** The same document as a string, for theme hooks that only restyle it (§1.7). Never throws. */
72
+ export declare function statusPageHtml(state: SiteState, input: StatusPageInput, options?: {
73
+ css?: string | undefined;
74
+ }): string;
75
+ /** The headers every status response carries (§1.2), for an HTML page or a plain answer. */
76
+ export declare function statusHeaders(state: SiteState, input: Pick<StatusPageInput, "until" | "now">, contentType: string, csp?: string): Headers;
77
+ export interface StatusPageOptions {
78
+ /** `HEAD` gets the headers without a body. */
79
+ readonly method?: string | undefined;
80
+ /**
81
+ * A theme's own document (§1.7), already checked by the hook runner: it replaces the built-in
82
+ * body and gets `csp` (the site's policy) instead of the built-in page's stricter one.
83
+ */
84
+ readonly html?: string | undefined;
85
+ readonly csp?: string | undefined;
86
+ }
87
+ /** The built-in page: status, headers and body. Never throws. */
88
+ export declare function statusPage(state: SiteState, input: StatusPageInput, options?: StatusPageOptions): Response;
89
+ /** Machine endpoints in the same states (§2.3): short plain text or JSON, same status and headers. */
90
+ export declare function statusPlain(state: SiteState, input: StatusPageInput, as: "text" | "json", options?: {
91
+ method?: string | undefined;
92
+ }): Response;
93
+ /** The owner's message as the hook sees it (`messageHtml`, §1.7). */
94
+ export { messageHtml } from "./status-html.js";