sugikit 0.0.0-stage → 0.1.0

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 (72) hide show
  1. package/LICENSE +29 -0
  2. package/README.md +44 -2
  3. package/dist/api/browser.d.ts +5 -0
  4. package/dist/api/browser.js +5 -0
  5. package/dist/api/rules.d.ts +12 -0
  6. package/dist/api/rules.js +14 -0
  7. package/dist/auth.d.ts +156 -0
  8. package/dist/auth.js +547 -0
  9. package/dist/browser.d.ts +25 -0
  10. package/dist/browser.js +120 -0
  11. package/dist/check.d.ts +21 -0
  12. package/dist/check.js +89 -0
  13. package/dist/convert-probe.d.ts +21 -0
  14. package/dist/convert-probe.js +647 -0
  15. package/dist/fetch.d.ts +6 -0
  16. package/dist/fetch.js +22 -0
  17. package/dist/framework.d.ts +2 -0
  18. package/dist/framework.js +17 -0
  19. package/dist/gather.d.ts +48 -0
  20. package/dist/gather.js +211 -0
  21. package/dist/html.d.ts +17 -0
  22. package/dist/html.js +43 -0
  23. package/dist/http.d.ts +36 -0
  24. package/dist/http.js +57 -0
  25. package/dist/index.d.ts +2 -0
  26. package/dist/index.js +331 -0
  27. package/dist/links.d.ts +8 -0
  28. package/dist/links.js +69 -0
  29. package/dist/polish-run.d.ts +115 -0
  30. package/dist/polish-run.js +357 -0
  31. package/dist/probe.d.ts +14 -0
  32. package/dist/probe.js +457 -0
  33. package/dist/psi-scores.d.ts +4 -0
  34. package/dist/psi-scores.js +7 -0
  35. package/dist/psi.d.ts +56 -0
  36. package/dist/psi.js +192 -0
  37. package/dist/release.d.ts +1 -0
  38. package/dist/release.js +5 -0
  39. package/dist/render.d.ts +28 -0
  40. package/dist/render.js +270 -0
  41. package/dist/report.d.ts +227 -0
  42. package/dist/report.js +69 -0
  43. package/dist/rules/convert-render.d.ts +100 -0
  44. package/dist/rules/convert-render.js +279 -0
  45. package/dist/rules/convert.d.ts +15 -0
  46. package/dist/rules/convert.js +322 -0
  47. package/dist/rules/fixes.d.ts +16 -0
  48. package/dist/rules/fixes.js +536 -0
  49. package/dist/rules/geo.d.ts +15 -0
  50. package/dist/rules/geo.js +112 -0
  51. package/dist/rules/polish.d.ts +59 -0
  52. package/dist/rules/polish.js +223 -0
  53. package/dist/rules/robots.d.ts +14 -0
  54. package/dist/rules/robots.js +69 -0
  55. package/dist/rules/seo.d.ts +31 -0
  56. package/dist/rules/seo.js +208 -0
  57. package/dist/rules/types.d.ts +55 -0
  58. package/dist/rules/types.js +78 -0
  59. package/dist/score.d.ts +6 -0
  60. package/dist/score.js +23 -0
  61. package/dist/skills.d.ts +9 -0
  62. package/dist/skills.js +22 -0
  63. package/package.json +63 -3
  64. package/skills/INSTALL_PROMPT.md +9 -0
  65. package/skills/sugikit/SKILL.md +147 -0
  66. package/skills/sugikit/references/convert.md +127 -0
  67. package/skills/sugikit/references/geo.md +97 -0
  68. package/skills/sugikit/references/next.md +254 -0
  69. package/skills/sugikit/references/pagespeed.md +94 -0
  70. package/skills/sugikit/references/polish.md +123 -0
  71. package/skills/sugikit/references/seo.md +150 -0
  72. package/skills/sugikit/references/tanstack-start.md +246 -0
@@ -0,0 +1,17 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ export async function detectFramework(projectDir) {
4
+ let pkg;
5
+ try {
6
+ pkg = JSON.parse(await readFile(join(projectDir, "package.json"), "utf8"));
7
+ }
8
+ catch {
9
+ return "unknown";
10
+ }
11
+ const deps = { ...pkg.dependencies, ...pkg.devDependencies };
12
+ if ("next" in deps)
13
+ return "next";
14
+ if ("@tanstack/react-start" in deps)
15
+ return "tanstack-start";
16
+ return "unknown";
17
+ }
@@ -0,0 +1,48 @@
1
+ import { type FetchLike, type FetchResult } from "./http.js";
2
+ import { type Finding, type Framework, type PageSpeedScores, type PageType, type Report, type Screenshot } from "./report.js";
3
+ import { type PageContext, type RuleResult, type TextResource } from "./rules/types.js";
4
+ export interface GatherOptions {
5
+ fetch?: FetchLike;
6
+ framework?: Framework | Promise<Framework>;
7
+ page?: FetchResult;
8
+ isLocalRun?: (pageUrl: string) => boolean;
9
+ pageType?: PageType;
10
+ checkLinks?: boolean;
11
+ }
12
+ export interface GatheredPage extends PageContext {
13
+ status: number;
14
+ sitemapNote?: RuleResult;
15
+ sitemapFailure?: RuleResult;
16
+ }
17
+ export declare const ORIGIN_RULES: Set<string>;
18
+ export declare function httpStatusResult(status: number, url: string): RuleResult;
19
+ type SitemapResource = TextResource & {
20
+ url: string;
21
+ };
22
+ export declare function resolveSitemap(robots: TextResource | undefined, pageUrl: string, framework?: Framework, options?: Pick<GatherOptions, "fetch" | "isLocalRun">): Promise<{
23
+ sitemap?: SitemapResource;
24
+ note?: RuleResult;
25
+ failure?: RuleResult;
26
+ }>;
27
+ export declare function gatherPage(url: string, options?: GatherOptions): Promise<GatheredPage>;
28
+ export declare function runRules(ctx: PageContext): RuleResult[];
29
+ export declare function isPageError(page: Pick<GatheredPage, "status">): boolean;
30
+ export declare function runStaticRules(page: GatheredPage, options?: {
31
+ notes?: RuleResult[];
32
+ }): RuleResult[];
33
+ export declare function toFinding(item: RuleResult, ctx: Pick<PageContext, "framework" | "url">): Finding;
34
+ export interface ReportInput {
35
+ target: string;
36
+ url: string;
37
+ framework: Framework;
38
+ pageType?: PageType;
39
+ startedAt: string;
40
+ finishedAt?: string;
41
+ results: RuleResult[];
42
+ pagespeed?: PageSpeedScores;
43
+ states?: string[];
44
+ screenshots?: Screenshot[];
45
+ }
46
+ export declare function toReport(input: ReportInput): Report;
47
+ export declare function checkStatic(url: string, options?: GatherOptions): Promise<Report>;
48
+ export {};
package/dist/gather.js ADDED
@@ -0,0 +1,211 @@
1
+ // Worker-safe static checks: fetch the page, its origin files and its same-origin links with an injected fetch,
2
+ // run the SEO, GEO and static convert rules, build the report. No node:* imports, no undici, no playwright.
3
+ import { load } from "cheerio";
4
+ import { defaultFetch, fetchPageWith, fetchTextWith, isBlockedResponse, isLoopbackUrl } from "./http.js";
5
+ import { checkLinks, internalLinks } from "./links.js";
6
+ import { REPORT_VERSION, reportSchema, } from "./report.js";
7
+ import { convertRules, detectPageType } from "./rules/convert.js";
8
+ import { fixFor } from "./rules/fixes.js";
9
+ import { geoRules } from "./rules/geo.js";
10
+ import { parseRobots } from "./rules/robots.js";
11
+ import { seoRules } from "./rules/seo.js";
12
+ import { result, validText } from "./rules/types.js";
13
+ import { scoreCategory } from "./score.js";
14
+ // Rules that read robots.txt, the sitemap or llms files, not the page itself.
15
+ export const ORIGIN_RULES = new Set([
16
+ "seo/robots-txt",
17
+ "seo/robots-sitemap",
18
+ "seo/sitemap",
19
+ "seo/sitemap-lastmod",
20
+ "geo/llms-txt",
21
+ "geo/llms-txt-format",
22
+ "geo/llms-full-txt",
23
+ "geo/ai-crawlers",
24
+ ]);
25
+ export function httpStatusResult(status, url) {
26
+ const ok = status >= 200 && status < 300;
27
+ const blocked = isBlockedResponse({ status, body: "" }) ? "; bot protection or a firewall may block automated requests" : "";
28
+ return result("seo/http-status", "error", ok, ok ? `Page returned HTTP ${status}` : `Page returned HTTP ${status}, not 2xx${blocked}`, {
29
+ url,
30
+ });
31
+ }
32
+ async function tryFetch(fetcher, url) {
33
+ try {
34
+ return await fetchTextWith(fetcher, url);
35
+ }
36
+ catch {
37
+ return undefined;
38
+ }
39
+ }
40
+ function resource(res) {
41
+ if (!res)
42
+ return undefined;
43
+ const contentType = res.headers["content-type"];
44
+ return { status: res.status, body: res.body, ...(contentType ? { contentType } : {}) };
45
+ }
46
+ async function fetchSitemap(fetcher, url) {
47
+ const fetched = resource(await tryFetch(fetcher, url));
48
+ return fetched ? { ...fetched, url } : undefined;
49
+ }
50
+ function listedSitemap(robots, robotsUrl) {
51
+ const listed = validText(robots) ? parseRobots(robots.body).sitemaps[0] : undefined;
52
+ if (!listed)
53
+ return undefined;
54
+ try {
55
+ return new URL(listed, robotsUrl);
56
+ }
57
+ catch {
58
+ return undefined;
59
+ }
60
+ }
61
+ // robots.txt on a preview or dev server often lists the production sitemap. A local run checks the same path
62
+ // on the tested origin then. A public page must list a sitemap crawlers can reach, so it is never swapped.
63
+ export async function resolveSitemap(robots, pageUrl, framework = "unknown", options = {}) {
64
+ const fetcher = options.fetch ?? defaultFetch;
65
+ const isLocalRun = options.isLocalRun ?? isLoopbackUrl;
66
+ const origin = new URL(pageUrl).origin;
67
+ const listed = listedSitemap(robots, new URL("/robots.txt", origin).href);
68
+ if (!listed) {
69
+ const sitemap = await fetchSitemap(fetcher, new URL("/sitemap.xml", origin).href);
70
+ return sitemap ? { sitemap } : {};
71
+ }
72
+ if (listed.host === new URL(pageUrl).host) {
73
+ const sitemap = await fetchSitemap(fetcher, listed.href);
74
+ return sitemap ? { sitemap } : {};
75
+ }
76
+ if (!isLocalRun(pageUrl)) {
77
+ if (isLoopbackUrl(listed.href)) {
78
+ const failure = result("seo/sitemap", "error", false, `robots.txt lists the sitemap on ${listed.host}; crawlers cannot reach it. Use the production URL.`, { url: listed.href, fix: fixFor("seo/sitemap-local", framework) });
79
+ return { failure };
80
+ }
81
+ const sitemap = await fetchSitemap(fetcher, listed.href);
82
+ if (sitemap)
83
+ return { sitemap };
84
+ return { failure: result("seo/sitemap", "error", false, "Sitemap returned no response", { url: listed.href }) };
85
+ }
86
+ const localUrl = new URL(listed.pathname + listed.search, origin).href;
87
+ const note = result("seo/sitemap-host", "info", false, `robots.txt lists the sitemap on ${listed.host}; checked ${localUrl} instead. This is normal for a preview or dev server.`, { url: listed.href });
88
+ const sitemap = await fetchSitemap(fetcher, localUrl);
89
+ return sitemap ? { sitemap, note } : { note };
90
+ }
91
+ // Fetches the page (unless options.page is given), robots.txt, the sitemap, llms.txt and llms-full.txt.
92
+ // Throws when the page itself cannot be fetched; the origin files count as missing then.
93
+ export async function gatherPage(url, options = {}) {
94
+ const fetcher = options.fetch ?? defaultFetch;
95
+ const frameworkPromise = Promise.resolve(options.framework ?? "unknown");
96
+ const page = options.page ?? (await fetchPageWith(fetcher, url));
97
+ const origin = new URL(page.finalUrl).origin;
98
+ const at = (path) => new URL(path, origin).href;
99
+ const robotsPromise = tryFetch(fetcher, at("/robots.txt"));
100
+ const [framework, robots, { sitemap, note: sitemapNote, failure: sitemapFailure }, llms, llmsFull] = await Promise.all([
101
+ frameworkPromise,
102
+ robotsPromise,
103
+ Promise.all([robotsPromise, frameworkPromise]).then(([res, fw]) => resolveSitemap(resource(res), page.finalUrl, fw, {
104
+ fetch: fetcher,
105
+ ...(options.isLocalRun ? { isLocalRun: options.isLocalRun } : {}),
106
+ })),
107
+ tryFetch(fetcher, at("/llms.txt")),
108
+ tryFetch(fetcher, at("/llms-full.txt")),
109
+ ]);
110
+ const $ = load(page.body);
111
+ const ctx = {
112
+ url: page.finalUrl,
113
+ html: page.body,
114
+ $,
115
+ headers: page.headers,
116
+ framework,
117
+ status: page.status,
118
+ pageType: options.pageType ?? detectPageType($),
119
+ };
120
+ if (options.checkLinks !== false && page.status < 400) {
121
+ ctx.linkChecks = await checkLinks(fetcher, internalLinks($, page.finalUrl));
122
+ }
123
+ const robotsTxt = resource(robots);
124
+ if (robotsTxt)
125
+ ctx.robotsTxt = robotsTxt;
126
+ if (sitemap)
127
+ ctx.sitemap = sitemap;
128
+ const llmsTxt = resource(llms);
129
+ if (llmsTxt)
130
+ ctx.llmsTxt = llmsTxt;
131
+ const llmsFullTxt = resource(llmsFull);
132
+ if (llmsFullTxt)
133
+ ctx.llmsFullTxt = llmsFullTxt;
134
+ if (sitemapNote)
135
+ ctx.sitemapNote = sitemapNote;
136
+ if (sitemapFailure)
137
+ ctx.sitemapFailure = sitemapFailure;
138
+ return ctx;
139
+ }
140
+ export function runRules(ctx) {
141
+ return [...seoRules, ...geoRules, ...convertRules].map((rule) => rule(ctx));
142
+ }
143
+ export function isPageError(page) {
144
+ return page.status >= 400;
145
+ }
146
+ // The SEO, GEO and static convert results of `sugikit check --no-browser`, in report order. `notes` go right after
147
+ // seo/http-status (the CLI puts its seo/fetch-blocked note there).
148
+ export function runStaticRules(page, options = {}) {
149
+ const pageError = isPageError(page);
150
+ const rules = runRules(page)
151
+ .filter((item) => !pageError || ORIGIN_RULES.has(item.id))
152
+ .map((item) => (page.sitemapFailure && item.id === "seo/sitemap" ? page.sitemapFailure : item));
153
+ return [
154
+ httpStatusResult(page.status, page.url),
155
+ ...(options.notes ?? []),
156
+ ...rules,
157
+ ...(page.sitemapNote ? [page.sitemapNote] : []),
158
+ ];
159
+ }
160
+ export function toFinding(item, ctx) {
161
+ return {
162
+ id: item.id,
163
+ category: item.category,
164
+ rule: item.id.slice(item.id.indexOf("/") + 1),
165
+ severity: item.severity,
166
+ message: item.message,
167
+ ...(item.selector ? { selector: item.selector } : {}),
168
+ url: item.url ?? ctx.url,
169
+ ...(item.evidence ? { evidence: item.evidence } : {}),
170
+ fix: item.fix ?? fixFor(item.id, ctx.framework),
171
+ ...(item.engine ? { engine: item.engine } : {}),
172
+ ...(item.confidence ? { confidence: item.confidence } : {}),
173
+ ...(item.source ? { source: item.source } : {}),
174
+ ...(item.needsOwner !== undefined ? { needsOwner: item.needsOwner } : {}),
175
+ };
176
+ }
177
+ // Scores the results and builds the report JSON, validated against reportSchema.
178
+ export function toReport(input) {
179
+ const { results } = input;
180
+ return reportSchema.parse({
181
+ version: REPORT_VERSION,
182
+ target: input.target,
183
+ framework: input.framework,
184
+ startedAt: input.startedAt,
185
+ finishedAt: input.finishedAt ?? new Date().toISOString(),
186
+ scores: {
187
+ polish: scoreCategory(results, "polish"),
188
+ seo: scoreCategory(results, "seo"),
189
+ geo: scoreCategory(results, "geo"),
190
+ convert: scoreCategory(results, "convert"),
191
+ ...(input.pagespeed ? { pagespeed: input.pagespeed } : {}),
192
+ },
193
+ ...(input.pageType ? { pageType: input.pageType } : {}),
194
+ findings: results.filter((item) => !item.pass).map((item) => toFinding(item, input)),
195
+ ...(input.states ? { states: input.states } : {}),
196
+ ...(input.screenshots ? { screenshots: input.screenshots } : {}),
197
+ });
198
+ }
199
+ // The same report as `sugikit check <url> --no-browser` (framework detection aside: pass options.framework).
200
+ export async function checkStatic(url, options = {}) {
201
+ const startedAt = new Date().toISOString();
202
+ const page = await gatherPage(url, options);
203
+ return toReport({
204
+ target: url,
205
+ url: page.url,
206
+ framework: page.framework,
207
+ ...(page.pageType ? { pageType: page.pageType } : {}),
208
+ startedAt,
209
+ results: runStaticRules(page),
210
+ });
211
+ }
package/dist/html.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { Report } from "./report.js";
2
+ export { escapeHtml, polishRan, splitFindings, type FindingSplit } from "./render.js";
3
+ export interface RunInput {
4
+ report: Report;
5
+ path: string;
6
+ psi?: Report;
7
+ }
8
+ export interface HtmlInput {
9
+ before?: RunInput;
10
+ after: RunInput;
11
+ out: string;
12
+ }
13
+ export declare function pngSize(path: string): {
14
+ width: number;
15
+ height: number;
16
+ } | undefined;
17
+ export declare function renderHtml(input: HtmlInput): string;
package/dist/html.js ADDED
@@ -0,0 +1,43 @@
1
+ import { closeSync, openSync, readSync } from "node:fs";
2
+ import { dirname, relative, resolve, sep } from "node:path";
3
+ import { pngSizeFromBytes, renderHtml as renderPage } from "./render.js";
4
+ export { escapeHtml, polishRan, splitFindings } from "./render.js";
5
+ export function pngSize(path) {
6
+ let fd;
7
+ try {
8
+ fd = openSync(path, "r");
9
+ const head = Buffer.alloc(24);
10
+ if (readSync(fd, head, 0, 24, 0) < 24)
11
+ return undefined;
12
+ return pngSizeFromBytes(head);
13
+ }
14
+ catch {
15
+ return undefined;
16
+ }
17
+ finally {
18
+ if (fd !== undefined)
19
+ closeSync(fd);
20
+ }
21
+ }
22
+ function imageHref(reportPath, shotPath, out) {
23
+ const file = resolve(dirname(reportPath), shotPath);
24
+ const rel = relative(dirname(out), file).split(sep).join("/");
25
+ return { href: rel.split("/").map(encodeURIComponent).join("/"), file };
26
+ }
27
+ // Resolves screenshot files next to the report and reads their sizes, then renders with the pure renderer.
28
+ function renderRun(input, out) {
29
+ const images = {};
30
+ for (const shot of input.report.screenshots ?? []) {
31
+ const { href, file } = imageHref(input.path, shot.path, out);
32
+ const size = pngSize(file);
33
+ if (size)
34
+ images[shot.state] = { src: href, ...size };
35
+ }
36
+ return { report: input.report, images, ...(input.psi ? { psi: input.psi } : {}) };
37
+ }
38
+ export function renderHtml(input) {
39
+ return renderPage({
40
+ after: renderRun(input.after, input.out),
41
+ ...(input.before ? { before: renderRun(input.before, input.out) } : {}),
42
+ });
43
+ }
package/dist/http.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ export interface FetchResult {
2
+ status: number;
3
+ headers: Record<string, string>;
4
+ body: string;
5
+ finalUrl: string;
6
+ }
7
+ export type FetchLike = (url: string, init: {
8
+ redirect: "follow" | "manual";
9
+ signal: AbortSignal;
10
+ headers: Record<string, string>;
11
+ method?: "GET" | "HEAD";
12
+ }) => Promise<{
13
+ status: number;
14
+ url: string;
15
+ headers: {
16
+ forEach(cb: (value: string, key: string) => void): void;
17
+ };
18
+ text(): Promise<string>;
19
+ }>;
20
+ export declare const FETCH_TIMEOUT_MS = 10000;
21
+ export declare const PAGE_RETRY_TIMEOUT_MS = 15000;
22
+ export declare const INCONCLUSIVE_STATUS: ReadonlySet<number>;
23
+ export declare const BROWSER_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/159.0.0.0 Safari/537.36";
24
+ export declare const REQUEST_HEADERS: {
25
+ readonly "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/159.0.0.0 Safari/537.36";
26
+ readonly accept: "text/html,application/xhtml+xml,application/xml;q=0.9,text/plain;q=0.8,*/*;q=0.7";
27
+ readonly "accept-language": "en-US,en;q=0.9";
28
+ };
29
+ export declare function isChallengePage(body: string): boolean;
30
+ export declare function isBlockedResponse(res: Pick<FetchResult, "status" | "body">): boolean;
31
+ export declare function isLocalHostname(hostname: string): boolean;
32
+ export declare function isLocalHost(url: string): boolean;
33
+ export declare function isLoopbackUrl(url: string): boolean;
34
+ export declare function fetchTextWith(fetcher: FetchLike, url: string, timeoutMs?: number): Promise<FetchResult>;
35
+ export declare function fetchPageWith(fetcher: FetchLike, url: string): Promise<FetchResult>;
36
+ export declare const defaultFetch: FetchLike;
package/dist/http.js ADDED
@@ -0,0 +1,57 @@
1
+ // Worker-safe HTTP helpers: no node:* imports and no undici. The CLI adds local TLS handling in fetch.ts.
2
+ export const FETCH_TIMEOUT_MS = 10_000;
3
+ // The page fetch tries once more, with this longer timeout, after a timeout or network error (not after an HTTP
4
+ // error status): a slow first byte on a cold server is common and usually fine on the second try.
5
+ export const PAGE_RETRY_TIMEOUT_MS = 15_000;
6
+ // Answers that bot protection, a login wall or a rate limit gives to automated requests: not proof a link is broken.
7
+ export const INCONCLUSIVE_STATUS = new Set([401, 403, 429, 503]);
8
+ // A current desktop Chrome user agent with no product token: bot filters answer 403 to tool user agents.
9
+ export const BROWSER_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/159.0.0.0 Safari/537.36";
10
+ export const REQUEST_HEADERS = {
11
+ "user-agent": BROWSER_USER_AGENT,
12
+ accept: "text/html,application/xhtml+xml,application/xml;q=0.9,text/plain;q=0.8,*/*;q=0.7",
13
+ "accept-language": "en-US,en;q=0.9",
14
+ };
15
+ const BLOCKED_STATUSES = new Set([403, 429, 503]);
16
+ // "challenge-platform" alone also shows up in the bot script Cloudflare adds to normal pages, so it only
17
+ // counts together with a blocking status, which flags the page anyway.
18
+ export function isChallengePage(body) {
19
+ return body.includes("cf-chl") || /<title>\s*Just a moment\.\.\.\s*<\/title>/i.test(body);
20
+ }
21
+ // Bot protection answered instead of the page: a blocking status or a challenge page.
22
+ export function isBlockedResponse(res) {
23
+ return BLOCKED_STATUSES.has(res.status) || isChallengePage(res.body);
24
+ }
25
+ export function isLocalHostname(hostname) {
26
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname.endsWith(".localhost");
27
+ }
28
+ export function isLocalHost(url) {
29
+ return isLocalHostname(new URL(url).hostname);
30
+ }
31
+ // Hosts only the machine running the server can reach. Wider than isLocalHost, which decides TLS handling.
32
+ export function isLoopbackUrl(url) {
33
+ const hostname = new URL(url).hostname;
34
+ return isLocalHostname(hostname) || hostname === "[::1]" || hostname === "0.0.0.0";
35
+ }
36
+ export async function fetchTextWith(fetcher, url, timeoutMs = FETCH_TIMEOUT_MS) {
37
+ const response = await fetcher(url, {
38
+ redirect: "follow",
39
+ signal: AbortSignal.timeout(timeoutMs),
40
+ headers: { ...REQUEST_HEADERS },
41
+ });
42
+ const headers = {};
43
+ response.headers.forEach((value, key) => {
44
+ headers[key] = value;
45
+ });
46
+ return { status: response.status, headers, body: await response.text(), finalUrl: response.url || url };
47
+ }
48
+ // The page itself: one retry with a longer timeout when the first try throws (timeout or network error).
49
+ export async function fetchPageWith(fetcher, url) {
50
+ try {
51
+ return await fetchTextWith(fetcher, url, FETCH_TIMEOUT_MS);
52
+ }
53
+ catch {
54
+ return fetchTextWith(fetcher, url, PAGE_RETRY_TIMEOUT_MS);
55
+ }
56
+ }
57
+ export const defaultFetch = (url, init) => globalThis.fetch(url, init);
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};