Files
faunosteo/packages/pdf/src/attachment/read.ts
T

37 lines
1.1 KiB
TypeScript

import { ArchiveReadError } from '../errors'
import {
ARCHIVE_STAMP,
hasPdfHeader,
locateAttachment,
MAX_PDF_BYTES,
} from './scan'
/**
* The raw JSON a Faunosteo PDF carries, read from the bytes alone (BLK-009):
* no PDF object graph is parsed, so no object stream is ever inflated. pdf-lib
* stays on the export side.
*/
export { ATTACHMENT_NAME_PREFIX } from './scan'
export const PDF_KEYWORD = `${ARCHIVE_STAMP}/1`
/** Size cap first, then the `%PDF-` header: anything else is `invalid_pdf`. */
function assertPdfBytes(bytes: Uint8Array): void {
if (bytes.byteLength > MAX_PDF_BYTES || !hasPdfHeader(bytes)) {
throw new ArchiveReadError('invalid_pdf')
}
}
/**
* The raw JSON carried by the PDF, whatever its shape (bare report of any
* version or archive envelope), before any validation: what the archive
* check tool shows next to the read result. Throws ArchiveReadError
* (`invalid_pdf`, `no_attachment`, `attachment_stripped`).
*/
export async function readAttachedDocument(
bytes: Uint8Array,
): Promise<unknown> {
assertPdfBytes(bytes)
return (await locateAttachment(bytes)).data
}