From 7bf87caa803c82dbbfec1c442014a1997c73f7da Mon Sep 17 00:00:00 2001 From: krflol Date: Fri, 30 Oct 2026 16:36:38 -0500 Subject: [PATCH 1/3] Add compiled document batches across JavaScript runtimes --- .github/workflows/ci.yml | 11 ++ README.md | 68 ++++++++- engine/Cargo.lock | 102 +++++++++++++- engine/Cargo.toml | 5 +- engine/src/main.rs | 106 +++++++++++++- package-lock.json | 4 +- package.json | 4 +- src/batch-report.cjs | 46 +++++++ src/batch.d.ts | 23 ++++ src/browser.d.ts | 8 ++ src/browser.js | 14 +- src/cloudflare.d.ts | 8 ++ src/cloudflare.js | 19 ++- src/engine-runner.js | 13 +- src/index.cjs | 24 +++- src/index.d.ts | 11 ++ src/index.js | 2 +- src/process-worker.cjs | 7 +- src/render-input.cjs | 53 ++++++- src/render-queue.cjs | 7 +- src/ui.d.ts | 6 +- test/batch-fixtures.mjs | 41 ++++++ test/batch-input.test.mjs | 58 ++++++++ test/browser/harness.js | 8 +- test/cloudflare/baseline.mjs | 17 ++- test/cloudflare/cases.mjs | 15 +- test/cloudflare/worker.mjs | 10 +- test/default-styles.test.mjs | 7 +- test/fixtures/process-driver.cjs | 6 +- test/process-isolation.test.mjs | 20 ++- test/render-batch.test.mjs | 206 ++++++++++++++++++++++++++++ test/test_batch_release.py | 73 ++++++++++ test/types/browser.mts | 8 ++ test/types/cloudflare.mts | 10 +- test/types/import.mts | 15 +- test/types/require.cts | 5 + test/types/ui-render.mts | 9 +- tools/build.mjs | 3 +- tools/check-browser.py | 45 +++++- tools/check-build.mjs | 4 +- tools/check-cloudflare.mjs | 12 +- tools/check-installed-batches.mjs | 70 ++++++++++ tools/prepare-browser-check.mjs | 12 +- tools/radial_gradient_inspection.py | 3 +- tools/render-hidden-text.mjs | 5 +- tools/verify-batches.py | 98 +++++++++++++ tools/verify-engine-paint.py | 4 +- tools/verify-generated-quotes.py | 3 +- tools/verify-hidden-text.py | 6 +- tools/verify-native.mjs | 25 +++- tools/verify-pack.mjs | 23 +++- tools/verify-radial-gradients.py | 2 +- tools/verify-release.py | 66 ++++++++- tools/verify-text-decorations.py | 4 +- 54 files changed, 1335 insertions(+), 99 deletions(-) create mode 100644 src/batch-report.cjs create mode 100644 src/batch.d.ts create mode 100644 test/batch-fixtures.mjs create mode 100644 test/batch-input.test.mjs create mode 100644 test/render-batch.test.mjs create mode 100644 test/test_batch_release.py create mode 100644 tools/check-installed-batches.mjs create mode 100644 tools/verify-batches.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c3a717..3863319 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,6 +37,8 @@ jobs: with: python-version: '3.12' - run: python -m pip install pypdf==6.19.0 pypdfium2==5.14.0 fonttools==4.65.0 pillow==12.3.0 + - name: Read installed batch outputs and actual README examples independently + run: python tools/verify-batches.py - name: Verify the packed Workers entry in local workerd run: | npm run verify:cloudflare @@ -144,6 +146,11 @@ jobs: output/native-verification/**/*.png output/native-verification/**/input.html output/native-verification/**/style.css + output/native-verification/**/records.json + output/native-verification/**/result.json + output/batch-verification/ + output/pack-verification/batch-tests.log + output/pack-verification/installed-batches.log output/pack-verification/verification.json output/pack-verification/text-extraction.json output/pack-verification/queue-tests.log @@ -251,6 +258,7 @@ jobs: npm run render:radial npm run verify:radial python tools/verify-components.py + python tools/verify-batches.py npm run verify:cloudflare python tools/verify-cloudflare.py npm run verify:cloudflare:starter @@ -298,6 +306,9 @@ jobs: output/pack-verification/queue-tests.log output/pack-verification/default-styles-tests.log output/pack-verification/component-tests.log + output/pack-verification/batch-tests.log + output/pack-verification/installed-batches.log + output/batch-verification/ output/component-verification/ output/default-styles/ output/queue-verification/ diff --git a/README.md b/README.md index 098f8dc..9487df2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ no Python, Rust compiler, browser, or system fonts. MIT licensed. Use `fullbleed` in Node.js, `fullbleed/browser` in a web application, or [`fullbleed/cloudflare`](docs/cloudflare.md) inside a Cloudflare Worker. -Package **0.8.7** wraps the published **Fullbleed 2.5.43** engine. +Package **0.9.0** wraps the published **Fullbleed 2.5.46** engine. [Try the browser playground](https://docs.fullbleed.dev/playground/) · [Documentation](https://docs.fullbleed.dev/getting-started/node/) · @@ -38,7 +38,7 @@ await writeFile('invoice.png', result.previews[0]); Open `invoice.pdf` or its PNG preview. Inter is the default font. CommonJS also works: `const { renderPdf } = require('fullbleed')`. TypeScript declarations are included. For a pinned installation, use -`npm install --save-exact fullbleed@0.8.7`. +`npm install --save-exact fullbleed@0.9.0`. Prefer components? [Compose documents with functions or JSX](#compose-documents-with-components) and export HTML with or without styles using `fullbleed/ui`. @@ -192,6 +192,67 @@ cover the twelve Latin faces, an embedded-font control, and the blank-preview failure in public 0.3.1. Font provenance and complete license notices ship with both the npm package and copied browser runtime. +## Generate a batch from one template + +Use `renderPdfBatch()` for notices, letters, invoices, or statements that share a +design. The engine compiles the template once per request and returns one PDF in +record order, with page ranges and selected-font character reports: + +```javascript +import { writeFile } from 'node:fs/promises'; +import { renderPdfBatch } from 'fullbleed'; + +const result = await renderPdfBatch({ + html: '

Account {{account}}

{{message}}

', + css: '@page { size: A5; margin: 18mm } h1 { color: #175c52 }', + records: [ + { account: '1042', message: 'Your account is up to date.' }, + { account: '1043', message: 'Your next review is scheduled for November.' }, + ], +}); +await writeFile('notices.pdf', result.pdf); +for (const row of result.records) { + console.log(`Record ${row.recordIndex}: pages ${row.pageOffset + 1}–${row.pageOffset + row.pageCount}`); +} +``` + +`layout: 'reflow'` is the default: longer text can wrap and add pages. Choose +`layout: 'fixed'` for values that fit the compiled geometry; it retains the +template's layout and selected fonts. Records contain plain strings, including +literal `<` and `>` characters. Format numbers and dates in your application. +Every record must have the same own enumerable fields, exactly matching the +template's text bindings. Use ordinary renders for changes to document structure, +image sources, attributes, or CSS. + +Component-authored templates use the same batch function: + +```javascript +import { renderPdfBatch } from 'fullbleed'; +import { Document, Heading, Paragraph } from 'fullbleed/ui'; + +const notice = Document(Heading('Account {{account}}'), Paragraph('{{message}}')); +const result = await notice.renderPdf(renderPdfBatch, { + records: [{ account: '1042', message: 'Your account is up to date.' }], +}); +``` + +Each `result.records` entry has zero-based `recordIndex` and `pageOffset`, a +`pageCount`, and a `missingGlyphs` array. Missing characters reject the batch by +default. With `allowMissingGlyphs: true`, inspect each entry's `codepoint`, `char`, +`count`, and `fontsTried` before accepting the PDF. `fontsTried` identifies the +first selected registered font where the character was absent. The top-level +`missingGlyphs` number counts distinct missing code points across all records. +These checks exclude Base-14 fonts and text in images; they do not establish +correct shaping, layout, accessibility, or PDF conformance. + +Batches accept the other rendering options, including previews and the combined +`maxPages` limit. Up to 10,000 records are accepted. HTML, CSS, and all record +keys/values share the 4,000,000 UTF-8 byte budget (1,000,000 in Cloudflare). +The PDF, reports, and optional previews remain in memory; split large workloads +according to your host's memory and CPU budget. Compilation is per request. +The browser renderer exposes `renderer.renderPdfBatch()` and Cloudflare exports +`renderPdfBatch` from `fullbleed/cloudflare`. + ## Limit concurrent renders Create one queue per application process and reuse it across requests: @@ -291,7 +352,8 @@ Error codes include `INVALID_INPUT`, `MISSING_GLYPHS`, `PAGE_LIMIT`, `TIMEOUT`, include `exitCode` and `signal`. Failed or cancelled calls do not return a partial PDF. Caller-owned asset buffers are copied when a call starts. -`createRenderQueue({ concurrency?, maxQueue? })` exposes `renderPdf()` with the +`createRenderQueue({ concurrency?, maxQueue? })` exposes `renderPdf()` and +`renderPdfBatch()` with the same options and result. Defaults are one active render and sixteen waiting jobs. `maxQueue: 0` rejects buffering. Accepted calls snapshot their strings and asset/font bytes immediately. The queue adds `QUEUE_FULL` and `QUEUE_CLOSED`, diff --git a/engine/Cargo.lock b/engine/Cargo.lock index 2b377b0..6b5bf9f 100644 --- a/engine/Cargo.lock +++ b/engine/Cargo.lock @@ -4,9 +4,9 @@ version = 4 [[package]] name = "fullbleed" -version = "2.5.43" +version = "2.5.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83de6953d083d5479e7820d9f3235a6eda0f2288d95753e5129566459134dde4" +checksum = "833f291584597549d6c3b411e2275c5d5e67c21ea40fa8e2766210b2768a6491" dependencies = [ "fullbleed_audit_contract", "subsetter", @@ -15,9 +15,10 @@ dependencies = [ [[package]] name = "fullbleed-node-adapter" -version = "0.8.7" +version = "0.9.0" dependencies = [ "fullbleed", + "serde_json", ] [[package]] @@ -26,12 +27,84 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61400ea6ceb63537e5089dd945823a33c293d7aa0765b76239f2d8ec2ef96127" +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rustc-hash" version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + [[package]] name = "subsetter" version = "0.2.6" @@ -41,8 +114,31 @@ dependencies = [ "rustc-hash", ] +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "unicode-bidi" version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/engine/Cargo.toml b/engine/Cargo.toml index cab8601..4cd6c8a 100644 --- a/engine/Cargo.toml +++ b/engine/Cargo.toml @@ -1,12 +1,13 @@ [package] name = "fullbleed-node-adapter" -version = "0.8.7" +version = "0.9.0" edition = "2024" license = "MIT" publish = false [dependencies] -fullbleed = { version = "=2.5.43", features = ["svg_raster"] } +fullbleed = { version = "=2.5.46", features = ["svg_raster"] } +serde_json = "=1.0.154" [profile.release] lto = true diff --git a/engine/src/main.rs b/engine/src/main.rs index 38d1ded..19c86e5 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -1,5 +1,78 @@ // SPDX-License-Identifier: MIT use fullbleed::{Asset, AssetBundle, AssetKind, FullBleed}; +use serde_json::{Value, json}; +use std::collections::{HashMap, HashSet}; + +fn render_batch( + engine: &FullBleed, + html: &str, + css: &str, + reflow: bool, +) -> Result<(Vec, usize, usize, Value), Box> { + let encoded = std::fs::read("records.json")?; + // JSON escaping can expand the bounded UTF-8 source, but cannot grow without limit. + if encoded.len() > 32_000_000 { + return Err("INVALID_BINDINGS: Encoded batch is too large.".into()); + } + let records: Vec> = serde_json::from_slice(&encoded) + .map_err(|error| format!("INVALID_BINDINGS: Invalid string records: {error}"))?; + if records.is_empty() || records.len() > 10_000 { + return Err("INVALID_BINDINGS: Expected between 1 and 10,000 records.".into()); + } + let template = engine.compile_document(html, css)?; + let slots = if reflow { + template.reflow_binding_slots() + } else { + template.binding_slots() + }; + if slots.is_empty() { + return Err( + "INVALID_BINDINGS: The template must contain plain-text {{field}} bindings.".into(), + ); + } + let mut columns: HashMap> = slots + .iter() + .map(|slot| (slot.clone(), Vec::with_capacity(records.len()))) + .collect(); + for (index, mut record) in records.into_iter().enumerate() { + if record.len() != slots.len() || slots.iter().any(|slot| !record.contains_key(slot)) { + return Err(format!("INVALID_BINDINGS: Record {index} fields must exactly match the template bindings {slots:?}.").into()); + } + for slot in slots { + columns + .get_mut(slot) + .unwrap() + .push(record.remove(slot).unwrap()); + } + } + let (pdf, report) = if reflow { + template.render_reflow_bindings_to_buffer_with_report(&columns)? + } else { + template.render_bindings_to_buffer_with_report(&columns)? + }; + let mut all_missing = HashSet::new(); + let rows: Vec = report + .records + .iter() + .map(|row| { + let missing: Vec = row.glyph_report.missing().iter().map(|glyph| { + all_missing.insert(glyph.codepoint); + json!({"codepoint":glyph.codepoint,"char":glyph.ch.to_string(),"count":glyph.count, + "fontsTried":glyph.fonts_tried}) + }).collect(); + json!({"recordIndex":row.record_index,"pageOffset":row.page_offset, + "pageCount":row.page_count,"missingGlyphs":missing}) + }) + .collect(); + let pages = report.page_count; + let missing = all_missing.len(); + Ok(( + pdf, + pages, + missing, + json!({"pages":pages,"missingGlyphs":missing,"records":rows}), + )) +} fn render() -> Result<(), Box> { let mut args = std::env::args().skip(1); @@ -7,19 +80,41 @@ fn render() -> Result<(), Box> { let dpi: u32 = args.next().ok_or("Missing preview DPI")?.parse()?; let allow_missing = args.next().ok_or("Missing glyph policy")? == "allow"; let mut fonts = AssetBundle::default(); + let mut batch = None; for font in args { + if font.starts_with("--batch-") { + let reflow = match font.as_str() { + "--batch-fixed" => false, + "--batch-reflow" => true, + _ => return Err("INVALID_BINDINGS: Unknown batch layout.".into()), + }; + if batch.replace(reflow).is_some() { + return Err("INVALID_BINDINGS: Duplicate batch layout.".into()); + } + continue; + } let data = std::fs::read(&font)?; fonts.add(Asset::new(font, AssetKind::Font, data, None, true)); } let html = std::fs::read_to_string("input.html")?; let css = std::fs::read_to_string("style.css")?; let engine = FullBleed::builder().register_bundle(fonts).build()?; - let (pdf, glyphs, document) = engine.render_with_glyph_report_and_document(&html, &css)?; - let pages = document.pages.len(); + let (pdf, pages, missing, report) = if let Some(reflow) = batch { + render_batch(&engine, &html, &css, reflow)? + } else { + let (pdf, glyphs, document) = engine.render_with_glyph_report_and_document(&html, &css)?; + let pages = document.pages.len(); + let missing = glyphs.missing().len(); + ( + pdf, + pages, + missing, + json!({"pages":pages,"missingGlyphs":missing}), + ) + }; if pages == 0 || pages > max_pages { return Err(format!("PAGE_LIMIT: Rendered {pages} pages; maxPages is {max_pages}.").into()); } - let missing = glyphs.missing().len(); if missing > 0 && !allow_missing { return Err(format!("MISSING_GLYPHS: {missing} glyphs are unavailable. Supply fonts for the document's characters.").into()); } @@ -33,10 +128,7 @@ fn render() -> Result<(), Box> { std::fs::write(format!("preview-{}.png", index + 1), png)?; } } - std::fs::write( - "result.json", - format!("{{\"pages\":{pages},\"missingGlyphs\":{missing}}}"), - )?; + std::fs::write("result.json", serde_json::to_vec(&report)?)?; Ok(()) } diff --git a/package-lock.json b/package-lock.json index 61c8d4b..ddd2304 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fullbleed", - "version": "0.8.7", + "version": "0.9.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "fullbleed", - "version": "0.8.7", + "version": "0.9.0", "license": "MIT", "dependencies": { "@bjorn3/browser_wasi_shim": "0.4.2" diff --git a/package.json b/package.json index c411cf7..b488bdc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fullbleed", - "version": "0.8.7", + "version": "0.9.0", "description": "Generate PDFs from HTML/CSS, components or JSX in Node.js, browsers and Cloudflare Workers, with bundled fonts and PNG previews.", "type": "module", "main": "./src/index.cjs", @@ -140,7 +140,7 @@ "access": "public" }, "fullbleed": { - "engineVersion": "2.5.43" + "engineVersion": "2.5.46" }, "devDependencies": { "@types/node": "22.20.5", diff --git a/src/batch-report.cjs b/src/batch-report.cjs new file mode 100644 index 0000000..586ce64 --- /dev/null +++ b/src/batch-report.cjs @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MIT +'use strict'; + +// Validate reports before trusting worker/process replies or allocating previews. +function validBatchReport(result, job) { + if (!job.batch) return result?.records === undefined; + if (!Number.isSafeInteger(result?.pages) || result.pages < 1 || result.pages > job.maxPages + || !Number.isSafeInteger(result.missingGlyphs) || result.missingGlyphs < 0 + || !Array.isArray(result.records) || result.records.length !== job.batch.records.length) return false; + let offset = 0; + const allMissing = new Set(); + for (let index = 0; index < result.records.length; index++) { + const row = result.records[index]; + if (!row || row.recordIndex !== index || row.pageOffset !== offset + || !Number.isSafeInteger(row.pageCount) || row.pageCount < 1 + || row.pageCount > result.pages - offset || !Array.isArray(row.missingGlyphs)) return false; + offset += row.pageCount; + const seen = new Set(); + for (const glyph of row.missingGlyphs) { + if (!glyph || !Number.isInteger(glyph.codepoint) || glyph.codepoint < 0 || glyph.codepoint > 0x10ffff + || (glyph.codepoint >= 0xd800 && glyph.codepoint <= 0xdfff) + || glyph.char !== String.fromCodePoint(glyph.codepoint) + || !Number.isSafeInteger(glyph.count) || glyph.count < 1 + || !Array.isArray(glyph.fontsTried) || !glyph.fontsTried.length + || !Array.from(glyph.fontsTried).every(font => typeof font === 'string' && font.length > 0) + || seen.has(glyph.codepoint)) return false; + seen.add(glyph.codepoint); + allMissing.add(glyph.codepoint); + } + } + return offset === result.pages && allMissing.size === result.missingGlyphs + && (job.allowMissingGlyphs || allMissing.size === 0); +} + +const batchResult = (result, job) => job.batch ? { records: result.records } : {}; +function validRenderResult(result, job) { + return result?.pdf instanceof Uint8Array && [37, 80, 68, 70, 45].every((byte, i) => result.pdf[i] === byte) + && Number.isSafeInteger(result.pages) && result.pages > 0 && result.pages <= job.maxPages + && Number.isSafeInteger(result.missingGlyphs) && result.missingGlyphs >= 0 + && (job.allowMissingGlyphs || result.missingGlyphs === 0) + && Array.isArray(result.previews) && result.previews.length === (job.previewDpi ? result.pages : 0) + && Array.from(result.previews).every(bytes => bytes instanceof Uint8Array + && [137, 80, 78, 71, 13, 10, 26, 10].every((byte, i) => bytes[i] === byte)) + && validBatchReport(result, job); +} +module.exports = { validBatchReport, validRenderResult, batchResult }; diff --git a/src/batch.d.ts b/src/batch.d.ts new file mode 100644 index 0000000..2e51c5b --- /dev/null +++ b/src/batch.d.ts @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: MIT +export interface BatchOptions { + /** Up to 10,000 records with identical own enumerable fields and string values. Values are plain text. */ + records: readonly Readonly>[]; + /** Default reflow: text can wrap and change page counts. Fixed retains compiled geometry and fonts. */ + layout?: 'fixed' | 'reflow'; +} +export interface MissingGlyph { + codepoint: number; + char: string; + count: number; + /** The first selected registered font where the character was absent. */ + fontsTried: string[]; +} +export interface BatchRecordReport { + /** Zero-based position in the input records. */ + recordIndex: number; + /** Zero-based first page in the combined PDF. */ + pageOffset: number; + pageCount: number; + /** Selected registered-font coverage only; excludes Base-14/image text and is not a conformance check. */ + missingGlyphs: MissingGlyph[]; +} diff --git a/src/browser.d.ts b/src/browser.d.ts index 031e18a..4186934 100644 --- a/src/browser.d.ts +++ b/src/browser.d.ts @@ -1,4 +1,6 @@ // SPDX-License-Identifier: MIT +import type { BatchOptions, BatchRecordReport } from './batch.js'; +export type { BatchOptions, BatchRecordReport, MissingGlyph } from './batch.js'; export interface BrowserRendererOptions { /** Same-origin directory URL ending in /, containing the copied browser assets. */ assetBaseUrl: string | URL; @@ -29,6 +31,12 @@ export interface BrowserRenderResult { } export interface BrowserRenderer { renderPdf(options: BrowserRenderOptions): Promise; + /** Compile once per request; template and records share the 4,000,000 UTF-8 byte limit. */ + renderPdfBatch(options: BrowserBatchRenderOptions): Promise; +} +export interface BrowserBatchRenderOptions extends BrowserRenderOptions, BatchOptions {} +export interface BrowserBatchRenderResult extends BrowserRenderResult { + records: BatchRecordReport[]; } export class FullbleedError extends Error { readonly code: string; diff --git a/src/browser.js b/src/browser.js index 1700636..af295bb 100644 --- a/src/browser.js +++ b/src/browser.js @@ -1,5 +1,6 @@ // SPDX-License-Identifier: MIT import input from './render-input.cjs'; +import reports from './batch-report.cjs'; export const version = __FULLBLEED_PACKAGE_VERSION__; export const engineVersion = __FULLBLEED_ENGINE_VERSION__; @@ -23,9 +24,9 @@ export function createRenderer(options) { const entry = new URL('worker.js', base); entry.searchParams.set('v', version); - async function renderPdf(options) { + async function render(options, batch) { const start = Date.now(); - const job = input.normalize(options, { browser: true }); + const job = input.normalize(options, { browser: true, batch }); const aborted = () => new FullbleedError('ABORTED', 'PDF rendering was aborted.'); if (job.signal?.aborted) throw aborted(); const remaining = job.timeoutMs - (Date.now() - start); @@ -69,6 +70,7 @@ export function createRenderer(options) { html: job.html, css: input.stylesheet(job), fonts: job.customFonts, assets: job.assetFiles, previewDpi: job.previewDpi, maxPages: job.maxPages, allowMissingGlyphs: job.allowMissingGlyphs, + ...(job.batch ? { batch: job.batch } : {}), } }); } catch (cause) { finish(new FullbleedError('WORKER_FAILED', 'Could not send the document to the rendering worker.', { cause })); } return; @@ -87,11 +89,13 @@ export function createRenderer(options) { || !Number.isSafeInteger(data.pages) || data.pages < 1 || data.pages > job.maxPages || !Array.isArray(data.previews) || data.previews.length !== (job.previewDpi ? data.pages : 0) || !data.previews.every(bytes => bytes instanceof Uint8Array && [137, 80, 78, 71, 13, 10, 26, 10].every((value, i) => bytes[i] === value)) - || !Number.isSafeInteger(data.missingGlyphs) || data.missingGlyphs < 0 || data.engineVersion !== engineVersion) { + || !Number.isSafeInteger(data.missingGlyphs) || data.missingGlyphs < 0 || data.engineVersion !== engineVersion + || !reports.validRenderResult(data, job)) { finish(new FullbleedError('WORKER_FAILED', 'The rendering worker returned an invalid result.')); return; } - finish(null, { pdf: data.pdf, previews: data.previews, pages: data.pages, missingGlyphs: data.missingGlyphs, engineVersion }); + finish(null, { pdf: data.pdf, previews: data.previews, pages: data.pages, missingGlyphs: data.missingGlyphs, engineVersion, + ...reports.batchResult(data, job) }); }); if (job.signal?.aborted) abort(); } catch (cause) { @@ -99,5 +103,5 @@ export function createRenderer(options) { } }); } - return Object.freeze({ renderPdf }); + return Object.freeze({ renderPdf: options => render(options, false), renderPdfBatch: options => render(options, true) }); } diff --git a/src/cloudflare.d.ts b/src/cloudflare.d.ts index 3395105..3017fe9 100644 --- a/src/cloudflare.d.ts +++ b/src/cloudflare.d.ts @@ -1,4 +1,6 @@ // SPDX-License-Identifier: MIT +import type { BatchOptions, BatchRecordReport } from './batch.js'; +export type { BatchOptions, BatchRecordReport, MissingGlyph } from './batch.js'; export interface CloudflareRenderOptions { /** HTML and CSS together are limited to 1,000,000 UTF-8 bytes. */ html: string; @@ -28,5 +30,11 @@ export class FullbleedError extends Error { } /** Runs synchronously in the isolate and returns a settled Promise. No SDK cancellation or timeout. */ export function renderPdf(options: CloudflareRenderOptions): Promise; +export interface CloudflareBatchRenderOptions extends CloudflareRenderOptions, BatchOptions {} +export interface CloudflareBatchRenderResult extends CloudflareRenderResult { + records: BatchRecordReport[]; +} +/** Compile once per request. Template and records share the 1,000,000 UTF-8 byte limit; platform CPU limits apply. */ +export function renderPdfBatch(options: CloudflareBatchRenderOptions): Promise; export const version: string; export const engineVersion: string; diff --git a/src/cloudflare.js b/src/cloudflare.js index 5f3d4c8..4329045 100644 --- a/src/cloudflare.js +++ b/src/cloudflare.js @@ -5,6 +5,7 @@ import serif from '../assets/fonts/DMSerifDisplay-Regular.ttf'; import serifItalic from '../assets/fonts/DMSerifDisplay-Italic.ttf'; import display from '../assets/fonts/BebasNeue-Regular.ttf'; import input from './render-input.cjs'; +import reports from './batch-report.cjs'; import { runEngineSync } from './engine-runner.js'; export const version = __FULLBLEED_PACKAGE_VERSION__; @@ -23,21 +24,31 @@ if (!(module instanceof WebAssembly.Module) || !WebAssembly.Module.imports(modul /** Render inside the current Worker isolate. CPU deadlines are platform-owned. */ export async function renderPdf(options) { - const job = input.normalize(options, { browser: true, interruptible: false, + return render(options, false); +} + +export async function renderPdfBatch(options) { + return render(options, true); +} + +function render(options, batch) { + const job = input.normalize(options, { browser: true, interruptible: false, batch, maxSourceBytes: 1_000_000, maxAssetBytes: 8 * 1024 * 1024, defaultMaxPages: 20 }); // Deliberately no await before or during the render: another request must not // acquire a second live engine instance while this call is executing. const result = runEngineSync({ module, html: job.html, css: input.stylesheet(job), fonts: [...bundledFonts, ...job.customFonts], assets: job.assetFiles, - previewDpi: job.previewDpi, maxPages: job.maxPages, allowMissingGlyphs: job.allowMissingGlyphs }); + previewDpi: job.previewDpi, maxPages: job.maxPages, allowMissingGlyphs: job.allowMissingGlyphs, + ...(job.batch ? { batch: job.batch } : {}) }); if (!result.ok) throw new FullbleedError(result.code, result.message); if (!(result.pdf instanceof Uint8Array) || !Number.isSafeInteger(result.pages) || result.pages < 1 || result.pages > job.maxPages || !Number.isSafeInteger(result.missingGlyphs) || result.missingGlyphs < 0 || !Array.isArray(result.previews) || result.previews.length !== (job.previewDpi ? result.pages : 0) || !result.previews.every(bytes => bytes instanceof Uint8Array && - [137, 80, 78, 71, 13, 10, 26, 10].every((value, i) => bytes[i] === value))) { + [137, 80, 78, 71, 13, 10, 26, 10].every((value, i) => bytes[i] === value)) + || !reports.validRenderResult(result, job)) { throw new FullbleedError('RENDER_FAILED', 'The rendering engine returned an invalid result.'); } return { pdf: result.pdf, previews: result.previews, pages: result.pages, - missingGlyphs: result.missingGlyphs, engineVersion }; + missingGlyphs: result.missingGlyphs, engineVersion, ...reports.batchResult(result, job) }; } diff --git a/src/engine-runner.js b/src/engine-runner.js index c77dd0c..6d201a9 100644 --- a/src/engine-runner.js +++ b/src/engine-runner.js @@ -1,5 +1,6 @@ // SPDX-License-Identifier: MIT import { WASI, File, OpenFile, Directory, ConsoleStdout, PreopenDirectory } from '@bjorn3/browser_wasi_shim'; +import reports from './batch-report.cjs'; // A compiled module can be instantiated synchronously. Keeping the entire // in-process call synchronous avoids overlapping Wasm instances in one Worker @@ -19,16 +20,18 @@ export function runEngineSync(job) { const encoder = new TextEncoder(); put('input.html', encoder.encode(job.html)); put('style.css', encoder.encode(job.css)); + if (job.batch) put('records.json', encoder.encode(JSON.stringify(job.batch.records))); for (const file of [...job.fonts, ...job.assets]) put(file.name, file.data); let log = ''; const collect = line => { if (log.length < 4000) log += String(line).slice(0, 4000 - log.length) + '\n'; }; - const wasi = new WASI(['fullbleed', String(job.maxPages), String(job.previewDpi), job.allowMissingGlyphs ? 'allow' : 'reject', ...job.fonts.map(f => f.name)], [], [ + const wasi = new WASI(['fullbleed', String(job.maxPages), String(job.previewDpi), job.allowMissingGlyphs ? 'allow' : 'reject', + ...job.fonts.map(f => f.name), ...(job.batch ? ['--batch-' + job.batch.layout] : [])], [], [ new OpenFile(new File([])), ConsoleStdout.lineBuffered(collect), ConsoleStdout.lineBuffered(collect), root, ], { debug: false }); const instance = new WebAssembly.Instance(job.module, { wasi_snapshot_preview1: wasi.wasiImport }); const status = wasi.start(instance); if (status !== 0) { - const code = ['PAGE_LIMIT', 'MISSING_GLYPHS', 'PREVIEW_FAILED'].find(c => log.includes(c + ':')) ?? 'RENDER_FAILED'; + const code = ['PAGE_LIMIT', 'MISSING_GLYPHS', 'PREVIEW_FAILED', 'INVALID_BINDINGS'].find(c => log.includes(c + ':')) ?? 'RENDER_FAILED'; return { ok: false, code, message: log.trim() || `The engine exited with code ${status}.` }; } else { const read = name => { @@ -37,10 +40,14 @@ export function runEngineSync(job) { return Uint8Array.from(file.data); }; const report = JSON.parse(new TextDecoder().decode(read('result.json'))); + if (!Number.isSafeInteger(report.pages) || report.pages < 1 || report.pages > job.maxPages + || !reports.validBatchReport(report, job)) throw new Error('Engine output has an invalid page or batch report.'); const pdf = read('document.pdf'); if (new TextDecoder().decode(pdf.slice(0, 5)) !== '%PDF-') throw new Error('Engine output is not a PDF.'); const previews = job.previewDpi ? Array.from({ length: report.pages }, (_, i) => read(`preview-${i + 1}.png`)) : []; - return { ok: true, pdf, previews, ...report }; + const result = { ok: true, pdf, previews, ...report }; + if (!reports.validRenderResult(result, job)) throw new Error('Engine output has an invalid result.'); + return result; } } catch (cause) { const code = cause instanceof WebAssembly.RuntimeError || cause instanceof RangeError ? 'ENGINE_LIMIT' : 'RENDER_FAILED'; diff --git a/src/index.cjs b/src/index.cjs index 565ac4f..35dd958 100644 --- a/src/index.cjs +++ b/src/index.cjs @@ -10,6 +10,7 @@ const pkg = require('../package.json'); const { FullbleedError, normalize, stylesheet } = require('./render-input.cjs'); const { createQueue } = require('./render-queue.cjs'); +const { validRenderResult, batchResult } = require('./batch-report.cjs'); const hash = bytes => createHash('sha256').update(bytes).digest('hex'); const root = join(__dirname, '..'); let runtimePromise; @@ -86,6 +87,7 @@ function renderProcess(job, remaining) { && Buffer.from(bytes.subarray(0, 8)).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) || !Number.isSafeInteger(result.missingGlyphs) || result.missingGlyphs < 0 || result.engineVersion !== pkg.fullbleed.engineVersion + || !validRenderResult(result, job) : typeof message.code !== 'string' || typeof message.message !== 'string') { stop(new FullbleedError('PROCESS_FAILED', 'The rendering process returned an invalid result.')); return; @@ -113,7 +115,7 @@ function renderProcess(job, remaining) { } else { const result = reply.result; resolve({ pdf: Buffer.from(result.pdf), previews: result.previews.map(bytes => Buffer.from(bytes)), - pages: result.pages, missingGlyphs: result.missingGlyphs, engineVersion: result.engineVersion }); + pages: result.pages, missingGlyphs: result.missingGlyphs, engineVersion: result.engineVersion, ...batchResult(result, job) }); } }; // All stdio is ignored except IPC. Waiting for both exit and disconnect @@ -133,12 +135,13 @@ function renderProcess(job, remaining) { if (!failure) abort(); return; } - child.send({ type: 'render', options: { + child.send({ type: job.batch ? 'render-batch' : 'render', options: { html: job.html, css: job.css, fonts: job.customFonts.map(font => ({ name: font.name.split('/').at(-1), data: font.data })), assets: Object.fromEntries(job.assetFiles.map(file => [file.name.slice('assets/'.length), file.data])), previewDpi: job.previewDpi, maxPages: job.maxPages, allowMissingGlyphs: job.allowMissingGlyphs, defaultStyles: job.defaultStyles, timeoutMs: remaining, + ...(job.batch ? { records: job.batch.records, layout: job.batch.layout } : {}), } }, error => { if (error) stop(new FullbleedError('PROCESS_FAILED', 'Could not send the rendering request.', { cause: error })); }); @@ -161,6 +164,13 @@ async function renderPdf(options) { return renderJob(job, start); } +async function renderPdfBatch(options) { + if (Number(process.versions.node.split('.')[0]) < 22) throw new FullbleedError('UNSUPPORTED_NODE', 'Fullbleed requires Node.js 22 or newer.'); + const start = Date.now(); + const job = normalize(options, { batch: true }); + return renderJob(job, start); +} + async function renderJob(job, start) { if (Number(process.versions.node.split('.')[0]) < 22) throw new FullbleedError('UNSUPPORTED_NODE', 'Fullbleed requires Node.js 22 or newer.'); const aborted = () => new FullbleedError('ABORTED', 'PDF rendering was aborted.'); @@ -201,6 +211,7 @@ async function renderJob(job, start) { module: loaded.module, fonts: [...loaded.fonts, ...job.customFonts], assets: job.assetFiles, html: job.html, css: stylesheet(job), previewDpi: job.previewDpi, maxPages: job.maxPages, allowMissingGlyphs: job.allowMissingGlyphs, + ...(job.batch ? { batch: job.batch } : {}), }, }); worker.once('error', cause => finish(new FullbleedError('WORKER_FAILED', cause.message, { cause }))); @@ -208,9 +219,13 @@ async function renderJob(job, start) { if (!settled) finish(new FullbleedError('WORKER_FAILED', `Rendering worker exited before producing a result (code ${code}).`)); }); worker.once('message', result => { - if (!result.ok) return finish(new FullbleedError(result.code, result.message)); + if (typeof result?.ok !== 'boolean') return finish(new FullbleedError('WORKER_FAILED', 'The rendering worker returned an invalid response.')); + if (!result.ok) return finish(typeof result.code === 'string' && typeof result.message === 'string' + ? new FullbleedError(result.code, result.message) + : new FullbleedError('WORKER_FAILED', 'The rendering worker returned an invalid error.')); + if (!validRenderResult(result, job)) return finish(new FullbleedError('WORKER_FAILED', 'The rendering worker returned an invalid result.')); finish(null, { pdf: Buffer.from(result.pdf), previews: result.previews.map(bytes => Buffer.from(bytes)), - pages: result.pages, missingGlyphs: result.missingGlyphs, engineVersion: pkg.fullbleed.engineVersion }); + pages: result.pages, missingGlyphs: result.missingGlyphs, engineVersion: pkg.fullbleed.engineVersion, ...batchResult(result, job) }); }); if (job.signal?.aborted) abort(); }).catch(cause => finish(cause instanceof FullbleedError ? cause : new FullbleedError('WORKER_FAILED', cause.message, { cause }))); @@ -218,6 +233,7 @@ async function renderJob(job, start) { } exports.renderPdf = renderPdf; +exports.renderPdfBatch = renderPdfBatch; exports.createRenderQueue = options => createQueue(renderJob, options); exports.FullbleedError = FullbleedError; exports.engineVersion = pkg.fullbleed.engineVersion; diff --git a/src/index.d.ts b/src/index.d.ts index df6b0cd..a240464 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,5 +1,7 @@ // SPDX-License-Identifier: MIT import type { Buffer } from 'node:buffer'; +import type { BatchOptions, BatchRecordReport } from './batch.js'; +export type { BatchOptions, BatchRecordReport, MissingGlyph } from './batch.js'; export interface RenderOptions { html: string; @@ -39,6 +41,13 @@ export class FullbleedError extends Error { constructor(code: string, message: string, options?: ErrorOptions); } export function renderPdf(options: RenderOptions): Promise; +export interface BatchRenderOptions extends RenderOptions, BatchOptions {} +export interface BatchRenderResult extends RenderResult { + /** Input order, with page ranges in the combined PDF. missingGlyphs totals distinct code points across records. */ + records: BatchRecordReport[]; +} +/** Compile once per request; HTML, CSS and record keys/values share the 4,000,000 UTF-8 byte limit. */ +export function renderPdfBatch(options: BatchRenderOptions): Promise; export interface RenderQueueOptions { /** Maximum simultaneous renders in this queue. Default: 1. */ concurrency?: number; @@ -48,6 +57,8 @@ export interface RenderQueueOptions { export interface RenderQueue { /** Render in FIFO start order, or reject with QUEUE_FULL/QUEUE_CLOSED. Inputs are copied at admission. */ renderPdf(options: RenderOptions): Promise; + /** Shares FIFO admission, concurrency, deadlines and cleanup with ordinary renders. */ + renderPdfBatch(options: BatchRenderOptions): Promise; /** Stop admission, cancel accepted jobs and await worker/process cleanup. Idempotent. */ close(): Promise; readonly concurrency: number; diff --git a/src/index.js b/src/index.js index ab1ed39..7068ed9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,3 @@ // SPDX-License-Identifier: MIT import api from './index.cjs'; -export const { renderPdf, createRenderQueue, FullbleedError, engineVersion, version } = api; +export const { renderPdf, renderPdfBatch, createRenderQueue, FullbleedError, engineVersion, version } = api; diff --git a/src/process-worker.cjs b/src/process-worker.cjs index b0c3ae1..6f4292b 100644 --- a/src/process-worker.cjs +++ b/src/process-worker.cjs @@ -1,13 +1,14 @@ // SPDX-License-Identifier: MIT 'use strict'; -const { renderPdf } = require('./index.cjs'); +const { renderPdf, renderPdfBatch } = require('./index.cjs'); const controller = new AbortController(); process.once('disconnect', () => controller.abort()); process.once('message', async message => { let reply; try { - if (message?.type !== 'render') throw new Error('Invalid render request.'); - const result = await renderPdf({ ...message.options, isolation: 'worker', signal: controller.signal }); + if (message?.type !== 'render' && message?.type !== 'render-batch') throw new Error('Invalid render request.'); + const render = message.type === 'render-batch' ? renderPdfBatch : renderPdf; + const result = await render({ ...message.options, isolation: 'worker', signal: controller.signal }); reply = { type: 'result', ok: true, result }; } catch (error) { reply = { type: 'result', ok: false, code: error.code ?? 'RENDER_FAILED', message: error.message }; diff --git a/src/render-input.cjs b/src/render-input.cjs index 9f1d0a4..2d565ab 100644 --- a/src/render-input.cjs +++ b/src/render-input.cjs @@ -11,11 +11,56 @@ class FullbleedError extends Error { const invalid = message => { throw new FullbleedError('INVALID_INPUT', message); }; const sourceLimit = 4_000_000; const assetLimit = 64 * 1024 * 1024; +const recordLimit = 10_000; + +function snapshotBatch(options, sourceBytes, maxSourceBytes) { + const { records, layout = 'reflow' } = options; + if (layout !== 'fixed' && layout !== 'reflow') invalid('layout must be fixed or reflow.'); + const recordCount = Array.isArray(records) ? records.length : undefined; + if (!Number.isSafeInteger(recordCount) || recordCount < 1 || recordCount > recordLimit) { + invalid(`records must be a nonempty array with at most ${recordLimit.toLocaleString('en-US')} records.`); + } + const encoder = new TextEncoder(); + const text = (value, label) => { + if (typeof value !== 'string') invalid(`${label} must be a string; format numbers and dates before rendering.`); + for (const character of value) { + const point = character.codePointAt(0); + if (point >= 0xd800 && point <= 0xdfff) invalid(`${label} contains an unpaired UTF-16 surrogate.`); + } + sourceBytes += encoder.encode(value).byteLength; + if (sourceBytes > maxSourceBytes) invalid(`HTML, CSS and record keys/values together must not exceed ${maxSourceBytes.toLocaleString('en-US')} UTF-8 bytes.`); + return value; + }; + let fields; + const copied = []; + for (let index = 0; index < recordCount; index++) { + if (!Object.hasOwn(records, index)) invalid(`records[${index}] is missing.`); + const row = records[index]; + if (!row || typeof row !== 'object' || Array.isArray(row)) invalid(`records[${index}] must map field names to strings.`); + if (Object.getOwnPropertySymbols(row).some(key => Object.prototype.propertyIsEnumerable.call(row, key))) { + invalid(`records[${index}] field names must be strings.`); + } + const names = Object.keys(row).sort(); + if (!names.length || names.some(name => !name)) invalid(`records[${index}] must have nonempty field names.`); + if (!fields) fields = names; + else if (names.length !== fields.length || names.some((name, i) => name !== fields[i])) { + invalid(`records[${index}] must have the same fields as records[0].`); + } + const copy = Object.create(null); + for (const name of names) { + text(name, `records[${index}] field name`); + copy[name] = text(row[name], `records[${index}][${JSON.stringify(name)}]`); + } + copied.push(copy); + } + return { records: copied, layout }; +} function normalize(options, { browser = false, interruptible = true, - maxSourceBytes = sourceLimit, maxAssetBytes = assetLimit, defaultMaxPages = 1000 } = {}) { + maxSourceBytes = sourceLimit, maxAssetBytes = assetLimit, defaultMaxPages = 1000, batch = false } = {}) { if (!options || typeof options !== 'object' || Array.isArray(options)) invalid('Pass an options object with an html string.'); const supported = new Set(['html', 'css', 'defaultStyles', 'fonts', 'assets', 'previewDpi', 'maxPages', 'allowMissingGlyphs']); + if (batch) { supported.add('records'); supported.add('layout'); } if (interruptible) { supported.add('timeoutMs'); supported.add('signal'); } if (!browser) supported.add('isolation'); for (const key of Object.keys(options)) if (!supported.has(key)) invalid(`Unknown option: ${key}`); @@ -23,7 +68,8 @@ function normalize(options, { browser = false, interruptible = true, if (typeof html !== 'string' || !html.trim()) invalid('html must be a nonempty string.'); if (typeof css !== 'string') invalid('css must be a string.'); if (typeof defaultStyles !== 'boolean') invalid('defaultStyles must be a boolean.'); - if (new TextEncoder().encode(html).byteLength + new TextEncoder().encode(css).byteLength > maxSourceBytes) { + const sourceBytes = new TextEncoder().encode(html).byteLength + new TextEncoder().encode(css).byteLength; + if (sourceBytes > maxSourceBytes) { invalid(`HTML and CSS together must not exceed ${maxSourceBytes.toLocaleString('en-US')} UTF-8 bytes.`); } if (!Number.isInteger(previewDpi) || (previewDpi !== 0 && (previewDpi < 36 || previewDpi > 300))) invalid('previewDpi must be 0 (disabled) or an integer from 36 to 300.'); @@ -73,7 +119,8 @@ function normalize(options, { browser = false, interruptible = true, for (let i = 1; i < segments.length; i++) if (fileNames.has(segments.slice(0, i).join('/'))) invalid(`Asset is both a file and a directory: ${name}`); return { name: 'assets/' + name, data: bytes(assets[name], `assets[${name}]`) }; }); - return { html, css, defaultStyles, customFonts, assetFiles, previewDpi, timeoutMs, maxPages, allowMissingGlyphs, signal, isolation }; + return { html, css, defaultStyles, customFonts, assetFiles, previewDpi, timeoutMs, maxPages, allowMissingGlyphs, signal, isolation, + ...(batch ? { batch: snapshotBatch(options, sourceBytes, maxSourceBytes) } : {}) }; } function stylesheet(job) { diff --git a/src/render-queue.cjs b/src/render-queue.cjs index c6063a4..cfb3b58 100644 --- a/src/render-queue.cjs +++ b/src/render-queue.cjs @@ -76,12 +76,12 @@ function createQueue(run, options = {}) { } } - function renderPdf(options) { + function submit(options, batch) { let error = admissionError(); if (error) return Promise.reject(error); const start = Date.now(); let job; - try { job = normalize(options); } catch (error) { return Promise.reject(error); } + try { job = normalize(options, { batch }); } catch (error) { return Promise.reject(error); } // Getters on the input can invoke application code during normalization. // Recheck admission before retaining the copied document buffers. error = admissionError(); @@ -114,7 +114,8 @@ function createQueue(run, options = {}) { } return Object.freeze({ - renderPdf, close, concurrency, maxQueue, + renderPdf: options => submit(options, false), renderPdfBatch: options => submit(options, true), + close, concurrency, maxQueue, get activeCount() { return active.size; }, get pendingCount() { return pending.length; }, get closed() { return closed; }, diff --git a/src/ui.d.ts b/src/ui.d.ts index c968dc1..36d8971 100644 --- a/src/ui.d.ts +++ b/src/ui.d.ts @@ -129,6 +129,8 @@ export interface DocumentOptions { export type RenderInput = { html: string; css?: string }; export type Renderer = ((options: O) => R) | { renderPdf(options: O): R }; export type DocumentRenderOptions = Omit & { styles?: SheetFragment; html?: never; css?: never }; +export type DocumentRenderArguments = {} extends DocumentRenderOptions + ? [options?: DocumentRenderOptions] : [options: DocumentRenderOptions]; export class DocumentArtifact { readonly title: string; readonly lang: string; @@ -143,13 +145,13 @@ export class DocumentArtifact { */ toHtml(options?: HtmlExportOptions): string; toCss(...overrides: SheetFragment[]): string; - renderPdf(renderer: Renderer, options?: DocumentRenderOptions): R; + renderPdf(renderer: Renderer, ...options: DocumentRenderArguments): R; } export function Document(options: DocumentOptions, ...children: Child[]): DocumentArtifact; export function Document(...children: Child[]): DocumentArtifact; export const document: typeof Document; /** Render JSX or function-composed documents through an explicit engine function, queue or browser renderer. */ -export function renderDocument(node: Renderable, renderer: Renderer, options?: DocumentRenderOptions): R; +export function renderDocument(node: Renderable, renderer: Renderer, ...options: DocumentRenderArguments): R; export class Style { constructor(...fragments: StyleFragment[]); merge(...fragments: StyleFragment[]): this; diff --git a/test/batch-fixtures.mjs b/test/batch-fixtures.mjs new file mode 100644 index 0000000..881fc96 --- /dev/null +++ b/test/batch-fixtures.mjs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: MIT +import { document, Heading, Paragraph } from 'fullbleed/ui'; + +export const batchShort = { + html: '

Account {{account}}

{{message}}

END {{account}}

', + css: '@page {size:220pt 160pt; margin:16pt} body {font:10pt Inter;line-height:12pt} h1 {font-size:14pt;margin:0 0 6pt} p {margin:6pt 0}', + records: [{ account: '1042', message: 'Active.' }, { account: '1043', message: 'Review scheduled.' }], +}; +export const batchLong = { ...batchShort, records: [batchShort.records[0], + { account: '1043', message: 'This longer notice continues onto another page. '.repeat(20) }] }; +export const batchComponentTemplate = document({ title: 'Batch notices', styles: '@page {size:A5;margin:18mm} body{font-family:Inter}' }, + Heading('Account {{account}}'), Paragraph('{{message}}')); +const missing = String.fromCodePoint(0x10ffff); +const literal = Object.fromEntries([['__proto__', 'Café & literal "quoted" \\ path']]); +const fallback = { ...batchShort, + css: batchShort.css + ' body {font-family:Inter,"Noto Sans Math"}', + records: [{ account: '1042', message: 'ASCII math' }, { account: '1043', message: 'Integral: ⨌' }], +}; + +// Pure fixture data can be copied into an installed consumer or browser harness. +// Each runtime loads fontFiles explicitly; no fixture fetches its own assets. +export const batchFixtures = [ + { name: 'batch-fixed', pages: 2, recordPages: [1, 1], missingGlyphs: 0, missingRows: [[], []], + options: { ...batchShort, layout: 'fixed', previewDpi: 72 } }, + { name: 'batch-reflow', pages: 5, recordPages: [1, 4], missingGlyphs: 0, missingRows: [[], []], + options: { ...batchLong, previewDpi: 72 } }, + { name: 'batch-missing', pages: 2, recordPages: [1, 1], missingGlyphs: 1, + missingRows: [[{ codepoint: 0x10ffff, count: 1 }], [{ codepoint: 0x10ffff, count: 3 }]], + options: { ...batchShort, records: [{ account: '1042', message: missing }, { account: '1043', message: missing.repeat(3) }], + allowMissingGlyphs: true, previewDpi: 0 } }, + { name: 'batch-components', pages: 2, recordPages: [1, 1], missingGlyphs: 0, missingRows: [[], []], + options: { html: batchComponentTemplate.toHtml(), css: batchComponentTemplate.toCss(), records: batchShort.records, + defaultStyles: false, previewDpi: 72 } }, + { name: 'batch-literal', pages: 1, recordPages: [1], missingGlyphs: 0, missingRows: [[]], + options: { html: '

{{__proto__}}

', css: '@page {size:A5;margin:18mm}', records: [literal], previewDpi: 72 } }, + { name: 'batch-fallback-fixed', pages: 2, recordPages: [1, 1], missingGlyphs: 1, + missingRows: [[], [{ codepoint: 0x2a0c, count: 1 }]], fontFiles: ['NotoSansMath-Regular.ttf'], + options: { ...fallback, layout: 'fixed', allowMissingGlyphs: true, previewDpi: 72 } }, + { name: 'batch-fallback-reflow', pages: 2, recordPages: [1, 1], missingGlyphs: 0, missingRows: [[], []], + fontFiles: ['NotoSansMath-Regular.ttf'], options: { ...fallback, layout: 'reflow', previewDpi: 72 } }, +]; diff --git a/test/batch-input.test.mjs b/test/batch-input.test.mjs new file mode 100644 index 0000000..9e572da --- /dev/null +++ b/test/batch-input.test.mjs @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: MIT +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { createRenderQueue, renderPdf, renderPdfBatch, FullbleedError } from 'fullbleed'; + +const base = { html: '

{{account}}

', records: [{ account: '1042' }] }; +const invalid = error => error instanceof FullbleedError && error.code === 'INVALID_INPUT'; +const cases = [ + ['no records', { ...base, records: undefined }], + ['zero records', { ...base, records: [] }], + ['too many records', { ...base, records: Array(10_001).fill({ account: '' }) }], + ['sparse records', { ...base, records: Array(1) }], + ['invalid proxy length', { ...base, records: new Proxy([{ account: '1042' }], { get(target, key, receiver) { + return key === 'length' ? NaN : Reflect.get(target, key, receiver); + } }) }], + ['non-object record', { ...base, records: [null] }], + ['array record', { ...base, records: [['1042']] }], + ['numeric value', { ...base, records: [{ account: 1042 }] }], + ['object value', { ...base, records: [{ account: { toString: () => '1042' } }] }], + ['no fields', { ...base, records: [{}] }], + ['inherited field', { ...base, records: [Object.create({ account: '1042' })] }], + ['empty field', { ...base, records: [{ '': '1042' }] }], + ['extra field in later row', { ...base, records: [{ account: '1042' }, { account: '1043', extra: '' }] }], + ['different fields', { ...base, records: [{ account: '1042' }, { client: '1043' }] }], + ['symbol field', { ...base, records: [{ account: '1042', [Symbol('extra')]: '' }] }], + ['unpaired surrogate', { ...base, records: [{ account: '\ud800' }] }], + ['invalid layout', { ...base, layout: 'auto' }], + ['unknown option', { ...base, rowCount: 1 }], + ['UTF-8 data over budget', { ...base, records: [{ account: 'é'.repeat(2_000_000) }] }], + ['combined source/data over budget', { ...base, html: '

{{account}}

' + ' '.repeat(2_000_000), records: [{ account: 'x'.repeat(2_000_000) }] }], +]; + +test('invalid batch data is rejected before starting any rendering worker', async () => { + let workers = 0; + const observe = () => workers++; + process.on('worker', observe); + try { + for (const [label, options] of cases) await assert.rejects(renderPdfBatch(options), invalid, label); + assert.equal(workers, 0); + } finally { process.off('worker', observe); } +}); + +test('ordinary renders still reject batch-only options', async () => { + await assert.rejects(renderPdf(base), invalid); + await assert.rejects(renderPdf({ html: 'Invoice', layout: 'fixed' }), invalid); +}); + +test('invalid batch admission does not occupy a queue slot', async () => { + const queue = createRenderQueue({ maxQueue: 0 }); + try { + for (const [label, options] of cases.slice(0, -2)) { + await assert.rejects(queue.renderPdfBatch(options), invalid, label); + assert.equal(queue.activeCount, 0); + assert.equal(queue.pendingCount, 0); + } + } finally { await queue.close(); } + await assert.rejects(queue.renderPdfBatch(base), error => error.code === 'QUEUE_CLOSED'); +}); diff --git a/test/browser/harness.js b/test/browser/harness.js index 7722d62..aa45434 100644 --- a/test/browser/harness.js +++ b/test/browser/harness.js @@ -34,6 +34,7 @@ const hash = async bytes => Array.from(new Uint8Array(await crypto.subtle.digest async function input(name) { const { fontFiles = [], fontNames = [], assetTexts = {}, ...options } = inputs[name]; return { ...options, + ...(options.records ? { records: structuredClone(options.records) } : {}), fonts: await Promise.all(fontFiles.map(async (file, i) => { const data = new Uint8Array(await (await fetch('./fixtures/' + file)).arrayBuffer()); return fontNames[i] ? { name: fontNames[i], data } : data; @@ -52,14 +53,19 @@ globalThis.harness = { async render(name, snapshot = false) { const options = await input(name); const pending = components.has(name) ? renderDocument(components.get(name), renderer, { defaultStyles: false, previewDpi: 96 }) - : renderer.renderPdf(options); + : options.records ? renderer.renderPdfBatch(options) : renderer.renderPdf(options); if (snapshot) { options.fonts.forEach(font => (font instanceof Uint8Array ? font : font.data).fill(0)); Object.values(options.assets).forEach(bytes => bytes.fill(0)); + if (options.records) { + for (const row of options.records) for (const name of Object.keys(row)) row[name] = 'CHANGED AFTER ADMISSION'; + options.records.push({ unexpected: 'New row' }); + } } const result = await pending; results.set(name, result); return { pages: result.pages, missingGlyphs: result.missingGlyphs, engineVersion: result.engineVersion, + ...(result.records ? { records: result.records } : {}), pdf: await hash(result.pdf), previews: await Promise.all(result.previews.map(hash)), browserBytes: result.pdf instanceof Uint8Array && result.pdf.buffer instanceof ArrayBuffer, stats: stats() }; }, diff --git a/test/cloudflare/baseline.mjs b/test/cloudflare/baseline.mjs index b6c99b7..ac68b66 100644 --- a/test/cloudflare/baseline.mjs +++ b/test/cloudflare/baseline.mjs @@ -1,18 +1,25 @@ -import { renderPdf, version, engineVersion } from 'fullbleed'; +import { renderPdf, renderPdfBatch, version, engineVersion } from 'fullbleed'; +import assert from 'node:assert/strict'; import { readFile, writeFile } from 'node:fs/promises'; import { join } from 'node:path'; import { createHash } from 'node:crypto'; import { caseNames, expected, optionsFor } from './cases.mjs'; const output = process.argv[2]; const font = new Uint8Array(await readFile(new URL('./custom-font.ttf', import.meta.url))); +const mathFont = new Uint8Array(await readFile(new URL('./math-font.ttf', import.meta.url))); const hash = bytes => createHash('sha256').update(bytes).digest('hex'); const records = []; for (const name of caseNames) { - const options = optionsFor(name, font); - const result = await renderPdf(options); - if (result.pages !== expected[name].pages || result.missingGlyphs !== 0) throw new Error('Invalid baseline: ' + name); + const options = optionsFor(name, font, mathFont); + const result = await (options.records ? renderPdfBatch : renderPdf)(options); + if (result.pages !== expected[name].pages || result.missingGlyphs !== (expected[name].missingGlyphs ?? 0)) throw new Error('Invalid baseline: ' + name); + if (options.records) { + assert.deepEqual(result.records.map(row => row.pageCount), expected[name].recordPages); + assert.deepEqual(result.records.map(row => row.missingGlyphs.map(({ codepoint, count }) => ({ codepoint, count }))), expected[name].missingRows); + } await writeFile(join(output, name + '-node.pdf'), result.pdf); for (const [i, bytes] of result.previews.entries()) await writeFile(join(output, `${name}-node-${i + 1}.png`), bytes); - records.push({ name, ...expected[name], pdf: hash(result.pdf), previews: result.previews.map(hash), missingGlyphs: result.missingGlyphs }); + records.push({ name, ...expected[name], pdf: hash(result.pdf), previews: result.previews.map(hash), missingGlyphs: result.missingGlyphs, + ...(result.records ? { records: result.records } : {}) }); } await writeFile(join(output, 'node-baseline.json'), JSON.stringify({ ok: true, version, engineVersion, records }, null, 2) + '\n'); diff --git a/test/cloudflare/cases.mjs b/test/cloudflare/cases.mjs index e67bf31..89aee38 100644 --- a/test/cloudflare/cases.mjs +++ b/test/cloudflare/cases.mjs @@ -1,8 +1,12 @@ import { Document, Heading, Paragraph, Box, Table, THead, TBody, Tr, Th, Td } from 'fullbleed/ui'; import raw from './raw.json' with { type: 'json' }; +import { batchFixtures } from '../batch-fixtures.mjs'; -export const caseNames = ['invoice', 'report', 'custom-assets', 'named-font', 'components', 'components-inline']; +export const caseNames = ['invoice', 'report', 'custom-assets', 'named-font', 'components', 'components-inline', ...batchFixtures.map(fixture => fixture.name)]; export const expected = { + ...Object.fromEntries(batchFixtures.map(fixture => [fixture.name, { pages: fixture.pages, missingGlyphs: fixture.missingGlyphs, + recordPages: fixture.recordPages, missingRows: fixture.missingRows, + text: fixture.name === 'batch-literal' ? ['Café & literal', 'quoted', 'path'] : ['Account 1042', 'Account 1043'] }])), invoice: { pages: 1, text: ['Northstar', 'NS-1042', 'Cedar & Co.', '$1,870.00'] }, report: { pages: 3, text: ['COMMON', 'GROUND', '1,240', '4,860', '$420,000.00'] }, 'custom-assets': { pages: 1, text: ['Custom font and image', 'Type Alpha with care.'], face: 'LibreCaslonText-Regular' }, @@ -11,7 +15,9 @@ export const expected = { 'components-inline': { pages: 1, text: ['Worker statement', 'Cedar & Co. safe', '$1,870.00'] }, }; -export function optionsFor(name, customFont) { +export function optionsFor(name, customFont, mathFont) { + const batch = batchFixtures.find(fixture => fixture.name === name); + if (batch) return { ...batch.options, ...(batch.fontFiles ? { fonts: [mathFont] } : {}) }; const common = { defaultStyles: false, maxPages: 20, previewDpi: 96 }; if (name === 'invoice' || name === 'report') return { ...common, ...raw[name] }; if (name === 'named-font') return { ...common, fonts: [{ name: 'Café Brand.ttf', data: customFont }], @@ -49,6 +55,11 @@ export function errorOptions(name, customFont) { case 'pages': return { ...optionsFor('report', customFont), maxPages: 1, previewDpi: 0 }; case 'default-pages': return { html: '
Page
'.repeat(21), css: 'section{break-after:page}' }; case 'glyph': return { html: '

漢字

', css: 'body{font-family:Inter}' }; + case 'batch-source-limit': return { html: '

{{value}}

', records: [{ value: 'é'.repeat(500_000) }] }; + case 'batch-fields': return { html: '

{{value}}

', records: [{ value: 'ok' }, { other: 'wrong' }] }; + case 'batch-bindings': return { html: '

{{value}}

', records: [{ other: 'wrong' }] }; + case 'batch-pages': return { ...optionsFor('batch-reflow'), maxPages: 1 }; + case 'batch-glyph': return { ...optionsFor('batch-missing'), allowMissingGlyphs: false }; default: throw new Error('Unknown error fixture'); } } diff --git a/test/cloudflare/worker.mjs b/test/cloudflare/worker.mjs index 7fc9b04..a229204 100644 --- a/test/cloudflare/worker.mjs +++ b/test/cloudflare/worker.mjs @@ -1,5 +1,6 @@ -import { renderPdf, FullbleedError, version, engineVersion } from 'fullbleed/cloudflare'; +import { renderPdf, renderPdfBatch, FullbleedError, version, engineVersion } from 'fullbleed/cloudflare'; import customFont from './custom-font.ttf'; +import mathFont from './math-font.ttf'; import { optionsFor, errorOptions } from './cases.mjs'; // The installed adapter must use static assets and a synchronous instance path. @@ -16,7 +17,7 @@ WebAssembly.Instance = function(...args) { }; const render = options => { synchronous = true; - try { return renderPdf(options); } + try { return (options.records ? renderPdfBatch : renderPdf)(options); } finally { synchronous = false; } }; const base64 = bytes => { @@ -33,10 +34,11 @@ export default { const { name, error, burst, allowMissingGlyphs } = await request.json(); const font = new Uint8Array(customFont); if (burst) { - const results = await Promise.all([render(optionsFor('invoice', font)), render(optionsFor('report', font))]); + const results = await Promise.all([render(optionsFor('invoice', font)), render(optionsFor('report', font)), + render(optionsFor('batch-reflow', font, new Uint8Array(mathFont)))]); return Response.json({ results: results.map(encode), outsideSynchronousCall, instances }); } - const options = error ? errorOptions(name, font) : optionsFor(name, font); + const options = error ? errorOptions(name, font) : optionsFor(name, font, new Uint8Array(mathFont)); if (allowMissingGlyphs) options.allowMissingGlyphs = true; const result = await render(options); return Response.json({ ...encode(result), outsideSynchronousCall, instances }); diff --git a/test/default-styles.test.mjs b/test/default-styles.test.mjs index 619dba5..36aa768 100644 --- a/test/default-styles.test.mjs +++ b/test/default-styles.test.mjs @@ -7,11 +7,13 @@ import { join } from 'node:path'; import { renderPdf, createRenderQueue, FullbleedError, version, engineVersion } from 'fullbleed'; const fixture = JSON.parse(await readFile(new URL('./fixtures/default-styles.json', import.meta.url), 'utf8')); +// Retain the original reference version and hashes across the engine upgrade. +assert.equal(fixture.engineVersion, '2.5.43'); const hash = bytes => createHash('sha256').update(bytes).digest('hex'); const records = []; const out = process.env.FULLBLEED_STYLE_EVIDENCE; async function verify(name, result, expected) { - assert.equal(result.engineVersion, fixture.engineVersion); + assert.equal(result.engineVersion, engineVersion); assert.equal(result.pages, 1); assert.equal(result.missingGlyphs, 0); assert.equal(result.previews.length, 1); @@ -65,6 +67,7 @@ after(async () => { if (!out) return; const record = { ok: records.length === fixture.cases.length * 8, packageVersion: version, engineVersion, node: process.version, platform: process.platform, fixtures: fixture.cases.length, - baseline: fixture.baseline, sourceSha256: hash(await readFile(new URL('./fixtures/default-styles.json', import.meta.url))), records }; + baseline: fixture.baseline, baselineEngineVersion: fixture.engineVersion, + sourceSha256: hash(await readFile(new URL('./fixtures/default-styles.json', import.meta.url))), records }; await writeFile(join(out, 'verification.json'), JSON.stringify(record, null, 2) + '\n'); }); diff --git a/test/fixtures/process-driver.cjs b/test/fixtures/process-driver.cjs index 7d038bd..48f2a47 100644 --- a/test/fixtures/process-driver.cjs +++ b/test/fixtures/process-driver.cjs @@ -1,14 +1,16 @@ // SPDX-License-Identifier: MIT 'use strict'; // Test-only child used to exercise the parent's IPC and shutdown handling. -const { renderPdf } = require('fullbleed'); +const { renderPdf, renderPdfBatch } = require('fullbleed'); process.once('message', async message => { const mode = process.argv[2]; if (mode === 'no-result') return process.disconnect(); if (mode === 'invalid-result') { return process.send({ type: 'result', ok: true, result: { pdf: Buffer.from('invalid') } }); } - const result = await renderPdf({ ...message.options, isolation: 'worker' }); + const render = message.type === 'render-batch' ? renderPdfBatch : renderPdf; + const result = await render({ ...message.options, isolation: 'worker' }); + if (mode === 'invalid-batch-report') result.records[0].pageOffset = 1; const reply = { type: 'result', ok: true, result }; process.send(reply, () => { if (mode === 'fail-after-result') process.exit(23); diff --git a/test/process-isolation.test.mjs b/test/process-isolation.test.mjs index 6a38c34..653aa0f 100644 --- a/test/process-isolation.test.mjs +++ b/test/process-isolation.test.mjs @@ -3,7 +3,8 @@ import assert from 'node:assert/strict'; import { ChildProcess } from 'node:child_process'; import { readFile } from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; -import { renderPdf, FullbleedError } from 'fullbleed'; +import { renderPdf, renderPdfBatch, FullbleedError } from 'fullbleed'; +import { batchShort } from './batch-fixtures.mjs'; const input = { html: '

Isolated invoice

Invoice NS-1042

', previewDpi: 96, isolation: 'process' }; const expectCode = code => error => error instanceof FullbleedError && error.code === code; @@ -93,6 +94,23 @@ test('invalid responses and missing responses reject without leaking children', processes.stopped(3); }); +test('batch results do not hide a child crash or an invalid record report', async t => { + let mode = 'fail-after-result'; + let received = 0; + const processes = observe(t, child => child.once('message', message => { + assert.equal(message.result.pdf.subarray(0, 5).toString(), '%PDF-'); + assert.equal(message.result.records.length, 2); + received++; + }), options => fixture(mode)(options)); + await assert.rejects(renderPdfBatch({ ...batchShort, isolation: 'process' }), + error => expectCode('PROCESS_FAILED')(error) && error.exitCode === 23); + processes.stopped(1); + mode = 'invalid-batch-report'; + await assert.rejects(renderPdfBatch({ ...batchShort, isolation: 'process' }), expectCode('PROCESS_FAILED')); + processes.stopped(2); + assert.equal(received, 2, 'Both controlled children must send real batch PDFs.'); +}); + test('an unavailable executable produces a structured spawn failure', async t => { const processes = observe(t, () => {}, options => ({ ...options, file: fileURLToPath(new URL('fixtures/no-such-node-executable', import.meta.url)), diff --git a/test/render-batch.test.mjs b/test/render-batch.test.mjs new file mode 100644 index 0000000..8d4edcd --- /dev/null +++ b/test/render-batch.test.mjs @@ -0,0 +1,206 @@ +// SPDX-License-Identifier: MIT +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { createHash } from 'node:crypto'; +import { mkdir, writeFile, readFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { createRenderQueue, renderPdf, renderPdfBatch, FullbleedError, version, engineVersion } from 'fullbleed'; +import { batchShort as short, batchLong as long, batchFixtures, batchComponentTemplate } from './batch-fixtures.mjs'; +const code = wanted => error => error instanceof FullbleedError && error.code === wanted; +const hash = data => createHash('sha256').update(data).digest('hex'); + +async function save(name, input, result) { + const out = process.env.FULLBLEED_BATCH_EVIDENCE; + if (!out) return; + await mkdir(out, { recursive: true }); + await writeFile(join(out, name + '.pdf'), result.pdf); + for (let i = 0; i < result.previews.length; i++) await writeFile(join(out, `${name}-${i + 1}.png`), result.previews[i]); + await writeFile(join(out, name + '.json'), JSON.stringify({ + status: 'rendered-for-independent-review', packageVersion: version, engineVersion, node: process.version, + input, pages: result.pages, missingGlyphs: result.missingGlyphs, records: result.records, + pdfSha256: hash(result.pdf), previewSha256: result.previews.map(hash), + }, null, 2) + '\n'); +} + +test('fixed batches preserve record order and match worker/process bytes and previews', async () => { + const options = { ...short, layout: 'fixed', previewDpi: 72 }; + const worker = await renderPdfBatch(options); + await save('fixed', options, worker); + assert.equal(worker.pages, 2); + assert.deepEqual(worker.records, [ + { recordIndex: 0, pageOffset: 0, pageCount: 1, missingGlyphs: [] }, + { recordIndex: 1, pageOffset: 1, pageCount: 1, missingGlyphs: [] }, + ]); + const child = await renderPdfBatch({ ...options, isolation: 'process' }); + assert.deepEqual(child, worker); +}); + +test('default reflow returns the actual page range of a longer record', async () => { + const options = { ...long, previewDpi: 72 }; + const result = await renderPdfBatch(options); + await save('reflow', options, result); + assert.deepEqual(result.records[0], { recordIndex: 0, pageOffset: 0, pageCount: 1, missingGlyphs: [] }); + assert.equal(result.records[1].recordIndex, 1); + assert.equal(result.records[1].pageOffset, 1); + assert(result.records[1].pageCount > 1); + assert.equal(result.pages, 1 + result.records[1].pageCount); + assert.equal(result.previews.length, result.pages); + const explicit = await renderPdfBatch({ ...options, layout: 'reflow' }); + assert.deepEqual(explicit, result); +}); + +test('missing-character policy rejects by default and reports distinct characters and per-record occurrences', async () => { + const missing = String.fromCodePoint(0x10ffff); + const options = { ...short, records: [{ account: '1042', message: missing }, { account: '1043', message: missing.repeat(3) }] }; + await assert.rejects(renderPdfBatch(options), code('MISSING_GLYPHS')); + const allowed = await renderPdfBatch({ ...options, allowMissingGlyphs: true }); + await save('missing', { ...options, allowMissingGlyphs: true }, allowed); + assert.equal(allowed.missingGlyphs, 1); + assert.deepEqual(allowed.records.map(row => row.missingGlyphs.map(glyph => [glyph.codepoint, glyph.char, glyph.count])), + [[[0x10ffff, missing, 1]], [[0x10ffff, missing, 3]]]); + assert(allowed.records.every(row => row.missingGlyphs[0].fontsTried.length > 0)); + assert.equal((await renderPdfBatch(short)).missingGlyphs, 0); +}); + +test('binding mismatches and the combined page limit fail before returning a PDF and permit recovery', async () => { + await assert.rejects(renderPdfBatch({ ...short, records: [{ wrong: '1042' }] }), code('INVALID_BINDINGS')); + await assert.rejects(renderPdfBatch({ ...short, html: '

No bindings

' }), code('INVALID_BINDINGS')); + await assert.rejects(renderPdfBatch({ ...short, maxPages: 1 }), code('PAGE_LIMIT')); + assert.equal((await renderPdfBatch(short)).pages, 2); +}); + +test('queued batches snapshot nested records and share FIFO slots with ordinary renders', async () => { + const options = { ...short, records: short.records.map(row => ({ ...row })) }; + const expected = await renderPdfBatch(options); + const queue = createRenderQueue({ concurrency: 1, maxQueue: 1 }); + const order = []; + try { + const first = queue.renderPdf({ html: '

First

' }).then(result => { order.push('single'); return result; }); + const second = queue.renderPdfBatch(options).then(result => { order.push('batch'); return result; }); + assert.equal(queue.activeCount, 1); + assert.equal(queue.pendingCount, 1); + options.records[0].account = 'CHANGED'; + options.records[1].message = 'Caller changed this after admission.'; + options.records.push({ account: '1044', message: 'Extra' }); + await assert.rejects(queue.renderPdfBatch(short), code('QUEUE_FULL')); + const [, actual] = await Promise.all([first, second]); + assert.deepEqual(actual, expected); + assert.deepEqual(order, ['single', 'batch']); + assert.equal(queue.activeCount, 0); + assert.equal(queue.pendingCount, 0); + } finally { await queue.close(); } +}); + +test('reentrant record getters cannot bypass a closed queue', async () => { + const queue = createRenderQueue(); + let closing; + const record = { get account() { closing = queue.close(); return '1042'; }, message: 'Active.' }; + await assert.rejects(queue.renderPdfBatch({ ...short, records: [record] }), code('QUEUE_CLOSED')); + await closing; + assert.equal(queue.activeCount, 0); + assert.equal(queue.pendingCount, 0); +}); + +test('a waiting batch retains its original deadline and starts no expired worker', async t => { + let now = 1000, workers = 0; + t.mock.method(Date, 'now', () => now); + const queue = createRenderQueue({ concurrency: 1, maxQueue: 1 }); + const listener = worker => { workers++; worker.once('online', () => { now += 2000; }); }; + process.on('worker', listener); + try { + const first = queue.renderPdf({ html: '

First

', timeoutMs: 1000 }); + const expired = assert.rejects(queue.renderPdfBatch({ ...short, timeoutMs: 1000 }), code('TIMEOUT')); + await Promise.all([first, expired]); + assert.equal(workers, 1); + assert.equal(queue.activeCount, 0); + assert.equal(queue.pendingCount, 0); + } finally { process.off('worker', listener); await queue.close(); } +}); + +test('component-authored templates pass records through the existing document adapter', async () => { + const template = batchComponentTemplate; + const options = { records: short.records, defaultStyles: false, previewDpi: 72 }; + const authored = await template.renderPdf(renderPdfBatch, options); + const explicit = await renderPdfBatch({ ...options, html: template.toHtml(), css: template.toCss() }); + assert.deepEqual(authored, explicit); + assert.equal(authored.pages, 2); + await save('components', { ...options, html: template.toHtml(), css: template.toCss() }, authored); +}); + +test('record text and prototype-shaped field names remain literal data', async () => { + const options = batchFixtures.find(fixture => fixture.name === 'batch-literal').options; + const result = await renderPdfBatch(options); + assert.equal(result.pages, 1); + assert.equal(result.missingGlyphs, 0); + assert.deepEqual(result.records, [{ recordIndex: 0, pageOffset: 0, pageCount: 1, missingGlyphs: [] }]); + await save('literal', options, result); +}); + +test('a record array length is captured once at admission', async () => { + let reads = 0; + const records = new Proxy([{ account: '1042', message: 'Active.' }], { get(target, key, receiver) { + if (key === 'length') return ++reads === 1 ? 1 : 0; + return Reflect.get(target, key, receiver); + } }); + const result = await renderPdfBatch({ ...short, records }); + assert.equal(reads, 1); + assert.equal(result.pages, 1); + assert.equal(result.records.length, 1); +}); + +test('fixed diagnostics retain selected fonts while reflow selects a registered fallback per record', async () => { + for (const fixture of batchFixtures.filter(item => item.fontFiles)) { + const fonts = await Promise.all(fixture.fontFiles.map(name => readFile(new URL('./fonts/' + name, import.meta.url)))); + const result = await renderPdfBatch({ ...fixture.options, fonts }); + await save(fixture.name.slice('batch-'.length), { ...fixture.options, fontFiles: fixture.fontFiles }, result); + assert.equal(result.pages, fixture.pages); + assert.equal(result.missingGlyphs, fixture.missingGlyphs); + assert.deepEqual(result.records.map(row => row.missingGlyphs.map(({ codepoint, count }) => ({ codepoint, count }))), fixture.missingRows); + } +}); + +test('aborting an active batch waits for worker shutdown and the queue recovers', async t => { + const controller = new AbortController(); + const queue = createRenderQueue({ maxQueue: 1 }); + let observed; + const listener = worker => { + observed ??= worker; + if (worker === observed) worker.once('online', () => controller.abort()); + }; + process.on('worker', listener); + t.after(() => process.off('worker', listener)); + try { + const cancelled = assert.rejects(queue.renderPdfBatch({ ...long, signal: controller.signal }), code('ABORTED')); + const next = queue.renderPdfBatch(short); + await cancelled; + assert(observed); + assert.equal(observed.threadId, -1); + assert.equal((await next).pages, 2); + } finally { await queue.close(); } +}); + +test('malformed batch reports from a worker reject and release that worker', async () => { + const baseline = await renderPdfBatch(short); + const mutations = [ + result => { result.records[1].recordIndex = 0; }, + result => { result.records[1].pageOffset = 0; }, + result => { result.records[1].pageCount = Number.MAX_SAFE_INTEGER; }, + result => { result.records.pop(); }, + result => { result.missingGlyphs = 1; }, + result => { result.pdf = new Uint8Array([0]); }, + result => { result.records[0].missingGlyphs = [{ codepoint: 0x110000, char: '?', count: 1, fontsTried: ['Inter'] }]; }, + ]; + for (const mutate of mutations) { + const reply = { ok: true, ...structuredClone(baseline) }; + mutate(reply); + let worker; + const listener = created => { worker = created; queueMicrotask(() => created.emit('message', reply)); }; + process.once('worker', listener); + try { + await assert.rejects(renderPdfBatch(short), code('WORKER_FAILED')); + assert(worker); + assert.equal(worker.threadId, -1); + } finally { process.off('worker', listener); } + } + assert.equal((await renderPdf({ html: '

Recovered

' })).pages, 1); +}); diff --git a/test/test_batch_release.py b/test/test_batch_release.py new file mode 100644 index 0000000..c29ed16 --- /dev/null +++ b/test/test_batch_release.py @@ -0,0 +1,73 @@ +"""Release gates reject missing batch surfaces, examples and independent evidence.""" +from copy import deepcopy +from hashlib import sha256 +import importlib.util +from pathlib import Path +import unittest + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location('batch_release_verification', ROOT / 'tools/verify-release.py') +release = importlib.util.module_from_spec(spec) +spec.loader.exec_module(release) + + +class BatchReleaseTests(unittest.TestCase): + def setUp(self): + source = sha256(release.BATCH_SOURCE.read_bytes()).hexdigest() + pages = {'batch-fixed': 2, 'batch-reflow': 5, 'batch-missing': 2, 'batch-components': 2, + 'batch-literal': 1, 'batch-fallback-fixed': 2, 'batch-fallback-reflow': 2} + fixtures = [dict(name=name, pages=count, cjsProcessAndQueueEqual=True, pdf='c' * 64) for name, count in pages.items()] + batch = dict(ok=True, queueClosed=True, tests=28, packageVersion='0.9.0', engineVersion='2.5.46', fixtures=fixtures, + readmeExamples=[dict(name='readme-batch-' + str(index), pages=count, unmodifiedExampleSha256='d' * 64) + for index, count in [(1, 2), (2, 1)]]) + reader = dict(ok=True, package_version='0.9.0', engine_version='2.5.46', tarball_sha256='a' * 64, + source_sha256=source, documents=9, pages=19, record_ranges_verified=True, glyph_reports_verified=True) + self.report = dict(package_version='0.9.0', engine_version='2.5.46', package_sha256='a' * 64, batch_source_sha256=source, + installed_matrix=[dict(tarball_sha256='a' * 64, batch_smoke=deepcopy(batch)) for _ in range(9)], + batch_readers=[dict(reader, platform=platform) for platform in ('linux', 'win32')], + browser_matrix=[dict(browser=browser, batch_cases=7, batch_source_sha256=source, batch_reports_equal=True, + batch_records_snapshot=True, batch_failure_recovery=True) for browser in ('chrome', 'firefox', 'webkit')], + native_wasi_fixtures=[dict(name=name, pages=count, batch=True, nativeWasiReportsEqual=True, + nativeWasiPdfEqual=True, nativeWasiPreviewsEqual=True) for name, count in pages.items()]) + + def test_complete_evidence_is_accepted(self): + release.verify_batch_evidence(self.report) + + def test_wrong_source_or_tarball_is_rejected(self): + for field in ('source', 'tarball'): + with self.subTest(field=field): + self.setUp() + if field == 'source': + self.report['batch_source_sha256'] = 'b' * 64 + else: + self.report['installed_matrix'][0]['tarball_sha256'] = 'b' * 64 + with self.assertRaises(ValueError): + release.verify_batch_evidence(self.report) + + def test_duplicate_cases_or_missing_readme_examples_are_rejected(self): + for field in ('fixtures', 'readmeExamples'): + with self.subTest(field=field): + self.setUp() + rows = self.report['installed_matrix'][0]['batch_smoke'][field] + rows[-1] = deepcopy(rows[0]) + with self.assertRaises(ValueError): + release.verify_batch_evidence(self.report) + + def test_missing_or_duplicate_independent_platform_is_rejected(self): + self.report['batch_readers'][1]['platform'] = 'linux' + with self.assertRaises(ValueError): + release.verify_batch_evidence(self.report) + + def test_passing_pdf_bytes_cannot_hide_bad_reports_or_cleanup(self): + for surface, field in [('native_wasi_fixtures', 'nativeWasiReportsEqual'), + ('browser_matrix', 'batch_reports_equal'), ('browser_matrix', 'batch_records_snapshot'), + ('browser_matrix', 'batch_failure_recovery'), ('batch_readers', 'glyph_reports_verified')]: + with self.subTest(surface=surface, field=field): + self.setUp() + self.report[surface][0][field] = False + with self.assertRaises(ValueError): + release.verify_batch_evidence(self.report) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/types/browser.mts b/test/types/browser.mts index bf0590f..1ee14ca 100644 --- a/test/types/browser.mts +++ b/test/types/browser.mts @@ -17,3 +17,11 @@ createRenderer({}); // @ts-expect-error Style injection is a boolean choice. renderer.renderPdf({ html: 'Hello', defaultStyles: 'false' }); void [pdf, png, versions]; +const batch = await renderer.renderPdfBatch({ html: '

{{account}}

', records: [{ account: '1042' }] as const }); +new Blob([batch.pdf], { type: 'application/pdf' }); +const firstPage: number = batch.records[0].pageOffset; +void firstPage; +// @ts-expect-error Browser batches cannot start child processes. +renderer.renderPdfBatch({ html: '

{{account}}

', records: [{ account: '1042' }], isolation: 'process' }); +// @ts-expect-error Records contain strings, not HTML/component objects. +renderer.renderPdfBatch({ html: '

{{account}}

', records: [{ account: { html: '1042' } }] }); diff --git a/test/types/cloudflare.mts b/test/types/cloudflare.mts index cf7e1c6..742e365 100644 --- a/test/types/cloudflare.mts +++ b/test/types/cloudflare.mts @@ -1,4 +1,4 @@ -import { renderPdf, FullbleedError, version, engineVersion, type CloudflareRenderOptions } from 'fullbleed/cloudflare'; +import { renderPdf, renderPdfBatch, FullbleedError, version, engineVersion, type CloudflareRenderOptions } from 'fullbleed/cloudflare'; import { document, Heading, Paragraph } from 'fullbleed/ui'; const authored = document(Heading('Worker report'), Paragraph('Component-authored PDF.')); const options: CloudflareRenderOptions = { html: authored.toHtml(), css: authored.toCss(), maxPages: 5 }; @@ -17,3 +17,11 @@ await renderPdf({ html: 'x', timeoutMs: 1000 }); await renderPdf({ html: 'x', isolation: 'worker' }); // @ts-expect-error In-isolate rendering cannot process cancellation during computation. await renderPdf({ html: 'x', signal: new AbortController().signal }); +const batch = await renderPdfBatch({ html: '

{{account}}

', records: [{ account: '1042' }], layout: 'fixed' }); +new Response(batch.pdf); +const pages: number = batch.records[0].pageCount; +void pages; +// @ts-expect-error Batch CPU deadlines remain platform-owned. +renderPdfBatch({ html: '

{{account}}

', records: [{ account: '1042' }], timeoutMs: 5000 }); +// @ts-expect-error A batch requires record data. +renderPdfBatch({ html: '

{{account}}

' }); diff --git a/test/types/import.mts b/test/types/import.mts index 7dea0ea..b37e67c 100644 --- a/test/types/import.mts +++ b/test/types/import.mts @@ -1,4 +1,4 @@ -import { renderPdf, createRenderQueue, FullbleedError, type RenderOptions, type RenderResult, type RenderQueue } from 'fullbleed'; +import { renderPdf, renderPdfBatch, createRenderQueue, FullbleedError, type RenderOptions, type RenderResult, type RenderQueue, type BatchRenderOptions, type BatchRenderResult } from 'fullbleed'; import type { Buffer } from 'node:buffer'; const options: RenderOptions = { html: '

Typed PDF

', defaultStyles: false, fonts: [new Uint8Array([1])], previewDpi: 96, isolation: 'process' }; renderPdf({ html: 'Named font', fonts: [{ name: 'brand.ttf', data: new Uint8Array([1]) }] }); @@ -23,3 +23,16 @@ renderPdf({ html: 'Hello', pdfProfile: 'pdfua1' }); renderPdf({ html: 'Hello', isolation: 'browser' }); // @ts-expect-error Style injection is a boolean choice. renderPdf({ html: 'Hello', defaultStyles: 'false' }); +const batchOptions: BatchRenderOptions = { html: '

{{account}}

', records: [{ account: '1042' }] as const, layout: 'reflow' }; +const batch: BatchRenderResult = await renderPdfBatch(batchOptions); +const batchPdf: Buffer = batch.pdf; +const pageOffset: number = batch.records[0].pageOffset; +const selectedFont: string | undefined = batch.records[0].missingGlyphs[0]?.fontsTried[0]; +await queue.renderPdfBatch(batchOptions); +void [batchPdf, pageOffset, selectedFont]; +// @ts-expect-error Format record values explicitly as strings. +renderPdfBatch({ html: '

{{account}}

', records: [{ account: 1042 }] }); +// @ts-expect-error Batch rendering requires records. +renderPdfBatch({ html: '

{{account}}

' }); +// @ts-expect-error Batch data belongs to the batch API. +renderPdf({ html: '

{{account}}

', records: [{ account: '1042' }] }); diff --git a/test/types/require.cts b/test/types/require.cts index 22ae8db..a429cc8 100644 --- a/test/types/require.cts +++ b/test/types/require.cts @@ -7,6 +7,11 @@ async function example() { const error: Error = new api.FullbleedError('EXAMPLE', 'Example'); const queue: api.RenderQueue = api.createRenderQueue({ concurrency: 1, maxQueue: 2 }); await queue.renderPdf({ html: 'Queued CommonJS' }); + const batch: api.BatchRenderResult = await api.renderPdfBatch({ html: '

{{account}}

', records: [{ account: '1042' }], isolation: 'process' }); + const record: api.BatchRecordReport = batch.records[0]; + const batchPdf: Buffer = batch.pdf; + await queue.renderPdfBatch({ html: '

{{account}}

', records: [{ account: '1043' }] }); + void [record, batchPdf]; await queue.close(); return { pdf, error }; } diff --git a/test/types/ui-render.mts b/test/types/ui-render.mts index 64a05d3..d1c41cc 100644 --- a/test/types/ui-render.mts +++ b/test/types/ui-render.mts @@ -1,4 +1,4 @@ -import { renderPdf, createRenderQueue, type RenderResult } from 'fullbleed'; +import { renderPdf, renderPdfBatch, createRenderQueue, type RenderResult, type BatchRenderResult } from 'fullbleed'; import { Document, Paragraph, renderDocument } from 'fullbleed/ui'; const doc = Document({ title: 'Types' }, Paragraph('Hello')); const a: Promise = doc.renderPdf(renderPdf, { previewDpi: 96, isolation: 'process' }); @@ -12,3 +12,10 @@ renderDocument(doc, renderPdf, { html: '

lost

' }); // @ts-expect-error Renderer options must remain checked for queue objects too. renderDocument(doc, queue, { timeoutMs: 'long' }); void [a, b, buffer]; +const batch: Promise = doc.renderPdf(renderPdfBatch, { records: [{ account: '1042' }] }); +renderDocument(doc, queue.renderPdfBatch, { records: [{ account: '1043' }] }); +// @ts-expect-error Required renderer options remain required through component authoring. +doc.renderPdf(renderPdfBatch); +// @ts-expect-error The standalone component adapter also requires batch records. +renderDocument(doc, renderPdfBatch); +void batch; diff --git a/tools/build.mjs b/tools/build.mjs index dae3177..2b231b0 100644 --- a/tools/build.mjs +++ b/tools/build.mjs @@ -27,7 +27,8 @@ assert(core.source?.startsWith('registry+'), 'Build from the published crate'); const engineFeatures = metadata.resolve.nodes.find(node => node.id === core.id).features.sort(); assert.deepEqual(engineFeatures, ['svg_raster'], 'Build with the native SVG fallback enabled'); await mkdir(dist, { recursive: true }); -await copyFile(join(root, 'src/index.d.ts'), join(dist, 'index.d.cts')); +await writeFile(join(dist, 'index.d.cts'), (await readFile(join(root, 'src/index.d.ts'), 'utf8')).replaceAll('./batch.js', './batch.cjs')); +await copyFile(join(root, 'src/batch.d.ts'), join(dist, 'batch.d.cts')); await copyFile(join(root, 'src/ui.d.ts'), join(dist, 'ui.d.cts')); await writeFile(join(dist, 'jsx-runtime.d.cts'), (await readFile(join(root, 'src/jsx-runtime.d.ts'), 'utf8')).replaceAll('./ui.js', './ui.cjs')); await copyFile(join(engine, 'target/wasm32-wasip1/release/fullbleed-node-adapter.wasm'), join(dist, 'engine.wasm')); diff --git a/tools/check-browser.py b/tools/check-browser.py index e52da7d..8b0c72b 100644 --- a/tools/check-browser.py +++ b/tools/check-browser.py @@ -24,7 +24,7 @@ out = root / f'output/browser-{args.browser}-{args.label}' out.mkdir(exist_ok=False) requests, errors, checks, documents = [], [], [], [] -gates = {name: {'started': Event(), 'release': Event()} for name in ['slow-abort', 'slow-timeout']} +gates = {name: {'started': Event(), 'release': Event()} for name in ['slow-abort', 'slow-timeout', 'batch-abort', 'batch-timeout']} policy = "default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self'; connect-src 'self'; img-src 'self' blob:; style-src 'self'; object-src 'none'; base-uri 'none'" class Handler(BaseHTTPRequestHandler): @@ -37,7 +37,7 @@ def do_GET(self): requests.append(request) relative = path.lstrip('/') or 'index.html' prefix = relative.split('/')[0] - if prefix in ['wrong', 'corrupt', 'missing', *gates]: + if prefix in ['wrong', 'corrupt', 'missing', 'bad-batch', *gates]: relative = 'fullbleed/' + relative.split('/', 1)[1] file = (site / relative).resolve() if not file.is_relative_to(site.resolve()) or not file.is_file(): @@ -54,6 +54,9 @@ def do_GET(self): data = b'self.postMessage({type:"ready",protocol:1,version:"0.0.0",engineVersion:"0.0.0"});' if prefix == 'corrupt' and relative.endswith('/engine.wasm'): data = data[:-1] + bytes([data[-1] ^ 1]) + if prefix == 'bad-batch' and relative.endswith('/worker.js'): + # Keep the real renderer and deliberately corrupt only its record report. + data = b'const originalPost = self.postMessage.bind(self); self.postMessage = (data, ...rest) => { if(data.type === "result" && data.ok) data.records[0].pageOffset = 1; return originalPost(data, ...rest); };\n' + data mime = 'application/javascript' if file.suffix in ('.js', '.mjs') else 'application/wasm' if file.suffix == '.wasm' else mimetypes.guess_type(file.name)[0] or 'application/octet-stream' try: self.send_response(200) @@ -92,6 +95,8 @@ def check(name, condition): for name, baseline in prepared['baselines'].items(): result = page.evaluate('name => harness.render(name)', name) check(name + ': browser PDF and all previews match the installed Node package', result['pdf'] == baseline['pdf'] and result['previews'] == baseline['previews'] and result['pages'] == baseline['pages'] and result['missingGlyphs'] == baseline['missingGlyphs']) + if 'records' in baseline: + check(name + ': every record report matches the installed Node package', result.get('records') == baseline['records']) check(name + ': result uses browser byte arrays and requests worker termination', result['browserBytes'] and result['stats']['allTerminatedOnce']) for preview in range(-1, len(result['previews'])): with page.expect_download() as event: @@ -132,6 +137,29 @@ def check(name, condition): check('caller-owned font and asset buffers are copied before awaiting', copied['pdf'] == prepared['baselines']['custom']['pdf']) named = page.evaluate("() => harness.render('named-font', true)") check('named font bytes are copied before awaiting', named['pdf'] == prepared['baselines']['named-font']['pdf']) + batch_snapshot = page.evaluate("() => harness.render('batch-fixed', true)") + check('batch records are copied before awaiting', batch_snapshot['pdf'] == prepared['baselines']['batch-fixed']['pdf'] + and batch_snapshot['records'] == prepared['baselines']['batch-fixed']['records']) + batch_invalid = page.evaluate("""async () => { + const before = harness.stats().created; + const basic = {html:'

{{value}}

',records:[{value:'ok'}]}; + const inputs = [{...basic,records:[]}, {...basic,records:[{value:1}]}, + {...basic,records:[{value:'x'},{other:'y'}]}, {...basic,records:[{value:'é'.repeat(2000001)}]}]; + const results = await Promise.all(inputs.map(input => harness.capture(harness.renderer.renderPdfBatch(input)))); + return {results,created:harness.stats().created-before}; + }""") + check('invalid batch records and combined UTF-8 budget start no browser worker', batch_invalid['created'] == 0 + and all(row.get('code') == 'INVALID_INPUT' and row.get('fullbleedError') for row in batch_invalid['results'])) + for name, overrides, code in [('batch-missing', {'allowMissingGlyphs': False}, 'MISSING_GLYPHS'), + ('batch-fixed', {'maxPages': 1}, 'PAGE_LIMIT'), + ('batch-fixed', {'records': [{'wrong': 'value'}]}, 'INVALID_BINDINGS')]: + failure = page.evaluate("""async ({name,overrides}) => harness.capture(harness.renderer.renderPdfBatch({ + ...await harness.input(name), ...overrides}))""", {'name': name, 'overrides': overrides}) + check(name + ': batch ' + code, failure.get('code') == code and failure.get('fullbleedError')) + corrupt_report = page.evaluate("""async () => harness.capture(harness.api.createRenderer({ + assetBaseUrl:new URL('bad-batch/',location.href)}).renderPdfBatch(await harness.input('batch-fixed')))""") + check('malformed browser batch reports reject and terminate the worker', corrupt_report.get('code') == 'WORKER_FAILED' + and page.evaluate('harness.stats().allTerminatedOnce')) invalid = page.evaluate("""async () => { const before = harness.stats().created; @@ -166,15 +194,17 @@ def check(name, condition): }""") check('aborting during construction still terminates the returned worker', race.get('code') == 'ABORTED' and page.evaluate('harness.stats().allTerminatedOnce')) - for prefix, code in [('slow-abort', 'ABORTED'), ('slow-timeout', 'TIMEOUT')]: + for prefix, code in [('slow-abort', 'ABORTED'), ('slow-timeout', 'TIMEOUT'), ('batch-abort', 'ABORTED'), ('batch-timeout', 'TIMEOUT')]: page.evaluate("""prefix => { globalThis.controller = new AbortController(); const renderer = harness.api.createRenderer({assetBaseUrl:new URL(prefix+'/',location.href)}); - globalThis.pending = harness.capture(renderer.renderPdf({html:'Delayed runtime load', - signal:controller.signal,timeoutMs:prefix==='slow-timeout'?1500:10000})); + const options = {html:'Delayed runtime load {{value}}', + signal:controller.signal,timeoutMs:prefix.endsWith('timeout')?1500:10000}; + globalThis.pending = harness.capture(prefix.startsWith('batch-') + ? renderer.renderPdfBatch({...options,records:[{value:'batch'}]}) : renderer.renderPdf(options)); }""", prefix) assert gates[prefix]['started'].wait(5), 'Observe actual engine fetching before interrupting it.' - if prefix == 'slow-abort': + if prefix.endswith('abort'): page.evaluate('controller.abort()') failure = page.evaluate('pending') gates[prefix]['release'].set() @@ -188,6 +218,9 @@ def check(name, condition): check('mismatched workers receive no document payload', page.evaluate('harness.stats().postedMessages') == before['postedMessages']) recovered = page.evaluate("() => harness.render('invoice')") check('a normal render recovers after errors, timeout and cancellation', recovered['pdf'] == prepared['baselines']['invoice']['pdf'] and recovered['stats']['allTerminatedOnce']) + batch_recovered = page.evaluate("() => harness.render('batch-reflow')") + check('a batch recovers after errors, timeout and cancellation', batch_recovered['pdf'] == prepared['baselines']['batch-reflow']['pdf'] + and batch_recovered['records'] == prepared['baselines']['batch-reflow']['records'] and batch_recovered['stats']['allTerminatedOnce']) check('all requests are same-origin GETs or local image blobs without document upload', all(row['method'] == 'GET' and not row['body'] and 'PRIVATE_BROWSER_MARKER' not in row['url'] and (row['url'].startswith(base) or (row['resourceType'] == 'image' and row['url'].startswith('blob:' + base))) diff --git a/tools/check-build.mjs b/tools/check-build.mjs index 0bfe12a..f88ae27 100644 --- a/tools/check-build.mjs +++ b/tools/check-build.mjs @@ -20,7 +20,9 @@ for (const [name, expected] of Object.entries(manifest.files)) { assert.equal(bytes.length, expected.bytes, name); assert.equal(createHash('sha256').update(bytes).digest('hex'), expected.sha256, name); } -assert.deepEqual(await readFile(new URL('src/index.d.ts', root)), await readFile(new URL('dist/index.d.cts', root))); +assert.equal((await readFile(new URL('src/index.d.ts', root), 'utf8')).replaceAll('./batch.js', './batch.cjs'), + await readFile(new URL('dist/index.d.cts', root), 'utf8')); +assert.deepEqual(await readFile(new URL('src/batch.d.ts', root)), await readFile(new URL('dist/batch.d.cts', root))); assert.deepEqual(await readFile(new URL('src/ui.d.ts', root)), await readFile(new URL('dist/ui.d.cts', root))); assert.equal((await readFile(new URL('src/jsx-runtime.d.ts', root), 'utf8')).replaceAll('./ui.js', './ui.cjs'), await readFile(new URL('dist/jsx-runtime.d.cts', root), 'utf8')); diff --git a/tools/check-cloudflare.mjs b/tools/check-cloudflare.mjs index bf351fa..10179d7 100644 --- a/tools/check-cloudflare.mjs +++ b/tools/check-cloudflare.mjs @@ -28,6 +28,9 @@ catch (error) { if (!error.cause && error.name !== 'TimeoutError' && !String(err for (const name of ['worker.mjs', 'cases.mjs', 'baseline.mjs']) { await copyFile(join(root, 'test/cloudflare', name), join(consumer, name)); } +await copyFile(join(root, 'test/batch-fixtures.mjs'), join(consumer, '../batch-fixtures.mjs')); +await copyFile(join(root, 'test/fonts/NotoSansMath-Regular.ttf'), join(consumer, 'math-font.ttf')); +await copyFile(join(root, 'test/fonts/NotoSansMath-OFL.txt'), join(consumer, 'math-font-OFL.txt')); await copyFile(join(root, 'test/fonts/family/LibreCaslonText[wght].ttf'), join(consumer, 'custom-font.ttf')); await copyFile(join(root, 'test/fonts/family/OFL.txt'), join(consumer, 'custom-font-OFL.txt')); const raw = {}; @@ -76,7 +79,8 @@ async function post(payload) { async function checkResult(data, expected, suffix) { assert.equal(data.engineVersion, pkg.fullbleed.engineVersion); assert.equal(data.pages, expected.pages); - assert.equal(data.missingGlyphs, 0); + assert.equal(data.missingGlyphs, expected.missingGlyphs); + if (expected.records) assert.deepEqual(data.records, expected.records, expected.name + ' record reports'); const pdf = Buffer.from(data.pdf, 'base64'); assert.equal(hash(pdf), expected.pdf, expected.name + ' PDF bytes'); assert.equal(data.previews.length, expected.previews.length); @@ -110,7 +114,9 @@ try { baseline.records.find(item => item.name === 'components-inline').pdf, 'Styled and separate HTML/CSS component exports must agree.'); const errors = { timeout: 'INVALID_INPUT', signal: 'INVALID_INPUT', isolation: 'INVALID_INPUT', 'source-limit': 'INVALID_INPUT', 'asset-limit': 'INVALID_INPUT', 'asset-path': 'INVALID_INPUT', - 'asset-collision': 'INVALID_INPUT', pages: 'PAGE_LIMIT', 'default-pages': 'PAGE_LIMIT', glyph: 'MISSING_GLYPHS' }; + 'asset-collision': 'INVALID_INPUT', pages: 'PAGE_LIMIT', 'default-pages': 'PAGE_LIMIT', glyph: 'MISSING_GLYPHS', + 'batch-source-limit': 'INVALID_INPUT', 'batch-fields': 'INVALID_INPUT', 'batch-bindings': 'INVALID_BINDINGS', + 'batch-pages': 'PAGE_LIMIT', 'batch-glyph': 'MISSING_GLYPHS' }; for (const [name, code] of Object.entries(errors)) { const { status, data } = await post({ name, error: true }); assert.equal(status, 422, JSON.stringify(data)); @@ -127,7 +133,7 @@ try { assert.equal(burstResponse.status, 200); const burst = await burstResponse.json(); assert.equal(burst.outsideSynchronousCall, 0); - for (const [i, name] of ['invoice', 'report'].entries()) { + for (const [i, name] of ['invoice', 'report', 'batch-reflow'].entries()) { await checkResult(burst.results[i], baseline.records.find(item => item.name === name), 'after-errors'); } record.synchronousInstantiation = true; diff --git a/tools/check-installed-batches.mjs b/tools/check-installed-batches.mjs new file mode 100644 index 0000000..bc37327 --- /dev/null +++ b/tools/check-installed-batches.mjs @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: MIT +// Copied into the isolated tarball consumer by verify-pack.mjs. +import assert from 'node:assert/strict'; +import { readFile, writeFile, mkdir } from 'node:fs/promises'; +import { dirname, join } from 'node:path'; +import { createRequire } from 'node:module'; +import { createHash } from 'node:crypto'; +import { spawnSync } from 'node:child_process'; +import { renderPdfBatch, createRenderQueue, version, engineVersion } from 'fullbleed'; +import { batchFixtures } from './batch-fixtures.mjs'; + +const out = process.argv[2]; +await mkdir(out, { recursive: true }); +const hash = bytes => createHash('sha256').update(bytes).digest('hex'); +const require = createRequire(import.meta.url); +const cjs = require('fullbleed'); +const queue = createRenderQueue({ concurrency: 1, maxQueue: 1 }); +const fixtures = []; +try { + for (const fixture of batchFixtures) { + const fonts = await Promise.all((fixture.fontFiles ?? []).map(name => readFile(new URL('./fonts/' + name, import.meta.url)))); + const options = { ...fixture.options, fonts }; + const result = await renderPdfBatch(options); + assert.equal(result.engineVersion, engineVersion); + assert.equal(result.pages, fixture.pages); + assert.equal(result.missingGlyphs, fixture.missingGlyphs); + assert.deepEqual(result.records.map(row => row.pageCount), fixture.recordPages); + assert.deepEqual(result.records.map(row => row.missingGlyphs.map(({ codepoint, count }) => ({ codepoint, count }))), fixture.missingRows); + assert.deepEqual(await cjs.renderPdfBatch({ ...options, isolation: 'process' }), result); + assert.deepEqual(await queue.renderPdfBatch(options), result); + await writeFile(join(out, fixture.name + '.pdf'), result.pdf); + for (const [i, png] of result.previews.entries()) await writeFile(join(out, `${fixture.name}-${i + 1}.png`), png); + fixtures.push({ name: fixture.name, pages: result.pages, missingGlyphs: result.missingGlyphs, records: result.records, + pdf: hash(result.pdf), previews: result.previews.map(hash), cjsProcessAndQueueEqual: true }); + } +} finally { await queue.close(); } + +const readme = await readFile(join(dirname(require.resolve('fullbleed')), '../README.md'), 'utf8'); +const section = readme.split('## Generate a batch from one template')[1]?.split('\n## ')[0]; +assert(section, 'The installed README must include batch authoring.'); +const examples = [...section.matchAll(/```javascript\r?\n([\s\S]*?)\r?\n```/g)].map(match => match[1]); +assert.equal(examples.length, 2); +const readmeExamples = []; +for (const [index, code] of examples.entries()) { + const name = 'readme-batch-' + (index + 1); + const expectedPages = index === 0 ? 2 : 1; + const script = name + '.mjs'; + const report = join(out, name + '.json'); + const suffix = `\nimport _batchAssert from 'node:assert/strict'; +import { writeFile as _batchWrite } from 'node:fs/promises'; +import { createHash as _batchHash } from 'node:crypto'; +_batchAssert.equal(result.pages, ${expectedPages}); +_batchAssert.equal(result.records.length, ${expectedPages}); +_batchAssert.equal(result.missingGlyphs, 0); +_batchAssert.equal(result.engineVersion, ${JSON.stringify(engineVersion)}); +await _batchWrite(${JSON.stringify(join(out, name + '.pdf'))}, result.pdf); +await _batchWrite(${JSON.stringify(report)}, JSON.stringify({pages:result.pages,records:result.records,pdf:_batchHash('sha256').update(result.pdf).digest('hex')},null,2)+'\\n'); +`; + await writeFile(script, code + suffix); + await writeFile(join(out, script), code + suffix); + const run = spawnSync(process.execPath, [script], { encoding: 'utf8', timeout: 60000 }); + await writeFile(join(out, name + '.log'), run.stdout + run.stderr); + assert.equal(run.status, 0, run.stderr || run.stdout); + readmeExamples.push({ name, unmodifiedExampleSha256: hash(code), ...JSON.parse(await readFile(report, 'utf8')) }); +} +const record = { ok: true, packageVersion: version, engineVersion, node: process.version, platform: process.platform, + fixtures, readmeExamples, queueClosed: queue.closed, + scope: 'Seven installed batch fixtures through ESM workers, CommonJS processes and the shared queue; both actual README examples executed.' }; +await writeFile(join(out, 'verification.json'), JSON.stringify(record, null, 2) + '\n'); +console.log(JSON.stringify({ ok: true, fixtures: fixtures.length, readmeExamples: readmeExamples.length })); diff --git a/tools/prepare-browser-check.mjs b/tools/prepare-browser-check.mjs index e197601..d0b3c26 100644 --- a/tools/prepare-browser-check.mjs +++ b/tools/prepare-browser-check.mjs @@ -15,6 +15,7 @@ import { fixtures as paginationFixtures } from '../test/pagination-fixtures.mjs' import { tableWrappingFixtures, tableWrappingSource } from '../test/table-wrapping.mjs'; import { generatedQuotesFixtures } from '../test/generated-quotes.mjs'; import { textDecorationsFixtures } from '../test/text-decorations.mjs'; +import { batchFixtures } from '../test/batch-fixtures.mjs'; const root = resolve('.'); const out = join(root, 'output/browser-verification'); @@ -63,6 +64,7 @@ const styleFixtures = JSON.parse(await readFile('test/fixtures/default-styles.js const styleInputs = Object.fromEntries(styleFixtures.cases.flatMap(({ name, html, css, previewDpi }) => [false, true].map(defaultStyles => [`styles-${name}-${defaultStyles}`, { html, css, previewDpi, defaultStyles }]))); const inputs = { invoice: await fixture('invoice'), report: await fixture('report'), + ...Object.fromEntries(batchFixtures.map(({ name, options, fontFiles }) => [name, { ...options, fontFiles }])), ...Object.fromEntries(components.map(({ name, node }) => [name, { html: node.toHtml(), css: node.toCss(), previewDpi: 96, defaultStyles: false }])), ...styleInputs, ...Object.fromEntries(inlineFixtures.map(({ name, pages, ...input }) => [name, { ...input, previewDpi: 96 }])), @@ -96,14 +98,15 @@ for (const [name, fixture] of Object.entries(inputs)) { return fontNames[i] ? { name: fontNames[i], data } : data; })); options.assets = Object.fromEntries(Object.entries(assetTexts).map(([key, value]) => [key, Buffer.from(value)])); - const result = await node.renderPdf(options); + const result = await (options.records ? node.renderPdfBatch : node.renderPdf)(options); await writeFile(join(out, name + '.pdf'), result.pdf); const previews = []; for (const [i, png] of result.previews.entries()) { await writeFile(join(out, `${name}-${i + 1}.png`), png); previews.push(hash(png)); } - baselines[name] = { pages: result.pages, missingGlyphs: result.missingGlyphs, pdf: hash(result.pdf), previews }; + baselines[name] = { pages: result.pages, missingGlyphs: result.missingGlyphs, pdf: hash(result.pdf), previews, + ...(result.records ? { records: result.records } : {}) }; } for (const { name, engine, legacy } of styleFixtures.cases) { for (const [choice, expected] of [[false, engine], [true, legacy]]) { @@ -112,7 +115,8 @@ for (const { name, engine, legacy } of styleFixtures.cases) { assert.deepEqual(actual.previews, [expected.png], name + ': independent preview baseline'); } } -assert.equal(pkg.fullbleed.engineVersion, hiddenTextSource.engineVersion); +assert.equal(node.engineVersion, pkg.fullbleed.engineVersion); +assert.equal(hiddenTextSource.engineVersion, '2.5.43', 'Preserve the historical hidden-text baseline.'); for (const { name, expected } of hiddenTextSource.cases) { for (const variant of ['source', 'control']) { const actual = baselines[`visibility-${name}-${variant}`]; @@ -122,6 +126,8 @@ for (const { name, expected } of hiddenTextSource.cases) { } for (const name of ['index.html', 'harness.js', 'components.css']) await copyFile('test/browser/' + name, join(site, name)); const record = { package: pkg.version, engine: pkg.fullbleed.engineVersion, installedTarballSha256: pack.tarballSha256, + batchSourceSha256: hash(await readFile(join(root, 'test/batch-fixtures.mjs'))), + hiddenTextBaselineEngine: hiddenTextSource.engineVersion, enginePaintSourceSha256: hash(await readFile(join(root, 'test/fixtures/engine-paint-2.5.28.json'))), tableWrappingSourceSha256: hash(await readFile(join(root, 'test/fixtures/table-wrapping-2.5.37.json'))), generatedQuotesSourceSha256: hash(await readFile(join(root, 'test/fixtures/generated-quotes-2.5.38.json'))), diff --git a/tools/radial_gradient_inspection.py b/tools/radial_gradient_inspection.py index eb65f64..51589b8 100644 --- a/tools/radial_gradient_inspection.py +++ b/tools/radial_gradient_inspection.py @@ -47,7 +47,8 @@ def verify(directory, *, baseline=False): assert (directory / 'inputs.json').read_bytes() == source_bytes assert renders['inputSha256'] == sha256(source_bytes).hexdigest() assert baseline == (renders['packageVersion'] == '0.8.6') - assert renders['engineVersion'] == ('2.5.41' if baseline else '2.5.43') + package = json.loads((ROOT / 'package.json').read_text(encoding='utf-8')) + assert renders['engineVersion'] == ('2.5.41' if baseline else package['fullbleed']['engineVersion']) fixtures = {row['name']: row for row in source['cases']} expected = {(mode, name) for name in fixtures for mode in MODES} actual = {(row['mode'], row['name']) for row in renders['cases']} diff --git a/tools/render-hidden-text.mjs b/tools/render-hidden-text.mjs index 6f5c6bf..9ff175e 100644 --- a/tools/render-hidden-text.mjs +++ b/tools/render-hidden-text.mjs @@ -29,7 +29,9 @@ if (values['negative-control']) { assert.equal(version, '0.5.0'); assert.equal(engineVersion, '2.5.22'); } else { - assert.equal(engineVersion, hiddenTextSource.engineVersion); + const pkg = JSON.parse(await readFile(join(root, 'package.json'), 'utf8')); + assert.equal(engineVersion, pkg.fullbleed.engineVersion); + assert.equal(hiddenTextSource.engineVersion, '2.5.43', 'Preserve the historical hidden-text baseline.'); } const out = resolve(values.out ?? join(root, 'output/hidden-text')); await mkdir(join(out, '..'), { recursive: true }); @@ -37,6 +39,7 @@ await mkdir(out); const report = { schema: 'fullbleed.javascript.hidden-text.renders.v1', ok: false, packageVersion: version, engineVersion, node: process.version, platform: process.platform, negativeControl: values['negative-control'], sourceCommit: hiddenTextSource.sourceCommit, + baselineEngineVersion: hiddenTextSource.engineVersion, fixtureSha256: hash(await readFile(join(root, 'test/fixtures/hidden-text-2.5.24.json'))), wasmSha256: build.files['dist/engine.wasm'].sha256, installedTarballSha256: packed?.tarballSha256 ?? null, cases: [], failures: [] }; diff --git a/tools/verify-batches.py b/tools/verify-batches.py new file mode 100644 index 0000000..8646d5d --- /dev/null +++ b/tools/verify-batches.py @@ -0,0 +1,98 @@ +"""Independently read and render the batch PDFs retained from the installed package.""" +from datetime import datetime, timezone +from hashlib import sha256 +from importlib.metadata import version +from io import BytesIO +import json +from pathlib import Path +import sys + +from PIL import Image +from pypdf import PdfReader +import pypdfium2 as pdfium + +root = Path(__file__).resolve().parents[1] +folder = Path(sys.argv[1]) if len(sys.argv) > 1 else root / 'output/batch-verification' +record = json.loads((folder / 'verification.json').read_text(encoding='utf-8')) +assert record['ok'] and record['queueClosed'] +assert len(record['fixtures']) == 7 and len(record['readmeExamples']) == 2 +pack = json.loads((root / 'output/pack-verification/verification.json').read_text(encoding='utf-8')) +assert pack['ok'] and pack['batchSmoke']['fixtures'] == record['fixtures'] +assert pack['batchSmoke']['readmeExamples'] == record['readmeExamples'] +plain = lambda text: ' '.join(text.split()) +digest = lambda data: sha256(data).hexdigest() +expected_pages = {'batch-fixed': 2, 'batch-reflow': 5, 'batch-missing': 2, 'batch-components': 2, + 'batch-literal': 1, 'batch-fallback-fixed': 2, 'batch-fallback-reflow': 2, + 'readme-batch-1': 2, 'readme-batch-2': 1} +images = folder / 'independent-pages' +images.mkdir(exist_ok=True) +checked = [] +for item in [*record['fixtures'], *record['readmeExamples']]: + name = item['name'] + raw = (folder / (name + '.pdf')).read_bytes() + assert digest(raw) == item['pdf'] + reader = PdfReader(BytesIO(raw), strict=True) + texts = [plain(page.extract_text()) for page in reader.pages] + assert len(texts) == item['pages'] == expected_pages[name] + offset = 0 + ranges = [] + for index, row in enumerate(item['records']): + assert row['recordIndex'] == index and row['pageOffset'] == offset and row['pageCount'] > 0 + text = plain(' '.join(texts[offset:offset + row['pageCount']])) + if name == 'batch-literal': + assert text == 'Café & literal "quoted" \\ path', text + else: + account, other = ('1042', '1043') if index == 0 else ('1043', '1042') + assert 'Account ' + account in text and other not in text, (name, index, text) + if name in ('batch-fixed', 'batch-reflow'): + assert 'END ' + account in text + message = 'Active.' if index == 0 else ('Review scheduled.' if name == 'batch-fixed' + else plain('This longer notice continues onto another page. ' * 20)) + assert message in text, (name, index, text) + if name in ('batch-fallback-fixed', 'batch-fallback-reflow'): + assert ('ASCII math' if index == 0 else 'Integral:') in text + if index == 1 and name.endswith('reflow'): + assert '⨌' in text + if name.startswith('readme-'): + assert ('Your account is up to date.' if index == 0 else 'Your next review is scheduled for November.') in text + ranges.append({**row, 'extractedText': text}) + offset += row['pageCount'] + assert offset == len(texts) + if name == 'batch-reflow': + assert [row['pageCount'] for row in item['records']] == [1, 4] + if name == 'batch-missing': + assert item['missingGlyphs'] == 1 + assert [[(glyph['codepoint'], glyph['count']) for glyph in row['missingGlyphs']] for row in item['records']] == [[(0x10ffff, 1)], [(0x10ffff, 3)]] + elif name == 'batch-fallback-fixed': + assert item['missingGlyphs'] == 1 and item['records'][0]['missingGlyphs'] == [] + assert item['records'][1]['missingGlyphs'] == [{'codepoint': 0x2a0c, 'char': '⨌', 'count': 1, 'fontsTried': ['Inter']}] + else: + assert item.get('missingGlyphs', 0) == 0 and all(not row['missingGlyphs'] for row in item['records']) + document = pdfium.PdfDocument(raw) + page_images = [] + assert len(document) == len(texts) + for index in range(len(document)): + page = document[index] + bitmap = page.render(scale=1) + image = bitmap.to_pil() + path = images / f'{name}-{index + 1}.png' + image.save(path) + page_images.append({'file': str(path.relative_to(folder)), 'sha256': digest(path.read_bytes()), + 'width': image.width, 'height': image.height}) + if item.get('previews'): + preview = folder / f'{name}-{index + 1}.png' + assert digest(preview.read_bytes()) == item['previews'][index] + with Image.open(preview) as candidate: + candidate.load() + assert abs(candidate.width - image.width) <= 1 and abs(candidate.height - image.height) <= 1 + image.close() + bitmap.close() + page.close() + document.close() + checked.append({'name': name, 'pdf': item['pdf'], 'pages': len(texts), 'records': ranges, 'images': page_images}) +result = {'ok': True, 'checkedAt': datetime.now(timezone.utc).isoformat(), 'packageVersion': record['packageVersion'], + 'engineVersion': record['engineVersion'], 'tarballSha256': pack['tarballSha256'], + 'readers': {name: version(name) for name in ('pypdf', 'pypdfium2', 'Pillow')}, 'documents': checked, + 'scope': 'Expected text, page ranges, selected-font diagnostics, and independent PDFium page rendering for these nine installed-package examples. Visual review is separate; no conformance certification.'} +(folder / 'independent-reader.json').write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8') +print(json.dumps({'ok': True, 'documents': len(checked), 'pages': sum(row['pages'] for row in checked)})) diff --git a/tools/verify-engine-paint.py b/tools/verify-engine-paint.py index 9f8f471..9dccff5 100644 --- a/tools/verify-engine-paint.py +++ b/tools/verify-engine-paint.py @@ -97,7 +97,9 @@ def main(): if args.negative_control: assert (package, engine) == (source['negativeControl']['packageVersion'], source['negativeControl']['engineVersion']) else: - assert engine == source['engineVersion'] + package_metadata = json.loads((ROOT / 'package.json').read_text(encoding='utf-8')) + assert package == package_metadata['version'] and engine == package_metadata['fullbleed']['engineVersion'] + assert source['engineVersion'] == '2.5.43', 'Preserve the historical paint baseline' report = dict(schema='fullbleed.javascript.engine-paint.verification.v1', ok=False, checkedAt=datetime.now(timezone.utc).isoformat(), packageVersion=package, engineVersion=engine, sourceSha256=source_hash, installedTarballSha256=origin.get('installedTarballSha256'), diff --git a/tools/verify-generated-quotes.py b/tools/verify-generated-quotes.py index 3b6cd85..17874e8 100644 --- a/tools/verify-generated-quotes.py +++ b/tools/verify-generated-quotes.py @@ -38,7 +38,8 @@ assert sha256((args.root/'inputs.json').read_bytes()).hexdigest() == source_sha if not args.negative_control: assert re.fullmatch(r'[a-f0-9]{64}', renders['tarballSha256']) - assert renders['engineVersion'] == '2.5.43' + package = json.loads((source_file.parents[2] / 'package.json').read_text(encoding='utf-8')) + assert renders['packageVersion'] == package['version'] and renders['engineVersion'] == package['fullbleed']['engineVersion'] modes = {'browser'} if args.browser else {'worker','process'} assert len(renders['cases']) == len(fixtures)*len(modes) assert {(c['name'],c['isolation']) for c in renders['cases']} == {(name,mode) for name in fixtures for mode in modes} diff --git a/tools/verify-hidden-text.py b/tools/verify-hidden-text.py index d1a9da6..585df90 100644 --- a/tools/verify-hidden-text.py +++ b/tools/verify-hidden-text.py @@ -56,10 +56,14 @@ def main(): negative=origin.get('negativeControl',False) assert not (negative and args.browser) if negative:assert (origin['packageVersion'],origin['engineVersion'])==('0.5.0','2.5.22') - else:assert origin['engine' if args.browser else 'engineVersion']==source['engineVersion'] + else: + package=json.loads((ROOT/'package.json').read_text(encoding='utf-8')) + assert origin['engine' if args.browser else 'engineVersion']==package['fullbleed']['engineVersion'] + assert source['engineVersion']=='2.5.43', 'Preserve the historical hidden-text baseline' report={'ok':False,'checked_at':datetime.now(timezone.utc).isoformat(),'negativeControl':negative, 'packageVersion':origin['package' if args.browser else 'packageVersion'], 'engineVersion':origin['engine' if args.browser else 'engineVersion'], + 'baselineEngineVersion':source['engineVersion'], 'installedTarballSha256':origin.get('installedTarballSha256'), 'browser':args.browser,'fixture_sha256':sha256(fixture.read_bytes()).hexdigest(), 'versions':{name:version(name) for name in ('pypdf','pypdfium2','pillow')},'cases':[],'failures':[], diff --git a/tools/verify-native.mjs b/tools/verify-native.mjs index afd3fde..20510fd 100644 --- a/tools/verify-native.mjs +++ b/tools/verify-native.mjs @@ -5,7 +5,8 @@ import { createHash } from 'node:crypto'; import { spawnSync } from 'node:child_process'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; -import { renderPdf } from 'fullbleed'; +import { renderPdf, renderPdfBatch } from 'fullbleed'; +import { batchFixtures } from '../test/batch-fixtures.mjs'; import { gradientFixtures } from '../test/gradients.mjs'; import { inlineFixtures } from '../test/inline-wrapping.mjs'; import { standardFontFixtures } from '../test/standard-fonts.mjs'; @@ -39,6 +40,9 @@ for (const [name, pages] of [['invoice', 1], ['report', 3]]) { } fixtures.push({ name: 'custom-font', pages: 1, html: '

Integral: ⨌

', css: "body {font-family:Inter,'Noto Sans Math'}", fonts: [await readFile(join(root, 'test/fonts/NotoSansMath-Regular.ttf'))] }); fixtures.push({ name: 'asset', pages: 1, html: '

Brand asset

', css: '', assets: { 'brand/logo.svg': Buffer.from('') } }); +for (const fixture of batchFixtures) fixtures.push({ name: fixture.name, pages: fixture.pages, ...fixture.options, + recordPages: fixture.recordPages, expectedMissingGlyphs: fixture.missingGlyphs, missingRows: fixture.missingRows, + fonts: await Promise.all((fixture.fontFiles ?? []).map(name => readFile(join(root, 'test/fonts', name)))) }); for (const fixture of fixtures) { const directory = await mkdtemp(join(output, fixture.name + '-')); await mkdir(join(directory, 'fonts')); @@ -56,14 +60,26 @@ for (const fixture of fixtures) { } await writeFile(join(directory, 'input.html'), fixture.html); await writeFile(join(directory, 'style.css'), (fixture.defaultStyles === false ? '' : 'body { font-family: Inter; }\n') + fixture.css); + const batch = fixture.records !== undefined; + if (batch) await writeFile(join(directory, 'records.json'), JSON.stringify(fixture.records)); const dpi = fixture.previewDpi ?? 96; - const native = spawnSync(executable, ['1000', String(dpi), 'reject', ...names], { cwd: directory, encoding: 'utf8', timeout: 60000 }); + const native = spawnSync(executable, ['1000', String(dpi), fixture.allowMissingGlyphs ? 'allow' : 'reject', ...names, + ...(batch ? ['--batch-' + (fixture.layout ?? 'reflow')] : [])], { cwd: directory, encoding: 'utf8', timeout: 60000 }); assert.equal(native.status, 0, native.stderr); const options = { html: fixture.html, css: fixture.css, previewDpi: dpi, fonts: fixture.fonts, assets: fixture.assets, + ...(batch ? { records: fixture.records, layout: fixture.layout, allowMissingGlyphs: fixture.allowMissingGlyphs ?? false } : {}), ...(fixture.defaultStyles === undefined ? {} : { defaultStyles: fixture.defaultStyles }) }; - const wasi = await renderPdf(options); + const wasi = await (batch ? renderPdfBatch : renderPdf)(options); assert.equal(wasi.pages, fixture.pages); - assert.equal(wasi.missingGlyphs, 0); + assert.equal(wasi.missingGlyphs, fixture.expectedMissingGlyphs ?? 0); + const nativeReport = JSON.parse(await readFile(join(directory, 'result.json'), 'utf8')); + assert.equal(nativeReport.pages, wasi.pages); + assert.equal(nativeReport.missingGlyphs, wasi.missingGlyphs); + if (batch) { + assert.deepEqual(nativeReport.records, wasi.records); + assert.deepEqual(wasi.records.map(row => row.pageCount), fixture.recordPages); + assert.deepEqual(wasi.records.map(row => row.missingGlyphs.map(({ codepoint, count }) => ({ codepoint, count }))), fixture.missingRows); + } const pdf = await readFile(join(directory, 'document.pdf')); assert.equal(hash(wasi.pdf), hash(pdf), fixture.name + ' PDF'); await writeFile(join(directory, 'wasi.pdf'), wasi.pdf); @@ -79,6 +95,7 @@ for (const fixture of fixtures) { } const previewsEqual = JSON.stringify(previews) === JSON.stringify(wasiPreviews); records.push({ name: fixture.name, pages: wasi.pages, pdfSha256: hash(pdf), previewsSha256: previews, + ...(batch ? { batch: true, missingGlyphs: wasi.missingGlyphs, recordReports: wasi.records, nativeWasiReportsEqual: true } : {}), wasiPreviewsSha256: wasiPreviews, directory: directory.slice(output.length + 1), nativeWasiPdfEqual: true, nativeWasiPreviewsEqual: previewsEqual, nativeWasiEqual: previewsEqual, previewComparisonRequired, previewScope: 'Exact finalized preview bytes, including bundled Standard 14 substitutes' }); diff --git a/tools/verify-pack.mjs b/tools/verify-pack.mjs index 907c7fd..e5c5e6e 100644 --- a/tools/verify-pack.mjs +++ b/tools/verify-pack.mjs @@ -28,6 +28,8 @@ for (const name of ['src/ui.js', 'src/ui.cjs', 'src/ui.d.ts', 'dist/ui.js', 'dis 'src/jsx-runtime.js', 'src/jsx-runtime.cjs', 'src/jsx-runtime.d.ts', 'dist/jsx-runtime.js', 'dist/jsx-runtime.d.cts']) assert(names.includes(name), 'Missing component authoring export: ' + name); assert(names.includes('src/render-queue.cjs'), 'The Node render queue must ship in the tarball.'); +for (const name of ['src/batch-report.cjs', 'src/batch.d.ts', 'dist/batch.d.cts']) + assert(names.includes(name), 'The batch API must ship in the tarball: ' + name); for (const name of ['dist/cloudflare.js', 'src/cloudflare.d.ts']) assert(names.includes(name), 'The Workers entry must ship in the tarball: ' + name); for (const required of ['dist/engine.wasm', 'dist/build.json', 'dist/index.d.cts', 'dist/THIRD_PARTY_NOTICES.txt', 'dist/browser/client.js', 'dist/browser/worker.js', 'dist/browser/asset-manifest.json', 'src/index.js', 'src/index.cjs', 'src/index.d.ts', 'src/browser.d.ts', 'src/engine-runner.js', 'src/render-input.cjs', 'src/copy-browser-assets.cjs', 'src/worker.js', 'src/process-worker.cjs', 'LICENSE', 'README.md', 'package.json']) assert(names.includes(required), required); @@ -160,6 +162,25 @@ await mkdir(join(install, 'fonts/family'), { recursive: true }); await copyFile(join(root, 'test/named-fonts.test.mjs'), join(install, 'named-fonts.test.mjs')); for (const name of ['LibreCaslonText[wght].ttf', 'OFL.txt']) await copyFile(join(root, 'test/fonts/family', name), join(install, 'fonts/family', name)); +await copyFile(join(root, 'test/fonts/NotoSansMath-Regular.ttf'), join(install, 'fonts/NotoSansMath-Regular.ttf')); +await mkdir(join(install, 'fixtures'), { recursive: true }); +await copyFile(join(root, 'test/fixtures/process-driver.cjs'), join(install, 'fixtures/process-driver.cjs')); +for (const name of ['batch-fixtures.mjs', 'batch-input.test.mjs', 'render-batch.test.mjs', 'process-isolation.test.mjs']) + await copyFile(join(root, 'test', name), join(install, name)); +const batchOutput = join(root, 'output/batch-verification'); +const batchRun = spawnSync(process.execPath, ['--test', '--test-reporter=tap', 'batch-input.test.mjs', 'render-batch.test.mjs', 'process-isolation.test.mjs'], { + cwd: install, env: { ...env, FULLBLEED_BATCH_EVIDENCE: join(batchOutput, 'runtime') }, encoding: 'utf8', timeout: 120000, +}); +await writeFile(join(output, 'batch-tests.log'), batchRun.stdout + batchRun.stderr); +assert.equal(batchRun.status, 0, batchRun.stderr || batchRun.stdout); +const batchTests = Number(batchRun.stdout.match(/^# tests (\d+)$/m)?.[1]); +assert(batchTests >= 28 && batchTests === Number(batchRun.stdout.match(/^# pass (\d+)$/m)?.[1])); +await copyFile(join(root, 'tools/check-installed-batches.mjs'), join(install, 'check-batches.mjs')); +const installedBatches = spawnSync(process.execPath, ['check-batches.mjs', batchOutput], { cwd: install, env, encoding: 'utf8', timeout: 120000 }); +await writeFile(join(output, 'installed-batches.log'), installedBatches.stdout + installedBatches.stderr); +assert.equal(installedBatches.status, 0, installedBatches.stderr || installedBatches.stdout); +const batchSmoke = { ...JSON.parse(await readFile(join(batchOutput, 'verification.json'), 'utf8')), tests: batchTests }; +assert(batchSmoke.ok && batchSmoke.fixtures.length === 7 && batchSmoke.readmeExamples.length === 2); const namedFontOutput = join(output, 'named-fonts'); const namedFontRun = spawnSync(process.execPath, ['--test', 'named-fonts.test.mjs'], { cwd: install, env: { ...env, FULLBLEED_NAMED_FONT_EVIDENCE: namedFontOutput }, encoding: 'utf8', timeout: 60000, @@ -200,6 +221,6 @@ assert.equal(inline.stdout.trim().replaceAll('\r', ''), '1\n1'); const report = { ok: true, node: process.version, platform: process.platform, package: packed.filename, tarballSha256: createHash('sha256').update(await readFile(tarball)).digest('hex'), files: names, freshCacheInstall: true, offlineInstallFromGeneratedLock: true, runtimePath: 'Node executable directory only', installDirectory: install.slice(output.length + 1), - inlineModule: true, smoke, processSmoke, queueSmoke, styleSmoke, componentSmoke, namedFonts }; + inlineModule: true, smoke, processSmoke, queueSmoke, styleSmoke, componentSmoke, namedFonts, batchSmoke }; await writeFile(join(output, 'verification.json'), JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify(report)); diff --git a/tools/verify-radial-gradients.py b/tools/verify-radial-gradients.py index fb7518f..ef560d2 100644 --- a/tools/verify-radial-gradients.py +++ b/tools/verify-radial-gradients.py @@ -17,7 +17,7 @@ assert renders['packageVersion'] == '0.8.6' and renders['engineVersion'] == '2.5.41' else: package = json.loads((ROOT / 'package.json').read_text(encoding='utf-8')) - assert renders['packageVersion'] == package['version'] and renders['engineVersion'] == package['fullbleed']['engineVersion'] == '2.5.43' + assert renders['packageVersion'] == package['version'] and renders['engineVersion'] == package['fullbleed']['engineVersion'] assert renders['tarballSha256'], 'Candidate must be rendered from the installed tarball.' report = verify(args.directory, baseline=args.negative_control) invalid = sum(bool(row['stitchingBounds']['invalid']) for row in report['cases']) diff --git a/tools/verify-release.py b/tools/verify-release.py index af6c8b9..46fa131 100644 --- a/tools/verify-release.py +++ b/tools/verify-release.py @@ -31,6 +31,7 @@ RADIAL_GRADIENT_SOURCE = ENGINE_REGRESSION_SOURCE.with_name('radial-gradient-endpoints-2.5.43.json') NAMED_FONT_SOURCE = ENGINE_REGRESSION_SOURCE.parents[1] / 'named-fonts.test.mjs' NAMED_FONT_FILE = ENGINE_REGRESSION_SOURCE.parents[1] / 'fonts/family/LibreCaslonText[wght].ttf' +BATCH_SOURCE = ENGINE_REGRESSION_SOURCE.parents[1] / 'batch-fixtures.mjs' def require(condition, message): @@ -77,7 +78,7 @@ def verify_engine_paint_evidence(report): source_sha = hashlib.sha256(source).hexdigest() names = {case['name'] for case in fixture['cases']} families = {case['family'] for case in fixture['cases']} - require(report.get('engine_version') == fixture['engineVersion'], 'Paint fixtures target another engine') + require(report.get('engine_version') in {fixture['engineVersion'], '2.5.46'}, 'Paint fixtures target another engine') evidence = report.get('engine_paint', []) require(len(evidence) == 2 and {item.get('platform') for item in evidence} == {'win32', 'linux'}, 'Paint inspection requires Windows and Linux evidence') @@ -101,18 +102,20 @@ def verify_engine_paint_evidence(report): def verify_cloudflare_evidence(report): - worker_documents = 8 if tuple(map(int, report['package_version'].split('.'))) >= (0, 8, 5) else 7 + release_version = tuple(map(int, report['package_version'].split('.'))) + worker_documents = 16 if release_version >= (0, 9, 0) else 8 if release_version >= (0, 8, 5) else 7 for key, source, documents in [('cloudflare_workers', CLOUDFLARE_SOURCE, worker_documents), ('cloudflare_starter', CLOUDFLARE_STARTER_SOURCE, 6)]: source_sha = hashlib.sha256(source.read_bytes()).hexdigest() evidence = report.get(key, []) + errors = 15 if key == 'cloudflare_workers' and release_version >= (0, 9, 0) else 10 require(len(evidence) == 2 and {item.get('platform') for item in evidence} == {'win32', 'linux'}, 'Workers evidence requires Windows and Linux local runtime checks') require(all(item.get('ok') is True and item.get('package_version') == report['package_version'] and item.get('engine_version') == report['engine_version'] and item.get('tarball_sha256') == report['package_sha256'] and item.get('source_sha256') == source_sha and item.get('documents') == documents - and item.get('error_cases') == 10 and item.get('node_bytes_identical') is True + and item.get('error_cases') == errors and item.get('node_bytes_identical') is True and item.get('recovery') is True and item.get('server_stopped') is True and item.get('independent_reader') is True and item.get('local_only') is True and item.get('hosted_verified') is False and item.get('free_plan_verified') is False @@ -256,7 +259,7 @@ def verify_text_decorations_evidence(report): expectations_sha = hashlib.sha256(TEXT_DECORATIONS_EXPECTATIONS.read_bytes()).hexdigest() oracle = json.loads(TEXT_DECORATIONS_EXPECTATIONS.read_bytes()) require(oracle['inputSha256'] == source_sha and oracle['engineVersion'] == '2.5.40' - and report.get('engine_version') in {'2.5.40', '2.5.41', '2.5.43'}, + and report.get('engine_version') in {'2.5.40', '2.5.41', '2.5.43', '2.5.46'}, 'Text decorations target another core or fixture') names = sorted(row['name'] for row in oracle['cases']) failing_names = sorted(row['name'] for row in oracle['cases'] if row['previousEngineFails']) @@ -344,7 +347,7 @@ def verify_radial_gradient_evidence(report): evidence = report.get('radial_gradients', []) require(len(evidence) == 2 and {row.get('platform') for row in evidence} == {'linux', 'win32'}, 'Radial gradients require separate Linux and Windows installed-package evidence') - require(report.get('engine_version') == '2.5.43', 'Radial evidence targets another engine') + require(report.get('engine_version') in {'2.5.43', '2.5.46'}, 'Radial evidence targets another engine') for row in evidence: require(row.get('ok') is True and row.get('negative_control') is False and row.get('package_version') == report.get('package_version') @@ -371,6 +374,53 @@ def verify_radial_gradient_evidence(report): 'Actual public 0.8.6 radial-gradient failure control is absent or uncalibrated') +def verify_batch_evidence(report): + expected_pages = {'batch-fixed': 2, 'batch-reflow': 5, 'batch-missing': 2, 'batch-components': 2, + 'batch-literal': 1, 'batch-fallback-fixed': 2, 'batch-fallback-reflow': 2} + require(report.get('batch_source_sha256') == hashlib.sha256(BATCH_SOURCE.read_bytes()).hexdigest(), + 'Batch evidence targets another fixture source') + installed = report.get('installed_matrix', []) + require(len(installed) == 9, 'Batch rendering requires all nine installed-package jobs') + for item in installed: + batch = item.get('batch_smoke', {}) + require(batch.get('ok') is True and batch.get('queueClosed') is True and batch.get('tests', 0) >= 28 + and batch.get('packageVersion') == report['package_version'] + and batch.get('engineVersion') == report['engine_version'] + and item.get('tarball_sha256') == report['package_sha256'], + 'Installed batch lifecycle evidence is incomplete or targets another package') + fixtures = batch.get('fixtures', []) + require(len(fixtures) == 7 and {row.get('name') for row in fixtures} == set(expected_pages), + 'Installed batch cases are missing or duplicated') + require(all(row.get('pages') == expected_pages[row['name']] and row.get('cjsProcessAndQueueEqual') is True + and re.fullmatch(r'[a-f0-9]{64}', row.get('pdf', '')) for row in fixtures), + 'Batch output or CommonJS/process/queue comparison is incomplete') + examples = batch.get('readmeExamples', []) + require(len(examples) == 2 and {row.get('name'): row.get('pages') for row in examples} + == {'readme-batch-1': 2, 'readme-batch-2': 1} + and all(re.fullmatch(r'[a-f0-9]{64}', row.get('unmodifiedExampleSha256', '')) for row in examples), + 'Both actual installed README batch examples must run') + readers = report.get('batch_readers', []) + require(len(readers) == 2 and {row.get('platform') for row in readers} == {'linux', 'win32'}, + 'Batch PDFs require separate Linux and Windows independent-reader evidence') + require(all(row.get('ok') is True and row.get('package_version') == report['package_version'] + and row.get('engine_version') == report['engine_version'] and row.get('tarball_sha256') == report['package_sha256'] + and row.get('source_sha256') == report['batch_source_sha256'] and row.get('documents') == 9 and row.get('pages') == 19 + and row.get('record_ranges_verified') is True and row.get('glyph_reports_verified') is True for row in readers), + 'Independent batch text, range or character evidence is incomplete') + browsers = report.get('browser_matrix', []) + require(len(browsers) == 3 and {row.get('browser') for row in browsers} == {'chrome', 'firefox', 'webkit'}, + 'Batch rendering requires all three browser download checks') + require(all(row.get('batch_cases') == 7 and row.get('batch_source_sha256') == report['batch_source_sha256'] + and row.get('batch_reports_equal') is True and row.get('batch_records_snapshot') is True + and row.get('batch_failure_recovery') is True for row in browsers), + 'Browser batch reports, snapshots or failure recovery are incomplete') + native_rows = [row for row in report.get('native_wasi_fixtures', []) if row.get('batch')] + require(len(native_rows) == 7 and {row.get('name') for row in native_rows} == set(expected_pages) + and all(row.get('pages') == expected_pages[row['name']] and row.get('nativeWasiReportsEqual') is True + and row.get('nativeWasiPdfEqual') is True and row.get('nativeWasiPreviewsEqual') is True for row in native_rows), + 'Native/WASI batch PDF, preview or record-report evidence is incomplete') + + def verify_binding(version, run_id, run, release, tag_commit, report): require(re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+', version), 'Use a stable numeric version') require(re.fullmatch(r'[0-9]+', run_id), 'Use a numeric CI run ID') @@ -441,7 +491,7 @@ def verify_binding(version, run_id, run, release, tag_commit, report): hidden_sha = hashlib.sha256(hidden_source).hexdigest() hidden_fixture = json.loads(hidden_source) hidden_cases = hidden_fixture['cases'] - require(report.get('engine_version') == hidden_fixture['engineVersion'], + require(report.get('engine_version') in {hidden_fixture['engineVersion'], '2.5.46'}, 'Hidden-text fixtures target another engine') hidden = report.get('hidden_text', []) require(len(hidden) == 2 and {item.get('platform') for item in hidden} == {'win32', 'linux'}, @@ -486,6 +536,8 @@ def verify_binding(version, run_id, run, release, tag_commit, report): verify_preformatted_evidence(report) if release_version >= (0, 8, 7): verify_radial_gradient_evidence(report) + if release_version >= (0, 9, 0): + verify_batch_evidence(report) def verify_tarball(data, info, report): @@ -528,6 +580,8 @@ def verify_tarball(data, info, report): 'dist/jsx-runtime.js', 'dist/jsx-runtime.d.cts'}.issubset(names), 'Component/JSX package files are missing') if tuple(map(int, info['version'].split('.'))) >= (0, 8, 0): require({'dist/cloudflare.js', 'src/cloudflare.d.ts'}.issubset(names), 'Workers package files are missing') + if tuple(map(int, info['version'].split('.'))) >= (0, 9, 0): + require({'src/batch-report.cjs', 'src/batch.d.ts', 'dist/batch.d.cts'}.issubset(names), 'Batch runtime or type files are missing') return sha diff --git a/tools/verify-text-decorations.py b/tools/verify-text-decorations.py index e7256d8..2711b96 100644 --- a/tools/verify-text-decorations.py +++ b/tools/verify-text-decorations.py @@ -54,7 +54,9 @@ assert renders['packageVersion'] == '0.8.3' and renders['engineVersion'] == '2.5.38' else: # Preserve the independently captured 2.5.40 reference outputs for this engine upgrade. - assert renders['engineVersion'] == '2.5.43' and oracle['engineVersion'] == '2.5.40' + package = json.loads((repo / 'package.json').read_text(encoding='utf-8')) + assert renders['packageVersion'] == package['version'] and renders['engineVersion'] == package['fullbleed']['engineVersion'] + assert oracle['engineVersion'] == '2.5.40' assert re.fullmatch(r'[a-f0-9]{64}', renders['tarballSha256']) modes = {'browser'} if args.browser else {'worker', 'process'} assert len(renders['cases']) == len(fixtures) * len(modes) From e380411a6630bd361b0cf10e05915a2eb93967ac Mon Sep 17 00:00:00 2001 From: krflol Date: Fri, 30 Oct 2026 16:52:39 -0500 Subject: [PATCH 2/3] Align Workers starter and runtime guides with version 0.9.0 --- docs/browser.md | 18 +++++++++++++----- docs/cloudflare.md | 19 ++++++++++++++----- examples/cloudflare/package.json | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/docs/browser.md b/docs/browser.md index 7477016..668a117 100644 --- a/docs/browser.md +++ b/docs/browser.md @@ -1,7 +1,7 @@ # Generate PDFs in a browser -The `fullbleed/browser` entry in Fullbleed 0.8.7 renders static HTML/CSS in a -dedicated Web Worker. It uses the same pinned 2.5.43 engine and bundled fonts as +The `fullbleed/browser` entry in Fullbleed 0.9.0 renders static HTML/CSS in a +dedicated Web Worker. It uses the same pinned 2.5.46 engine and bundled fonts as the Node entry. The result is a `Uint8Array`, suitable for a PDF `Blob`, plus optional PNG previews. No PDF server or account is required. @@ -21,7 +21,7 @@ Install the package in your web project, then copy its browser files into the directory your application serves as static files: ```sh -npm install --save-exact fullbleed@0.8.7 +npm install --save-exact fullbleed@0.9.0 npx fullbleed-browser-assets public/fullbleed ``` @@ -76,6 +76,14 @@ document off the page's JavaScript thread. Normal browser HTTP caching can reuse the static files according to your server's headers. There is no persistent renderer pool or offline-service-worker installation. +For documents sharing one template, use `renderer.renderPdfBatch()` with +string-valued `records`. It returns one combined PDF and a `records` array of +page ranges and selected-font character reports. Reflow is the default; choose +`layout: 'fixed'` to retain the compiled geometry and fonts. Up to 10,000 records +are accepted, and their keys and values share the 4,000,000-byte source budget. +See the [batch authoring guide](../README.md#generate-a-batch-from-one-template) +for HTML and component examples. Download `result.pdf` with the same Blob flow. + ## Inputs, limits and errors `renderer.renderPdf(options)` returns `{ pdf, previews, pages, missingGlyphs, @@ -83,7 +91,7 @@ engineVersion }`. `pdf` and each preview are byte arrays backed by ordinary `ArrayBuffer` objects. A preview can be displayed with an `image/png` Blob URL; release those URLs when replacing a preview or unmounting the component. -Options are `html`, `css`, `fonts`, `assets`, `previewDpi`, `timeoutMs`, `maxPages`, +Options are `html`, `css`, `defaultStyles`, `fonts`, `assets`, `previewDpi`, `timeoutMs`, `maxPages`, `allowMissingGlyphs` and `signal`. They follow the [Node input rules](../README.md#api): 4,000,000 UTF-8 bytes of HTML/CSS, 64 MiB of supplied fonts/assets, previews at 36–300 DPI or disabled, a default 30-second deadline and 1,000-page limit. @@ -117,7 +125,7 @@ WebAssembly instance has a 512 MiB ceiling, and previews need additional memory. `FullbleedError.code` includes `INVALID_INPUT`, `UNSUPPORTED_BROWSER`, `VERSION_MISMATCH`, `WORKER_FAILED`, `ENGINE_LOAD_FAILED`, `MISSING_GLYPHS`, -`PAGE_LIMIT`, `TIMEOUT`, `ABORTED`, `ENGINE_LIMIT`, `RENDER_FAILED` and +`INVALID_BINDINGS`, `PAGE_LIMIT`, `TIMEOUT`, `ABORTED`, `ENGINE_LIMIT`, `RENDER_FAILED` and `PREVIEW_FAILED`. Version mismatch means the installed client and hosted worker must be deployed together. Engine-load errors can indicate missing or changed files, blocked requests or a content security policy that prevents WebAssembly. diff --git a/docs/cloudflare.md b/docs/cloudflare.md index 2e4d579..2734770 100644 --- a/docs/cloudflare.md +++ b/docs/cloudflare.md @@ -5,9 +5,9 @@ the package's pinned WebAssembly engine and four font faces with your applicatio The renderer uses an in-memory filesystem and produces PDF bytes plus optional PNG previews. HTML/CSS and document data stay in the Worker handling the request. -This entry is checked in local `workerd` on Windows and Linux. A hosted -[Workers Playground smoke test](#hosted-playground-smoke-test) also reproduces the -invoice starter's PDFs. Production CPU, memory, startup behavior, and Free-plan +This entry is checked in local `workerd` on Windows and Linux. The historical +[Workers Playground smoke test](#hosted-playground-smoke-test) covers version +0.8.0. Production CPU, memory, startup behavior, and Free-plan suitability still require testing in your deployment. See [Cloudflare's current limits](https://developers.cloudflare.com/workers/platform/limits/). @@ -21,7 +21,7 @@ local tests for normal, long, escaped and invalid invoices. Use Node.js 22 or newer for your development tools: ```sh -npm install --save-exact fullbleed@0.8.7 +npm install --save-exact fullbleed@0.9.0 npm install --save-dev --save-exact wrangler@4.151.3 ``` @@ -96,6 +96,14 @@ its stylesheet embedded. This also renders with `css: ''` and `defaultStyles: false`; the Workers checks compare both forms' PDF and preview bytes. Remote stylesheets are not fetched. +Import `renderPdfBatch` from the same entry for documents sharing one template. +It accepts string-valued `records`, defaults to reflow, and returns one combined +PDF with page ranges and selected-font character reports for each record. +Record keys and values share the 1,000,000-byte source budget; the 20-page default +applies to the whole PDF. Up to 10,000 records are accepted. See the +[batch authoring guide](../README.md#generate-a-batch-from-one-template) for the +fixed-layout option, component templates, and missing-character policy. + ## Options and execution `renderPdf(options)` returns a Promise for `{ pdf, previews, pages, @@ -148,7 +156,8 @@ python tools/verify-cloudflare.py The Workers check uses the isolated tarball installation. It compares PDF and PNG bytes with Node for Northstar, Common Ground, a custom font/SVG fixture, and -component-authored documents. It exercises input budgets, unsupported controls, +component-authored documents, and seven fixed/reflow batch cases. It compares +per-record reports and exercises input budgets, unsupported controls, asset paths, page limits, missing glyphs, repeated calls, and failure recovery. Runtime fetching and asynchronous Wasm compilation are disabled during the check. An independent PDF reader checks pages, expected text, and the custom font face; diff --git a/examples/cloudflare/package.json b/examples/cloudflare/package.json index 2edda77..1cfad7e 100644 --- a/examples/cloudflare/package.json +++ b/examples/cloudflare/package.json @@ -11,6 +11,6 @@ "test": "node --test test/request.test.mjs", "test:worker": "node test/worker.mjs" }, - "dependencies": { "fullbleed": "0.8.7" }, + "dependencies": { "fullbleed": "0.9.0" }, "devDependencies": { "wrangler": "4.151.3" } } From 4f94363b7de7bf46f037c779809ad8bfd4d4bf15 Mon Sep 17 00:00:00 2001 From: krflol Date: Fri, 30 Oct 2026 17:15:19 -0500 Subject: [PATCH 3/3] Bind unchanged font layout expectations to engine 2.5.46 --- .github/workflows/ci.yml | 4 +- test/fixtures/inline-layout-2.5.46.json | 94 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/inline-layout-2.5.46.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3863319..e251578 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -119,7 +119,7 @@ jobs: npm run render:fonts -- --package-root output/font-baseline-consumer/node_modules/fullbleed --out output/font-baseline npm run verify:fonts -- --evidence-root output/font-baseline --allow-legacy-metadata npm run render:fonts - npm run verify:fonts -- --baseline output/font-baseline --reviewed-layout test/fixtures/inline-layout-2.5.43.json + npm run verify:fonts -- --baseline output/font-baseline --reviewed-layout test/fixtures/inline-layout-2.5.46.json - name: Verify installed custom font families against explicit faces run: | npm run render:families @@ -232,7 +232,7 @@ jobs: run: | python -m pip install pypdf==6.19.0 pypdfium2==5.14.0 fonttools==4.65.0 pillow==12.3.0 npm run render:fonts - npm run verify:fonts -- --reviewed-layout test/fixtures/inline-layout-2.5.43.json + npm run verify:fonts -- --reviewed-layout test/fixtures/inline-layout-2.5.46.json npm run render:families npm run verify:families npm run render:inline diff --git a/test/fixtures/inline-layout-2.5.46.json b/test/fixtures/inline-layout-2.5.46.json new file mode 100644 index 0000000..22bc72f --- /dev/null +++ b/test/fixtures/inline-layout-2.5.46.json @@ -0,0 +1,94 @@ +{ + "schema": "fullbleed.reviewed_inline_layout.v1", + "engineVersion": "2.5.46", + "source": "https://crates.io/crates/fullbleed/2.5.46", + "reason": "Engine 2.5.46 must preserve the reviewed 2.5.43 outputs. Expected PDF, preview and text values are unchanged.", + "fonts": [ + { + "name": "Inter-Variable.ttf", + "path": "fonts/Inter-Variable.ttf", + "bytes": 876576, + "sha256": "29160a80ff49ddcab2c97711247e08b1fab27a484a329ce8b813d820dc559031" + }, + { + "name": "DMSerifDisplay-Regular.ttf", + "path": "fonts/DMSerifDisplay-Regular.ttf", + "bytes": 76580, + "sha256": "8cc3643535edf039aa5d95440a8542735e9197e4f4b8d9303e980fefbf5ab616" + }, + { + "name": "DMSerifDisplay-Italic.ttf", + "path": "fonts/DMSerifDisplay-Italic.ttf", + "bytes": 71208, + "sha256": "df74c0ac387baeaeb0fe4f2324e1668e6a3ed8c09cd9796fe162c71753e19e45" + }, + { + "name": "BebasNeue-Regular.ttf", + "path": "fonts/BebasNeue-Regular.ttf", + "bytes": 61400, + "sha256": "08e4623805102d819f58601e46e345648846075e363b2ceb23313c2d1c83ec73" + }, + { + "name": "NotoSansMath-Regular.ttf", + "path": "fonts/NotoSansMath-Regular.ttf", + "bytes": 590904, + "sha256": "80b61fd613d3519197e64fff6f7e71fdc7f3e6526440ea4115b554ef7fd59af7" + } + ], + "fixtures": { + "invoice": { + "htmlSha256": "195725eeeb32c521e19b47dbacbb55048bebb6df8566ab135cdc41fbf33fe57a", + "cssSha256": "cda22b39ab1f7a1eeac7cd76d162b05cd3568c2430c099123ba30a2fbd58a296", + "pages": 1, + "text": "Northstar\nStudio\nD E S I G N W I T H D I R E C T I O N\nB R A N D\nD I G I T A L\nE X P E R I E N C E S\nT H A T M A T T E R .\n42 Cedar Avenue\nPortland, OR 97205\nhello@example.invalid\nnorthstar.example.invalid\nINVOICE\nGOOD WORK.\nCLEAR DETAILS.\nLET'S MAKE\nWHAT'S NEXT.\nP R E P A R E D F O R\nMaple & Finch\nAttn. Avery Morgan\nPortland, Oregon\nT H E P R O J E C T\nAutumn launch\nBrand workshop, digital\ndesign & handoff\nI N V O I C E N S - 1 0 4 2\nIssued · 01 October 2026\nDue · 31 October 2026\nCurrency · USD\nDESCRIPTION HOURS RATE AMOUNT\nDiscovery & brand workshop\nPositioning, priorities, and a clear creative direction\n8 $125.00 $1,000.00\nDigital design & implementation\nA cohesive visual system, from layout to launch\n6 $95.00 $570.00\nReview & studio handoff\nFinal refinements, documentation, and delivery\n2 $150.00 $300.00\nA little clarity.\nA lot of possibility.\nThank you for inviting us\nto help shape your next chapter.\nSubtotal $1,870.00\nTax · 0% in this sample $0.00\nT O T A L D U E · U S D\n$1,870.00\nP A Y M E N T D E T A I L S\nReference NS-1042 with your payment.\nPayment instructions are supplied separately.\nA N O T E F R O M T H E S T U D I O\nQuestions about a line item?\nWrite to hello@example.invalid.\nNORTHSTAR STUDIO / INVOICE NS-1042 FICTIONAL SAMPLE · 01 / 01", + "pdfSha256": "3078d155f30536f5ad721f7be3c241cd78661ae41249131fc0fd65f37f23e408", + "previewsSha256": [ + "72420749ccc3deaa8025daf7baffcdf79be716b6e8e93ed605effebed01e86c7" + ], + "pdfiumPixelSha256": [ + "d71293741974bef9abffbdf5d88b0827b48fe6523f8d9baf46f46fe1000895a5" + ] + }, + "report": { + "htmlSha256": "46b0e975d43914c8b4492218feae09ea0ad684091a9f9ee3b18f66a87202e521", + "cssSha256": "519591ae75c239ec602324f008dd99108ebeae0b9306df7215fa5f0442905473", + "pages": 3, + "text": "Common Ground\nC O M M U N I T Y T R U S T\nP E O P L E\nP L A C E S\nP O S S I B I L I T I E S\nCOMMON\nGROUND\nStronger places.\nMore room\nfor everyone.\n2026\nC O M M U N I T Y R E V I E W\nA Y E A R\nI N C O M M O N\n0 2 / O U R R E A C H\nSmall actions,\nshared outcomes.\n0 3 / O U R R E S O U R C E S\nWhere the\nsupport goes.\nCOMMON GROUND / COMMUNITY REVIEW FICTIONAL DATA · 01 / 03Common Ground\n0 2 / O U R R E A C H\nA P L A C E T O B E G I N\nSmall actions.\nShared outcomes.\nAn open door, a working kitchen, a garden plot. This year, our fictional neighborhood\nprograms connected more people with practical spaces to learn, make, and belong.\n1,240\nhouseholds took part\nacross four neighborhoods\n4,860\nvolunteer hours\nshared across our programs\n12\ncommunity spaces\nkept open and active\nMore neighbors, each quarter.\nP A R T I C I P A T I N G H O U S E H O L D S\n420\n560\n790\n980\nQ1 Q2 Q3 Q4\nQuarterly participation counts; a household can participate in more than one quarter.\n“Belonging starts\nwith somewhere\nto show up.”\nO U R P R O G R A M P R I N C I P L E\nBuilt around daily life.\nEvening workshops, shared meals, and weekend garden\nsessions made participation easier for people with different\nschedules. The next step is a longer opening window at\neach neighborhood space.\nILLUSTRATIVE ANNUAL REVIEW / NOT AN ACTUAL ORGANIZATION 02 / 03Common Ground\n0 3 / O U R R E S O U R C E S\nF O L L O W T H E S U P P O R T\nGood intentions.\nClear allocations.\nA practical view of this fictional operating budget. Every category supports the same\naim: useful places, open to more people.\nPROGRAM AREA ALLOCATION SHARE\nNeighborhood spaces $168,000.00 40%\nLearning & workshops $126,000.00 30%\nShared meals & gardens $84,000.00 20%\nAccess & transport $42,000.00 10%\nT O T A L O P E R A T I N G A L L O C A T I O N · U S D $420,000.00\nWhat we count.\nHouseholds are deduplicated within the annual program\nregister. Volunteer hours include recorded event and\nmaintenance time. Space counts refer to year-round\nlocations, not individual events.\nQuarterly counts show participation within each period and\nmust not be added to estimate unique annual reach.\nWhat comes next.\nExtend evening access. Improve step-free entrances.\nPublish a simple monthly view of program activity and\nspending.\nA shared tomorrow\nis built in the everyday.\nCOMMON GROUND / METHODS & ALLOCATIONS FICTIONAL DATA · 03 / 03", + "pdfSha256": "80fc4306db1898688d846cfea4fb1b4ef5bf07713fe9301662143619441205b5", + "previewsSha256": [ + "33ee3398d59f6836368cdab0f3c725c3cf12cbf2f9f3a3ac6a6b168f6c1886f5", + "9e883a04ab32392aa87ef5931761285b8dc15f8fd642b4f9bbea582c044bcdbe", + "8c8bfdc01ef7a3c376fbe61a0736dbc5b65280b0781f0040c550e097e71a5dcd" + ], + "pdfiumPixelSha256": [ + "c7dc8db8fc08a78f74c4477629e0f68f3b34aa37b9b9eba46a977805933d3f03", + "50298b3a05370f4dcefedf47b121c1da74e48307e85fac695dfbc5c1207b100e", + "55c5468322b64ae2583439553a13057218c418d38c4fcbeb0e492ec98160961c" + ] + } + }, + "baselineSource": "https://crates.io/crates/fullbleed/2.5.24", + "previousReference": "inline-layout-2.5.41.json", + "previousReferenceSha256": "e6c796356e3b1360885ad3ea9c1ae27adda484f76cc6fee9c1f31fb7c9eae828", + "previousReview": { + "unchangedFixtures": [ + "invoice", + "inter", + "dm-serif", + "dm-serif-italic", + "bebas-neue", + "math" + ], + "reportPages": 3, + "changedGlyphsPerPage": [ + 0, + 12, + 12 + ], + "change": "Common Ground heading baseline: 0.75pt down on pages 2 and 3.", + "allContentPreserved": true, + "allGlyphsWithinPage": true, + "publicPythonCore": "2.5.34", + "publicPythonPdfSha256": "80fc4306db1898688d846cfea4fb1b4ef5bf07713fe9301662143619441205b5" + } +}