/**
* `npm run build:index` — project the committed dataset down to the fields the
* catalog list needs and write it as a content-addressed file:
*
* public/data/index-<sha256[0:8]>.json fetched by the catalog island
* src/data/index-hash.json { file, hash, bytes, count } — imported
* by catalog.astro to point at the file
*
* The hashed name lets vercel.json serve it `immutable`; a data change makes a
* new name, so a stale cache is impossible. Runs at the head of `npm run build`
* and `npm run dev`; validate-data.mjs fails if index-hash.json is stale.
*/
import { readFileSync, writeFileSync, mkdirSync, readdirSync, unlinkSync, existsSync } from 'node:fs';
import { createHash } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
/** Mirror of LIST_FIELDS in src/lib/render-row.ts (test/build-index.test.mjs pins them). */
export const LIST_FIELDS = [
'id', 'toolId', 'toolName', 'name', 'source', 'platform', 'capability', 'nativeCategory',
'command', 'description', 'usecase', 'mitre', 'privilege', 'added',
'context', 'requires', 'services', 'environment', 'aliases', 'availability', 'verification',
'reviewedAt', 'compatibility', 'expected', 'troubleshooting', 'sideEffects', 'restore', 'template', 'references', 'detection',
];
export function project(t) {
const out = {};
for (const k of LIST_FIELDS) if (t[k] !== undefined) out[k] = t[k];
return out;
}
/** Deterministic: same techniques → same bytes → same hash. */
export function buildIndex(techniques) {
const list = techniques.map(project);
const json = JSON.stringify(list);
const hash = createHash('sha256').update(json).digest('hex').slice(0, 8);
return { json, hash, file: `index-${hash}.json`, bytes: Buffer.byteLength(json), count: list.length };
}
export function main() {
const techniques = JSON.parse(readFileSync(join(ROOT, 'src/data/techniques.json'), 'utf8'));
const idx = buildIndex(techniques);
const pub = join(ROOT, 'public', 'data');
mkdirSync(pub, { recursive: true });
for (const f of readdirSync(pub)) if (/^index-[0-9a-f]{8}\.json$/.test(f) && f !== idx.file) unlinkSync(join(pub, f));
writeFileSync(join(pub, idx.file), idx.json);
const meta = { file: idx.file, hash: idx.hash, bytes: idx.bytes, count: idx.count };
const metaPath = join(ROOT, 'src/data/index-hash.json');
const prev = existsSync(metaPath) ? readFileSync(metaPath, 'utf8') : '';
const next = JSON.stringify(meta, null, 2) + '\n';
if (prev !== next) writeFileSync(metaPath, next);
console.log(`build:index — public/data/${idx.file}: ${idx.count} rows, ${idx.bytes.toLocaleString('en-US')} B${prev !== next ? ' (index-hash.json updated)' : ''}`);
}
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) main();