#!/usr/bin/env node // Fetches the corpora in manifest.json into ../fixtures/fetched/, refusing any file whose sha256 differs from the // pinned one. Nothing fetched is ever committed (.gitignore). Usage: node fixtures-fetch/fetch.mjs import { createHash } from 'node:crypto'; import { mkdirSync, readFileSync, writeFileSync, existsSync, renameSync } from 'node:fs'; import { dirname, join, basename } from 'node:path'; import { fileURLToPath } from 'node:url'; const here = dirname(fileURLToPath(import.meta.url)); const out = join(here, '..', 'fixtures', 'fetched'); const manifest = JSON.parse(readFileSync(join(here, 'manifest.json'), 'utf8')); mkdirSync(out, { recursive: true }); let bad = 0; for (const f of manifest.files) { if (!/^[0-9a-f]{64}$/.test(f.sha256) || basename(f.name) !== f.name || !f.url.startsWith('https://')) { console.error(`skip ${f.name}: the manifest entry is not pinned (https URL, plain name, sha256)`); bad++; continue; } const target = join(out, f.name); if (existsSync(target) && createHash('sha256').update(readFileSync(target)).digest('hex') === f.sha256) { console.log(`ok ${f.name} (cached)`); continue; } const res = await fetch(f.url, { redirect: 'follow' }); if (!res.ok) { console.error(`fail ${f.name}: HTTP ${res.status}`); bad++; continue; } const buf = Buffer.from(await res.arrayBuffer()); const got = createHash('sha256').update(buf).digest('hex'); if (got !== f.sha256) { console.error(`fail ${f.name}: sha256 ${got} is not the pinned ${f.sha256}`); bad++; continue; } writeFileSync(target + '.part', buf); renameSync(target + '.part', target); console.log(`ok ${f.name}`); } console.log(`${manifest.files.length} pinned, ${bad} failed`); process.exit(bad ? 1 : 0);