feat(domain): R4.5e wasm proof export, resources per page, N-1 and TS-engine read-back, cost comparison

export_pdf_for_test on the proofs build (ExportInput JSON + base64 rasters): the 14 O2
runs give the native digests through wasm; the frozen O11 scanner reads every R4 PDF and
the N-1 Zod readers accept its envelope; a stripped R4 PDF answers attachment_stripped;
the TypeScript engine reads the R4 PDFs back to the R3 outcome; E2 size ≤ R3 + 5 %. Page
resources list the images the page draws (listing every embed cost 9 MB on CAP). Node,
stub rasters, TS → Rust: demo 198 → 36 ms, L 772 → 57 ms, CAP 5.7 s → 0.22 s; files
236 KB, 386 KB, 1.6 MB against 243 KB, 463 KB, 2.2 MB. Module with the writer linked
(proofs build): 531.6 KB gzip; shipped unchanged at 426 KB until R4.6 links it.
This commit is contained in:
bchanot committed 2026-10-18 23:21:05 +02:00
1 parent 4d45de5745
commit 50c8fe0b33
21 files changed
+485 -19

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@ mod conclusion;
mod footer;
mod header;
mod observation;
mod png;
pub mod png;
use super::draw_log::{DrawLog, Rgb, rgb};
use super::input::{CheckedInput, ExportError, ExportErrorKind, ExportWarning, LogoSlot, Rasters};
+22 -6
View File
@@ -10,7 +10,7 @@
use miniz_oxide::deflate::compress_to_vec_zlib;
use super::content::{font_resource_name, image_resource_name, page_content};
use super::draw_log::{DrawLog, Embed, PAGE_HEIGHT, PAGE_WIDTH};
use super::draw_log::{DrawLog, DrawOp, Embed, PAGE_HEIGHT, PAGE_WIDTH, PageLog};
use super::input::{ExportError, ExportErrorKind};
use super::objects::{Objects, Ref, dict, hex_ascii, hex_text, literal, name, numbers, pdf_date};
use super::png::{DecodedPng, decode_png};
@@ -101,15 +101,31 @@ fn write_fonts(objects: &mut Objects) -> [(Font, Ref); 3] {
})
}
fn resources(fonts: &[(Font, Ref); 3], images: &[Ref]) -> String {
/// The page's resources: the three fonts, and the images the page draws (as pdf-lib listed
/// per page what its calls used; listing every embed on every page costs megabytes on a
/// report at the caps).
fn resources(fonts: &[(Font, Ref); 3], images: &[Ref], page: &PageLog) -> String {
let font_entries: Vec<String> = fonts
.iter()
.map(|(font, r)| format!("/{} {}", font_resource_name(*font), r.text()))
.collect();
let image_entries: Vec<String> = images
let mut used: Vec<usize> = page
.ops
.iter()
.enumerate()
.map(|(i, r)| format!("/{} {}", image_resource_name(i), r.text()))
.filter_map(|op| match op {
DrawOp::Image(image) => Some(image.embed),
_ => None,
})
.collect();
used.sort_unstable();
used.dedup();
let image_entries: Vec<String> = used
.into_iter()
.filter_map(|i| {
images
.get(i)
.map(|r| format!("/{} {}", image_resource_name(i), r.text()))
})
.collect();
format!(
"<< /Font << {} >> /XObject << {} >> >>",
@@ -166,7 +182,7 @@ fn write_page(
("Parent", shared.pages.text()),
("MediaBox", numbers(&[0.0, 0.0, PAGE_WIDTH, PAGE_HEIGHT])),
("Contents", content.text()),
("Resources", resources(shared.fonts, shared.images)),
("Resources", resources(shared.fonts, shared.images, page)),
];
if !annots.is_empty() {
entries.push(("Annots", format!("[{}]", annots.join(" "))));
+3 -1
View File
@@ -15,11 +15,13 @@ path = "src/lib.rs"
default = []
# The R0 trap proofs (panic_for_test, recurse_for_test): built for the proofs, never for the
# module the application ships (scripts/wasm-build.sh passes it until R2 wires the real loader).
proofs = []
proofs = ["dep:serde_json"]
[dependencies]
faunosteo-core = { path = "../core", features = ["pdf-read", "pdf-write"] }
wasm-bindgen.workspace = true
# The proofs build reads the test rasters' JSON (never shipped).
serde_json = { workspace = true, optional = true }
[lints]
workspace = true
+48
View File
@@ -124,6 +124,54 @@ pub fn import_pdf_unbounded_for_test(bundle: &str, bytes: &[u8]) -> Result<Strin
})
}
/// R4.5 proof: one export through the module from an `ExportInput` JSON and the rasters
/// as `{ "headerLogo"|"footerLogo"|"poweredBy"|"partner": base64|null, "views": [{
/// "observationId", "viewId", "png": base64 }] }`, so the wasm output can be compared to the
/// native digests and the module's size with the writer linked be measured. Never shipped:
/// the application's export goes through the R4.6 session.
#[cfg(feature = "proofs")]
#[wasm_bindgen]
pub fn export_pdf_for_test(input: &str, rasters: &str) -> Result<Vec<u8>, JsError> {
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::pdf::write::export::export_pdf;
use faunosteo_core::pdf::write::input::{ExportInput, Rasters};
use faunosteo_core::pdf::write::layout::png::base64_bytes;
let input: ExportInput = parse_json(input)
.ok()
.and_then(|v| serde_json::from_value(v).ok())
.ok_or_else(|| JsError::new("invalid input"))?;
let spec = parse_json(rasters).map_err(|e| JsError::new(&e.to_string()))?;
let slot = |key: &str| {
spec.get(key)
.and_then(|v| v.as_str())
.and_then(base64_bytes)
};
let mut set = Rasters {
header_logo: slot("headerLogo"),
footer_logo: slot("footerLogo"),
powered_by: slot("poweredBy"),
partner: slot("partner"),
views: Default::default(),
};
for view in spec
.get("views")
.and_then(|v| v.as_array())
.into_iter()
.flatten()
{
let key = (
view["observationId"].as_str().unwrap_or("").to_owned(),
view["viewId"].as_str().unwrap_or("").to_owned(),
);
if let Some(png) = view["png"].as_str().and_then(base64_bytes) {
set.views.insert(key, png);
}
}
export_pdf(input, &set)
.map(|result| result.pdf)
.map_err(|error| JsError::new(&format!("{}: {}", error.kind.as_str(), error.detail)))
}
/// R4.2e proof: the linear memory ceiling (`--max-memory`, `.cargo/config.toml`). Allocates
/// and touches `mib` MiB; past the ceiling the allocation fails and Rust aborts, a trap.
#[cfg(feature = "proofs")]