sugikit 0.0.1 → 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.
- package/LICENSE +29 -21
- package/README.md +45 -0
- package/dist/api/browser.d.ts +5 -0
- package/dist/api/browser.js +5 -0
- package/dist/api/rules.d.ts +12 -0
- package/dist/api/rules.js +14 -0
- package/dist/auth.d.ts +156 -0
- package/dist/auth.js +547 -0
- package/dist/browser.d.ts +25 -0
- package/dist/browser.js +120 -0
- package/dist/check.d.ts +21 -0
- package/dist/check.js +89 -0
- package/dist/convert-probe.d.ts +21 -0
- package/dist/convert-probe.js +647 -0
- package/dist/fetch.d.ts +6 -0
- package/dist/fetch.js +22 -0
- package/dist/framework.d.ts +2 -0
- package/dist/framework.js +17 -0
- package/dist/gather.d.ts +48 -0
- package/dist/gather.js +211 -0
- package/dist/html.d.ts +17 -0
- package/dist/html.js +43 -0
- package/dist/http.d.ts +36 -0
- package/dist/http.js +57 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +331 -0
- package/dist/links.d.ts +8 -0
- package/dist/links.js +69 -0
- package/dist/polish-run.d.ts +115 -0
- package/dist/polish-run.js +357 -0
- package/dist/probe.d.ts +14 -0
- package/dist/probe.js +457 -0
- package/dist/psi-scores.d.ts +4 -0
- package/dist/psi-scores.js +7 -0
- package/dist/psi.d.ts +56 -0
- package/dist/psi.js +192 -0
- package/dist/release.d.ts +1 -0
- package/dist/release.js +5 -0
- package/dist/render.d.ts +28 -0
- package/dist/render.js +270 -0
- package/dist/report.d.ts +227 -0
- package/dist/report.js +69 -0
- package/dist/rules/convert-render.d.ts +100 -0
- package/dist/rules/convert-render.js +279 -0
- package/dist/rules/convert.d.ts +15 -0
- package/dist/rules/convert.js +322 -0
- package/dist/rules/fixes.d.ts +16 -0
- package/dist/rules/fixes.js +536 -0
- package/dist/rules/geo.d.ts +15 -0
- package/dist/rules/geo.js +112 -0
- package/dist/rules/polish.d.ts +59 -0
- package/dist/rules/polish.js +223 -0
- package/dist/rules/robots.d.ts +14 -0
- package/dist/rules/robots.js +69 -0
- package/dist/rules/seo.d.ts +31 -0
- package/dist/rules/seo.js +208 -0
- package/dist/rules/types.d.ts +55 -0
- package/dist/rules/types.js +78 -0
- package/dist/score.d.ts +6 -0
- package/dist/score.js +23 -0
- package/dist/skills.d.ts +9 -0
- package/dist/skills.js +22 -0
- package/package.json +64 -4
- package/skills/INSTALL_PROMPT.md +9 -0
- package/skills/sugikit/SKILL.md +147 -0
- package/skills/sugikit/references/convert.md +127 -0
- package/skills/sugikit/references/geo.md +97 -0
- package/skills/sugikit/references/next.md +254 -0
- package/skills/sugikit/references/pagespeed.md +94 -0
- package/skills/sugikit/references/polish.md +123 -0
- package/skills/sugikit/references/seo.md +150 -0
- package/skills/sugikit/references/tanstack-start.md +246 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { dirname, relative, resolve } from "node:path";
|
|
7
|
+
import { parseArgs } from "node:util";
|
|
8
|
+
import { DEFAULT_BUDGET_SECONDS, runChecks, shotsDirFor, toFinding } from "./check.js";
|
|
9
|
+
import { detectFramework } from "./framework.js";
|
|
10
|
+
import { polishRan, renderHtml } from "./html.js";
|
|
11
|
+
import { DEFAULT_RUNS, minScore, runPsi } from "./psi.js";
|
|
12
|
+
import { pageTypeSchema, REPORT_VERSION, reportSchema } from "./report.js";
|
|
13
|
+
import { AuthError, defaultContext, login, logout, redact, readAuth, requirePlan, whoami } from "./auth.js";
|
|
14
|
+
import { installSkills } from "./skills.js";
|
|
15
|
+
const HELP = `sugikit: check a landing page and write a JSON report for coding agents
|
|
16
|
+
|
|
17
|
+
Usage:
|
|
18
|
+
sugikit check <url> [--project <dir>] [--out <file>] [--no-browser] [--psi] [--runs <n>] [--page-type <type>] [--timeout <s>]
|
|
19
|
+
Check a URL and write a JSON report
|
|
20
|
+
sugikit psi <url> [--project <dir>] [--out <file>] [--runs <n>]
|
|
21
|
+
Run PageSpeed Insights only and write a JSON report
|
|
22
|
+
sugikit report [--before <file>] [--after <file>] [--psi-before <file>] [--psi-after <file>] [--out <file>]
|
|
23
|
+
Write a local before/after HTML page from two reports
|
|
24
|
+
sugikit skills install [--dir <project>]
|
|
25
|
+
Copy the sugikit agent skill into <project>/.claude/skills/
|
|
26
|
+
sugikit browsers install
|
|
27
|
+
Install the headless Chromium build that Sugikit's own Playwright version needs
|
|
28
|
+
sugikit login Log in to your Sugikit account in the browser (device code)
|
|
29
|
+
sugikit logout Log out and delete the saved login and license files
|
|
30
|
+
sugikit whoami Print the account email and plan (also: sugikit account)
|
|
31
|
+
sugikit --version Print the version
|
|
32
|
+
sugikit --help Print this help
|
|
33
|
+
|
|
34
|
+
Options:
|
|
35
|
+
--project <dir> Project directory for framework detection (default: cwd)
|
|
36
|
+
--out <file> Output path (default: .sugikit/report.json, sugikit psi: .sugikit/psi.json, sugikit report: .sugikit/report.html);
|
|
37
|
+
sugikit check screenshots go to <name>-shots/ next to the report (.sugikit/report-shots/)
|
|
38
|
+
--no-browser Skip the headless browser polish checks
|
|
39
|
+
--psi Add PageSpeed Insights (needs a public URL and the PSI_API_KEY environment variable)
|
|
40
|
+
--page-type <type> product, launch or waitlist; overrides the detected page type for the convert checks
|
|
41
|
+
--timeout <s> sugikit check: soft time budget in seconds (default: ${DEFAULT_BUDGET_SECONDS}); when it runs out, the dark,
|
|
42
|
+
reduced motion and CTA click checks are skipped and reported as polish/slow-page
|
|
43
|
+
--runs <n> PageSpeed runs per strategy, the best score per category is kept (default: ${DEFAULT_RUNS})
|
|
44
|
+
--before <file> sugikit report: baseline report (default: .sugikit/before.json, skipped when missing)
|
|
45
|
+
--after <file> sugikit report: latest report (default: .sugikit/report.json)
|
|
46
|
+
--psi-before <file> sugikit report: sugikit psi report for the baseline
|
|
47
|
+
--psi-after <file> sugikit report: sugikit psi report for the latest run
|
|
48
|
+
--dir <project> sugikit skills install: project directory (default: cwd)
|
|
49
|
+
`;
|
|
50
|
+
function version() {
|
|
51
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
52
|
+
return pkg.version;
|
|
53
|
+
}
|
|
54
|
+
function isHttpUrl(value) {
|
|
55
|
+
try {
|
|
56
|
+
const { protocol } = new URL(value);
|
|
57
|
+
return protocol === "http:" || protocol === "https:";
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function counts(report) {
|
|
64
|
+
const count = (severity) => report.findings.filter((f) => f.severity === severity).length;
|
|
65
|
+
return `errors ${count("error")} warnings ${count("warn")} info ${count("info")}`;
|
|
66
|
+
}
|
|
67
|
+
function summary(report, out) {
|
|
68
|
+
const { pagespeed } = report.scores;
|
|
69
|
+
return [
|
|
70
|
+
`sugikit check ${report.target} (framework: ${report.framework})`,
|
|
71
|
+
[
|
|
72
|
+
polishRan(report) ? `polish ${report.scores.polish}/100` : "polish skipped",
|
|
73
|
+
`seo ${report.scores.seo}/100`,
|
|
74
|
+
`geo ${report.scores.geo}/100`,
|
|
75
|
+
...(report.scores.convert !== undefined ? [`convert ${report.scores.convert}/100`] : []),
|
|
76
|
+
...(pagespeed ? [`pagespeed ${minScore(pagespeed)}/100 (${pagespeed.method})`] : []),
|
|
77
|
+
].join(" "),
|
|
78
|
+
counts(report),
|
|
79
|
+
`report: ${out}`,
|
|
80
|
+
"",
|
|
81
|
+
].join("\n");
|
|
82
|
+
}
|
|
83
|
+
function psiTable(report, out) {
|
|
84
|
+
const { pagespeed } = report.scores;
|
|
85
|
+
const lines = [`sugikit psi ${report.target} (framework: ${report.framework})`];
|
|
86
|
+
if (pagespeed) {
|
|
87
|
+
const row = (label, mobile, desktop) => `${label.padEnd(16)}${String(mobile).padStart(6)}${String(desktop).padStart(9)}`;
|
|
88
|
+
lines.push(`${pagespeed.method}`, `${"category".padEnd(16)}${"mobile".padStart(6)}${"desktop".padStart(9)}`, row("performance", pagespeed.mobile.performance, pagespeed.desktop.performance), row("accessibility", pagespeed.mobile.accessibility, pagespeed.desktop.accessibility), row("best practices", pagespeed.mobile.bestPractices, pagespeed.desktop.bestPractices), row("seo", pagespeed.mobile.seo, pagespeed.desktop.seo));
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
lines.push("pagespeed skipped", ...report.findings.map((f) => `${f.id}: ${f.message}`));
|
|
92
|
+
}
|
|
93
|
+
lines.push(counts(report), `report: ${out}`, "");
|
|
94
|
+
return lines.join("\n");
|
|
95
|
+
}
|
|
96
|
+
async function writeReport(out, report) {
|
|
97
|
+
await mkdir(dirname(out), { recursive: true });
|
|
98
|
+
await writeFile(out, `${JSON.stringify(report, null, 2)}\n`);
|
|
99
|
+
}
|
|
100
|
+
async function psiReport(target, runs, projectDir) {
|
|
101
|
+
const startedAt = new Date().toISOString();
|
|
102
|
+
const framework = await detectFramework(projectDir);
|
|
103
|
+
const psi = await runPsi(target, { runs, framework });
|
|
104
|
+
return reportSchema.parse({
|
|
105
|
+
version: REPORT_VERSION,
|
|
106
|
+
target,
|
|
107
|
+
framework,
|
|
108
|
+
startedAt,
|
|
109
|
+
finishedAt: new Date().toISOString(),
|
|
110
|
+
scores: psi.pagespeed ? { pagespeed: psi.pagespeed } : {},
|
|
111
|
+
findings: psi.results.filter((item) => !item.pass).map((item) => toFinding(item, { framework, url: target })),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function readReport(path) {
|
|
115
|
+
return reportSchema.parse(JSON.parse(readFileSync(path, "utf8")));
|
|
116
|
+
}
|
|
117
|
+
function runInput(path, psiPath) {
|
|
118
|
+
return { report: readReport(path), path, ...(psiPath ? { psi: readReport(resolve(psiPath)) } : {}) };
|
|
119
|
+
}
|
|
120
|
+
function htmlReport(values) {
|
|
121
|
+
const afterPath = resolve(values.after ?? ".sugikit/report.json");
|
|
122
|
+
const beforePath = resolve(values.before ?? ".sugikit/before.json");
|
|
123
|
+
const out = resolve(values.out ?? ".sugikit/report.html");
|
|
124
|
+
if (!existsSync(afterPath)) {
|
|
125
|
+
process.stderr.write(`no report at ${afterPath}; run sugikit check first or pass --after <file>\n`);
|
|
126
|
+
return 1;
|
|
127
|
+
}
|
|
128
|
+
if (values.before !== undefined && !existsSync(beforePath)) {
|
|
129
|
+
process.stderr.write(`no report at ${beforePath}\n`);
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
const hasBefore = existsSync(beforePath);
|
|
133
|
+
if (!hasBefore && values["psi-before"] !== undefined) {
|
|
134
|
+
process.stderr.write(`--psi-before needs a before report\n`);
|
|
135
|
+
return 1;
|
|
136
|
+
}
|
|
137
|
+
let html;
|
|
138
|
+
try {
|
|
139
|
+
html = renderHtml({
|
|
140
|
+
after: runInput(afterPath, values["psi-after"]),
|
|
141
|
+
...(hasBefore ? { before: runInput(beforePath, values["psi-before"]) } : {}),
|
|
142
|
+
out,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
process.stderr.write(`could not read reports: ${error.message}\n`);
|
|
147
|
+
return 1;
|
|
148
|
+
}
|
|
149
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
150
|
+
writeFileSync(out, html);
|
|
151
|
+
process.stderr.write(hasBefore ? `before: ${beforePath}\nafter: ${afterPath}\n` : `single run (no before report): ${afterPath}\n`);
|
|
152
|
+
process.stdout.write(`${out}\n`);
|
|
153
|
+
return 0;
|
|
154
|
+
}
|
|
155
|
+
async function skillsInstall(dir) {
|
|
156
|
+
const project = resolve(dir ?? ".");
|
|
157
|
+
try {
|
|
158
|
+
const done = await installSkills(project);
|
|
159
|
+
if (done.replaced)
|
|
160
|
+
process.stderr.write(`replaced the existing skill at ${done.target}\n`);
|
|
161
|
+
process.stdout.write([`installed sugikit skill to ${done.target}`, ...done.files.map((file) => ` ${relative(project, resolve(done.target, file))}`), ""].join("\n"));
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
process.stderr.write(`could not install the skill: ${error.message}\n`);
|
|
166
|
+
return 1;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// npx playwright resolves the latest Playwright, whose Chromium build can differ from the one Sugikit's pinned version needs.
|
|
170
|
+
async function browsersInstall() {
|
|
171
|
+
const require = createRequire(import.meta.url);
|
|
172
|
+
const pkgPath = require.resolve("playwright/package.json");
|
|
173
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
174
|
+
const cli = resolve(dirname(pkgPath), pkg.bin.playwright);
|
|
175
|
+
process.stderr.write(`playwright ${pkg.version}: ${process.execPath} ${cli} install chromium\n`);
|
|
176
|
+
return new Promise((done) => {
|
|
177
|
+
const child = spawn(process.execPath, [cli, "install", "chromium"], { stdio: "inherit" });
|
|
178
|
+
child.on("error", (error) => {
|
|
179
|
+
process.stderr.write(`could not run the Playwright installer: ${error.message}\n`);
|
|
180
|
+
done(1);
|
|
181
|
+
});
|
|
182
|
+
child.on("exit", (code) => done(code ?? 1));
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// Account commands print a plain message on failure. The token never reaches the output.
|
|
186
|
+
async function account(run) {
|
|
187
|
+
const ctx = defaultContext();
|
|
188
|
+
try {
|
|
189
|
+
return await run(ctx);
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
let token;
|
|
193
|
+
try {
|
|
194
|
+
token = readAuth(ctx)?.access_token;
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
token = undefined;
|
|
198
|
+
}
|
|
199
|
+
const message = error instanceof AuthError ? error.message : `unexpected error: ${error.message}`;
|
|
200
|
+
process.stderr.write(`${redact(message, [token])}\n`);
|
|
201
|
+
return 1;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async function main(argv) {
|
|
205
|
+
let parsed;
|
|
206
|
+
try {
|
|
207
|
+
parsed = parseArgs({
|
|
208
|
+
args: argv,
|
|
209
|
+
allowPositionals: true,
|
|
210
|
+
options: {
|
|
211
|
+
help: { type: "boolean", short: "h" },
|
|
212
|
+
version: { type: "boolean", short: "v" },
|
|
213
|
+
project: { type: "string" },
|
|
214
|
+
out: { type: "string" },
|
|
215
|
+
"no-browser": { type: "boolean" },
|
|
216
|
+
psi: { type: "boolean" },
|
|
217
|
+
runs: { type: "string" },
|
|
218
|
+
timeout: { type: "string" },
|
|
219
|
+
"page-type": { type: "string" },
|
|
220
|
+
before: { type: "string" },
|
|
221
|
+
after: { type: "string" },
|
|
222
|
+
"psi-before": { type: "string" },
|
|
223
|
+
"psi-after": { type: "string" },
|
|
224
|
+
dir: { type: "string" },
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
process.stderr.write(`${error.message}\n\n${HELP}`);
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
const { values, positionals } = parsed;
|
|
233
|
+
if (values.version) {
|
|
234
|
+
process.stdout.write(`${version()}\n`);
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
237
|
+
const [command, target] = positionals;
|
|
238
|
+
if (values.help || command === undefined) {
|
|
239
|
+
process.stdout.write(HELP);
|
|
240
|
+
return 0;
|
|
241
|
+
}
|
|
242
|
+
if (command === "report")
|
|
243
|
+
return htmlReport(values);
|
|
244
|
+
if (command === "login")
|
|
245
|
+
return account(login);
|
|
246
|
+
if (command === "logout")
|
|
247
|
+
return account(logout);
|
|
248
|
+
if (command === "whoami" || command === "account")
|
|
249
|
+
return account(whoami);
|
|
250
|
+
// Hidden: exercises the paid-command gate until the first gated command ships.
|
|
251
|
+
if (command === "__require-plan") {
|
|
252
|
+
if (target !== "kit" && target !== "pro") {
|
|
253
|
+
process.stderr.write("usage: sugikit __require-plan <kit|pro>\n");
|
|
254
|
+
return 1;
|
|
255
|
+
}
|
|
256
|
+
return account(async (ctx) => {
|
|
257
|
+
const license = await requirePlan(target, ctx);
|
|
258
|
+
process.stdout.write(`ok: ${license.plan}${license.cached ? " (cached)" : ""}\n`);
|
|
259
|
+
return 0;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
if (command === "skills") {
|
|
263
|
+
if (target !== "install") {
|
|
264
|
+
process.stderr.write(`unknown skills command: ${target ?? "(none)"}; use sugikit skills install\n`);
|
|
265
|
+
return 1;
|
|
266
|
+
}
|
|
267
|
+
return skillsInstall(values.dir);
|
|
268
|
+
}
|
|
269
|
+
if (command === "browsers") {
|
|
270
|
+
if (target !== "install") {
|
|
271
|
+
process.stderr.write(`unknown browsers command: ${target ?? "(none)"}; use sugikit browsers install\n`);
|
|
272
|
+
return 1;
|
|
273
|
+
}
|
|
274
|
+
return browsersInstall();
|
|
275
|
+
}
|
|
276
|
+
if (command !== "check" && command !== "psi") {
|
|
277
|
+
process.stderr.write(`unknown command: ${command}\n\n${HELP}`);
|
|
278
|
+
return 1;
|
|
279
|
+
}
|
|
280
|
+
if (target === undefined) {
|
|
281
|
+
process.stderr.write(`missing <url>\n\n${HELP}`);
|
|
282
|
+
return 1;
|
|
283
|
+
}
|
|
284
|
+
if (!isHttpUrl(target)) {
|
|
285
|
+
process.stderr.write(`not an http(s) URL: ${target}\n`);
|
|
286
|
+
return 1;
|
|
287
|
+
}
|
|
288
|
+
const runs = values.runs === undefined ? DEFAULT_RUNS : Number(values.runs);
|
|
289
|
+
if (!Number.isInteger(runs) || runs < 1) {
|
|
290
|
+
process.stderr.write(`--runs must be a whole number of 1 or more\n`);
|
|
291
|
+
return 1;
|
|
292
|
+
}
|
|
293
|
+
const budgetSeconds = values.timeout === undefined ? undefined : Number(values.timeout);
|
|
294
|
+
if (budgetSeconds !== undefined && !(Number.isFinite(budgetSeconds) && budgetSeconds > 0)) {
|
|
295
|
+
process.stderr.write(`--timeout must be a number of seconds greater than 0\n`);
|
|
296
|
+
return 1;
|
|
297
|
+
}
|
|
298
|
+
const pageType = values["page-type"] === undefined ? undefined : pageTypeSchema.safeParse(values["page-type"]);
|
|
299
|
+
if (pageType && !pageType.success) {
|
|
300
|
+
process.stderr.write(`--page-type must be product, launch or waitlist\n`);
|
|
301
|
+
return 1;
|
|
302
|
+
}
|
|
303
|
+
if (command === "psi") {
|
|
304
|
+
const out = resolve(values.out ?? ".sugikit/psi.json");
|
|
305
|
+
const report = await psiReport(target, runs, values.project ? resolve(values.project) : process.cwd());
|
|
306
|
+
await writeReport(out, report);
|
|
307
|
+
process.stderr.write(psiTable(report, out));
|
|
308
|
+
return report.scores.pagespeed && !report.findings.some((f) => f.severity === "error") ? 0 : 1;
|
|
309
|
+
}
|
|
310
|
+
const out = resolve(values.out ?? ".sugikit/report.json");
|
|
311
|
+
let report;
|
|
312
|
+
try {
|
|
313
|
+
report = await runChecks(target, {
|
|
314
|
+
...(values.project ? { projectDir: resolve(values.project) } : {}),
|
|
315
|
+
browser: !values["no-browser"],
|
|
316
|
+
outDir: dirname(out),
|
|
317
|
+
shotsDir: shotsDirFor(out),
|
|
318
|
+
...(values.psi ? { psi: { runs } } : {}),
|
|
319
|
+
...(pageType?.data ? { pageType: pageType.data } : {}),
|
|
320
|
+
...(budgetSeconds !== undefined ? { budgetSeconds } : {}),
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
process.stderr.write(`could not check ${target}: ${error.message}\n`);
|
|
325
|
+
return 1;
|
|
326
|
+
}
|
|
327
|
+
await writeReport(out, report);
|
|
328
|
+
process.stderr.write(summary(report, out));
|
|
329
|
+
return report.findings.some((f) => f.severity === "error") ? 1 : 0;
|
|
330
|
+
}
|
|
331
|
+
process.exitCode = await main(process.argv.slice(2));
|
package/dist/links.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CheerioAPI } from "cheerio";
|
|
2
|
+
import { type FetchLike } from "./http.js";
|
|
3
|
+
import type { LinkCheck } from "./rules/types.js";
|
|
4
|
+
export declare const MAX_LINK_CHECKS = 20;
|
|
5
|
+
export declare const LINK_CONCURRENCY = 4;
|
|
6
|
+
export declare const LINK_TIMEOUT_MS = 5000;
|
|
7
|
+
export declare function internalLinks($: CheerioAPI, pageUrl: string, max?: number): string[];
|
|
8
|
+
export declare function checkLinks(fetcher: FetchLike, urls: string[]): Promise<LinkCheck[]>;
|
package/dist/links.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { REQUEST_HEADERS } from "./http.js";
|
|
2
|
+
export const MAX_LINK_CHECKS = 20;
|
|
3
|
+
export const LINK_CONCURRENCY = 4;
|
|
4
|
+
export const LINK_TIMEOUT_MS = 5_000;
|
|
5
|
+
// Same-origin http(s) links in the page, without the hash, without the page itself, in page order.
|
|
6
|
+
export function internalLinks($, pageUrl, max = MAX_LINK_CHECKS) {
|
|
7
|
+
const page = new URL(pageUrl);
|
|
8
|
+
page.hash = "";
|
|
9
|
+
const seen = new Set([page.href]);
|
|
10
|
+
const links = [];
|
|
11
|
+
$("a[href]").each((_, el) => {
|
|
12
|
+
if (links.length >= max)
|
|
13
|
+
return false;
|
|
14
|
+
const href = $(el).attr("href")?.trim();
|
|
15
|
+
if (!href)
|
|
16
|
+
return undefined;
|
|
17
|
+
let target;
|
|
18
|
+
try {
|
|
19
|
+
target = new URL(href, page);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
if (target.origin !== page.origin || (target.protocol !== "http:" && target.protocol !== "https:"))
|
|
25
|
+
return undefined;
|
|
26
|
+
target.hash = "";
|
|
27
|
+
if (seen.has(target.href))
|
|
28
|
+
return undefined;
|
|
29
|
+
seen.add(target.href);
|
|
30
|
+
links.push(target.href);
|
|
31
|
+
return undefined;
|
|
32
|
+
});
|
|
33
|
+
return links;
|
|
34
|
+
}
|
|
35
|
+
async function status(fetcher, url, method) {
|
|
36
|
+
const res = await fetcher(url, {
|
|
37
|
+
method,
|
|
38
|
+
redirect: "follow",
|
|
39
|
+
signal: AbortSignal.timeout(LINK_TIMEOUT_MS),
|
|
40
|
+
headers: { ...REQUEST_HEADERS },
|
|
41
|
+
});
|
|
42
|
+
// Read the body so the connection is released; a HEAD body is empty.
|
|
43
|
+
await res.text().catch(() => "");
|
|
44
|
+
return res.status;
|
|
45
|
+
}
|
|
46
|
+
// HEAD each link (GET when the server answers 405 to HEAD), at most LINK_CONCURRENCY at a time.
|
|
47
|
+
// A link that times out or fails to connect gets status 0; only 4xx counts as broken.
|
|
48
|
+
export async function checkLinks(fetcher, urls) {
|
|
49
|
+
const results = new Array(urls.length);
|
|
50
|
+
let next = 0;
|
|
51
|
+
const worker = async () => {
|
|
52
|
+
while (next < urls.length) {
|
|
53
|
+
const index = next++;
|
|
54
|
+
const url = urls[index];
|
|
55
|
+
let code = 0;
|
|
56
|
+
try {
|
|
57
|
+
code = await status(fetcher, url, "HEAD");
|
|
58
|
+
if (code === 405)
|
|
59
|
+
code = await status(fetcher, url, "GET");
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
code = 0;
|
|
63
|
+
}
|
|
64
|
+
results[index] = { url, status: code };
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
await Promise.all(Array.from({ length: Math.min(LINK_CONCURRENCY, urls.length) }, worker));
|
|
68
|
+
return results;
|
|
69
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { type FetchLike } from "./http.js";
|
|
2
|
+
import { type ConvertRenderData } from "./rules/convert-render.js";
|
|
3
|
+
import { REDUCED_MOTION_STATE, type StateName } from "./rules/polish.js";
|
|
4
|
+
import { type RuleResult } from "./rules/types.js";
|
|
5
|
+
export interface PolishPage {
|
|
6
|
+
goto(url: string, options: {
|
|
7
|
+
waitUntil: "load";
|
|
8
|
+
timeout: number;
|
|
9
|
+
}): Promise<unknown>;
|
|
10
|
+
waitForLoadState(state: "networkidle", options: {
|
|
11
|
+
timeout: number;
|
|
12
|
+
}): Promise<void>;
|
|
13
|
+
waitForTimeout(ms: number): Promise<void>;
|
|
14
|
+
evaluate<R, A>(fn: ((arg: A) => R | Promise<R>) | string, arg?: A): Promise<R>;
|
|
15
|
+
screenshot(options: {
|
|
16
|
+
fullPage: boolean;
|
|
17
|
+
clip: {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
}): Promise<Uint8Array>;
|
|
24
|
+
setViewportSize(size: {
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
emulateMedia(options: {
|
|
29
|
+
colorScheme: "light" | "dark";
|
|
30
|
+
reducedMotion: "reduce" | "no-preference";
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
addInitScript?(script: () => void): Promise<unknown>;
|
|
33
|
+
context?(): RouteHost;
|
|
34
|
+
route?: RouteHost["route"];
|
|
35
|
+
unroute?: RouteHost["unroute"];
|
|
36
|
+
}
|
|
37
|
+
export interface RouteHost {
|
|
38
|
+
route(url: string, handler: (route: RouteLike) => Promise<void>): Promise<unknown>;
|
|
39
|
+
unroute?(url: string, handler: (route: RouteLike) => Promise<void>): Promise<unknown>;
|
|
40
|
+
}
|
|
41
|
+
export interface RouteLike {
|
|
42
|
+
request(): {
|
|
43
|
+
method(): string;
|
|
44
|
+
url(): string;
|
|
45
|
+
};
|
|
46
|
+
fulfill(options: {
|
|
47
|
+
status: number;
|
|
48
|
+
body: string;
|
|
49
|
+
}): Promise<void>;
|
|
50
|
+
fallback(): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
export interface PolishState {
|
|
53
|
+
name: StateName | typeof REDUCED_MOTION_STATE;
|
|
54
|
+
viewport: {
|
|
55
|
+
width: number;
|
|
56
|
+
height: number;
|
|
57
|
+
};
|
|
58
|
+
isMobile?: boolean;
|
|
59
|
+
hasTouch?: boolean;
|
|
60
|
+
deviceScaleFactor?: number;
|
|
61
|
+
colorScheme: "light" | "dark";
|
|
62
|
+
reducedMotion?: "reduce";
|
|
63
|
+
}
|
|
64
|
+
export declare const POLISH_STATES: Record<StateName, PolishState>;
|
|
65
|
+
export declare const REDUCED_MOTION: PolishState;
|
|
66
|
+
export interface StateContextOptions {
|
|
67
|
+
viewport: {
|
|
68
|
+
width: number;
|
|
69
|
+
height: number;
|
|
70
|
+
};
|
|
71
|
+
colorScheme: "light" | "dark";
|
|
72
|
+
isMobile?: boolean;
|
|
73
|
+
hasTouch?: boolean;
|
|
74
|
+
deviceScaleFactor?: number;
|
|
75
|
+
reducedMotion?: "reduce";
|
|
76
|
+
}
|
|
77
|
+
export declare function stateContextOptions(state: PolishState): StateContextOptions;
|
|
78
|
+
export interface OpenedPage {
|
|
79
|
+
page: PolishPage;
|
|
80
|
+
close(): Promise<void>;
|
|
81
|
+
}
|
|
82
|
+
export type PolishSource = PolishPage | ((state: PolishState) => Promise<OpenedPage>);
|
|
83
|
+
export interface PolishOptions {
|
|
84
|
+
url: string;
|
|
85
|
+
screenshots?: boolean | readonly StateName[];
|
|
86
|
+
axeSource?: string;
|
|
87
|
+
fetch?: FetchLike;
|
|
88
|
+
local?: boolean;
|
|
89
|
+
budget?: PolishBudget;
|
|
90
|
+
}
|
|
91
|
+
export interface PolishBudget {
|
|
92
|
+
deadline: number;
|
|
93
|
+
seconds: number;
|
|
94
|
+
}
|
|
95
|
+
export declare function slowPageResult(budget: PolishBudget, skipped: string[]): RuleResult;
|
|
96
|
+
export interface PolishShot {
|
|
97
|
+
state: StateName;
|
|
98
|
+
png: Uint8Array;
|
|
99
|
+
scrollY: number;
|
|
100
|
+
}
|
|
101
|
+
export interface PolishRun {
|
|
102
|
+
results: RuleResult[];
|
|
103
|
+
states?: string[];
|
|
104
|
+
screenshots?: PolishShot[];
|
|
105
|
+
convert?: ConvertRenderData;
|
|
106
|
+
}
|
|
107
|
+
export declare const NETWORK_IDLE_MS = 15000;
|
|
108
|
+
export declare const SETTLE_MS = 500;
|
|
109
|
+
export declare const NAVIGATION_MS = 30000;
|
|
110
|
+
export declare const EARLY_POPUP_MS = 1000;
|
|
111
|
+
export declare const LATE_POPUP_MS = 4000;
|
|
112
|
+
export declare const CLICK_WAIT_MS = 2000;
|
|
113
|
+
export declare function settle(page: Pick<PolishPage, "waitForLoadState" | "waitForTimeout">): Promise<void>;
|
|
114
|
+
export declare function firstLine(error: unknown): string;
|
|
115
|
+
export declare function runPolishChecks(source: PolishSource, options: PolishOptions): Promise<PolishRun>;
|