pnp-rs
This crate implements the Yarn Plug'n'Play resolution algorithms for Rust so that it can be easily reused within Rust-based tools. It also includes utilities allowing to transparently read files from within zip archives.
Install
cargo add pnp
Resolution
Filesystem utilities
While PnP only deals with the resolution, not the filesystem, the file maps generated by Yarn rely on virtual filesystem layers for two reasons:
-
Virtual packages, which require a same package to have different paths to account for different set of dependencies (this only happens for packages that list peer dependencies)
-
Zip storage, which Yarn uses so the installed files never have to be unpacked from their archives, leading to faster installs and fewer risks of cache corruption.
To make it easier to work with these virtual filesystems, the pnp crate also includes a VPath enum that lets you resolve virtual paths, and a set of zip manipulation utils (open_zip_via_read by default, and open_zip_via_mmap if the mmap feature is enabled).
use ;
Cache reuse
Opening and dropping a zip archive for every single file access would be expensive. To avoid that, pnp-rs provides an helper class called LruZipCache which lets you abstract away the zip opening and closing, and only keep the most recently used archives open.
use ;
const ZIP_CACHE: = new;