#!/usr/bin/env node /** * R5.6 preparation: the independent Node reference of RFC 8785 for the Rust canonicaliser * (`crates/core/src/domain/sheet/fingerprint.rs`). Its own `canonicalize` (JSON.parse, * JSON.stringify, `<` on strings: UTF-16 order), written apart from the Rust and from the R5.1 * study script. Writes `crates/core/tests/oracles/r5/fingerprint-differential.json` once, only * with MAKE_R5_ORACLES=1 and only when the file is absent (an oracle is added, never rewritten). * * MAKE_R5_ORACLES=1 node scripts/r5-fingerprint-differential.mjs */ import { createHash } from 'node:crypto' import { existsSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' const OUT = resolve( dirname(fileURLToPath(import.meta.url)), '../crates/core/tests/oracles/r5/fingerprint-differential.json', ) 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)) return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalize(value[k])}`).join(',')}}` } const INPUTS = [ '[1e21, 1e-7, 1E+2, 0.1, -0, 5e-324, 1.7976931348623157e308, 123456789012345678901, 9007199254740993, 100, 1.5e-6, 0.000001, 1e20, 999999999999999999999]', '{"\\ud83d\\ude00": 1, "\\ufb33": 2, "\\u00e9": 3, "e\\u0301": 4, "Z": 5, "a": 6, "": 7, "aa": 8, "\\u0080": 9}', '{"s": "\\u0000\\u0001\\u001f\\b\\f\\n\\r\\t\\"\\\\/\\u007f\\u2028\\u2029\\u00e9e\\u0301\\ud83d\\ude00"}', '{"b": {"z": [true, false, null, {"y": 1, "x": [ ]}], "a": {}}, "a": [[[]]]}', '{"geometry": {"cx": 743.0451049804688, "cy": 469.1115417480469, "r": 12}, "name": {"fr": "Atlas (C1)", "en": "Atlas"}}', '{"k": 1, "k": 2}', '[-1.0, 2.50, 1e0, -1e-0, 0.1e1, 12345678.9, 4.35, 0.3, 1e-6, 1e-5]', ] const cases = INPUTS.map((input) => { const canonical = canonicalize(JSON.parse(input)) return { input, canonical, sha256: createHash('sha256').update(canonical, 'utf8').digest('hex'), } }) if (process.env.MAKE_R5_ORACLES !== '1') { console.log(JSON.stringify(cases, null, 2)) } else if (existsSync(OUT)) { console.error(`${OUT} exists: an oracle is never rewritten`) process.exit(1) } else { const frozen = { frozen_from: `Node ${process.version}`, cases } writeFileSync(OUT, `${JSON.stringify(frozen, null, 2)}\n`) console.log(`wrote ${cases.length} cases`) }