System Architecture
doc-kit uses a dependency-driven generator orchestration system that manages the execution of documentation generators in the correct order, with support for parallel processing and streaming results.Core Components
The architecture consists of three main layers:- Generator Orchestration (
src/generators.mjs) - Manages dependency chains and execution order - Worker Thread Pool (
src/threading/) - Distributes work across CPU cores - Streaming System (
src/streaming.mjs) - Handles async generators and result collection
Dependency Chain
Generators can depend on the output of other generators using thedependsOn property:
Execution Order
The system automatically resolves dependencies and executes generators in the correct order:- Scheduling Phase - Recursively schedule generators and their dependencies
- Execution Phase - Execute generators once dependencies are resolved
- Collection Phase - Collect results from streaming generators
Generator Cache
The orchestration system maintains a cache of generator promises to:- Prevent duplicate execution - Each generator runs only once per pipeline
- Enable parallel collection - Multiple consumers can await the same generator
- Support streaming - Async generators are collected once and shared
Streaming vs Non-Streaming
Non-Streaming Generators
Return a promise that resolves to the complete result:Streaming Generators
Return an async generator that yields chunks of results:Parallel Processing
Generators withhasParallelProcessor: true receive a parallel worker instance:
Complete Pipeline Flow
Step-by-Step Execution
- Initialize - Create worker pool with specified thread count
- Schedule - Recursively schedule all requested generators and dependencies
- Execute - Start generator execution (dependencies first)
- Stream - For parallel generators, distribute work across threads
- Collect - Gather all results, collecting streaming generators
- Cleanup - Destroy worker pool and return results
Configuration Flow
Configuration is passed through the entire pipeline:- Global settings -
threads,chunkSize,input,output - Generator-specific config - From
configuration[generatorName]
Performance Characteristics
Memory Efficiency
- Streaming generators - Process and yield chunks incrementally
- Worker isolation - Each thread has its own memory space
- Shared cache - Results are cached to avoid recomputation
CPU Utilization
- Parallel processing - Work distributed across available CPU cores
- Async execution - Non-blocking I/O operations
- Lazy collection - Streaming generators only collected when needed
Optimal Configuration
Error Handling
The system handles errors at multiple levels:- Generator errors - Caught and logged by orchestration system
- Worker errors - Piscina handles thread crashes and restarts
- Dependency errors - Propagate to dependent generators
Next Steps
Worker Threads
Deep dive into the worker thread implementation
Streaming
Learn about async generators and streaming architecture