Core Components
FormatHandler Interface
TheFormatHandler interface is the foundation of the conversion system. Each conversion tool is wrapped in a handler that implements this interface.
src/FormatHandler.ts
Key Properties
name
name
A descriptive string identifying the tool or library being wrapped (e.g., “FFmpeg”, “canvasToBlob”).
supportedFormats
supportedFormats
An array of
FileFormat objects that specify which formats can be converted to/from by this handler.Each format includes:from: Whether conversion FROM this format is supportedto: Whether conversion TO this format is supported- Format metadata (name, extension, MIME type, category)
supportAnyInput
supportAnyInput
Optional flag for handlers that can accept any input format. These handlers are only used as a fallback when no direct conversion path is found.
ready
ready
Boolean flag indicating whether the handler has completed initialization and is ready to perform conversions.
FileFormat Structure
Formats define the characteristics of a file type:src/FormatHandler.ts
FileData Structure
File data is passed between handlers:src/FormatHandler.ts
TraversionGraph System
TheTraversionGraph class implements intelligent pathfinding to find optimal conversion routes between formats.
Graph Structure
The graph consists of:- Nodes: Represent unique file formats (identified by MIME type and format)
- Edges: Represent possible conversions between formats using a specific handler
- Costs: Each edge has a cost based on multiple factors
Pathfinding Algorithm
The system uses Dijkstra’s algorithm to find the optimal conversion path:src/TraversionGraph.ts
Cost Calculation
The pathfinding considers multiple cost factors:- Base Costs
- Quality Costs
- Priority Costs
Category Change Costs
Conversions between different media categories have specific costs:src/TraversionGraph.ts
The system can apply handler-specific category costs to avoid using tools that can’t perform certain types of conversions.
Handler Registry
All handlers are registered insrc/handlers/index.ts:
src/handlers/index.ts
Handlers are wrapped in try-catch blocks to ensure one failing handler doesn’t break the entire system.
Format Definitions
Common formats are defined insrc/CommonFormats.ts using the FormatDefinition class:
src/CommonFormats.ts
Conversion Flow
Format Detection
User uploads a file, and the system detects its format based on MIME type and extension.
Path Search
The TraversionGraph searches for the optimal conversion path from the input format to the desired output format.
Handler Execution
Each handler in the path is executed sequentially:
- Handler is initialized if not ready
doConvert()is called with input files- Output becomes input for the next handler
Categories
File formats are organized into categories:src/CommonFormats.ts
Formats can belong to multiple categories. For example, GIF is both “image” and “video”.
Key Design Principles
- Abstraction: Each tool is wrapped in a standard interface
- Flexibility: Multi-step conversions enable support for indirect format pairs
- Optimization: Cost-based pathfinding prefers shorter, lossless conversion paths
- Resilience: Failed handlers don’t crash the entire system
- Performance: Format caching reduces initialization time