From 959739f609dddacde3bbe9ecede2f409214fb34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 23 May 2024 22:26:23 +0100 Subject: FUTURE: initial support for .npmrc file (#23560) This commit adds initial support for ".npmrc" files. Currently we only discover ".npmrc" files next to "package.json" files and discovering these files in user home dir is left for a follow up. This pass supports "_authToken" and "_auth" configuration for providing authentication. LSP support has been left for a follow up PR. Towards https://github.com/denoland/deno/issues/16105 --- cli/npm/common.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'cli/npm/common.rs') diff --git a/cli/npm/common.rs b/cli/npm/common.rs index e40924838..bf45aa5b8 100644 --- a/cli/npm/common.rs +++ b/cli/npm/common.rs @@ -1,5 +1,8 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use deno_npm::npm_rc::RegistryConfig; +use reqwest::header; + /// Gets the corresponding @types package for the provided package name. pub fn types_package_name(package_name: &str) -> String { debug_assert!(!package_name.starts_with("@types/")); @@ -8,6 +11,27 @@ pub fn types_package_name(package_name: &str) -> String { format!("@types/{}", package_name.replace('/', "__")) } +// TODO(bartlomieju): support more auth methods besides token and basic auth +pub fn maybe_auth_header_for_npm_registry( + registry_config: &RegistryConfig, +) -> Option<(header::HeaderName, header::HeaderValue)> { + if let Some(token) = registry_config.auth_token.as_ref() { + return Some(( + header::AUTHORIZATION, + header::HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(), + )); + } + + if let Some(auth) = registry_config.auth.as_ref() { + return Some(( + header::AUTHORIZATION, + header::HeaderValue::from_str(&format!("Basic {}", auth)).unwrap(), + )); + } + + None +} + #[cfg(test)] mod test { use super::types_package_name; -- cgit v1.2.3