Files

91 lines
3.3 KiB
TypeScript

/**
* O11 (R4 plan B.5.1, B.2.2): the frozen TypeScript scanner, the permanent N-1 PDF reader,
* still reads the archives of every earlier build. The copy under `./n-1/scanner` stands for
* the R3 build and release 0.2.0: an R4 PDF it cannot read would break a rollback.
*/
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { readAttachedDocument } from './n-1/scanner/attachment/read'
import { ArchiveReadError } from './n-1/scanner/errors'
const FIXTURES = '../../../apps/app/tests/fixtures/'
const fixtureBytes = (name: string): Uint8Array =>
new Uint8Array(
readFileSync(fileURLToPath(new URL(`${FIXTURES}${name}`, import.meta.url))),
)
const fixtureJson = (name: string): unknown =>
JSON.parse(new TextDecoder().decode(fixtureBytes(name)))
const ascii = (text: string): Uint8Array =>
Uint8Array.from(text, (c) => c.charCodeAt(0))
/** `faunosteo-archive/1` as pdf-lib 1.17's `setKeywords` writes it: UTF-16BE hex, BOM first. */
const HEX_STAMP =
'<FEFF006600610075006E006F007300740065006F002D0061007200630068006900760065002F0031>'
/**
* A one-page PDF with a classic xref and no EmbeddedFile, whose Info dictionary carries
* `keywords` as its `/Keywords` token: what a stamped archive looks like once a tool has
* removed its attachment. Built by hand: pdf-lib is not a dependency of this package.
*/
function pdfWithoutAttachment(keywords: string): Uint8Array {
const objects = [
'<< /Type /Catalog /Pages 2 0 R >>',
'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595.28 841.89] >>',
`<< /Keywords ${keywords} >>`,
]
let text = '%PDF-1.7\n'
const offsets = objects.map((body, i) => {
const offset = text.length
text += `${i + 1} 0 obj\n${body}\nendobj\n`
return offset
})
const xrefAt = text.length
text += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`
text += offsets
.map((o) => `${String(o).padStart(10, '0')} 00000 n \n`)
.join('')
text += `trailer\n<< /Size ${objects.length + 1} /Root 1 0 R /Info 4 0 R >>\n`
text += `startxref\n${xrefAt}\n%%EOF\n`
return ascii(text)
}
async function readFailure(bytes: Uint8Array): Promise<ArchiveReadError> {
const failure: unknown = await readAttachedDocument(bytes).then(
() => null,
(error: unknown) => error,
)
expect(failure).toBeInstanceOf(ArchiveReadError)
return failure as ArchiveReadError
}
describe('the frozen scanner reads the archives of earlier builds', () => {
it.each([
['report-v2.full.pdf', 'report-v2.full.json'],
['report-v2.minimal.pdf', 'report-v2.minimal.json'],
['report-v3.bare.pdf', 'report-v3.demo.json'],
])('%s carries %s', async (pdf, json) => {
const data = await readAttachedDocument(fixtureBytes(pdf))
expect(data).toStrictEqual(fixtureJson(json))
})
})
describe('a stamped archive whose attachment was removed', () => {
it.each([
['UTF-16BE hex (the export)', HEX_STAMP],
['literal', '(faunosteo-archive/1)'],
])('stamp written as %s: attachment_stripped', async (_form, token) => {
const failure = await readFailure(pdfWithoutAttachment(token))
expect(failure.kind).toBe('attachment_stripped')
})
it('the same file without the stamp: no_attachment', async () => {
const failure = await readFailure(pdfWithoutAttachment('(other)'))
expect(failure.kind).toBe('no_attachment')
})
})