Skip to content

Commit 06c5f99

Browse files
authored
refactor: better handling for registry urls (denoland#21545)
1 parent 93ea46b commit 06c5f99

File tree

3 files changed

+70
-21
lines changed

3 files changed

+70
-21
lines changed

cli/args/mod.rs

+66
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,64 @@ pub fn npm_registry_default_url() -> &'static Url {
104104
&NPM_REGISTRY_DEFAULT_URL
105105
}
106106

107+
pub fn deno_registry_url() -> &'static Url {
108+
static DENO_REGISTRY_URL: Lazy<Url> = Lazy::new(|| {
109+
let env_var_name = "DENO_REGISTRY_URL";
110+
if let Ok(registry_url) = std::env::var(env_var_name) {
111+
// ensure there is a trailing slash for the directory
112+
let registry_url = format!("{}/", registry_url.trim_end_matches('/'));
113+
match Url::parse(&registry_url) {
114+
Ok(url) => {
115+
return url;
116+
}
117+
Err(err) => {
118+
log::debug!(
119+
"Invalid {} environment variable: {:#}",
120+
env_var_name,
121+
err,
122+
);
123+
}
124+
}
125+
}
126+
127+
deno_graph::source::DEFAULT_DENO_REGISTRY_URL.clone()
128+
});
129+
130+
&DENO_REGISTRY_URL
131+
}
132+
133+
pub fn deno_registry_api_url() -> &'static Url {
134+
static DENO_REGISTRY_API_URL: Lazy<Url> = Lazy::new(|| {
135+
let env_var_name = "DENO_REGISTRY_API_URL";
136+
if let Ok(registry_url) = std::env::var(env_var_name) {
137+
// ensure there is a trailing slash for the directory
138+
let registry_url = format!("{}/", registry_url.trim_end_matches('/'));
139+
match Url::parse(&registry_url) {
140+
Ok(url) => {
141+
return url;
142+
}
143+
Err(err) => {
144+
log::debug!(
145+
"Invalid {} environment variable: {:#}",
146+
env_var_name,
147+
err,
148+
);
149+
}
150+
}
151+
}
152+
153+
let host = deno_graph::source::DEFAULT_DENO_REGISTRY_URL
154+
.host_str()
155+
.unwrap();
156+
157+
let mut url = deno_graph::source::DEFAULT_DENO_REGISTRY_URL.clone();
158+
url.set_host(Some(&format!("api.{}", host))).unwrap();
159+
url
160+
});
161+
162+
&DENO_REGISTRY_API_URL
163+
}
164+
107165
pub fn ts_config_to_emit_options(
108166
config: deno_config::TsConfig,
109167
) -> deno_ast::EmitOptions {
@@ -1855,4 +1913,12 @@ mod test {
18551913
]
18561914
)
18571915
}
1916+
1917+
#[test]
1918+
fn deno_registry_urls() {
1919+
let reg_url = deno_registry_url();
1920+
assert!(reg_url.as_str().ends_with('/'));
1921+
let reg_api_url = deno_registry_api_url();
1922+
assert!(reg_api_url.as_str().ends_with('/'));
1923+
}
18581924
}

cli/cache/mod.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
22

3+
use crate::args::deno_registry_url;
34
use crate::args::CacheSetting;
45
use crate::errors::get_error_class_name;
56
use crate::file_fetcher::FetchOptions;
@@ -17,7 +18,6 @@ use deno_graph::source::LoadFuture;
1718
use deno_graph::source::LoadResponse;
1819
use deno_graph::source::Loader;
1920
use deno_runtime::permissions::PermissionsContainer;
20-
use once_cell::sync::Lazy;
2121
use std::collections::HashMap;
2222
use std::path::Path;
2323
use std::path::PathBuf;
@@ -165,27 +165,9 @@ impl FetchCacher {
165165
}
166166
}
167167

168-
pub(crate) static DENO_REGISTRY_URL: Lazy<Url> = Lazy::new(|| {
169-
let env_var_name = "DENO_REGISTRY_URL";
170-
if let Ok(registry_url) = std::env::var(env_var_name) {
171-
// ensure there is a trailing slash for the directory
172-
let registry_url = format!("{}/", registry_url.trim_end_matches('/'));
173-
match Url::parse(&registry_url) {
174-
Ok(url) => {
175-
return url;
176-
}
177-
Err(err) => {
178-
log::debug!("Invalid {} environment variable: {:#}", env_var_name, err,);
179-
}
180-
}
181-
}
182-
183-
deno_graph::source::DEFAULT_DENO_REGISTRY_URL.clone()
184-
});
185-
186168
impl Loader for FetchCacher {
187169
fn registry_url(&self) -> &Url {
188-
&DENO_REGISTRY_URL
170+
deno_registry_url()
189171
}
190172

191173
fn get_cache_info(&self, specifier: &ModuleSpecifier) -> Option<CacheInfo> {

cli/tools/registry/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use serde::de::DeserializeOwned;
2727
use serde::Serialize;
2828
use sha2::Digest;
2929

30+
use crate::args::deno_registry_api_url;
3031
use crate::args::Flags;
3132
use crate::args::PublishFlags;
3233
use crate::factory::CliFactory;
@@ -226,7 +227,7 @@ async fn perform_publish(
226227
auth_method: AuthMethod,
227228
) -> Result<(), AnyError> {
228229
let client = http_client.client()?;
229-
let registry_url = crate::cache::DENO_REGISTRY_URL.to_string();
230+
let registry_url = deno_registry_api_url().to_string();
230231

231232
let permissions = packages
232233
.iter()

0 commit comments

Comments
 (0)