diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c3a717..e251578 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 @@ -117,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 @@ -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 @@ -225,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 @@ -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: '
{{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/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/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{{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: '漢字
', 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/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" + } +} 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: '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: '{{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: Promiselost
' }); // @ts-expect-error Renderer options must remain checked for queue objects too. renderDocument(doc, queue, { timeoutMs: 'long' }); void [a, b, buffer]; +const batch: Promise{{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: '