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, resolve the newest version matching a semver range, and
8//! search the registry ([`Registry::search`](registry::Registry::search)).
9//! - [`download`] — fetch bytes over HTTP (with a retry) and build GitHub
10//! archive URLs.
11//! - [`extract`] — unpack `.tar.gz` and `.zip` archives into a destination
12//! directory, selecting all files, an explicit file map, or a predicate, with
13//! path-traversal protection.
14//! - [`path_safety`] — the path-traversal hardening shared by `extract` and
15//! `install`: reject `..`/absolute paths and refuse symlink-redirected writes.
16//! - [`cache`] — content-hash markers, a cross-process build lock, and directory
17//! helpers for skip-if-unchanged download caches.
18//! - [`package_json`] — read pinned dependency versions from a `package.json`, and
19//! resolve its `exports`/`module`/`browser`/`main` to browser entry points (for
20//! generating an ES-module import map).
21//! - [`install`] — produce a real `node_modules/` directory, pure Rust, with every tarball
22//! sha512-verified: resolve a `package.json`'s transitive `dependencies` against the registry
23//! ([`install::node_modules`]), or install the exact tree a `package-lock.json` pins —
24//! devDependencies included, `.bin` shims and all — an `npm ci` in Rust
25//! ([`install::from_lockfile`]).
26//! - [`project`] — mutate a `package.json` project: keep the lock + `node_modules/` in sync
27//! ([`project::sync`]), upgrade dependencies within their ranges with a dry-run plan
28//! ([`project::plan_upgrade`] / [`project::upgrade`]), and remove them ([`project::remove`]).
29//! - [`integrity`] — verify a downloaded tarball's `sha512` Subresource-Integrity (both
30//! install paths check it before trusting bytes).
31//! - [`sbom`] — render the packages a `package-lock.json` pins as a license summary, a CycloneDX
32//! 1.6 document, or an SPDX 2.3 document — compliance artifacts, pure Rust, no Node.
33//! - [`audit`] — check those same pinned packages against vulnerability advisories from multiple
34//! sources (npm's registry endpoint, OSV) behind a small source trait — `npm audit`, pure Rust.
35//!
36//! ```no_run
37//! use npm_utils::{download, extract, registry::Registry};
38//!
39//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
40//! let reg = Registry::npm();
41//! let lit = reg.resolve("lit", &"^3".parse()?)?;
42//! let tgz = download::fetch(&lit.tarball_url)?;
43//! extract::tar_gz(&tgz, "dist/lit".as_ref(), Some("package/"), extract::Select::All)?;
44//! # Ok(()) }
45//! ```
46
47#![forbid(unsafe_code)]
48
49/// The crate's boxed, thread-safe error type. A single alias so the whole crate shares one error
50/// spelling, errors cross thread boundaries, and a future switch to a structured enum is one edit.
51pub type Error = Box<dyn std::error::Error + Send + Sync>;
52/// The crate's result type, defaulting the error to [`Error`].
53pub type Result<T, E = Error> = std::result::Result<T, E>;
54
55// Vulnerability auditing (`npm audit`, pure Rust): check the packages a `package-lock.json` pins
56// against multiple advisory sources (npm's registry endpoint, OSV) behind a small source trait.
57pub mod audit;
58pub mod cache;
59// The command-line tool (`npm-utils` / `cargo npm-utils`), behind the `cli` feature so a default
60// library build pulls no `clap`. Drives the primitives below — `registry`, `install`, `project`, and
61// the `package_json` manifest/lock writers — for `install`/`ci`/`add`/`remove`/`init`/`upgrade`.
62#[cfg(feature = "cli")]
63pub mod cli;
64pub mod download;
65pub mod extract;
66pub mod install;
67pub mod integrity;
68// The npm `package.json` / `package-lock.json` schemas — a pure-parsing module (no IO),
69// modeled on the npm specs, with strict spec-conformance tests living beside it.
70pub mod package_json;
71pub mod path_safety;
72// Project mutations (`sync` / `upgrade` / `remove`) that keep package.json + lock + node_modules in
73// step — the IO orchestration over the pure `package_json` transforms and `registry`/`install`.
74pub mod project;
75pub mod registry;
76// License/SBOM output (license summary · CycloneDX · SPDX) for a parsed `package-lock.json`.
77pub mod sbom;
78// Crate-internal `npm-utils:` warning routing — the CLI installs a sink so library warnings print
79// above a live progress region instead of corrupting it.
80mod warn;