diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-12-19 14:31:19 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-19 14:31:19 -0500 |
commit | 2ff27a1f9327bf913d7b9d6aadc703a30a297e11 (patch) | |
tree | 2643c8b11279542c462c6c3d50da5f89d22e0d8f /cli/http_util.rs | |
parent | 5a8adc342bcf01f535079d06778a9f244ba7e96f (diff) |
fix: hide progress bars when showing permission prompt (#17130)
Also adds download bytes progress when downloading remote specifiers.
Closes #16860
Diffstat (limited to 'cli/http_util.rs')
-rw-r--r-- | cli/http_util.rs | 60 |
1 files changed, 26 insertions, 34 deletions
diff --git a/cli/http_util.rs b/cli/http_util.rs index 966ba693e..e7fbcd1f3 100644 --- a/cli/http_util.rs +++ b/cli/http_util.rs @@ -1,5 +1,4 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use crate::auth_tokens::AuthToken; use crate::util::progress_bar::UpdateGuard; use crate::version::get_user_agent; @@ -218,21 +217,6 @@ impl CacheSemantics { } } -#[derive(Debug, Eq, PartialEq)] -pub enum FetchOnceResult { - Code(Vec<u8>, HeadersMap), - NotModified, - Redirect(Url, HeadersMap), -} - -#[derive(Debug)] -pub struct FetchOnceArgs { - pub url: Url, - pub maybe_accept: Option<String>, - pub maybe_etag: Option<String>, - pub maybe_auth_token: Option<AuthToken>, -} - #[derive(Debug, Clone)] pub struct HttpClient(reqwest::Client); @@ -312,24 +296,9 @@ impl HttpClient { ); } - if let Some(progress_guard) = progress_guard { - if let Some(total_size) = response.content_length() { - progress_guard.set_total_size(total_size); - let mut current_size = 0; - let mut data = Vec::with_capacity(total_size as usize); - let mut stream = response.bytes_stream(); - while let Some(item) = stream.next().await { - let bytes = item?; - current_size += bytes.len() as u64; - progress_guard.set_position(current_size); - data.extend(bytes.into_iter()); - } - return Ok(Some(data)); - } - } - - let bytes = response.bytes().await?; - Ok(Some(bytes.into())) + get_response_body_with_progress(response, progress_guard) + .await + .map(Some) } async fn get_redirected_response<U: reqwest::IntoUrl>( @@ -358,6 +327,29 @@ impl HttpClient { } } +pub async fn get_response_body_with_progress( + response: reqwest::Response, + progress_guard: Option<&UpdateGuard>, +) -> Result<Vec<u8>, AnyError> { + if let Some(progress_guard) = progress_guard { + if let Some(total_size) = response.content_length() { + progress_guard.set_total_size(total_size); + let mut current_size = 0; + let mut data = Vec::with_capacity(total_size as usize); + let mut stream = response.bytes_stream(); + while let Some(item) = stream.next().await { + let bytes = item?; + current_size += bytes.len() as u64; + progress_guard.set_position(current_size); + data.extend(bytes.into_iter()); + } + return Ok(data); + } + } + let bytes = response.bytes().await?; + Ok(bytes.into()) +} + #[cfg(test)] mod test { use super::*; |