diff --git a/crates/core/src/facade.rs b/crates/core/src/facade.rs index 62b015c5..f0af39aa 100644 --- a/crates/core/src/facade.rs +++ b/crates/core/src/facade.rs @@ -77,6 +77,27 @@ pub fn import_pdf(sheet: &LoadedSheet, bytes: &[u8]) -> String { render(outcome) } +/// A rescue copy picked as a file (R4.3, plan §B.1.5 item 4), read as the bytes the worker +/// streamed: strict UTF-8 as `TextDecoder('utf-8', { fatal: true })` read it, one leading +/// BOM dropped, then `draft.restore` with no archive, including its OD-3 value budget. The +/// kinds are `draft.restore`'s; a text that is not UTF-8 is `invalid`, as the application +/// already called it (`error_invalid_copy`). Takes the bytes by value: the copy lives once. +pub fn restore_copy(sheet: &LoadedSheet, bytes: Vec) -> String { + use crate::domain::session::restore_draft; + use crate::ops::{over_value_budget, restore_failure}; + let outcome = String::from_utf8(bytes) + .map_err(|_| Failure::new("invalid", "the copy is not valid UTF-8")) + .and_then(|text| { + let text = text.strip_prefix('\u{FEFF}').map_or(text.as_str(), |t| t); + if over_value_budget(text) { + return Err(Failure::new("invalid", "json value budget exceeded")); + } + restore_draft(sheet, text, None).map_err(restore_failure) + }) + .and_then(|restore| to_value(&restore)); + render(outcome) +} + fn unknown(op: &str) -> Failure { Failure::new("unknown_op", format!("no operation named {op}")) } diff --git a/crates/core/src/ops.rs b/crates/core/src/ops.rs index 511b1755..a3dab857 100644 --- a/crates/core/src/ops.rs +++ b/crates/core/src/ops.rs @@ -323,16 +323,21 @@ fn draft_restore(sheet: &LoadedSheet, input: &str) -> Outcome { return Err(Failure::new("invalid", "json value budget exceeded")); } session::restore_draft(sheet, &input.draft, input.archive.as_deref()) - .map_err(|failure| match failure { - RestoreFailure::UnsupportedVersion(found) => versioned("unsupported_version", found), - RestoreFailure::LoneSurrogate(detail) => Failure::new("lone_surrogate", detail), - RestoreFailure::Invalid(detail) => Failure::new("invalid", detail), - RestoreFailure::Rebase(detail) => Failure::new("rebase_failed", detail), - }) + .map_err(restore_failure) .and_then(|restore| to_value(&restore)) } -fn over_value_budget(text: &str) -> bool { +/// The closed kinds of a draft restore failure, shared with the rescue copy read as bytes. +pub(crate) fn restore_failure(failure: RestoreFailure) -> Failure { + match failure { + RestoreFailure::UnsupportedVersion(found) => versioned("unsupported_version", found), + RestoreFailure::LoneSurrogate(detail) => Failure::new("lone_surrogate", detail), + RestoreFailure::Invalid(detail) => Failure::new("invalid", detail), + RestoreFailure::Rebase(detail) => Failure::new("rebase_failed", detail), + } +} + +pub(crate) fn over_value_budget(text: &str) -> bool { prescan(text.as_bytes()).values > MAX_JSON_VALUES } diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index d7474268..a5368562 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -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, + 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 diff --git a/packages/core-wasm/src/runtime.ts b/packages/core-wasm/src/runtime.ts index 92cce312..fa842b0b 100644 --- a/packages/core-wasm/src/runtime.ts +++ b/packages/core-wasm/src/runtime.ts @@ -23,12 +23,24 @@ export interface GlueModule { core_version(): string call(op: string, input: string): string SheetHandle: new (bundle: string) => GlueSheetHandle + FileBytes: new (expected: number) => GlueFileBytes } export interface GlueSheetHandle { call(op: string, input: string): string /** R4: the PDF import, bytes in, the same envelope out (`PdfImportKind` on failure). */ import_pdf(bytes: Uint8Array): string + /** R4.3: the same import on bytes streamed into the module's memory (consumed). */ + import_pdf_bytes(bytes: GlueFileBytes): string + /** R4.3: a rescue copy streamed into the module's memory (consumed): `draft.restore`'s kinds. */ + restore_copy_bytes(bytes: GlueFileBytes): string +} + +/** A file gathered chunk by chunk in the module's memory (crates/wasm `FileBytes`). */ +export interface GlueFileBytes { + push(chunk: Uint8Array): boolean + len(): number + free(): void } /** Where the module comes from: the browser (Vite asset URLs) or Node (files). */ @@ -178,9 +190,7 @@ export class CoreRuntime { this.glue = glue this.generation += 1 this.setState('ready') - this.spare = this.source - .importGlue(this.nextImport()) - .catch(() => null) + this.spare = this.source.importGlue(this.nextImport()).catch(() => null) } private setState(next: CoreStatus): void { @@ -240,7 +250,9 @@ function closedRefusal(error: unknown): string | null { if (!(error instanceof Error) || error.name !== 'Error') return null const match = /^([a-z_]+): /.exec(error.message) if (!match) return null - return match[1] === 'invalid_sheet' ? error.message.slice(match[0].length) : error.message + return match[1] === 'invalid_sheet' + ? error.message.slice(match[0].length) + : error.message } /** The calls that need the session's sheet (`SheetHandle` in the module's memory). */ @@ -271,7 +283,8 @@ export class SheetSession { /** The handle of the live instance; a handle of a dropped instance is never called. */ private current(): CoreOutcome { if (this.handle && this.handleGeneration === this.runtime.generation) { - if (this.runtime.status === 'ready') return { ok: true, value: this.handle } + if (this.runtime.status === 'ready') + return { ok: true, value: this.handle } this.runtime.kickRestart() return CORE_UNAVAILABLE } diff --git a/packages/core-wasm/tests/pdf-import.test.ts b/packages/core-wasm/tests/pdf-import.test.ts index e48dede6..0e77451e 100644 --- a/packages/core-wasm/tests/pdf-import.test.ts +++ b/packages/core-wasm/tests/pdf-import.test.ts @@ -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: '' })