Skip to content

Commit

Permalink
fix(npm): use an http client with connection pool (#16705)
Browse files Browse the repository at this point in the history
Should make downloading npm packages faster and more reliable.
  • Loading branch information
dsherret committed Nov 18, 2022
1 parent 6962808 commit 763d492
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 418 deletions.
64 changes: 23 additions & 41 deletions cli/file_fetcher.rs
Expand Up @@ -3,13 +3,12 @@
use crate::auth_tokens::AuthTokens;
use crate::colors;
use crate::http_cache::HttpCache;
use crate::http_util::fetch_once;
use crate::http_util::CacheSemantics;
use crate::http_util::FetchOnceArgs;
use crate::http_util::FetchOnceResult;
use crate::http_util::HttpClient;
use crate::progress_bar::ProgressBar;
use crate::text_encoding;
use crate::version::get_user_agent;

use data_url::DataUrl;
use deno_ast::MediaType;
Expand All @@ -22,8 +21,6 @@ use deno_core::futures;
use deno_core::futures::future::FutureExt;
use deno_core::parking_lot::Mutex;
use deno_core::ModuleSpecifier;
use deno_runtime::deno_fetch::create_http_client;
use deno_runtime::deno_fetch::reqwest;
use deno_runtime::deno_tls::rustls;
use deno_runtime::deno_tls::rustls::RootCertStore;
use deno_runtime::deno_tls::rustls_native_certs::load_native_certs;
Expand Down Expand Up @@ -333,7 +330,7 @@ pub struct FileFetcher {
cache: FileCache,
cache_setting: CacheSetting,
pub http_cache: HttpCache,
http_client: reqwest::Client,
http_client: HttpClient,
blob_store: BlobStore,
download_log_level: log::Level,
progress_bar: Option<ProgressBar>,
Expand All @@ -344,9 +341,8 @@ impl FileFetcher {
http_cache: HttpCache,
cache_setting: CacheSetting,
allow_remote: bool,
root_cert_store: Option<RootCertStore>,
http_client: HttpClient,
blob_store: BlobStore,
unsafely_ignore_certificate_errors: Option<Vec<String>>,
progress_bar: Option<ProgressBar>,
) -> Result<Self, AnyError> {
Ok(Self {
Expand All @@ -355,14 +351,7 @@ impl FileFetcher {
cache: Default::default(),
cache_setting,
http_cache,
http_client: create_http_client(
get_user_agent(),
root_cert_store,
vec![],
None,
unsafely_ignore_certificate_errors,
None,
)?,
http_client,
blob_store,
download_log_level: log::Level::Info,
progress_bar,
Expand Down Expand Up @@ -628,14 +617,14 @@ impl FileFetcher {
let file_fetcher = self.clone();
// A single pass of fetch either yields code or yields a redirect.
async move {
match fetch_once(FetchOnceArgs {
client,
url: specifier.clone(),
maybe_accept: maybe_accept.clone(),
maybe_etag,
maybe_auth_token,
})
.await?
match client
.fetch_once(FetchOnceArgs {
url: specifier.clone(),
maybe_accept: maybe_accept.clone(),
maybe_etag,
maybe_auth_token,
})
.await?
{
FetchOnceResult::NotModified => {
let file = file_fetcher.fetch_cached(&specifier, 10)?.unwrap();
Expand Down Expand Up @@ -765,6 +754,8 @@ impl FileFetcher {

#[cfg(test)]
mod tests {
use crate::http_util::HttpClient;

use super::*;
use deno_core::error::get_custom_error_class;
use deno_core::resolve_url;
Expand Down Expand Up @@ -793,10 +784,9 @@ mod tests {
HttpCache::new(&location),
cache_setting,
true,
None,
HttpClient::new(None, None).unwrap(),
blob_store.clone(),
None,
None,
)
.unwrap();
(file_fetcher, temp_dir, blob_store)
Expand Down Expand Up @@ -1232,10 +1222,9 @@ mod tests {
HttpCache::new(&location),
CacheSetting::ReloadAll,
true,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let result = file_fetcher
Expand All @@ -1259,10 +1248,9 @@ mod tests {
HttpCache::new(&location),
CacheSetting::Use,
true,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let specifier =
Expand All @@ -1287,10 +1275,9 @@ mod tests {
HttpCache::new(&location),
CacheSetting::Use,
true,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let result = file_fetcher_02
Expand Down Expand Up @@ -1431,10 +1418,9 @@ mod tests {
HttpCache::new(&location),
CacheSetting::Use,
true,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let specifier =
Expand All @@ -1461,10 +1447,9 @@ mod tests {
HttpCache::new(&location),
CacheSetting::Use,
true,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let result = file_fetcher_02
Expand Down Expand Up @@ -1562,10 +1547,9 @@ mod tests {
HttpCache::new(&location),
CacheSetting::Use,
false,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let specifier =
Expand All @@ -1589,20 +1573,18 @@ mod tests {
HttpCache::new(&location),
CacheSetting::Only,
true,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let file_fetcher_02 = FileFetcher::new(
HttpCache::new(&location),
CacheSetting::Use,
true,
None,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
None,
)
.unwrap();
let specifier =
Expand Down
10 changes: 5 additions & 5 deletions cli/http_cache.rs
Expand Up @@ -73,11 +73,6 @@ pub fn url_to_filename(url: &Url) -> Option<PathBuf> {
Some(cache_filename)
}

#[derive(Debug, Clone, Default)]
pub struct HttpCache {
pub location: PathBuf,
}

#[derive(Serialize, Deserialize)]
pub struct Metadata {
pub headers: HeadersMap,
Expand Down Expand Up @@ -107,6 +102,11 @@ impl Metadata {
}
}

#[derive(Debug, Clone, Default)]
pub struct HttpCache {
pub location: PathBuf,
}

impl HttpCache {
/// Returns a new instance.
///
Expand Down

0 comments on commit 763d492

Please sign in to comment.