diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-12-12 21:30:44 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-12 21:30:44 -0500 |
commit | 8972ebc9cc33ef1df21c899c35006ea712f7f91f (patch) | |
tree | 396e0aaf422b61e1a0c4c7a09a7bf84a84e7f459 /cli/tools | |
parent | 8c026dab92b20fea44bc66f84db48b885c7264d1 (diff) |
fix: always derive http client from cli flags (#17029)
I'm not sure how to test this. It doesn't seem to have an existing test.
Closes #15921
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/run.rs | 5 | ||||
-rw-r--r-- | cli/tools/standalone.rs | 13 | ||||
-rw-r--r-- | cli/tools/upgrade.rs | 62 |
3 files changed, 38 insertions, 42 deletions
diff --git a/cli/tools/run.rs b/cli/tools/run.rs index d714c55d3..617ec28f6 100644 --- a/cli/tools/run.rs +++ b/cli/tools/run.rs @@ -45,7 +45,10 @@ To grant permissions, set them before the script argument. For example: // Run a background task that checks for available upgrades. If an earlier // run of this background task found a new version of Deno. - super::upgrade::check_for_upgrades(ps.dir.upgrade_check_file_path()); + super::upgrade::check_for_upgrades( + ps.http_client.clone(), + ps.dir.upgrade_check_file_path(), + ); let main_module = if NpmPackageReference::from_str(&run_flags.script).is_ok() { diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index c276119ea..558adc460 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -5,6 +5,7 @@ use crate::args::Flags; use crate::cache::DenoDir; use crate::graph_util::create_graph_and_maybe_check; use crate::graph_util::error_for_any_npm_specifier; +use crate::http_util::HttpClient; use crate::standalone::Metadata; use crate::standalone::MAGIC_TRAILER; use crate::util::path::path_has_trailing_slash; @@ -18,7 +19,6 @@ use deno_core::serde_json; use deno_core::url::Url; use deno_graph::ModuleSpecifier; use deno_runtime::colors; -use deno_runtime::deno_fetch::reqwest::Client; use deno_runtime::permissions::Permissions; use std::env; use std::fs; @@ -64,7 +64,8 @@ pub async fn compile( // Select base binary based on target let original_binary = - get_base_binary(deno_dir, compile_flags.target.clone()).await?; + get_base_binary(&ps.http_client, deno_dir, compile_flags.target.clone()) + .await?; let final_bin = create_standalone_binary( original_binary, @@ -82,6 +83,7 @@ pub async fn compile( } async fn get_base_binary( + client: &HttpClient, deno_dir: &DenoDir, target: Option<String>, ) -> Result<Vec<u8>, AnyError> { @@ -103,7 +105,8 @@ async fn get_base_binary( let binary_path = download_directory.join(&binary_path_suffix); if !binary_path.exists() { - download_base_binary(&download_directory, &binary_path_suffix).await?; + download_base_binary(client, &download_directory, &binary_path_suffix) + .await?; } let archive_data = tokio::fs::read(binary_path).await?; @@ -114,14 +117,12 @@ async fn get_base_binary( } async fn download_base_binary( + client: &HttpClient, output_directory: &Path, binary_path_suffix: &str, ) -> Result<(), AnyError> { let download_url = format!("https://dl.deno.land/{}", binary_path_suffix); - let client_builder = Client::builder(); - let client = client_builder.build()?; - log::info!("Checking {}", &download_url); let res = client.get(&download_url).send().await?; diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs index 82949410b..1e0be728e 100644 --- a/cli/tools/upgrade.rs +++ b/cli/tools/upgrade.rs @@ -2,8 +2,11 @@ //! This module provides feature to upgrade deno executable +use crate::args::Flags; use crate::args::UpgradeFlags; use crate::colors; +use crate::http_util::HttpClient; +use crate::proc_state::ProcState; use crate::util::display::human_download_size; use crate::util::progress_bar::ProgressBar; use crate::util::progress_bar::ProgressBarStyle; @@ -15,8 +18,6 @@ use deno_core::error::AnyError; use deno_core::futures::future::BoxFuture; use deno_core::futures::FutureExt; use deno_core::futures::StreamExt; -use deno_runtime::deno_fetch::reqwest; -use deno_runtime::deno_fetch::reqwest::Client; use once_cell::sync::Lazy; use std::borrow::Cow; use std::env; @@ -49,13 +50,15 @@ trait UpdateCheckerEnvironment: Clone + Send + Sync { #[derive(Clone)] struct RealUpdateCheckerEnvironment { + http_client: HttpClient, cache_file_path: PathBuf, current_time: chrono::DateTime<chrono::Utc>, } impl RealUpdateCheckerEnvironment { - pub fn new(cache_file_path: PathBuf) -> Self { + pub fn new(http_client: HttpClient, cache_file_path: PathBuf) -> Self { Self { + http_client, cache_file_path, // cache the current time current_time: chrono::Utc::now(), @@ -65,12 +68,12 @@ impl RealUpdateCheckerEnvironment { impl UpdateCheckerEnvironment for RealUpdateCheckerEnvironment { fn latest_version(&self) -> BoxFuture<'static, Result<String, AnyError>> { - async { - let client = build_http_client(None)?; + let http_client = self.http_client.clone(); + async move { if version::is_canary() { - get_latest_canary_version(&client).await + get_latest_canary_version(&http_client).await } else { - get_latest_release_version(&client).await + get_latest_release_version(&http_client).await } } .boxed() @@ -161,12 +164,12 @@ impl<TEnvironment: UpdateCheckerEnvironment> UpdateChecker<TEnvironment> { } } -pub fn check_for_upgrades(cache_file_path: PathBuf) { +pub fn check_for_upgrades(http_client: HttpClient, cache_file_path: PathBuf) { if env::var("DENO_NO_UPDATE_CHECK").is_ok() { return; } - let env = RealUpdateCheckerEnvironment::new(cache_file_path); + let env = RealUpdateCheckerEnvironment::new(http_client, cache_file_path); let update_checker = UpdateChecker::new(env); if update_checker.should_check_for_new_version() { @@ -236,7 +239,11 @@ async fn fetch_and_store_latest_version< ); } -pub async fn upgrade(upgrade_flags: UpgradeFlags) -> Result<(), AnyError> { +pub async fn upgrade( + flags: Flags, + upgrade_flags: UpgradeFlags, +) -> Result<(), AnyError> { + let ps = ProcState::build(flags).await?; let current_exe_path = std::env::current_exe()?; let metadata = fs::metadata(¤t_exe_path)?; let permissions = metadata.permissions(); @@ -258,7 +265,7 @@ pub async fn upgrade(upgrade_flags: UpgradeFlags) -> Result<(), AnyError> { ), current_exe_path.display()); } - let client = build_http_client(upgrade_flags.ca_file)?; + let client = ps.http_client.clone(); let install_version = match upgrade_flags.version { Some(passed_version) => { @@ -316,7 +323,11 @@ pub async fn upgrade(upgrade_flags: UpgradeFlags) -> Result<(), AnyError> { { log::info!( "Local deno version {} is the most recent release", - crate::version::deno() + if upgrade_flags.canary { + crate::version::GIT_COMMIT_HASH.to_string() + } else { + crate::version::deno() + } ); return Ok(()); } else { @@ -342,7 +353,7 @@ pub async fn upgrade(upgrade_flags: UpgradeFlags) -> Result<(), AnyError> { ) }; - let archive_data = download_package(client, &download_url).await?; + let archive_data = download_package(&client, &download_url).await?; log::info!("Deno is upgrading to version {}", &install_version); @@ -388,27 +399,8 @@ pub async fn upgrade(upgrade_flags: UpgradeFlags) -> Result<(), AnyError> { Ok(()) } -fn build_http_client( - ca_file: Option<String>, -) -> Result<reqwest::Client, AnyError> { - let mut client_builder = - Client::builder().user_agent(version::get_user_agent()); - - // If we have been provided a CA Certificate, add it into the HTTP client - let ca_file = ca_file.or_else(|| env::var("DENO_CERT").ok()); - if let Some(ca_file) = ca_file { - let buf = std::fs::read(ca_file)?; - let cert = reqwest::Certificate::from_pem(&buf)?; - client_builder = client_builder.add_root_certificate(cert); - } - - let client = client_builder.build()?; - - Ok(client) -} - async fn get_latest_release_version( - client: &Client, + client: &HttpClient, ) -> Result<String, AnyError> { let res = client .get("https://dl.deno.land/release-latest.txt") @@ -419,7 +411,7 @@ async fn get_latest_release_version( } async fn get_latest_canary_version( - client: &Client, + client: &HttpClient, ) -> Result<String, AnyError> { let res = client .get("https://dl.deno.land/canary-latest.txt") @@ -430,7 +422,7 @@ async fn get_latest_canary_version( } async fn download_package( - client: Client, + client: &HttpClient, download_url: &str, ) -> Result<Vec<u8>, AnyError> { log::info!("Downloading {}", &download_url); |