Skip to main content

npmrc_config_rs/
lib.rs

1//! Rust port of npm .npmrc configuration logic.
2//!
3//! This crate provides functionality to load and query npm configuration
4//! from `.npmrc` files, including support for:
5//!
6//! - Multiple config levels (global, user, project)
7//! - Scoped registries
8//! - Authentication (bearer tokens, basic auth, mTLS)
9//! - Environment variable expansion
10//!
11//! # Quick Start
12//!
13//! ```no_run
14//! use npmrc_config_rs::NpmrcConfig;
15//!
16//! // Load config from standard locations
17//! let config = NpmrcConfig::load().unwrap();
18//!
19//! // Get registry for a package
20//! let registry = config.registry_for("@myorg/package");
21//!
22//! // Get credentials for authentication
23//! if let Some(creds) = config.credentials_for(&registry) {
24//!     match creds {
25//!         npmrc_config_rs::Credentials::Token { token, .. } => {
26//!             println!("Using token: {}", token);
27//!         }
28//!         npmrc_config_rs::Credentials::BasicAuth { username, password, .. } => {
29//!             println!("Using basic auth: {}:***", username);
30//!         }
31//!         _ => {}
32//!     }
33//! }
34//! ```
35//!
36//! # Configuration Priority
37//!
38//! Configuration is loaded from multiple levels with the following priority
39//! (highest to lowest):
40//!
41//! 1. **Project** - `{localPrefix}/.npmrc` (found by walking up from cwd)
42//! 2. **User** - `~/.npmrc`
43//! 3. **Global** - `{globalPrefix}/etc/npmrc`
44//!
45//! Values from higher-priority sources override lower-priority ones.
46//!
47//! # Authentication (Nerf-Darting)
48//!
49//! npm uses "nerf-darting" to scope credentials to specific registries,
50//! preventing credential leakage. Registry URLs are converted to a
51//! canonical form:
52//!
53//! ```text
54//! https://registry.npmjs.org/ → //registry.npmjs.org/
55//! ```
56//!
57//! Auth configuration uses this format:
58//!
59//! ```ini
60//! //registry.npmjs.org/:_authToken = your-token
61//! //private.registry.com/:username = user
62//! //private.registry.com/:_password = base64-encoded-password
63//! ```
64
65mod auth;
66mod config;
67mod error;
68mod parser;
69mod paths;
70pub mod registry;
71
72// Re-export main types
73pub use auth::{nerf_dart, ClientCert, Credentials};
74pub use config::{ConfigData, LoadOptions, NpmrcConfig};
75pub use error::{Error, Result};
76pub use parser::{expand_env_vars, parse_bool};
77pub use paths::{
78    expand_tilde, find_global_prefix, find_local_prefix, global_config_path, project_config_path,
79    user_config_path,
80};