|
1 | 1 | import { DOMParser } from "linkedom"; |
2 | 2 | import * as path from "@std/path"; |
3 | | - |
4 | | -import { launchProd } from "../packages/plugin-vite/tests/test_utils.ts"; |
| 3 | +import { TextLineStream } from "@std/streams/text-line-stream"; |
| 4 | +import { mergeReadableStreams } from "@std/streams"; |
| 5 | +import * as colors from "@std/fmt/colors"; |
5 | 6 | import { createBuilder } from "vite"; |
6 | 7 |
|
7 | 8 | const www = path.join(import.meta.dirname!, "..", "www"); |
8 | | -const builder = await createBuilder({ |
9 | | - root: www, |
10 | | -}); |
11 | 9 |
|
| 10 | +const totalStart = performance.now(); |
| 11 | + |
| 12 | +// deno-lint-ignore no-console |
| 13 | +console.log("Building www..."); |
| 14 | +let stepStart = performance.now(); |
| 15 | +const builder = await createBuilder({ root: www }); |
12 | 16 | await builder.buildApp(); |
| 17 | +// deno-lint-ignore no-console |
| 18 | +console.log( |
| 19 | + `Build completed in ${((performance.now() - stepStart) / 1000).toFixed(1)}s`, |
| 20 | +); |
| 21 | + |
| 22 | +const EXCLUDED_PREFIXES = [ |
| 23 | + "mailto:", |
| 24 | + "javascript:", |
| 25 | + "vscode:", |
| 26 | + "data:", |
| 27 | +]; |
13 | 28 |
|
14 | | -interface CheckLink { |
15 | | - url: URL; |
16 | | - referrer: URL | null; |
| 29 | +interface FailedLink { |
| 30 | + url: string; |
| 31 | + status: number; |
| 32 | + referrer: string; |
17 | 33 | } |
18 | 34 |
|
19 | | -await launchProd({ cwd: www }, async (address) => { |
20 | | - const first = new URL(address); |
| 35 | +const checkedUrls = new Map<string, number>(); |
| 36 | +const visitedPages = new Set<string>(); |
| 37 | +const failedLinks: FailedLink[] = []; |
| 38 | +const CONCURRENCY = 10; |
21 | 39 |
|
22 | | - const stack: CheckLink[] = [{ referrer: null, url: first }]; |
| 40 | +// deno-lint-ignore no-console |
| 41 | +console.log("Starting server..."); |
23 | 42 |
|
24 | | - const seen = new Set<string>(); |
| 43 | +// Spawn the prod server directly to avoid importing test_utils.tsx |
| 44 | +// (which launches a headless browser at module scope) |
| 45 | +const cp = new Deno.Command(Deno.execPath(), { |
| 46 | + args: ["serve", "-A", "--cached-only", "--port", "0", "_fresh/server.js"], |
| 47 | + stdin: "null", |
| 48 | + stdout: "piped", |
| 49 | + stderr: "piped", |
| 50 | + cwd: www, |
| 51 | +}).spawn(); |
25 | 52 |
|
26 | | - let current: CheckLink | undefined; |
27 | | - while ((current = stack.pop()) !== undefined) { |
28 | | - seen.add(current.url.pathname); |
| 53 | +// Read server output to find the address |
| 54 | +const linesStdout = cp.stdout |
| 55 | + .pipeThrough(new TextDecoderStream()) |
| 56 | + .pipeThrough(new TextLineStream()); |
| 57 | +const linesStderr = cp.stderr |
| 58 | + .pipeThrough(new TextDecoderStream()) |
| 59 | + .pipeThrough(new TextLineStream()); |
| 60 | +const lines = mergeReadableStreams(linesStdout, linesStderr); |
29 | 61 |
|
30 | | - // deno-lint-ignore no-console |
31 | | - console.log("Checking...", current.url.href); |
| 62 | +let address = ""; |
| 63 | +// @ts-ignore yes it does |
| 64 | +for await (const raw of lines.values({ preventCancel: true })) { |
| 65 | + const line = colors.stripAnsiCode(raw); |
| 66 | + const match = line.match(/https?:\/\/[^:]+:\d+(\/\w+[-\w]*)*/g); |
| 67 | + if (match) { |
| 68 | + address = match[0]; |
| 69 | + break; |
| 70 | + } |
| 71 | +} |
32 | 72 |
|
33 | | - const headers = new Headers(); |
34 | | - headers.set( |
35 | | - "accept", |
36 | | - "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8", |
37 | | - ); |
38 | | - const res = await fetch(current.url, { headers }); |
39 | | - const text = await res.text(); |
| 73 | +if (!address) { |
| 74 | + // deno-lint-ignore no-console |
| 75 | + console.error("Could not find server address"); |
| 76 | + cp.kill(); |
| 77 | + Deno.exit(1); |
| 78 | +} |
| 79 | + |
| 80 | +// deno-lint-ignore no-console |
| 81 | +console.log(`Server listening at ${address}`); |
| 82 | + |
| 83 | +const rootUrl = new URL(address); |
40 | 84 |
|
41 | | - if (res.status === 404) { |
42 | | - throw new Error( |
43 | | - `Failed url ${current.url.href}, referrer: ${current.referrer?.href}`, |
44 | | - ); |
| 85 | +async function checkUrl( |
| 86 | + url: string, |
| 87 | + referrer: string, |
| 88 | +): Promise<number> { |
| 89 | + const cached = checkedUrls.get(url); |
| 90 | + if (cached !== undefined) return cached; |
| 91 | + |
| 92 | + // Mark as in-flight to avoid duplicate checks |
| 93 | + checkedUrls.set(url, 0); |
| 94 | + |
| 95 | + try { |
| 96 | + const res = await fetch(url, { |
| 97 | + method: "HEAD", |
| 98 | + headers: { "User-Agent": "fresh-link-checker" }, |
| 99 | + redirect: "follow", |
| 100 | + }); |
| 101 | + checkedUrls.set(url, res.status); |
| 102 | + if (res.status >= 400) { |
| 103 | + failedLinks.push({ url, status: res.status, referrer }); |
45 | 104 | } |
| 105 | + return res.status; |
| 106 | + } catch { |
| 107 | + checkedUrls.set(url, 0); |
| 108 | + failedLinks.push({ url, status: 0, referrer }); |
| 109 | + return 0; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +async function crawlPage(pageUrl: URL, referrer: string) { |
| 114 | + const pathname = pageUrl.pathname; |
| 115 | + if (visitedPages.has(pathname)) return; |
| 116 | + visitedPages.add(pathname); |
46 | 117 |
|
47 | | - if (!res.headers.get("Content-type")?.includes("text/html")) { |
| 118 | + let res: Response; |
| 119 | + try { |
| 120 | + res = await fetch(pageUrl, { |
| 121 | + headers: { |
| 122 | + accept: |
| 123 | + "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8", |
| 124 | + }, |
| 125 | + }); |
| 126 | + } catch { |
| 127 | + failedLinks.push({ url: pageUrl.href, status: 0, referrer }); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + if (res.status >= 400) { |
| 132 | + failedLinks.push({ url: pageUrl.href, status: res.status, referrer }); |
| 133 | + await res.body?.cancel(); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (!res.headers.get("content-type")?.includes("text/html")) { |
| 138 | + await res.body?.cancel(); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + const text = await res.text(); |
| 143 | + Deno.stdout.writeSync(new TextEncoder().encode(".")); |
| 144 | + const doc = new DOMParser().parseFromString(text, "text/html"); |
| 145 | + |
| 146 | + const linkChecks: Array<Promise<void>> = []; |
| 147 | + const internalPages: Array<{ url: URL; referrer: string }> = []; |
| 148 | + |
| 149 | + for (const link of doc.querySelectorAll("a")) { |
| 150 | + const href = link.getAttribute("href")?.trim(); |
| 151 | + if (!href) continue; |
| 152 | + if (EXCLUDED_PREFIXES.some((p) => href.startsWith(p))) continue; |
| 153 | + if (href.startsWith("#")) continue; |
| 154 | + |
| 155 | + let nextUrl: URL; |
| 156 | + try { |
| 157 | + nextUrl = new URL(href, pageUrl); |
| 158 | + } catch { |
48 | 159 | continue; |
49 | 160 | } |
50 | 161 |
|
51 | | - const doc = new DOMParser().parseFromString(text, "text/html"); |
| 162 | + // Strip fragment |
| 163 | + nextUrl.hash = ""; |
| 164 | + const urlStr = nextUrl.href; |
52 | 165 |
|
53 | | - for (const link of doc.querySelectorAll("a")) { |
54 | | - const next = new URL(link.href, first.origin); |
55 | | - if (next.origin !== first.origin) continue; |
56 | | - if (seen.has(next.pathname)) continue; |
| 166 | + if (nextUrl.origin === rootUrl.origin) { |
| 167 | + // Internal link — crawl the page if it's a docs page |
| 168 | + if ( |
| 169 | + !visitedPages.has(nextUrl.pathname) && |
| 170 | + nextUrl.pathname.startsWith("/docs") |
| 171 | + ) { |
| 172 | + internalPages.push({ url: nextUrl, referrer: pathname }); |
| 173 | + } else if (!visitedPages.has(nextUrl.pathname)) { |
| 174 | + // Non-docs internal page: just check it returns OK |
| 175 | + if (!checkedUrls.has(urlStr)) { |
| 176 | + linkChecks.push(checkUrl(urlStr, pathname).then(() => {})); |
| 177 | + } |
| 178 | + } |
| 179 | + } else { |
| 180 | + // External link — verify it's live |
| 181 | + if (!checkedUrls.has(urlStr)) { |
| 182 | + linkChecks.push(checkUrl(urlStr, pathname).then(() => {})); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
57 | 186 |
|
58 | | - stack.push({ url: next, referrer: current.url }); |
| 187 | + // Check external/non-docs links concurrently |
| 188 | + const batched: Array<Promise<void>> = []; |
| 189 | + for (const check of linkChecks) { |
| 190 | + batched.push(check); |
| 191 | + if (batched.length >= CONCURRENCY) { |
| 192 | + await Promise.all(batched); |
| 193 | + batched.length = 0; |
59 | 194 | } |
60 | 195 | } |
61 | | -}); |
| 196 | + if (batched.length > 0) await Promise.all(batched); |
| 197 | + |
| 198 | + // Crawl internal docs pages |
| 199 | + for (const page of internalPages) { |
| 200 | + await crawlPage(page.url, page.referrer); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +// Start crawling from /docs |
| 205 | +stepStart = performance.now(); |
| 206 | +// deno-lint-ignore no-console |
| 207 | +console.log("Crawling docs pages..."); |
| 208 | + |
| 209 | +const docsUrl = new URL("/docs", rootUrl); |
| 210 | +await crawlPage(docsUrl, "(start)"); |
| 211 | + |
| 212 | +// deno-lint-ignore no-console |
| 213 | +console.log(); |
| 214 | +// deno-lint-ignore no-console |
| 215 | +console.log( |
| 216 | + `\nCrawl completed in ${ |
| 217 | + ((performance.now() - stepStart) / 1000).toFixed(1) |
| 218 | + }s`, |
| 219 | +); |
| 220 | +// deno-lint-ignore no-console |
| 221 | +console.log(`Docs pages crawled: ${visitedPages.size}`); |
| 222 | +// deno-lint-ignore no-console |
| 223 | +console.log(`Total links checked: ${checkedUrls.size}`); |
| 224 | +// deno-lint-ignore no-console |
| 225 | +console.log( |
| 226 | + `Total time: ${((performance.now() - totalStart) / 1000).toFixed(1)}s`, |
| 227 | +); |
| 228 | + |
| 229 | +// Kill the server |
| 230 | +cp.kill(); |
| 231 | +await cp.status; |
| 232 | + |
| 233 | +if (failedLinks.length > 0) { |
| 234 | + // deno-lint-ignore no-console |
| 235 | + console.error(`\nBroken links found: ${failedLinks.length}`); |
| 236 | + for (const link of failedLinks) { |
| 237 | + // deno-lint-ignore no-console |
| 238 | + console.error( |
| 239 | + ` ${link.status} ${link.url} (linked from ${link.referrer})`, |
| 240 | + ); |
| 241 | + } |
| 242 | + Deno.exit(1); |
| 243 | +} |
| 244 | + |
| 245 | +// deno-lint-ignore no-console |
| 246 | +console.log("\nAll links OK!"); |
| 247 | +Deno.exit(0); |
0 commit comments