feat(app): R4.3a the page reads the file chunk by chunk and streams it to the worker's sink

Measured at R4.3a (RUST.md §10.5): reading a file inside a WebKit worker costs about
180 MiB of content RSS whatever the method (arrayBuffer, stream, BYOB), 14 MiB on the
page; a page-side BYOB read feeding the worker's FileBytes gives 106 MiB on a 50 MiB file
against 272 in the worker and 76 for the TypeScript. Protocol v1 gains chunk / ack / end:
one 1 MiB buffer travels (transferred) and comes back after each push, so the page holds
one chunk and the worker's sink the only whole copy; no byte is parsed on the page. The
worker refuses an oversize announced before any chunk and a size that changes while
reading. Tests adapted (handler, client with a scripted ack, read failure on the page).
This commit is contained in:
bchanot committed 2026-10-18 18:57:55 +02:00
1 parent 49591c53cd
commit e659924ec0
12 files changed
+552 -272

No files matched your search

@@ -146,12 +146,17 @@ describe('HomeRoute import: success paths', () => {
}
expect(workers).toHaveLength(1)
const task = workers[0]!.received[0] as ImportTaskMessage
expect(task).toMatchObject({ type: 'task', task: 'pdf' })
expect(task.file).toBe(file)
expect(task).toMatchObject({ type: 'task', task: 'pdf', size: file.size })
expect(task.sheet).toBe(JSON.stringify(await bundledSheet()))
expect(task.module).toBeInstanceOf(WebAssembly.Module)
// The page streams the file chunk by chunk, never whole: no arrayBuffer, no text.
expect(read).not.toHaveBeenCalled()
expect(text).not.toHaveBeenCalled()
const chunks = workers[0]!.received.filter(
(m) => (m as { type: string }).type === 'chunk',
)
expect(chunks.length).toBeGreaterThanOrEqual(1)
expect(workers[0]!.received.at(-1)).toMatchObject({ type: 'end' })
const imported = resultOf(workers[0]!)
expect(hydrate).toHaveBeenCalledTimes(1)
const [session, index] = hydrate.mock.calls[0]!
@@ -268,7 +273,7 @@ describe('HomeRoute import: a core failure reading the tab draft', () => {
await mountAndPickPdf()
await screen.findByText(fr['home.import.error_core_unavailable']!)
// The worker read the file: the failure is the store's, after the result.
expect(workers[0]!.posted.map((m) => m.type)).toEqual(['ready', 'result'])
expect(workers[0]!.sequence()).toEqual(['ready', 'result'])
expect(alertMock()).not.toHaveBeenCalled()
expect(screen.queryByTestId('editor-route')).toBeNull()
} finally {
@@ -284,7 +289,7 @@ describe('HomeRoute import: a core failure reading the tab draft', () => {
try {
await mountAndPickPdf()
await screen.findByTestId('editor-route')
expect(workers[0]!.posted.map((m) => m.type)).toEqual(['ready', 'result'])
expect(workers[0]!.sequence()).toEqual(['ready', 'result'])
expect(alertMock()).toHaveBeenCalledWith(fr['session.held_blocked'])
} finally {
useReportStore.setState({ hydrateImport: real })