@connextar/house 0.23.0 → 0.23.3
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/README.md +27 -3
- package/dist/analytics/browser.d.ts +1 -0
- package/dist/analytics/browser.d.ts.map +1 -1
- package/dist/analytics/browser.js +20 -3
- package/dist/analytics/browser.js.map +1 -1
- package/dist/analytics/config.d.ts +5 -0
- package/dist/analytics/config.d.ts.map +1 -1
- package/dist/analytics/config.js +4 -1
- package/dist/analytics/config.js.map +1 -1
- package/dist/analytics/react.d.ts.map +1 -1
- package/dist/analytics/react.js +4 -2
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics/redact.d.ts.map +1 -1
- package/dist/analytics/redact.js +6 -1
- package/dist/analytics/redact.js.map +1 -1
- package/dist/rate-limit/prisma-index.d.ts +1 -1
- package/dist/rate-limit/prisma-index.d.ts.map +1 -1
- package/dist/rate-limit/prisma-index.js.map +1 -1
- package/dist/rate-limit/prisma.d.ts +10 -1
- package/dist/rate-limit/prisma.d.ts.map +1 -1
- package/dist/rate-limit/prisma.js +2 -1
- package/dist/rate-limit/prisma.js.map +1 -1
- package/package.json +5 -2
- package/src/account/data-contract.ts +112 -0
- package/src/account/index.ts +11 -0
- package/src/admin/access.ts +267 -0
- package/src/admin/audit.ts +238 -0
- package/src/admin/bootstrap.ts +220 -0
- package/src/admin/client-index.ts +19 -0
- package/src/admin/client.tsx +187 -0
- package/src/admin/index.ts +91 -0
- package/src/admin/proof.ts +155 -0
- package/src/admin/second-factor-index.ts +16 -0
- package/src/admin/second-factor.ts +403 -0
- package/src/admin/testing-index.ts +13 -0
- package/src/admin/testing.ts +299 -0
- package/src/admin/view-as.ts +511 -0
- package/src/ai/config-index.ts +40 -0
- package/src/ai/config.ts +176 -0
- package/src/ai/images.ts +18 -0
- package/src/ai/index.ts +21 -0
- package/src/ai/pricing.ts +107 -0
- package/src/ai/provider.ts +403 -0
- package/src/analytics/browser.ts +400 -0
- package/src/analytics/client-index.ts +12 -0
- package/src/analytics/config.ts +89 -0
- package/src/analytics/index.ts +17 -0
- package/src/analytics/ingest.ts +81 -0
- package/src/analytics/react-index.ts +16 -0
- package/src/analytics/react.tsx +241 -0
- package/src/analytics/redact.ts +146 -0
- package/src/analytics/server-index.ts +17 -0
- package/src/analytics/server.ts +277 -0
- package/src/auth/index.ts +30 -0
- package/src/auth/second-factor-index.ts +16 -0
- package/src/auth/second-factor-next.ts +136 -0
- package/src/auth/second-factor-routes.ts +40 -0
- package/src/auth/second-factor.ts +226 -0
- package/src/auth/step-up-client-index.ts +13 -0
- package/src/auth/step-up-client.ts +23 -0
- package/src/auth/step-up-index.ts +19 -0
- package/src/auth/step-up-routes.ts +99 -0
- package/src/auth/step-up.ts +432 -0
- package/src/auth/two-factor-policy.ts +112 -0
- package/src/changelog/changelog.tsx +367 -0
- package/src/changelog/index.ts +12 -0
- package/src/editor/block-handle.tsx +312 -0
- package/src/editor/blocks.ts +169 -0
- package/src/editor/bubble-toolbar.tsx +208 -0
- package/src/editor/collapsible.ts +159 -0
- package/src/editor/content-index.ts +1 -0
- package/src/editor/content.tsx +343 -0
- package/src/editor/editor.tsx +333 -0
- package/src/editor/emoji-index.ts +1 -0
- package/src/editor/emoji-picker.tsx +114 -0
- package/src/editor/emoji.ts +436 -0
- package/src/editor/index.ts +6 -0
- package/src/editor/sections.ts +83 -0
- package/src/editor/suggestion-popup.tsx +136 -0
- package/src/editor/suggestions.ts +309 -0
- package/src/editor/ui.tsx +171 -0
- package/src/errors/error-shell.tsx +72 -0
- package/src/errors/global-error-shell.tsx +116 -0
- package/src/errors/index.ts +2 -0
- package/src/grid/columns.ts +48 -0
- package/src/grid/compare.ts +35 -0
- package/src/grid/hidden-columns.ts +95 -0
- package/src/grid/index.ts +27 -0
- package/src/grid/use-grid.ts +282 -0
- package/src/grid/views.ts +70 -0
- package/src/guides/guide-article.tsx +92 -0
- package/src/guides/guide-figure-zoom.tsx +533 -0
- package/src/guides/guide-figure.tsx +68 -0
- package/src/guides/guide-toc.tsx +133 -0
- package/src/guides/guides-landing.tsx +57 -0
- package/src/guides/guides-search.tsx +160 -0
- package/src/guides/guides-shell.tsx +60 -0
- package/src/guides/guides-sidebar.tsx +93 -0
- package/src/guides/index.ts +28 -0
- package/src/guides/library.ts +165 -0
- package/src/health/health.ts +115 -0
- package/src/health/index.ts +11 -0
- package/src/internal/cx.ts +4 -0
- package/src/internal/next-link.ts +23 -0
- package/src/jobs/counts.ts +151 -0
- package/src/jobs/cron-auth.ts +60 -0
- package/src/jobs/health.ts +315 -0
- package/src/jobs/idle.ts +86 -0
- package/src/jobs/index.ts +120 -0
- package/src/jobs/job-health-list.tsx +316 -0
- package/src/jobs/next-index.ts +4 -0
- package/src/jobs/next.ts +65 -0
- package/src/jobs/prisma-index.ts +12 -0
- package/src/jobs/prisma.ts +170 -0
- package/src/jobs/prune.ts +79 -0
- package/src/jobs/react-index.ts +35 -0
- package/src/jobs/registry.ts +189 -0
- package/src/jobs/scheduler.ts +437 -0
- package/src/jobs/store.ts +212 -0
- package/src/jobs/windows.ts +157 -0
- package/src/maintenance/core.ts +270 -0
- package/src/maintenance/gate.ts +171 -0
- package/src/maintenance/index.ts +40 -0
- package/src/maintenance/page.ts +76 -0
- package/src/maintenance/proxy.ts +88 -0
- package/src/maintenance/react-index.ts +7 -0
- package/src/maintenance/react.tsx +190 -0
- package/src/maintenance/route.ts +82 -0
- package/src/maintenance/unavailable.ts +61 -0
- package/src/markdown/figure.tsx +132 -0
- package/src/markdown/figures.ts +237 -0
- package/src/markdown/guide.tsx +536 -0
- package/src/markdown/index.ts +53 -0
- package/src/markdown/table-view.tsx +74 -0
- package/src/markdown/table.ts +90 -0
- package/src/password/common.ts +58 -0
- package/src/password/estimate-index.ts +18 -0
- package/src/password/estimate.ts +715 -0
- package/src/password/index.ts +40 -0
- package/src/password/password-input.tsx +116 -0
- package/src/password/password-strength.tsx +157 -0
- package/src/password/reveal.ts +151 -0
- package/src/rate-limit/better-auth.ts +94 -0
- package/src/rate-limit/index.ts +32 -0
- package/src/rate-limit/keys.ts +110 -0
- package/src/rate-limit/limiter.ts +276 -0
- package/src/rate-limit/prisma-index.ts +12 -0
- package/src/rate-limit/prisma.ts +157 -0
- package/src/rate-limit/store.ts +47 -0
- package/src/release/changelog.ts +364 -0
- package/src/release/index.ts +32 -0
- package/src/release/version.ts +64 -0
- package/src/setup-guide/bits.tsx +420 -0
- package/src/setup-guide/card.tsx +358 -0
- package/src/setup-guide/class-names.ts +144 -0
- package/src/setup-guide/context.tsx +239 -0
- package/src/setup-guide/guide.ts +271 -0
- package/src/setup-guide/index.ts +76 -0
- package/src/setup-guide/launcher.tsx +207 -0
- package/src/setup-guide/strip.tsx +127 -0
- package/src/styles/house.css +384 -0
- package/src/trail/index.ts +12 -0
- package/src/trail/rules.ts +84 -0
- package/src/trail/trail.tsx +367 -0
- package/src/ux/errors.ts +32 -0
- package/src/ux/feedback.tsx +41 -0
- package/src/ux/index.ts +44 -0
- package/src/ux/navigation-index.ts +16 -0
- package/src/ux/navigation-progress.tsx +270 -0
- package/src/ux/optimistic.ts +415 -0
- package/src/ux/use-action.ts +155 -0
- package/src/ux/use-optimistic-list.ts +227 -0
- package/src/ux/use-optimistic-value.ts +136 -0
- package/src/web-search/index.ts +13 -0
- package/src/web-search/providers.ts +177 -0
- package/src/web-search/types.ts +98 -0
- package/src/whats-new/body.tsx +190 -0
- package/src/whats-new/index.ts +43 -0
- package/src/whats-new/seen.ts +40 -0
- package/src/whats-new/select.ts +230 -0
- package/src/whats-new/use-whats-new.ts +109 -0
- package/src/wizard/index.ts +61 -0
- package/src/wizard/invalid.ts +152 -0
- package/src/wizard/react-hook-form-index.ts +11 -0
- package/src/wizard/rhf-adapter.ts +67 -0
- package/src/wizard/rules.ts +137 -0
- package/src/wizard/wizard.tsx +442 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape a help centre is drawn from: the guides one reader may open, grouped into
|
|
3
|
+
* categories, and a small search index of their titles, summaries and section headings.
|
|
4
|
+
*
|
|
5
|
+
* Each app builds it on the server for one reader — the public, or someone signed in — so a
|
|
6
|
+
* guide that reader can't open is never sent to the browser. It is plain data, so it crosses
|
|
7
|
+
* into the client components (`GuidesShell`, `GuidesSearch`, `GuidesSidebar`) as props.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface GuideNavItem {
|
|
11
|
+
slug: string;
|
|
12
|
+
title: string;
|
|
13
|
+
/** One or two sentences, shown on landing cards and searched. */
|
|
14
|
+
summary: string;
|
|
15
|
+
/** Where the guide opens — `/guides/<slug>`, `/dashboard/guides/<slug>`, or anywhere else. */
|
|
16
|
+
href: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface GuideNavCategory {
|
|
20
|
+
id: string;
|
|
21
|
+
title: string;
|
|
22
|
+
/** One line under the category's heading on a landing page. */
|
|
23
|
+
summary: string;
|
|
24
|
+
items: GuideNavItem[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface GuideSearchEntry {
|
|
28
|
+
href: string;
|
|
29
|
+
title: string;
|
|
30
|
+
summary: string;
|
|
31
|
+
category: string;
|
|
32
|
+
/** The guide's meaningful `##`/`###` headings, so a search can land on a section. */
|
|
33
|
+
sections: { id: string; text: string }[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface GuideLibrary {
|
|
37
|
+
nav: GuideNavCategory[];
|
|
38
|
+
search: GuideSearchEntry[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface GuideSearchResult {
|
|
42
|
+
href: string;
|
|
43
|
+
title: string;
|
|
44
|
+
detail: string;
|
|
45
|
+
/** A section within a guide rather than the guide itself. */
|
|
46
|
+
section: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The category a guide sits in, for the eyebrow above its title. */
|
|
50
|
+
export function categoryOf(library: GuideLibrary, slug: string): GuideNavCategory | undefined {
|
|
51
|
+
return library.nav.find((category) => category.items.some((item) => item.slug === slug));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Headings nearly every guide has, which would only crowd a search. An app building its index
|
|
56
|
+
* can drop them with `meaningfulSections`.
|
|
57
|
+
*/
|
|
58
|
+
export const BOILERPLATE_HEADINGS =
|
|
59
|
+
/^(what it does|where to find it|step by step|what each screen shows|tips|tips\s*(?:&|and)\s*good to know|troubleshooting|related guides)$/i;
|
|
60
|
+
|
|
61
|
+
/** A guide's headings without the boilerplate ones, ready for `GuideSearchEntry.sections`. */
|
|
62
|
+
export function meaningfulSections(headings: readonly { id: string; text: string }[]): { id: string; text: string }[] {
|
|
63
|
+
return headings.filter((h) => !BOILERPLATE_HEADINGS.test(h.text)).map(({ id, text }) => ({ id, text }));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Search a library's index. Every word of the query must appear in a result's title or detail;
|
|
68
|
+
* guides come before sections. An empty query lists every guide.
|
|
69
|
+
*/
|
|
70
|
+
export function searchGuides(index: readonly GuideSearchEntry[], rawQuery: string, limit = 20): GuideSearchResult[] {
|
|
71
|
+
const query = rawQuery.trim().toLowerCase();
|
|
72
|
+
const guides = index.map((guide) => ({
|
|
73
|
+
href: guide.href,
|
|
74
|
+
title: guide.title,
|
|
75
|
+
detail: `${guide.category} · ${guide.summary}`,
|
|
76
|
+
section: false,
|
|
77
|
+
}));
|
|
78
|
+
if (!query) return guides;
|
|
79
|
+
const terms = query.split(/\s+/);
|
|
80
|
+
const matches = (value: string) => terms.every((term) => value.toLowerCase().includes(term));
|
|
81
|
+
const sections = index.flatMap((guide) =>
|
|
82
|
+
guide.sections.map((section) => ({
|
|
83
|
+
href: `${guide.href}#${section.id}`,
|
|
84
|
+
title: section.text,
|
|
85
|
+
detail: guide.title,
|
|
86
|
+
section: true,
|
|
87
|
+
})),
|
|
88
|
+
);
|
|
89
|
+
return [...guides, ...sections].filter((entry) => matches(`${entry.title} ${entry.detail}`)).slice(0, limit);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Every word the guides frame shows, in UK English by default. */
|
|
93
|
+
export interface GuidesLabels {
|
|
94
|
+
searchTrigger: string;
|
|
95
|
+
searchPlaceholder: string;
|
|
96
|
+
searchTitle: string;
|
|
97
|
+
searchAll: string;
|
|
98
|
+
searchMatches: string;
|
|
99
|
+
/** Said when nothing matched; `{query}` is replaced with what was typed. */
|
|
100
|
+
searchEmpty: string;
|
|
101
|
+
filterLabel: string;
|
|
102
|
+
filterPlaceholder: string;
|
|
103
|
+
/** Said when the filter hides every guide; `{query}` is replaced. */
|
|
104
|
+
filterEmpty: string;
|
|
105
|
+
menu: string;
|
|
106
|
+
onThisPage: string;
|
|
107
|
+
allGuides: string;
|
|
108
|
+
// The zoom's words came in 0.18.0, after this type first shipped, so each is optional: an app that writes out
|
|
109
|
+
// every label (a translation) keeps typechecking, and gets the house's word for any it leaves out.
|
|
110
|
+
/** The hint under a picture in a guide that opens it full-size, where the pointer is a mouse or a trackpad. */
|
|
111
|
+
zoomHint?: string;
|
|
112
|
+
/**
|
|
113
|
+
* The same hint on a touch screen (`@media (pointer: coarse)`). Left out, it is `zoomHint` when the
|
|
114
|
+
* app set that, else "Tap to enlarge".
|
|
115
|
+
*/
|
|
116
|
+
zoomHintTouch?: string;
|
|
117
|
+
/** The name of that button; `{alt}` is replaced with the picture's alt text. */
|
|
118
|
+
zoomOpen?: string;
|
|
119
|
+
zoomIn?: string;
|
|
120
|
+
zoomOut?: string;
|
|
121
|
+
/** The button showing the zoom level, which switches between fit and full size. */
|
|
122
|
+
zoomToggle?: string;
|
|
123
|
+
/** Read out as the zoom changes; `{percent}` is replaced. */
|
|
124
|
+
zoomLevel?: string;
|
|
125
|
+
zoomClose?: string;
|
|
126
|
+
/** Shown above the picture on wider screens. */
|
|
127
|
+
zoomInstructions?: string;
|
|
128
|
+
/**
|
|
129
|
+
* On a phone, the button under an enlarged picture that shows its description (the alt text), which
|
|
130
|
+
* waits behind it there so the picture keeps the screen. Optional: added in 0.19.1.
|
|
131
|
+
*/
|
|
132
|
+
zoomShowDescription?: string;
|
|
133
|
+
/** The same button once the description is showing. Optional: added in 0.19.1. */
|
|
134
|
+
zoomHideDescription?: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const GUIDES_LABELS: Required<GuidesLabels> = {
|
|
138
|
+
searchTrigger: "Search the guides…",
|
|
139
|
+
searchPlaceholder: "Search guides and sections…",
|
|
140
|
+
searchTitle: "Search the guides",
|
|
141
|
+
searchAll: "All guides",
|
|
142
|
+
searchMatches: "Matches",
|
|
143
|
+
searchEmpty: "Nothing matched “{query}”. Try another word.",
|
|
144
|
+
filterLabel: "Filter guides",
|
|
145
|
+
filterPlaceholder: "Filter guides…",
|
|
146
|
+
filterEmpty: "No guide matches “{query}”.",
|
|
147
|
+
menu: "Guides",
|
|
148
|
+
onThisPage: "On this page",
|
|
149
|
+
allGuides: "All guides",
|
|
150
|
+
zoomHint: "Click to enlarge",
|
|
151
|
+
zoomHintTouch: "Tap to enlarge",
|
|
152
|
+
zoomOpen: "Enlarge picture: {alt}",
|
|
153
|
+
zoomIn: "Zoom in",
|
|
154
|
+
zoomOut: "Zoom out",
|
|
155
|
+
zoomToggle: "Switch between fit and full size",
|
|
156
|
+
zoomLevel: "Zoom {percent}%",
|
|
157
|
+
zoomClose: "Close",
|
|
158
|
+
zoomInstructions: "Pinch or ⌘/Ctrl + scroll to zoom · drag to move · double-click for full size",
|
|
159
|
+
zoomShowDescription: "Show description",
|
|
160
|
+
zoomHideDescription: "Hide description",
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export function withQuery(label: string, query: string): string {
|
|
164
|
+
return label.replace("{query}", query.trim());
|
|
165
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A liveness check that says which release is running: `GET /healthz` answers
|
|
3
|
+
* `ok 0.24.1 2c9a3cf`, so a deploy can be confirmed from outside with one curl.
|
|
4
|
+
*
|
|
5
|
+
* Lifted from PharmaLineAI's voice relay (v0.24.1), generalised for the Next.js
|
|
6
|
+
* apps on Vercel and anything on Render. The endpoint is public, so it says only
|
|
7
|
+
* a version and a short commit hash, and only when each looks like one: a value
|
|
8
|
+
* that came from the environment is never echoed as-is. It always answers 200
|
|
9
|
+
* with `ok` first, which is what a host's health check looks for.
|
|
10
|
+
*
|
|
11
|
+
* Liveness only: no database, no provider, nothing that can be slow or fail. A
|
|
12
|
+
* readiness check is a separate route of the app's own (see the README).
|
|
13
|
+
*
|
|
14
|
+
* Imports no package: the route handler is plain web `Request` and `Response`,
|
|
15
|
+
* which is what a Next route handler takes and returns.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The release and commit a running app was built from. Either may be unknown. */
|
|
19
|
+
export interface BuildInfo {
|
|
20
|
+
version?: string;
|
|
21
|
+
commit?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** MAJOR.MINOR.PATCH with an optional pre-release (`1.2.3-rc.1`). No `v`, no build metadata. */
|
|
25
|
+
const SEMVER = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
26
|
+
/** A git object name: 7 to 64 hex digits (SHA-1 or SHA-256), nothing else. */
|
|
27
|
+
const HEX_HASH = /^[0-9a-f]{7,64}$/i;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Where each host puts the commit it deployed, in the order they are read:
|
|
31
|
+
* Vercel (a system environment variable, exposed by default), Render, then a
|
|
32
|
+
* `GIT_COMMIT` an app or another host sets itself.
|
|
33
|
+
*/
|
|
34
|
+
export const COMMIT_ENV_VARS = ["VERCEL_GIT_COMMIT_SHA", "RENDER_GIT_COMMIT", "GIT_COMMIT"] as const;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The build from a version (package.json's, passed in) and the commit the host
|
|
38
|
+
* deployed, cut to 7. A version that isn't semver-shaped is dropped; the commit
|
|
39
|
+
* is the first of `COMMIT_ENV_VARS` that is a whole hex hash, so a variable set
|
|
40
|
+
* to something else is skipped rather than repeated on a public page.
|
|
41
|
+
*/
|
|
42
|
+
export function buildInfo(version: unknown, env: Record<string, string | undefined> = process.env): BuildInfo {
|
|
43
|
+
const v = typeof version === "string" ? version.trim() : "";
|
|
44
|
+
const commit = COMMIT_ENV_VARS.map((name) => (env[name] ?? "").trim()).find((value) => HEX_HASH.test(value));
|
|
45
|
+
return {
|
|
46
|
+
...(SEMVER.test(v) ? { version: v } : {}),
|
|
47
|
+
...(commit ? { commit: commit.slice(0, 7).toLowerCase() } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What the check says: `ok`, then the version and commit when known — `ok 1.2.3 abc1234`. */
|
|
52
|
+
export function healthText(build: BuildInfo = {}): string {
|
|
53
|
+
return ["ok", build.version, build.commit].filter(Boolean).join(" ");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type HealthFormat = "text" | "json";
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The format a request asks for: `?format=json` or `?format=text` first, then an
|
|
60
|
+
* `Accept` header that names `application/json`. Anything else — a browser, curl's
|
|
61
|
+
* `*\/*`, a host's checker — gets text.
|
|
62
|
+
*/
|
|
63
|
+
export function healthFormat(request: Request): HealthFormat {
|
|
64
|
+
const asked = new URL(request.url).searchParams.get("format")?.trim().toLowerCase();
|
|
65
|
+
if (asked === "json" || asked === "text") return asked;
|
|
66
|
+
const accept = (request.headers.get("accept") ?? "").toLowerCase();
|
|
67
|
+
return /(^|[\s,])application\/json(\s*;|\s*,|\s*$)/.test(accept) ? "json" : "text";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const HEADERS = {
|
|
71
|
+
// A health check must never be answered from a cache: it would say a deploy is live that isn't.
|
|
72
|
+
"cache-control": "no-store",
|
|
73
|
+
"x-content-type-options": "nosniff",
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** The answer itself: 200, `text/plain` by default, `{ status: "ok", version, commit }` as JSON. */
|
|
77
|
+
export function healthResponse(build: BuildInfo = {}, { format = "text" }: { format?: HealthFormat } = {}): Response {
|
|
78
|
+
if (format === "json") {
|
|
79
|
+
const body = {
|
|
80
|
+
status: "ok",
|
|
81
|
+
...(build.version ? { version: build.version } : {}),
|
|
82
|
+
...(build.commit ? { commit: build.commit } : {}),
|
|
83
|
+
};
|
|
84
|
+
return new Response(JSON.stringify(body), {
|
|
85
|
+
status: 200,
|
|
86
|
+
headers: { ...HEADERS, "content-type": "application/json; charset=utf-8" },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return new Response(healthText(build), {
|
|
90
|
+
status: 200,
|
|
91
|
+
headers: { ...HEADERS, "content-type": "text/plain; charset=utf-8" },
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface HealthRouteOptions {
|
|
96
|
+
/** The app's release: `pkg.version` from its own package.json, imported in the route file. */
|
|
97
|
+
version: unknown;
|
|
98
|
+
/** Where the commit is read from. Default: `process.env`, read on each request. */
|
|
99
|
+
env?: Record<string, string | undefined>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A Next route handler for `app/healthz/route.ts`, where `pkg` is the app's own package.json
|
|
104
|
+
* imported in that file (a route file is server-only, so it never reaches the browser):
|
|
105
|
+
*
|
|
106
|
+
* export const { GET } = createHealthRoute({ version: pkg.version });
|
|
107
|
+
*
|
|
108
|
+
* Next answers `HEAD` from `GET` on its own.
|
|
109
|
+
*/
|
|
110
|
+
export function createHealthRoute({ version, env }: HealthRouteOptions): { GET: (request: Request) => Response } {
|
|
111
|
+
return {
|
|
112
|
+
GET: (request: Request) =>
|
|
113
|
+
healthResponse(buildInfo(version, env ?? process.env), { format: healthFormat(request) }),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import NextLink from "next/link.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `next/link`, typed as the component it is.
|
|
5
|
+
*
|
|
6
|
+
* next/link is CommonJS, and Next ends that file with
|
|
7
|
+
* `module.exports = exports.default`, so a default import is the component under
|
|
8
|
+
* a bundler and under Node's own ESM loader (Vitest) alike. Only TypeScript
|
|
9
|
+
* disagrees: under `nodenext` it types a CommonJS default import as the whole
|
|
10
|
+
* module. The cast fixes the type and nothing else.
|
|
11
|
+
*
|
|
12
|
+
* It must stay a cast. In a server component the import is a client reference,
|
|
13
|
+
* and reading any property of one — `.default` included — throws "You cannot
|
|
14
|
+
* dot into a client module from a server component" and fails the page's
|
|
15
|
+
* prerender. `next-link.test.ts` holds that line.
|
|
16
|
+
*
|
|
17
|
+
* The specifier carries `.js` because `next` publishes no exports map. Next
|
|
18
|
+
* aliases the fully specified path to the App Router's Link exactly as it does
|
|
19
|
+
* the bare one (see its build/create-compiler-aliases.js).
|
|
20
|
+
*/
|
|
21
|
+
type LinkComponent = typeof NextLink.default;
|
|
22
|
+
|
|
23
|
+
export const Link = NextLink as unknown as LinkComponent;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a run did, in words a person reads.
|
|
3
|
+
*
|
|
4
|
+
* Both apps stored a job's result as `Record<string, number>` and printed it
|
|
5
|
+
* key by key, so production showed "0 chargesRaised · 0 totalOutstanding" —
|
|
6
|
+
* a variable name where a sentence belonged, and an amount of money printed as
|
|
7
|
+
* if it were a number of things. The fix is not a better printer. It is that a
|
|
8
|
+
* count cannot reach the screen without having been declared: every key has
|
|
9
|
+
* the words that follow its number, and a kind that says how the number is
|
|
10
|
+
* written. An undeclared key is not shown at all, so there is no fallback for
|
|
11
|
+
* a raw name to slip through.
|
|
12
|
+
*
|
|
13
|
+
* No imports: this runs in a server component, a route and a client card alike.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export type CountKind = "count" | "money" | "duration";
|
|
17
|
+
|
|
18
|
+
/** The words after the number: one form, or a singular and a plural. */
|
|
19
|
+
export type CountLabel = string | { one: string; other: string };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* One thing a job reports. The label is what follows the number —
|
|
23
|
+
* "reminders sent", so the line reads "3 reminders sent".
|
|
24
|
+
*
|
|
25
|
+
* Money says its currency **and its unit**, because the two apps already
|
|
26
|
+
* disagree: AltEd's `totalOutstanding` is pounds, summed from a decimal column,
|
|
27
|
+
* while a new job would count pence. A declaration that left the unit to a
|
|
28
|
+
* default would be out by a factor of a hundred for one of them.
|
|
29
|
+
*/
|
|
30
|
+
export type CountDeclaration =
|
|
31
|
+
| { label: CountLabel; kind?: "count" }
|
|
32
|
+
| { label: CountLabel; kind: "money"; currency: string; unit: "minor" | "major" }
|
|
33
|
+
| { label: CountLabel; kind: "duration"; unit: "ms" | "s" };
|
|
34
|
+
|
|
35
|
+
export type CountDeclarations = Readonly<Record<string, CountDeclaration>>;
|
|
36
|
+
|
|
37
|
+
/** What a job's `run` resolves with: a number for every key it declared. */
|
|
38
|
+
export type CountValues<C extends CountDeclarations> = { [K in keyof C]: number };
|
|
39
|
+
|
|
40
|
+
/** One count, ready to show. `text` is the whole phrase: "£1,204.50 outstanding". */
|
|
41
|
+
export interface CountLine {
|
|
42
|
+
key: string;
|
|
43
|
+
kind: CountKind;
|
|
44
|
+
value: number;
|
|
45
|
+
text: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface CountFormatOptions {
|
|
49
|
+
/** BCP 47 locale for numbers and currency. Default `en-GB`. */
|
|
50
|
+
locale?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const DEFAULT_LOCALE = "en-GB";
|
|
54
|
+
|
|
55
|
+
function labelFor(label: CountLabel, value: number, locale: string): string {
|
|
56
|
+
if (typeof label === "string") return label;
|
|
57
|
+
return new Intl.PluralRules(locale).select(value) === "one" ? label.one : label.other;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** An amount of money in its currency's own style. Float noise (12.300000000000001) is rounded away here. */
|
|
61
|
+
export function formatMoney(value: number, currency: string, unit: "minor" | "major", locale = DEFAULT_LOCALE) {
|
|
62
|
+
const format = new Intl.NumberFormat(locale, { style: "currency", currency });
|
|
63
|
+
const digits = format.resolvedOptions().maximumFractionDigits ?? 2;
|
|
64
|
+
return format.format(unit === "minor" ? value / 10 ** digits : value);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const SECOND = 1000;
|
|
68
|
+
const MINUTE = 60 * SECOND;
|
|
69
|
+
const HOUR = 60 * MINUTE;
|
|
70
|
+
const DAY = 24 * HOUR;
|
|
71
|
+
|
|
72
|
+
/** How long something took, compactly: "450 ms", "2.5 s", "2 min 5 s", "1 h 3 min", "2 days 4 h". */
|
|
73
|
+
export function formatDuration(ms: number, locale = DEFAULT_LOCALE): string {
|
|
74
|
+
const n = (value: number, digits = 0) =>
|
|
75
|
+
new Intl.NumberFormat(locale, { maximumFractionDigits: digits }).format(value);
|
|
76
|
+
const abs = Math.abs(ms);
|
|
77
|
+
if (abs < SECOND) return `${n(ms)} ms`;
|
|
78
|
+
if (abs < 10 * SECOND) return `${n(ms / SECOND, 1)} s`;
|
|
79
|
+
if (abs < MINUTE) return `${n(Math.round(ms / SECOND))} s`;
|
|
80
|
+
const pair = (big: number, bigUnit: string, small: number, smallUnit: string) =>
|
|
81
|
+
small ? `${n(big)} ${bigUnit} ${n(small)} ${smallUnit}` : `${n(big)} ${bigUnit}`;
|
|
82
|
+
const whole = Math.round(abs / SECOND) * SECOND;
|
|
83
|
+
if (whole < HOUR) return pair(Math.floor(whole / MINUTE), "min", Math.round((whole % MINUTE) / SECOND), "s");
|
|
84
|
+
if (whole < DAY) return pair(Math.floor(whole / HOUR), "h", Math.round((whole % HOUR) / MINUTE), "min");
|
|
85
|
+
const days = Math.floor(whole / DAY);
|
|
86
|
+
return pair(days, days === 1 ? "day" : "days", Math.round((whole % DAY) / HOUR), "h");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A threshold in the unit a person would say it in: "26 hours", "8 days",
|
|
91
|
+
* "90 minutes". For "no successful run in over 26 hours", where "1 day 2 h"
|
|
92
|
+
* would read as a stopwatch.
|
|
93
|
+
*/
|
|
94
|
+
export function describeSpan(ms: number): string {
|
|
95
|
+
const unit = (value: number, one: string, many: string) => `${value} ${value === 1 ? one : many}`;
|
|
96
|
+
if (ms >= 2 * DAY && ms % DAY === 0) return unit(ms / DAY, "day", "days");
|
|
97
|
+
if (ms >= HOUR && ms % HOUR === 0) return unit(ms / HOUR, "hour", "hours");
|
|
98
|
+
if (ms >= MINUTE && ms % MINUTE === 0) return unit(ms / MINUTE, "minute", "minutes");
|
|
99
|
+
return formatDuration(ms);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** One declared count as its phrase. */
|
|
103
|
+
export function formatCount(value: number, declaration: CountDeclaration, options: CountFormatOptions = {}): string {
|
|
104
|
+
const locale = options.locale ?? DEFAULT_LOCALE;
|
|
105
|
+
const label = labelFor(declaration.label, value, locale);
|
|
106
|
+
switch (declaration.kind) {
|
|
107
|
+
case "money":
|
|
108
|
+
return `${formatMoney(value, declaration.currency, declaration.unit, locale)} ${label}`;
|
|
109
|
+
case "duration":
|
|
110
|
+
return `${formatDuration(declaration.unit === "s" ? value * SECOND : value, locale)} ${label}`;
|
|
111
|
+
default:
|
|
112
|
+
return `${new Intl.NumberFormat(locale).format(value)} ${label}`;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A stored result, read through the job's declarations: in declaration order,
|
|
118
|
+
* each with its phrase. Keys the job no longer declares, and values that are not
|
|
119
|
+
* finite numbers, are left out — a ledger row is JSON someone wrote at another
|
|
120
|
+
* time, so it is repaired rather than trusted.
|
|
121
|
+
*/
|
|
122
|
+
export function countLines(
|
|
123
|
+
declarations: CountDeclarations,
|
|
124
|
+
stored: unknown,
|
|
125
|
+
options: CountFormatOptions = {},
|
|
126
|
+
): CountLine[] {
|
|
127
|
+
if (!stored || typeof stored !== "object" || Array.isArray(stored)) return [];
|
|
128
|
+
const values = stored as Record<string, unknown>;
|
|
129
|
+
return Object.entries(declarations).flatMap(([key, declaration]): CountLine[] => {
|
|
130
|
+
const value = values[key];
|
|
131
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return [];
|
|
132
|
+
return [{ key, kind: declaration.kind ?? "count", value, text: formatCount(value, declaration, options) }];
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Only the declared, finite counts — what the dispatcher writes to the ledger. */
|
|
137
|
+
export function declaredCounts(declarations: CountDeclarations, returned: unknown): Record<string, number> {
|
|
138
|
+
if (!returned || typeof returned !== "object") return {};
|
|
139
|
+
const values = returned as Record<string, unknown>;
|
|
140
|
+
const kept: Record<string, number> = {};
|
|
141
|
+
for (const key of Object.keys(declarations)) {
|
|
142
|
+
const value = values[key];
|
|
143
|
+
if (typeof value === "number" && Number.isFinite(value)) kept[key] = value;
|
|
144
|
+
}
|
|
145
|
+
return kept;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The phrases joined for one line: "3 reminders sent · £1,204.50 outstanding". */
|
|
149
|
+
export function countsSummary(lines: readonly CountLine[], separator = " · "): string {
|
|
150
|
+
return lines.map((line) => line.text).join(separator);
|
|
151
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether a request may run the scheduled jobs: the shared `CRON_SECRET`, as
|
|
5
|
+
* Vercel's cron sends it (`Authorization: Bearer <secret>`).
|
|
6
|
+
*
|
|
7
|
+
* Both apps wrote this, the same way in substance. The union keeps each one's
|
|
8
|
+
* better half:
|
|
9
|
+
*
|
|
10
|
+
* - **An unset secret refuses everything (503)** rather than running
|
|
11
|
+
* unauthenticated. Both.
|
|
12
|
+
* - **Compared as SHA-256 digests with `timingSafeEqual`** (Quickie). Equal
|
|
13
|
+
* lengths whatever was sent, so the comparison is constant-time and leaks not
|
|
14
|
+
* even the secret's length. AltEd compared the raw bytes after a length check.
|
|
15
|
+
* - **A committed development secret is refused in production** (Quickie). Next
|
|
16
|
+
* loads `.env` in production too, so a host that forgets its own value would
|
|
17
|
+
* otherwise run on the one in the repository.
|
|
18
|
+
* - **No secret in the address unless asked for** (AltEd). `?secret=` lands in
|
|
19
|
+
* every request log on the way; a route only Vercel calls has no use for it.
|
|
20
|
+
* Quickie accepted it everywhere, AltEd on the tick. Off by default here.
|
|
21
|
+
* - **The Bearer scheme is required.** Quickie's check stripped an optional
|
|
22
|
+
* prefix, so a bare secret in the header also passed.
|
|
23
|
+
*
|
|
24
|
+
* Returns a decision, not a `Response`: each app answers in its own envelope.
|
|
25
|
+
*/
|
|
26
|
+
export type CronAuthResult = { ok: true } | { ok: false; status: 401 | 503; message: string };
|
|
27
|
+
|
|
28
|
+
export interface CronAuthOptions {
|
|
29
|
+
/** The secret. Default `process.env.CRON_SECRET`. */
|
|
30
|
+
secret?: string;
|
|
31
|
+
/** Accept `?secret=` from a scheduler that can only call an address. Default `false`. */
|
|
32
|
+
allowQuery?: boolean;
|
|
33
|
+
/** Values that are public — committed to the repository — and so refused in production. */
|
|
34
|
+
publicSecrets?: readonly string[];
|
|
35
|
+
/** Default `process.env.NODE_ENV === "production"`. */
|
|
36
|
+
production?: boolean;
|
|
37
|
+
/** What the refusal calls this endpoint: "Cron", "The harvest". Default "Cron". */
|
|
38
|
+
label?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const digest = (value: string) => createHash("sha256").update(value).digest();
|
|
42
|
+
|
|
43
|
+
export function checkCronRequest(request: Request, options: CronAuthOptions = {}): CronAuthResult {
|
|
44
|
+
const label = options.label ?? "Cron";
|
|
45
|
+
const secret = options.secret ?? process.env.CRON_SECRET ?? "";
|
|
46
|
+
if (!secret) return { ok: false, status: 503, message: `${label} is not configured (CRON_SECRET unset).` };
|
|
47
|
+
const production = options.production ?? process.env.NODE_ENV === "production";
|
|
48
|
+
if (production && options.publicSecrets?.includes(secret)) {
|
|
49
|
+
return { ok: false, status: 503, message: `${label} is not configured (CRON_SECRET is the development value).` };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const header = request.headers.get("authorization") ?? "";
|
|
53
|
+
const bearer = /^Bearer\s+(.+)$/i.exec(header)?.[1];
|
|
54
|
+
const provided =
|
|
55
|
+
bearer ?? (options.allowQuery ? (new URL(request.url).searchParams.get("secret") ?? undefined) : undefined);
|
|
56
|
+
if (!provided || !timingSafeEqual(digest(provided), digest(secret))) {
|
|
57
|
+
return { ok: false, status: 401, message: "Unauthorized" };
|
|
58
|
+
}
|
|
59
|
+
return { ok: true };
|
|
60
|
+
}
|