$ show thx/resvg-js
This is a high-performance SVG rendering library for Node.js, Deno, Bun, and browsers, powered by the Rust `resvg` engine. It converts SVG to PNG/AVIF with support for cropping, scaling, background colors, custom fonts (including WOFF2), bounding box extraction, and simplified SVG output. The library is implemented as a native addon via `napi-rs` for optimal speed on server-side runtimes, and also ships a WebAssembly build for browsers and edge environments. The codebase is well-structured, with clean separation between Rust core (rendering, option handling, font management) and JavaScript/TypeScript bindings. It includes comprehensive test suites, cross-platform CI/CD with prebuilt binaries for 18+ platforms, and thorough examples. The architecture demonstrates high craftsmanship in memory management (ObjectFinalize), error handling (panic catching), and pattern reuse (shared crop logic between NAPI and WASM).
The LLM identified 4 files worth highlighting for reuse:
Implements `RenderedImage` with `ObjectFinalize` and `adjust_external_memory` to properly manage Rust-allocated pixel buffers in Node.js native addon memory accounting. This pattern is reusable for any napi-rs project that needs to avoid memory leaks when transferring large binary data to JavaScript.
Defines `FitToDef` enum and `CropOptions` struct with custom serde deserialization from JavaScript objects. The `FitToDef` enum elegantly models scaling modes (original, width, height, zoom) and its serialization/deserialization logic can be extracted for any Rust library that needs to accept polymorphic options from JS bindings.
Contains `render_inner_catch_unwind` which wraps Rust panics with `std::panic::catch_unwind` and converts them into JavaScript errors. This pattern is essential for any Rust-based Node.js addon using napi-rs where panics must not crash the Node.js process, and can be reused as a generic wrapper for fallible operations.
Implements separate font loading strategies for native (system fonts + custom files/directories) and WASM environments (preloaded buffer fonts including WOFF2). The pattern of conditionally compiling font sources and providing fallback logic is reusable for any cross-platform Rust library that needs to handle font availability differences.