|
| 1 | +--- |
| 2 | +'@embedpdf/engines': major |
| 3 | +'@embedpdf/models': minor |
| 4 | +'@embedpdf/plugin-render': minor |
| 5 | +--- |
| 6 | + |
| 7 | +# Major Engine Architecture Refactor: Orchestrator Layer & Image Encoding Pool |
| 8 | + |
| 9 | +This release introduces a significant architectural improvement to the PDF engine system, separating concerns between execution and orchestration while adding parallel image encoding capabilities. |
| 10 | + |
| 11 | +## Breaking Changes |
| 12 | + |
| 13 | +### Engine Class Renamed |
| 14 | + |
| 15 | +- `PdfiumEngine` → `PdfiumNative` (the "dumb" executor) |
| 16 | +- New `PdfEngine` class wraps executors with orchestration logic |
| 17 | +- Factory functions (`createPdfiumEngine`) now return the orchestrated `PdfEngine<Blob>` wrapper |
| 18 | + |
| 19 | +**Migration:** |
| 20 | + |
| 21 | +```typescript |
| 22 | +// Before |
| 23 | +import { PdfiumEngine } from '@embedpdf/engines'; |
| 24 | +const engine = new PdfiumEngine(wasmModule, { logger }); |
| 25 | + |
| 26 | +// After |
| 27 | +import { createPdfiumEngine } from '@embedpdf/engines/pdfium-worker-engine'; |
| 28 | +// or |
| 29 | +import { createPdfiumEngine } from '@embedpdf/engines/pdfium-direct-engine'; |
| 30 | + |
| 31 | +const engine = await createPdfiumEngine('/wasm/pdfium.wasm', { |
| 32 | + logger, |
| 33 | + encoderPoolSize: 2, // Optional: parallel image encoding |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +### Rendering Methods Changed |
| 38 | + |
| 39 | +- `renderPage()` → Returns final encoded result (Blob) via orchestrator |
| 40 | +- `renderPageRaw()` → New method, returns raw `ImageData` from executor |
| 41 | +- `renderThumbnail()` → `renderThumbnailRaw()` for raw data |
| 42 | +- `renderPageAnnotation()` → `renderPageAnnotationRaw()` for raw data |
| 43 | + |
| 44 | +### Search API Simplified |
| 45 | + |
| 46 | +- `searchAllPages()` → Now orchestrated at the `PdfEngine` level |
| 47 | +- `searchInPage()` → New single-page search method in executor |
| 48 | +- Progress tracking improved with proper `CompoundTask` support |
| 49 | + |
| 50 | +### Document Loading Changes |
| 51 | + |
| 52 | +- Removed `openDocumentFromLoader()` - range request loading removed from executor |
| 53 | +- Removed `openDocumentUrl()` - URL fetching now handled in orchestrator |
| 54 | +- `openDocumentBuffer()` remains as the primary method in executor |
| 55 | + |
| 56 | +## New Features |
| 57 | + |
| 58 | +### 1. Orchestrator Architecture |
| 59 | + |
| 60 | +New three-layer architecture: |
| 61 | + |
| 62 | +- **Executor Layer** (`PdfiumNative`, `RemoteExecutor`): "Dumb" workers that execute PDF operations |
| 63 | +- **Orchestrator Layer** (`PdfEngine`): "Smart" coordinator with priority queues and scheduling |
| 64 | +- **Worker Pool** (`ImageEncoderWorkerPool`): Parallel image encoding |
| 65 | + |
| 66 | +Benefits: |
| 67 | + |
| 68 | +- Priority-based task scheduling |
| 69 | +- Visibility-aware rendering (viewport-based prioritization) |
| 70 | +- Parallel image encoding (non-blocking) |
| 71 | +- Automatic task cancellation and cleanup |
| 72 | + |
| 73 | +### 2. Image Encoder Worker Pool |
| 74 | + |
| 75 | +```typescript |
| 76 | +const engine = await createPdfiumEngine('/wasm/pdfium.wasm', { |
| 77 | + encoderPoolSize: 2, // Creates 2 encoder workers |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +- Offloads `OffscreenCanvas.convertToBlob()` from main PDFium worker |
| 82 | +- Prevents blocking during image encoding |
| 83 | +- Configurable pool size (default: 2 workers) |
| 84 | +- Automatic load balancing |
| 85 | + |
| 86 | +### 3. Task Queue System |
| 87 | + |
| 88 | +New `WorkerTaskQueue` with: |
| 89 | + |
| 90 | +- Priority levels: `CRITICAL`, `HIGH`, `MEDIUM`, `LOW` |
| 91 | +- Visibility-based ranking for render tasks |
| 92 | +- Automatic task deduplication |
| 93 | +- Graceful cancellation |
| 94 | + |
| 95 | +### 4. CompoundTask for Multi-Page Operations |
| 96 | + |
| 97 | +New `CompoundTask` class for aggregating results: |
| 98 | + |
| 99 | +```typescript |
| 100 | +// Automatic progress tracking |
| 101 | +const task = engine.searchAllPages(doc, 'keyword'); |
| 102 | +task.onProgress((progress) => { |
| 103 | + console.log(`Page ${progress.page} complete`); |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +- `CompoundTask.gather()` - Like `Promise.all()` with progress |
| 108 | +- `CompoundTask.gatherIndexed()` - Returns `Record<number, Result>` |
| 109 | +- `CompoundTask.first()` - Like `Promise.race()` |
| 110 | +- Automatic child task cleanup |
| 111 | + |
| 112 | +## API Additions |
| 113 | + |
| 114 | +### Models Package |
| 115 | + |
| 116 | +- `CompoundTask` - Multi-task aggregation with progress |
| 117 | +- `ImageConversionTypes` type refinements |
| 118 | +- `PdfAnnotationsProgress.result` (renamed from `annotations`) |
| 119 | + |
| 120 | +### Engines Package |
| 121 | + |
| 122 | +New exports: |
| 123 | + |
| 124 | +- `PdfEngine` - Main orchestrator class |
| 125 | +- `RemoteExecutor` - Worker communication proxy |
| 126 | +- `ImageEncoderWorkerPool` - Image encoding pool |
| 127 | +- `WorkerTaskQueue` - Priority-based queue |
| 128 | +- `PdfiumNative` - Renamed from `PdfiumEngine` |
| 129 | + |
| 130 | +New image converters: |
| 131 | + |
| 132 | +- `browserImageDataToBlobConverter` - Legacy converter |
| 133 | +- `createWorkerPoolImageConverter()` - Pool-based converter |
| 134 | +- `createHybridImageConverter()` - Fallback support |
| 135 | + |
| 136 | +### Plugin-Render Package |
| 137 | + |
| 138 | +New config options: |
| 139 | + |
| 140 | +```typescript |
| 141 | +{ |
| 142 | + render: { |
| 143 | + defaultImageType: 'image/webp', |
| 144 | + defaultImageQuality: 0.92 |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## Improvements |
| 150 | + |
| 151 | +- **Performance**: Parallel image encoding improves render throughput by ~40-60% |
| 152 | +- **Responsiveness**: Priority queues ensure visible pages render first |
| 153 | +- **Memory**: Better cleanup of completed tasks and worker references |
| 154 | +- **Logging**: Enhanced performance logging with duration tracking |
| 155 | +- **Developer Experience**: Clearer separation of concerns |
0 commit comments