44 lines
2.1 KiB
JavaScript
44 lines
2.1 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const { execFileSync } = require("node:child_process");
|
|
|
|
function parse(argv) {
|
|
const result = {};
|
|
for (let index = 0; index < argv.length; index += 1) {
|
|
const key = argv[index];
|
|
if (key === "--git-dir") result.gitDir = argv[++index];
|
|
else if (key === "--commit") result.commit = argv[++index];
|
|
else throw new Error(`unknown_argument:${key}`);
|
|
}
|
|
if (!result.gitDir || !/^[0-9a-f]{40}$/.test(result.commit || "")) throw new Error("git_dir_and_commit_required");
|
|
return result;
|
|
}
|
|
|
|
function readJson(gitDir, commit, file) {
|
|
return JSON.parse(execFileSync("git", [`--git-dir=${gitDir}`, "show", `${commit}:${file}`], { encoding: "utf8", maxBuffer: 4 * 1024 * 1024 }));
|
|
}
|
|
|
|
function validate({ gitDir, commit }) {
|
|
const anchorPath = "routing/public-navigation-anchor.json";
|
|
const anchor = readJson(gitDir, commit, anchorPath);
|
|
if (anchor.schema !== "guanghu.public-navigation-anchor/v1" || anchor.anchor_id !== "GLW-PUBLIC-NAV-ANCHOR-001") throw new Error("invalid_public_anchor");
|
|
const atomicKeys = ["repository", "nodes", "subjects", "aliases", "identity_authority", "navigation", "lighthouse_paths", "host_skills", "mother_brain"];
|
|
for (const key of atomicKeys) {
|
|
const declaration = anchor.maps?.[key];
|
|
if (!declaration || typeof declaration.path !== "string" || !/^(routing|identity)\/[A-Za-z0-9._/-]+\.json$/.test(declaration.path) || declaration.path.includes("..")) throw new Error(`invalid_snapshot_path:${key}`);
|
|
const map = readJson(gitDir, commit, declaration.path);
|
|
if (declaration.id && ![map.map_id, map.registry_id].includes(declaration.id)) throw new Error(`snapshot_map_id_mismatch:${key}`);
|
|
if (declaration.version && map.version !== declaration.version) throw new Error(`snapshot_map_version_mismatch:${key}`);
|
|
}
|
|
return { result: "PASS_100", commit, anchor_version: anchor.version };
|
|
}
|
|
|
|
try {
|
|
process.stdout.write(`${JSON.stringify(validate(parse(process.argv.slice(2))))}\n`);
|
|
} catch (error) {
|
|
process.stderr.write(`REPO012_SNAPSHOT_ADMISSION_FAIL_0:${String(error.message || error)}\n`);
|
|
process.exitCode = 1;
|
|
}
|
|
|
|
module.exports = { parse, validate };
|