0
0
const [origin, dir] = Deno.args;
async function sha256(bytes: Uint8Array): Promise<string> {
const d = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
return [...d].map((b) => b.toString(16).padStart(2, "0")).join("");
}
async function* files(base: string, rel = ""): AsyncGenerator<string> {
for await (const e of Deno.readDir(`${base}/${rel}`)) {
const p = rel ? `${rel}/${e.name}` : e.name;
if (e.isDirectory) yield* files(base, p);
else yield p;
}
}
let bad = 0;
const paths = [];
for await (const p of files(dir)) paths.push(p);
paths.sort();
for (const p of paths) {
const url = new URL(p === "index.html" ? "" : p, origin).href;
const want = await sha256(await Deno.readFile(`${dir}/${p}`));
let got = "";
let status = 0;
try {
const r = await fetch(url, { headers: { "cache-control": "no-cache" } });
status = r.status;
got = await sha256(new Uint8Array(await r.arrayBuffer()));
} catch (e) {
got = String(e);
}
const ok = status === 200 && got === want;
if (!ok) bad++;
console.log(`${ok ? "OK " : "FAIL"} ${url} ${status} ${got === want ? want : `want ${want} got ${got}`}`);
}
if (bad) {
console.error(`verify: ${bad} of ${paths.length} differ`);
Deno.exit(1);
}
console.log(`verify: ${paths.length} of ${paths.length} match`);