#!/usr/bin/env node /** * D-R5 study (R5.1): the sheet fingerprint of every generated JSON artifact * under each candidate rule, from the committed bytes, read-only. * * node scripts/r5-fingerprints.mjs the markdown matrix * node scripts/r5-fingerprints.mjs --json the same as JSON * * Rules: today = sha256 of the file bytes (Prettier-formatted JSON written * by the TypeScript pipeline); A = sha256 of `JSON.stringify(v, null, 2)`, * which is what the core's `to_js_pretty` emits (option A of D-R5, bytes the * Rust pipeline would write), with and without a trailing newline, plus the * compact form; B = sha256 of the RFC 8785 canonical form (JCS). The RFC's * own vectors run first and stop the script if the canonicaliser fails them. */ import { createHash } from 'node:crypto' import { readFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..') const SEED = 'supabase/seed/anatomical-sheets/equine' const FILES = [ `${SEED}/legacy-import/document.json`, `${SEED}/legacy-import/legacy-id-map.json`, `${SEED}/legacy-import/assets.json`, `${SEED}/draft/document.json`, `${SEED}/draft/assets.json`, `${SEED}/draft/sheet-ref.json`, `${SEED}/draft/report-migration-map.json`, ] const sha256 = (text) => createHash('sha256').update(text).digest('hex') /** * RFC 8785: members sorted by the UTF-16 code units of their names (the * default `<` of JavaScript strings), no whitespace, numbers and strings as * `JSON.stringify` prints them (ES6 Number::toString, the JSON escape set). */ export function canonicalize(value) { if (value === null || typeof value !== 'object') return JSON.stringify(value) if (Array.isArray(value)) return `[${value.map(canonicalize).join(',')}]` const keys = Object.keys(value).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)) const members = keys.map( (k) => `${JSON.stringify(k)}:${canonicalize(value[k])}`, ) return `{${members.join(',')}}` } /** RFC 8785 §3.2.3 (numbers, string escapes) and the key-ordering example. */ const RFC_VECTORS = [ [ '{"numbers": [333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001], ' + '"string": "\\u20ac$\\u000F\\u000aA\'\\u0042\\u0022\\u005c\\\\\\"\\/", "literals": [null, true, false]}', '{"literals":[null,true,false],"numbers":[333333333.3333333,1e+30,4.5,0.002,1e-27],' + '"string":"€$\\u000f\\nA\'B\\"\\\\\\\\\\"/"}', ], [ '{"\\u20ac": "Euro Sign", "\\r": "Carriage Return", "\\ufb33": "Hebrew Letter Dalet With Dagesh", ' + '"1": "One", "\\ud83d\\ude00": "Emoji: Grinning Face", "\\u0080": "Control", ' + '"\\u00f6": "Latin Small Letter O With Diaeresis"}', '{"\\r":"Carriage Return","1":"One","\u0080":"Control","ö":"Latin Small Letter O With Diaeresis",' + '"€":"Euro Sign","😀":"Emoji: Grinning Face","דּ":"Hebrew Letter Dalet With Dagesh"}', ], ] function checkVectors() { for (const [input, expected] of RFC_VECTORS) { const got = canonicalize(JSON.parse(input)) if (got !== expected) { console.error( `RFC 8785 vector failed:\n got ${got}\n expected ${expected}`, ) process.exit(1) } } } function row(file) { const bytes = readFileSync(resolve(REPO_ROOT, file)) const value = JSON.parse(bytes.toString('utf8')) const pretty = JSON.stringify(value, null, 2) const canonical = canonicalize(value) return { file, bytes: bytes.length, today: sha256(bytes), a_pretty_newline: sha256(`${pretty}\n`), a_pretty: sha256(pretty), a_compact: sha256(JSON.stringify(value)), b_jcs: sha256(canonical), jcs_bytes: Buffer.byteLength(canonical), prettier_equals_stringify: bytes.toString('utf8') === `${pretty}\n`, } } checkVectors() const rows = FILES.map(row) if (process.argv.includes('--json')) { console.log(JSON.stringify(rows, null, 2)) } else { console.log('RFC 8785 vectors: pass\n') console.log( '| File | Bytes | Today (file bytes) | A `JSON.stringify(v, null, 2)` + LF | A without LF | A compact | B RFC 8785 | JCS bytes | Prettier = stringify |', ) console.log('|---|---|---|---|---|---|---|---|---|') for (const r of rows) { console.log( `| ${r.file.slice(SEED.length + 1)} | ${r.bytes} | ${r.today} | ${r.a_pretty_newline} | ${r.a_pretty} | ${r.a_compact} | ${r.b_jcs} | ${r.jcs_bytes} | ${r.prettier_equals_stringify ? 'yes' : 'no'} |`, ) } }