/** * Minimal PDFs for the import tests, byte for byte as `pdfWith` of * `scripts/browser/lib/hostile-classes.mjs` writes them (uncompressed streams, no * padding): the core's scanner reads the objects in clear, so an archive is a * `/Type /EmbeddedFile` stream, a `/Filespec` naming it, and the * `/Keywords (faunosteo-archive/1)` stamp. */ const ATTACHMENT_NAME = 'osteo-report.json' const STAMP = '<< /Keywords (faunosteo-archive/1) >>' const OTHER_PRODUCER = '<< /Producer (another tool) >>' const utf8 = (text: string) => new TextEncoder().encode(text) /** Bytes a `File` accepts as a part (never a shared buffer). */ type Bytes = Uint8Array interface EmbeddedStream { bytes: Bytes /** The Filespec name; none leaves the stream unnamed. */ name?: string } function concat(parts: Bytes[]): Bytes { const out = new Uint8Array(parts.reduce((sum, p) => sum + p.byteLength, 0)) let at = 0 for (const part of parts) { out.set(part, at) at += part.byteLength } return out } /** One EmbeddedFile object (number `n`) and its Filespec (`n + 1`) when named. */ function streamObjects(stream: EmbeddedStream, n: number): Bytes[] { const length = stream.bytes.byteLength const parts = [ utf8(`${n} 0 obj\n<< /Type /EmbeddedFile /Length ${length}`), utf8(' >>\nstream\n'), stream.bytes, utf8('\nendstream\nendobj\n'), ] if (stream.name) { parts.push( utf8(`${n + 1} 0 obj\n<< /Type /Filespec /F (${stream.name})`), utf8(` /EF << /F ${n} 0 R >> >>\nendobj\n`), ) } return parts } /** A PDF holding `streams`, then the Faunosteo stamp (or another producer's info). */ export function pdfWith( streams: EmbeddedStream[], { stamp = true }: { stamp?: boolean } = {}, ): Bytes { const parts = [utf8('%PDF-1.7\n%\xE2\xE3\xCF\xD3\n')] let n = 1 for (const stream of streams) { parts.push(...streamObjects(stream, n)) n += 2 } const info = stamp ? STAMP : OTHER_PRODUCER parts.push(utf8(`${n} 0 obj\n${info}\nendobj\n%%EOF\n`)) return concat(parts) } /** A Faunosteo PDF whose `osteo-report.json` attachment holds `json`. */ export function archivePdf(json: string): Bytes { return pdfWith([{ bytes: utf8(json), name: ATTACHMENT_NAME }]) } /** The stamp without its attachment: a Faunosteo PDF a tool stripped. */ export function strippedPdf(): Bytes { return pdfWith([]) } /** A PDF of another producer: no stamp, no attachment. */ export function foreignPdf(): Bytes { return pdfWith([], { stamp: false }) } /** `bytes` as the file the practitioner picks. */ export function pdfFile(bytes: Bytes, name = 'bilan.pdf'): File { return new File([bytes], name, { type: 'application/pdf' }) }