Skip to main content

npm_utils/
lib.rs

1//! Pure-Rust utilities for the npm registry and web assets.
2//!
3//! Building blocks for fetching browser/JS dependencies at build time without
4//! Node or npm:
5//!
6//! - [`registry`] — talk to an npm registry: build tarball URLs, fetch a
7//!   package's metadata, and resolve the newest version matching a semver range.
8//! - [`download`] — fetch bytes over HTTP (with a retry) and build GitHub
9//!   archive URLs.
10//! - [`extract`] — unpack `.tar.gz` and `.zip` archives into a destination
11//!   directory, selecting all files, an explicit file map, or a predicate, with
12//!   path-traversal protection.
13//! - [`cache`] — content-hash markers, a cross-process build lock, and directory
14//!   helpers for skip-if-unchanged download caches.
15//! - [`package_json`] — read pinned dependency versions from a `package.json`, and
16//!   resolve its `exports`/`module`/`browser`/`main` to browser entry points (for
17//!   generating an ES-module import map).
18//! - [`install`] — resolve a `package.json`'s transitive dependency graph and extract
19//!   the tree into a `node_modules/` directory (a minimal, pure-Rust "npm install").
20//!
21//! ```no_run
22//! use npm_utils::{download, extract, registry::Registry};
23//!
24//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
25//! let reg = Registry::npm();
26//! let lit = reg.resolve("lit", &"^3".parse()?)?;
27//! let tgz = download::fetch(&lit.tarball_url)?;
28//! extract::tar_gz(&tgz, "dist/lit".as_ref(), Some("package/"), extract::Select::All)?;
29//! # Ok(()) }
30//! ```
31
32pub mod cache;
33pub mod download;
34pub mod extract;
35pub mod install;
36pub mod package_json;
37pub mod registry;