diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 033b30d979..c65745e297 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -104,6 +104,64 @@ pub fn npm_registry_default_url() -> &'static Url { &NPM_REGISTRY_DEFAULT_URL } +pub fn deno_registry_url() -> &'static Url { + static DENO_REGISTRY_URL: Lazy = Lazy::new(|| { + let env_var_name = "DENO_REGISTRY_URL"; + if let Ok(registry_url) = std::env::var(env_var_name) { + // ensure there is a trailing slash for the directory + let registry_url = format!("{}/", registry_url.trim_end_matches('/')); + match Url::parse(®istry_url) { + Ok(url) => { + return url; + } + Err(err) => { + log::debug!( + "Invalid {} environment variable: {:#}", + env_var_name, + err, + ); + } + } + } + + deno_graph::source::DEFAULT_DENO_REGISTRY_URL.clone() + }); + + &DENO_REGISTRY_URL +} + +pub fn deno_registry_api_url() -> &'static Url { + static DENO_REGISTRY_API_URL: Lazy = Lazy::new(|| { + let env_var_name = "DENO_REGISTRY_API_URL"; + if let Ok(registry_url) = std::env::var(env_var_name) { + // ensure there is a trailing slash for the directory + let registry_url = format!("{}/", registry_url.trim_end_matches('/')); + match Url::parse(®istry_url) { + Ok(url) => { + return url; + } + Err(err) => { + log::debug!( + "Invalid {} environment variable: {:#}", + env_var_name, + err, + ); + } + } + } + + let host = deno_graph::source::DEFAULT_DENO_REGISTRY_URL + .host_str() + .unwrap(); + + let mut url = deno_graph::source::DEFAULT_DENO_REGISTRY_URL.clone(); + url.set_host(Some(&format!("api.{}", host))).unwrap(); + url + }); + + &DENO_REGISTRY_API_URL +} + pub fn ts_config_to_emit_options( config: deno_config::TsConfig, ) -> deno_ast::EmitOptions { @@ -1855,4 +1913,12 @@ mod test { ] ) } + + #[test] + fn deno_registry_urls() { + let reg_url = deno_registry_url(); + assert!(reg_url.as_str().ends_with('/')); + let reg_api_url = deno_registry_api_url(); + assert!(reg_api_url.as_str().ends_with('/')); + } } diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs index 81c1260987..214e2a3dcb 100644 --- a/cli/cache/mod.rs +++ b/cli/cache/mod.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::args::deno_registry_url; use crate::args::CacheSetting; use crate::errors::get_error_class_name; use crate::file_fetcher::FetchOptions; @@ -17,7 +18,6 @@ use deno_graph::source::LoadFuture; use deno_graph::source::LoadResponse; use deno_graph::source::Loader; use deno_runtime::permissions::PermissionsContainer; -use once_cell::sync::Lazy; use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; @@ -165,27 +165,9 @@ impl FetchCacher { } } -pub(crate) static DENO_REGISTRY_URL: Lazy = Lazy::new(|| { - let env_var_name = "DENO_REGISTRY_URL"; - if let Ok(registry_url) = std::env::var(env_var_name) { - // ensure there is a trailing slash for the directory - let registry_url = format!("{}/", registry_url.trim_end_matches('/')); - match Url::parse(®istry_url) { - Ok(url) => { - return url; - } - Err(err) => { - log::debug!("Invalid {} environment variable: {:#}", env_var_name, err,); - } - } - } - - deno_graph::source::DEFAULT_DENO_REGISTRY_URL.clone() -}); - impl Loader for FetchCacher { fn registry_url(&self) -> &Url { - &DENO_REGISTRY_URL + deno_registry_url() } fn get_cache_info(&self, specifier: &ModuleSpecifier) -> Option { diff --git a/cli/tools/registry/mod.rs b/cli/tools/registry/mod.rs index 410025b2a7..202d66ee98 100644 --- a/cli/tools/registry/mod.rs +++ b/cli/tools/registry/mod.rs @@ -27,6 +27,7 @@ use serde::de::DeserializeOwned; use serde::Serialize; use sha2::Digest; +use crate::args::deno_registry_api_url; use crate::args::Flags; use crate::args::PublishFlags; use crate::factory::CliFactory; @@ -226,7 +227,7 @@ async fn perform_publish( auth_method: AuthMethod, ) -> Result<(), AnyError> { let client = http_client.client()?; - let registry_url = crate::cache::DENO_REGISTRY_URL.to_string(); + let registry_url = deno_registry_api_url().to_string(); let permissions = packages .iter()