import { readArchiveDocument, UnsupportedReportVersionError, type ArchiveReadOutcome, type ReportMigrationMap, } from '@faunosteo/domain' import { readAttachedDocument } from './attachment/read' import { ArchiveReadError } from './errors' export { MAX_PDF_BYTES } from './attachment/scan' /** * Reads a Faunosteo PDF back: size cap and `%PDF-` header (`invalid_pdf`), * attachment located by a byte scan (filename first, never a PDF parse: * BLK-009), JSON, then the layered reading of the domain (report first, then * the envelope accepted whole or dropped with a reason). An unknown report * version is `incompatible_version` with that version; any other content * failure is `no_attachment`, as today. */ export async function readReportArchive( pdfBytes: Uint8Array, map: ReportMigrationMap, ): Promise { const data = await readAttachedDocument(pdfBytes) try { return readArchiveDocument(data, map) } catch (error) { // A numeric but unknown version is an incompatible Faunosteo report; no // version at all is not a Faunosteo attachment (today's message). if ( error instanceof UnsupportedReportVersionError && error.foundVersion !== null ) { throw new ArchiveReadError('incompatible_version', error.foundVersion) } throw new ArchiveReadError('no_attachment') } }