diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-12-12 15:45:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-12 23:45:20 +0900 |
commit | 06c5f99a01698feba4ce67fa911428cf2d8c95d4 (patch) | |
tree | ee9b098bc7eb6ae95687916c8b2d9483732700e5 /cli/args/mod.rs | |
parent | 93ea46b31d7dc13354fc16308f73163375de6af7 (diff) |
refactor: better handling for registry urls (#21545)
Diffstat (limited to 'cli/args/mod.rs')
-rw-r--r-- | cli/args/mod.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 033b30d97..c65745e29 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<Url> = 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<Url> = 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('/')); + } } |