@itsaakashpatel/seo-webmaster-mcp 0.0.0-stage → 1.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/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +380 -2
- package/build/approvals/format.d.ts +29 -0
- package/build/approvals/format.js +88 -0
- package/build/approvals/gate.d.ts +23 -0
- package/build/approvals/gate.js +102 -0
- package/build/approvals/groups.d.ts +14 -0
- package/build/approvals/groups.js +113 -0
- package/build/approvals/session.d.ts +8 -0
- package/build/approvals/session.js +23 -0
- package/build/cli/args.d.ts +18 -0
- package/build/cli/args.js +41 -0
- package/build/cli/commands.d.ts +8 -0
- package/build/cli/commands.js +64 -0
- package/build/cli/engines.d.ts +1 -0
- package/build/cli/engines.js +6 -0
- package/build/cli/help.d.ts +2 -0
- package/build/cli/help.js +29 -0
- package/build/cli/install/clients.d.ts +18 -0
- package/build/cli/install/clients.js +46 -0
- package/build/cli/install/command.d.ts +21 -0
- package/build/cli/install/command.js +44 -0
- package/build/cli/install/exec.d.ts +3 -0
- package/build/cli/install/exec.js +14 -0
- package/build/cli/install/flow.d.ts +14 -0
- package/build/cli/install/flow.js +114 -0
- package/build/cli/install/json-config.d.ts +24 -0
- package/build/cli/install/json-config.js +70 -0
- package/build/cli/prompter.d.ts +21 -0
- package/build/cli/prompter.js +15 -0
- package/build/cli/run.d.ts +17 -0
- package/build/cli/run.js +48 -0
- package/build/cli/screens.d.ts +45 -0
- package/build/cli/screens.js +83 -0
- package/build/cli/setup/approvals.d.ts +2 -0
- package/build/cli/setup/approvals.js +59 -0
- package/build/cli/setup/bing.d.ts +2 -0
- package/build/cli/setup/bing.js +35 -0
- package/build/cli/setup/flow.d.ts +8 -0
- package/build/cli/setup/flow.js +91 -0
- package/build/cli/setup/google.d.ts +2 -0
- package/build/cli/setup/google.js +94 -0
- package/build/cli/setup/helpers.d.ts +13 -0
- package/build/cli/setup/helpers.js +38 -0
- package/build/cli/setup/indexnow.d.ts +3 -0
- package/build/cli/setup/indexnow.js +89 -0
- package/build/cli/setup/services.d.ts +5 -0
- package/build/cli/setup/services.js +62 -0
- package/build/cli/setup/text.d.ts +10 -0
- package/build/cli/setup/text.js +40 -0
- package/build/cli/setup/types.d.ts +35 -0
- package/build/cli/setup/types.js +1 -0
- package/build/cli/status.d.ts +14 -0
- package/build/cli/status.js +54 -0
- package/build/cli/terminal.d.ts +6 -0
- package/build/cli/terminal.js +134 -0
- package/build/config/file.d.ts +29 -0
- package/build/config/file.js +58 -0
- package/build/config/index.d.ts +24 -0
- package/build/config/index.js +72 -0
- package/build/config/paths.d.ts +11 -0
- package/build/config/paths.js +19 -0
- package/build/core/analytics.d.ts +5 -0
- package/build/core/analytics.js +18 -0
- package/build/core/constants.d.ts +9 -0
- package/build/core/constants.js +9 -0
- package/build/core/errors.d.ts +18 -0
- package/build/core/errors.js +53 -0
- package/build/core/filters.d.ts +14 -0
- package/build/core/filters.js +42 -0
- package/build/core/guards.d.ts +11 -0
- package/build/core/guards.js +32 -0
- package/build/core/http.d.ts +17 -0
- package/build/core/http.js +51 -0
- package/build/core/insights/compare.d.ts +32 -0
- package/build/core/insights/compare.js +56 -0
- package/build/core/insights/crawl.d.ts +9 -0
- package/build/core/insights/crawl.js +61 -0
- package/build/core/insights/links.d.ts +5 -0
- package/build/core/insights/links.js +20 -0
- package/build/core/insights/metrics.d.ts +8 -0
- package/build/core/insights/metrics.js +15 -0
- package/build/core/insights/opportunities.d.ts +15 -0
- package/build/core/insights/opportunities.js +40 -0
- package/build/core/insights/periods.d.ts +12 -0
- package/build/core/insights/periods.js +38 -0
- package/build/core/insights/types.d.ts +31 -0
- package/build/core/insights/types.js +1 -0
- package/build/core/markdown.d.ts +14 -0
- package/build/core/markdown.js +28 -0
- package/build/core/paging.d.ts +11 -0
- package/build/core/paging.js +5 -0
- package/build/core/provider.d.ts +15 -0
- package/build/core/provider.js +1 -0
- package/build/core/rate-limit.d.ts +8 -0
- package/build/core/rate-limit.js +19 -0
- package/build/core/registry.d.ts +11 -0
- package/build/core/registry.js +16 -0
- package/build/core/responses.d.ts +13 -0
- package/build/core/responses.js +9 -0
- package/build/core/secrets.d.ts +2 -0
- package/build/core/secrets.js +9 -0
- package/build/core/types.d.ts +137 -0
- package/build/core/types.js +13 -0
- package/build/core/untrusted.d.ts +11 -0
- package/build/core/untrusted.js +34 -0
- package/build/core/urls.d.ts +16 -0
- package/build/core/urls.js +80 -0
- package/build/core/validation.d.ts +23 -0
- package/build/core/validation.js +100 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +30 -0
- package/build/providers/bing/auth.d.ts +11 -0
- package/build/providers/bing/auth.js +87 -0
- package/build/providers/bing/mappers.d.ts +18 -0
- package/build/providers/bing/mappers.js +87 -0
- package/build/providers/bing/operations.d.ts +8 -0
- package/build/providers/bing/operations.js +40 -0
- package/build/providers/bing/provider.d.ts +16 -0
- package/build/providers/bing/provider.js +132 -0
- package/build/providers/bing/records.d.ts +11 -0
- package/build/providers/bing/records.js +47 -0
- package/build/providers/bing/wire.d.ts +86 -0
- package/build/providers/bing/wire.js +38 -0
- package/build/providers/bing/writes.d.ts +9 -0
- package/build/providers/bing/writes.js +31 -0
- package/build/providers/google/auth.d.ts +13 -0
- package/build/providers/google/auth.js +174 -0
- package/build/providers/google/indexing.d.ts +14 -0
- package/build/providers/google/indexing.js +89 -0
- package/build/providers/google/mappers.d.ts +6 -0
- package/build/providers/google/mappers.js +96 -0
- package/build/providers/google/probe.d.ts +9 -0
- package/build/providers/google/probe.js +42 -0
- package/build/providers/google/provider.d.ts +15 -0
- package/build/providers/google/provider.js +118 -0
- package/build/providers/index.d.ts +4 -0
- package/build/providers/index.js +7 -0
- package/build/providers/indexnow/probe.d.ts +8 -0
- package/build/providers/indexnow/probe.js +17 -0
- package/build/providers/indexnow/provider.d.ts +21 -0
- package/build/providers/indexnow/provider.js +96 -0
- package/build/providers/insights/bing.d.ts +2 -0
- package/build/providers/insights/bing.js +73 -0
- package/build/providers/insights/google.d.ts +2 -0
- package/build/providers/insights/google.js +67 -0
- package/build/providers/insights/index.d.ts +5 -0
- package/build/providers/insights/index.js +15 -0
- package/build/providers/insights/source.d.ts +14 -0
- package/build/providers/insights/source.js +1 -0
- package/build/server.d.ts +2 -0
- package/build/server.js +14 -0
- package/build/tools/bing/catalog-crawl.d.ts +2 -0
- package/build/tools/bing/catalog-crawl.js +125 -0
- package/build/tools/bing/catalog-settings.d.ts +2 -0
- package/build/tools/bing/catalog-settings.js +51 -0
- package/build/tools/bing/catalog-site.d.ts +2 -0
- package/build/tools/bing/catalog-site.js +70 -0
- package/build/tools/bing/catalog-traffic.d.ts +2 -0
- package/build/tools/bing/catalog-traffic.js +88 -0
- package/build/tools/bing/index.d.ts +6 -0
- package/build/tools/bing/index.js +26 -0
- package/build/tools/bing/inputs.d.ts +21 -0
- package/build/tools/bing/inputs.js +34 -0
- package/build/tools/bing/read-tool.d.ts +23 -0
- package/build/tools/bing/read-tool.js +71 -0
- package/build/tools/bing/render.d.ts +8 -0
- package/build/tools/bing/render.js +50 -0
- package/build/tools/bing/write-tool.d.ts +25 -0
- package/build/tools/bing/write-tool.js +127 -0
- package/build/tools/bing/writes-settings.d.ts +2 -0
- package/build/tools/bing/writes-settings.js +227 -0
- package/build/tools/bing/writes-sites.d.ts +2 -0
- package/build/tools/bing/writes-sites.js +172 -0
- package/build/tools/engine-status.d.ts +14 -0
- package/build/tools/engine-status.js +81 -0
- package/build/tools/get-sitemap.d.ts +4 -0
- package/build/tools/get-sitemap.js +46 -0
- package/build/tools/index.d.ts +6 -0
- package/build/tools/index.js +43 -0
- package/build/tools/insights/bing-crawl-overview.d.ts +2 -0
- package/build/tools/insights/bing-crawl-overview.js +46 -0
- package/build/tools/insights/bing-link-evidence.d.ts +2 -0
- package/build/tools/insights/bing-link-evidence.js +65 -0
- package/build/tools/insights/declining-pages.d.ts +2 -0
- package/build/tools/insights/declining-pages.js +59 -0
- package/build/tools/insights/performance-overview.d.ts +2 -0
- package/build/tools/insights/performance-overview.js +39 -0
- package/build/tools/insights/report.d.ts +50 -0
- package/build/tools/insights/report.js +86 -0
- package/build/tools/insights/search-opportunities.d.ts +2 -0
- package/build/tools/insights/search-opportunities.js +30 -0
- package/build/tools/insights/shared.d.ts +44 -0
- package/build/tools/insights/shared.js +90 -0
- package/build/tools/inspect-url.d.ts +4 -0
- package/build/tools/inspect-url.js +104 -0
- package/build/tools/list-sitemaps.d.ts +4 -0
- package/build/tools/list-sitemaps.js +37 -0
- package/build/tools/list-sites.d.ts +4 -0
- package/build/tools/list-sites.js +56 -0
- package/build/tools/search-analytics.d.ts +4 -0
- package/build/tools/search-analytics.js +108 -0
- package/build/tools/shared.d.ts +29 -0
- package/build/tools/shared.js +63 -0
- package/build/tools/sitemaps.d.ts +2 -0
- package/build/tools/sitemaps.js +140 -0
- package/build/tools/submit-google.d.ts +4 -0
- package/build/tools/submit-google.js +68 -0
- package/build/tools/submit-urls.d.ts +4 -0
- package/build/tools/submit-urls.js +65 -0
- package/package.json +66 -4
- package/server.json +58 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { isRecord } from "../core/guards.js";
|
|
5
|
+
export const APPROVAL_CHOICES = ["ask", "run"];
|
|
6
|
+
/** The groups that the user can set to "run without asking". `read` never asks. */
|
|
7
|
+
export const CONFIGURABLE_GROUPS = ["submit", "visibility", "access"];
|
|
8
|
+
const choice = z.enum(APPROVAL_CHOICES).optional();
|
|
9
|
+
const CONFIG_SCHEMA = z.object({
|
|
10
|
+
bing: z.object({ apiKey: z.string().min(1) }).optional(),
|
|
11
|
+
google: z.object({ keyFile: z.string().min(1) }).optional(),
|
|
12
|
+
indexnow: z
|
|
13
|
+
.object({ key: z.string().min(1), keyLocation: z.string().min(1).optional() })
|
|
14
|
+
.optional(),
|
|
15
|
+
approvals: z.object({ submit: choice, visibility: choice, access: choice }).optional(),
|
|
16
|
+
});
|
|
17
|
+
/** A configuration problem. The message never shows the content of the file. */
|
|
18
|
+
export class ConfigError extends Error {
|
|
19
|
+
constructor(message, options) {
|
|
20
|
+
super(message, options);
|
|
21
|
+
this.name = "ConfigError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function invalid(path, cause) {
|
|
25
|
+
return new ConfigError(`The configuration file "${path}" is not valid. Fix it, delete it, or run \`seo-webmaster-mcp setup\`.`, { cause });
|
|
26
|
+
}
|
|
27
|
+
/** Reads and checks the file. A missing file gives an empty configuration. */
|
|
28
|
+
export function readConfigFile(path) {
|
|
29
|
+
if (!existsSync(path)) {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
let json;
|
|
33
|
+
try {
|
|
34
|
+
json = JSON.parse(readFileSync(path, "utf8"));
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
// The parser message can quote the content, so it is not copied into the error.
|
|
38
|
+
throw invalid(path, err);
|
|
39
|
+
}
|
|
40
|
+
const parsed = isRecord(json) ? CONFIG_SCHEMA.safeParse(json) : undefined;
|
|
41
|
+
if (!parsed?.success) {
|
|
42
|
+
throw invalid(path);
|
|
43
|
+
}
|
|
44
|
+
return parsed.data;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Writes the file with mode 600 in a folder with mode 700. It writes a temporary file first and
|
|
48
|
+
* then renames it, so a reader never sees a half-written file.
|
|
49
|
+
*/
|
|
50
|
+
export function writeConfigFile(path, config) {
|
|
51
|
+
const dir = dirname(path);
|
|
52
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
53
|
+
chmodSync(dir, 0o700);
|
|
54
|
+
const temp = join(dir, `.config-${process.pid}-${Date.now()}.tmp`);
|
|
55
|
+
writeFileSync(temp, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600, flag: "wx" });
|
|
56
|
+
chmodSync(temp, 0o600);
|
|
57
|
+
renameSync(temp, path);
|
|
58
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ApprovalChoice, type ConfigFile, type ConfigurableGroup } from "./file.js";
|
|
2
|
+
/** The settings that can come from an environment variable or from the file. */
|
|
3
|
+
export type SettingName = "bingApiKey" | "googleKeyFile" | "googleServiceAccountKey" | "indexNowKey" | "indexNowKeyLocation";
|
|
4
|
+
export interface ResolvedSetting {
|
|
5
|
+
readonly value: string | undefined;
|
|
6
|
+
readonly source: "environment" | "file" | "none";
|
|
7
|
+
/** The variable that gave the value, when `source` is `environment`. */
|
|
8
|
+
readonly envName?: string;
|
|
9
|
+
/** True when a variable hides a different value in the file. */
|
|
10
|
+
readonly overridesFile: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Reads the configuration file once. The server calls this at start and then never again. */
|
|
13
|
+
export declare function loadConfig(path?: string): ConfigFile;
|
|
14
|
+
/** Drops the loaded file, or replaces it. Tests use this. */
|
|
15
|
+
export declare function resetConfig(file?: ConfigFile): void;
|
|
16
|
+
/** Reads a variable. An empty or blank value counts as not set. */
|
|
17
|
+
export declare function readEnv(name: string): string | undefined;
|
|
18
|
+
export declare function resolveSetting(name: SettingName): ResolvedSetting;
|
|
19
|
+
export declare function getSetting(name: SettingName): string | undefined;
|
|
20
|
+
/** The choice of the user for one group. The default is to ask. The file only sets it. */
|
|
21
|
+
export declare function getApprovalChoice(group: ConfigurableGroup): ApprovalChoice;
|
|
22
|
+
/** Text for `status` and setup. It never shows a value. */
|
|
23
|
+
export declare function describeSource(setting: ResolvedSetting): string;
|
|
24
|
+
export declare function currentConfig(): ConfigFile;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { readConfigFile, } from "./file.js";
|
|
2
|
+
import { configFilePath } from "./paths.js";
|
|
3
|
+
const SETTINGS = {
|
|
4
|
+
bingApiKey: { env: ["BING_WEBMASTER_API_KEY"], fromFile: (file) => file.bing?.apiKey },
|
|
5
|
+
googleKeyFile: {
|
|
6
|
+
env: ["GOOGLE_APPLICATION_CREDENTIALS"],
|
|
7
|
+
fromFile: (file) => file.google?.keyFile,
|
|
8
|
+
},
|
|
9
|
+
// Only an environment variable can hold the raw key. The file keeps a path, never a key.
|
|
10
|
+
googleServiceAccountKey: {
|
|
11
|
+
env: ["GOOGLE_SERVICE_ACCOUNT_KEY", "GOOGLE_SERVICE_ACCOUNT_JSON"],
|
|
12
|
+
fromFile: () => undefined,
|
|
13
|
+
},
|
|
14
|
+
indexNowKey: { env: ["INDEXNOW_KEY"], fromFile: (file) => file.indexnow?.key },
|
|
15
|
+
indexNowKeyLocation: {
|
|
16
|
+
env: ["INDEXNOW_KEY_LOCATION"],
|
|
17
|
+
fromFile: (file) => file.indexnow?.keyLocation,
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
let loadedFile;
|
|
21
|
+
/** Reads the configuration file once. The server calls this at start and then never again. */
|
|
22
|
+
export function loadConfig(path = configFilePath()) {
|
|
23
|
+
loadedFile = readConfigFile(path);
|
|
24
|
+
return loadedFile;
|
|
25
|
+
}
|
|
26
|
+
/** Drops the loaded file, or replaces it. Tests use this. */
|
|
27
|
+
export function resetConfig(file) {
|
|
28
|
+
loadedFile = file;
|
|
29
|
+
}
|
|
30
|
+
function currentFile() {
|
|
31
|
+
return loadedFile ?? loadConfig();
|
|
32
|
+
}
|
|
33
|
+
/** Reads a variable. An empty or blank value counts as not set. */
|
|
34
|
+
export function readEnv(name) {
|
|
35
|
+
return process.env[name]?.trim() || undefined;
|
|
36
|
+
}
|
|
37
|
+
export function resolveSetting(name) {
|
|
38
|
+
const spec = SETTINGS[name];
|
|
39
|
+
const envName = spec.env.find((candidate) => readEnv(candidate) !== undefined);
|
|
40
|
+
const fileValue = spec.fromFile(currentFile())?.trim() || undefined;
|
|
41
|
+
if (envName !== undefined) {
|
|
42
|
+
return {
|
|
43
|
+
value: readEnv(envName),
|
|
44
|
+
source: "environment",
|
|
45
|
+
envName,
|
|
46
|
+
overridesFile: fileValue !== undefined,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
value: fileValue,
|
|
51
|
+
source: fileValue === undefined ? "none" : "file",
|
|
52
|
+
overridesFile: false,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export function getSetting(name) {
|
|
56
|
+
return resolveSetting(name).value;
|
|
57
|
+
}
|
|
58
|
+
/** The choice of the user for one group. The default is to ask. The file only sets it. */
|
|
59
|
+
export function getApprovalChoice(group) {
|
|
60
|
+
return currentFile().approvals?.[group] ?? "ask";
|
|
61
|
+
}
|
|
62
|
+
/** Text for `status` and setup. It never shows a value. */
|
|
63
|
+
export function describeSource(setting) {
|
|
64
|
+
if (setting.source === "environment") {
|
|
65
|
+
const hint = setting.overridesFile ? "; the file value is not used" : "";
|
|
66
|
+
return `set by environment variable ${setting.envName}${hint}`;
|
|
67
|
+
}
|
|
68
|
+
return setting.source === "file" ? "set in the configuration file" : "not set";
|
|
69
|
+
}
|
|
70
|
+
export function currentConfig() {
|
|
71
|
+
return currentFile();
|
|
72
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const CONFIG_DIR_NAME = "seo-webmaster-mcp";
|
|
2
|
+
export declare const CONFIG_FILE_NAME = "config.json";
|
|
3
|
+
export interface PathContext {
|
|
4
|
+
readonly env: NodeJS.ProcessEnv;
|
|
5
|
+
readonly platform: NodeJS.Platform;
|
|
6
|
+
readonly home: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function currentPathContext(): PathContext;
|
|
9
|
+
/** The folder of the configuration file. Windows uses `%APPDATA%`, other systems use XDG rules. */
|
|
10
|
+
export declare function configDirPath(context?: PathContext): string;
|
|
11
|
+
export declare function configFilePath(context?: PathContext): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
export const CONFIG_DIR_NAME = "seo-webmaster-mcp";
|
|
4
|
+
export const CONFIG_FILE_NAME = "config.json";
|
|
5
|
+
export function currentPathContext() {
|
|
6
|
+
return { env: process.env, platform: process.platform, home: homedir() };
|
|
7
|
+
}
|
|
8
|
+
/** The folder of the configuration file. Windows uses `%APPDATA%`, other systems use XDG rules. */
|
|
9
|
+
export function configDirPath(context = currentPathContext()) {
|
|
10
|
+
const { env, platform, home } = context;
|
|
11
|
+
// `||`, not `??`: a client can pass an empty string for an unset variable.
|
|
12
|
+
if (platform === "win32") {
|
|
13
|
+
return join(env.APPDATA || join(home, "AppData", "Roaming"), CONFIG_DIR_NAME);
|
|
14
|
+
}
|
|
15
|
+
return join(env.XDG_CONFIG_HOME || join(home, ".config"), CONFIG_DIR_NAME);
|
|
16
|
+
}
|
|
17
|
+
export function configFilePath(context) {
|
|
18
|
+
return join(configDirPath(context), CONFIG_FILE_NAME);
|
|
19
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AnalyticsRow, AnalyticsSummary } from "./types.js";
|
|
2
|
+
/** Divides safely: returns 0 when the denominator is 0. */
|
|
3
|
+
export declare function ratio(numerator: number, denominator: number): number;
|
|
4
|
+
/** Totals the rows. The overall position is weighted by impressions. */
|
|
5
|
+
export declare function summarizeRows(rows: readonly AnalyticsRow[]): AnalyticsSummary;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Divides safely: returns 0 when the denominator is 0. */
|
|
2
|
+
export function ratio(numerator, denominator) {
|
|
3
|
+
return denominator > 0 ? numerator / denominator : 0;
|
|
4
|
+
}
|
|
5
|
+
/** Totals the rows. The overall position is weighted by impressions. */
|
|
6
|
+
export function summarizeRows(rows) {
|
|
7
|
+
const totals = rows.reduce((acc, row) => ({
|
|
8
|
+
clicks: acc.clicks + row.clicks,
|
|
9
|
+
impressions: acc.impressions + row.impressions,
|
|
10
|
+
weightedPosition: acc.weightedPosition + row.position * row.impressions,
|
|
11
|
+
}), { clicks: 0, impressions: 0, weightedPosition: 0 });
|
|
12
|
+
return {
|
|
13
|
+
totalClicks: totals.clicks,
|
|
14
|
+
totalImpressions: totals.impressions,
|
|
15
|
+
overallCtr: `${(ratio(totals.clicks, totals.impressions) * 100).toFixed(2)}%`,
|
|
16
|
+
overallPosition: ratio(totals.weightedPosition, totals.impressions).toFixed(1),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const SERVER_NAME = "seo-webmaster-mcp";
|
|
2
|
+
export declare const SERVER_VERSION = "1.1.0";
|
|
3
|
+
export declare const USER_AGENT = "seo-webmaster-mcp/1.1.0";
|
|
4
|
+
export declare const FETCH_TIMEOUT_MS = 15000;
|
|
5
|
+
export declare const MAX_ERROR_BODY_CHARS = 500;
|
|
6
|
+
export declare const MAX_REGEX_LENGTH = 200;
|
|
7
|
+
export declare const BING_REQUESTS_PER_SECOND = 5;
|
|
8
|
+
export declare const MAX_INDEXNOW_URLS = 10000;
|
|
9
|
+
export declare const MAX_GOOGLE_INDEXING_URLS = 200;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const SERVER_NAME = "seo-webmaster-mcp";
|
|
2
|
+
export const SERVER_VERSION = "1.1.0";
|
|
3
|
+
export const USER_AGENT = `${SERVER_NAME}/${SERVER_VERSION}`;
|
|
4
|
+
export const FETCH_TIMEOUT_MS = 15_000;
|
|
5
|
+
export const MAX_ERROR_BODY_CHARS = 500;
|
|
6
|
+
export const MAX_REGEX_LENGTH = 200;
|
|
7
|
+
export const BING_REQUESTS_PER_SECOND = 5;
|
|
8
|
+
export const MAX_INDEXNOW_URLS = 10_000;
|
|
9
|
+
export const MAX_GOOGLE_INDEXING_URLS = 200;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** An error that carries the HTTP status of a failed response. */
|
|
2
|
+
export declare class HttpError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
constructor(message: string, status: number, options?: ErrorOptions);
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The user did not approve a call, or the approval failed. The message is the full text of the
|
|
8
|
+
* tool result, so the error boundary adds no prefix.
|
|
9
|
+
*/
|
|
10
|
+
export declare class ApprovalError extends Error {
|
|
11
|
+
constructor(message: string);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Finds the HTTP status code of an error. It reads `code`, `status`, and `statusCode`, then the
|
|
15
|
+
* Axios/Gaxios `response`, then the `cause` chain.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getErrorCode(err: unknown): number | undefined;
|
|
18
|
+
export declare function getErrorMessage(err: unknown): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { isRecord } from "./guards.js";
|
|
2
|
+
const HTTP_STATUS_RE = /^\d{3}$/;
|
|
3
|
+
/** An error that carries the HTTP status of a failed response. */
|
|
4
|
+
export class HttpError extends Error {
|
|
5
|
+
status;
|
|
6
|
+
constructor(message, status, options) {
|
|
7
|
+
super(message, options);
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.name = "HttpError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The user did not approve a call, or the approval failed. The message is the full text of the
|
|
14
|
+
* tool result, so the error boundary adds no prefix.
|
|
15
|
+
*/
|
|
16
|
+
export class ApprovalError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "ApprovalError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function toStatus(value) {
|
|
23
|
+
if (typeof value === "number" && Number.isInteger(value)) {
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
if (typeof value === "string" && HTTP_STATUS_RE.test(value.trim())) {
|
|
27
|
+
return Number(value.trim());
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Finds the HTTP status code of an error. It reads `code`, `status`, and `statusCode`, then the
|
|
33
|
+
* Axios/Gaxios `response`, then the `cause` chain.
|
|
34
|
+
*/
|
|
35
|
+
export function getErrorCode(err) {
|
|
36
|
+
if (!isRecord(err)) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
const response = isRecord(err.response) ? err.response : {};
|
|
40
|
+
const data = isRecord(response.data) ? response.data : {};
|
|
41
|
+
const apiError = isRecord(data.error) ? data.error : {};
|
|
42
|
+
const candidates = [
|
|
43
|
+
err.code,
|
|
44
|
+
err.status,
|
|
45
|
+
err.statusCode,
|
|
46
|
+
response.status,
|
|
47
|
+
apiError.code,
|
|
48
|
+
];
|
|
49
|
+
return candidates.map(toStatus).find((code) => code !== undefined) ?? getErrorCode(err.cause);
|
|
50
|
+
}
|
|
51
|
+
export function getErrorMessage(err) {
|
|
52
|
+
return err instanceof Error ? err.message : String(err);
|
|
53
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type FilterMode = "contains" | "equals" | "regex";
|
|
2
|
+
/** A parsed text filter. The same grammar applies to Google (server side) and Bing (client side). */
|
|
3
|
+
export interface TextFilter {
|
|
4
|
+
readonly mode: FilterMode;
|
|
5
|
+
readonly negate: boolean;
|
|
6
|
+
readonly expression: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Parses the filter grammar: plain text (contains), `!text` (does not contain), `exact:` / `!exact:`
|
|
10
|
+
* or `!=` (equals / does not equal), and `regex:` / `!regex:` (RE2 pattern).
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseTextFilter(raw: string, label: string): TextFilter;
|
|
13
|
+
/** Builds a case-insensitive predicate for client-side filtering. */
|
|
14
|
+
export declare function createTextMatcher(filter: TextFilter): (text: string) => boolean;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { buildSafeRegExp } from "./validation.js";
|
|
2
|
+
// Order matters: a longer prefix must come before a shorter prefix that it starts with.
|
|
3
|
+
const FILTER_PREFIXES = [
|
|
4
|
+
{ prefix: "!regex:", mode: "regex", negate: true },
|
|
5
|
+
{ prefix: "regex:", mode: "regex", negate: false },
|
|
6
|
+
{ prefix: "!exact:", mode: "equals", negate: true },
|
|
7
|
+
{ prefix: "!=", mode: "equals", negate: true },
|
|
8
|
+
{ prefix: "exact:", mode: "equals", negate: false },
|
|
9
|
+
{ prefix: "!", mode: "contains", negate: true },
|
|
10
|
+
];
|
|
11
|
+
const CONTAINS = { prefix: "", mode: "contains", negate: false };
|
|
12
|
+
/**
|
|
13
|
+
* Parses the filter grammar: plain text (contains), `!text` (does not contain), `exact:` / `!exact:`
|
|
14
|
+
* or `!=` (equals / does not equal), and `regex:` / `!regex:` (RE2 pattern).
|
|
15
|
+
*/
|
|
16
|
+
export function parseTextFilter(raw, label) {
|
|
17
|
+
const trimmed = raw.trim();
|
|
18
|
+
const { prefix, mode, negate } = FILTER_PREFIXES.find((candidate) => trimmed.startsWith(candidate.prefix)) ?? CONTAINS;
|
|
19
|
+
const expression = trimmed.slice(prefix.length).trim();
|
|
20
|
+
if (expression.length === 0) {
|
|
21
|
+
throw new Error(`Invalid ${label}: "${prefix || "filter"}" needs a non-empty value.`);
|
|
22
|
+
}
|
|
23
|
+
if (mode === "regex") {
|
|
24
|
+
buildSafeRegExp(expression, label);
|
|
25
|
+
}
|
|
26
|
+
return { mode, negate, expression };
|
|
27
|
+
}
|
|
28
|
+
function buildMatcher({ mode, expression }) {
|
|
29
|
+
if (mode === "regex") {
|
|
30
|
+
const regex = buildSafeRegExp(expression, "filter");
|
|
31
|
+
return (text) => regex.test(text);
|
|
32
|
+
}
|
|
33
|
+
const needle = expression.toLowerCase();
|
|
34
|
+
return mode === "equals"
|
|
35
|
+
? (text) => text.toLowerCase() === needle
|
|
36
|
+
: (text) => text.toLowerCase().includes(needle);
|
|
37
|
+
}
|
|
38
|
+
/** Builds a case-insensitive predicate for client-side filtering. */
|
|
39
|
+
export function createTextMatcher(filter) {
|
|
40
|
+
const matches = buildMatcher(filter);
|
|
41
|
+
return filter.negate ? (text) => !matches(text) : matches;
|
|
42
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
2
|
+
export declare function asRecord(value: unknown, label: string): Record<string, unknown>;
|
|
3
|
+
/** Returns the object entries of an array, or an empty list when the value is not an array. */
|
|
4
|
+
export declare function asRecordArray(value: unknown): Record<string, unknown>[];
|
|
5
|
+
export declare function asFiniteNumber(value: unknown, fallback: number): number;
|
|
6
|
+
export declare function asOptionalNumber(value: unknown): number | undefined;
|
|
7
|
+
export declare function asString(value: unknown, fallback: string): string;
|
|
8
|
+
export declare function asOptionalString(value: unknown): string | undefined;
|
|
9
|
+
export declare function isPresent<T>(value: T | null | undefined): value is T;
|
|
10
|
+
/** Removes `undefined`, `null`, `false`, and `""` from a list of optional items. */
|
|
11
|
+
export declare function compact<T>(items: ReadonlyArray<T | null | undefined | false | "">): T[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function isRecord(value) {
|
|
2
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
|
+
}
|
|
4
|
+
export function asRecord(value, label) {
|
|
5
|
+
if (isRecord(value)) {
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
throw new Error(`Invalid ${label}: expected an object.`);
|
|
9
|
+
}
|
|
10
|
+
/** Returns the object entries of an array, or an empty list when the value is not an array. */
|
|
11
|
+
export function asRecordArray(value) {
|
|
12
|
+
return Array.isArray(value) ? value.filter(isRecord) : [];
|
|
13
|
+
}
|
|
14
|
+
export function asFiniteNumber(value, fallback) {
|
|
15
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
16
|
+
}
|
|
17
|
+
export function asOptionalNumber(value) {
|
|
18
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
19
|
+
}
|
|
20
|
+
export function asString(value, fallback) {
|
|
21
|
+
return typeof value === "string" ? value : fallback;
|
|
22
|
+
}
|
|
23
|
+
export function asOptionalString(value) {
|
|
24
|
+
return typeof value === "string" ? value : undefined;
|
|
25
|
+
}
|
|
26
|
+
export function isPresent(value) {
|
|
27
|
+
return value !== null && value !== undefined;
|
|
28
|
+
}
|
|
29
|
+
/** Removes `undefined`, `null`, `false`, and `""` from a list of optional items. */
|
|
30
|
+
export function compact(items) {
|
|
31
|
+
return items.filter((item) => Boolean(item));
|
|
32
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface FetchOptions {
|
|
2
|
+
readonly method?: "GET" | "POST";
|
|
3
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
4
|
+
readonly body?: string;
|
|
5
|
+
readonly timeoutMs?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function truncateBody(body: string, maxChars?: number): string;
|
|
8
|
+
export declare function readBodyText(res: Response): Promise<string>;
|
|
9
|
+
/** Reads a JSON body. Throws a clear error when the server sent HTML or broken JSON. */
|
|
10
|
+
export declare function readJsonSafe(res: Response): Promise<unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* Sends a request with a timeout. A network failure reports only the host, so a secret in the
|
|
13
|
+
* query string (for example `apikey`) never reaches an error message.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fetchWithTimeout(url: URL, options?: FetchOptions): Promise<Response>;
|
|
16
|
+
/** Throws an `HttpError` with the status and a truncated body when the response is not 2xx. */
|
|
17
|
+
export declare function ensureOk(res: Response, label: string): Promise<Response>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { FETCH_TIMEOUT_MS, MAX_ERROR_BODY_CHARS } from "./constants.js";
|
|
2
|
+
import { HttpError, getErrorMessage } from "./errors.js";
|
|
3
|
+
export function truncateBody(body, maxChars = MAX_ERROR_BODY_CHARS) {
|
|
4
|
+
const trimmed = body.trim();
|
|
5
|
+
return trimmed.length <= maxChars ? trimmed : `${trimmed.slice(0, maxChars)}…(truncated)`;
|
|
6
|
+
}
|
|
7
|
+
export async function readBodyText(res) {
|
|
8
|
+
try {
|
|
9
|
+
return truncateBody(await res.text());
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
// The body stream failed or was already read. The status text is the best detail left.
|
|
13
|
+
return res.statusText;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Reads a JSON body. Throws a clear error when the server sent HTML or broken JSON. */
|
|
17
|
+
export async function readJsonSafe(res) {
|
|
18
|
+
const text = await res.text();
|
|
19
|
+
try {
|
|
20
|
+
const json = JSON.parse(text);
|
|
21
|
+
return json;
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
throw new Error(`Invalid JSON response (status ${res.status}): ${getErrorMessage(err)}. Body: ${truncateBody(text)}`, { cause: err });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Sends a request with a timeout. A network failure reports only the host, so a secret in the
|
|
29
|
+
* query string (for example `apikey`) never reaches an error message.
|
|
30
|
+
*/
|
|
31
|
+
export async function fetchWithTimeout(url, options = {}) {
|
|
32
|
+
try {
|
|
33
|
+
return await fetch(url, {
|
|
34
|
+
method: options.method ?? "GET",
|
|
35
|
+
headers: options.headers,
|
|
36
|
+
body: options.body,
|
|
37
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? FETCH_TIMEOUT_MS),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
throw new Error(`Request to ${url.host} failed: ${getErrorMessage(err)}`, { cause: err });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Throws an `HttpError` with the status and a truncated body when the response is not 2xx. */
|
|
45
|
+
export async function ensureOk(res, label) {
|
|
46
|
+
if (res.ok) {
|
|
47
|
+
return res;
|
|
48
|
+
}
|
|
49
|
+
const body = await readBodyText(res);
|
|
50
|
+
throw new HttpError(`${label} error (${res.status}): ${body || res.statusText}`, res.status);
|
|
51
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { MetricRow, Metrics } from "./types.js";
|
|
2
|
+
export interface Change {
|
|
3
|
+
readonly clicks: number;
|
|
4
|
+
readonly impressions: number;
|
|
5
|
+
readonly ctr: number | null;
|
|
6
|
+
readonly position: number | null;
|
|
7
|
+
}
|
|
8
|
+
export interface ChangedRow {
|
|
9
|
+
readonly key: string;
|
|
10
|
+
readonly current: Metrics;
|
|
11
|
+
readonly previous: Metrics;
|
|
12
|
+
readonly change: Change;
|
|
13
|
+
}
|
|
14
|
+
export interface OneSidedRow {
|
|
15
|
+
readonly key: string;
|
|
16
|
+
readonly current: Metrics | null;
|
|
17
|
+
readonly previous: Metrics | null;
|
|
18
|
+
}
|
|
19
|
+
export interface Comparison {
|
|
20
|
+
readonly changed: readonly ChangedRow[];
|
|
21
|
+
readonly lost: readonly OneSidedRow[];
|
|
22
|
+
readonly added: readonly OneSidedRow[];
|
|
23
|
+
}
|
|
24
|
+
/** `comparable` is false when CTR and position would mislead, so their change stays `null`. */
|
|
25
|
+
export declare function toChange(current: Metrics, previous: Metrics, comparable: boolean): Change;
|
|
26
|
+
/** Splits the rows into rows of both periods, `lost` rows and new rows. No value is invented. */
|
|
27
|
+
export declare function compareRows(current: readonly MetricRow[], previous: readonly MetricRow[], weeks: number | null): Comparison;
|
|
28
|
+
export interface Declines {
|
|
29
|
+
readonly declined: readonly ChangedRow[];
|
|
30
|
+
readonly lost: readonly OneSidedRow[];
|
|
31
|
+
}
|
|
32
|
+
export declare function findDeclines({ changed, lost }: Comparison): Declines;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const MIN_PREVIOUS_CLICKS = 10;
|
|
2
|
+
const MIN_LOST_CLICKS = 5;
|
|
3
|
+
const MIN_LOSS_RATIO = 0.2;
|
|
4
|
+
/** `comparable` is false when CTR and position would mislead, so their change stays `null`. */
|
|
5
|
+
export function toChange(current, previous, comparable) {
|
|
6
|
+
const hasPositions = current.position !== null && previous.position !== null;
|
|
7
|
+
return {
|
|
8
|
+
clicks: current.clicks - previous.clicks,
|
|
9
|
+
impressions: current.impressions - previous.impressions,
|
|
10
|
+
ctr: comparable ? current.ctr - previous.ctr : null,
|
|
11
|
+
position: comparable && hasPositions ? Number(current.position) - Number(previous.position) : null,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function hasEveryWeek(row, weeks) {
|
|
15
|
+
return weeks === null || row.weeks === weeks;
|
|
16
|
+
}
|
|
17
|
+
function byClicks(a, b) {
|
|
18
|
+
return b.metrics.clicks - a.metrics.clicks;
|
|
19
|
+
}
|
|
20
|
+
/** Splits the rows into rows of both periods, `lost` rows and new rows. No value is invented. */
|
|
21
|
+
export function compareRows(current, previous, weeks) {
|
|
22
|
+
const before = new Map(previous.map((row) => [row.key, row]));
|
|
23
|
+
const now = new Set(current.map((row) => row.key));
|
|
24
|
+
const changed = current.flatMap((row) => {
|
|
25
|
+
const old = before.get(row.key);
|
|
26
|
+
if (!old) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
const comparable = hasEveryWeek(row, weeks) && hasEveryWeek(old, weeks);
|
|
30
|
+
const change = toChange(row.metrics, old.metrics, comparable);
|
|
31
|
+
return [{ key: row.key, current: row.metrics, previous: old.metrics, change }];
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
changed: changed.toSorted((a, b) => Math.abs(b.change.clicks) - Math.abs(a.change.clicks)),
|
|
35
|
+
lost: previous
|
|
36
|
+
.filter((row) => !now.has(row.key))
|
|
37
|
+
.toSorted(byClicks)
|
|
38
|
+
.map((row) => ({ key: row.key, current: null, previous: row.metrics })),
|
|
39
|
+
added: current
|
|
40
|
+
.filter((row) => !before.has(row.key))
|
|
41
|
+
.toSorted(byClicks)
|
|
42
|
+
.map((row) => ({ key: row.key, current: row.metrics, previous: null })),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function isDecline({ previous, change }) {
|
|
46
|
+
const loss = -change.clicks;
|
|
47
|
+
return (previous.clicks >= MIN_PREVIOUS_CLICKS &&
|
|
48
|
+
loss >= MIN_LOST_CLICKS &&
|
|
49
|
+
loss / previous.clicks >= MIN_LOSS_RATIO);
|
|
50
|
+
}
|
|
51
|
+
export function findDeclines({ changed, lost }) {
|
|
52
|
+
return {
|
|
53
|
+
declined: changed.filter(isDecline).toSorted((a, b) => a.change.clicks - b.change.clicks),
|
|
54
|
+
lost: lost.filter((row) => (row.previous?.clicks ?? 0) >= MIN_PREVIOUS_CLICKS),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PeriodPair } from "./types.js";
|
|
2
|
+
type CrawlRecord = Readonly<Record<string, unknown>>;
|
|
3
|
+
export declare function newestDay(records: readonly CrawlRecord[]): string | undefined;
|
|
4
|
+
export declare function oldestDay(records: readonly CrawlRecord[]): string | undefined;
|
|
5
|
+
/** Totals of both periods and the newest index values, which are never added up. */
|
|
6
|
+
export declare function summarizeCrawl(records: readonly CrawlRecord[], periods: PeriodPair): Record<string, unknown>;
|
|
7
|
+
/** For each issue name: the number of URLs and the URLs with the most inbound links. */
|
|
8
|
+
export declare function groupIssues(records: readonly CrawlRecord[]): Record<string, unknown>[];
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { asFiniteNumber, asString } from "../guards.js";
|
|
2
|
+
import { isInPeriod } from "./periods.js";
|
|
3
|
+
const TOTAL_FIELDS = [
|
|
4
|
+
"crawledPages",
|
|
5
|
+
"crawlErrors",
|
|
6
|
+
"code2xx",
|
|
7
|
+
"code301",
|
|
8
|
+
"code302",
|
|
9
|
+
"code4xx",
|
|
10
|
+
"code5xx",
|
|
11
|
+
"blockedByRobotsTxt",
|
|
12
|
+
"containsMalware",
|
|
13
|
+
];
|
|
14
|
+
const EXAMPLES_PER_ISSUE = 3;
|
|
15
|
+
function dayOf(record) {
|
|
16
|
+
return asString(record.date, "").slice(0, 10);
|
|
17
|
+
}
|
|
18
|
+
export function newestDay(records) {
|
|
19
|
+
return records.map(dayOf).filter(Boolean).toSorted().at(-1);
|
|
20
|
+
}
|
|
21
|
+
export function oldestDay(records) {
|
|
22
|
+
return records.map(dayOf).filter(Boolean).toSorted()[0];
|
|
23
|
+
}
|
|
24
|
+
function totals(records, period) {
|
|
25
|
+
const inside = records.filter((record) => isInPeriod(dayOf(record), period));
|
|
26
|
+
return Object.fromEntries(TOTAL_FIELDS.map((field) => [
|
|
27
|
+
field,
|
|
28
|
+
inside.reduce((sum, record) => sum + asFiniteNumber(record[field], 0), 0),
|
|
29
|
+
]));
|
|
30
|
+
}
|
|
31
|
+
/** Totals of both periods and the newest index values, which are never added up. */
|
|
32
|
+
export function summarizeCrawl(records, periods) {
|
|
33
|
+
const current = totals(records, periods.current);
|
|
34
|
+
const previous = totals(records, periods.previous);
|
|
35
|
+
const latest = records.find((record) => dayOf(record) === periods.current.endDate);
|
|
36
|
+
return {
|
|
37
|
+
current,
|
|
38
|
+
previous,
|
|
39
|
+
change: Object.fromEntries(TOTAL_FIELDS.map((field) => [field, (current[field] ?? 0) - (previous[field] ?? 0)])),
|
|
40
|
+
inIndex: latest?.inIndex ?? null,
|
|
41
|
+
inLinks: latest?.inLinks ?? null,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** For each issue name: the number of URLs and the URLs with the most inbound links. */
|
|
45
|
+
export function groupIssues(records) {
|
|
46
|
+
const pairs = records.flatMap((record) => (Array.isArray(record.issues) ? record.issues : []).map((issue) => ({
|
|
47
|
+
issue: String(issue),
|
|
48
|
+
url: asString(record.url, ""),
|
|
49
|
+
inLinks: asFiniteNumber(record.inLinks, 0),
|
|
50
|
+
})));
|
|
51
|
+
return [...Map.groupBy(pairs, (pair) => pair.issue)]
|
|
52
|
+
.map(([issue, group]) => ({
|
|
53
|
+
issue,
|
|
54
|
+
urlCount: group.length,
|
|
55
|
+
examples: group
|
|
56
|
+
.toSorted((a, b) => b.inLinks - a.inLinks)
|
|
57
|
+
.slice(0, EXAMPLES_PER_ISSUE)
|
|
58
|
+
.map((pair) => pair.url),
|
|
59
|
+
}))
|
|
60
|
+
.toSorted((a, b) => b.urlCount - a.urlCount);
|
|
61
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
type LinkRecord = Readonly<Record<string, unknown>>;
|
|
2
|
+
export declare function groupByDomain(details: readonly LinkRecord[]): Record<string, string | number>[];
|
|
3
|
+
export declare function groupByAnchor(details: readonly LinkRecord[]): Record<string, string | number>[];
|
|
4
|
+
export declare function rankByLinkCount(links: readonly LinkRecord[]): LinkRecord[];
|
|
5
|
+
export {};
|