@connextar/house 0.21.2 → 0.22.2

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 (39) hide show
  1. package/README.md +91 -0
  2. package/dist/maintenance/core.d.ts +109 -0
  3. package/dist/maintenance/core.d.ts.map +1 -0
  4. package/dist/maintenance/core.js +189 -0
  5. package/dist/maintenance/core.js.map +1 -0
  6. package/dist/maintenance/gate.d.ts +48 -0
  7. package/dist/maintenance/gate.d.ts.map +1 -0
  8. package/dist/maintenance/gate.js +114 -0
  9. package/dist/maintenance/gate.js.map +1 -0
  10. package/dist/maintenance/index.d.ts +7 -0
  11. package/dist/maintenance/index.d.ts.map +1 -0
  12. package/dist/maintenance/index.js +7 -0
  13. package/dist/maintenance/index.js.map +1 -0
  14. package/dist/maintenance/page.d.ts +21 -0
  15. package/dist/maintenance/page.d.ts.map +1 -0
  16. package/dist/maintenance/page.js +53 -0
  17. package/dist/maintenance/page.js.map +1 -0
  18. package/dist/maintenance/proxy.d.ts +41 -0
  19. package/dist/maintenance/proxy.d.ts.map +1 -0
  20. package/dist/maintenance/proxy.js +33 -0
  21. package/dist/maintenance/proxy.js.map +1 -0
  22. package/dist/maintenance/react-index.d.ts +2 -0
  23. package/dist/maintenance/react-index.d.ts.map +1 -0
  24. package/dist/maintenance/react-index.js +2 -0
  25. package/dist/maintenance/react-index.js.map +1 -0
  26. package/dist/maintenance/react.d.ts +36 -0
  27. package/dist/maintenance/react.d.ts.map +1 -0
  28. package/dist/maintenance/react.js +141 -0
  29. package/dist/maintenance/react.js.map +1 -0
  30. package/dist/maintenance/route.d.ts +37 -0
  31. package/dist/maintenance/route.d.ts.map +1 -0
  32. package/dist/maintenance/route.js +41 -0
  33. package/dist/maintenance/route.js.map +1 -0
  34. package/dist/maintenance/unavailable.d.ts +11 -0
  35. package/dist/maintenance/unavailable.d.ts.map +1 -0
  36. package/dist/maintenance/unavailable.js +63 -0
  37. package/dist/maintenance/unavailable.js.map +1 -0
  38. package/docs/Maintenance.md +121 -0
  39. package/package.json +21 -13
@@ -0,0 +1,53 @@
1
+ import { describeWindow } from "./core.js";
2
+ function esc(value) {
3
+ return value.replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c] ?? c);
4
+ }
5
+ export function maintenancePageHtml(window, options) {
6
+ const app = esc(options.appName);
7
+ const refresh = Math.max(10, Math.round(options.refreshSeconds ?? 30));
8
+ const words = window.id === "override" ? null : describeWindow(window, options);
9
+ const backBy = words
10
+ ? `It should be back by <strong>${esc(words.end)} ${esc(words.zone)}</strong>${words.day ? ` (${esc(words.day)})` : ""}, usually sooner.`
11
+ : "It will be back shortly.";
12
+ const message = window.message ? `<p class="note">${esc(window.message)}</p>` : "";
13
+ const contact = options.contactEmail
14
+ ? `<p class="small">Something urgent? <a href="mailto:${esc(options.contactEmail)}">${esc(options.contactEmail)}</a></p>`
15
+ : "";
16
+ return `<!doctype html>
17
+ <html lang="en">
18
+ <head>
19
+ <meta charset="utf-8">
20
+ <meta name="viewport" content="width=device-width, initial-scale=1">
21
+ <meta name="robots" content="noindex">
22
+ <meta http-equiv="refresh" content="${refresh}">
23
+ <title>${app} — back shortly</title>
24
+ <style>
25
+ :root { color-scheme: light dark; --bg: #f7f7f5; --card: #ffffff; --fg: #1c1c1a; --muted: #5f5f5a; --border: #e3e3de; --accent: #2f6f5e; }
26
+ @media (prefers-color-scheme: dark) { :root { --bg: #141413; --card: #1d1d1b; --fg: #f1f1ec; --muted: #a9a9a2; --border: #33332f; --accent: #7cc4ad; } }
27
+ * { box-sizing: border-box; }
28
+ body { margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; background: var(--bg); color: var(--fg); font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
29
+ main { width: 100%; max-width: 30rem; background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 32px 28px; }
30
+ .kicker { margin: 0; font: 600 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); }
31
+ h1 { margin: 12px 0 0; font-size: 1.6rem; line-height: 1.25; letter-spacing: -.01em; }
32
+ p { margin: 14px 0 0; color: var(--muted); }
33
+ strong { color: var(--fg); }
34
+ .note { padding: 12px 14px; border-left: 3px solid var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); color: var(--fg); border-radius: 6px; }
35
+ .small { font-size: .875rem; }
36
+ a { color: var(--accent); }
37
+ </style>
38
+ </head>
39
+ <body>
40
+ <main>
41
+ <p class="kicker">Planned maintenance</p>
42
+ <h1>${app} will be back shortly</h1>
43
+ <p>We're doing planned maintenance on the servers behind ${app}. Nothing you've saved is affected.</p>
44
+ <p>${backBy}</p>
45
+ ${message}
46
+ <p class="small">This page reloads by itself every ${refresh} seconds — you can leave it open.</p>
47
+ ${contact}
48
+ </main>
49
+ </body>
50
+ </html>
51
+ `;
52
+ }
53
+ //# sourceMappingURL=page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page.js","sourceRoot":"","sources":["../../src/maintenance/page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAA8C,MAAM,WAAW,CAAC;AAqBvF,SAAS,GAAG,CAAC,KAAa;IACxB,OAAO,KAAK,CAAC,OAAO,CAClB,UAAU,EACV,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CACzF,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,MAAyB,EAAE,OAA+B;IAC5F,MAAM,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACjC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,IAAI,EAAE,CAAC,CAAC,CAAC;IACvE,MAAM,KAAK,GAAG,MAAM,CAAC,EAAE,KAAK,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChF,MAAM,MAAM,GAAG,KAAK;QAClB,CAAC,CAAC,gCAAgC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,YAAY,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,mBAAmB;QACzI,CAAC,CAAC,0BAA0B,CAAC;IAC/B,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,mBAAmB,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;IACnF,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY;QAClC,CAAC,CAAC,sDAAsD,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,UAAU;QACzH,CAAC,CAAC,EAAE,CAAC;IACP,OAAO;;;;;;sCAM6B,OAAO;SACpC,GAAG;;;;;;;;;;;;;;;;;;;MAmBN,GAAG;2DACkD,GAAG;KACzD,MAAM;EACT,OAAO;qDAC4C,OAAO;EAC1D,OAAO;;;;CAIR,CAAC;AACF,CAAC"}
@@ -0,0 +1,41 @@
1
+ import type { MaintenanceState } from "./core.js";
2
+ import type { MaintenanceGate } from "./gate.js";
3
+ import { type MaintenancePageOptions } from "./page.js";
4
+ /**
5
+ * For the app's `proxy.ts` (Next 16; `middleware.ts` before it). Returns the
6
+ * response to send during a window, or `undefined` to carry on:
7
+ *
8
+ * ```ts
9
+ * const gate = maintenanceGateFromEnv();
10
+ * export async function proxy(request: NextRequest) {
11
+ * return maintenanceResponse(request, gate, { page: { appName: "AltEd" } });
12
+ * }
13
+ * ```
14
+ *
15
+ * During a window:
16
+ * - **pages** get the maintenance page itself (`maintenancePageHtml`) with `503`
17
+ * and `Retry-After` — a client-side navigation receives HTML instead of its
18
+ * payload and falls back to loading the page, which is the maintenance page;
19
+ * - **APIs** get `503` JSON naming the window;
20
+ * - **crons** get `200 { skipped: "maintenance" }`, so a scheduler doesn't count
21
+ * a failure and the jobs don't log a database error each;
22
+ * - the state route, Next's own assets and the app's static files pass, as do
23
+ * the paths in `keepUp`.
24
+ *
25
+ * Plain web `Request` and `Response`: no import from `next`.
26
+ */
27
+ export interface MaintenanceProxyOptions {
28
+ page: MaintenancePageOptions;
29
+ /** The state route; always passes. Default `/api/house/maintenance`. */
30
+ statePath?: string;
31
+ /** Path prefixes that stay up (they don't need the database). */
32
+ keepUp?: readonly string[];
33
+ /** Default `/api/cron/`. */
34
+ cronPrefix?: string;
35
+ /** Default `/api/`. */
36
+ apiPrefix?: string;
37
+ }
38
+ export declare function passesDuringMaintenance(pathname: string, options?: Omit<MaintenanceProxyOptions, "page">): boolean;
39
+ export declare function maintenanceOnlyResponse(request: Request, state: MaintenanceState, options: MaintenanceProxyOptions): Response | undefined;
40
+ export declare function maintenanceResponse(request: Request, gate: MaintenanceGate, options: MaintenanceProxyOptions): Promise<Response | undefined>;
41
+ //# sourceMappingURL=proxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../src/maintenance/proxy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAuB,KAAK,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAG7E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,uBAAuB;IACtC,IAAI,EAAE,sBAAsB,CAAC;IAC7B,wEAAwE;IACxE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3B,4BAA4B;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uBAAuB;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAKD,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,MAAM,EAChB,OAAO,GAAE,IAAI,CAAC,uBAAuB,EAAE,MAAM,CAAM,GAClD,OAAO,CAOT;AAED,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,gBAAgB,EACvB,OAAO,EAAE,uBAAuB,GAC/B,QAAQ,GAAG,SAAS,CAoBtB;AAED,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,eAAe,EACrB,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAE/B"}
@@ -0,0 +1,33 @@
1
+ import { maintenancePageHtml } from "./page.js";
2
+ import { publicWindow } from "./route.js";
3
+ /** Files that are never generated from the database. json, txt and xml are left out: apps generate those. */
4
+ const STATIC_FILE = /\.(?:ico|png|jpe?g|gif|webp|avif|svg|css|js|mjs|map|woff2?|ttf|otf|webmanifest)$/i;
5
+ export function passesDuringMaintenance(pathname, options = {}) {
6
+ const statePath = options.statePath ?? "/api/house/maintenance";
7
+ if (pathname === statePath || pathname.startsWith("/_next/"))
8
+ return true;
9
+ if (STATIC_FILE.test(pathname))
10
+ return true;
11
+ return (options.keepUp ?? []).some((prefix) => pathname === prefix || pathname.startsWith(prefix.endsWith("/") ? prefix : `${prefix}/`));
12
+ }
13
+ export function maintenanceOnlyResponse(request, state, options) {
14
+ if (state.phase !== "active" || !state.window)
15
+ return undefined;
16
+ const { pathname } = new URL(request.url);
17
+ if (passesDuringMaintenance(pathname, options))
18
+ return undefined;
19
+ const retryAfter = String(state.retryAfterSeconds ?? 300);
20
+ const headers = { "cache-control": "no-store", "retry-after": retryAfter, "x-content-type-options": "nosniff" };
21
+ if (pathname.startsWith(options.cronPrefix ?? "/api/cron/")) {
22
+ return Response.json({ skipped: "maintenance", window: publicWindow(state.window) }, { headers: { "cache-control": "no-store" } });
23
+ }
24
+ if (pathname.startsWith(options.apiPrefix ?? "/api/")) {
25
+ return Response.json({ error: "maintenance", window: publicWindow(state.window), retryAfterSeconds: state.retryAfterSeconds }, { status: 503, headers });
26
+ }
27
+ const html = request.method === "HEAD" ? null : maintenancePageHtml(state.window, options.page);
28
+ return new Response(html, { status: 503, headers: { ...headers, "content-type": "text/html; charset=utf-8" } });
29
+ }
30
+ export async function maintenanceResponse(request, gate, options) {
31
+ return maintenanceOnlyResponse(request, await gate.state(), options);
32
+ }
33
+ //# sourceMappingURL=proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../src/maintenance/proxy.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAA+B,MAAM,WAAW,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAqC1C,6GAA6G;AAC7G,MAAM,WAAW,GAAG,mFAAmF,CAAC;AAExG,MAAM,UAAU,uBAAuB,CACrC,QAAgB,EAChB,UAAiD,EAAE;IAEnD,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,wBAAwB,CAAC;IAChE,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,CAAC,UAAU,CAAC,SAAS,CAAC;QAAE,OAAO,IAAI,CAAC;IAC1E,IAAI,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC;QAAE,OAAO,IAAI,CAAC;IAC5C,OAAO,CAAC,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,IAAI,CAChC,CAAC,MAAM,EAAE,EAAE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BAAiB,EAAE,EACxG,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,CACzB,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,mBAAmB,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAChG,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,GAAG,OAAO,EAAE,cAAc,EAAE,0BAA0B,EAAE,EAAE,CAAC,CAAC;AAClH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,OAAgB,EAChB,IAAqB,EACrB,OAAgC;IAEhC,OAAO,uBAAuB,CAAC,OAAO,EAAE,MAAM,IAAI,CAAC,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;AACvE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { MaintenanceBanner, useMaintenanceState, useServiceCheck, type MaintenanceBannerProps, type ServiceCheck, } from "./react.js";
2
+ //# sourceMappingURL=react-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-index.d.ts","sourceRoot":"","sources":["../../src/maintenance/react-index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,EACf,KAAK,sBAAsB,EAC3B,KAAK,YAAY,GAClB,MAAM,YAAY,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { MaintenanceBanner, useMaintenanceState, useServiceCheck, } from "./react.js";
2
+ //# sourceMappingURL=react-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-index.js","sourceRoot":"","sources":["../../src/maintenance/react-index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,eAAe,GAGhB,MAAM,YAAY,CAAC"}
@@ -0,0 +1,36 @@
1
+ import * as React from "react";
2
+ import type { MaintenanceStateBody } from "./route.js";
3
+ /**
4
+ * The app's maintenance state, read on mount, when the tab comes back into view,
5
+ * and every five minutes — every 30 seconds in the quarter hour before a window.
6
+ * Null until the first answer (and when the route can't be reached).
7
+ */
8
+ export declare function useMaintenanceState({ statePath, }?: {
9
+ statePath?: string;
10
+ }): MaintenanceStateBody | null;
11
+ export interface MaintenanceBannerProps {
12
+ /** "AltEd" */
13
+ appName: string;
14
+ statePath?: string;
15
+ timeZone?: string;
16
+ className?: string;
17
+ }
18
+ /**
19
+ * A notice across the top of the page while a window is coming (dismissible;
20
+ * a moved window has a new id and shows again) or under way (not dismissible:
21
+ * the next page someone opens will be the maintenance page).
22
+ */
23
+ export declare function MaintenanceBanner({ appName, statePath, timeZone, className }: MaintenanceBannerProps): React.JSX.Element | null;
24
+ export type ServiceCheck = "checking" | "up" | "down" | "maintenance" | "unknown";
25
+ /**
26
+ * For an error page: is this a planned window, the database not answering, or a
27
+ * bug? In production an error page gets only a digest — never the message — so
28
+ * it can't tell from the error; it asks the state route to probe instead.
29
+ *
30
+ * `maintenance` and `down` deserve "temporarily unavailable, try again shortly";
31
+ * `up` and `unknown` the ordinary error page.
32
+ */
33
+ export declare function useServiceCheck({ statePath }?: {
34
+ statePath?: string;
35
+ }): ServiceCheck;
36
+ //# sourceMappingURL=react.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/maintenance/react.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,KAAK,EAAE,oBAAoB,EAA2B,MAAM,YAAY,CAAC;AAkChF;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,SAA8B,GAC/B,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,oBAAoB,GAAG,IAAI,CA2B3D;AAoBD,MAAM,WAAW,sBAAsB;IACrC,cAAc;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,EAAE,sBAAsB,4BA6CpG;AAED,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,IAAI,GAAG,MAAM,GAAG,aAAa,GAAG,SAAS,CAAC;AAElF;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,EAAE,SAA8B,EAAE,GAAE;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,YAAY,CAe7G"}
@@ -0,0 +1,141 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { describeWindow } from "./core.js";
5
+ /**
6
+ * The browser half: the planned-maintenance banner, and a check an error page
7
+ * can make. Both read the app's state route (`createMaintenanceRoute`), never
8
+ * the server's feed directly, so the app's override and timing rules apply and
9
+ * nothing here knows which servers exist.
10
+ *
11
+ * Drawn after the page loads, from a fetch: pages built ahead of time (a blog
12
+ * post, a guide) show the banner without being rebuilt, and nothing about it is
13
+ * in the server-rendered HTML to mismatch on hydration.
14
+ */
15
+ const DEFAULT_STATE_PATH = "/api/house/maintenance";
16
+ const NEAR_MS = 15 * 60_000;
17
+ function asWindow(w) {
18
+ return { ...w, endedAt: null, cancelled: false, affects: [], server: "" };
19
+ }
20
+ async function readState(statePath, probe = false) {
21
+ try {
22
+ const res = await fetch(probe ? `${statePath}?probe=1` : statePath, {
23
+ cache: "no-store",
24
+ headers: { accept: "application/json" },
25
+ });
26
+ if (!res.ok && res.status !== 503)
27
+ return null;
28
+ const body = (await res.json());
29
+ return body && typeof body.phase === "string" ? body : null;
30
+ }
31
+ catch {
32
+ return null;
33
+ }
34
+ }
35
+ /**
36
+ * The app's maintenance state, read on mount, when the tab comes back into view,
37
+ * and every five minutes — every 30 seconds in the quarter hour before a window.
38
+ * Null until the first answer (and when the route can't be reached).
39
+ */
40
+ export function useMaintenanceState({ statePath = DEFAULT_STATE_PATH, } = {}) {
41
+ const [state, setState] = React.useState(null);
42
+ React.useEffect(() => {
43
+ let cancelled = false;
44
+ let timer;
45
+ const load = async () => {
46
+ clearTimeout(timer);
47
+ const next = await readState(statePath);
48
+ if (cancelled)
49
+ return;
50
+ setState(next);
51
+ const startsIn = next?.window ? Date.parse(next.window.startsAt) - Date.now() : Infinity;
52
+ timer = setTimeout(load, next?.phase === "upcoming" && startsIn < NEAR_MS ? 30_000 : 300_000);
53
+ };
54
+ const onVisible = () => {
55
+ if (document.visibilityState === "visible")
56
+ void load();
57
+ };
58
+ void load();
59
+ document.addEventListener("visibilitychange", onVisible);
60
+ return () => {
61
+ cancelled = true;
62
+ clearTimeout(timer);
63
+ document.removeEventListener("visibilitychange", onVisible);
64
+ };
65
+ }, [statePath]);
66
+ return state;
67
+ }
68
+ const DISMISSED = "house:maintenance-dismissed:";
69
+ function wasDismissed(id) {
70
+ try {
71
+ return window.localStorage.getItem(DISMISSED + id) === "1";
72
+ }
73
+ catch {
74
+ return false;
75
+ }
76
+ }
77
+ function rememberDismissed(id) {
78
+ try {
79
+ window.localStorage.setItem(DISMISSED + id, "1");
80
+ }
81
+ catch {
82
+ // Private mode or blocked storage: dismissed for this page view only.
83
+ }
84
+ }
85
+ /**
86
+ * A notice across the top of the page while a window is coming (dismissible;
87
+ * a moved window has a new id and shows again) or under way (not dismissible:
88
+ * the next page someone opens will be the maintenance page).
89
+ */
90
+ export function MaintenanceBanner({ appName, statePath, timeZone, className }) {
91
+ const state = useMaintenanceState({ statePath });
92
+ const [dismissed, setDismissed] = React.useState(null);
93
+ const w = state?.window ?? null;
94
+ const id = w?.id ?? null;
95
+ React.useEffect(() => {
96
+ if (id && wasDismissed(id))
97
+ setDismissed(id);
98
+ }, [id]);
99
+ if (!state || !w || state.phase === "none")
100
+ return null;
101
+ const active = state.phase === "active";
102
+ if (!active && dismissed === w.id)
103
+ return null;
104
+ const words = describeWindow(asWindow(w), { timeZone });
105
+ const text = active
106
+ ? `Planned maintenance is under way. ${appName} may not load until ${words.end} ${words.zone}.`
107
+ : `Planned maintenance ${words.text}: ${appName} will be unavailable for ${words.expected}.`;
108
+ return (_jsx("div", { role: "status", className: ["border-b border-border bg-muted text-foreground", className].filter(Boolean).join(" "), children: _jsxs("div", { className: "mx-auto flex max-w-screen-xl items-start gap-3 px-4 py-2 text-sm", children: [_jsxs("p", { className: "min-w-0 flex-1 leading-relaxed", children: [_jsx("span", { className: "font-medium", children: text }), w.message ? _jsxs("span", { className: "text-muted-foreground", children: [" ", w.message] }) : null] }), active ? null : (_jsx("button", { type: "button", onClick: () => {
109
+ rememberDismissed(w.id);
110
+ setDismissed(w.id);
111
+ }, className: "-my-1 shrink-0 rounded-md px-2 py-1 text-muted-foreground hover:bg-background hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring", "aria-label": "Dismiss the maintenance notice", children: "\u2715" }))] }) }));
112
+ }
113
+ /**
114
+ * For an error page: is this a planned window, the database not answering, or a
115
+ * bug? In production an error page gets only a digest — never the message — so
116
+ * it can't tell from the error; it asks the state route to probe instead.
117
+ *
118
+ * `maintenance` and `down` deserve "temporarily unavailable, try again shortly";
119
+ * `up` and `unknown` the ordinary error page.
120
+ */
121
+ export function useServiceCheck({ statePath = DEFAULT_STATE_PATH } = {}) {
122
+ const [check, setCheck] = React.useState("checking");
123
+ React.useEffect(() => {
124
+ let cancelled = false;
125
+ void readState(statePath, true).then((body) => {
126
+ if (cancelled)
127
+ return;
128
+ if (!body)
129
+ setCheck("unknown");
130
+ else if (body.phase === "active")
131
+ setCheck("maintenance");
132
+ else
133
+ setCheck(body.service ?? "unknown");
134
+ });
135
+ return () => {
136
+ cancelled = true;
137
+ };
138
+ }, [statePath]);
139
+ return check;
140
+ }
141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AAK,CAAC,QAAQ,CAAe,UAAU,CAAC,CAAC;IACnE,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,SAAS,GAAG,KAAK,CAAC;QACtB,KAAK,SAAS,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE;YAC5C,IAAI,SAAS;gBAAE,OAAO;YACtB,IAAI,CAAC,IAAI;gBAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;iBAC1B,IAAI,IAAI,CAAC,KAAK,KAAK,QAAQ;gBAAE,QAAQ,CAAC,aAAa,CAAC,CAAC;;gBACrD,QAAQ,CAAC,IAAI,CAAC,OAAO,IAAI,SAAS,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,EAAE;YACV,SAAS,GAAG,IAAI,CAAC;QACnB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAChB,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,37 @@
1
+ import type { MaintenanceState, MaintenanceWindow } from "./core.js";
2
+ import type { MaintenanceGate } from "./gate.js";
3
+ /**
4
+ * `GET /api/house/maintenance`: the gate's state as JSON, for the browser — the
5
+ * banner reads it, and an error page asks it whether the database is reachable.
6
+ * Never touches the database unless asked to probe it.
7
+ */
8
+ /** What the browser is told about a window: no server names. */
9
+ export interface PublicMaintenanceWindow {
10
+ id: string;
11
+ kind: MaintenanceWindow["kind"];
12
+ startsAt: string;
13
+ endsBy: string;
14
+ expectedMinutes: number;
15
+ message: string | null;
16
+ }
17
+ export interface MaintenanceStateBody {
18
+ phase: MaintenanceState["phase"];
19
+ window: PublicMaintenanceWindow | null;
20
+ retryAfterSeconds: number | null;
21
+ /** Only with `?probe=1`: whether the app's own probe (a `SELECT 1`) answered. */
22
+ service?: "up" | "down";
23
+ }
24
+ export declare function publicWindow(window: MaintenanceWindow | null): PublicMaintenanceWindow | null;
25
+ export interface MaintenanceRouteOptions {
26
+ /** Answers when the database is reachable, throws (or hangs) when it isn't. Without it, `?probe=1` is ignored. */
27
+ probe?: () => Promise<unknown>;
28
+ /** Default 1500 ms. */
29
+ probeTimeoutMs?: number;
30
+ /** One probe answers every caller for this long, so the public route can't be used to load the database. Default 5 s. */
31
+ probeCacheSeconds?: number;
32
+ now?: () => number;
33
+ }
34
+ export declare function createMaintenanceRoute(gate: MaintenanceGate, options?: MaintenanceRouteOptions): {
35
+ GET: (request: Request) => Promise<Response>;
36
+ };
37
+ //# sourceMappingURL=route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/maintenance/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAEjD;;;;GAIG;AAEH,gEAAgE;AAChE,MAAM,WAAW,uBAAuB;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED,MAAM,WAAW,oBAAoB;IACnC,KAAK,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACjC,MAAM,EAAE,uBAAuB,GAAG,IAAI,CAAC;IACvC,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,iFAAiF;IACjF,OAAO,CAAC,EAAE,IAAI,GAAG,MAAM,CAAC;CACzB;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,iBAAiB,GAAG,IAAI,GAAG,uBAAuB,GAAG,IAAI,CAI7F;AAED,MAAM,WAAW,uBAAuB;IACtC,kHAAkH;IAClH,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/B,uBAAuB;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,yHAAyH;IACzH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,GAAE,uBAA4B;mBAwBrE,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC;EAcxD"}
@@ -0,0 +1,41 @@
1
+ export function publicWindow(window) {
2
+ if (!window)
3
+ return null;
4
+ const { id, kind, startsAt, endsBy, expectedMinutes, message } = window;
5
+ return { id, kind, startsAt, endsBy, expectedMinutes, message };
6
+ }
7
+ export function createMaintenanceRoute(gate, options = {}) {
8
+ const now = options.now ?? Date.now;
9
+ const timeoutMs = options.probeTimeoutMs ?? 1500;
10
+ const cacheMs = (options.probeCacheSeconds ?? 5) * 1000;
11
+ let last = null;
12
+ function probe() {
13
+ const at = now();
14
+ if (last && at - last.at < cacheMs)
15
+ return last.result;
16
+ const run = options.probe;
17
+ const result = new Promise((resolve) => {
18
+ const timer = setTimeout(() => resolve("down"), timeoutMs);
19
+ Promise.resolve()
20
+ .then(run)
21
+ .then(() => resolve("up"), () => resolve("down"))
22
+ .finally(() => clearTimeout(timer));
23
+ });
24
+ last = { at, result };
25
+ return result;
26
+ }
27
+ async function GET(request) {
28
+ const state = await gate.state();
29
+ const body = {
30
+ phase: state.phase,
31
+ window: publicWindow(state.window),
32
+ retryAfterSeconds: state.retryAfterSeconds,
33
+ };
34
+ if (options.probe && new URL(request.url).searchParams.get("probe") === "1") {
35
+ body.service = state.phase === "active" ? "down" : await probe();
36
+ }
37
+ return Response.json(body, { headers: { "cache-control": "no-store", "x-content-type-options": "nosniff" } });
38
+ }
39
+ return { GET };
40
+ }
41
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","sourceRoot":"","s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GAAG,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC;QACxC,CAAC,CAAC,CAAC;QACH,IAAI,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;QACtB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,KAAK,UAAU,GAAG,CAAC,OAAgB;QACjC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACjC,MAAM,IAAI,GAAyB;YACjC,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,MAAM,EAAE,YAAY,CAAC,KAAK,CAAC,MAAM,CAAC;YAClC,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;SAC3C,CAAC;QACF,IAAI,OAAO,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,GAAG,EAAE,CAAC;YAC5E,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,KAAK,EAAE,CAAC;QACnE,CAAC;QACD,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,EAAE,eAAe,EAAE,UAAU,EAAE,wBAAwB,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC;IAChH,CAAC;IAED,OAAO,EAAE,GAAG,EAAE,CAAC;AACjB,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Whether an error means "the database (or the file store) can't be reached
3
+ * right now", as opposed to a bug. A connection refused during a reboot, a pool
4
+ * that timed out, Postgres terminating connections as it shuts down.
5
+ *
6
+ * Errors arrive wrapped — Prisma's adapter puts the driver's error under
7
+ * `meta.driverAdapterError`, fetch puts the socket's under `cause`, the AWS SDK
8
+ * nests it again — so the walk follows those links a few levels down.
9
+ */
10
+ export declare function isUnavailableError(error: unknown, depth?: number): boolean;
11
+ //# sourceMappingURL=unavailable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unavailable.d.ts","sourceRoot":"","sources":["../../src/maintenance/unavailable.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA0CH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,SAAI,GAAG,OAAO,CAUrE"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Whether an error means "the database (or the file store) can't be reached
3
+ * right now", as opposed to a bug. A connection refused during a reboot, a pool
4
+ * that timed out, Postgres terminating connections as it shuts down.
5
+ *
6
+ * Errors arrive wrapped — Prisma's adapter puts the driver's error under
7
+ * `meta.driverAdapterError`, fetch puts the socket's under `cause`, the AWS SDK
8
+ * nests it again — so the walk follows those links a few levels down.
9
+ */
10
+ /** Prisma: can't reach the server, timed out, connection closed, pool timeout. */
11
+ const PRISMA_CODES = new Set(["P1001", "P1002", "P1017", "P2024"]);
12
+ /**
13
+ * SQLSTATE: admin/crash shutdown, cannot connect now (starting or stopping),
14
+ * the connection-exception class, too many connections.
15
+ */
16
+ const SQLSTATES = new Set(["57P01", "57P02", "57P03", "08000", "08001", "08003", "08004", "08006", "53300"]);
17
+ /** Node's socket errors. */
18
+ const ERRNOS = new Set([
19
+ "ECONNREFUSED",
20
+ "ECONNRESET",
21
+ "ETIMEDOUT",
22
+ "EHOSTUNREACH",
23
+ "ENETUNREACH",
24
+ "EPIPE",
25
+ "EAI_AGAIN",
26
+ "UND_ERR_CONNECT_TIMEOUT",
27
+ "UND_ERR_SOCKET",
28
+ ]);
29
+ const MESSAGES = [
30
+ /connection terminated unexpectedly/i,
31
+ /timeout exceeded when trying to connect/i,
32
+ /terminating connection due to administrator command/i,
33
+ /the database system is (starting up|shutting down)/i,
34
+ /can't reach database server/i,
35
+ /connection (refused|reset)/i,
36
+ ];
37
+ function children(error) {
38
+ const meta = error.meta;
39
+ const adapter = meta?.driverAdapterError;
40
+ return [
41
+ error.cause,
42
+ error.originalError,
43
+ adapter,
44
+ adapter?.cause,
45
+ ...(Array.isArray(error.errors) ? error.errors : []),
46
+ ];
47
+ }
48
+ export function isUnavailableError(error, depth = 0) {
49
+ if (depth > 6 || typeof error !== "object" || error === null)
50
+ return false;
51
+ const e = error;
52
+ for (const key of ["code", "errno", "originalCode"]) {
53
+ const v = e[key];
54
+ if (typeof v === "string" && (PRISMA_CODES.has(v) || SQLSTATES.has(v) || ERRNOS.has(v)))
55
+ return true;
56
+ }
57
+ if (e.kind === "DatabaseNotReachable" || e.kind === "ConnectionClosed" || e.kind === "SocketTimeout")
58
+ return true;
59
+ if (typeof e.message === "string" && MESSAGES.some((re) => re.test(e.message)))
60
+ return true;
61
+ return children(e).some((child) => child !== undefined && isUnavailableError(child, depth + 1));
62
+ }
63
+ //# sourceMappingURL=unavailable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unavailable.js","sourceRoot":"","sources":["../../src/maintenance/unavailable.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,kFAAkF;AAClF,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;AACnE;;;GAGG;AACH,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;AAC7G,4BAA4B;AAC5B,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC;IACrB,cAAc;IACd,YAAY;IACZ,WAAW;IACX,cAAc;IACd,aAAa;IACb,OAAO;IACP,WAAW;IACX,yBAAyB;IACzB,gBAAgB;CACjB,CAAC,CAAC;AACH,MAAM,QAAQ,GAAG;IACf,qCAAqC;IACrC,0CAA0C;IAC1C,sDAAsD;IACtD,qDAAqD;IACrD,8BAA8B;IAC9B,6BAA6B;CAC9B,CAAC;AA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