1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor: better handling for registry urls (#21545)

This commit is contained in:
Bartek Iwańczuk 2023-12-12 15:45:20 +01:00 committed by GitHub
parent 93ea46b31d
commit 06c5f99a01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 21 deletions

View file

@ -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(&registry_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(&registry_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('/'));
}
}

22
cli/cache/mod.rs vendored
View file

@ -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<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(&registry_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<CacheInfo> {

View file

@ -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()