import { readFileSync, unlinkSync, writeFileSync } from 'node:fs' import { join } from 'node:path' import { deflateSync } from 'node:zlib' import { nodeSheet } from './core-module.mjs' import { FIXTURES_DIR } from './paths.mjs' /** * The hostile classes of packages/core-wasm/tests/pdf-import.budget.test.ts * (plan §B.3, W4), built in Node with the same builders, written as * temporary files for the browser run. The L and CAP envelopes are built * as that suite builds them: the demo archive read by the shipped module, * the report swapped in, a fresh `archive.snapshot`. */ const MiB = 1024 * 1024 // UTF-8, as the suite's TextEncoder (the envelopes hold accented text). const ascii = (text) => Buffer.from(text, 'utf8') /** A PDF holding `streams` as EmbeddedFile objects, Filespecs, the stamp. */ export function pdfWith(streams, padding = 0) { const parts = [ascii('%PDF-1.7\n%\xE2\xE3\xCF\xD3\n')] if (padding > 0) parts.push(ascii(`%${'x'.repeat(padding - 2)}\n`)) let n = 1 for (const stream of streams) { const filter = stream.flate ? ' /Filter /FlateDecode' : '' const length = stream.bytes.byteLength parts.push( ascii(`${n} 0 obj\n<< /Type /EmbeddedFile /Length ${length}${filter}`), ascii(' >>\nstream\n'), stream.bytes, ascii('\nendstream\nendobj\n'), ) if (stream.name) { parts.push( ascii(`${n + 1} 0 obj\n<< /Type /Filespec /F (${stream.name})`), ascii(` /EF << /F ${n} 0 R >> >>\nendobj\n`), ) } n += 2 } parts.push( ascii(`${n} 0 obj\n<< /Keywords (faunosteo-archive/1) >>\nendobj\n%%EOF\n`), ) return Buffer.concat(parts) } const zeros = (bytes) => Buffer.alloc(bytes) const deflate = (bytes) => deflateSync(bytes) /** `{"version":3,"a":[1,1,…]}` of about `bytes` bytes. */ export function denseNumbers(bytes) { const head = '{"version":3,"a":[' const count = Math.floor((bytes - head.length - 2) / 2) return ascii(`${head}${'1,'.repeat(count - 1)}1]}`) } /** `{"version":3,"a":["","",…]}`: one string per three bytes. */ function denseStrings(bytes) { const head = '{"version":3,"a":[' const count = Math.floor((bytes - head.length - 2) / 3) return ascii(`${head}${'"",'.repeat(count - 1)}""]}`) } /** `{"version":3,"a":[[…[1,1,…]…]]}` nested to `depth`. */ function deepArrays(bytes, depth) { const head = `{"version":3,"a":${'['.repeat(depth - 1)}` const tail = `${']'.repeat(depth - 1)}}` const count = Math.floor((bytes - head.length - tail.length) / 2) return ascii(`${head}${'1,'.repeat(count - 1)}1${tail}`) } const named = (bytes, extra = {}) => ({ bytes, name: 'osteo-report.json', ...extra, }) /** The demo's envelope with `report` and a fresh snapshot. */ function envelopePdf(sheet, demoArchive, reportFile) { const report = JSON.parse(readFileSync(join(FIXTURES_DIR, reportFile))) const input = JSON.stringify({ report }) const snapshot = JSON.parse(sheet.call('archive.snapshot', input)) if (!snapshot.ok) throw new Error(`${reportFile}: ${snapshot.kind}`) const envelope = { ...demoArchive, report, sheetSnapshot: snapshot.value } return pdfWith([named(ascii(JSON.stringify(envelope, null, 2)))]) } async function envelopeClasses(bundle, pkgDir) { const sheet = await nodeSheet(bundle, pkgDir) const demo = readFileSync(join(FIXTURES_DIR, 'pdf/r3/demo.fr.logos.pdf')) const read = JSON.parse(sheet.import_pdf(new Uint8Array(demo))) if (!read.ok) throw new Error(`demo archive: ${read.kind}`) const archive = read.value.read.archive const build = (file) => () => envelopePdf(sheet, archive, file) return [ { id: 'L-envelope', build: build('r4/reports/L.json') }, { id: 'CAP-envelope', build: build('r4/reports/CAP.json') }, ] } const DENSE = 16 * MiB - 64 const ATTACK_CLASSES = [ { id: 'dense-numbers-16mib', build: () => pdfWith([named(denseNumbers(DENSE))]), }, { id: 'dense-strings-16mib', build: () => pdfWith([named(denseStrings(DENSE))]), }, { id: 'depth-64-16mib', build: () => pdfWith([named(deepArrays(DENSE, 64))]), }, { id: 'dense-numbers-copy-16mib', ext: 'json', build: () => denseNumbers(DENSE), }, { id: 'inflation-16mib', build: () => pdfWith([named(deflate(zeros(16 * MiB)), { flate: true })]), }, { id: 'bomb-16mib-plus-1', build: () => pdfWith([named(deflate(zeros(16 * MiB + 1)), { flate: true })]), }, { id: 'bombs-32x17mib', build: () => { const bomb = { bytes: deflate(zeros(17 * MiB)), flate: true } const bombs = Array.from({ length: 32 }, () => bomb) return pdfWith([...bombs, named(ascii('{"version":3}'))]) }, }, { id: 'file-50mib', build: () => pdfWith([named(ascii('{"version":3}'))], 50 * MiB - 400), }, ] /** * Every class (or the `wanted` ids) built and written under `dir`, the * envelopes through the shipped build in `pkgDir`; `remove()` deletes them. */ export async function writeHostileClasses({ bundle, dir, pkgDir, wanted }) { const envelopes = await envelopeClasses(bundle, pkgDir) const all = [...envelopes, ...ATTACK_CLASSES] const classes = all.filter((c) => !wanted || wanted.includes(c.id)) const written = classes.map((c) => { const path = join(dir, `${c.id}.${c.ext ?? 'pdf'}`) const bytes = c.build() writeFileSync(path, bytes) return { id: c.id, path, bytes: bytes.length } }) const remove = () => written.forEach((c) => unlinkSync(c.path)) return { classes: written, remove } }