feat(domain): R4.3a FileBytes sink and the bytes entries of the wasm facade

FileBytes gathers a file chunk by chunk in linear memory (capacity reserved from the
announced size, refused above the PDF cap); SheetHandle.import_pdf_bytes and
restore_copy_bytes consume it, so the worker streams file.stream() and the file lives
once, never as a JavaScript ArrayBuffer nor as a JSON-wrapped string. restore_copy:
strict UTF-8, one BOM, the OD-3 budget, draft.restore's kinds. Node: same envelopes as
import_pdf on the 51 O8 cases; rescue-copy parity with draft.restore.
This commit is contained in:
bchanot committed 2026-10-18 17:45:39 +02:00
1 parent f41dd59b2c
commit 52471dbbc0
5 files changed
+166 -12

No files matched your search

@@ -179,6 +179,62 @@ describe('R4 PDF import through the shipped module (Node)', () => {
}
})
it('gives the same envelope when the file is streamed into the module (FileBytes)', () => {
const oracle = JSON.parse(
read(join(ROOT, 'crates/core/tests/oracles/r4/tolerance.json')),
) as { cases: Array<{ id: string; pdf: string }> }
for (const c of oracle.cases) {
const pdf = new Uint8Array(readFileSync(join(ROOT, c.pdf)))
const bytes = new glue.FileBytes(pdf.byteLength)
for (let at = 0; at < pdf.byteLength; at += 4096) {
expect(bytes.push(pdf.subarray(at, at + 4096)), c.id).toBe(true)
}
expect(bytes.len()).toBe(pdf.byteLength)
expect(sheet.import_pdf_bytes(bytes), c.id).toBe(sheet.import_pdf(pdf))
}
const over = new glue.FileBytes(3)
expect(over.push(new Uint8Array([1, 2, 3, 4]))).toBe(false)
expect(over.len()).toBe(0)
over.free()
const huge = new glue.FileBytes(60 * 1024 * 1024)
expect(huge.push(new Uint8Array(1))).toBe(false)
huge.free()
})
it('reads a rescue copy from streamed bytes as draft.restore reads its text', () => {
const copy = read(join(FIXTURES, 'report-v3.demo.json'))
const restoreText = (text: string) =>
sheet.call(
'draft.restore',
JSON.stringify({ draft: text, archive: null }),
)
const restoreBytes = (data: Uint8Array) => {
const bytes = new glue.FileBytes(data.byteLength)
bytes.push(data)
return sheet.restore_copy_bytes(bytes)
}
const encode = (text: string) => new TextEncoder().encode(text)
expect(restoreBytes(encode(copy))).toBe(restoreText(copy))
expect(JSON.parse(restoreBytes(encode(copy))).ok).toBe(true)
// One BOM dropped, as TextDecoder did.
expect(restoreBytes(encode(`\uFEFF${copy}`))).toBe(restoreText(copy))
const invalid = JSON.parse(restoreText(`${copy} trailing`))
expect(restoreBytes(encode(`${copy} trailing`))).toBe(
restoreText(`${copy} trailing`),
)
expect(invalid.kind).toBe('invalid')
// Not UTF-8: refused as invalid, the kind the application maps to error_invalid_copy.
const notUtf8 = JSON.parse(restoreBytes(new Uint8Array([0x7b, 0xff, 0x7d])))
expect(notUtf8).toMatchObject({ ok: false, kind: 'invalid' })
// A lone surrogate escape is refused, never rewritten.
const lone = JSON.parse(
restoreBytes(
encode(copy.replace('"conclusion": "', '"conclusion": "\\ud800')),
),
)
expect(lone).toMatchObject({ ok: false, kind: 'lone_surrogate' })
})
it('answers invalid_pdf on bytes that are not a PDF, without a trap', () => {
const envelope = JSON.parse(sheet.import_pdf(new Uint8Array([1, 2, 3])))
expect(envelope).toEqual({ ok: false, kind: 'invalid_pdf', detail: '' })