// Publishes the exact source this deployment is built from, so anyone can // audit it and check it against the public repository. // Runs automatically before `npm run build` (the "prebuild" script) and writes: // public/source/SHA256SUMS standard `sha256sum` list of every file // public/source/manifest.json same data + archive hash, for the /source page // public/source/sanario-source.tar.gz the source archive (deterministic: sorted, // fixed mtime/owner, gzip without timestamp) // public/source/files/.txt each file as plain text, for reading in the browser import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; import { execFileSync } from 'node:child_process'; const root = path.resolve(path.dirname(new URL(import.meta.url).pathname), '..'); const outDir = path.join(root, 'public/source'); const SKIP_DIRS = new Set(['node_modules', '.next', '.git', '.wrangler', 'coverage']); const skipFile = (rel) => rel.startsWith('public/source/') || rel.endsWith('.tsbuildinfo') || rel.endsWith('.log') || (/(^|\/)\.env/.test(rel) && !rel.endsWith('.env.example')); function walk(dir, out = []) { for (const e of fs.readdirSync(dir, { withFileTypes: true })) { const abs = path.join(dir, e.name); const rel = path.relative(root, abs).split(path.sep).join('/'); if (e.isDirectory()) { if (!SKIP_DIRS.has(e.name) && rel !== 'public/source') walk(abs, out); } else if (e.isFile() && !skipFile(rel)) out.push(rel); } return out; } const sha256 = (buf) => crypto.createHash('sha256').update(buf).digest('hex'); const files = walk(root).sort(); fs.rmSync(outDir, { recursive: true, force: true }); fs.mkdirSync(path.join(outDir, 'files'), { recursive: true }); const entries = files.map((rel) => { const buf = fs.readFileSync(path.join(root, rel)); const text = !buf.includes(0); if (text) { const dest = path.join(outDir, 'files', rel + '.txt'); fs.mkdirSync(path.dirname(dest), { recursive: true }); fs.writeFileSync(dest, buf); } return { path: rel, sha256: sha256(buf), bytes: buf.length, text }; }); const sums = entries.map((e) => `${e.sha256} ${e.path}\n`).join(''); fs.writeFileSync(path.join(outDir, 'SHA256SUMS'), sums); const listFile = path.join(outDir, '.filelist'); fs.writeFileSync(listFile, files.join('\n') + '\n'); const tarPath = path.join(outDir, 'sanario-source.tar'); execFileSync('tar', [ '--sort=name', '--mtime=@0', '--owner=0', '--group=0', '--numeric-owner', '--mode=go-w', '--transform=s,^,sanario/,', '-cf', tarPath, '-C', root, '-T', listFile, ]); fs.rmSync(listFile); execFileSync('gzip', ['-n', '-9', '-f', tarPath]); const archive = fs.readFileSync(tarPath + '.gz'); const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')); const manifest = { name: pkg.name, version: pkg.version, license: pkg.license, repository: process.env.NEXT_PUBLIC_SOURCE_REPO || null, builtAt: new Date().toISOString(), sumsSha256: sha256(sums), archive: { file: 'sanario-source.tar.gz', sha256: sha256(archive), bytes: archive.length }, files: entries, }; fs.writeFileSync(path.join(outDir, 'manifest.json'), JSON.stringify(manifest, null, 2)); console.log(`source: ${entries.length} files, archive ${(archive.length / 1024).toFixed(0)} KiB, SHA256SUMS ${manifest.sumsSha256.slice(0, 16)}…`);