web-discovery-kit

    Offline RFC 9309 robots policy and Sitemap protocol toolkit with streaming XML and consistency audits.

    robots
    sitemap
    rfc9309
    seo
    moonbit
    Download zip
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    3 months ago
    Downloads
    40

    #Web Discovery Kit

    Web Discovery Kit 是一个离线 MoonBit 库和 native CLI:它按照 RFC 9309 解析并判定 robots.txt,读取、验证、序列化和分片 Sitemap,并报告两份配置之间可复现的结构性冲突。 它不会联网、不会抓取网页,也不会对收录或 SEO 效果作出结论。

    #它解决什么问题

    这是一个给网站运维、爬虫开发者和 CI 使用的离线网站发现配置检查工具。在网站上线、 迁移或批量生成 Sitemap 前,可以用它确认:目标爬虫是否会被 robots.txt 拒绝、Sitemap 是否符合协议、以及 Sitemap 中声明的页面是否与爬取规则冲突。

    它不是爬虫:不会请求 URL、不会下载 robots.txt 或 Sitemap,也不会替你判断搜索引擎是否 收录网站。它只检查你提供的本地文本、文件或标准输入,因此可安全地放进 CI。

    #适用场景

    • 上线前检查:验证 robots.txt 是否意外禁止了业务页面或指定爬虫。
    • Sitemap 生成流水线:校验 XML、发现重复 URL,并按 URL 数或文件大小稳定拆分大文件。
    • 配置变更审计:联合检查 robots 与 Sitemap,识别被禁止、跨域或重复的页面声明。
    • CI / 质量平台:输出 JSON、NDJSON 或 SARIF,便于机器读取、告警与留档。

    典型流程是:生成或导出 robots.txt、sitemap.xml → 运行 wadis audit → 发现问题后修正 配置 → 再将通过的文件发布到网站。整个流程无需访问生产站点。

    #能做什么

    • robots:User-agent / Allow / Disallow groups,product-token 选择、最长 octet 匹配、*、末尾 $、percent-escape 和 winning-rule provenance;Sitemap 与 Crawl-delay 等非核心 record 会保留,但不参与 RFC 判定。还可离线诊断无效/ 冲突规则并提取可用 Sitemap 提示。
    • sitemap:受限且 fail-closed 的 XML tokenizer、Sitemap 0.9 namespace 的 urlset / sitemapindex、增量 events、字段/host/大小限制、稳定序列化与 URL-set 分片。构造的文档可不经 XML round-trip 校验和统计,也可按 entry 数和精确 UTF-8 序列化字节数确定性分片。输入必须是已解压的 UTF-8 XML;不支持 gzip、RSS、Atom 或通用 XML。
    • audit:离线报告 robots 禁止、跨 origin、重复 location 和文档条目数问题;稳定 text/JSON/NDJSON/SARIF renderer 和汇总适合 CI 消费。

    #安装

    在你的 MoonBit 项目根目录执行:

    moon add yeshenyue2017/web-discovery-kit@0.1.1

    随后在 moon.pkg 中导入根包,并使用 README 的 Library API 示例调用 parse_robots、 parse_sitemap、decide 或 audit_report。

    #从源码验证

    需要 MoonBit 0.10.3 或更新版本;CLI 的标准输入和原子文件输出使用 moonbitlang/async,但 library core 保持 all-target。

    moon fmt --check moon check --target all --deny-warn moon test --target all --warn-list -68-1+73 moon build src/cmd/wadis --target native --deny-warn bash scripts/run_e2e.sh

    #CLI 示例

    所有输入都是本地文件或一次标准输入 -,从不把 URL 当作联网请求。诊断写入 stderr;正常结果写入 stdout。

    moon run src/cmd/wadis --target native -- \ robots check --user-agent MoonBot --url /admin tests/fixtures/robots-demo.txt # stdout: disallow; exit 3 moon run src/cmd/wadis --target native -- \ sitemap validate tests/fixtures/sitemap-demo.xml moon run src/cmd/wadis --target native -- \ robots inspect tests/fixtures/robots-demo.txt moon run src/cmd/wadis --target native -- \ sitemap inspect tests/fixtures/sitemap-demo.xml moon run src/cmd/wadis --target native -- \ audit --user-agent MoonBot --origin https://example.test --json \ tests/fixtures/robots-demo.txt tests/fixtures/sitemap-demo.xml moon run src/cmd/wadis --target native -- \ sitemap split --max-urls 2 --out /tmp/wadis-part tests/fixtures/sitemap-demo.xml moon run src/cmd/wadis --target native -- \ sitemap partition --max-entries 2 --max-bytes 10000 --out /tmp/wadis-packed \ tests/fixtures/sitemap-demo.xml moon run src/cmd/wadis --target native -- \ audit --user-agent MoonBot --origin https://example.test --report-json \ tests/fixtures/robots-demo.txt tests/fixtures/sitemap-demo.xml moon run src/cmd/wadis --target native -- \ audit --user-agent MoonBot --origin https://example.test --sarif \ tests/fixtures/robots-demo.txt tests/fixtures/sitemap-demo.xml printf 'User-agent: TestBot\nAllow: /\n' | \ moon run src/cmd/wadis --target native -- robots parse -

    sitemap split 与 sitemap partition 先把每个完整 XML 写到同目录临时文件,再 rename 到目标文件;单个目标文件不会暴露半写入内容。partition 同时满足 entry 与 canonical XML 字节上限,若单项无法放入上限会失败。audit 最多允许一个输入为 -,避免同一 stdin 被消费两次。

    Exit含义
    0命令成功;audit 仅有 warning/information 或没有 finding
    1文件读取、UTF-8 或协议输入失败
    2参数、命令或调用者提供的 origin/split limit 无效
    3robots check 的 URI 被允许策略拒绝
    4audit 产生 error severity finding

    #Library API

    根包为 yeshenyue2017/web-discovery-kit,不泄漏 internal XML token。以下是只使用 facade 的最小闭环(错误会以 DiscoveryError 传播):

    // moon.pkg
    import { "yeshenyue2017/web-discovery-kit" @wadis }

    // main.mbt

    fn check_site(robots_text : String, sitemap_xml : String) raise @wadis.DiscoveryError {
    let robots = @wadis.parse_robots(robots_text)
    let sitemap = @wadis.parse_sitemap(sitemap_xml)
    let decision = @wadis.decide(robots, "MoonBot", "/admin")
    let report = @wadis.audit_report(robots, sitemap, "MoonBot", "https://example.test")
    let inspection = @wadis.inspect_sitemap(sitemap)
    println("allowed=\{decision.allowed}; findings=\{report.findings.length()}; entries=\{inspection.entry_count}")
    }

    SitemapDecoder 及其 event API 位于公共 sitemap package,适合持续向单个 decoder 推送已解压 XML chunk;同一个 decoder 不支持并发 push。

    #互操作与来源

    项目以 Apache-2.0 发布。fixtures 为本仓库原创,清单见 tests/fixtures/MANIFEST.md。RFC 9309 和 Sitemap 协议是 行为规范;Google robotstxt 作为固定版本的 Apache-2.0 oracle。脚本默认在被忽略的 .oracle-cache/ 中构建其官方 robots 二进制;需要 git、cmake、C++14 编译器和网络。 也可用环境变量复用已构建的官方二进制:

    GOOGLE_ROBOTSTXT_BIN=/absolute/path/to/official-robots \ bash scripts/run_google_robotstxt_diff.sh

    预置工具链或网络不可用时脚本会失败,不再把跳过误报为通过。

    AuditFinding

    Public discovery models and errors.

    AuditReport

    Structured audit results owned by the offline audit package.

    AuditSummary

    Structured audit results owned by the offline audit package.

    DiscoveryError

    Public discovery models and errors.

    FindingSeverity

    Public discovery models and errors.

    RobotsDecision

    Public discovery models and errors.

    RobotsDiagnostic

    Non-fatal policy inspection types owned by the robots package.

    RobotsDiagnosticCode

    Non-fatal policy inspection types owned by the robots package.

    RobotsDocument

    Public discovery models and errors.

    RobotsGroup

    Public discovery models and errors.

    RobotsPolicyInspection

    Non-fatal policy inspection types owned by the robots package.

    RobotsRule

    Public discovery models and errors.

    SitemapDocument

    Public discovery models and errors.

    SitemapEvent

    Public discovery models and errors.

    SitemapInspection

    Protocol inspection and byte-aware partitioning types owned by sitemap.

    SitemapKind

    Protocol inspection and byte-aware partitioning types owned by sitemap.

    SitemapPartitionLimits

    Protocol inspection and byte-aware partitioning types owned by sitemap.

    SitemapRef

    Public discovery models and errors.

    SitemapShard

    Protocol inspection and byte-aware partitioning types owned by sitemap.

    SitemapUrl

    Public discovery models and errors.

    audit_report

    Run the enhanced offline audit with bounded retained findings and summary counts for dashboards and CI.

    inspect_robots

    Inspect a robots policy for non-fatal quality issues and Sitemap hints.

    normalize_uri_for_match

    fn normalize_uri_for_match(uri : String) -> String raise
    DiscoveryError

    Normalize an origin-form or absolute URI for RFC 9309 rule matching.

    parse_robots

    Parse robots.txt through the stable facade.

    parse_robots_bytes

    Decode UTF-8 bytes and parse robots.txt without converting malformed input into a normal MoonBit String first.

    parse_sitemap

    Parse a complete Sitemap or Sitemap index.

    render_audit_report

    fn render_audit_report(report :
    AuditReport
    ) -> String

    Render an enhanced report with a human-readable summary header.

    render_audit_report_json

    fn render_audit_report_json(report :
    AuditReport
    ) -> String

    Render an enhanced report as one stable JSON object.

    render_audit_report_sarif

    fn render_audit_report_sarif(report :
    AuditReport
    ) -> String

    Render an audit report as SARIF 2.1.0 for generic code-scanning viewers.

    render_findings

    Render stable human-readable audit findings.

    render_findings_json

    fn render_findings_json(findings : ArrayView[
    AuditFinding
    ]) -> String

    Render stable JSON audit findings without requiring a JSON dependency.

    render_findings_ndjson

    fn render_findings_ndjson(findings : ArrayView[
    AuditFinding
    ]) -> String

    Render each finding as a separate JSON line for append-only tooling.

    robots_sitemap_urls

    Extract valid first-seen absolute HTTP(S) Sitemap URLs from robots records.

    serialize_sitemap

    Serialize a Sitemap document with its protocol namespace and escaped fields.

    summarize_findings

    Summarize arbitrary audit findings with stable severity and subject counts.

    validate_sitemap

    Validate a constructed Sitemap document without a serialize/parse round trip. This checks the protocol fields, shared host, entry, and byte limits.

    Source Files