seo-supervisor 1.2.0 → 2.0.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/.claude-plugin/icon.png +0 -0
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +16 -3
- package/CHANGELOG.md +430 -0
- package/NOTICE +7 -0
- package/README.md +230 -25
- package/agents/seo-auditor.md +16 -0
- package/agents/seo-reviewer.md +16 -0
- package/bin/agents.mjs +194 -29
- package/bin/extras.mjs +422 -0
- package/bin/prompt.mjs +34 -14
- package/bin/seo-supervisor.mjs +386 -53
- package/commands/seo-audit.md +29 -3
- package/commands/seo-brief.md +28 -0
- package/commands/seo-connect.md +27 -0
- package/commands/seo-fix.md +24 -3
- package/commands/seo-grow.md +26 -0
- package/commands/seo-launch.md +17 -3
- package/commands/seo-review.md +13 -1
- package/commands/seo-rewrite.md +25 -0
- package/commands/seo-setup.md +17 -1
- package/commands/seo-watch.md +28 -0
- package/hooks/hooks.json +117 -0
- package/package.json +20 -7
- package/skills/seo-supervisor/NOTICE +7 -0
- package/skills/seo-supervisor/SKILL.md +302 -21
- package/skills/seo-supervisor/audience.md +61 -0
- package/skills/seo-supervisor/examples/drafts/security.md +41 -0
- package/skills/seo-supervisor/examples/hooks/README.md +28 -0
- package/skills/seo-supervisor/examples/hooks/claude-code-settings.json +15 -0
- package/skills/seo-supervisor/examples/hooks/codex-hooks.json +17 -0
- package/skills/seo-supervisor/examples/hooks/copilot-hooks.json +15 -0
- package/skills/seo-supervisor/examples/hooks/cursor-hooks.json +11 -0
- package/skills/seo-supervisor/examples/hooks/github-actions.yml +21 -0
- package/skills/seo-supervisor/examples/hooks/pre-commit +17 -0
- package/skills/seo-supervisor/examples/hooks/reading-ease.yml +24 -0
- package/skills/seo-supervisor/examples/sample-audit.json +94 -2
- package/skills/seo-supervisor/examples/sample-report.html +111 -61
- package/skills/seo-supervisor/examples/sample-report.md +147 -44
- package/skills/seo-supervisor/examples/scheduled-runs.md +89 -0
- package/skills/seo-supervisor/examples/snippets.md +648 -1
- package/skills/seo-supervisor/grow.md +342 -0
- package/skills/seo-supervisor/og-images.md +85 -0
- package/skills/seo-supervisor/page-types.md +124 -0
- package/skills/seo-supervisor/planning.md +19 -7
- package/skills/seo-supervisor/profile-template.md +41 -0
- package/skills/seo-supervisor/report-format.md +177 -10
- package/skills/seo-supervisor/rewrite.md +59 -0
- package/skills/seo-supervisor/rules/ai-search.md +65 -12
- package/skills/seo-supervisor/rules/content.md +33 -9
- package/skills/seo-supervisor/rules/crawl-index.md +33 -6
- package/skills/seo-supervisor/rules/internal-linking.md +49 -6
- package/skills/seo-supervisor/rules/launch-monitoring.md +17 -8
- package/skills/seo-supervisor/rules/media.md +13 -1
- package/skills/seo-supervisor/rules/metadata.md +41 -9
- package/skills/seo-supervisor/rules/performance.md +50 -5
- package/skills/seo-supervisor/rules/rendering.md +39 -2
- package/skills/seo-supervisor/rules/search-intent.md +32 -0
- package/skills/seo-supervisor/rules/sitemaps-robots.md +24 -12
- package/skills/seo-supervisor/rules/structured-data.md +37 -14
- package/skills/seo-supervisor/rules/trust-security.md +25 -2
- package/skills/seo-supervisor/rules/urls.md +10 -2
- package/skills/seo-supervisor/scripts/ai-answers.mjs +314 -0
- package/skills/seo-supervisor/scripts/backlinks.mjs +412 -0
- package/skills/seo-supervisor/scripts/change-check.mjs +281 -0
- package/skills/seo-supervisor/scripts/checks/agents.mjs +488 -0
- package/skills/seo-supervisor/scripts/checks/assets.mjs +404 -0
- package/skills/seo-supervisor/scripts/checks/bots.mjs +146 -0
- package/skills/seo-supervisor/scripts/checks/business-type.mjs +193 -0
- package/skills/seo-supervisor/scripts/checks/content.mjs +391 -0
- package/skills/seo-supervisor/scripts/checks/coverage.mjs +184 -0
- package/skills/seo-supervisor/scripts/checks/crawl.mjs +95 -0
- package/skills/seo-supervisor/scripts/checks/hreflang.mjs +396 -0
- package/skills/seo-supervisor/scripts/checks/hygiene.mjs +227 -0
- package/skills/seo-supervisor/scripts/checks/lighthouse.mjs +271 -0
- package/skills/seo-supervisor/scripts/checks/link-strength.mjs +59 -0
- package/skills/seo-supervisor/scripts/checks/links.mjs +360 -0
- package/skills/seo-supervisor/scripts/checks/local.mjs +101 -0
- package/skills/seo-supervisor/scripts/checks/media.mjs +194 -0
- package/skills/seo-supervisor/scripts/checks/outbound.mjs +53 -0
- package/skills/seo-supervisor/scripts/checks/page.mjs +223 -0
- package/skills/seo-supervisor/scripts/checks/probes.mjs +130 -0
- package/skills/seo-supervisor/scripts/checks/render.mjs +552 -0
- package/skills/seo-supervisor/scripts/checks/robots.mjs +144 -0
- package/skills/seo-supervisor/scripts/checks/schema.mjs +206 -0
- package/skills/seo-supervisor/scripts/checks/site-agents.mjs +79 -0
- package/skills/seo-supervisor/scripts/checks/site-content.mjs +135 -0
- package/skills/seo-supervisor/scripts/checks/site-hygiene.mjs +202 -0
- package/skills/seo-supervisor/scripts/checks/site-pages.mjs +474 -0
- package/skills/seo-supervisor/scripts/checks/site-security.mjs +102 -0
- package/skills/seo-supervisor/scripts/checks/sitemap.mjs +473 -0
- package/skills/seo-supervisor/scripts/checks/speed.mjs +267 -0
- package/skills/seo-supervisor/scripts/checks/technical.mjs +368 -0
- package/skills/seo-supervisor/scripts/checks/tls.mjs +297 -0
- package/skills/seo-supervisor/scripts/checks/trust.mjs +421 -0
- package/skills/seo-supervisor/scripts/checks/visual.mjs +1132 -0
- package/skills/seo-supervisor/scripts/checks/writing.mjs +192 -0
- package/skills/seo-supervisor/scripts/commoncrawl.mjs +452 -0
- package/skills/seo-supervisor/scripts/connect-keys.mjs +422 -0
- package/skills/seo-supervisor/scripts/content-scan.mjs +515 -158
- package/skills/seo-supervisor/scripts/crawl/catalog.mjs +1248 -0
- package/skills/seo-supervisor/scripts/crawl/fetch.mjs +362 -0
- package/skills/seo-supervisor/scripts/crawl/page.mjs +741 -0
- package/skills/seo-supervisor/scripts/crawl/queue.mjs +190 -0
- package/skills/seo-supervisor/scripts/dashboard.mjs +140 -0
- package/skills/seo-supervisor/scripts/data/ai-bots.json +38 -0
- package/skills/seo-supervisor/scripts/data/build-schema-vocab.mjs +57 -0
- package/skills/seo-supervisor/scripts/data/business-types.json +338 -0
- package/skills/seo-supervisor/scripts/data/google-rich-results.json +79 -0
- package/skills/seo-supervisor/scripts/data/schema-vocab.json +1 -0
- package/skills/seo-supervisor/scripts/deploy-check.mjs +462 -0
- package/skills/seo-supervisor/scripts/disavow.mjs +176 -0
- package/skills/seo-supervisor/scripts/doctor.mjs +174 -0
- package/skills/seo-supervisor/scripts/geo-grid.mjs +371 -0
- package/skills/seo-supervisor/scripts/google/search-console.mjs +151 -0
- package/skills/seo-supervisor/scripts/google/session.mjs +167 -0
- package/skills/seo-supervisor/scripts/google-updates.mjs +189 -0
- package/skills/seo-supervisor/scripts/google.mjs +1341 -0
- package/skills/seo-supervisor/scripts/gsc-import.mjs +298 -0
- package/skills/seo-supervisor/scripts/guard/adapters/claude-code.mjs +149 -0
- package/skills/seo-supervisor/scripts/guard/adapters/cline.mjs +28 -0
- package/skills/seo-supervisor/scripts/guard/adapters/codex.mjs +26 -0
- package/skills/seo-supervisor/scripts/guard/adapters/copilot.mjs +36 -0
- package/skills/seo-supervisor/scripts/guard/adapters/cursor.mjs +33 -0
- package/skills/seo-supervisor/scripts/guard/adapters/devin.mjs +25 -0
- package/skills/seo-supervisor/scripts/guard/adapters/edits.mjs +124 -0
- package/skills/seo-supervisor/scripts/guard/mode.mjs +37 -0
- package/skills/seo-supervisor/scripts/guard/rules.mjs +931 -0
- package/skills/seo-supervisor/scripts/guard.mjs +74 -0
- package/skills/seo-supervisor/scripts/images.mjs +408 -0
- package/skills/seo-supervisor/scripts/indexnow.mjs +300 -0
- package/skills/seo-supervisor/scripts/issues.mjs +217 -0
- package/skills/seo-supervisor/scripts/keys/authority.mjs +135 -0
- package/skills/seo-supervisor/scripts/keys/bing.mjs +239 -0
- package/skills/seo-supervisor/scripts/keys/clarity.mjs +137 -0
- package/skills/seo-supervisor/scripts/keys/dataforseo.mjs +57 -0
- package/skills/seo-supervisor/scripts/keys/googleplaces.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/http.mjs +212 -0
- package/skills/seo-supervisor/scripts/keys/matomo.mjs +138 -0
- package/skills/seo-supervisor/scripts/keys/openai.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/perplexity.mjs +26 -0
- package/skills/seo-supervisor/scripts/keys/posthog.mjs +165 -0
- package/skills/seo-supervisor/scripts/keys/semrush.mjs +25 -0
- package/skills/seo-supervisor/scripts/keys/services.mjs +41 -0
- package/skills/seo-supervisor/scripts/keys/store.mjs +140 -0
- package/skills/seo-supervisor/scripts/keys/visits.mjs +58 -0
- package/skills/seo-supervisor/scripts/keys/youtube.mjs +84 -0
- package/skills/seo-supervisor/scripts/keyword-data.mjs +316 -0
- package/skills/seo-supervisor/scripts/keyword-ideas.mjs +503 -0
- package/skills/seo-supervisor/scripts/lib/args.mjs +56 -0
- package/skills/seo-supervisor/scripts/lib/business.mjs +27 -0
- package/skills/seo-supervisor/scripts/lib/cdp.mjs +242 -0
- package/skills/seo-supervisor/scripts/lib/cli.mjs +56 -0
- package/skills/seo-supervisor/scripts/lib/csv.mjs +38 -0
- package/skills/seo-supervisor/scripts/lib/findings.mjs +100 -0
- package/skills/seo-supervisor/scripts/lib/google-client.mjs +9 -0
- package/skills/seo-supervisor/scripts/lib/html.mjs +146 -0
- package/skills/seo-supervisor/scripts/lib/http.mjs +28 -0
- package/skills/seo-supervisor/scripts/lib/image-size.mjs +78 -0
- package/skills/seo-supervisor/scripts/lib/is-main.mjs +13 -0
- package/skills/seo-supervisor/scripts/lib/net.mjs +116 -0
- package/skills/seo-supervisor/scripts/lib/proxy.mjs +47 -0
- package/skills/seo-supervisor/scripts/lib/recheck.mjs +106 -0
- package/skills/seo-supervisor/scripts/lib/redact.mjs +78 -0
- package/skills/seo-supervisor/scripts/lib/robots.mjs +73 -0
- package/skills/seo-supervisor/scripts/lib/score.mjs +266 -0
- package/skills/seo-supervisor/scripts/lib/settings-dir.mjs +81 -0
- package/skills/seo-supervisor/scripts/links-import.mjs +603 -0
- package/skills/seo-supervisor/scripts/log-scan.mjs +597 -0
- package/skills/seo-supervisor/scripts/og-card.mjs +273 -0
- package/skills/seo-supervisor/scripts/osm.mjs +359 -0
- package/skills/seo-supervisor/scripts/rdap.mjs +164 -0
- package/skills/seo-supervisor/scripts/render-report.mjs +324 -690
- package/skills/seo-supervisor/scripts/report/client.js +37 -0
- package/skills/seo-supervisor/scripts/report/diff.mjs +264 -0
- package/skills/seo-supervisor/scripts/report/findings.mjs +165 -0
- package/skills/seo-supervisor/scripts/report/format.mjs +111 -0
- package/skills/seo-supervisor/scripts/report/markdown.mjs +250 -0
- package/skills/seo-supervisor/scripts/report/model.mjs +171 -0
- package/skills/seo-supervisor/scripts/report/overview-lines.mjs +158 -0
- package/skills/seo-supervisor/scripts/report/overview.mjs +224 -0
- package/skills/seo-supervisor/scripts/report/page.mjs +127 -0
- package/skills/seo-supervisor/scripts/report/panel.mjs +172 -0
- package/skills/seo-supervisor/scripts/report/panels/ai.mjs +211 -0
- package/skills/seo-supervisor/scripts/report/panels/connections.mjs +139 -0
- package/skills/seo-supervisor/scripts/report/panels/content.mjs +204 -0
- package/skills/seo-supervisor/scripts/report/panels/data-search.mjs +379 -0
- package/skills/seo-supervisor/scripts/report/panels/data-site.mjs +44 -0
- package/skills/seo-supervisor/scripts/report/panels/links.mjs +380 -0
- package/skills/seo-supervisor/scripts/report/panels/local.mjs +221 -0
- package/skills/seo-supervisor/scripts/report/panels/site.mjs +359 -0
- package/skills/seo-supervisor/scripts/report/panels/speed-media.mjs +267 -0
- package/skills/seo-supervisor/scripts/report/sources.mjs +163 -0
- package/skills/seo-supervisor/scripts/report/styles.css +223 -0
- package/skills/seo-supervisor/scripts/report/traffic.mjs +264 -0
- package/skills/seo-supervisor/scripts/report-plan.mjs +112 -33
- package/skills/seo-supervisor/scripts/schema-check.mjs +717 -0
- package/skills/seo-supervisor/scripts/seo-crawl.mjs +1086 -638
- package/skills/seo-supervisor/scripts/seo-diff.mjs +584 -0
- package/skills/seo-supervisor/scripts/update-check.mjs +82 -0
- package/skills/seo-supervisor/site-types/content.md +34 -1
- package/skills/seo-supervisor/site-types/ecommerce.md +38 -1
- package/skills/seo-supervisor/site-types/local-business.md +96 -6
- package/skills/seo-supervisor/site-types/services.md +46 -0
- package/skills/seo-supervisor/site-types/tools.md +3 -3
- package/skills/seo-supervisor/strategy.md +232 -0
- package/skills/seo-supervisor/troubleshooting.md +56 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// Pre-action hook: reads the hook's JSON on stdin and answers allow, ask (with a plain reason) or deny. Rules live in
|
|
4
|
+
// guard/rules.mjs, each host's payload and answer format in guard/adapters/<agent>.mjs.
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import { isMain } from "./lib/is-main.mjs";
|
|
7
|
+
import { checkAction } from "./guard/rules.mjs";
|
|
8
|
+
import { guardMode, projectSettings } from "./guard/mode.mjs";
|
|
9
|
+
|
|
10
|
+
/** Checks the adapter's actions in order; the first that isn't allowed decides.
|
|
11
|
+
* @param {string} agent @param {any} input
|
|
12
|
+
* @param {{ check?: typeof checkAction, env?: Record<string, string | undefined>, event?: string, cwd?: string }} [opts]
|
|
13
|
+
* @returns {Promise<{ stdout: string, stderr: string, exitCode: number }>} */
|
|
14
|
+
export async function run(agent, input, { check = checkAction, env = process.env, event, cwd } = {}) {
|
|
15
|
+
if (!/^[a-z][a-z0-9-]*$/.test(String(agent))) throw new Error(`unknown agent "${agent}"`);
|
|
16
|
+
const ad = await import(`./guard/adapters/${agent}.mjs`);
|
|
17
|
+
const ev = event || (typeof input?.hook_event_name === "string" ? input.hook_event_name : "");
|
|
18
|
+
const dir = cwd || (typeof input?.cwd === "string" && input.cwd) || process.cwd();
|
|
19
|
+
const actions = ad.toActions(input, ev, dir) || [];
|
|
20
|
+
const own = ad.session?.(input, ev, dir, actions, env); // an adapter may answer session events itself
|
|
21
|
+
if (own) return own;
|
|
22
|
+
if (!actions.length) return ad.respond({ decision: "allow" }, ev);
|
|
23
|
+
const mode = guardMode(env, dir),
|
|
24
|
+
settings = mode === "off" ? {} : projectSettings(dir, env);
|
|
25
|
+
let result = { decision: "allow" };
|
|
26
|
+
for (const a of actions) {
|
|
27
|
+
const r = check(a, { mode, settings, cwd: dir });
|
|
28
|
+
if (r?.decision && r.decision !== "allow") {
|
|
29
|
+
result = r;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return ad.respond(result, ev);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (isMain(import.meta.url)) {
|
|
37
|
+
const argv = process.argv.slice(2),
|
|
38
|
+
opt = n => {
|
|
39
|
+
const i = argv.indexOf(n);
|
|
40
|
+
return i >= 0 ? argv[i + 1] : undefined;
|
|
41
|
+
};
|
|
42
|
+
if (argv.includes("--help") || argv.includes("-h")) {
|
|
43
|
+
console.log(
|
|
44
|
+
"usage: node guard.mjs --agent claude-code [--event <hook event>] (reads the agent hook's JSON on stdin)",
|
|
45
|
+
);
|
|
46
|
+
process.exit(0);
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const raw = readFileSync(0, "utf8");
|
|
50
|
+
const r = await run(opt("--agent") || "claude-code", raw.trim() ? JSON.parse(raw) : {}, { event: opt("--event") });
|
|
51
|
+
if (r.stdout) process.stdout.write(r.stdout);
|
|
52
|
+
if (r.stderr) process.stderr.write(r.stderr);
|
|
53
|
+
process.exitCode = r.exitCode || 0;
|
|
54
|
+
} catch (e) {
|
|
55
|
+
// The guard must never block on its own failure: bad input or a bug allows the action, with one line on stderr.
|
|
56
|
+
process.stderr.write(
|
|
57
|
+
`seo-supervisor guard: allowed the action after its own error (${String(e?.message || e)
|
|
58
|
+
.split("\n")[0]
|
|
59
|
+
.slice(0, 200)})\n`,
|
|
60
|
+
);
|
|
61
|
+
// Answer in the host's own format: some hooks (Cursor's) block on empty or invalid output.
|
|
62
|
+
try {
|
|
63
|
+
const agent = opt("--agent") || "claude-code";
|
|
64
|
+
if (/^[a-z][a-z0-9-]*$/.test(agent)) {
|
|
65
|
+
const out = (await import(`./guard/adapters/${agent}.mjs`)).respond(
|
|
66
|
+
{ decision: "allow" },
|
|
67
|
+
opt("--event") || "",
|
|
68
|
+
).stdout;
|
|
69
|
+
if (out) process.stdout.write(out);
|
|
70
|
+
}
|
|
71
|
+
} catch {}
|
|
72
|
+
process.exitCode = 0;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// Converts the heavy JPEG and PNG files an audit found to WebP or AVIF, and labels images as AI-made in their XMP.
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import {
|
|
6
|
+
constants,
|
|
7
|
+
copyFileSync,
|
|
8
|
+
existsSync,
|
|
9
|
+
mkdirSync,
|
|
10
|
+
readFileSync,
|
|
11
|
+
realpathSync,
|
|
12
|
+
rmSync,
|
|
13
|
+
statSync,
|
|
14
|
+
writeFileSync,
|
|
15
|
+
} from "node:fs";
|
|
16
|
+
import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
|
|
17
|
+
import { isMain } from "./lib/is-main.mjs";
|
|
18
|
+
import { parseArgs } from "./lib/args.mjs";
|
|
19
|
+
import { Fail, runMain } from "./lib/cli.mjs";
|
|
20
|
+
import { toolsDir } from "./lib/settings-dir.mjs";
|
|
21
|
+
import { AI_SOURCES, imageMeta } from "./lib/image-size.mjs";
|
|
22
|
+
|
|
23
|
+
// Rough saving in bytes. Lossy WebP is 25–34% smaller than JPEG at the same SSIM and lossless WebP 26% smaller than
|
|
24
|
+
// PNG; the low end is used. https://developers.google.com/speed/webp
|
|
25
|
+
// Resizing is taken to cut bytes in proportion to pixels, a rule of thumb: photos compress unevenly.
|
|
26
|
+
const CUT = { jpeg: 0.25, png: 0.26 };
|
|
27
|
+
export function estSaving({ bytes, width, format }, maxWidth = 1600) {
|
|
28
|
+
const scale = width > maxWidth ? (maxWidth / width) ** 2 : 1;
|
|
29
|
+
return Math.round(bytes * (1 - scale * (1 - (CUT[format] || 0))));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @type {(msg: string, code?: number) => never} */
|
|
33
|
+
const fail = (msg, code = 2) => {
|
|
34
|
+
throw new Fail("images: " + msg, code);
|
|
35
|
+
};
|
|
36
|
+
const kb = n => `${Math.round(n / 1024).toLocaleString("en-US")} KB`;
|
|
37
|
+
const swapExt = (p, ext) => p.slice(0, p.length - extname(p).length) + "." + ext;
|
|
38
|
+
const ADD_ON = "npx seo-supervisor --with-image-tools";
|
|
39
|
+
// The path with links resolved, for a folder not made yet too (its nearest existing parent's, plus the rest).
|
|
40
|
+
const real = p => {
|
|
41
|
+
try {
|
|
42
|
+
return realpathSync.native(p);
|
|
43
|
+
} catch {
|
|
44
|
+
return dirname(p) === p ? p : join(real(dirname(p)), basename(p));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const within = (p, base) => p === base || p.startsWith(base + sep);
|
|
48
|
+
|
|
49
|
+
const USAGE =
|
|
50
|
+
"usage: node images.mjs --crawl seo-audit/seo-crawl.json --root <public folder> [--avif] [--max-width 1600] [--out-dir <dir>] [--follow-links] [--yes]";
|
|
51
|
+
const LABEL_USAGE = "usage: node images.mjs --label-ai <file>[,<file>…] [--source trainedAlgorithmicMedia] [--yes]";
|
|
52
|
+
async function main(argv) {
|
|
53
|
+
const args = parseArgs(argv, {
|
|
54
|
+
flags: ["crawl", "root", "max-width", "out-dir", "label-ai", "source"],
|
|
55
|
+
booleans: ["avif", "follow-links", "yes"],
|
|
56
|
+
positionals: 0,
|
|
57
|
+
usage: `${USAGE}\n${LABEL_USAGE}`,
|
|
58
|
+
});
|
|
59
|
+
if (args["label-ai"] !== undefined) return labelMain(args);
|
|
60
|
+
if (typeof args.crawl !== "string" || typeof args.root !== "string") fail(USAGE);
|
|
61
|
+
const mw = args["max-width"] ?? "1600",
|
|
62
|
+
maxWidth = Number(mw);
|
|
63
|
+
if (!/^\d+$/.test(mw) || maxWidth < 320)
|
|
64
|
+
fail(`--max-width needs a width in pixels, 320 or more (got ${mw === true ? "no value" : mw}).`);
|
|
65
|
+
const root = resolve(args.root),
|
|
66
|
+
outDir = typeof args["out-dir"] === "string" ? resolve(args["out-dir"]) : null;
|
|
67
|
+
let crawl;
|
|
68
|
+
try {
|
|
69
|
+
crawl = JSON.parse(readFileSync(args.crawl, "utf8"));
|
|
70
|
+
} catch (e) {
|
|
71
|
+
fail(`couldn't read ${args.crawl} (${e.code || e.message}).`);
|
|
72
|
+
}
|
|
73
|
+
if (!Array.isArray(crawl?.images))
|
|
74
|
+
fail(`${args.crawl} has no image list: it was made by an older crawler. Re-run the audit, then this.`, 1);
|
|
75
|
+
if (!existsSync(root)) fail(`--root ${args.root} doesn't exist.`);
|
|
76
|
+
|
|
77
|
+
const show = p => relative(process.cwd(), p).replace(/\\/g, "/") || ".";
|
|
78
|
+
const formats = ["webp", ...(args.avif === true ? ["avif"] : [])];
|
|
79
|
+
const todo = [],
|
|
80
|
+
missing = [],
|
|
81
|
+
outside = [],
|
|
82
|
+
notFiles = [],
|
|
83
|
+
linked = [],
|
|
84
|
+
seen = new Set();
|
|
85
|
+
const realRoot = real(root),
|
|
86
|
+
realOut = outDir ? real(outDir) : realRoot;
|
|
87
|
+
let modern = 0;
|
|
88
|
+
for (const img of crawl.images) {
|
|
89
|
+
// 100 KB is PERF-08's threshold.
|
|
90
|
+
if (!img || typeof img.url !== "string" || !(img.bytes > 100 * 1024)) continue;
|
|
91
|
+
if (!/^(jpeg|png)$/.test(img.format)) {
|
|
92
|
+
if (img.bytes > 200 * 1024) modern++;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
let path;
|
|
96
|
+
try {
|
|
97
|
+
path = decodeURIComponent(new URL(img.url).pathname);
|
|
98
|
+
} catch {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// A script that makes images (timthumb.php?src=a.jpg) isn't the image file; a ?v=3 on photo.jpg is fine.
|
|
102
|
+
if (!/\.(jpe?g|png)$/i.test(path)) {
|
|
103
|
+
notFiles.push(img.url);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const file = join(root, path);
|
|
107
|
+
if (!file.startsWith(root + sep)) {
|
|
108
|
+
outside.push(img.url);
|
|
109
|
+
continue;
|
|
110
|
+
} // a crafted path can't reach outside --root
|
|
111
|
+
if (!existsSync(file) || !statSync(file).isFile()) {
|
|
112
|
+
missing.push(path);
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const out = outDir ? join(outDir, relative(root, file)) : file,
|
|
116
|
+
key = real(file);
|
|
117
|
+
// A symlink or junction leading outside --root or --out-dir needs --follow-links; Laravel's public/storage is one.
|
|
118
|
+
if (args["follow-links"] !== true && !(within(key, realRoot) && within(real(dirname(out)), realOut))) {
|
|
119
|
+
linked.push(path);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (seen.has(key)) continue; // the same file by another URL (%7a for z, another ?v=)
|
|
123
|
+
seen.add(key);
|
|
124
|
+
todo.push({
|
|
125
|
+
img,
|
|
126
|
+
file,
|
|
127
|
+
path,
|
|
128
|
+
target: f => swapExt(out, f),
|
|
129
|
+
src: f => swapExt(path, f),
|
|
130
|
+
saving: estSaving(img, maxWidth),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
todo.sort((a, b) => b.saving - a.saving);
|
|
134
|
+
|
|
135
|
+
const total = todo.reduce((n, t) => n + t.saving, 0);
|
|
136
|
+
console.log(
|
|
137
|
+
todo.length
|
|
138
|
+
? `images: ${todo.length} image(s) to convert to ${formats.length > 1 ? "WebP and AVIF" : "WebP"}, about ${kb(total)} smaller in all (a rough estimate for WebP${formats.length > 1 ? "; AVIF is often smaller still" : ""}):`
|
|
139
|
+
: "images: no JPEG or PNG over 100 KB from the crawl was found under --root, so there's nothing to convert.",
|
|
140
|
+
);
|
|
141
|
+
for (const t of todo) {
|
|
142
|
+
const pages = (Array.isArray(t.img.pages) ? t.img.pages : []).slice(0, 3).map(p => {
|
|
143
|
+
try {
|
|
144
|
+
return new URL(p).pathname;
|
|
145
|
+
} catch {
|
|
146
|
+
return String(p);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
console.log(
|
|
150
|
+
` ${show(t.file)} (${kb(t.img.bytes)}${t.img.width ? `, ${t.img.width} px wide` : ""}) → ${t.src("webp")}, about ${kb(t.saving)} smaller${pages.length ? `; on ${pages.join(", ")}` : ""}`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (missing.length)
|
|
154
|
+
console.log(
|
|
155
|
+
`Not found under ${show(root)}, left alone: ${missing.slice(0, 10).join(", ")}${missing.length > 10 ? ` and ${missing.length - 10} more` : ""}`,
|
|
156
|
+
);
|
|
157
|
+
if (outside.length) console.log(`Outside --root, refused: ${outside.slice(0, 3).join(", ")}`);
|
|
158
|
+
if (notFiles.length)
|
|
159
|
+
console.log(
|
|
160
|
+
`Not a .jpg, .jpeg or .png file (a script makes these), left alone: ${notFiles.slice(0, 3).join(", ")}${notFiles.length > 3 ? ` and ${notFiles.length - 3} more` : ""}`,
|
|
161
|
+
);
|
|
162
|
+
if (linked.length)
|
|
163
|
+
console.log(
|
|
164
|
+
`Reached through a link that leads outside ${outDir ? "--root or --out-dir" : "--root"}, refused: ${linked.slice(0, 3).join(", ")}${linked.length > 3 ? ` and ${linked.length - 3} more` : ""}. Add --follow-links if that link is meant to be followed (Laravel's public/storage is one).`,
|
|
165
|
+
);
|
|
166
|
+
if (modern)
|
|
167
|
+
console.log(
|
|
168
|
+
`${modern} heavy image(s) are already WebP, AVIF or another format; resize or compress those where they're made.`,
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
// sharp is an optional add-on kept in the tools folder, never in the project.
|
|
172
|
+
const SHARP = join(toolsDir(), "node_modules", "sharp");
|
|
173
|
+
const installed = existsSync(SHARP);
|
|
174
|
+
if (todo.length && args.yes !== true) {
|
|
175
|
+
console.log(
|
|
176
|
+
`\nDry run: nothing was written. With --yes it writes the new files ${outDir ? `under ${show(outDir)}` : "next to the originals"} and changes nothing else.`,
|
|
177
|
+
);
|
|
178
|
+
if (!installed)
|
|
179
|
+
console.log(
|
|
180
|
+
`It needs the optional image add-on (sharp, about 20 MB in ${toolsDir()}, not your project): ${ADD_ON}`,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
let failed = 0;
|
|
184
|
+
if (todo.length && args.yes === true) {
|
|
185
|
+
if (!installed) {
|
|
186
|
+
console.log(
|
|
187
|
+
`\nNothing was converted: the optional image add-on isn't installed. Add it with ${ADD_ON} (sharp, about 20 MB in ${toolsDir()}, not your project), then run this again.`,
|
|
188
|
+
);
|
|
189
|
+
return 0;
|
|
190
|
+
}
|
|
191
|
+
let sharp;
|
|
192
|
+
try {
|
|
193
|
+
sharp = createRequire(import.meta.url)(SHARP);
|
|
194
|
+
} catch (e) {
|
|
195
|
+
fail(`sharp is in ${SHARP} but didn't load (${e.message}). Reinstall it with ${ADD_ON}.`, 1);
|
|
196
|
+
}
|
|
197
|
+
console.log("");
|
|
198
|
+
for (const t of todo)
|
|
199
|
+
for (const f of formats) {
|
|
200
|
+
const out = t.target(f);
|
|
201
|
+
// Nothing is overwritten or deleted: originals stay, and an existing output is left alone.
|
|
202
|
+
if (existsSync(out)) {
|
|
203
|
+
console.log(` ${show(out)} exists, left alone`);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
let info;
|
|
207
|
+
try {
|
|
208
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
209
|
+
const pipe = sharp(t.file).autoOrient().resize({ width: maxWidth, withoutEnlargement: true });
|
|
210
|
+
info = await (f === "avif" ? pipe.avif() : pipe.webp()).toFile(out);
|
|
211
|
+
} catch (e) {
|
|
212
|
+
// too many pixels, a corrupt file: name it and carry on with the rest
|
|
213
|
+
failed++;
|
|
214
|
+
rmSync(out, { force: true }); // only a half-written file of this run's: out didn't exist above
|
|
215
|
+
console.log(` couldn't convert ${show(t.file)} (${e.message}), left alone`);
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
const was = statSync(t.file).size;
|
|
219
|
+
console.log(
|
|
220
|
+
` wrote ${show(out)}: ${kb(info.size)}, was ${kb(was)}${info.size >= was ? " (not smaller: keep using the original)" : ""}`,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (todo.length) {
|
|
225
|
+
const t = todo[0],
|
|
226
|
+
alt = formats.includes("avif") ? `<source srcset="${t.src("avif")}" type="image/avif">` : "";
|
|
227
|
+
console.log(
|
|
228
|
+
`\nThen point the markup at the new files${outDir ? `, once they're served next to the originals` : ""}. Keep the original as the fallback:`,
|
|
229
|
+
);
|
|
230
|
+
console.log(
|
|
231
|
+
` <picture>${alt}<source srcset="${t.src("webp")}" type="image/webp"><img src="${t.path}" alt="…" width="…" height="…"></picture>`,
|
|
232
|
+
);
|
|
233
|
+
console.log(
|
|
234
|
+
"or change the src to the .webp, which every current browser supports. A framework image component (next/image, Astro's <Image>, Nuxt Image) can serve WebP and AVIF for you instead.",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
return failed ? 1 : 0;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ---------- the AI label ----------
|
|
241
|
+
// Only the metadata changes, so the image isn't re-encoded and sharp isn't needed.
|
|
242
|
+
// Iptc4xmpExt:DigitalSourceType takes a URI from IPTC's digitalsourcetype vocabulary.
|
|
243
|
+
// https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata
|
|
244
|
+
// XMP sits in a JPEG APP1 segment after XMP_ID, and in a PNG iTXt chunk keyed PNG_KEY (XMP specification, part 3).
|
|
245
|
+
const IPTC_NS = "http://iptc.org/std/Iptc4xmpExt/2008-02-29/",
|
|
246
|
+
DST = "http://cv.iptc.org/newscodes/digitalsourcetype/";
|
|
247
|
+
const XMP_ID = "http://ns.adobe.com/xap/1.0/\0",
|
|
248
|
+
PNG_KEY = "XML:com.adobe.xmp";
|
|
249
|
+
// The label added to the packet's first rdf:Description, or a new packet when the file has none.
|
|
250
|
+
function labelPacket(xmp, source) {
|
|
251
|
+
const attr = ` Iptc4xmpExt:DigitalSourceType="${DST}${source}"`;
|
|
252
|
+
if (!xmp)
|
|
253
|
+
return `<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?><x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:Iptc4xmpExt="${IPTC_NS}"${attr}/></rdf:RDF></x:xmpmeta><?xpacket end="w"?>`;
|
|
254
|
+
const m = /<rdf:Description\b[^>]*?(?=\/?>)/.exec(xmp);
|
|
255
|
+
if (!m) throw new Error("its XMP has no rdf:Description to add the label to");
|
|
256
|
+
const end = m.index + m[0].length;
|
|
257
|
+
return (
|
|
258
|
+
xmp.slice(0, end) +
|
|
259
|
+
(/\sxmlns:Iptc4xmpExt\s*=/.test(m[0]) ? "" : ` xmlns:Iptc4xmpExt="${IPTC_NS}"`) +
|
|
260
|
+
attr +
|
|
261
|
+
xmp.slice(end)
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
const CRC = Array.from({ length: 256 }, (_, n) => {
|
|
265
|
+
for (let k = 0; k < 8; k++) n = n & 1 ? 0xedb88320 ^ (n >>> 1) : n >>> 1;
|
|
266
|
+
return n >>> 0;
|
|
267
|
+
});
|
|
268
|
+
const crc32 = b => {
|
|
269
|
+
let c = ~0;
|
|
270
|
+
for (const x of b) c = CRC[(c ^ x) & 255] ^ (c >>> 8);
|
|
271
|
+
return ~c >>> 0;
|
|
272
|
+
};
|
|
273
|
+
const pngChunk = (type, data) => {
|
|
274
|
+
const t = Buffer.from(type, "latin1"),
|
|
275
|
+
n = Buffer.alloc(4),
|
|
276
|
+
c = Buffer.alloc(4);
|
|
277
|
+
n.writeUInt32BE(data.length);
|
|
278
|
+
c.writeUInt32BE(crc32(Buffer.concat([t, data])));
|
|
279
|
+
return Buffer.concat([n, t, data, c]);
|
|
280
|
+
};
|
|
281
|
+
function labelJpeg(b, source) {
|
|
282
|
+
// The leading APPn segments (JFIF, Exif, XMP…): an XMP one is edited in place, else the new one goes after them.
|
|
283
|
+
let i = 2,
|
|
284
|
+
seg = null;
|
|
285
|
+
while (i + 4 <= b.length && b[i] === 0xff && b[i + 1] >= 0xe0 && b[i + 1] <= 0xef) {
|
|
286
|
+
const end = i + 2 + b.readUInt16BE(i + 2);
|
|
287
|
+
if (b[i + 1] === 0xe1 && b.toString("latin1", i + 4, i + 4 + XMP_ID.length) === XMP_ID) seg = [i, end];
|
|
288
|
+
i = end;
|
|
289
|
+
}
|
|
290
|
+
const old = seg ? b.toString("utf8", seg[0] + 4 + XMP_ID.length, seg[1]) : "";
|
|
291
|
+
const body = Buffer.concat([Buffer.from(XMP_ID, "latin1"), Buffer.from(labelPacket(old, source), "utf8")]),
|
|
292
|
+
head = Buffer.from([0xff, 0xe1, 0, 0]);
|
|
293
|
+
if (body.length + 2 > 0xffff) throw new Error("its XMP would be over the 64 KB one JPEG segment holds");
|
|
294
|
+
head.writeUInt16BE(body.length + 2, 2);
|
|
295
|
+
const [s, e] = seg || [i, i];
|
|
296
|
+
return Buffer.concat([b.subarray(0, s), head, body, b.subarray(e)]);
|
|
297
|
+
}
|
|
298
|
+
function labelPng(b, source) {
|
|
299
|
+
let i = 8,
|
|
300
|
+
seg = null,
|
|
301
|
+
idat = null;
|
|
302
|
+
while (i + 12 <= b.length) {
|
|
303
|
+
const n = b.readUInt32BE(i),
|
|
304
|
+
type = b.toString("latin1", i + 4, i + 8);
|
|
305
|
+
if (type === "iTXt" && b.toString("latin1", i + 8, i + 9 + PNG_KEY.length) === PNG_KEY + "\0")
|
|
306
|
+
seg = [i, i + 12 + n];
|
|
307
|
+
if (type === "IDAT") idat ??= i;
|
|
308
|
+
i += 12 + n;
|
|
309
|
+
}
|
|
310
|
+
if (idat == null) throw new Error("it has no image data (IDAT)");
|
|
311
|
+
let old = "";
|
|
312
|
+
if (seg) {
|
|
313
|
+
// keyword\0, compression flag, method, language\0, translated keyword\0, then the text
|
|
314
|
+
const d = b.subarray(seg[0] + 8, seg[1] - 4),
|
|
315
|
+
k = PNG_KEY.length + 1;
|
|
316
|
+
if (d[k] !== 0) throw new Error("its XMP is compressed, which this doesn't edit");
|
|
317
|
+
const lang = d.indexOf(0, k + 2) + 1;
|
|
318
|
+
old = d.toString("utf8", d.indexOf(0, lang) + 1);
|
|
319
|
+
}
|
|
320
|
+
const data = Buffer.concat([
|
|
321
|
+
Buffer.from(PNG_KEY + "\0\0\0\0\0", "latin1"),
|
|
322
|
+
Buffer.from(labelPacket(old, source), "utf8"),
|
|
323
|
+
]);
|
|
324
|
+
const [s, e] = seg || [idat, idat]; // XMP goes before the image data
|
|
325
|
+
return Buffer.concat([b.subarray(0, s), pngChunk("iTXt", data), b.subarray(e)]);
|
|
326
|
+
}
|
|
327
|
+
/** The file's bytes with the AI label added to its XMP, image data untouched; throws when it can't be labelled. */
|
|
328
|
+
export function withAiLabel(b, source = "trainedAlgorithmicMedia") {
|
|
329
|
+
if (!AI_SOURCES.includes(source))
|
|
330
|
+
throw new Error(`the source must be one of ${AI_SOURCES.join(", ")} (got ${source})`);
|
|
331
|
+
const jpeg = b[0] === 0xff && b[1] === 0xd8,
|
|
332
|
+
png = b.toString("latin1", 0, 8) === "\x89PNG\r\n\x1a\n";
|
|
333
|
+
if (!jpeg && !png) throw new Error("only a JPEG or PNG file can be labelled here");
|
|
334
|
+
const had = imageMeta(b).sourceType;
|
|
335
|
+
if (had) throw new Error(`already labelled: ${had}`);
|
|
336
|
+
return jpeg ? labelJpeg(b, source) : labelPng(b, source);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function labelMain(args) {
|
|
340
|
+
const list =
|
|
341
|
+
typeof args["label-ai"] === "string"
|
|
342
|
+
? args["label-ai"]
|
|
343
|
+
.split(",")
|
|
344
|
+
.map(s => s.trim())
|
|
345
|
+
.filter(Boolean)
|
|
346
|
+
: [];
|
|
347
|
+
const source = typeof args.source === "string" ? args.source : "trainedAlgorithmicMedia";
|
|
348
|
+
if (!list.length) fail(LABEL_USAGE);
|
|
349
|
+
if (!AI_SOURCES.includes(source)) fail(`--source must be one of ${AI_SOURCES.join(", ")} (got ${source}).`);
|
|
350
|
+
const show = p => relative(process.cwd(), p).replace(/\\/g, "/") || ".";
|
|
351
|
+
const todo = [],
|
|
352
|
+
skipped = [],
|
|
353
|
+
seen = new Set();
|
|
354
|
+
for (const f of list) {
|
|
355
|
+
const file = resolve(f),
|
|
356
|
+
backup = file.slice(0, file.length - extname(file).length) + ".original" + extname(file);
|
|
357
|
+
if (seen.has(file)) continue; // the same file named twice ("a.jpg,./a.jpg") is labelled once
|
|
358
|
+
seen.add(file);
|
|
359
|
+
if (!existsSync(file) || !statSync(file).isFile()) {
|
|
360
|
+
skipped.push(`${f}: not found`);
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
let out;
|
|
364
|
+
try {
|
|
365
|
+
out = withAiLabel(readFileSync(file), source);
|
|
366
|
+
} catch (e) {
|
|
367
|
+
skipped.push(`${f}: ${e.message}`);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
if (existsSync(backup)) {
|
|
371
|
+
skipped.push(`${f}: ${show(backup)} already exists, so the original couldn't be kept`);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
todo.push({ file, backup, out });
|
|
375
|
+
}
|
|
376
|
+
console.log(
|
|
377
|
+
todo.length
|
|
378
|
+
? `images: ${todo.length} file(s) to label as made with generative AI (IPTC digital source type ${source}):`
|
|
379
|
+
: "images: no file here can be labelled.",
|
|
380
|
+
);
|
|
381
|
+
for (const t of todo) console.log(` ${show(t.file)}`);
|
|
382
|
+
if (skipped.length) console.log(`Left alone:\n${skipped.map(s => ` ${s}`).join("\n")}`);
|
|
383
|
+
let failed = 0;
|
|
384
|
+
if (todo.length && args.yes !== true)
|
|
385
|
+
console.log(
|
|
386
|
+
`\nDry run: nothing was written. With --yes each file gets the label in its XMP metadata (the image itself isn't re-encoded), and the original is kept next to it as name.original.ext.`,
|
|
387
|
+
);
|
|
388
|
+
else
|
|
389
|
+
for (const t of todo) {
|
|
390
|
+
copyFileSync(t.file, t.backup, constants.COPYFILE_EXCL);
|
|
391
|
+
writeFileSync(t.file, t.out);
|
|
392
|
+
const back = imageMeta(readFileSync(t.file)).sourceType;
|
|
393
|
+
if (back === source) {
|
|
394
|
+
console.log(` labelled ${show(t.file)}, original kept as ${show(t.backup)}; read back: ${back}`);
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
copyFileSync(t.backup, t.file); // put the original back
|
|
398
|
+
failed++;
|
|
399
|
+
console.log(` couldn't label ${show(t.file)}: the label didn't read back, so the original was put back`);
|
|
400
|
+
}
|
|
401
|
+
if (todo.length && args.yes === true)
|
|
402
|
+
console.log(
|
|
403
|
+
"\nImage CDNs and optimisers often strip metadata. Run the audit again once these are live: it reads the label from the served files.",
|
|
404
|
+
);
|
|
405
|
+
return failed || skipped.length ? 1 : 0;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (isMain(import.meta.url)) await runMain(() => main(process.argv.slice(2)));
|