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

Generated
+1
View File
@@ -311,6 +311,7 @@ name = "faunosteo-wasm"
version = "0.1.0"
dependencies = [
"faunosteo-core",
"serde_json",
"wasm-bindgen",
]
@@ -1 +1 @@
ecc0d4aea358620e160cbb22d6bf4e8a426dab3efac9f3806dd657b3dea516e7
2d75e1791c025472dcf90a0df7fd0c4ece5d85456b5d25e8be70d5e4e5786e86
@@ -1 +1 @@
4f70895ae318f8f595c9978dd654cefda8f9efe10b35ea8e50d37617a9ea4332
b534afb3a7b40b2c04dbc55b9909835a15324f5b1c49069eee28f061a6f07712
@@ -1 +1 @@
b8c5492b6a09bf7caeef7a299d410e1b193272ece57268d672e55da21f204d73
ce9bdaab621d510e0e7d307f67f410bac1a5b69a65cb74cd289ff4a6b149e820
@@ -1 +1 @@
7afcca9d929fc3b0d04e7e3cfda0aaa3999f4dd784910b91d2aafdcae925a778
e6ed5ce14bf4338c0f161c51900443747bb033403ea4164f96e13d0810abf881
@@ -1 +1 @@
0540d6a281bbec25478cc9b956b8ba9f5ae9f8f6c2561a869b0fed0b0ddb0e38
5f98fb659c7d587cf3921d5b9871e7dd10a2a8148003d1063f74d7a1951e37c4
@@ -1 +1 @@
8b42a89904f2b5544be5ecb834d8c15203560264388f95fab159bb5ec9039e5b
1faf76663e834c5297c2244310a7366d508010d146ba789d4e24016e0959771d
@@ -1 +1 @@
c1710db98a3e3265dc5097bbea02996f489c81ed1f29e2bd6608ca4010d328b7
2321f77c211656a41f870584a31d9c44c00868ab420f68428194b254e4c166cd
@@ -1 +1 @@
6a77c18ca8d00ea1d063cd5f7e3367d76654b765ad1cbfedddba3d038c5aba08
781868dbd08050b8191fb727a1b553348c2992c80aff5693fa7781abebc58e2e
@@ -1 +1 @@
ace38ed895694f4f86011f28892846ac41829d15bce485f82cbf29b4cd586715
48503857cab7ba4a27bdc1fec834f77cb1a88f4f9262d1f28f0c262f6ffa7667
@@ -1 +1 @@
57bcac416cce264a09e221d8ece8dd8ebfcb7244b3feae6659a4c95a99549383
bb88a9cebd07a66c669b7fbf656275f003ce327972523cb369494fdfd1abe6c3
+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")]
+1
View File
@@ -21,6 +21,7 @@
"build": "tsc -p tsconfig.json --noEmit"
},
"devDependencies": {
"@faunosteo/pdf": "workspace:*",
"@types/node": "^20.19.41",
"eslint": "^9.17.0",
"globals": "^15.14.0",
+2
View File
@@ -56,6 +56,8 @@ export interface ProofModule extends GlueModule {
import_pdf_unbounded_for_test(bundle: string, bytes: Uint8Array): string
/** R4.2e: allocates `mib` MiB, the linear ceiling proof. */
allocate_for_test(mib: number): number
/** R4.5: one export from an `ExportInput` JSON and base64 rasters (wasm = native proof). */
export_pdf_for_test(input: string, rasters: string): Uint8Array
}
/** A fresh, initialised glue of the proofs build, outside any runtime (R0 proofs). */
+223
View File
@@ -0,0 +1,223 @@
/**
* R4.5: the Rust writer through the module (proofs build, `export_pdf_for_test`): the wasm
* output is byte-identical to the native one (the digests `goldens/r4/pdf.*.sha256` the
* cargo suite froze), the frozen N-1 scanner (O11) reads every R4 PDF and the N-1 Zod
* readers accept its envelope (plan §B.2.2: what the R3 build and release 0.2.0 read), and
* an R4 PDF with its embedded file removed answers `attachment_stripped` through O11.
* The inputs are the envelopes of the R3 PDFs (read by O11 here), the rasters the stubs of
* the goldens, the real PNGs for the real-raster run.
*/
import { createHash } from 'node:crypto'
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { loadProofModule } from '../src/node'
import { readArchiveDocument, ReportArchiveEnvelopeSchema } from './n-1'
import { ReportMigrationMapSchema } from './n-1/domain'
import { ArchiveReadError } from './n-1/scanner/errors'
import { readAttachedDocument } from './n-1/scanner/attachment/read'
const ROOT = fileURLToPath(new URL('../../../', import.meta.url))
const FIXTURES = join(ROOT, 'apps/app/tests/fixtures')
const sha256 = (data: Uint8Array) =>
createHash('sha256').update(data).digest('hex')
const ONE_PX_PNG =
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='
const TWO_BY_ONE_PNG =
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC'
interface Run {
name: string
r3: string
report?: string
rasters: (envelope: Envelope) => string
}
interface Envelope {
report: {
observations: Array<{ id: string; zones: Array<{ viewId: string }> }>
}
sheetSnapshot: unknown
branding: unknown
labels: unknown
locale: string
exportedAt: string
}
/** The stub rasters of the goldens: every slot and every (observation, view). */
function stubRasters(png: string): (envelope: Envelope) => string {
return (envelope) => {
const views = new Map<
string,
{ observationId: string; viewId: string; png: string }
>()
for (const observation of envelope.report.observations) {
for (const zone of observation.zones) {
const key = `${observation.id}\u0000${zone.viewId}`
if (!views.has(key))
views.set(key, {
observationId: observation.id,
viewId: zone.viewId,
png,
})
}
}
return JSON.stringify({
headerLogo: png,
footerLogo: png,
poweredBy: png,
partner: png,
views: [...views.values()],
})
}
}
function realRasters(envelope: Envelope): string {
const manifest = JSON.parse(
readFileSync(join(FIXTURES, 'pdf/r3/real/manifest.json'), 'utf8'),
) as {
calls: Array<{
png: string
subject: { kind: string; observationId?: string; viewId?: string }
}>
}
const spec = JSON.parse(stubRasters(TWO_BY_ONE_PNG)(envelope)) as Record<
string,
unknown
> & {
views: Array<{ observationId: string; viewId: string; png: string }>
}
for (const call of manifest.calls) {
const png = readFileSync(join(FIXTURES, 'pdf/r3/real', call.png)).toString(
'base64',
)
if (call.subject.kind === 'logo') spec['partner'] = png
else
spec.views = spec.views.map((v) =>
v.observationId === call.subject.observationId &&
v.viewId === call.subject.viewId
? { ...v, png }
: v,
)
}
return JSON.stringify(spec)
}
const RUNS: Run[] = []
for (const report of ['demo', 'v2full', 'v2unresolved']) {
for (const locale of ['fr', 'en']) {
for (const mode of ['nologos', 'logos']) {
RUNS.push({
name: `${report}.${locale}.${mode}`,
r3: `pdf/r3/${report}.${locale}.${mode}.pdf`,
rasters: stubRasters(mode === 'logos' ? TWO_BY_ONE_PNG : ONE_PX_PNG),
})
}
}
}
RUNS.push({
name: 'real.v2full-obs1.fr.logos',
r3: 'pdf/r3/real/v2full-obs1.fr.logos.pdf',
rasters: realRasters,
})
RUNS.push({
name: 'store-path.fr.nologos',
r3: 'pdf/r3/store-path.fr.nologos.pdf',
report: 'pdf/r3/store-path.report.json',
rasters: stubRasters(ONE_PX_PNG),
})
async function envelopeOf(pdf: Uint8Array): Promise<Envelope> {
return (await readAttachedDocument(pdf)) as Envelope
}
function inputOf(envelope: Envelope, reportOverride?: string): string {
const report = reportOverride
? JSON.parse(readFileSync(join(FIXTURES, reportOverride), 'utf8'))
: envelope.report
return JSON.stringify({
report,
sheetSnapshot: envelope.sheetSnapshot,
branding: envelope.branding,
labels: envelope.labels,
locale: envelope.locale,
exportedAt: envelope.exportedAt,
})
}
/** The R4 PDF with its embedded file hidden from the scanner (the stamp stays). */
function stripped(pdf: Uint8Array): Uint8Array {
const text = Buffer.from(pdf).toString('latin1')
const at = text.indexOf('/Type /EmbeddedFile')
expect(at).toBeGreaterThan(0)
const copy = Buffer.from(pdf)
copy.write('/Type /XmbeddedFile', at, 'latin1')
return new Uint8Array(copy)
}
describe('R4 writer through the module (proofs build)', () => {
it('gives the native bytes on every O2 run, which the N-1 readers accept', async () => {
const proofs = await loadProofModule()
const map = ReportMigrationMapSchema.parse(
JSON.parse(
readFileSync(
join(
ROOT,
'supabase/seed/anatomical-sheets/equine/draft/report-migration-map.json',
),
'utf8',
),
),
)
for (const run of RUNS) {
const r3 = new Uint8Array(readFileSync(join(FIXTURES, run.r3)))
const envelope = await envelopeOf(r3)
const pdf = proofs.export_pdf_for_test(
inputOf(envelope, run.report),
run.rasters(envelope),
)
const expected = readFileSync(
join(FIXTURES, `goldens/r4/pdf.${run.name}.sha256`),
'utf8',
).trim()
expect(sha256(pdf), run.name).toBe(expected)
// O11, the frozen scanner of the R3 build and 0.2.0, reads it; the N-1 Zod readers accept it.
const read = (await readAttachedDocument(pdf)) as Envelope
expect(read.locale, run.name).toBe(envelope.locale)
expect(
ReportArchiveEnvelopeSchema.safeParse(read).success,
run.name,
).toBe(true)
const outcome = readArchiveDocument(read, map)
expect(outcome.report.version, run.name).toBe(3)
if (!run.report) expect(read, run.name).toEqual(envelope)
const failure = await readAttachedDocument(stripped(pdf)).then(
() => null,
(e: unknown) => e,
)
expect(failure, run.name).toBeInstanceOf(ArchiveReadError)
expect((failure as ArchiveReadError).kind, run.name).toBe(
'attachment_stripped',
)
}
}, 60_000)
it('refuses the export errors as closed kinds, never a trap', async () => {
const proofs = await loadProofModule()
const r3 = new Uint8Array(
readFileSync(join(FIXTURES, 'pdf/r3/demo.fr.nologos.pdf')),
)
const envelope = await envelopeOf(r3)
const bad = {
...JSON.parse(inputOf(envelope)),
branding: { siteUrl: 'nope' },
}
expect(() =>
proofs.export_pdf_for_test(
JSON.stringify(bad),
stubRasters(ONE_PX_PNG)(envelope),
),
).toThrow(/branding_invalid: branding/)
})
})
@@ -0,0 +1,170 @@
/**
* R4.5: the Rust writer's output under the TypeScript engine's own reader (plan §B.2.2: the
* R3 build reads R4 PDFs) and against that engine's export on the same inputs and
* conditions: the demo, L and CAP reports with the stub rasteriser, in Node, time, process
* RSS growth and file size (E2: ≤ R3 + 5 % on the O2 runs). The Rust side runs through the
* proofs build; the TypeScript side is `@faunosteo/pdf`'s `exportReportArchive`. Numbers
* printed for RUST.md §10.7. The engine and this test leave with R4.8.
*/
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import {
exportReportArchive,
readAttachedDocument,
readReportArchive,
type ExportInput,
} from '@faunosteo/pdf'
import { loadProofModule } from '../src/node'
import { ReportMigrationMapSchema } from './n-1/domain'
const ROOT = fileURLToPath(new URL('../../../', import.meta.url))
const FIXTURES = join(ROOT, 'apps/app/tests/fixtures')
const ONE_PX_PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
'base64',
)
const TWO_BY_ONE_PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC',
'base64',
)
const FIXED_EXPORTED_AT = new Date('2026-10-11T12:00:00.000Z')
type Report = ExportInput['report']
interface Envelope {
report: Report
sheetSnapshot: ExportInput['sheet']
branding: ExportInput['branding']
labels: ExportInput['labels']
locale: 'fr' | 'en'
exportedAt: string
}
function rastersFor(report: Report, png: Buffer): string {
const seen = new Set<string>()
const views: Array<{ observationId: string; viewId: string; png: string }> =
[]
for (const observation of report.observations) {
for (const zone of observation.zones) {
const key = `${observation.id}\u0000${zone.viewId}`
if (seen.has(key)) continue
seen.add(key)
views.push({
observationId: observation.id,
viewId: zone.viewId,
png: png.toString('base64'),
})
}
}
const logo = png.toString('base64')
return JSON.stringify({
headerLogo: logo,
footerLogo: logo,
poweredBy: logo,
partner: logo,
views,
})
}
function migrationMap() {
return ReportMigrationMapSchema.parse(
JSON.parse(
readFileSync(
join(
ROOT,
'supabase/seed/anatomical-sheets/equine/draft/report-migration-map.json',
),
'utf8',
),
),
)
}
describe('R4 writer under the TypeScript engine (same inputs, Node)', () => {
it('the engine reads every R4 PDF of the O2 runs back as it reads the R3 one; E2 size', async () => {
const proofs = await loadProofModule()
const map = migrationMap()
for (const report of ['demo', 'v2full', 'v2unresolved']) {
for (const locale of ['fr', 'en']) {
for (const mode of ['nologos', 'logos']) {
const name = `${report}.${locale}.${mode}`
const r3 = new Uint8Array(
readFileSync(join(FIXTURES, `pdf/r3/${name}.pdf`)),
)
const envelope = (await readAttachedDocument(r3)) as Envelope
const png = mode === 'logos' ? TWO_BY_ONE_PNG : ONE_PX_PNG
const pdf = proofs.export_pdf_for_test(
JSON.stringify(envelope),
rastersFor(envelope.report, png),
)
expect(await readReportArchive(pdf, map), name).toEqual(
await readReportArchive(r3, map),
)
expect(pdf.byteLength, `${name}: size`).toBeLessThanOrEqual(
Math.ceil(r3.byteLength * 1.05),
)
}
}
}
})
it('costs less than the engine on demo, L and CAP with the stub rasters (printed)', async () => {
const proofs = await loadProofModule()
const map = migrationMap()
const demo = (await readAttachedDocument(
new Uint8Array(readFileSync(join(FIXTURES, 'pdf/r3/demo.fr.logos.pdf'))),
)) as Envelope
const rows = [
'| Report | TS ms | TS RSS MiB | TS bytes | Rust ms | Rust RSS MiB | Rust bytes |',
'|---|---|---|---|---|---|---|',
]
for (const name of ['demo', 'L', 'CAP']) {
const report: Report =
name === 'demo'
? demo.report
: JSON.parse(
readFileSync(join(FIXTURES, `r4/reports/${name}.json`), 'utf8'),
)
if (name !== 'demo') report.sheet = demo.report.sheet
let rss = process.memoryUsage().rss
let started = performance.now()
const ts = await exportReportArchive({
report,
sheet: demo.sheetSnapshot,
branding: demo.branding,
labels: demo.labels,
locale: 'fr',
rasterizer: async () => new Uint8Array(TWO_BY_ONE_PNG),
exportedAt: FIXED_EXPORTED_AT,
})
const tsMs = performance.now() - started
const tsRss = (process.memoryUsage().rss - rss) / 1024 / 1024
const input = JSON.stringify({
...demo,
report,
locale: 'fr',
exportedAt: FIXED_EXPORTED_AT.toISOString(),
})
rss = process.memoryUsage().rss
started = performance.now()
const rust = proofs.export_pdf_for_test(
input,
rastersFor(report, TWO_BY_ONE_PNG),
)
const rustMs = performance.now() - started
const rustRss = (process.memoryUsage().rss - rss) / 1024 / 1024
rows.push(
`| ${name} | ${tsMs.toFixed(0)} | ${tsRss.toFixed(1)} | ${ts.pdf.byteLength} | ${rustMs.toFixed(0)} | ${rustRss.toFixed(1)} | ${rust.byteLength} |`,
)
expect(
(await readReportArchive(rust, map)).report.observations.length,
).toBe(report.observations.length)
expect(rustMs).toBeLessThan(tsMs)
}
console.info(
`[core-wasm] R4 writer vs TS engine (Node, stub rasters)\n${rows.join('\n')}`,
)
}, 300_000)
})
+1 -1
View File
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export type ExportErrorKind = "snapshot_invalid" | "snapshot_too_large" | "sheet_mismatch" | "too_many_pages" | "branding_invalid";
export type ExportErrorKind = "snapshot_invalid" | "snapshot_too_large" | "sheet_mismatch" | "too_many_pages" | "branding_invalid" | "self_check_failed" | "invalid_input";
+3
View File
@@ -152,6 +152,9 @@ importers:
specifier: ^3.24.1
version: 3.25.76
devDependencies:
'@faunosteo/pdf':
specifier: workspace:*
version: link:../pdf
'@types/node':
specifier: ^20.19.41
version: 20.19.41