1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
const HOST = (process.env.GLUECRON_HOST || "https://gluecron.com").replace(/\/+$/, "");
const PAT = process.env.GLUECRON_PAT?.trim() || "";
const SLOW_MS = Number(process.env.NAV_SLOW_MS || 8000);
const argv = process.argv.slice(2);
const flag = (name: string): string | undefined => {
const i = argv.indexOf(name);
return i >= 0 ? argv[i + 1] : undefined;
};
type Row = {
group: string;
label: string;
url: string;
status: number;
ms: number;
ok: boolean;
note: string;
body?: string;
};
let HOST_ORIGIN = "";
try {
HOST_ORIGIN = new URL(HOST).origin;
} catch {
}
function authHeaders(url: string): Record<string, string> {
if (!PAT) return {};
try {
if (new URL(url).origin === HOST_ORIGIN) return { authorization: `Bearer ${PAT}` };
} catch {
}
return {};
}
function errorBody(html: string): string {
const title = (html.match(/<title>([^<]*)<\/title>/i)?.[1] || "").trim();
if (/(^|\s)(not found|error|forbidden|something went wrong)(\s|$)/i.test(title)) {
return `error title: "${title}"`;
}
if (/>\s*4\d\d\s*<|Page not found|Internal Server Error|Application error/i.test(html)) {
return "error content in body";
}
return "";
}
async function hit(group: string, label: string, path: string): Promise<Row> {
const url = path.startsWith("http") ? path : HOST + path;
const t0 = Date.now();
try {
const res = await fetch(url, { headers: authHeaders(url), redirect: "manual" });
const ms = Date.now() - t0;
let note = "";
let ok = res.status >= 200 && res.status < 400;
let body: string | undefined;
if (res.status >= 200 && res.status < 300) {
body = await res.text();
const eb = errorBody(body);
if (eb) {
ok = false;
note = eb;
}
} else if (res.status >= 400) {
ok = false;
note = `HTTP ${res.status}`;
}
if (ok && ms > SLOW_MS) {
ok = false;
note = `slow render ${ms}ms (> ${SLOW_MS}ms)`;
}
return { group, label, url, status: res.status, ms, ok, note, body };
} catch (err) {
return {
group,
label,
url,
status: 0,
ms: Date.now() - t0,
ok: false,
note: `fetch failed: ${(err as Error).message}`,
};
}
}
const REPO_TABS: Array<[string, string]> = [
["Code", ""],
["Issues", "/issues"],
["Pull Requests", "/pulls"],
["Actions", "/actions"],
["Security", "/security/vulnerabilities"],
["Insights", "/insights"],
["Settings", "/settings"],
["AI:Explain", "/explain"],
["AI:Ask", "/ask"],
["AI:Workspace", "/workspace"],
["AI:Spec", "/spec"],
["AI:Tests", "/ai/tests"],
["AI:NL Search", "/search/nl"],
["AI:Debt Map", "/debt-map"],
["AI:Archaeology", "/archaeology"],
["More:Commits", "/commits"],
["More:Discussions", "/discussions"],
["More:Wiki", "/wiki"],
["More:Projects", "/projects"],
["More:Releases", "/releases"],
["More:Contributors", "/contributors"],
["More:Pulse", "/pulse"],
["More:Gates", "/gates"],
["More:Deployments", "/cloud-deployments"],
["More:Pipeline", "/pipeline"],
["More:Agents", "/agents"],
["More:Traffic", "/traffic"],
];
const INSIGHT_REPORTS: Array<[string, string]> = [
["Insights hub", "/insights"],
["DORA", "/insights/dora"],
["Velocity", "/insights/velocity"],
["Health", "/insights/health"],
["Hot files", "/insights/hotfiles"],
["Bus factor", "/insights/bus-factor"],
["Test gaps", "/insights/test-gaps"],
["Engineering", "/insights/engineering"],
["Coupling", "/coupling"],
["Dependencies", "/dependencies"],
["Repo health", "/health"],
];
const SETTINGS_PAGES = [
"/settings", "/settings/keys", "/settings/signing-keys", "/settings/2fa",
"/settings/passkeys", "/settings/sessions", "/settings/tokens",
"/settings/notifications", "/settings/replies", "/settings/billing",
"/settings/sponsors", "/settings/applications", "/settings/apps",
"/settings/authorizations", "/settings/agents", "/settings/deploy-targets",
"/settings/integrations", "/settings/incident-hooks", "/settings/audit",
];
const ADMIN_PAGES = [
"/admin", "/admin/command", "/admin/ops", "/admin/health", "/admin/env-health",
"/admin/status", "/admin/self-host", "/admin/diagnose", "/admin/users",
"/admin/repos", "/admin/flags", "/admin/security", "/admin/soc2",
"/admin/deletions", "/admin/stripe", "/admin/google-oauth", "/admin/github-oauth",
"/admin/sso", "/admin/ai-costs", "/admin/growth", "/admin/autopilot",
"/admin/digests", "/admin/integrations", "/admin/advancement", "/admin/deploys",
"/admin/autopilot/health",
];
const GLOBAL_PAGES = [
"/", "/explore", "/dashboard", "/notifications", "/activity", "/pricing",
"/status", "/help", "/docs", "/changelog",
];
type Spec = { group: string; label: string; path: string };
function repoSpecs(ownerRepo: string): Spec[] {
const base = `/${ownerRepo}`;
return [
...REPO_TABS.map(([label, sub]) => ({ group: `repo:${ownerRepo}`, label, path: base + sub })),
...INSIGHT_REPORTS.map(([label, sub]) => ({ group: `insights:${ownerRepo}`, label, path: base + sub })),
];
}
function globalSpecs(): Spec[] {
return [
...GLOBAL_PAGES.map((p) => ({ group: "global", label: p, path: p })),
...SETTINGS_PAGES.map((p) => ({ group: "settings", label: p, path: p })),
...ADMIN_PAGES.map((p) => ({ group: "admin", label: p, path: p })),
];
}
async function runConcurrent(specs: Spec[], concurrency = 10): Promise<Row[]> {
const rows: Row[] = new Array(specs.length);
let next = 0;
async function worker() {
while (true) {
const i = next++;
if (i >= specs.length) return;
const s = specs[i];
rows[i] = await hit(s.group, s.label, s.path);
}
}
await Promise.all(Array.from({ length: Math.min(concurrency, specs.length) }, worker));
return rows;
}
async function listAllRepos(): Promise<string[]> {
const out = new Set<string>();
try {
const me = await fetch(HOST + "/api/v2/user", { headers: authHeaders(HOST + "/api/v2/user") }).then((r) => r.json());
if (me?.username) {
const reposUrl = HOST + `/api/v2/users/${encodeURIComponent(me.username)}/repos`;
const repos = await fetch(reposUrl, { headers: authHeaders(reposUrl) }).then((r) => r.json());
if (Array.isArray(repos)) for (const r of repos) if (r?.name) out.add(`${me.username}/${r.name}`);
}
} catch { }
return [...out];
}
const DANGER = /(logout|sign-?out|delete|destroy|revoke|remove|\/raw\/|\/git-|uninstall|disable|reset|purge|cancel|deactivate)/i;
async function crawlSite(startUrl: string, maxPages = 150): Promise<Row[]> {
const origin = new URL(startUrl).origin;
const seen = new Set<string>();
const queue: string[] = [startUrl];
const rows: Row[] = [];
while (queue.length && seen.size < maxPages) {
const url = queue.shift()!;
if (seen.has(url)) continue;
seen.add(url);
const row = await hit("crawl", new URL(url).pathname || "/", url);
rows.push(row);
if (row.status >= 200 && row.status < 300 && !row.note && row.body) {
try {
const html = row.body;
const hrefs = [...html.matchAll(/href="([^"#?]+)"/gi)].map((m) => m[1]);
for (const href of hrefs) {
if (DANGER.test(href)) continue;
let abs: string;
try {
abs = new URL(href, url).toString();
} catch {
continue;
}
if (!abs.startsWith(origin)) continue;
if (DANGER.test(abs)) continue;
if (!seen.has(abs) && !queue.includes(abs)) queue.push(abs);
}
} catch { }
}
}
return rows;
}
function report(rows: Row[]): number {
const fails = rows.filter((r) => !r.ok);
const slow = rows.filter((r) => r.ok && r.ms > SLOW_MS / 2);
console.log(`\n# nav-audit — ${HOST} — ${new Date().toISOString()}`);
console.log(`# ${rows.length} pages checked · PAT ${PAT ? "present" : "absent"}\n`);
if (fails.length) {
console.log(`## FAILURES (${fails.length})\n`);
console.log("status | ms | group / label → url | note");
console.log("-------+-------+---------------------+-----");
for (const r of fails) {
console.log(`${String(r.status).padStart(6)} | ${String(r.ms).padStart(5)} | ${r.group} / ${r.label} → ${r.url} | ${r.note}`);
}
console.log("");
}
if (slow.length) {
console.log(`## SLOW (ok but > ${Math.round(SLOW_MS / 2)}ms) — worth a look\n`);
for (const r of slow) console.log(` ${String(r.ms).padStart(6)}ms ${r.group} / ${r.label}`);
console.log("");
}
const passed = rows.length - fails.length;
console.log(fails.length === 0 ? `nav-audit: ALL GREEN — ${passed}/${rows.length} pages render` : `nav-audit: ${fails.length} FAILURE(S) — ${passed}/${rows.length} render`);
return fails.length === 0 ? 0 : 1;
}
async function main() {
let rows: Row[] = [];
const crawlUrl = flag("--crawl");
if (crawlUrl) {
console.log(`crawling ${crawlUrl} (same-origin, GET-only, safe)…`);
rows = await crawlSite(crawlUrl);
} else if (argv.includes("--all-repos")) {
const repos = await listAllRepos();
console.log(`auditing ${repos.length} repos + global/settings/admin…`);
const specs = [...globalSpecs(), ...repos.flatMap(repoSpecs)];
rows = await runConcurrent(specs);
} else {
const repo = flag("--repo") || "ccantynz/Gluecron.com";
console.log(`auditing repo ${repo} + global/settings/admin…`);
rows = await runConcurrent([...globalSpecs(), ...repoSpecs(repo)]);
}
process.exit(report(rows));
}
main();
|