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

+59
View File
@@ -4,6 +4,7 @@
//! the application behind the sheet and import paths (`packages/core-wasm/src/runtime.ts`).
use faunosteo_core::domain::sheet::LoadedSheet;
use faunosteo_core::pdf::read::MAX_PDF_BYTES;
use wasm_bindgen::prelude::{JsError, wasm_bindgen};
/// Version of the core compiled into this module.
@@ -48,6 +49,64 @@ impl SheetHandle {
pub fn import_pdf(&self, bytes: &[u8]) -> String {
faunosteo_core::facade::import_pdf(&self.sheet, bytes)
}
/// The same import on bytes streamed into the module's memory (`FileBytes`), consumed:
/// the file lives once, in linear memory, never as a JavaScript ArrayBuffer (R4.3a).
pub fn import_pdf_bytes(&self, bytes: FileBytes) -> String {
faunosteo_core::facade::import_pdf(&self.sheet, &bytes.data)
}
/// A rescue copy streamed into the module's memory, consumed: strict UTF-8, one BOM,
/// the value budget, then `draft.restore` without an archive (R4.3a).
pub fn restore_copy_bytes(&self, bytes: FileBytes) -> String {
faunosteo_core::facade::restore_copy(&self.sheet, bytes.data)
}
}
/// A file's bytes gathered chunk by chunk in the module's memory (R4.3a): the worker reads
/// `file.stream()` and pushes each chunk, so the only whole copy of the file is this one.
/// The capacity is reserved up front from the file size the worker announces, capped at the
/// PDF limit; a push past the announced size is refused (`false`) and the bytes dropped.
#[wasm_bindgen]
pub struct FileBytes {
data: Vec<u8>,
expected: usize,
}
#[wasm_bindgen]
impl FileBytes {
/// `expected` is the file size; above the PDF cap nothing is reserved and every push
/// is refused, so the caller's own size check stays the only one that reads a file.
#[wasm_bindgen(constructor)]
pub fn new(expected: u32) -> FileBytes {
let expected = expected as usize;
let expected = if expected > MAX_PDF_BYTES {
0
} else {
expected
};
FileBytes {
data: Vec::with_capacity(expected),
expected,
}
}
/// Appends a chunk; `false` (nothing appended) once the announced size would be passed.
pub fn push(&mut self, chunk: &[u8]) -> bool {
if self.data.len().saturating_add(chunk.len()) > self.expected {
return false;
}
self.data.extend_from_slice(chunk);
true
}
pub fn len(&self) -> u32 {
u32::try_from(self.data.len()).unwrap_or(u32::MAX)
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
}
/// R4.2e proof: the import with the OD-3 value cap off, so that a token-dense document runs