Files
bchanot e659924ec0 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).
2026-10-18 18:57:55 +02:00

121 lines
3.7 KiB
TypeScript

import { nodeCoreSource } from '@faunosteo/core-wasm/node'
import type { WorkerLike } from '@/core/worker/client'
import { setImportWorkerSpawn } from '@/core/worker/controller'
import {
createWorkerHandler,
type EngineLoader,
type WorkerEngine,
} from '@/core/worker/handler'
import type { WorkerMessage } from '@/core/worker/protocol'
/**
* The import worker run in-process (happy-dom has no `Worker`): a `WorkerLike` whose
* `postMessage` feeds the worker handler and whose answers come back through `onmessage`
* on a later tick, as a real worker's would; `terminate` drops every later answer. The
* engine is the Node module by default; a test passes its own loader for failures.
*/
let instances = 0
/** The shipped module instantiated afresh (one instance per worker, as in the browser). */
export const nodeEngineLoader: EngineLoader = async (module) => {
const source = nodeCoreSource('pkg')
instances += 1
const glue = await source.importGlue(1000 + instances)
const compiled = module ?? (await source.compile())
const exports = (await glue.default({ module_or_path: compiled })) as {
memory?: WebAssembly.Memory
}
return {
SheetHandle: glue.SheetHandle,
FileBytes: glue.FileBytes,
memory: exports.memory ?? null,
}
}
export interface FakeWorker extends WorkerLike {
terminated: boolean
/** Every message the page posted to the worker (the task, as the worker receives it). */
received: unknown[]
/** Every message the worker posted, dropped or delivered. */
posted: WorkerMessage[]
/** The posted message types without the per-chunk `ack`s. */
sequence(): string[]
}
export function inProcessWorker(
load: EngineLoader = nodeEngineLoader,
): FakeWorker {
const worker: FakeWorker = {
terminated: false,
received: [],
posted: [],
onmessage: null,
onerror: null,
onmessageerror: null,
terminate() {
worker.terminated = true
},
sequence() {
return worker.posted.map((m) => m.type).filter((t) => t !== 'ack')
},
postMessage(message) {
worker.received.push(message)
void handle(message)
},
}
const handle = createWorkerHandler(load, (message) => {
worker.posted.push(message)
setTimeout(() => {
if (!worker.terminated)
worker.onmessage?.({ data: message } as MessageEvent)
}, 0)
})
return worker
}
/** Routes the page's imports through in-process workers; returns the spawned ones. */
export function useInProcessImportWorker(
load: EngineLoader = nodeEngineLoader,
): { workers: FakeWorker[]; restore: () => void } {
const workers: FakeWorker[] = []
setImportWorkerSpawn(() => {
const worker = inProcessWorker(load)
workers.push(worker)
return worker
})
return { workers, restore: () => setImportWorkerSpawn(null) }
}
/** An engine whose `SheetHandle` throws `error` on every import call (a trap, say). */
export function throwingEngineLoader(error: Error): EngineLoader {
return async (module) => {
const engine = await nodeEngineLoader(module)
class Throwing extends engine.SheetHandle {
import_pdf_bytes(): string {
throw error
}
restore_copy_bytes(): string {
throw error
}
}
return { ...engine, SheetHandle: Throwing } as WorkerEngine
}
}
/** An engine whose import calls answer the envelope text `out` (a core refusal, say). */
export function answeringEngineLoader(out: string): EngineLoader {
return async (module) => {
const engine = await nodeEngineLoader(module)
class Answering extends engine.SheetHandle {
import_pdf_bytes(): string {
return out
}
restore_copy_bytes(): string {
return out
}
}
return { ...engine, SheetHandle: Answering } as WorkerEngine
}
}