summaryrefslogtreecommitdiff
path: root/cli/file_fetcher.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2022-11-28 17:28:54 -0500
committerGitHub <noreply@github.com>2022-11-28 17:28:54 -0500
commit2d4c46c975eb916dc622cc729a1a8d397582a76f (patch)
tree445e819117acd2f94ffc9d7da7ed8e3e604435d0 /cli/file_fetcher.rs
parentf526513d74d34ac254aa40ef9b73238cb21c395b (diff)
refactor: create util folder, move nap_sym to napi/sym, move http_cache to cache folder (#16857)
Diffstat (limited to 'cli/file_fetcher.rs')
-rw-r--r--cli/file_fetcher.rs219
1 files changed, 46 insertions, 173 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs
index b9a91d41a..12f39c7e3 100644
--- a/cli/file_fetcher.rs
+++ b/cli/file_fetcher.rs
@@ -1,18 +1,18 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+use crate::args::CacheSetting;
use crate::auth_tokens::AuthTokens;
+use crate::cache::HttpCache;
use crate::colors;
-use crate::http_cache::HttpCache;
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::util::progress_bar::ProgressBar;
+use crate::util::text_encoding;
use data_url::DataUrl;
use deno_ast::MediaType;
-use deno_core::anyhow::anyhow;
use deno_core::error::custom_error;
use deno_core::error::generic_error;
use deno_core::error::uri_error;
@@ -21,11 +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_tls::rustls;
-use deno_runtime::deno_tls::rustls::RootCertStore;
-use deno_runtime::deno_tls::rustls_native_certs::load_native_certs;
-use deno_runtime::deno_tls::rustls_pemfile;
-use deno_runtime::deno_tls::webpki_roots;
use deno_runtime::deno_web::BlobStore;
use deno_runtime::permissions::Permissions;
use log::debug;
@@ -34,7 +29,6 @@ use std::collections::HashMap;
use std::env;
use std::fs;
use std::future::Future;
-use std::io::BufReader;
use std::io::Read;
use std::path::PathBuf;
use std::pin::Pin;
@@ -82,86 +76,6 @@ impl FileCache {
}
}
-/// Indicates how cached source files should be handled.
-#[derive(Debug, Clone, Eq, PartialEq)]
-pub enum CacheSetting {
- /// Only the cached files should be used. Any files not in the cache will
- /// error. This is the equivalent of `--cached-only` in the CLI.
- Only,
- /// No cached source files should be used, and all files should be reloaded.
- /// This is the equivalent of `--reload` in the CLI.
- ReloadAll,
- /// Only some cached resources should be used. This is the equivalent of
- /// `--reload=https://deno.land/std` or
- /// `--reload=https://deno.land/std,https://deno.land/x/example`.
- ReloadSome(Vec<String>),
- /// The usability of a cached value is determined by analyzing the cached
- /// headers and other metadata associated with a cached response, reloading
- /// any cached "non-fresh" cached responses.
- RespectHeaders,
- /// The cached source files should be used for local modules. This is the
- /// default behavior of the CLI.
- Use,
-}
-
-impl CacheSetting {
- /// Returns if the cache should be used for a given specifier.
- pub fn should_use(
- &self,
- specifier: &ModuleSpecifier,
- http_cache: &HttpCache,
- ) -> bool {
- match self {
- CacheSetting::ReloadAll => false,
- CacheSetting::Use | CacheSetting::Only => true,
- CacheSetting::RespectHeaders => {
- if let Ok((_, headers, cache_time)) = http_cache.get(specifier) {
- let cache_semantics =
- CacheSemantics::new(headers, cache_time, SystemTime::now());
- cache_semantics.should_use()
- } else {
- false
- }
- }
- CacheSetting::ReloadSome(list) => {
- let mut url = specifier.clone();
- url.set_fragment(None);
- if list.contains(&url.as_str().to_string()) {
- return false;
- }
- url.set_query(None);
- let mut path = PathBuf::from(url.as_str());
- loop {
- if list.contains(&path.to_str().unwrap().to_string()) {
- return false;
- }
- if !path.pop() {
- break;
- }
- }
- true
- }
- }
- }
-
- pub fn should_use_for_npm_package(&self, package_name: &str) -> bool {
- match self {
- CacheSetting::ReloadAll => false,
- CacheSetting::ReloadSome(list) => {
- if list.contains(&"npm:".to_string()) {
- return false;
- }
- let specifier = format!("npm:{}", package_name);
- if list.contains(&specifier) {
- return false;
- }
- true
- }
- _ => true,
- }
- }
-}
-
/// Fetch a source file from the local file system.
fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> {
let local = specifier.to_file_path().map_err(|_| {
@@ -182,80 +96,6 @@ fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> {
})
}
-/// Create and populate a root cert store based on the passed options and
-/// environment.
-pub fn get_root_cert_store(
- maybe_root_path: Option<PathBuf>,
- maybe_ca_stores: Option<Vec<String>>,
- maybe_ca_file: Option<String>,
-) -> Result<RootCertStore, AnyError> {
- let mut root_cert_store = RootCertStore::empty();
- let ca_stores: Vec<String> = maybe_ca_stores
- .or_else(|| {
- let env_ca_store = env::var("DENO_TLS_CA_STORE").ok()?;
- Some(
- env_ca_store
- .split(',')
- .map(|s| s.trim().to_string())
- .filter(|s| !s.is_empty())
- .collect(),
- )
- })
- .unwrap_or_else(|| vec!["mozilla".to_string()]);
-
- for store in ca_stores.iter() {
- match store.as_str() {
- "mozilla" => {
- root_cert_store.add_server_trust_anchors(
- webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
- rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
- ta.subject,
- ta.spki,
- ta.name_constraints,
- )
- }),
- );
- }
- "system" => {
- let roots = load_native_certs().expect("could not load platform certs");
- for root in roots {
- root_cert_store
- .add(&rustls::Certificate(root.0))
- .expect("Failed to add platform cert to root cert store");
- }
- }
- _ => {
- return Err(anyhow!("Unknown certificate store \"{}\" specified (allowed: \"system,mozilla\")", store));
- }
- }
- }
-
- let ca_file = maybe_ca_file.or_else(|| env::var("DENO_CERT").ok());
- if let Some(ca_file) = ca_file {
- let ca_file = if let Some(root) = &maybe_root_path {
- root.join(&ca_file)
- } else {
- PathBuf::from(ca_file)
- };
- let certfile = fs::File::open(&ca_file)?;
- let mut reader = BufReader::new(certfile);
-
- match rustls_pemfile::certs(&mut reader) {
- Ok(certs) => {
- root_cert_store.add_parsable_certificates(&certs);
- }
- Err(e) => {
- return Err(anyhow!(
- "Unable to add pem file to certificate store: {}",
- e
- ));
- }
- }
- }
-
- Ok(root_cert_store)
-}
-
/// Returns the decoded body and content-type of a provided
/// data URL.
pub fn get_source_from_data_url(
@@ -571,7 +411,7 @@ impl FileFetcher {
return futures::future::err(err).boxed();
}
- if self.cache_setting.should_use(specifier, &self.http_cache) {
+ if self.should_use_cache(specifier) {
match self.fetch_cached(specifier, redirect_limit) {
Ok(Some(file)) => {
return futures::future::ok(file).boxed();
@@ -654,6 +494,41 @@ impl FileFetcher {
.boxed()
}
+ /// Returns if the cache should be used for a given specifier.
+ fn should_use_cache(&self, specifier: &ModuleSpecifier) -> bool {
+ match &self.cache_setting {
+ CacheSetting::ReloadAll => false,
+ CacheSetting::Use | CacheSetting::Only => true,
+ CacheSetting::RespectHeaders => {
+ if let Ok((_, headers, cache_time)) = self.http_cache.get(specifier) {
+ let cache_semantics =
+ CacheSemantics::new(headers, cache_time, SystemTime::now());
+ cache_semantics.should_use()
+ } else {
+ false
+ }
+ }
+ CacheSetting::ReloadSome(list) => {
+ let mut url = specifier.clone();
+ url.set_fragment(None);
+ if list.contains(&url.as_str().to_string()) {
+ return false;
+ }
+ url.set_query(None);
+ let mut path = PathBuf::from(url.as_str());
+ loop {
+ if list.contains(&path.to_str().unwrap().to_string()) {
+ return false;
+ }
+ if !path.pop() {
+ break;
+ }
+ }
+ true
+ }
+ }
+ }
+
/// Fetch a source file and asynchronously return it.
pub async fn fetch(
&self,
@@ -754,6 +629,7 @@ impl FileFetcher {
#[cfg(test)]
mod tests {
+ use crate::cache::CachedUrlMetadata;
use crate::http_util::HttpClient;
use super::*;
@@ -1175,8 +1051,7 @@ mod tests {
.http_cache
.get_cache_filename(&specifier)
.unwrap();
- let mut metadata =
- crate::http_cache::Metadata::read(&cache_filename).unwrap();
+ let mut metadata = CachedUrlMetadata::read(&cache_filename).unwrap();
metadata.headers = HashMap::new();
metadata
.headers
@@ -1265,8 +1140,7 @@ mod tests {
.await;
assert!(result.is_ok());
- let metadata_filename =
- crate::http_cache::Metadata::filename(&cache_filename);
+ let metadata_filename = CachedUrlMetadata::filename(&cache_filename);
let metadata_file = fs::File::open(metadata_filename).unwrap();
let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap();
@@ -1285,8 +1159,7 @@ mod tests {
.await;
assert!(result.is_ok());
- let metadata_filename =
- crate::http_cache::Metadata::filename(&cache_filename);
+ let metadata_filename = CachedUrlMetadata::filename(&cache_filename);
let metadata_file = fs::File::open(metadata_filename).unwrap();
let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap();
@@ -1438,7 +1311,7 @@ mod tests {
assert!(result.is_ok());
let metadata_filename =
- crate::http_cache::Metadata::filename(&redirected_cache_filename);
+ CachedUrlMetadata::filename(&redirected_cache_filename);
let metadata_file = fs::File::open(metadata_filename).unwrap();
let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap();
@@ -1458,7 +1331,7 @@ mod tests {
assert!(result.is_ok());
let metadata_filename =
- crate::http_cache::Metadata::filename(&redirected_cache_filename);
+ CachedUrlMetadata::filename(&redirected_cache_filename);
let metadata_file = fs::File::open(metadata_filename).unwrap();
let metadata_file_metadata = metadata_file.metadata().unwrap();
let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap();