@connextar/house 0.23.1 → 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 +22 -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/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,299 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { join, sep } from "node:path";
|
|
3
|
+
|
|
4
|
+
import type { CookieJar, ViewAsCookieOptions } from "./view-as.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The bypass sweep: every admin surface goes through the one guard — generalised from PharmaLine's
|
|
8
|
+
* `admin-guard.test.ts`, the strongest of the apps' versions, with Quickie's checks that nothing escapes it.
|
|
9
|
+
*
|
|
10
|
+
* The rule lives in one guard. A page or route that forgot to call it serves administration without it, and no type
|
|
11
|
+
* or test of the guard itself notices — so this reads every one of them, and fails the day one is added without:
|
|
12
|
+
*
|
|
13
|
+
* - **Routes:** in every `route.ts` under `apiDirs` (and under `pageDirs`), the guard is the **first `await`** of every
|
|
14
|
+
* handler — not merely called somewhere, after a body was parsed or a row read. Handlers must be exported as
|
|
15
|
+
* `export async function GET(…)`; `export const GET = …` or `export { x as GET }` is reported, since the sweep
|
|
16
|
+
* cannot read it. A handler that grants nothing (stopping a "view as") goes in `exempt`, with why.
|
|
17
|
+
* - **Pages:** every page file under `pageDirs` awaits the page guard itself — a layout is not re-rendered on every
|
|
18
|
+
* navigation between its children, so a guard only there guards only the first page visited (PM Tool).
|
|
19
|
+
* - **No server actions** under either: a `"use server"` file is an endpoint the route sweep cannot see.
|
|
20
|
+
* - **The flag:** only allow-listed files mention the raw admin flag, each with a reason. A new reader fails until
|
|
21
|
+
* someone has checked it goes through the guard; an entry that no longer reads it fails too, so the list stays true.
|
|
22
|
+
*
|
|
23
|
+
* Comments are ignored when looking for the guard (a comment saying `await requireAdmin()` is not a call), and kept
|
|
24
|
+
* when looking for the flag (a comment naming it is still listed, with "comment" as its reason — as PharmaLine does).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
export interface AdminGuardCoverageOptions {
|
|
28
|
+
/** The app's root. Default `process.cwd()`. */
|
|
29
|
+
root?: string;
|
|
30
|
+
/** Where the admin API routes are, relative to the root: `["app/api/admin"]`. */
|
|
31
|
+
apiDirs: readonly string[];
|
|
32
|
+
/** Where the admin pages are: `["app/dashboard/admin"]`. */
|
|
33
|
+
pageDirs: readonly string[];
|
|
34
|
+
/** The route guard's name — or names — as called: `"requirePlatformAdmin"`. */
|
|
35
|
+
apiGuard: string | readonly string[];
|
|
36
|
+
/** The page guard's name — or names: `"requirePlatformAdminPage"`. */
|
|
37
|
+
pageGuard: string | readonly string[];
|
|
38
|
+
/** Who may mention the raw admin flag. */
|
|
39
|
+
flagReaders?: {
|
|
40
|
+
pattern: RegExp;
|
|
41
|
+
/** Where to look: `["app", "components", "lib"]`. */
|
|
42
|
+
dirs: readonly string[];
|
|
43
|
+
/** Each file that may, relative to the root, and why that is safe. */
|
|
44
|
+
allowed: Record<string, string>;
|
|
45
|
+
/** Files to leave out besides tests — generated code. */
|
|
46
|
+
ignore?: (file: string) => boolean;
|
|
47
|
+
};
|
|
48
|
+
/** `"<route file> <METHOD>"` → why it needs no guard. */
|
|
49
|
+
exempt?: Record<string, string>;
|
|
50
|
+
/** Which files are pages. Default `page.tsx`, `page.ts`, `page.jsx`, `page.js`. */
|
|
51
|
+
pageFile?: RegExp;
|
|
52
|
+
/** Fewer than this many found is a problem: a moved folder would otherwise pass with nothing checked. Default 1 each. */
|
|
53
|
+
minimum?: { routes?: number; handlers?: number; pages?: number };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface AdminGuardCoverageReport {
|
|
57
|
+
routes: string[];
|
|
58
|
+
/** `"<route file> <METHOD>"`, each checked. */
|
|
59
|
+
handlers: string[];
|
|
60
|
+
pages: string[];
|
|
61
|
+
flagReaders: string[];
|
|
62
|
+
problems: string[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class AdminGuardCoverageError extends Error {
|
|
66
|
+
readonly problems: string[];
|
|
67
|
+
|
|
68
|
+
constructor(problems: string[]) {
|
|
69
|
+
super(
|
|
70
|
+
`Admin guard coverage — ${problems.length} problem${problems.length === 1 ? "" : "s"}:\n - ${problems.join("\n - ")}`,
|
|
71
|
+
);
|
|
72
|
+
this.name = "AdminGuardCoverageError";
|
|
73
|
+
this.problems = problems;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const METHODS = "GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS";
|
|
78
|
+
const HANDLER = new RegExp(String.raw`export\s+async\s+function\s+(${METHODS})\s*\(`, "g");
|
|
79
|
+
const UNREADABLE = [
|
|
80
|
+
new RegExp(String.raw`export\s+(?:const|let|var)\s+(${METHODS})\b`, "g"),
|
|
81
|
+
new RegExp(String.raw`export\s+function\s+(${METHODS})\b`, "g"),
|
|
82
|
+
new RegExp(String.raw`export\s*\{[^}]*\b(${METHODS})\b[^}]*\}`, "g"),
|
|
83
|
+
new RegExp(String.raw`export\s+default\b`, "g"),
|
|
84
|
+
];
|
|
85
|
+
const SOURCE = /\.(?:[cm]?[jt]sx?)$/;
|
|
86
|
+
const TEST = /\.(?:test|spec)\.[cm]?[jt]sx?$/;
|
|
87
|
+
const ROUTE = /^route\.[cm]?[jt]sx?$/;
|
|
88
|
+
const DEFAULT_PAGE = /^page\.[jt]sx?$/;
|
|
89
|
+
|
|
90
|
+
/** Source with its comments blanked out, strings and template literals left as they are. */
|
|
91
|
+
export function stripComments(source: string): string {
|
|
92
|
+
let out = "";
|
|
93
|
+
let i = 0;
|
|
94
|
+
let quote: string | null = null;
|
|
95
|
+
while (i < source.length) {
|
|
96
|
+
const char = source[i]!;
|
|
97
|
+
const next = source[i + 1];
|
|
98
|
+
if (quote) {
|
|
99
|
+
out += char;
|
|
100
|
+
if (char === "\\") {
|
|
101
|
+
out += next ?? "";
|
|
102
|
+
i += 2;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (char === quote) quote = null;
|
|
106
|
+
i += 1;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (char === "\\") {
|
|
110
|
+
out += char + (next ?? "");
|
|
111
|
+
i += 2;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
115
|
+
quote = char;
|
|
116
|
+
out += char;
|
|
117
|
+
i += 1;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (char === "/" && next === "/") {
|
|
121
|
+
while (i < source.length && source[i] !== "\n") i += 1;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (char === "/" && next === "*") {
|
|
125
|
+
const end = source.indexOf("*/", i + 2);
|
|
126
|
+
const stop = end === -1 ? source.length : end + 2;
|
|
127
|
+
// Keep the line breaks, so nothing after the comment moves line.
|
|
128
|
+
out += source.slice(i, stop).replace(/[^\n]/g, " ");
|
|
129
|
+
i = stop;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
out += char;
|
|
133
|
+
i += 1;
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function filesUnder(root: string, dir: string, match: (name: string) => boolean): string[] {
|
|
139
|
+
const full = join(root, dir);
|
|
140
|
+
if (!existsSync(full)) return [];
|
|
141
|
+
const out: string[] = [];
|
|
142
|
+
for (const name of readdirSync(full).sort()) {
|
|
143
|
+
if (name === "node_modules" || name.startsWith(".")) continue;
|
|
144
|
+
const path = join(dir, name);
|
|
145
|
+
if (statSync(join(root, path)).isDirectory()) out.push(...filesUnder(root, path, match));
|
|
146
|
+
else if (match(name)) out.push(path);
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const posix = (path: string) => path.split(sep).join("/");
|
|
152
|
+
const names = (guard: string | readonly string[]) => (typeof guard === "string" ? [guard] : [...guard]);
|
|
153
|
+
|
|
154
|
+
/** The sweep, answering every problem it found rather than stopping at the first. */
|
|
155
|
+
export function checkAdminGuardCoverage(options: AdminGuardCoverageOptions): AdminGuardCoverageReport {
|
|
156
|
+
const root = options.root ?? process.cwd();
|
|
157
|
+
const apiGuards = names(options.apiGuard);
|
|
158
|
+
const pageGuards = names(options.pageGuard);
|
|
159
|
+
const exempt = options.exempt ?? {};
|
|
160
|
+
const pageFile = options.pageFile ?? DEFAULT_PAGE;
|
|
161
|
+
const problems: string[] = [];
|
|
162
|
+
const read = (file: string) => readFileSync(join(root, file), "utf8");
|
|
163
|
+
const calls = (name: string, guards: string[]) =>
|
|
164
|
+
guards.some((guard) => name === guard || name.endsWith(`.${guard}`));
|
|
165
|
+
|
|
166
|
+
for (const dir of [...options.apiDirs, ...options.pageDirs]) {
|
|
167
|
+
if (!existsSync(join(root, dir))) problems.push(`${dir}: not found — the sweep would check nothing there`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const routes = [
|
|
171
|
+
...new Set([...options.apiDirs, ...options.pageDirs].flatMap((dir) => filesUnder(root, dir, (n) => ROUTE.test(n)))),
|
|
172
|
+
]
|
|
173
|
+
.map(posix)
|
|
174
|
+
.sort();
|
|
175
|
+
const handlers: string[] = [];
|
|
176
|
+
const usedExemptions = new Set<string>();
|
|
177
|
+
|
|
178
|
+
for (const route of routes) {
|
|
179
|
+
const code = stripComments(read(route));
|
|
180
|
+
for (const pattern of UNREADABLE) {
|
|
181
|
+
for (const match of code.matchAll(pattern)) {
|
|
182
|
+
problems.push(
|
|
183
|
+
`${route}: \`${match[0].replace(/\s+/g, " ").trim()}\` — export handlers as \`export async function ${match[1] ?? "GET"}(…)\` so the sweep can read them`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const found = [...code.matchAll(HANDLER)];
|
|
188
|
+
if (found.length === 0) problems.push(`${route}: no \`export async function <METHOD>(…)\` handler found`);
|
|
189
|
+
found.forEach((handler, index) => {
|
|
190
|
+
const name = `${route} ${handler[1]}`;
|
|
191
|
+
handlers.push(name);
|
|
192
|
+
if (Object.hasOwn(exempt, name)) {
|
|
193
|
+
usedExemptions.add(name);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
const end = found[index + 1]?.index ?? code.length;
|
|
197
|
+
const body = code.slice(handler.index, end);
|
|
198
|
+
const first = /\bawait\s+([\w$.]+)\s*\(/.exec(body)?.[1];
|
|
199
|
+
if (!first) problems.push(`${name}: never awaits ${apiGuards.join(" or ")}`);
|
|
200
|
+
else if (!calls(first, apiGuards)) {
|
|
201
|
+
problems.push(`${name}: the first await is ${first}(), not ${apiGuards.join(" or ")}()`);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
for (const name of Object.keys(exempt)) {
|
|
206
|
+
if (!usedExemptions.has(name)) problems.push(`exempt "${name}": no such handler — remove the exemption`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const pages = [...new Set(options.pageDirs.flatMap((dir) => filesUnder(root, dir, (n) => pageFile.test(n))))]
|
|
210
|
+
.map(posix)
|
|
211
|
+
.sort();
|
|
212
|
+
const pageCall = new RegExp(String.raw`\bawait\s+(?:[\w$]+\.)*(?:${pageGuards.map(escape).join("|")})\s*\(`);
|
|
213
|
+
for (const page of pages) {
|
|
214
|
+
if (!pageCall.test(stripComments(read(page))))
|
|
215
|
+
problems.push(`${page}: does not await ${pageGuards.join(" or ")}()`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
for (const file of [...options.apiDirs, ...options.pageDirs].flatMap((dir) =>
|
|
219
|
+
filesUnder(root, dir, (n) => SOURCE.test(n) && !TEST.test(n)),
|
|
220
|
+
)) {
|
|
221
|
+
if (/^\s*(?:\/\/[^\n]*\n\s*|\/\*[\s\S]*?\*\/\s*)*["']use server["']/.test(read(file))) {
|
|
222
|
+
problems.push(`${posix(file)}: a "use server" file is an endpoint the route sweep cannot see — make it a route`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let flagReaders: string[] = [];
|
|
227
|
+
if (options.flagReaders) {
|
|
228
|
+
const { pattern, dirs, allowed, ignore } = options.flagReaders;
|
|
229
|
+
const probe = new RegExp(pattern.source, pattern.flags.replace(/g|y/g, ""));
|
|
230
|
+
flagReaders = [...new Set(dirs.flatMap((dir) => filesUnder(root, dir, (n) => SOURCE.test(n) && !TEST.test(n))))]
|
|
231
|
+
.map(posix)
|
|
232
|
+
.filter((file) => !ignore?.(file))
|
|
233
|
+
.filter((file) => probe.test(read(file)))
|
|
234
|
+
.sort();
|
|
235
|
+
for (const file of flagReaders) {
|
|
236
|
+
if (!Object.hasOwn(allowed, file)) {
|
|
237
|
+
problems.push(
|
|
238
|
+
`${file}: mentions the admin flag (${pattern}) and is not on the allow-list — check it goes through the guard, then list it with why`,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (const file of Object.keys(allowed)) {
|
|
243
|
+
if (!flagReaders.includes(file))
|
|
244
|
+
problems.push(`${file}: is on the flag allow-list but no longer mentions it — remove it`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const minimum = { routes: 1, handlers: 1, pages: 1, ...options.minimum };
|
|
249
|
+
if (options.apiDirs.length && routes.length < minimum.routes) {
|
|
250
|
+
problems.push(`found ${routes.length} admin route file(s), expected at least ${minimum.routes}`);
|
|
251
|
+
}
|
|
252
|
+
if (options.apiDirs.length && handlers.length < minimum.handlers) {
|
|
253
|
+
problems.push(`found ${handlers.length} admin handler(s), expected at least ${minimum.handlers}`);
|
|
254
|
+
}
|
|
255
|
+
if (options.pageDirs.length && pages.length < minimum.pages) {
|
|
256
|
+
problems.push(`found ${pages.length} admin page(s), expected at least ${minimum.pages}`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return { routes, handlers, pages, flagReaders, problems };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function escape(text: string) {
|
|
263
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** The sweep, throwing every problem at once. Answers what it checked, so a test can say how much. */
|
|
267
|
+
export function assertAdminGuardCoverage(options: AdminGuardCoverageOptions): AdminGuardCoverageReport {
|
|
268
|
+
const report = checkAdminGuardCoverage(options);
|
|
269
|
+
if (report.problems.length) throw new AdminGuardCoverageError(report.problems);
|
|
270
|
+
return report;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** A cookie store in memory, shaped like Next's `cookies()`, for testing an app's "view as". */
|
|
274
|
+
export function memoryCookieJar(initial: Record<string, string> = {}) {
|
|
275
|
+
const values = new Map(Object.entries(initial));
|
|
276
|
+
const attributes = new Map<string, ViewAsCookieOptions | undefined>();
|
|
277
|
+
const jar = {
|
|
278
|
+
get(name: string) {
|
|
279
|
+
const value = values.get(name);
|
|
280
|
+
return value === undefined ? undefined : { name, value };
|
|
281
|
+
},
|
|
282
|
+
set(name: string, value: string, options?: ViewAsCookieOptions) {
|
|
283
|
+
values.set(name, value);
|
|
284
|
+
attributes.set(name, options);
|
|
285
|
+
},
|
|
286
|
+
delete(name: string) {
|
|
287
|
+
values.delete(name);
|
|
288
|
+
attributes.delete(name);
|
|
289
|
+
},
|
|
290
|
+
/** What `set` was given for a cookie. */
|
|
291
|
+
options(name: string) {
|
|
292
|
+
return attributes.get(name);
|
|
293
|
+
},
|
|
294
|
+
has(name: string) {
|
|
295
|
+
return values.has(name);
|
|
296
|
+
},
|
|
297
|
+
} satisfies CookieJar & Record<string, unknown>;
|
|
298
|
+
return jar;
|
|
299
|
+
}
|