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,300 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// Tells the IndexNow engines (Bing, Yandex, Seznam, Naver, Yep, Internet Archive, Amazonbot) which URLs changed.
|
|
4
|
+
// Google doesn't use IndexNow.
|
|
5
|
+
// https://www.indexnow.org/documentation
|
|
6
|
+
import { readFileSync, writeFileSync, mkdirSync, readdirSync, existsSync, statSync } from "node:fs";
|
|
7
|
+
import { randomBytes } from "node:crypto";
|
|
8
|
+
import { gunzipSync } from "node:zlib";
|
|
9
|
+
import { dirname, join, resolve } from "node:path";
|
|
10
|
+
import { isMain } from "./lib/is-main.mjs";
|
|
11
|
+
import { parseArgs } from "./lib/args.mjs";
|
|
12
|
+
import { Fail, runMain, warnProxy } from "./lib/cli.mjs";
|
|
13
|
+
import { bodyUpTo } from "./lib/http.mjs";
|
|
14
|
+
import { isPublic } from "./lib/net.mjs";
|
|
15
|
+
import { changedPages } from "./seo-diff.mjs";
|
|
16
|
+
|
|
17
|
+
const USAGE =
|
|
18
|
+
"usage: node indexnow.mjs key --public-dir <dir> [--site https://x]\n node indexnow.mjs submit --site https://x --key <key> (--urls file.txt | --from-diff seo-diff.json | --from-sitemap https://x/sitemap.xml) [--endpoint url] [--out seo-audit/indexnow.json] [--yes]";
|
|
19
|
+
const fail = msg => {
|
|
20
|
+
throw new Fail("indexnow: " + msg);
|
|
21
|
+
};
|
|
22
|
+
const KEY = /^[a-zA-Z0-9-]{8,128}$/;
|
|
23
|
+
const UA = "seo-supervisor-indexnow";
|
|
24
|
+
// A sitemap may be at most 50 MB uncompressed, so neither the answer nor a .gz's output is read past that: a
|
|
25
|
+
// decompression bomb can't fill memory.
|
|
26
|
+
// https://www.sitemaps.org/protocol.html
|
|
27
|
+
const SITEMAP_MAX = 52428800;
|
|
28
|
+
const siteOf = s => {
|
|
29
|
+
try {
|
|
30
|
+
const u = new URL(s);
|
|
31
|
+
if (/^https?:$/.test(u.protocol)) return u;
|
|
32
|
+
} catch {}
|
|
33
|
+
fail(`--site must be the site's origin, like https://www.example.com (got ${s})`);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const read = f => {
|
|
37
|
+
try {
|
|
38
|
+
return readFileSync(f, "utf8");
|
|
39
|
+
} catch (e) {
|
|
40
|
+
fail(`can't read ${f} (${e.code || e.message})`);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const getText = async u => {
|
|
44
|
+
let r, b;
|
|
45
|
+
try {
|
|
46
|
+
r = await fetch(u, { headers: { "user-agent": UA }, signal: AbortSignal.timeout(20000) });
|
|
47
|
+
if (r.ok) b = await bodyUpTo(r, SITEMAP_MAX);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
fail(`couldn't fetch ${u} (${e.cause?.code || e.message})`);
|
|
50
|
+
}
|
|
51
|
+
if (!r.ok) fail(`${u} returned ${r.status}`);
|
|
52
|
+
if (!b) fail(`${u} is over 50 MB, more than a sitemap may be (sitemaps.org)`);
|
|
53
|
+
// A gzipped sitemap (sitemap.xml.gz) starts with gzip's magic bytes 1f 8b; a server that already decoded it doesn't.
|
|
54
|
+
if (b[0] !== 0x1f || b[1] !== 0x8b) return b.toString("utf8");
|
|
55
|
+
try {
|
|
56
|
+
return gunzipSync(b, { maxOutputLength: SITEMAP_MAX }).toString("utf8");
|
|
57
|
+
} catch (e) {
|
|
58
|
+
fail(
|
|
59
|
+
e.code === "ERR_BUFFER_TOO_LARGE"
|
|
60
|
+
? `${u} unpacks to more than 50 MB, more than a sitemap may be (sitemaps.org)`
|
|
61
|
+
: `${u} looks gzipped but couldn't be unpacked`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const ENT = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
66
|
+
const decode = s =>
|
|
67
|
+
s
|
|
68
|
+
.trim()
|
|
69
|
+
.replace(/&(#x[0-9a-f]+|#\d+|\w+);/gi, (m, e) =>
|
|
70
|
+
e[0] === "#"
|
|
71
|
+
? String.fromCodePoint(e[1].toLowerCase() === "x" ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10))
|
|
72
|
+
: (ENT[e.toLowerCase()] ?? m),
|
|
73
|
+
);
|
|
74
|
+
const locs = (xml, tag) =>
|
|
75
|
+
[...xml.matchAll(new RegExp(`<${tag}\\b[\\s\\S]*?<loc>([\\s\\S]*?)</loc>`, "gi"))].map(m => decode(m[1]));
|
|
76
|
+
// An index is followed one level, to children on its own host or a public address: the index itself may be local, but
|
|
77
|
+
// it can't point this script at a private network.
|
|
78
|
+
async function sitemapLocs(u) {
|
|
79
|
+
const xml = await getText(u);
|
|
80
|
+
if (!/<sitemapindex\b/i.test(xml)) return locs(xml, "url");
|
|
81
|
+
const out = [],
|
|
82
|
+
host = new URL(u).host;
|
|
83
|
+
for (const child of locs(xml, "sitemap")) {
|
|
84
|
+
let same = false;
|
|
85
|
+
try {
|
|
86
|
+
same = new URL(child).host === host;
|
|
87
|
+
} catch {}
|
|
88
|
+
if (same || isPublic(child)) out.push(...locs(await getText(child), "url"));
|
|
89
|
+
else console.log(`indexnow: skipped the child sitemap ${child}: it isn't on ${host} or a public web address.`);
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const MEANING = {
|
|
95
|
+
200: "accepted, the URLs were submitted",
|
|
96
|
+
202: "accepted, the URLs were received while IndexNow still validates the key",
|
|
97
|
+
400: "bad request, the request isn't in IndexNow's format",
|
|
98
|
+
403: "refused, the key file isn't reachable or doesn't match the key",
|
|
99
|
+
422: "refused, some URLs don't belong to the host or the key doesn't match",
|
|
100
|
+
429: "refused, too many requests (IndexNow treats this as possible spam); wait before submitting again",
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const strOf = args => k => (typeof args[k] === "string" ? args[k] : null);
|
|
104
|
+
|
|
105
|
+
async function key(args) {
|
|
106
|
+
const str = strOf(args),
|
|
107
|
+
dir = str("public-dir");
|
|
108
|
+
if (!dir) throw new Fail(USAGE);
|
|
109
|
+
if (!existsSync(dir) || !statSync(dir).isDirectory())
|
|
110
|
+
fail(
|
|
111
|
+
`${dir} isn't a folder. Pass the committed folder the site serves at its root, like public/ or static/ (not the build output, which the next build wipes).`,
|
|
112
|
+
);
|
|
113
|
+
const site = str("site") && siteOf(args.site);
|
|
114
|
+
const existing = readdirSync(dir, { withFileTypes: true })
|
|
115
|
+
.filter(f => f.isFile() && f.name.endsWith(".txt"))
|
|
116
|
+
.map(f => f.name.slice(0, -4))
|
|
117
|
+
.find(k => KEY.test(k) && readFileSync(join(dir, k + ".txt"), "utf8").trim() === k);
|
|
118
|
+
const k = existing || randomBytes(16).toString("hex");
|
|
119
|
+
if (!existing) writeFileSync(join(dir, k + ".txt"), k, { flag: "wx" }); // wx: never overwrites a file that's already there
|
|
120
|
+
console.log(
|
|
121
|
+
`indexnow: ${existing ? "reusing the key file" : "wrote"} ${join(dir, k + ".txt")}\nkey: ${k}\nOnce deployed it must open at ${site ? new URL(`/${k}.txt`, site).href : `https://<your-site>/${k}.txt`} (the site root). Then submit with --key ${k}.`,
|
|
122
|
+
);
|
|
123
|
+
return 0;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function submit(args) {
|
|
127
|
+
const str = strOf(args);
|
|
128
|
+
if (!str("site") || !str("key") || !(str("urls") || str("from-diff") || str("from-sitemap"))) throw new Fail(USAGE);
|
|
129
|
+
const site = siteOf(args.site),
|
|
130
|
+
key = args.key;
|
|
131
|
+
if (!KEY.test(key)) fail(`--key must be 8 to 128 letters, digits or dashes, as IndexNow requires (got "${key}").`);
|
|
132
|
+
const keyLocation = new URL(`/${key}.txt`, site).href;
|
|
133
|
+
const ENDPOINT = str("endpoint") || "https://api.indexnow.org/indexnow";
|
|
134
|
+
// A dry run writes its own file, so it never overwrites the record of a real send.
|
|
135
|
+
const OUT = resolve(str("out") || "seo-audit/indexnow.json"),
|
|
136
|
+
DRY_OUT = OUT.replace(/(\.json)?$/i, "-dryrun.json");
|
|
137
|
+
|
|
138
|
+
// "www.example.com/about" is a URL without its scheme; "page.html" and "docs/setup" stay paths.
|
|
139
|
+
const schemeless = s =>
|
|
140
|
+
/^[a-z0-9-]+(\.[a-z0-9-]+)+(:\d+)?(\/|$)/i.test(s) &&
|
|
141
|
+
!/^[^/]*\.(html?|php|aspx?|jsp|xml|txt|json|pdf)(\/|$)/i.test(s);
|
|
142
|
+
const found = [];
|
|
143
|
+
if (str("urls"))
|
|
144
|
+
found.push(
|
|
145
|
+
...read(args.urls)
|
|
146
|
+
.split(/\r?\n/)
|
|
147
|
+
.map(s => s.trim())
|
|
148
|
+
.filter(s => s && !s.startsWith("#"))
|
|
149
|
+
.map(s => (schemeless(s) ? "https://" + s : s)),
|
|
150
|
+
);
|
|
151
|
+
if (str("from-diff")) {
|
|
152
|
+
let d;
|
|
153
|
+
try {
|
|
154
|
+
d = JSON.parse(read(args["from-diff"]));
|
|
155
|
+
} catch (e) {
|
|
156
|
+
if (e instanceof Fail) throw e;
|
|
157
|
+
fail(`${args["from-diff"]} isn't a seo-diff.json (run seo-diff.mjs first).`);
|
|
158
|
+
}
|
|
159
|
+
// seo-diff already leaves failing URLs out of the added pages.
|
|
160
|
+
found.push(...changedPages(d));
|
|
161
|
+
}
|
|
162
|
+
if (str("from-sitemap")) found.push(...(await sitemapLocs(args["from-sitemap"])));
|
|
163
|
+
|
|
164
|
+
// IndexNow refuses the whole batch (422) when a URL belongs to another host, so only this host's URLs go.
|
|
165
|
+
const kept = new Set(),
|
|
166
|
+
skipped = [];
|
|
167
|
+
for (const s of found) {
|
|
168
|
+
let u;
|
|
169
|
+
try {
|
|
170
|
+
u = new URL(s, site);
|
|
171
|
+
} catch {
|
|
172
|
+
skipped.push(s);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
u.hash = "";
|
|
176
|
+
if (u.host === site.host && /^https?:$/.test(u.protocol)) kept.add(u.href);
|
|
177
|
+
else skipped.push(s);
|
|
178
|
+
}
|
|
179
|
+
const urls = [...kept];
|
|
180
|
+
if (skipped.length)
|
|
181
|
+
console.log(
|
|
182
|
+
`indexnow: skipped ${skipped.length} URL(s) on another host (IndexNow only takes ${site.host}'s own URLs), e.g. ${skipped[0]}`,
|
|
183
|
+
);
|
|
184
|
+
if (!urls.length) {
|
|
185
|
+
console.log(`indexnow: no URLs on ${site.host} to submit.`);
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
// IndexNow takes at most 10,000 URLs per POST.
|
|
189
|
+
const batches = [];
|
|
190
|
+
for (let i = 0; i < urls.length; i += 10000) batches.push(urls.slice(i, i + 10000));
|
|
191
|
+
const record = (sent, results) => {
|
|
192
|
+
mkdirSync(dirname(OUT), { recursive: true });
|
|
193
|
+
writeFileSync(
|
|
194
|
+
sent ? OUT : DRY_OUT,
|
|
195
|
+
JSON.stringify(
|
|
196
|
+
{
|
|
197
|
+
when: new Date().toISOString(),
|
|
198
|
+
sent,
|
|
199
|
+
endpoint: ENDPOINT,
|
|
200
|
+
host: site.host,
|
|
201
|
+
keyLocation,
|
|
202
|
+
count: urls.length,
|
|
203
|
+
skipped: skipped.length,
|
|
204
|
+
batches: results,
|
|
205
|
+
urls,
|
|
206
|
+
},
|
|
207
|
+
null,
|
|
208
|
+
2,
|
|
209
|
+
),
|
|
210
|
+
);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
if (!args.yes) {
|
|
214
|
+
record(false, []);
|
|
215
|
+
console.log(
|
|
216
|
+
`indexnow: dry run, nothing sent. It would POST ${urls.length} URL(s) for ${site.host} to ${ENDPOINT} in ${batches.length} batch(es), with keyLocation ${keyLocation}:\n${urls
|
|
217
|
+
.slice(0, 20)
|
|
218
|
+
.map(u => " " + u)
|
|
219
|
+
.join(
|
|
220
|
+
"\n",
|
|
221
|
+
)}${urls.length > 20 ? `\n … and ${urls.length - 20} more` : ""}\nThe full list is in ${DRY_OUT}. Sending tells outside search engines about these URLs: re-run with --yes once the owner approves and ${keyLocation} is live.`,
|
|
222
|
+
);
|
|
223
|
+
return 0;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let check;
|
|
227
|
+
// Redirects aren't followed: IndexNow wants the key at keyLocation itself, and a redirect to another host means
|
|
228
|
+
// --site is wrong.
|
|
229
|
+
try {
|
|
230
|
+
const r = await fetch(keyLocation, {
|
|
231
|
+
redirect: "manual",
|
|
232
|
+
headers: { "user-agent": UA },
|
|
233
|
+
signal: AbortSignal.timeout(20000),
|
|
234
|
+
});
|
|
235
|
+
check = {
|
|
236
|
+
status: r.status,
|
|
237
|
+
body: String((await bodyUpTo(r, 1024)) ?? "").trim(),
|
|
238
|
+
location: r.headers.get("location"),
|
|
239
|
+
};
|
|
240
|
+
} catch (e) {
|
|
241
|
+
check = { status: e.cause?.code || e.message, body: "" };
|
|
242
|
+
}
|
|
243
|
+
let moved;
|
|
244
|
+
try {
|
|
245
|
+
moved = check.location && new URL(check.location, keyLocation);
|
|
246
|
+
} catch {}
|
|
247
|
+
if (moved && moved.host !== site.host)
|
|
248
|
+
fail(
|
|
249
|
+
`${keyLocation} redirects to ${moved.href}: your site answers at ${moved.host}; use --site ${moved.origin}. Nothing was sent.`,
|
|
250
|
+
);
|
|
251
|
+
if (check.status !== 200 || check.body !== key)
|
|
252
|
+
fail(
|
|
253
|
+
`the key file isn't deployed yet: ${keyLocation} ${check.status === 200 ? "doesn't contain the key" : `returned ${check.status}`}. Deploy it (node indexnow.mjs key --public-dir <the folder served at the site root>), check it opens in a browser, then re-run. Nothing was sent.`,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
const results = [];
|
|
257
|
+
for (const [i, list] of batches.entries()) {
|
|
258
|
+
let status = null,
|
|
259
|
+
meaning,
|
|
260
|
+
detail = "";
|
|
261
|
+
try {
|
|
262
|
+
const r = await fetch(ENDPOINT, {
|
|
263
|
+
method: "POST",
|
|
264
|
+
headers: { "content-type": "application/json; charset=utf-8", "user-agent": UA },
|
|
265
|
+
body: JSON.stringify({ host: site.host, key, keyLocation, urlList: list }),
|
|
266
|
+
signal: AbortSignal.timeout(30000),
|
|
267
|
+
});
|
|
268
|
+
status = r.status;
|
|
269
|
+
detail = (await r.text()).trim().slice(0, 300);
|
|
270
|
+
meaning = MEANING[status] || "unexpected response";
|
|
271
|
+
} catch (e) {
|
|
272
|
+
meaning = `no response (${e.cause?.code || e.message})`;
|
|
273
|
+
}
|
|
274
|
+
const ok = status === 200 || status === 202;
|
|
275
|
+
results.push({ urls: list.length, status, meaning, ...(!ok && detail ? { detail } : {}) });
|
|
276
|
+
console.log(
|
|
277
|
+
`indexnow: batch ${i + 1}/${batches.length} (${list.length} URLs): ${status ?? "-"} ${meaning}${!ok && detail ? `: ${detail}` : ""}`,
|
|
278
|
+
);
|
|
279
|
+
if (!ok) {
|
|
280
|
+
if (i + 1 < batches.length) console.log(`indexnow: stopped, ${batches.length - i - 1} batch(es) not sent.`);
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
record(true, results);
|
|
285
|
+
console.log(`indexnow: wrote ${OUT}`);
|
|
286
|
+
return results.every(r => r.status === 200 || r.status === 202) ? 0 : 1;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (isMain(import.meta.url))
|
|
290
|
+
await runMain(() => {
|
|
291
|
+
const args = parseArgs(process.argv.slice(2), {
|
|
292
|
+
flags: ["public-dir", "site", "key", "urls", "from-diff", "from-sitemap", "endpoint", "out"],
|
|
293
|
+
booleans: ["yes"],
|
|
294
|
+
positionals: 1,
|
|
295
|
+
usage: USAGE,
|
|
296
|
+
}),
|
|
297
|
+
cmd = args._[0];
|
|
298
|
+
if (cmd !== "key") warnProxy(); // once, before any request: key only writes a local file
|
|
299
|
+
return cmd === "key" ? key(args) : cmd === "submit" ? submit(args) : Promise.reject(new Fail(USAGE));
|
|
300
|
+
});
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @ts-check
|
|
3
|
+
// Files audit findings as GitHub issues through the gh CLI.
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
6
|
+
import { dirname, resolve } from "node:path";
|
|
7
|
+
import { isMain } from "./lib/is-main.mjs";
|
|
8
|
+
import { parseArgs } from "./lib/args.mjs";
|
|
9
|
+
import { Fail, runMain } from "./lib/cli.mjs";
|
|
10
|
+
import { findingsOf } from "./lib/score.mjs";
|
|
11
|
+
import { redact } from "./lib/redact.mjs";
|
|
12
|
+
import { mdSafe, noteCode as code, noteMd } from "./report/format.mjs";
|
|
13
|
+
|
|
14
|
+
const SEV_LABEL = {
|
|
15
|
+
blocker: "Blocker",
|
|
16
|
+
high: "High",
|
|
17
|
+
"needs-decision": "Needs a decision",
|
|
18
|
+
medium: "Medium",
|
|
19
|
+
low: "Low",
|
|
20
|
+
};
|
|
21
|
+
const REPO = /^[\w.][\w.-]*(\/[\w.][\w.-]*){1,2}$/;
|
|
22
|
+
// @name in a finding's text (often quoted from a crawled page) would notify that GitHub user: a zero-width space after
|
|
23
|
+
// the @ stops it. An email address (a letter right before the @) is left as it is.
|
|
24
|
+
const noPing = s => String(s ?? "").replace(/(^|[^\w])@(?=\w)/g, "$1@");
|
|
25
|
+
// Findings quote crawled pages and other sites, so mdSafe escapes them: `code` spans and **bold** stay, bare URLs
|
|
26
|
+
// become code spans, and the rest is backslash-escaped. Nothing can show as a link, image or tag, or notify anyone.
|
|
27
|
+
const md = s => mdSafe(s, t => noPing(noteMd(t)));
|
|
28
|
+
// gh's own error text, with any secret masked (lib/redact.mjs knows GitHub's token prefixes too).
|
|
29
|
+
const tidy = s =>
|
|
30
|
+
redact(
|
|
31
|
+
String(s || "no message")
|
|
32
|
+
.trim()
|
|
33
|
+
.split("\n")
|
|
34
|
+
.slice(-3)
|
|
35
|
+
.join(" "),
|
|
36
|
+
).slice(0, 300);
|
|
37
|
+
|
|
38
|
+
/** One finding as an issue: its own title, what, why, fix and up to 10 evidence lines, then the marker. */
|
|
39
|
+
export function issueOf(f, site) {
|
|
40
|
+
let host = "";
|
|
41
|
+
try {
|
|
42
|
+
host = new URL(site).host;
|
|
43
|
+
} catch {}
|
|
44
|
+
const ev = (Array.isArray(f.evidence) ? f.evidence : []).filter(e => e && (e.url || e.file)),
|
|
45
|
+
total = Math.max(Number(f.count) || 0, ev.length),
|
|
46
|
+
shown = Math.min(10, ev.length);
|
|
47
|
+
const marker = `<!-- seo-supervisor:${host ? host + ":" : ""}${f.id} -->`;
|
|
48
|
+
const body = [
|
|
49
|
+
f.what && `**What's wrong.** ${md(f.what)}`,
|
|
50
|
+
f.why && `**Why it matters.** ${md(f.why)}`,
|
|
51
|
+
f.fix && `**How to fix.** ${md(f.fix)}`,
|
|
52
|
+
shown &&
|
|
53
|
+
[
|
|
54
|
+
`**Evidence**${shown < total ? ` (${shown} of ${total.toLocaleString("en-US")} cases)` : ""}:`,
|
|
55
|
+
...ev
|
|
56
|
+
.slice(0, 10)
|
|
57
|
+
.map(
|
|
58
|
+
e =>
|
|
59
|
+
`- ${[e.url && code(e.url), e.file && code(e.file + (e.line ? ":" + e.line : ""))].filter(Boolean).join(" ")}${e.detail ? ` ${md(e.detail)}` : ""}`,
|
|
60
|
+
),
|
|
61
|
+
].join("\n"),
|
|
62
|
+
`${SEV_LABEL[f.severity] || "Low"}${f.rule ? ` · Rule ${f.rule}` : ""} · Finding ${f.id} from SEO Supervisor's audit${host ? ` of ${host}` : ""}.`,
|
|
63
|
+
marker,
|
|
64
|
+
]
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.join("\n\n");
|
|
67
|
+
return { id: f.id, title: noPing(String(f.title || f.id).replace(/\s+/g, " ")).slice(0, 256), body, marker };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The findings to file: the ids asked for, or every open one except those marked won't fix (wontFix: how many). */
|
|
71
|
+
export function pickFindings(findings, ids) {
|
|
72
|
+
if (!ids)
|
|
73
|
+
return {
|
|
74
|
+
picked: findings.filter(f => !["fixed", "wont-fix"].includes(f.status)),
|
|
75
|
+
unknown: [],
|
|
76
|
+
wontFix: findings.filter(f => f.status === "wont-fix").length,
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
picked: findings.filter(f => ids.includes(f.id)),
|
|
80
|
+
unknown: ids.filter(id => !findings.some(f => f.id === id)),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Never through a shell, so nothing in a title or body is read as a command. gh keeps its own login, so no token is
|
|
85
|
+
// read, stored or printed here.
|
|
86
|
+
const gh = (args, input) =>
|
|
87
|
+
new Promise(done => {
|
|
88
|
+
const p = spawn("gh", args, { stdio: ["pipe", "pipe", "pipe"], windowsHide: true });
|
|
89
|
+
let stdout = "",
|
|
90
|
+
stderr = "";
|
|
91
|
+
p.stdout.on("data", c => {
|
|
92
|
+
stdout += c;
|
|
93
|
+
});
|
|
94
|
+
p.stderr.on("data", c => {
|
|
95
|
+
stderr += c;
|
|
96
|
+
});
|
|
97
|
+
p.on("error", e =>
|
|
98
|
+
done({
|
|
99
|
+
code: 1,
|
|
100
|
+
stdout,
|
|
101
|
+
stderr:
|
|
102
|
+
/** @type {any} */ (e).code === "ENOENT"
|
|
103
|
+
? "the GitHub CLI (gh) isn't installed or isn't on PATH: see cli.github.com"
|
|
104
|
+
: e.message,
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
p.on("close", c => done({ code: c ?? 1, stdout, stderr }));
|
|
108
|
+
p.stdin.on("error", () => {});
|
|
109
|
+
p.stdin.end(input ?? "");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/** Prints the dry run, or with yes files each issue not filed before. run(args, stdin) is gh; tests pass their own.
|
|
113
|
+
* @param {{ id: string, title: string, body: string, marker: string }[]} list
|
|
114
|
+
* @param {{ yes?: boolean, repo?: string | null, run?: (args: string[], input?: string) => Promise<{ code: number, stdout: string, stderr: string }>, log?: (line: string) => void }} [options] */
|
|
115
|
+
export async function fileIssues(list, { yes = false, repo = null, run = gh, log = console.log } = {}) {
|
|
116
|
+
if (repo != null && !REPO.test(repo))
|
|
117
|
+
throw new Fail(`issues: --repo takes owner/name (or host/owner/name), not "${repo}".`);
|
|
118
|
+
const where = repo ? [`--repo=${repo}`] : [],
|
|
119
|
+
at = repo || "the current folder's GitHub repository",
|
|
120
|
+
n = `${list.length} issue${list.length === 1 ? "" : "s"}`;
|
|
121
|
+
if (!yes) {
|
|
122
|
+
log(
|
|
123
|
+
`Dry run: ${n} would be filed in ${at}. Nothing was sent. Run again with --yes to file them with your own gh login; findings already filed are skipped.`,
|
|
124
|
+
);
|
|
125
|
+
for (const i of list) log(`\n--- ${i.id}: ${i.title}\n\n${i.body}`);
|
|
126
|
+
return { created: 0, skipped: 0, failed: 0 };
|
|
127
|
+
}
|
|
128
|
+
// A finding whose marker is in one of the newest 1,000 issues, open or closed, was filed before.
|
|
129
|
+
// https://cli.github.com/manual/gh_issue_list
|
|
130
|
+
const listed = await run([
|
|
131
|
+
"issue",
|
|
132
|
+
"list",
|
|
133
|
+
"--state",
|
|
134
|
+
"all",
|
|
135
|
+
"--limit",
|
|
136
|
+
"1000",
|
|
137
|
+
"--json",
|
|
138
|
+
"number,url,body",
|
|
139
|
+
...where,
|
|
140
|
+
]);
|
|
141
|
+
let existing = null;
|
|
142
|
+
try {
|
|
143
|
+
existing = listed.code === 0 ? JSON.parse(listed.stdout) : null;
|
|
144
|
+
} catch {}
|
|
145
|
+
if (!Array.isArray(existing)) {
|
|
146
|
+
log(
|
|
147
|
+
`gh couldn't list the issues in ${at}, so none were filed: ${tidy(listed.stderr)}. Check gh auth status and --repo.`,
|
|
148
|
+
);
|
|
149
|
+
return { created: 0, skipped: 0, failed: list.length };
|
|
150
|
+
}
|
|
151
|
+
const r = { created: 0, skipped: 0, failed: 0 };
|
|
152
|
+
for (const [k, i] of list.entries()) {
|
|
153
|
+
const old = existing.find(x => typeof x?.body === "string" && x.body.includes(i.marker));
|
|
154
|
+
if (old) {
|
|
155
|
+
r.skipped++;
|
|
156
|
+
log(`${i.id}: already filed as #${old.number} (${old.url}), skipped.`);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
// --body-file - reads the body from standard input. https://cli.github.com/manual/gh_issue_create
|
|
160
|
+
const made = await run(["issue", "create", `--title=${i.title}`, "--body-file", "-", ...where], i.body);
|
|
161
|
+
if (made.code !== 0) {
|
|
162
|
+
r.failed = list.length - k;
|
|
163
|
+
log(`${i.id}: gh couldn't create the issue, so it and the ones after it weren't filed: ${tidy(made.stderr)}`);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
r.created++;
|
|
167
|
+
log(`${i.id}: filed ${made.stdout.trim()}`);
|
|
168
|
+
}
|
|
169
|
+
log(`Filed ${r.created}, skipped ${r.skipped} already filed${r.failed ? `, ${r.failed} not filed` : ""}.`);
|
|
170
|
+
return r;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const USAGE = "usage: node issues.mjs --audit seo-audit/seo-audit.json [--ids F-001,F-002] [--repo owner/name] [--yes]";
|
|
174
|
+
async function main(argv) {
|
|
175
|
+
const args = parseArgs(argv, { flags: ["audit", "ids", "repo"], booleans: ["yes"], positionals: 0, usage: USAGE }),
|
|
176
|
+
str = k => (typeof args[k] === "string" ? args[k] : null);
|
|
177
|
+
const file = str("audit") || "seo-audit/seo-audit.json",
|
|
178
|
+
ids =
|
|
179
|
+
str("ids")
|
|
180
|
+
?.split(",")
|
|
181
|
+
.map(s => s.trim())
|
|
182
|
+
.filter(Boolean) || null;
|
|
183
|
+
let raw;
|
|
184
|
+
try {
|
|
185
|
+
raw = JSON.parse(readFileSync(file, "utf8"));
|
|
186
|
+
} catch (e) {
|
|
187
|
+
throw new Fail(
|
|
188
|
+
e.code === "ENOENT"
|
|
189
|
+
? `issues: ${file} doesn't exist. Pass --audit with the audit's JSON file.`
|
|
190
|
+
: `issues: ${file} isn't JSON (${e.code || e.message}). Pass --audit with the audit's JSON file.`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
// findingsOf applies the project's check settings, so a check turned off isn't filed.
|
|
194
|
+
const loaded = findingsOf(raw, dirname(resolve(file)));
|
|
195
|
+
for (const w of loaded.warnings) console.error("Settings warning: " + w);
|
|
196
|
+
const { picked, unknown, wontFix } = pickFindings(loaded.findings, ids);
|
|
197
|
+
if (unknown.length)
|
|
198
|
+
console.error(
|
|
199
|
+
`No finding with the ID ${unknown.join(", ")} in ${file} (or its check is turned off in settings.json).`,
|
|
200
|
+
);
|
|
201
|
+
if (wontFix)
|
|
202
|
+
console.log(
|
|
203
|
+
`${wontFix} finding${wontFix === 1 ? " marked won't fix isn't" : "s marked won't fix aren't"} filed. The report still counts ${wontFix === 1 ? "it" : "them"} as open; pass --ids to file one anyway.`,
|
|
204
|
+
);
|
|
205
|
+
const site = raw?.site?.url || raw?.siteOrigin || "";
|
|
206
|
+
if (!picked.length) {
|
|
207
|
+
console.log("No findings to file.");
|
|
208
|
+
return 0;
|
|
209
|
+
}
|
|
210
|
+
const r = await fileIssues(
|
|
211
|
+
picked.map(f => issueOf(f, site)),
|
|
212
|
+
{ yes: !!args.yes, repo: str("repo") },
|
|
213
|
+
);
|
|
214
|
+
return r.failed ? 1 : 0;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (isMain(import.meta.url)) await runMain(() => main(process.argv.slice(2)));
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Authority scores for the site and its competitors: Open PageRank, Ahrefs' free Domain Rating and Moz.
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { Refused, setLine, PASTE, refusal, request, chunks, domainOf } from "./http.mjs";
|
|
5
|
+
|
|
6
|
+
export const openpagerank = {
|
|
7
|
+
name: "Open PageRank",
|
|
8
|
+
env: "OPENPAGERANK_KEY",
|
|
9
|
+
endpointEnv: "OPENPAGERANK_ENDPOINT",
|
|
10
|
+
endpoint: "https://openpagerank.keywordseverywhere.com/api/v1.0/getPageRank",
|
|
11
|
+
adds: "a 0–10 authority score for the site and the competitors you name, based on links between sites",
|
|
12
|
+
where: "keywordseverywhere.com/openpagerank",
|
|
13
|
+
guide: [
|
|
14
|
+
"Open PageRank (free, now run by Keywords Everywhere) adds a 0–10 authority score for your site and the competitors you name, based on links between sites.",
|
|
15
|
+
"1. Sign up at https://keywordseverywhere.com/openpagerank and open your Open PageRank dashboard.",
|
|
16
|
+
"2. Create an API key and register the IP address of the computer that will use it.",
|
|
17
|
+
`3. Copy the key, then in your own terminal run: ${setLine("openpagerank")}`,
|
|
18
|
+
PASTE,
|
|
19
|
+
"Since 1 November 2026, free keys only work from that one registered IP address. A 403 error usually means your IP address changed (a new network, a VPN, or your provider changed it): update it in the dashboard. You can change it once a week.",
|
|
20
|
+
"Free keys cover 30,000 domains a month.",
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
export const ahrefs = {
|
|
24
|
+
name: "Ahrefs Domain Rating",
|
|
25
|
+
env: "AHREFS_KEY",
|
|
26
|
+
endpointEnv: "AHREFS_ENDPOINT",
|
|
27
|
+
endpoint: "https://api.ahrefs.com/v3/public/domain-rating-free",
|
|
28
|
+
adds: "Ahrefs' 0–100 Domain Rating for the site and the competitors you name",
|
|
29
|
+
where: "ahrefs.com → Account settings → API keys",
|
|
30
|
+
guide: [
|
|
31
|
+
"Ahrefs' free Domain Rating adds Ahrefs' 0–100 Domain Rating for your site and the competitors you name. It's free, with a free Ahrefs account.",
|
|
32
|
+
"1. Create a free account at https://ahrefs.com, or sign in.",
|
|
33
|
+
"2. Go to Account settings → API keys and create a key.",
|
|
34
|
+
`3. Copy the key, then in your own terminal run: ${setLine("ahrefs")}`,
|
|
35
|
+
PASTE,
|
|
36
|
+
'Ahrefs\' licence requires every place that shows these numbers to say "Domain Rating by Ahrefs" with a link to https://ahrefs.com/. The report does this for you.',
|
|
37
|
+
],
|
|
38
|
+
};
|
|
39
|
+
// optional keeps it off fetch's Limits list when no key is set.
|
|
40
|
+
// https://moz.com/api/docs/guides/authentication
|
|
41
|
+
export const moz = {
|
|
42
|
+
name: "Moz",
|
|
43
|
+
env: "MOZ_TOKEN",
|
|
44
|
+
endpointEnv: "MOZ_ENDPOINT",
|
|
45
|
+
endpoint: "https://api.moz.com/jsonrpc",
|
|
46
|
+
optional: true,
|
|
47
|
+
adds: "Moz's Domain Authority, Spam Score and linking domains for the site and the competitors you name",
|
|
48
|
+
where: "moz.com/api/dashboard",
|
|
49
|
+
guide: [
|
|
50
|
+
"Moz adds its Domain Authority (1 to 100), Spam Score and the number of sites linking to you, for your site and the competitors you name: a second opinion next to Open PageRank and Ahrefs.",
|
|
51
|
+
"1. Sign up for the Moz API at https://moz.com/products/api/pricing and pick the free plan. The API is separate from a Moz Pro subscription. Moz asks for a card but charges nothing unless you upgrade.",
|
|
52
|
+
"2. Open https://moz.com/api/dashboard and generate a token.",
|
|
53
|
+
`3. Copy the token, then in your own terminal run: ${setLine("moz")}`,
|
|
54
|
+
PASTE,
|
|
55
|
+
"The free plan has 50 rows a month and each domain uses one, so fetch reads at most 10 domains a run, your site first. The rows refill every month.",
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
const ATTRIBUTION = { text: "Domain Rating by Ahrefs", url: "https://ahrefs.com/" };
|
|
59
|
+
export const MOZ_MAX = 10;
|
|
60
|
+
|
|
61
|
+
// Each read goes through fetchKeys' run(id, fn), which skips a service with no key and notes a refusal. end(id) is
|
|
62
|
+
// the service's address. Returns { authority, attribution, moz }.
|
|
63
|
+
export async function fetchAuthority(run, keys, domains, end, notes) {
|
|
64
|
+
const authority =
|
|
65
|
+
keys.openpagerank || keys.ahrefs ? domains.map(domain => ({ domain, openPageRank: null, ahrefsDR: null })) : [];
|
|
66
|
+
const set = (domain, field, v) => {
|
|
67
|
+
const a = authority.find(x => x.domain === domain);
|
|
68
|
+
if (a) a[field] = v;
|
|
69
|
+
};
|
|
70
|
+
let attribution = null,
|
|
71
|
+
mozRows = null;
|
|
72
|
+
await run("openpagerank", async key => {
|
|
73
|
+
for (const list of chunks(domains, 100)) {
|
|
74
|
+
const b = await request(
|
|
75
|
+
"openpagerank",
|
|
76
|
+
openpagerank,
|
|
77
|
+
`${end("openpagerank")}?${list.map(d => "domains[]=" + encodeURIComponent(d)).join("&")}`,
|
|
78
|
+
{ headers: { "API-OPR": key } },
|
|
79
|
+
);
|
|
80
|
+
for (const r of b?.response || [])
|
|
81
|
+
set(
|
|
82
|
+
domainOf(r.domain || ""),
|
|
83
|
+
"openPageRank",
|
|
84
|
+
r.status_code === 200 && !r.error && r.page_rank_decimal != null ? Number(r.page_rank_decimal) : null,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
await run("ahrefs", async key => {
|
|
89
|
+
for (const list of chunks(domains, 1000)) {
|
|
90
|
+
const b = await request("ahrefs", ahrefs, end("ahrefs"), {
|
|
91
|
+
method: "POST",
|
|
92
|
+
headers: { authorization: `Bearer ${key}`, "content-type": "application/json" },
|
|
93
|
+
body: JSON.stringify({ targets: list }),
|
|
94
|
+
});
|
|
95
|
+
for (const t of b?.domain_rating?.targets || [])
|
|
96
|
+
set(domainOf(t.target || ""), "ahrefsDR", typeof t.domain_rating === "number" ? t.domain_rating : null);
|
|
97
|
+
attribution = {
|
|
98
|
+
...ATTRIBUTION,
|
|
99
|
+
license: b?.domain_rating?.license || "https://ahrefs.com/legal/domain-rating-license",
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
// Moz's data.site.metrics.fetch with scope "domain" uses one row a call, and the free plan has 50 rows a month, so a
|
|
104
|
+
// run reads at most MOZ_MAX domains. Domains already read are kept on a refusal.
|
|
105
|
+
await run("moz", async key => {
|
|
106
|
+
mozRows = [];
|
|
107
|
+
if (domains.length > MOZ_MAX)
|
|
108
|
+
notes.push(
|
|
109
|
+
`Moz: read the first ${MOZ_MAX} of ${domains.length} domains. Each uses one of the free plan's 50 rows a month.`,
|
|
110
|
+
);
|
|
111
|
+
for (const domain of domains.slice(0, MOZ_MAX)) {
|
|
112
|
+
const b = await request("moz", moz, end("moz"), {
|
|
113
|
+
method: "POST",
|
|
114
|
+
headers: { "x-moz-token": key, "content-type": "application/json" },
|
|
115
|
+
body: JSON.stringify({
|
|
116
|
+
jsonrpc: "2.0",
|
|
117
|
+
id: randomUUID(),
|
|
118
|
+
method: "data.site.metrics.fetch",
|
|
119
|
+
params: { data: { site_query: { query: domain, scope: "domain" } } },
|
|
120
|
+
}),
|
|
121
|
+
});
|
|
122
|
+
if (b?.error) throw new Refused(refusal("moz", moz, Number(b.error.status || b.error.httpStatus) || 400, b));
|
|
123
|
+
const m = b?.result?.site_metrics || {},
|
|
124
|
+
n = v => (typeof v === "number" && v >= 0 ? v : null); // Spam Score -1: Moz has none
|
|
125
|
+
mozRows.push({
|
|
126
|
+
domain,
|
|
127
|
+
domainAuthority: n(m.domain_authority),
|
|
128
|
+
spamScore: n(m.spam_score),
|
|
129
|
+
linkingDomains: n(m.root_domains_to_root_domain),
|
|
130
|
+
inboundLinks: n(m.external_pages_to_root_domain),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
return { authority, attribution, moz: mozRows };
|
|
135
|
+
}
|