2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::args::CacheSetting;
|
2022-12-19 14:31:19 -05:00
|
|
|
use crate::auth_tokens::AuthToken;
|
2021-02-15 21:50:27 -05:00
|
|
|
use crate::auth_tokens::AuthTokens;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::cache::HttpCache;
|
2020-02-18 10:08:18 -05:00
|
|
|
use crate::colors;
|
2022-12-19 14:31:19 -05:00
|
|
|
use crate::http_util;
|
2022-12-14 08:47:18 -05:00
|
|
|
use crate::http_util::resolve_redirect_from_response;
|
2021-12-09 06:16:17 -05:00
|
|
|
use crate::http_util::CacheSemantics;
|
2022-12-19 14:31:19 -05:00
|
|
|
use crate::http_util::HeadersMap;
|
2022-11-18 17:28:14 -05:00
|
|
|
use crate::http_util::HttpClient;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
2022-12-19 14:31:19 -05:00
|
|
|
use crate::util::progress_bar::UpdateGuard;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::text_encoding;
|
2021-12-09 06:16:17 -05:00
|
|
|
|
2021-04-23 14:43:13 -04:00
|
|
|
use data_url::DataUrl;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::uri_error;
|
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures;
|
|
|
|
use deno_core::futures::future::FutureExt;
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-12-19 14:31:19 -05:00
|
|
|
use deno_core::url::Url;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-12-14 08:47:18 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest::header::HeaderValue;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::ACCEPT;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::AUTHORIZATION;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::header::IF_NONE_MATCH;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::StatusCode;
|
2021-07-05 09:34:37 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2019-07-31 07:58:41 -04:00
|
|
|
use std::collections::HashMap;
|
2021-02-15 21:50:27 -05:00
|
|
|
use std::env;
|
2019-07-31 07:58:41 -04:00
|
|
|
use std::fs;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
2019-07-31 07:58:41 -04:00
|
|
|
use std::path::PathBuf;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2019-07-31 07:58:41 -04:00
|
|
|
use std::sync::Arc;
|
2021-12-09 06:16:17 -05:00
|
|
|
use std::time::SystemTime;
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2021-04-07 09:22:14 -04:00
|
|
|
pub const SUPPORTED_SCHEMES: [&str; 5] =
|
|
|
|
["data", "blob", "file", "http", "https"];
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
/// A structure representing a source file.
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct File {
|
|
|
|
/// For remote files, if there was an `X-TypeScript-Type` header, the parsed
|
|
|
|
/// out value of that header.
|
|
|
|
pub maybe_types: Option<String>,
|
|
|
|
/// The resolved media type for the file.
|
2020-09-15 10:33:14 -04:00
|
|
|
pub media_type: MediaType,
|
2020-11-05 19:38:21 -05:00
|
|
|
/// The source of the file as a string.
|
2022-05-20 16:40:55 -04:00
|
|
|
pub source: Arc<str>,
|
2020-11-05 19:38:21 -05:00
|
|
|
/// The _final_ specifier for the file. The requested specifier and the final
|
|
|
|
/// specifier maybe different for remote files that have been redirected.
|
|
|
|
pub specifier: ModuleSpecifier,
|
2021-09-02 11:38:19 -04:00
|
|
|
|
|
|
|
pub maybe_headers: Option<HashMap<String, String>>,
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Simple struct implementing in-process caching to prevent multiple
|
|
|
|
/// fs reads/net fetches for same file.
|
2021-04-08 21:27:27 -04:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-11-05 19:38:21 -05:00
|
|
|
struct FileCache(Arc<Mutex<HashMap<ModuleSpecifier, File>>>);
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
impl FileCache {
|
|
|
|
pub fn get(&self, specifier: &ModuleSpecifier) -> Option<File> {
|
2021-07-06 23:48:01 -04:00
|
|
|
let cache = self.0.lock();
|
2020-11-12 17:17:31 -05:00
|
|
|
cache.get(specifier).cloned()
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
pub fn insert(&self, specifier: ModuleSpecifier, file: File) -> Option<File> {
|
2021-07-06 23:48:01 -04:00
|
|
|
let mut cache = self.0.lock();
|
2020-11-05 19:38:21 -05:00
|
|
|
cache.insert(specifier, file)
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Fetch a source file from the local file system.
|
|
|
|
fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> {
|
2021-02-17 13:47:18 -05:00
|
|
|
let local = specifier.to_file_path().map_err(|_| {
|
2023-01-27 10:43:16 -05:00
|
|
|
uri_error(format!("Invalid file path.\n Specifier: {specifier}"))
|
2020-11-05 19:38:21 -05:00
|
|
|
})?;
|
2023-07-01 18:52:30 -04:00
|
|
|
let bytes = fs::read(local)?;
|
2020-11-05 19:38:21 -05:00
|
|
|
let charset = text_encoding::detect_charset(&bytes).to_string();
|
2022-01-16 10:48:32 -05:00
|
|
|
let source = get_source_from_bytes(bytes, Some(charset))?;
|
2023-03-21 11:46:40 -04:00
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
Ok(File {
|
|
|
|
maybe_types: None,
|
|
|
|
media_type,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: source.into(),
|
2020-11-05 19:38:21 -05:00
|
|
|
specifier: specifier.clone(),
|
2021-09-02 11:38:19 -04:00
|
|
|
maybe_headers: None,
|
2020-11-05 19:38:21 -05:00
|
|
|
})
|
|
|
|
}
|
2019-11-22 12:46:57 -05:00
|
|
|
|
2021-09-13 00:19:23 -04:00
|
|
|
/// Returns the decoded body and content-type of a provided
|
|
|
|
/// data URL.
|
|
|
|
pub fn get_source_from_data_url(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(String, String), AnyError> {
|
|
|
|
let data_url = DataUrl::process(specifier.as_str())
|
2023-01-27 10:43:16 -05:00
|
|
|
.map_err(|e| uri_error(format!("{e:?}")))?;
|
2021-09-13 00:19:23 -04:00
|
|
|
let mime = data_url.mime_type();
|
|
|
|
let charset = mime.get_parameter("charset").map(|v| v.to_string());
|
|
|
|
let (bytes, _) = data_url
|
|
|
|
.decode_to_vec()
|
2023-01-27 10:43:16 -05:00
|
|
|
.map_err(|e| uri_error(format!("{e:?}")))?;
|
|
|
|
Ok((get_source_from_bytes(bytes, charset)?, format!("{mime}")))
|
2021-09-13 00:19:23 -04:00
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Given a vector of bytes and optionally a charset, decode the bytes to a
|
|
|
|
/// string.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub fn get_source_from_bytes(
|
2020-11-05 19:38:21 -05:00
|
|
|
bytes: Vec<u8>,
|
|
|
|
maybe_charset: Option<String>,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let source = if let Some(charset) = maybe_charset {
|
|
|
|
text_encoding::convert_to_utf8(&bytes, &charset)?.to_string()
|
|
|
|
} else {
|
|
|
|
String::from_utf8(bytes)?
|
|
|
|
};
|
2019-11-22 12:46:57 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
Ok(source)
|
|
|
|
}
|
2020-04-15 23:14:28 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Return a validated scheme for a given module specifier.
|
|
|
|
fn get_validated_scheme(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<String, AnyError> {
|
2021-02-17 13:47:18 -05:00
|
|
|
let scheme = specifier.scheme();
|
2020-11-05 19:38:21 -05:00
|
|
|
if !SUPPORTED_SCHEMES.contains(&scheme) {
|
|
|
|
Err(generic_error(format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Unsupported scheme \"{scheme}\" for module \"{specifier}\". Supported schemes: {SUPPORTED_SCHEMES:#?}"
|
2020-11-05 19:38:21 -05:00
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(scheme.to_string())
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
2020-11-05 19:38:21 -05:00
|
|
|
}
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Resolve a media type and optionally the charset from a module specifier and
|
|
|
|
/// the value of a content type header.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub fn map_content_type(
|
2020-11-05 19:38:21 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-01-26 17:24:03 -05:00
|
|
|
maybe_content_type: Option<&String>,
|
2020-11-05 19:38:21 -05:00
|
|
|
) -> (MediaType, Option<String>) {
|
|
|
|
if let Some(content_type) = maybe_content_type {
|
|
|
|
let mut content_types = content_type.split(';');
|
|
|
|
let content_type = content_types.next().unwrap();
|
2021-06-02 06:29:58 -04:00
|
|
|
let media_type = MediaType::from_content_type(specifier, content_type);
|
2020-11-05 19:38:21 -05:00
|
|
|
let charset = content_types
|
|
|
|
.map(str::trim)
|
|
|
|
.find_map(|s| s.strip_prefix("charset="))
|
|
|
|
.map(String::from);
|
2020-02-28 09:17:56 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
(media_type, charset)
|
|
|
|
} else {
|
2023-03-21 11:46:40 -04:00
|
|
|
(MediaType::from_specifier(specifier), None)
|
2020-11-05 19:38:21 -05:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// A structure for resolving, fetching and caching source files.
|
2021-04-08 21:27:27 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2020-11-05 19:38:21 -05:00
|
|
|
pub struct FileFetcher {
|
2021-02-15 21:50:27 -05:00
|
|
|
auth_tokens: AuthTokens,
|
2020-11-05 19:38:21 -05:00
|
|
|
allow_remote: bool,
|
|
|
|
cache: FileCache,
|
|
|
|
cache_setting: CacheSetting,
|
2023-08-01 20:49:09 -04:00
|
|
|
http_cache: Arc<dyn HttpCache>,
|
2023-05-01 16:42:05 -04:00
|
|
|
http_client: Arc<HttpClient>,
|
2023-07-01 18:52:30 -04:00
|
|
|
blob_store: Arc<BlobStore>,
|
2021-12-15 13:23:43 -05:00
|
|
|
download_log_level: log::Level,
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar: Option<ProgressBar>,
|
2020-11-05 19:38:21 -05:00
|
|
|
}
|
2019-12-03 17:48:53 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
impl FileFetcher {
|
|
|
|
pub fn new(
|
2023-08-01 20:49:09 -04:00
|
|
|
http_cache: Arc<dyn HttpCache>,
|
2020-11-05 19:38:21 -05:00
|
|
|
cache_setting: CacheSetting,
|
|
|
|
allow_remote: bool,
|
2023-05-01 16:42:05 -04:00
|
|
|
http_client: Arc<HttpClient>,
|
2023-07-01 18:52:30 -04:00
|
|
|
blob_store: Arc<BlobStore>,
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar: Option<ProgressBar>,
|
2023-01-25 16:51:04 -05:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2021-11-15 19:59:19 -05:00
|
|
|
auth_tokens: AuthTokens::new(env::var("DENO_AUTH_TOKENS").ok()),
|
2020-11-05 19:38:21 -05:00
|
|
|
allow_remote,
|
2021-02-15 21:50:27 -05:00
|
|
|
cache: Default::default(),
|
2020-11-05 19:38:21 -05:00
|
|
|
cache_setting,
|
|
|
|
http_cache,
|
2022-11-18 17:28:14 -05:00
|
|
|
http_client,
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store,
|
2021-12-15 13:23:43 -05:00
|
|
|
download_log_level: log::Level::Info,
|
2022-09-09 15:57:39 -04:00
|
|
|
progress_bar,
|
2023-01-25 16:51:04 -05:00
|
|
|
}
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2021-12-15 13:23:43 -05:00
|
|
|
/// Sets the log level to use when outputting the download message.
|
|
|
|
pub fn set_download_log_level(&mut self, level: log::Level) {
|
|
|
|
self.download_log_level = level;
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Creates a `File` structure for a remote file.
|
|
|
|
fn build_remote_file(
|
2020-05-11 07:13:27 -04:00
|
|
|
&self,
|
2020-11-05 19:38:21 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
headers: &HashMap<String, String>,
|
|
|
|
) -> Result<File, AnyError> {
|
2023-01-26 17:24:03 -05:00
|
|
|
let maybe_content_type = headers.get("content-type");
|
2020-11-05 19:38:21 -05:00
|
|
|
let (media_type, maybe_charset) =
|
|
|
|
map_content_type(specifier, maybe_content_type);
|
2022-01-16 10:48:32 -05:00
|
|
|
let source = get_source_from_bytes(bytes, maybe_charset)?;
|
2021-05-12 19:07:22 -04:00
|
|
|
let maybe_types = match media_type {
|
2021-11-01 16:22:27 -04:00
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Jsx => headers.get("x-typescript-types").cloned(),
|
2021-05-12 19:07:22 -04:00
|
|
|
_ => None,
|
|
|
|
};
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
Ok(File {
|
|
|
|
maybe_types,
|
2019-07-31 07:58:41 -04:00
|
|
|
media_type,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: source.into(),
|
2020-11-05 19:38:21 -05:00
|
|
|
specifier: specifier.clone(),
|
2021-09-02 11:38:19 -04:00
|
|
|
maybe_headers: Some(headers.clone()),
|
2019-07-31 07:58:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch cached remote file.
|
|
|
|
///
|
|
|
|
/// This is a recursive operation if source file has redirections.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn fetch_cached(
|
2020-01-04 05:20:52 -05:00
|
|
|
&self,
|
2020-11-05 19:38:21 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2020-06-15 17:46:48 -04:00
|
|
|
redirect_limit: i64,
|
2020-11-05 19:38:21 -05:00
|
|
|
) -> Result<Option<File>, AnyError> {
|
|
|
|
debug!("FileFetcher::fetch_cached - specifier: {}", specifier);
|
2020-06-15 17:46:48 -04:00
|
|
|
if redirect_limit < 0 {
|
2020-11-05 19:38:21 -05:00
|
|
|
return Err(custom_error("Http", "Too many redirects."));
|
2020-06-15 17:46:48 -04:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key = self.http_cache.cache_item_key(specifier)?; // compute this once
|
|
|
|
let Some(metadata) = self.http_cache.read_metadata(&cache_key)? else {
|
2023-07-08 16:06:45 -04:00
|
|
|
return Ok(None);
|
2021-02-17 13:47:18 -05:00
|
|
|
};
|
2023-07-08 16:06:45 -04:00
|
|
|
let headers = metadata.headers;
|
2020-11-05 19:38:21 -05:00
|
|
|
if let Some(redirect_to) = headers.get("location") {
|
|
|
|
let redirect =
|
2021-02-17 13:47:18 -05:00
|
|
|
deno_core::resolve_import(redirect_to, specifier.as_str())?;
|
2020-11-05 19:38:21 -05:00
|
|
|
return self.fetch_cached(&redirect, redirect_limit - 1);
|
2020-02-19 08:17:13 -05:00
|
|
|
}
|
2023-08-01 20:49:09 -04:00
|
|
|
let Some(bytes) = self.http_cache.read_file_bytes(&cache_key)? else {
|
2023-07-08 16:06:45 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2020-11-05 19:38:21 -05:00
|
|
|
let file = self.build_remote_file(specifier, bytes, &headers)?;
|
2020-02-19 08:17:13 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
Ok(Some(file))
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2021-01-05 21:22:38 -05:00
|
|
|
/// Convert a data URL into a file, resulting in an error if the URL is
|
|
|
|
/// invalid.
|
|
|
|
fn fetch_data_url(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<File, AnyError> {
|
|
|
|
debug!("FileFetcher::fetch_data_url() - specifier: {}", specifier);
|
2021-09-13 00:19:23 -04:00
|
|
|
let (source, content_type) = get_source_from_data_url(specifier)?;
|
2023-01-26 17:24:03 -05:00
|
|
|
let (media_type, _) = map_content_type(specifier, Some(&content_type));
|
2021-01-05 21:22:38 -05:00
|
|
|
let mut headers = HashMap::new();
|
|
|
|
headers.insert("content-type".to_string(), content_type);
|
|
|
|
Ok(File {
|
|
|
|
maybe_types: None,
|
|
|
|
media_type,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: source.into(),
|
2021-01-05 21:22:38 -05:00
|
|
|
specifier: specifier.clone(),
|
2021-10-10 17:26:22 -04:00
|
|
|
maybe_headers: Some(headers),
|
2021-01-05 21:22:38 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-07 09:22:14 -04:00
|
|
|
/// Get a blob URL.
|
2021-07-05 09:34:37 -04:00
|
|
|
async fn fetch_blob_url(
|
2021-04-07 09:22:14 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<File, AnyError> {
|
|
|
|
debug!("FileFetcher::fetch_blob_url() - specifier: {}", specifier);
|
2023-07-01 18:52:30 -04:00
|
|
|
let blob = self
|
|
|
|
.blob_store
|
|
|
|
.get_object_url(specifier.clone())
|
|
|
|
.ok_or_else(|| {
|
|
|
|
custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!("Blob URL not found: \"{specifier}\"."),
|
|
|
|
)
|
|
|
|
})?;
|
2021-04-07 09:22:14 -04:00
|
|
|
|
2021-07-05 09:34:37 -04:00
|
|
|
let content_type = blob.media_type.clone();
|
|
|
|
let bytes = blob.read_all().await?;
|
2021-04-07 09:22:14 -04:00
|
|
|
|
|
|
|
let (media_type, maybe_charset) =
|
2023-01-26 17:24:03 -05:00
|
|
|
map_content_type(specifier, Some(&content_type));
|
2022-01-16 10:48:32 -05:00
|
|
|
let source = get_source_from_bytes(bytes, maybe_charset)?;
|
2021-04-07 09:22:14 -04:00
|
|
|
let mut headers = HashMap::new();
|
|
|
|
headers.insert("content-type".to_string(), content_type);
|
|
|
|
|
|
|
|
Ok(File {
|
|
|
|
maybe_types: None,
|
|
|
|
media_type,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: source.into(),
|
2021-04-07 09:22:14 -04:00
|
|
|
specifier: specifier.clone(),
|
2021-10-10 17:26:22 -04:00
|
|
|
maybe_headers: Some(headers),
|
2021-04-07 09:22:14 -04:00
|
|
|
})
|
|
|
|
}
|
2021-12-20 21:40:22 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Asynchronously fetch remote source file specified by the URL following
|
|
|
|
/// redirects.
|
2020-02-19 17:51:10 -05:00
|
|
|
///
|
2020-11-05 19:38:21 -05:00
|
|
|
/// **Note** this is a recursive method so it can't be "async", but needs to
|
|
|
|
/// return a `Pin<Box<..>>`.
|
|
|
|
fn fetch_remote(
|
2020-01-04 05:20:52 -05:00
|
|
|
&self,
|
2020-11-05 19:38:21 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2020-11-05 19:38:21 -05:00
|
|
|
redirect_limit: i64,
|
2021-12-20 21:40:22 -05:00
|
|
|
maybe_accept: Option<String>,
|
2020-12-29 23:17:17 -05:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<File, AnyError>> + Send>> {
|
2020-11-05 19:38:21 -05:00
|
|
|
debug!("FileFetcher::fetch_remote() - specifier: {}", specifier);
|
2019-07-31 07:58:41 -04:00
|
|
|
if redirect_limit < 0 {
|
2020-11-05 19:38:21 -05:00
|
|
|
return futures::future::err(custom_error("Http", "Too many redirects."))
|
2020-12-29 23:17:17 -05:00
|
|
|
.boxed();
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
if let Err(err) = permissions.check_specifier(specifier) {
|
2020-12-29 23:17:17 -05:00
|
|
|
return futures::future::err(err).boxed();
|
2020-05-11 07:13:27 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
if self.should_use_cache(specifier) {
|
2020-11-05 19:38:21 -05:00
|
|
|
match self.fetch_cached(specifier, redirect_limit) {
|
|
|
|
Ok(Some(file)) => {
|
2020-12-29 23:17:17 -05:00
|
|
|
return futures::future::ok(file).boxed();
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
2020-11-05 19:38:21 -05:00
|
|
|
Ok(None) => {}
|
2019-07-31 07:58:41 -04:00
|
|
|
Err(err) => {
|
2020-12-29 23:17:17 -05:00
|
|
|
return futures::future::err(err).boxed();
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
if self.cache_setting == CacheSetting::Only {
|
|
|
|
return futures::future::err(custom_error(
|
2021-12-22 08:25:06 -05:00
|
|
|
"NotCached",
|
2020-11-05 19:38:21 -05:00
|
|
|
format!(
|
2023-01-27 10:43:16 -05:00
|
|
|
"Specifier not found in cache: \"{specifier}\", --cached-only is specified."
|
2020-11-05 19:38:21 -05:00
|
|
|
),
|
|
|
|
))
|
2020-12-29 23:17:17 -05:00
|
|
|
.boxed();
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2022-12-15 16:23:50 -05:00
|
|
|
let mut maybe_progress_guard = None;
|
2022-09-09 15:57:39 -04:00
|
|
|
if let Some(pb) = self.progress_bar.as_ref() {
|
2022-12-15 16:23:50 -05:00
|
|
|
maybe_progress_guard = Some(pb.update(specifier.as_str()));
|
2022-09-09 15:57:39 -04:00
|
|
|
} else {
|
|
|
|
log::log!(
|
|
|
|
self.download_log_level,
|
|
|
|
"{} {}",
|
|
|
|
colors::green("Download"),
|
|
|
|
specifier
|
|
|
|
);
|
|
|
|
}
|
2020-03-10 08:26:17 -04:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
let maybe_etag = self
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.cache_item_key(specifier)
|
2023-07-08 16:06:45 -04:00
|
|
|
.ok()
|
2023-08-01 20:49:09 -04:00
|
|
|
.and_then(|key| self.http_cache.read_metadata(&key).ok().flatten())
|
2023-07-08 16:06:45 -04:00
|
|
|
.and_then(|metadata| metadata.headers.get("etag").cloned());
|
2021-07-30 09:03:41 -04:00
|
|
|
let maybe_auth_token = self.auth_tokens.get(specifier);
|
2020-11-05 19:38:21 -05:00
|
|
|
let specifier = specifier.clone();
|
2021-02-15 21:50:27 -05:00
|
|
|
let client = self.http_client.clone();
|
|
|
|
let file_fetcher = self.clone();
|
2023-08-01 04:52:28 -04:00
|
|
|
// A single pass of fetch either yields code or yields a redirect, server
|
|
|
|
// error causes a single retry to avoid crashing hard on intermittent failures.
|
|
|
|
|
|
|
|
async fn handle_request_or_server_error(
|
|
|
|
retried: &mut bool,
|
|
|
|
specifier: &Url,
|
|
|
|
err_str: String,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
// Retry once, and bail otherwise.
|
|
|
|
if !*retried {
|
|
|
|
*retried = true;
|
|
|
|
log::debug!("Import '{}' failed: {}. Retrying...", specifier, err_str);
|
|
|
|
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(generic_error(format!(
|
|
|
|
"Import '{}' failed: {}",
|
|
|
|
specifier, err_str
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
async move {
|
2023-08-01 04:52:28 -04:00
|
|
|
let mut retried = false;
|
|
|
|
let result = loop {
|
|
|
|
let result = match fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url: specifier.clone(),
|
|
|
|
maybe_accept: maybe_accept.clone(),
|
|
|
|
maybe_etag: maybe_etag.clone(),
|
|
|
|
maybe_auth_token: maybe_auth_token.clone(),
|
|
|
|
maybe_progress_guard: maybe_progress_guard.as_ref(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
FetchOnceResult::NotModified => {
|
|
|
|
let file = file_fetcher.fetch_cached(&specifier, 10)?.unwrap();
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
FetchOnceResult::Redirect(redirect_url, headers) => {
|
|
|
|
file_fetcher.http_cache.set(&specifier, headers, &[])?;
|
|
|
|
file_fetcher
|
|
|
|
.fetch_remote(
|
|
|
|
&redirect_url,
|
|
|
|
permissions,
|
|
|
|
redirect_limit - 1,
|
|
|
|
maybe_accept,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
FetchOnceResult::Code(bytes, headers) => {
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
|
|
|
.set(&specifier, headers.clone(), &bytes)?;
|
|
|
|
let file =
|
|
|
|
file_fetcher.build_remote_file(&specifier, bytes, &headers)?;
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
FetchOnceResult::RequestError(err) => {
|
|
|
|
handle_request_or_server_error(&mut retried, &specifier, err)
|
|
|
|
.await?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
FetchOnceResult::ServerError(status) => {
|
|
|
|
handle_request_or_server_error(
|
|
|
|
&mut retried,
|
|
|
|
&specifier,
|
|
|
|
status.to_string(),
|
2021-12-20 21:40:22 -05:00
|
|
|
)
|
2023-08-01 04:52:28 -04:00
|
|
|
.await?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break result;
|
2022-12-15 16:23:50 -05:00
|
|
|
};
|
2023-08-01 04:52:28 -04:00
|
|
|
|
2022-12-15 16:23:50 -05:00
|
|
|
drop(maybe_progress_guard);
|
|
|
|
result
|
2020-11-05 19:38:21 -05:00
|
|
|
}
|
2020-12-29 23:17:17 -05:00
|
|
|
.boxed()
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
/// 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 => {
|
2023-08-01 20:49:09 -04:00
|
|
|
let Ok(cache_key) = self.http_cache.cache_item_key(specifier) else {
|
2023-07-08 16:06:45 -04:00
|
|
|
return false;
|
|
|
|
};
|
2023-08-01 20:49:09 -04:00
|
|
|
let Ok(Some(metadata)) = self.http_cache.read_metadata(&cache_key) else {
|
2023-07-08 16:06:45 -04:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
let cache_semantics = CacheSemantics::new(
|
|
|
|
metadata.headers,
|
|
|
|
metadata.time,
|
|
|
|
SystemTime::now(),
|
|
|
|
);
|
|
|
|
cache_semantics.should_use()
|
2022-11-28 17:28:54 -05:00
|
|
|
}
|
|
|
|
CacheSetting::ReloadSome(list) => {
|
|
|
|
let mut url = specifier.clone();
|
|
|
|
url.set_fragment(None);
|
2023-01-04 07:20:36 -05:00
|
|
|
if list.iter().any(|x| x == url.as_str()) {
|
2022-11-28 17:28:54 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Fetch a source file and asynchronously return it.
|
|
|
|
pub async fn fetch(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2020-11-05 19:38:21 -05:00
|
|
|
) -> Result<File, AnyError> {
|
|
|
|
debug!("FileFetcher::fetch() - specifier: {}", specifier);
|
2021-12-20 21:40:22 -05:00
|
|
|
self.fetch_with_accept(specifier, permissions, None).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_with_accept(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-01-07 11:25:34 -05:00
|
|
|
permissions: PermissionsContainer,
|
2021-12-20 21:40:22 -05:00
|
|
|
maybe_accept: Option<&str>,
|
|
|
|
) -> Result<File, AnyError> {
|
2020-11-05 19:38:21 -05:00
|
|
|
let scheme = get_validated_scheme(specifier)?;
|
|
|
|
permissions.check_specifier(specifier)?;
|
|
|
|
if let Some(file) = self.cache.get(specifier) {
|
|
|
|
Ok(file)
|
2021-01-05 21:22:38 -05:00
|
|
|
} else if scheme == "file" {
|
|
|
|
// we do not in memory cache files, as this would prevent files on the
|
|
|
|
// disk changing effecting things like workers and dynamic imports.
|
|
|
|
fetch_local(specifier)
|
|
|
|
} else if scheme == "data" {
|
2023-07-01 18:52:30 -04:00
|
|
|
self.fetch_data_url(specifier)
|
2021-04-07 09:22:14 -04:00
|
|
|
} else if scheme == "blob" {
|
2023-07-01 18:52:30 -04:00
|
|
|
self.fetch_blob_url(specifier).await
|
2021-01-05 21:22:38 -05:00
|
|
|
} else if !self.allow_remote {
|
|
|
|
Err(custom_error(
|
|
|
|
"NoRemote",
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("A remote specifier was requested: \"{specifier}\", but --no-remote is specified."),
|
2021-01-05 21:22:38 -05:00
|
|
|
))
|
2020-11-05 19:38:21 -05:00
|
|
|
} else {
|
2021-12-20 21:40:22 -05:00
|
|
|
let result = self
|
|
|
|
.fetch_remote(
|
|
|
|
specifier,
|
|
|
|
permissions,
|
|
|
|
10,
|
|
|
|
maybe_accept.map(String::from),
|
|
|
|
)
|
|
|
|
.await;
|
2021-01-05 21:22:38 -05:00
|
|
|
if let Ok(file) = &result {
|
|
|
|
self.cache.insert(specifier.clone(), file.clone());
|
2020-11-05 19:38:21 -05:00
|
|
|
}
|
2021-01-05 21:22:38 -05:00
|
|
|
result
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:19:31 -05:00
|
|
|
/// A synchronous way to retrieve a source file, where if the file has already
|
|
|
|
/// been cached in memory it will be returned, otherwise for local files will
|
|
|
|
/// be read from disk.
|
|
|
|
pub fn get_source(&self, specifier: &ModuleSpecifier) -> Option<File> {
|
|
|
|
let maybe_file = self.cache.get(specifier);
|
|
|
|
if maybe_file.is_none() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let is_local = specifier.scheme() == "file";
|
2020-11-15 21:19:31 -05:00
|
|
|
if is_local {
|
|
|
|
if let Ok(file) = fetch_local(specifier) {
|
|
|
|
return Some(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
maybe_file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
/// Insert a temporary module into the in memory cache for the file fetcher.
|
|
|
|
pub fn insert_cached(&self, file: File) -> Option<File> {
|
|
|
|
self.cache.insert(file.specifier.clone(), file)
|
2019-10-17 10:29:06 -04:00
|
|
|
}
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2022-12-19 14:31:19 -05:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
enum FetchOnceResult {
|
|
|
|
Code(Vec<u8>, HeadersMap),
|
|
|
|
NotModified,
|
|
|
|
Redirect(Url, HeadersMap),
|
2023-08-01 04:52:28 -04:00
|
|
|
RequestError(String),
|
|
|
|
ServerError(StatusCode),
|
2022-12-19 14:31:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct FetchOnceArgs<'a> {
|
|
|
|
pub url: Url,
|
|
|
|
pub maybe_accept: Option<String>,
|
|
|
|
pub maybe_etag: Option<String>,
|
|
|
|
pub maybe_auth_token: Option<AuthToken>,
|
|
|
|
pub maybe_progress_guard: Option<&'a UpdateGuard>,
|
|
|
|
}
|
|
|
|
|
2022-12-14 08:47:18 -05:00
|
|
|
/// Asynchronously fetches the given HTTP URL one pass only.
|
|
|
|
/// If no redirect is present and no error occurs,
|
|
|
|
/// yields Code(ResultPayload).
|
|
|
|
/// If redirect occurs, does not follow and
|
|
|
|
/// yields Redirect(url).
|
2022-12-19 14:31:19 -05:00
|
|
|
async fn fetch_once<'a>(
|
2022-12-14 08:47:18 -05:00
|
|
|
http_client: &HttpClient,
|
2022-12-19 14:31:19 -05:00
|
|
|
args: FetchOnceArgs<'a>,
|
2022-12-14 08:47:18 -05:00
|
|
|
) -> Result<FetchOnceResult, AnyError> {
|
2023-05-01 16:42:05 -04:00
|
|
|
let mut request = http_client.get_no_redirect(args.url.clone())?;
|
2022-12-14 08:47:18 -05:00
|
|
|
|
|
|
|
if let Some(etag) = args.maybe_etag {
|
|
|
|
let if_none_match_val = HeaderValue::from_str(&etag)?;
|
|
|
|
request = request.header(IF_NONE_MATCH, if_none_match_val);
|
|
|
|
}
|
|
|
|
if let Some(auth_token) = args.maybe_auth_token {
|
|
|
|
let authorization_val = HeaderValue::from_str(&auth_token.to_string())?;
|
|
|
|
request = request.header(AUTHORIZATION, authorization_val);
|
|
|
|
}
|
|
|
|
if let Some(accept) = args.maybe_accept {
|
|
|
|
let accepts_val = HeaderValue::from_str(&accept)?;
|
|
|
|
request = request.header(ACCEPT, accepts_val);
|
|
|
|
}
|
2023-08-01 04:52:28 -04:00
|
|
|
let response = match request.send().await {
|
|
|
|
Ok(resp) => resp,
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_connect() || err.is_timeout() {
|
|
|
|
return Ok(FetchOnceResult::RequestError(err.to_string()));
|
|
|
|
}
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
};
|
2022-12-14 08:47:18 -05:00
|
|
|
|
|
|
|
if response.status() == StatusCode::NOT_MODIFIED {
|
|
|
|
return Ok(FetchOnceResult::NotModified);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut result_headers = HashMap::new();
|
|
|
|
let response_headers = response.headers();
|
|
|
|
|
|
|
|
if let Some(warning) = response_headers.get("X-Deno-Warning") {
|
|
|
|
log::warn!(
|
|
|
|
"{} {}",
|
|
|
|
crate::colors::yellow("Warning"),
|
|
|
|
warning.to_str().unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for key in response_headers.keys() {
|
|
|
|
let key_str = key.to_string();
|
|
|
|
let values = response_headers.get_all(key);
|
|
|
|
let values_str = values
|
|
|
|
.iter()
|
|
|
|
.map(|e| e.to_str().unwrap().to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(",");
|
|
|
|
result_headers.insert(key_str, values_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.status().is_redirection() {
|
|
|
|
let new_url = resolve_redirect_from_response(&args.url, &response)?;
|
|
|
|
return Ok(FetchOnceResult::Redirect(new_url, result_headers));
|
|
|
|
}
|
|
|
|
|
2023-08-01 04:52:28 -04:00
|
|
|
let status = response.status();
|
|
|
|
|
|
|
|
if status.is_server_error() {
|
|
|
|
return Ok(FetchOnceResult::ServerError(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.is_client_error() {
|
2022-12-14 08:47:18 -05:00
|
|
|
let err = if response.status() == StatusCode::NOT_FOUND {
|
|
|
|
custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!("Import '{}' failed, not found.", args.url),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
generic_error(format!(
|
|
|
|
"Import '{}' failed: {}",
|
|
|
|
args.url,
|
|
|
|
response.status()
|
|
|
|
))
|
|
|
|
};
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
|
2022-12-19 14:31:19 -05:00
|
|
|
let body = http_util::get_response_body_with_progress(
|
|
|
|
response,
|
|
|
|
args.maybe_progress_guard,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-12-14 08:47:18 -05:00
|
|
|
|
|
|
|
Ok(FetchOnceResult::Code(body, result_headers))
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:58:41 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-08-01 20:49:09 -04:00
|
|
|
use crate::cache::GlobalHttpCache;
|
2022-11-18 17:28:14 -05:00
|
|
|
use crate::http_util::HttpClient;
|
2022-12-14 08:47:18 -05:00
|
|
|
use crate::version;
|
2022-11-18 17:28:14 -05:00
|
|
|
|
2019-07-31 07:58:41 -04:00
|
|
|
use super::*;
|
2020-11-05 19:38:21 -05:00
|
|
|
use deno_core::error::get_custom_error_class;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url;
|
2022-12-14 08:47:18 -05:00
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_runtime::deno_fetch::create_http_client;
|
2023-05-20 21:43:54 -04:00
|
|
|
use deno_runtime::deno_fetch::CreateHttpClientOptions;
|
2021-06-10 09:26:10 -04:00
|
|
|
use deno_runtime::deno_web::Blob;
|
2021-07-05 09:34:37 -04:00
|
|
|
use deno_runtime::deno_web::InMemoryBlobPart;
|
2022-12-14 08:47:18 -05:00
|
|
|
use std::fs::read;
|
2022-04-01 11:15:37 -04:00
|
|
|
use test_util::TempDir;
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
fn setup(
|
|
|
|
cache_setting: CacheSetting,
|
2022-04-01 11:15:37 -04:00
|
|
|
maybe_temp_dir: Option<TempDir>,
|
|
|
|
) -> (FileFetcher, TempDir) {
|
2021-04-07 09:22:14 -04:00
|
|
|
let (file_fetcher, temp_dir, _) =
|
2021-07-05 09:34:37 -04:00
|
|
|
setup_with_blob_store(cache_setting, maybe_temp_dir);
|
2021-04-07 09:22:14 -04:00
|
|
|
(file_fetcher, temp_dir)
|
|
|
|
}
|
|
|
|
|
2021-07-05 09:34:37 -04:00
|
|
|
fn setup_with_blob_store(
|
2021-04-07 09:22:14 -04:00
|
|
|
cache_setting: CacheSetting,
|
2022-04-01 11:15:37 -04:00
|
|
|
maybe_temp_dir: Option<TempDir>,
|
2023-07-01 18:52:30 -04:00
|
|
|
) -> (FileFetcher, TempDir, Arc<BlobStore>) {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = maybe_temp_dir.unwrap_or_default();
|
2023-06-10 11:09:45 -04:00
|
|
|
let location = temp_dir.path().join("deps").to_path_buf();
|
2023-07-01 18:52:30 -04:00
|
|
|
let blob_store: Arc<BlobStore> = Default::default();
|
2021-04-07 09:22:14 -04:00
|
|
|
let file_fetcher = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location)),
|
2021-04-07 09:22:14 -04:00
|
|
|
cache_setting,
|
|
|
|
true,
|
2023-05-01 16:42:05 -04:00
|
|
|
Arc::new(HttpClient::new(None, None)),
|
2021-07-05 09:34:37 -04:00
|
|
|
blob_store.clone(),
|
2021-08-09 10:53:21 -04:00
|
|
|
None,
|
2023-01-25 16:51:04 -05:00
|
|
|
);
|
2021-07-05 09:34:37 -04:00
|
|
|
(file_fetcher, temp_dir, blob_store)
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! file_url {
|
|
|
|
($path:expr) => {
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
concat!("file:///C:", $path)
|
|
|
|
} else {
|
|
|
|
concat!("file://", $path)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch(specifier: &ModuleSpecifier) -> (File, FileFetcher) {
|
|
|
|
let (file_fetcher, _) = setup(CacheSetting::ReloadAll, None);
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(specifier, PermissionsContainer::allow_all())
|
2020-11-05 19:38:21 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
(result.unwrap(), file_fetcher)
|
|
|
|
}
|
2019-10-17 10:29:06 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_remote(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> (File, HashMap<String, String>) {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let (file_fetcher, _) = setup(CacheSetting::ReloadAll, None);
|
|
|
|
let result: Result<File, AnyError> = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch_remote(specifier, PermissionsContainer::allow_all(), 1, None)
|
2020-11-05 19:38:21 -05:00
|
|
|
.await;
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key = file_fetcher.http_cache.cache_item_key(specifier).unwrap();
|
2023-07-08 16:06:45 -04:00
|
|
|
(
|
|
|
|
result.unwrap(),
|
2023-08-01 20:49:09 -04:00
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
|
|
|
.read_metadata(&cache_key)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.headers,
|
2023-07-08 16:06:45 -04:00
|
|
|
)
|
2020-11-05 19:38:21 -05:00
|
|
|
}
|
2019-10-17 10:29:06 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_remote_encoded(
|
|
|
|
fixture: &str,
|
|
|
|
charset: &str,
|
|
|
|
expected: &str,
|
|
|
|
) {
|
2023-01-27 10:43:16 -05:00
|
|
|
let url_str = format!("http://127.0.0.1:4545/encoding/{fixture}");
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url(&url_str).unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
let (file, headers) = test_fetch_remote(&specifier).await;
|
2022-05-20 16:40:55 -04:00
|
|
|
assert_eq!(&*file.source, expected);
|
2020-11-05 19:38:21 -05:00
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
2023-01-27 10:43:16 -05:00
|
|
|
&format!("application/typescript;charset={charset}")
|
2020-11-05 19:38:21 -05:00
|
|
|
);
|
|
|
|
}
|
2019-10-17 10:29:06 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_local_encoded(charset: &str, expected: String) {
|
2023-01-27 10:43:16 -05:00
|
|
|
let p = test_util::testdata_path().join(format!("encoding/{charset}.ts"));
|
2023-06-10 11:09:45 -04:00
|
|
|
let specifier = ModuleSpecifier::from_file_path(p).unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
let (file, _) = test_fetch(&specifier).await;
|
2022-05-20 16:40:55 -04:00
|
|
|
assert_eq!(&*file.source, expected);
|
2020-11-05 19:38:21 -05:00
|
|
|
}
|
2019-10-17 10:29:06 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
#[test]
|
|
|
|
fn test_get_validated_scheme() {
|
|
|
|
let fixtures = vec![
|
|
|
|
("https://deno.land/x/mod.ts", true, "https"),
|
|
|
|
("http://deno.land/x/mod.ts", true, "http"),
|
|
|
|
("file:///a/b/c.ts", true, "file"),
|
|
|
|
("file:///C:/a/b/c.ts", true, "file"),
|
2021-01-05 21:22:38 -05:00
|
|
|
("data:,some%20text", true, "data"),
|
2020-11-05 19:38:21 -05:00
|
|
|
("ftp://a/b/c.ts", false, ""),
|
|
|
|
("mailto:dino@deno.land", false, ""),
|
|
|
|
];
|
2019-10-17 10:29:06 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
for (specifier, is_ok, expected) in fixtures {
|
2023-03-13 19:31:03 -04:00
|
|
|
let specifier = ModuleSpecifier::parse(specifier).unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
let actual = get_validated_scheme(&specifier);
|
|
|
|
assert_eq!(actual.is_ok(), is_ok);
|
|
|
|
if is_ok {
|
|
|
|
assert_eq!(actual.unwrap(), expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 10:29:06 -04:00
|
|
|
|
2019-08-12 21:52:03 -04:00
|
|
|
#[test]
|
2020-11-05 19:38:21 -05:00
|
|
|
fn test_map_content_type() {
|
|
|
|
let fixtures = vec![
|
|
|
|
// Extension only
|
|
|
|
(file_url!("/foo/bar.ts"), None, MediaType::TypeScript, None),
|
2021-03-25 14:17:37 -04:00
|
|
|
(file_url!("/foo/bar.tsx"), None, MediaType::Tsx, None),
|
2021-11-01 16:22:27 -04:00
|
|
|
(file_url!("/foo/bar.d.cts"), None, MediaType::Dcts, None),
|
|
|
|
(file_url!("/foo/bar.d.mts"), None, MediaType::Dmts, None),
|
2020-11-05 19:38:21 -05:00
|
|
|
(file_url!("/foo/bar.d.ts"), None, MediaType::Dts, None),
|
|
|
|
(file_url!("/foo/bar.js"), None, MediaType::JavaScript, None),
|
2021-03-25 14:17:37 -04:00
|
|
|
(file_url!("/foo/bar.jsx"), None, MediaType::Jsx, None),
|
2020-11-05 19:38:21 -05:00
|
|
|
(file_url!("/foo/bar.json"), None, MediaType::Json, None),
|
|
|
|
(file_url!("/foo/bar.wasm"), None, MediaType::Wasm, None),
|
2021-11-01 16:22:27 -04:00
|
|
|
(file_url!("/foo/bar.cjs"), None, MediaType::Cjs, None),
|
|
|
|
(file_url!("/foo/bar.mjs"), None, MediaType::Mjs, None),
|
|
|
|
(file_url!("/foo/bar.cts"), None, MediaType::Cts, None),
|
|
|
|
(file_url!("/foo/bar.mts"), None, MediaType::Mts, None),
|
2020-11-05 19:38:21 -05:00
|
|
|
(file_url!("/foo/bar"), None, MediaType::Unknown, None),
|
|
|
|
// Media type no extension
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("application/typescript".to_string()),
|
|
|
|
MediaType::TypeScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/typescript".to_string()),
|
|
|
|
MediaType::TypeScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("video/vnd.dlna.mpeg-tts".to_string()),
|
|
|
|
MediaType::TypeScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("video/mp2t".to_string()),
|
|
|
|
MediaType::TypeScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("application/x-typescript".to_string()),
|
|
|
|
MediaType::TypeScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("application/javascript".to_string()),
|
|
|
|
MediaType::JavaScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/javascript".to_string()),
|
|
|
|
MediaType::JavaScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("application/ecmascript".to_string()),
|
|
|
|
MediaType::JavaScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/ecmascript".to_string()),
|
|
|
|
MediaType::JavaScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("application/x-javascript".to_string()),
|
|
|
|
MediaType::JavaScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("application/node".to_string()),
|
|
|
|
MediaType::JavaScript,
|
|
|
|
None,
|
|
|
|
),
|
2021-01-05 21:22:38 -05:00
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/jsx".to_string()),
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Jsx,
|
2021-01-05 21:22:38 -05:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/tsx".to_string()),
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Tsx,
|
2021-01-05 21:22:38 -05:00
|
|
|
None,
|
|
|
|
),
|
2020-11-05 19:38:21 -05:00
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/json".to_string()),
|
|
|
|
MediaType::Json,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/json; charset=utf-8".to_string()),
|
|
|
|
MediaType::Json,
|
|
|
|
Some("utf-8".to_string()),
|
|
|
|
),
|
|
|
|
// Extension with media type
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.ts",
|
|
|
|
Some("text/plain".to_string()),
|
|
|
|
MediaType::TypeScript,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.ts",
|
|
|
|
Some("foo/bar".to_string()),
|
|
|
|
MediaType::Unknown,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.tsx",
|
|
|
|
Some("application/typescript".to_string()),
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Tsx,
|
2020-11-05 19:38:21 -05:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.tsx",
|
|
|
|
Some("application/javascript".to_string()),
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Tsx,
|
2020-11-05 19:38:21 -05:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.jsx",
|
|
|
|
Some("application/javascript".to_string()),
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Jsx,
|
2020-11-05 19:38:21 -05:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.jsx",
|
|
|
|
Some("application/x-typescript".to_string()),
|
2021-03-25 14:17:37 -04:00
|
|
|
MediaType::Jsx,
|
2020-11-05 19:38:21 -05:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.d.ts",
|
|
|
|
Some("application/javascript".to_string()),
|
2021-11-01 16:22:27 -04:00
|
|
|
MediaType::Dts,
|
2020-11-05 19:38:21 -05:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.d.ts",
|
|
|
|
Some("text/plain".to_string()),
|
|
|
|
MediaType::Dts,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.d.ts",
|
|
|
|
Some("application/x-typescript".to_string()),
|
|
|
|
MediaType::Dts,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (specifier, maybe_content_type, media_type, maybe_charset) in fixtures {
|
2023-03-13 19:31:03 -04:00
|
|
|
let specifier = ModuleSpecifier::parse(specifier).unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
assert_eq!(
|
2023-01-26 17:24:03 -05:00
|
|
|
map_content_type(&specifier, maybe_content_type.as_ref()),
|
2020-11-05 19:38:21 -05:00
|
|
|
(media_type, maybe_charset)
|
|
|
|
);
|
2019-08-12 21:52:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_insert_cached() {
|
|
|
|
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
|
|
|
|
let local = temp_dir.path().join("a.ts");
|
2023-06-10 11:09:45 -04:00
|
|
|
let specifier = ModuleSpecifier::from_file_path(&local).unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
let file = File {
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::TypeScript,
|
2022-05-20 16:40:55 -04:00
|
|
|
source: "some source code".into(),
|
2020-11-05 19:38:21 -05:00
|
|
|
specifier: specifier.clone(),
|
2021-09-02 11:38:19 -04:00
|
|
|
maybe_headers: None,
|
2020-11-05 19:38:21 -05:00
|
|
|
};
|
|
|
|
file_fetcher.insert_cached(file.clone());
|
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-05 19:38:21 -05:00
|
|
|
let result_file = result.unwrap();
|
|
|
|
assert_eq!(result_file, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-15 21:19:31 -05:00
|
|
|
async fn test_get_source() {
|
2020-11-05 19:38:21 -05:00
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-08-11 10:20:47 -04:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4548/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-11-05 19:38:21 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-15 21:19:31 -05:00
|
|
|
let maybe_file = file_fetcher.get_source(&specifier);
|
2020-11-05 19:38:21 -05:00
|
|
|
assert!(maybe_file.is_some());
|
|
|
|
let file = maybe_file.unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
assert_eq!(&*file.source, "export const redirect = 1;\n");
|
2020-11-05 19:38:21 -05:00
|
|
|
assert_eq!(
|
|
|
|
file.specifier,
|
2021-08-11 10:20:47 -04:00
|
|
|
resolve_url("http://localhost:4545/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap()
|
2020-11-05 19:38:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-05 21:22:38 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_data_url() {
|
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap();
|
2021-01-05 21:22:38 -05:00
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2021-01-05 21:22:38 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*file.source,
|
2021-01-05 21:22:38 -05:00
|
|
|
"export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n"
|
|
|
|
);
|
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(file.maybe_types, None);
|
|
|
|
assert_eq!(file.specifier, specifier);
|
|
|
|
}
|
|
|
|
|
2021-04-07 09:22:14 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_blob_url() {
|
2021-07-05 09:34:37 -04:00
|
|
|
let (file_fetcher, _, blob_store) =
|
|
|
|
setup_with_blob_store(CacheSetting::Use, None);
|
|
|
|
|
|
|
|
let bytes =
|
|
|
|
"export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n"
|
|
|
|
.as_bytes()
|
|
|
|
.to_vec();
|
2021-04-07 09:22:14 -04:00
|
|
|
|
2021-07-05 09:34:37 -04:00
|
|
|
let specifier = blob_store.insert_object_url(
|
2021-04-07 09:22:14 -04:00
|
|
|
Blob {
|
|
|
|
media_type: "application/typescript".to_string(),
|
2021-08-14 04:27:27 -04:00
|
|
|
parts: vec![Arc::new(InMemoryBlobPart::from(bytes))],
|
2021-04-07 09:22:14 -04:00
|
|
|
},
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2021-04-07 09:22:14 -04:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*file.source,
|
2021-04-07 09:22:14 -04:00
|
|
|
"export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n"
|
|
|
|
);
|
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(file.maybe_types, None);
|
|
|
|
assert_eq!(file.specifier, specifier);
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_complex() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
|
|
|
|
let (file_fetcher_01, _) = setup(CacheSetting::Use, Some(temp_dir.clone()));
|
|
|
|
let (file_fetcher_02, _) = setup(CacheSetting::Use, Some(temp_dir.clone()));
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
2023-03-13 19:31:03 -04:00
|
|
|
ModuleSpecifier::parse("http://localhost:4545/subdir/mod2.ts").unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-11-05 19:38:21 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*file.source,
|
2020-10-07 07:43:44 -04:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-03 08:53:50 -05:00
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
2020-02-28 13:15:31 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_item_key =
|
|
|
|
file_fetcher.http_cache.cache_item_key(&specifier).unwrap();
|
2023-07-08 16:06:45 -04:00
|
|
|
let mut metadata = file_fetcher
|
2020-11-05 19:38:21 -05:00
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_metadata(&cache_item_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap()
|
2021-02-15 04:32:06 -05:00
|
|
|
.unwrap();
|
2020-02-28 13:15:31 -05:00
|
|
|
metadata.headers = HashMap::new();
|
|
|
|
metadata
|
|
|
|
.headers
|
2020-09-16 07:34:16 -04:00
|
|
|
.insert("content-type".to_string(), "text/javascript".to_string());
|
2023-07-08 16:06:45 -04:00
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.set(&specifier, metadata.headers.clone(), file.source.as_bytes())
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap();
|
2020-02-28 13:15:31 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher_01
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
2020-11-05 19:38:21 -05:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2020-02-03 08:53:50 -05:00
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*file.source,
|
2020-10-07 07:43:44 -04:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-03 08:53:50 -05:00
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
// This validates that when using the cached value, because we modified
|
|
|
|
// the value above.
|
|
|
|
assert_eq!(file.media_type, MediaType::JavaScript);
|
2020-02-19 08:17:13 -05:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
let headers = file_fetcher_02
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_metadata(&cache_item_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.headers;
|
2020-09-16 07:34:16 -04:00
|
|
|
assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
|
2020-02-28 13:15:31 -05:00
|
|
|
metadata.headers = HashMap::new();
|
|
|
|
metadata
|
|
|
|
.headers
|
2020-09-16 07:34:16 -04:00
|
|
|
.insert("content-type".to_string(), "application/json".to_string());
|
2023-07-08 16:06:45 -04:00
|
|
|
file_fetcher_02
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.set(&specifier, metadata.headers.clone(), file.source.as_bytes())
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap();
|
2020-02-28 13:15:31 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher_02
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
2020-11-05 19:38:21 -05:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2020-02-03 08:53:50 -05:00
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*file.source,
|
2020-10-07 07:43:44 -04:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-03 08:53:50 -05:00
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
assert_eq!(file.media_type, MediaType::Json);
|
|
|
|
|
|
|
|
// This creates a totally new instance, simulating another Deno process
|
|
|
|
// invocation and indicates to "cache bust".
|
2023-06-10 11:09:45 -04:00
|
|
|
let location = temp_dir.path().join("deps").to_path_buf();
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location)),
|
2020-11-05 19:38:21 -05:00
|
|
|
CacheSetting::ReloadAll,
|
|
|
|
true,
|
2023-05-01 16:42:05 -04:00
|
|
|
Arc::new(HttpClient::new(None, None)),
|
2023-07-01 18:52:30 -04:00
|
|
|
Default::default(),
|
2021-08-09 10:53:21 -04:00
|
|
|
None,
|
2023-01-25 16:51:04 -05:00
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-05 19:38:21 -05:00
|
|
|
let file = result.unwrap();
|
2020-02-19 08:17:13 -05:00
|
|
|
assert_eq!(
|
2022-05-20 16:40:55 -04:00
|
|
|
&*file.source,
|
2020-11-05 19:38:21 -05:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-19 08:17:13 -05:00
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_uses_cache() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2023-06-10 11:09:45 -04:00
|
|
|
let location = temp_dir.path().join("deps").to_path_buf();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
2021-08-11 10:20:47 -04:00
|
|
|
resolve_url("http://localhost:4545/subdir/mismatch_ext.ts").unwrap();
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
let file_modified_01 = {
|
|
|
|
let file_fetcher = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location.clone())),
|
2023-07-08 16:06:45 -04:00
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
Arc::new(HttpClient::new(None, None)),
|
|
|
|
Default::default(),
|
|
|
|
None,
|
|
|
|
);
|
2019-11-22 12:46:57 -05:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key =
|
|
|
|
file_fetcher.http_cache.cache_item_key(&specifier).unwrap();
|
2023-07-08 16:06:45 -04:00
|
|
|
(
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_modified_time(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap(),
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_metadata(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
};
|
2020-11-05 19:38:21 -05:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
let file_modified_02 = {
|
|
|
|
let file_fetcher = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location)),
|
2023-07-08 16:06:45 -04:00
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
Arc::new(HttpClient::new(None, None)),
|
|
|
|
Default::default(),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2019-11-22 12:46:57 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key =
|
|
|
|
file_fetcher.http_cache.cache_item_key(&specifier).unwrap();
|
2023-07-08 16:06:45 -04:00
|
|
|
(
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_modified_time(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap(),
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_metadata(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
};
|
2020-11-05 19:38:21 -05:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
assert_eq!(file_modified_01, file_modified_02);
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_redirected() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-05 19:38:21 -05:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-08-11 10:20:47 -04:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4546/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
|
|
|
let redirected_specifier =
|
|
|
|
resolve_url("http://localhost:4545/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-05 19:38:21 -05:00
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.specifier, redirected_specifier);
|
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_text_from_cache(&file_fetcher, &specifier),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_location_header_from_cache(&file_fetcher, &specifier),
|
|
|
|
Some("http://localhost:4545/subdir/redirects/redirect1.js".to_string()),
|
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_text_from_cache(&file_fetcher, &redirected_specifier),
|
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_location_header_from_cache(&file_fetcher, &redirected_specifier),
|
|
|
|
None,
|
|
|
|
);
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_multiple_redirects() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-05 19:38:21 -05:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-08-11 10:20:47 -04:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4548/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
|
|
|
let redirected_01_specifier =
|
|
|
|
resolve_url("http://localhost:4546/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
|
|
|
let redirected_02_specifier =
|
|
|
|
resolve_url("http://localhost:4545/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-05 19:38:21 -05:00
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.specifier, redirected_02_specifier);
|
2020-02-03 08:53:50 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_text_from_cache(&file_fetcher, &specifier),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_location_header_from_cache(&file_fetcher, &specifier),
|
|
|
|
Some("http://localhost:4546/subdir/redirects/redirect1.js".to_string()),
|
|
|
|
);
|
2020-02-19 08:17:13 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_text_from_cache(&file_fetcher, &redirected_01_specifier),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_location_header_from_cache(&file_fetcher, &redirected_01_specifier),
|
|
|
|
Some("http://localhost:4545/subdir/redirects/redirect1.js".to_string()),
|
|
|
|
);
|
2020-02-03 08:53:50 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_text_from_cache(&file_fetcher, &redirected_02_specifier),
|
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_location_header_from_cache(&file_fetcher, &redirected_02_specifier),
|
|
|
|
None,
|
|
|
|
);
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_uses_cache_with_redirects() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2023-06-10 11:09:45 -04:00
|
|
|
let location = temp_dir.path().join("deps").to_path_buf();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
2021-08-11 10:20:47 -04:00
|
|
|
resolve_url("http://localhost:4548/subdir/mismatch_ext.ts").unwrap();
|
2021-02-17 13:47:18 -05:00
|
|
|
let redirected_specifier =
|
2021-08-11 10:20:47 -04:00
|
|
|
resolve_url("http://localhost:4546/subdir/mismatch_ext.ts").unwrap();
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
let metadata_file_modified_01 = {
|
|
|
|
let file_fetcher = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location.clone())),
|
2023-07-08 16:06:45 -04:00
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
Arc::new(HttpClient::new(None, None)),
|
|
|
|
Default::default(),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-05 19:38:21 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key = file_fetcher
|
|
|
|
.http_cache
|
|
|
|
.cache_item_key(&redirected_specifier)
|
|
|
|
.unwrap();
|
2023-07-08 16:06:45 -04:00
|
|
|
(
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_modified_time(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap(),
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_metadata(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
};
|
2020-11-05 19:38:21 -05:00
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
let metadata_file_modified_02 = {
|
|
|
|
let file_fetcher = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location)),
|
2023-07-08 16:06:45 -04:00
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
Arc::new(HttpClient::new(None, None)),
|
|
|
|
Default::default(),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&redirected_specifier, PermissionsContainer::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-02-03 08:53:50 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key = file_fetcher
|
|
|
|
.http_cache
|
|
|
|
.cache_item_key(&redirected_specifier)
|
|
|
|
.unwrap();
|
2023-07-08 16:06:45 -04:00
|
|
|
(
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_modified_time(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap(),
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
2023-08-01 20:49:09 -04:00
|
|
|
.read_metadata(&cache_key)
|
2023-07-08 16:06:45 -04:00
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
};
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
assert_eq!(metadata_file_modified_01, metadata_file_modified_02);
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetcher_limits_redirects() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-05 19:38:21 -05:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-08-11 10:20:47 -04:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4548/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
2019-10-06 15:03:30 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch_remote(&specifier, PermissionsContainer::allow_all(), 2, None)
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch_remote(&specifier, PermissionsContainer::allow_all(), 1, None)
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_err());
|
2020-06-15 17:46:48 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher.fetch_cached(&specifier, 2);
|
2020-06-15 17:46:48 -04:00
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher.fetch_cached(&specifier, 1);
|
2020-06-15 17:46:48 -04:00
|
|
|
assert!(result.is_err());
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 09:04:18 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_same_host_redirect() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-05 19:38:21 -05:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url(
|
2021-08-11 10:20:47 -04:00
|
|
|
"http://localhost:4550/REDIRECT/subdir/redirects/redirect1.js",
|
2020-02-28 09:04:18 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-08-11 10:20:47 -04:00
|
|
|
let redirected_specifier =
|
|
|
|
resolve_url("http://localhost:4550/subdir/redirects/redirect1.js")
|
|
|
|
.unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-28 09:04:18 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-05 19:38:21 -05:00
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.specifier, redirected_specifier);
|
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_text_from_cache(&file_fetcher, &specifier),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_location_header_from_cache(&file_fetcher, &specifier),
|
|
|
|
Some("/subdir/redirects/redirect1.js".to_string()),
|
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
assert_eq!(
|
|
|
|
get_text_from_cache(&file_fetcher, &redirected_specifier),
|
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_location_header_from_cache(&file_fetcher, &redirected_specifier),
|
|
|
|
None
|
|
|
|
);
|
2020-02-28 09:04:18 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_no_remote() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2023-06-10 11:09:45 -04:00
|
|
|
let location = temp_dir.path().join("deps").to_path_buf();
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location)),
|
2020-11-05 19:38:21 -05:00
|
|
|
CacheSetting::Use,
|
|
|
|
false,
|
2023-05-01 16:42:05 -04:00
|
|
|
Arc::new(HttpClient::new(None, None)),
|
2023-07-01 18:52:30 -04:00
|
|
|
Default::default(),
|
2021-08-09 10:53:21 -04:00
|
|
|
None,
|
2023-01-25 16:51:04 -05:00
|
|
|
);
|
2022-09-19 10:32:21 -04:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4545/run/002_hello.ts").unwrap();
|
2019-12-03 17:48:53 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_err());
|
2020-11-05 19:38:21 -05:00
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(get_custom_error_class(&err), Some("NoRemote"));
|
2022-09-19 10:32:21 -04:00
|
|
|
assert_eq!(err.to_string(), "A remote specifier was requested: \"http://localhost:4545/run/002_hello.ts\", but --no-remote is specified.");
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_cache_only() {
|
2020-08-10 17:31:05 -04:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2023-06-10 11:09:45 -04:00
|
|
|
let location = temp_dir.path().join("deps").to_path_buf();
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher_01 = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location.clone())),
|
2020-11-05 19:38:21 -05:00
|
|
|
CacheSetting::Only,
|
|
|
|
true,
|
2023-05-01 16:42:05 -04:00
|
|
|
Arc::new(HttpClient::new(None, None)),
|
2023-07-01 18:52:30 -04:00
|
|
|
Default::default(),
|
2021-08-09 10:53:21 -04:00
|
|
|
None,
|
2023-01-25 16:51:04 -05:00
|
|
|
);
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher_02 = FileFetcher::new(
|
2023-08-01 20:49:09 -04:00
|
|
|
Arc::new(GlobalHttpCache::new(location)),
|
2020-11-05 19:38:21 -05:00
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
2023-05-01 16:42:05 -04:00
|
|
|
Arc::new(HttpClient::new(None, None)),
|
2023-07-01 18:52:30 -04:00
|
|
|
Default::default(),
|
2021-08-09 10:53:21 -04:00
|
|
|
None,
|
2023-01-25 16:51:04 -05:00
|
|
|
);
|
2022-09-19 10:32:21 -04:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4545/run/002_hello.ts").unwrap();
|
2019-07-31 07:58:41 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher_01
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
2020-11-05 19:38:21 -05:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
2022-09-19 10:32:21 -04:00
|
|
|
assert_eq!(err.to_string(), "Specifier not found in cache: \"http://localhost:4545/run/002_hello.ts\", --cached-only is specified.");
|
2023-07-08 16:06:45 -04:00
|
|
|
assert_eq!(get_custom_error_class(&err), Some("NotCached"));
|
2020-06-24 14:12:04 -04:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher_02
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-02-19 08:17:13 -05:00
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let result = file_fetcher_01
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-02-03 08:53:50 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-08-03 17:39:48 -04:00
|
|
|
}
|
|
|
|
|
2020-11-15 21:19:31 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_local_bypasses_file_cache() {
|
|
|
|
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
|
|
|
|
let fixture_path = temp_dir.path().join("mod.ts");
|
2023-03-13 19:31:03 -04:00
|
|
|
let specifier = ModuleSpecifier::from_file_path(&fixture_path).unwrap();
|
2021-11-15 19:59:19 -05:00
|
|
|
fs::write(fixture_path.clone(), r#"console.log("hello deno");"#).unwrap();
|
2020-11-15 21:19:31 -05:00
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-11-15 21:19:31 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
assert_eq!(&*file.source, r#"console.log("hello deno");"#);
|
2020-11-15 21:19:31 -05:00
|
|
|
|
2021-11-15 19:59:19 -05:00
|
|
|
fs::write(fixture_path, r#"console.log("goodbye deno");"#).unwrap();
|
2020-11-15 21:19:31 -05:00
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2020-11-15 21:19:31 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
assert_eq!(&*file.source, r#"console.log("goodbye deno");"#);
|
2020-11-15 21:19:31 -05:00
|
|
|
}
|
|
|
|
|
2021-12-09 06:16:17 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_respect_cache_revalidates() {
|
|
|
|
let _g = test_util::http_server();
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2021-12-09 06:16:17 -05:00
|
|
|
let (file_fetcher, _) =
|
|
|
|
setup(CacheSetting::RespectHeaders, Some(temp_dir.clone()));
|
|
|
|
let specifier =
|
|
|
|
ModuleSpecifier::parse("http://localhost:4545/dynamic").unwrap();
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2021-12-09 06:16:17 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let first = file.source;
|
2021-12-09 06:16:17 -05:00
|
|
|
|
|
|
|
let (file_fetcher, _) =
|
|
|
|
setup(CacheSetting::RespectHeaders, Some(temp_dir.clone()));
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2021-12-09 06:16:17 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let second = file.source;
|
2021-12-09 06:16:17 -05:00
|
|
|
|
|
|
|
assert_ne!(first, second);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_respect_cache_still_fresh() {
|
|
|
|
let _g = test_util::http_server();
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
2021-12-09 06:16:17 -05:00
|
|
|
let (file_fetcher, _) =
|
|
|
|
setup(CacheSetting::RespectHeaders, Some(temp_dir.clone()));
|
|
|
|
let specifier =
|
|
|
|
ModuleSpecifier::parse("http://localhost:4545/dynamic_cache").unwrap();
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2021-12-09 06:16:17 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let first = file.source;
|
2021-12-09 06:16:17 -05:00
|
|
|
|
|
|
|
let (file_fetcher, _) =
|
|
|
|
setup(CacheSetting::RespectHeaders, Some(temp_dir.clone()));
|
|
|
|
let result = file_fetcher
|
2023-01-07 11:25:34 -05:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2021-12-09 06:16:17 -05:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2022-05-20 16:40:55 -04:00
|
|
|
let second = file.source;
|
2021-12-09 06:16:17 -05:00
|
|
|
|
|
|
|
assert_eq!(first, second);
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:39:48 -04:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_local_utf_16be() {
|
|
|
|
let expected = String::from_utf8(
|
|
|
|
b"\xEF\xBB\xBFconsole.log(\"Hello World\");\x0A".to_vec(),
|
2020-08-03 17:39:48 -04:00
|
|
|
)
|
2020-11-05 19:38:21 -05:00
|
|
|
.unwrap();
|
|
|
|
test_fetch_local_encoded("utf-16be", expected).await;
|
2020-08-03 17:39:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_local_utf_16le() {
|
|
|
|
let expected = String::from_utf8(
|
|
|
|
b"\xEF\xBB\xBFconsole.log(\"Hello World\");\x0A".to_vec(),
|
2020-08-03 17:39:48 -04:00
|
|
|
)
|
2020-11-05 19:38:21 -05:00
|
|
|
.unwrap();
|
|
|
|
test_fetch_local_encoded("utf-16le", expected).await;
|
2020-08-03 17:39:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_local_utf8_with_bom() {
|
|
|
|
let expected = String::from_utf8(
|
|
|
|
b"\xEF\xBB\xBFconsole.log(\"Hello World\");\x0A".to_vec(),
|
2020-08-03 17:39:48 -04:00
|
|
|
)
|
2020-11-05 19:38:21 -05:00
|
|
|
.unwrap();
|
|
|
|
test_fetch_local_encoded("utf-8", expected).await;
|
2020-01-11 05:11:05 -05:00
|
|
|
}
|
2020-01-26 13:59:41 -05:00
|
|
|
|
2020-02-03 08:53:50 -05:00
|
|
|
#[tokio::test]
|
2021-05-12 19:07:22 -04:00
|
|
|
async fn test_fetch_remote_javascript_with_types() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
2023-03-13 19:31:03 -04:00
|
|
|
ModuleSpecifier::parse("http://127.0.0.1:4545/xTypeScriptTypes.js")
|
|
|
|
.unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
let (file, _) = test_fetch_remote(&specifier).await;
|
2020-02-03 08:53:50 -05:00
|
|
|
assert_eq!(
|
2020-11-05 19:38:21 -05:00
|
|
|
file.maybe_types,
|
2020-06-19 06:27:15 -04:00
|
|
|
Some("./xTypeScriptTypes.d.ts".to_string())
|
2020-02-03 08:53:50 -05:00
|
|
|
);
|
2020-01-26 13:59:41 -05:00
|
|
|
}
|
2020-08-03 17:39:48 -04:00
|
|
|
|
2021-05-12 19:07:22 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_remote_jsx_with_types() {
|
|
|
|
let specifier =
|
2023-03-13 19:31:03 -04:00
|
|
|
ModuleSpecifier::parse("http://127.0.0.1:4545/xTypeScriptTypes.jsx")
|
2021-05-12 19:07:22 -04:00
|
|
|
.unwrap();
|
|
|
|
let (file, _) = test_fetch_remote(&specifier).await;
|
|
|
|
assert_eq!(file.media_type, MediaType::Jsx,);
|
|
|
|
assert_eq!(
|
|
|
|
file.maybe_types,
|
|
|
|
Some("./xTypeScriptTypes.d.ts".to_string())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_remote_typescript_with_types() {
|
|
|
|
let specifier =
|
2023-03-13 19:31:03 -04:00
|
|
|
ModuleSpecifier::parse("http://127.0.0.1:4545/xTypeScriptTypes.ts")
|
|
|
|
.unwrap();
|
2021-05-12 19:07:22 -04:00
|
|
|
let (file, _) = test_fetch_remote(&specifier).await;
|
|
|
|
assert_eq!(file.maybe_types, None);
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:39:48 -04:00
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_remote_utf16_le() {
|
|
|
|
let expected =
|
2020-08-03 17:39:48 -04:00
|
|
|
std::str::from_utf8(b"\xEF\xBB\xBFconsole.log(\"Hello World\");\x0A")
|
|
|
|
.unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
test_fetch_remote_encoded("utf-16le.ts", "utf-16le", expected).await;
|
2020-08-03 17:39:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_remote_utf16_be() {
|
|
|
|
let expected =
|
2020-08-03 17:39:48 -04:00
|
|
|
std::str::from_utf8(b"\xEF\xBB\xBFconsole.log(\"Hello World\");\x0A")
|
|
|
|
.unwrap();
|
2020-11-05 19:38:21 -05:00
|
|
|
test_fetch_remote_encoded("utf-16be.ts", "utf-16be", expected).await;
|
2020-08-03 17:39:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-05 19:38:21 -05:00
|
|
|
async fn test_fetch_remote_window_1255() {
|
|
|
|
let expected = "console.log(\"\u{5E9}\u{5DC}\u{5D5}\u{5DD} \
|
2020-08-03 17:39:48 -04:00
|
|
|
\u{5E2}\u{5D5}\u{5DC}\u{5DD}\");\u{A}";
|
2020-11-05 19:38:21 -05:00
|
|
|
test_fetch_remote_encoded("windows-1255", "windows-1255", expected).await;
|
2020-08-03 17:39:48 -04:00
|
|
|
}
|
2022-12-14 08:47:18 -05:00
|
|
|
|
|
|
|
fn create_test_client() -> HttpClient {
|
|
|
|
HttpClient::from_client(
|
2023-05-20 21:43:54 -04:00
|
|
|
create_http_client("test_client", CreateHttpClientOptions::default())
|
2023-03-23 18:27:58 -04:00
|
|
|
.unwrap(),
|
2022-12-14 08:47:18 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_string() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url = Url::parse("http://127.0.0.1:4545/assets/fixture.json").unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(headers.get("content-type").unwrap(), "application/json");
|
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_gzip() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url = Url::parse("http://127.0.0.1:4545/run/import_compression/gziped")
|
|
|
|
.unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
|
|
|
"application/javascript"
|
|
|
|
);
|
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_etag() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let url = Url::parse("http://127.0.0.1:4545/etag_script.ts").unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url: url.clone(),
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
|
|
|
"application/typescript"
|
|
|
|
);
|
|
|
|
assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: Some("33a64df551425fcc55e".to_string()),
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_brotli() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url = Url::parse("http://127.0.0.1:4545/run/import_compression/brotli")
|
|
|
|
.unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
|
|
|
"application/javascript"
|
|
|
|
);
|
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_accept() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url = Url::parse("http://127.0.0.1:4545/echo_accept").unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: Some("application/json".to_string()),
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, _)) = result {
|
|
|
|
assert_eq!(body, r#"{"accept":"application/json"}"#.as_bytes());
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_once_with_redirect() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url = Url::parse("http://127.0.0.1:4546/assets/fixture.json").unwrap();
|
|
|
|
// Dns resolver substitutes `127.0.0.1` with `localhost`
|
|
|
|
let target_url =
|
|
|
|
Url::parse("http://localhost:4545/assets/fixture.json").unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Redirect(url, _)) = result {
|
|
|
|
assert_eq!(url, target_url);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_cafile_string() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url = Url::parse("https://localhost:5545/assets/fixture.json").unwrap();
|
|
|
|
|
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
2023-05-20 21:43:54 -04:00
|
|
|
CreateHttpClientOptions {
|
|
|
|
ca_certs: vec![read(
|
2023-06-10 11:09:45 -04:00
|
|
|
test_util::testdata_path().join("tls/RootCA.pem"),
|
2023-05-20 21:43:54 -04:00
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-12-14 08:47:18 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(headers.get("content-type").unwrap(), "application/json");
|
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_default_certificate_store() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server with a valid mozilla root CA cert.
|
2023-04-23 14:07:30 -04:00
|
|
|
let url = Url::parse("https://deno.land/x").unwrap();
|
2022-12-14 08:47:18 -05:00
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
2023-05-20 21:43:54 -04:00
|
|
|
CreateHttpClientOptions::default(),
|
2022-12-14 08:47:18 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("{result:?}");
|
2022-12-14 08:47:18 -05:00
|
|
|
if let Ok(FetchOnceResult::Code(body, _headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@justinmchase): Windows should verify certs too and fail to make this request without ca certs
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[tokio::test]
|
|
|
|
#[ignore] // https://github.com/denoland/deno/issues/12561
|
|
|
|
async fn test_fetch_with_empty_certificate_store() {
|
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
2023-05-01 16:42:05 -04:00
|
|
|
use deno_runtime::deno_tls::RootCertStoreProvider;
|
|
|
|
|
|
|
|
struct ValueRootCertStoreProvider(RootCertStore);
|
|
|
|
|
|
|
|
impl RootCertStoreProvider for ValueRootCertStoreProvider {
|
|
|
|
fn get_or_try_init(&self) -> Result<&RootCertStore, AnyError> {
|
|
|
|
Ok(&self.0)
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 08:47:18 -05:00
|
|
|
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server with a valid mozilla root CA cert.
|
|
|
|
let url = Url::parse("https://deno.land").unwrap();
|
|
|
|
let client = HttpClient::new(
|
2023-05-01 16:42:05 -04:00
|
|
|
// no certs loaded at all
|
|
|
|
Some(Arc::new(ValueRootCertStoreProvider(RootCertStore::empty()))),
|
2022-12-14 08:47:18 -05:00
|
|
|
None,
|
2023-05-01 16:42:05 -04:00
|
|
|
);
|
2022-12-14 08:47:18 -05:00
|
|
|
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Ok(FetchOnceResult::Code(_body, _headers)) = result {
|
|
|
|
// This test is expected to fail since to CA certs have been loaded
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_cafile_gzip() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url =
|
|
|
|
Url::parse("https://localhost:5545/run/import_compression/gziped")
|
|
|
|
.unwrap();
|
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
2023-05-20 21:43:54 -04:00
|
|
|
CreateHttpClientOptions {
|
|
|
|
ca_certs: vec![read(
|
|
|
|
test_util::testdata_path()
|
|
|
|
.join("tls/RootCA.pem")
|
2023-06-10 11:09:45 -04:00
|
|
|
.to_string(),
|
2023-05-20 21:43:54 -04:00
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-12-14 08:47:18 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('gzip')");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
|
|
|
"application/javascript"
|
|
|
|
);
|
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_cafile_with_etag() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let url = Url::parse("https://localhost:5545/etag_script.ts").unwrap();
|
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
2023-05-20 21:43:54 -04:00
|
|
|
CreateHttpClientOptions {
|
|
|
|
ca_certs: vec![read(
|
|
|
|
test_util::testdata_path()
|
|
|
|
.join("tls/RootCA.pem")
|
2023-06-10 11:09:45 -04:00
|
|
|
.to_string(),
|
2023-05-20 21:43:54 -04:00
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-12-14 08:47:18 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url: url.clone(),
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('etag')");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
|
|
|
"application/typescript"
|
|
|
|
);
|
|
|
|
assert_eq!(headers.get("etag").unwrap(), "33a64df551425fcc55e");
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: Some("33a64df551425fcc55e".to_string()),
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(res.unwrap(), FetchOnceResult::NotModified);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_with_cafile_brotli() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
// Relies on external http server. See target/debug/test_server
|
|
|
|
let url =
|
|
|
|
Url::parse("https://localhost:5545/run/import_compression/brotli")
|
|
|
|
.unwrap();
|
|
|
|
let client = HttpClient::from_client(
|
|
|
|
create_http_client(
|
|
|
|
version::get_user_agent(),
|
2023-05-20 21:43:54 -04:00
|
|
|
CreateHttpClientOptions {
|
|
|
|
ca_certs: vec![read(
|
|
|
|
test_util::testdata_path()
|
|
|
|
.join("tls/RootCA.pem")
|
2023-06-10 11:09:45 -04:00
|
|
|
.to_string(),
|
2023-05-20 21:43:54 -04:00
|
|
|
)
|
|
|
|
.unwrap()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-12-14 08:47:18 -05:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
);
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if let Ok(FetchOnceResult::Code(body, headers)) = result {
|
|
|
|
assert!(!body.is_empty());
|
|
|
|
assert_eq!(String::from_utf8(body).unwrap(), "console.log('brotli');");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
|
|
|
"application/javascript"
|
|
|
|
);
|
|
|
|
assert_eq!(headers.get("etag"), None);
|
|
|
|
assert_eq!(headers.get("x-typescript-types"), None);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn bad_redirect() {
|
|
|
|
let _g = test_util::http_server();
|
|
|
|
let url_str = "http://127.0.0.1:4545/bad_redirect";
|
|
|
|
let url = Url::parse(url_str).unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
2022-12-19 14:31:19 -05:00
|
|
|
maybe_progress_guard: None,
|
2022-12-14 08:47:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
// Check that the error message contains the original URL
|
|
|
|
assert!(err.to_string().contains(url_str));
|
|
|
|
}
|
2023-08-01 04:52:28 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn server_error() {
|
|
|
|
let _g = test_util::http_server();
|
|
|
|
let url_str = "http://127.0.0.1:4545/server_error";
|
|
|
|
let url = Url::parse(url_str).unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
|
|
|
maybe_progress_guard: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Ok(FetchOnceResult::ServerError(status)) = result {
|
|
|
|
assert_eq!(status, 500);
|
|
|
|
} else {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn request_error() {
|
|
|
|
let _g = test_util::http_server();
|
|
|
|
let url_str = "http://127.0.0.1:9999/";
|
|
|
|
let url = Url::parse(url_str).unwrap();
|
|
|
|
let client = create_test_client();
|
|
|
|
let result = fetch_once(
|
|
|
|
&client,
|
|
|
|
FetchOnceArgs {
|
|
|
|
url,
|
|
|
|
maybe_accept: None,
|
|
|
|
maybe_etag: None,
|
|
|
|
maybe_auth_token: None,
|
|
|
|
maybe_progress_guard: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
assert!(matches!(result, Ok(FetchOnceResult::RequestError(_))));
|
|
|
|
}
|
2023-08-01 20:49:09 -04:00
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn get_text_from_cache(
|
|
|
|
file_fetcher: &FileFetcher,
|
|
|
|
url: &ModuleSpecifier,
|
|
|
|
) -> String {
|
|
|
|
let cache_key = file_fetcher.http_cache.cache_item_key(url).unwrap();
|
|
|
|
let bytes = file_fetcher
|
|
|
|
.http_cache
|
|
|
|
.read_file_bytes(&cache_key)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
String::from_utf8(bytes).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn get_location_header_from_cache(
|
|
|
|
file_fetcher: &FileFetcher,
|
|
|
|
url: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
|
|
|
let cache_key = file_fetcher.http_cache.cache_item_key(url).unwrap();
|
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
|
|
|
.read_metadata(&cache_key)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.headers
|
|
|
|
.remove("location")
|
|
|
|
}
|
2019-07-31 07:58:41 -04:00
|
|
|
}
|