2021-01-11 17:13:41 +00:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2021-02-16 02:50:27 +00:00
|
|
|
use crate::auth_tokens::AuthTokens;
|
2020-02-18 15:08:18 +00:00
|
|
|
use crate::colors;
|
2020-02-19 13:17:13 +00:00
|
|
|
use crate::http_cache::HttpCache;
|
2020-01-16 15:10:01 +00:00
|
|
|
use crate::http_util::create_http_client;
|
2020-11-06 00:38:21 +00:00
|
|
|
use crate::http_util::fetch_once;
|
2021-02-16 02:50:27 +00:00
|
|
|
use crate::http_util::FetchOnceArgs;
|
2019-04-25 17:29:21 +00:00
|
|
|
use crate::http_util::FetchOnceResult;
|
2020-09-15 14:33:14 +00:00
|
|
|
use crate::media_type::MediaType;
|
2020-08-03 21:39:48 +00:00
|
|
|
use crate::text_encoding;
|
2021-01-08 02:08:51 +00:00
|
|
|
use crate::version::get_user_agent;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-11-06 00:38:21 +00:00
|
|
|
|
2020-09-14 16:48:57 +00: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 16:36:37 +00:00
|
|
|
use deno_core::futures;
|
|
|
|
use deno_core::futures::future::FutureExt;
|
2020-01-05 16:56:18 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_runtime::deno_fetch::reqwest;
|
2019-07-31 11:58:41 +00:00
|
|
|
use std::collections::HashMap;
|
2021-02-16 02:50:27 +00:00
|
|
|
use std::env;
|
2019-07-31 11:58:41 +00:00
|
|
|
use std::fs;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::future::Future;
|
2020-02-19 13:17:13 +00:00
|
|
|
use std::io::Read;
|
2019-07-31 11:58:41 +00:00
|
|
|
use std::path::PathBuf;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::pin::Pin;
|
2019-07-31 11:58:41 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2021-02-16 02:50:27 +00:00
|
|
|
static DENO_AUTH_TOKENS: &str = "DENO_AUTH_TOKENS";
|
2021-01-06 02:22:38 +00:00
|
|
|
pub const SUPPORTED_SCHEMES: [&str; 4] = ["data", "file", "http", "https"];
|
2020-11-06 00:38:21 +00:00
|
|
|
|
|
|
|
/// A structure representing a source file.
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct File {
|
|
|
|
/// The path to the local version of the source file. For local files this
|
|
|
|
/// will be the direct path to that file. For remote files, it will be the
|
|
|
|
/// path to the file in the HTTP cache.
|
|
|
|
pub local: PathBuf,
|
|
|
|
/// 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 14:33:14 +00:00
|
|
|
pub media_type: MediaType,
|
2020-11-06 00:38:21 +00:00
|
|
|
/// The source of the file as a string.
|
|
|
|
pub source: String,
|
|
|
|
/// 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,
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Simple struct implementing in-process caching to prevent multiple
|
|
|
|
/// fs reads/net fetches for same file.
|
|
|
|
#[derive(Clone, Default)]
|
2020-11-06 00:38:21 +00:00
|
|
|
struct FileCache(Arc<Mutex<HashMap<ModuleSpecifier, File>>>);
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
impl FileCache {
|
|
|
|
pub fn get(&self, specifier: &ModuleSpecifier) -> Option<File> {
|
|
|
|
let cache = self.0.lock().unwrap();
|
2020-11-12 22:17:31 +00:00
|
|
|
cache.get(specifier).cloned()
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
pub fn insert(&self, specifier: ModuleSpecifier, file: File) -> Option<File> {
|
|
|
|
let mut cache = self.0.lock().unwrap();
|
|
|
|
cache.insert(specifier, file)
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Indicates how cached source files should be handled.
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub enum CacheSetting {
|
|
|
|
/// Only the cached files should be used. Any files not in the cache will
|
|
|
|
/// error. This is the equivalent of `--cached-only` in the CLI.
|
|
|
|
Only,
|
|
|
|
/// No cached source files should be used, and all files should be reloaded.
|
|
|
|
/// This is the equivalent of `--reload` in the CLI.
|
|
|
|
ReloadAll,
|
|
|
|
/// Only some cached resources should be used. This is the equivalent of
|
|
|
|
/// `--reload=https://deno.land/std` or
|
|
|
|
/// `--reload=https://deno.land/std,https://deno.land/x/example`.
|
|
|
|
ReloadSome(Vec<String>),
|
|
|
|
/// The cached source files should be used for local modules. This is the
|
|
|
|
/// default behavior of the CLI.
|
|
|
|
Use,
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
impl CacheSetting {
|
|
|
|
/// Returns if the cache should be used for a given specifier.
|
|
|
|
pub fn should_use(&self, specifier: &ModuleSpecifier) -> bool {
|
|
|
|
match self {
|
|
|
|
CacheSetting::ReloadAll => false,
|
|
|
|
CacheSetting::Use | CacheSetting::Only => true,
|
|
|
|
CacheSetting::ReloadSome(list) => {
|
2021-02-17 18:47:18 +00:00
|
|
|
let mut url = specifier.clone();
|
2020-11-06 00:38:21 +00:00
|
|
|
url.set_fragment(None);
|
|
|
|
if list.contains(&url.as_str().to_string()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
url.set_query(None);
|
|
|
|
let mut path = PathBuf::from(url.as_str());
|
|
|
|
loop {
|
|
|
|
if list.contains(&path.to_str().unwrap().to_string()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if !path.pop() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Fetch a source file from the local file system.
|
|
|
|
fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> {
|
2021-02-17 18:47:18 +00:00
|
|
|
let local = specifier.to_file_path().map_err(|_| {
|
2020-11-06 00:38:21 +00:00
|
|
|
uri_error(format!("Invalid file path.\n Specifier: {}", specifier))
|
|
|
|
})?;
|
|
|
|
let bytes = fs::read(local.clone())?;
|
|
|
|
let charset = text_encoding::detect_charset(&bytes).to_string();
|
|
|
|
let source = strip_shebang(get_source_from_bytes(bytes, Some(charset))?);
|
|
|
|
let media_type = MediaType::from(specifier);
|
|
|
|
|
|
|
|
Ok(File {
|
|
|
|
local,
|
|
|
|
maybe_types: None,
|
|
|
|
media_type,
|
|
|
|
source,
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
})
|
|
|
|
}
|
2019-11-22 17:46:57 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Given a vector of bytes and optionally a charset, decode the bytes to a
|
|
|
|
/// string.
|
2020-12-07 10:46:39 +00:00
|
|
|
pub fn get_source_from_bytes(
|
2020-11-06 00:38:21 +00: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 17:46:57 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
Ok(source)
|
|
|
|
}
|
2020-04-16 03:14:28 +00:00
|
|
|
|
2021-01-06 02:22:38 +00:00
|
|
|
fn get_source_from_data_url(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<(String, MediaType, String), AnyError> {
|
2021-02-17 18:47:18 +00:00
|
|
|
if specifier.scheme() != "data" {
|
2021-01-06 02:22:38 +00:00
|
|
|
return Err(custom_error(
|
|
|
|
"BadScheme",
|
2021-02-17 18:47:18 +00:00
|
|
|
format!("Unexpected scheme of \"{}\"", specifier.scheme()),
|
2021-01-06 02:22:38 +00:00
|
|
|
));
|
|
|
|
}
|
2021-02-17 18:47:18 +00:00
|
|
|
let path = specifier.path();
|
2021-01-06 02:22:38 +00:00
|
|
|
let mut parts = path.splitn(2, ',');
|
|
|
|
let media_type_part =
|
|
|
|
percent_encoding::percent_decode_str(parts.next().unwrap())
|
|
|
|
.decode_utf8()?;
|
|
|
|
let data_part = if let Some(data) = parts.next() {
|
|
|
|
data
|
|
|
|
} else {
|
|
|
|
return Err(custom_error(
|
|
|
|
"BadUrl",
|
|
|
|
"The data URL is badly formed, missing a comma.",
|
|
|
|
));
|
|
|
|
};
|
|
|
|
let (media_type, maybe_charset) =
|
|
|
|
map_content_type(specifier, Some(media_type_part.to_string()));
|
|
|
|
let is_base64 = media_type_part.rsplit(';').any(|p| p == "base64");
|
|
|
|
let bytes = if is_base64 {
|
|
|
|
base64::decode(data_part)?
|
|
|
|
} else {
|
|
|
|
percent_encoding::percent_decode_str(data_part).collect()
|
|
|
|
};
|
|
|
|
let source = strip_shebang(get_source_from_bytes(bytes, maybe_charset)?);
|
|
|
|
Ok((source, media_type, media_type_part.to_string()))
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Return a validated scheme for a given module specifier.
|
|
|
|
fn get_validated_scheme(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<String, AnyError> {
|
2021-02-17 18:47:18 +00:00
|
|
|
let scheme = specifier.scheme();
|
2020-11-06 00:38:21 +00:00
|
|
|
if !SUPPORTED_SCHEMES.contains(&scheme) {
|
|
|
|
Err(generic_error(format!(
|
|
|
|
"Unsupported scheme \"{}\" for module \"{}\". Supported schemes: {:#?}",
|
|
|
|
scheme, specifier, SUPPORTED_SCHEMES
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(scheme.to_string())
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Resolve a media type and optionally the charset from a module specifier and
|
|
|
|
/// the value of a content type header.
|
2020-12-07 10:46:39 +00:00
|
|
|
pub fn map_content_type(
|
2020-11-06 00:38:21 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
maybe_content_type: Option<String>,
|
|
|
|
) -> (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();
|
|
|
|
let media_type = match content_type.trim().to_lowercase().as_ref() {
|
|
|
|
"application/typescript"
|
|
|
|
| "text/typescript"
|
|
|
|
| "video/vnd.dlna.mpeg-tts"
|
|
|
|
| "video/mp2t"
|
|
|
|
| "application/x-typescript" => {
|
|
|
|
map_js_like_extension(specifier, MediaType::TypeScript)
|
|
|
|
}
|
|
|
|
"application/javascript"
|
|
|
|
| "text/javascript"
|
|
|
|
| "application/ecmascript"
|
|
|
|
| "text/ecmascript"
|
|
|
|
| "application/x-javascript"
|
|
|
|
| "application/node" => {
|
|
|
|
map_js_like_extension(specifier, MediaType::JavaScript)
|
|
|
|
}
|
2021-03-25 18:17:37 +00:00
|
|
|
"text/jsx" => MediaType::Jsx,
|
|
|
|
"text/tsx" => MediaType::Tsx,
|
2020-11-06 00:38:21 +00:00
|
|
|
"application/json" | "text/json" => MediaType::Json,
|
|
|
|
"application/wasm" => MediaType::Wasm,
|
|
|
|
// Handle plain and possibly webassembly
|
|
|
|
"text/plain" | "application/octet-stream" => MediaType::from(specifier),
|
|
|
|
_ => {
|
|
|
|
debug!("unknown content type: {}", content_type);
|
|
|
|
MediaType::Unknown
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let charset = content_types
|
|
|
|
.map(str::trim)
|
|
|
|
.find_map(|s| s.strip_prefix("charset="))
|
|
|
|
.map(String::from);
|
2020-02-28 14:17:56 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
(media_type, charset)
|
|
|
|
} else {
|
|
|
|
(MediaType::from(specifier), None)
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Used to augment media types by using the path part of a module specifier to
|
|
|
|
/// resolve to a more accurate media type.
|
|
|
|
fn map_js_like_extension(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
default: MediaType,
|
|
|
|
) -> MediaType {
|
2021-02-17 18:47:18 +00:00
|
|
|
let path = if specifier.scheme() == "file" {
|
|
|
|
if let Ok(path) = specifier.to_file_path() {
|
2020-11-06 00:38:21 +00:00
|
|
|
path
|
|
|
|
} else {
|
2021-02-17 18:47:18 +00:00
|
|
|
PathBuf::from(specifier.path())
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
} else {
|
2021-02-17 18:47:18 +00:00
|
|
|
PathBuf::from(specifier.path())
|
2020-11-06 00:38:21 +00:00
|
|
|
};
|
|
|
|
match path.extension() {
|
|
|
|
None => default,
|
|
|
|
Some(os_str) => match os_str.to_str() {
|
|
|
|
None => default,
|
2021-03-25 18:17:37 +00:00
|
|
|
Some("jsx") => MediaType::Jsx,
|
|
|
|
Some("tsx") => MediaType::Tsx,
|
2020-11-06 00:38:21 +00:00
|
|
|
// Because DTS files do not have a separate media type, or a unique
|
|
|
|
// extension, we have to "guess" at those things that we consider that
|
|
|
|
// look like TypeScript, and end with `.d.ts` are DTS files.
|
|
|
|
Some("ts") => {
|
|
|
|
if default == MediaType::TypeScript {
|
|
|
|
match path.file_stem() {
|
|
|
|
None => default,
|
|
|
|
Some(os_str) => {
|
|
|
|
if let Some(file_stem) = os_str.to_str() {
|
|
|
|
if file_stem.ends_with(".d") {
|
|
|
|
MediaType::Dts
|
|
|
|
} else {
|
|
|
|
default
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
default
|
|
|
|
}
|
|
|
|
}
|
2020-02-23 19:51:29 +00:00
|
|
|
}
|
2020-02-19 22:51:10 +00:00
|
|
|
} else {
|
2020-11-06 00:38:21 +00:00
|
|
|
default
|
|
|
|
}
|
2020-02-19 22:51:10 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
Some(_) => default,
|
|
|
|
},
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Remove shebangs from the start of source code strings
|
2020-11-12 22:17:31 +00:00
|
|
|
fn strip_shebang(mut value: String) -> String {
|
2020-11-06 00:38:21 +00:00
|
|
|
if value.starts_with("#!") {
|
2020-11-12 22:17:31 +00:00
|
|
|
if let Some(mid) = value.find('\n') {
|
2020-11-06 00:38:21 +00:00
|
|
|
let (_, rest) = value.split_at(mid);
|
2020-11-12 22:17:31 +00:00
|
|
|
value = rest.to_string()
|
2020-11-06 00:38:21 +00:00
|
|
|
} else {
|
2020-11-12 22:17:31 +00:00
|
|
|
value.clear()
|
|
|
|
}
|
2020-04-16 03:14:28 +00:00
|
|
|
}
|
2020-11-12 22:17:31 +00:00
|
|
|
value
|
2020-11-06 00:38:21 +00:00
|
|
|
}
|
2020-04-16 03:14:28 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// A structure for resolving, fetching and caching source files.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct FileFetcher {
|
2021-02-16 02:50:27 +00:00
|
|
|
auth_tokens: AuthTokens,
|
2020-11-06 00:38:21 +00:00
|
|
|
allow_remote: bool,
|
|
|
|
cache: FileCache,
|
|
|
|
cache_setting: CacheSetting,
|
|
|
|
http_cache: HttpCache,
|
|
|
|
http_client: reqwest::Client,
|
|
|
|
}
|
2019-12-03 22:48:53 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
impl FileFetcher {
|
|
|
|
pub fn new(
|
|
|
|
http_cache: HttpCache,
|
|
|
|
cache_setting: CacheSetting,
|
|
|
|
allow_remote: bool,
|
2021-01-04 23:15:52 +00:00
|
|
|
ca_data: Option<Vec<u8>>,
|
2020-11-06 00:38:21 +00:00
|
|
|
) -> Result<Self, AnyError> {
|
|
|
|
Ok(Self {
|
2021-02-16 02:50:27 +00:00
|
|
|
auth_tokens: AuthTokens::new(env::var(DENO_AUTH_TOKENS).ok()),
|
2020-11-06 00:38:21 +00:00
|
|
|
allow_remote,
|
2021-02-16 02:50:27 +00:00
|
|
|
cache: Default::default(),
|
2020-11-06 00:38:21 +00:00
|
|
|
cache_setting,
|
|
|
|
http_cache,
|
2021-01-04 23:15:52 +00:00
|
|
|
http_client: create_http_client(get_user_agent(), ca_data)?,
|
2020-11-06 00:38:21 +00:00
|
|
|
})
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Creates a `File` structure for a remote file.
|
|
|
|
fn build_remote_file(
|
2020-05-11 11:13:27 +00:00
|
|
|
&self,
|
2020-11-06 00:38:21 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
headers: &HashMap<String, String>,
|
|
|
|
) -> Result<File, AnyError> {
|
2021-02-17 18:47:18 +00:00
|
|
|
let local =
|
|
|
|
self
|
|
|
|
.http_cache
|
|
|
|
.get_cache_filename(specifier)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
generic_error("Cannot convert specifier to cached filename.")
|
|
|
|
})?;
|
2020-11-06 00:38:21 +00:00
|
|
|
let maybe_content_type = headers.get("content-type").cloned();
|
|
|
|
let (media_type, maybe_charset) =
|
|
|
|
map_content_type(specifier, maybe_content_type);
|
|
|
|
let source = strip_shebang(get_source_from_bytes(bytes, maybe_charset)?);
|
|
|
|
let maybe_types = headers.get("x-typescript-types").cloned();
|
|
|
|
|
|
|
|
Ok(File {
|
|
|
|
local,
|
|
|
|
maybe_types,
|
2019-07-31 11:58:41 +00:00
|
|
|
media_type,
|
2020-11-06 00:38:21 +00:00
|
|
|
source,
|
|
|
|
specifier: specifier.clone(),
|
2019-07-31 11:58:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch cached remote file.
|
|
|
|
///
|
|
|
|
/// This is a recursive operation if source file has redirections.
|
2020-11-06 00:38:21 +00:00
|
|
|
fn fetch_cached(
|
2020-01-04 10:20:52 +00:00
|
|
|
&self,
|
2020-11-06 00:38:21 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
2020-06-15 21:46:48 +00:00
|
|
|
redirect_limit: i64,
|
2020-11-06 00:38:21 +00:00
|
|
|
) -> Result<Option<File>, AnyError> {
|
|
|
|
debug!("FileFetcher::fetch_cached - specifier: {}", specifier);
|
2020-06-15 21:46:48 +00:00
|
|
|
if redirect_limit < 0 {
|
2020-11-06 00:38:21 +00:00
|
|
|
return Err(custom_error("Http", "Too many redirects."));
|
2020-06-15 21:46:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 18:47:18 +00:00
|
|
|
let (mut source_file, headers) = match self.http_cache.get(specifier) {
|
|
|
|
Err(err) => {
|
|
|
|
if let Some(err) = err.downcast_ref::<std::io::Error>() {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
return Ok(None);
|
2020-02-19 13:17:13 +00:00
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
2021-02-17 18:47:18 +00:00
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(cache) => cache,
|
|
|
|
};
|
2020-11-06 00:38:21 +00:00
|
|
|
if let Some(redirect_to) = headers.get("location") {
|
|
|
|
let redirect =
|
2021-02-17 18:47:18 +00:00
|
|
|
deno_core::resolve_import(redirect_to, specifier.as_str())?;
|
2020-11-06 00:38:21 +00:00
|
|
|
return self.fetch_cached(&redirect, redirect_limit - 1);
|
2020-02-19 13:17:13 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
let mut bytes = Vec::new();
|
|
|
|
source_file.read_to_end(&mut bytes)?;
|
|
|
|
let file = self.build_remote_file(specifier, bytes, &headers)?;
|
2020-02-19 13:17:13 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
Ok(Some(file))
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 02:22:38 +00: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);
|
|
|
|
match self.fetch_cached(specifier, 0) {
|
|
|
|
Ok(Some(file)) => return Ok(file),
|
|
|
|
Ok(None) => {}
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.cache_setting == CacheSetting::Only {
|
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!(
|
|
|
|
"Specifier not found in cache: \"{}\", --cached-only is specified.",
|
|
|
|
specifier
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let (source, media_type, content_type) =
|
|
|
|
get_source_from_data_url(specifier)?;
|
2021-02-17 18:47:18 +00:00
|
|
|
let local =
|
|
|
|
self
|
|
|
|
.http_cache
|
|
|
|
.get_cache_filename(specifier)
|
|
|
|
.ok_or_else(|| {
|
|
|
|
generic_error("Cannot convert specifier to cached filename.")
|
|
|
|
})?;
|
2021-01-06 02:22:38 +00:00
|
|
|
let mut headers = HashMap::new();
|
|
|
|
headers.insert("content-type".to_string(), content_type);
|
2021-02-17 18:47:18 +00:00
|
|
|
self.http_cache.set(specifier, headers, source.as_bytes())?;
|
2021-01-06 02:22:38 +00:00
|
|
|
|
|
|
|
Ok(File {
|
|
|
|
local,
|
|
|
|
maybe_types: None,
|
|
|
|
media_type,
|
|
|
|
source,
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Asynchronously fetch remote source file specified by the URL following
|
|
|
|
/// redirects.
|
2020-02-19 22:51:10 +00:00
|
|
|
///
|
2020-11-06 00:38:21 +00: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 10:20:52 +00:00
|
|
|
&self,
|
2020-11-06 00:38:21 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
2020-05-11 11:13:27 +00:00
|
|
|
permissions: &Permissions,
|
2020-11-06 00:38:21 +00:00
|
|
|
redirect_limit: i64,
|
2020-12-30 04:17:17 +00:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<File, AnyError>> + Send>> {
|
2020-11-06 00:38:21 +00:00
|
|
|
debug!("FileFetcher::fetch_remote() - specifier: {}", specifier);
|
2019-07-31 11:58:41 +00:00
|
|
|
if redirect_limit < 0 {
|
2020-11-06 00:38:21 +00:00
|
|
|
return futures::future::err(custom_error("Http", "Too many redirects."))
|
2020-12-30 04:17:17 +00:00
|
|
|
.boxed();
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
if let Err(err) = permissions.check_specifier(specifier) {
|
2020-12-30 04:17:17 +00:00
|
|
|
return futures::future::err(err).boxed();
|
2020-05-11 11:13:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
if self.cache_setting.should_use(specifier) {
|
|
|
|
match self.fetch_cached(specifier, redirect_limit) {
|
|
|
|
Ok(Some(file)) => {
|
2020-12-30 04:17:17 +00:00
|
|
|
return futures::future::ok(file).boxed();
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
Ok(None) => {}
|
2019-07-31 11:58:41 +00:00
|
|
|
Err(err) => {
|
2020-12-30 04:17:17 +00:00
|
|
|
return futures::future::err(err).boxed();
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
if self.cache_setting == CacheSetting::Only {
|
|
|
|
return futures::future::err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!(
|
|
|
|
"Specifier not found in cache: \"{}\", --cached-only is specified.",
|
|
|
|
specifier
|
|
|
|
),
|
|
|
|
))
|
2020-12-30 04:17:17 +00:00
|
|
|
.boxed();
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
info!("{} {}", colors::green("Download"), specifier);
|
2020-03-10 12:26:17 +00:00
|
|
|
|
2021-02-17 18:47:18 +00:00
|
|
|
let maybe_etag = match self.http_cache.get(specifier) {
|
2020-11-06 00:38:21 +00:00
|
|
|
Ok((_, headers)) => headers.get("etag").cloned(),
|
|
|
|
_ => None,
|
2020-02-19 13:17:13 +00:00
|
|
|
};
|
2021-02-16 02:50:27 +00:00
|
|
|
let maybe_auth_token = self.auth_tokens.get(&specifier);
|
2020-11-06 00:38:21 +00:00
|
|
|
let specifier = specifier.clone();
|
2020-05-11 11:13:27 +00:00
|
|
|
let permissions = permissions.clone();
|
2021-02-16 02:50:27 +00:00
|
|
|
let client = self.http_client.clone();
|
|
|
|
let file_fetcher = self.clone();
|
2020-11-06 00:38:21 +00:00
|
|
|
// A single pass of fetch either yields code or yields a redirect.
|
|
|
|
async move {
|
2021-02-16 02:50:27 +00:00
|
|
|
match fetch_once(FetchOnceArgs {
|
|
|
|
client,
|
2021-02-17 18:47:18 +00:00
|
|
|
url: specifier.clone(),
|
2021-02-16 02:50:27 +00:00
|
|
|
maybe_etag,
|
|
|
|
maybe_auth_token,
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
2020-01-11 10:11:05 +00:00
|
|
|
FetchOnceResult::NotModified => {
|
2020-11-06 00:38:21 +00:00
|
|
|
let file = file_fetcher.fetch_cached(&specifier, 10)?.unwrap();
|
|
|
|
Ok(file)
|
2020-01-11 10:11:05 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
FetchOnceResult::Redirect(redirect_url, headers) => {
|
2021-02-17 18:47:18 +00:00
|
|
|
file_fetcher.http_cache.set(&specifier, headers, &[])?;
|
2020-11-06 00:38:21 +00:00
|
|
|
file_fetcher
|
2021-02-17 18:47:18 +00:00
|
|
|
.fetch_remote(&redirect_url, &permissions, redirect_limit - 1)
|
2020-01-04 10:20:52 +00:00
|
|
|
.await
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
FetchOnceResult::Code(bytes, headers) => {
|
2021-02-17 18:47:18 +00:00
|
|
|
file_fetcher
|
|
|
|
.http_cache
|
|
|
|
.set(&specifier, headers.clone(), &bytes)?;
|
2020-11-06 00:38:21 +00:00
|
|
|
let file =
|
|
|
|
file_fetcher.build_remote_file(&specifier, bytes, &headers)?;
|
|
|
|
Ok(file)
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
2019-04-25 17:29:21 +00:00
|
|
|
}
|
2020-11-06 00:38:21 +00:00
|
|
|
}
|
2020-12-30 04:17:17 +00:00
|
|
|
.boxed()
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Fetch a source file and asynchronously return it.
|
|
|
|
pub async fn fetch(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
permissions: &Permissions,
|
|
|
|
) -> Result<File, AnyError> {
|
|
|
|
debug!("FileFetcher::fetch() - specifier: {}", specifier);
|
|
|
|
let scheme = get_validated_scheme(specifier)?;
|
|
|
|
permissions.check_specifier(specifier)?;
|
|
|
|
if let Some(file) = self.cache.get(specifier) {
|
|
|
|
Ok(file)
|
2021-01-06 02:22:38 +00: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" {
|
|
|
|
let result = self.fetch_data_url(specifier);
|
|
|
|
if let Ok(file) = &result {
|
|
|
|
self.cache.insert(specifier.clone(), file.clone());
|
|
|
|
}
|
|
|
|
result
|
|
|
|
} else if !self.allow_remote {
|
|
|
|
Err(custom_error(
|
|
|
|
"NoRemote",
|
|
|
|
format!("A remote specifier was requested: \"{}\", but --no-remote is specified.", specifier),
|
|
|
|
))
|
2020-11-06 00:38:21 +00:00
|
|
|
} else {
|
2021-01-06 02:22:38 +00:00
|
|
|
let result = self.fetch_remote(specifier, permissions, 10).await;
|
|
|
|
if let Ok(file) = &result {
|
|
|
|
self.cache.insert(specifier.clone(), file.clone());
|
2020-11-06 00:38:21 +00:00
|
|
|
}
|
2021-01-06 02:22:38 +00:00
|
|
|
result
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
/// Get the location of the current HTTP cache associated with the fetcher.
|
|
|
|
pub fn get_http_cache_location(&self) -> PathBuf {
|
|
|
|
self.http_cache.location.clone()
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 02:19:31 +00: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 18:47:18 +00:00
|
|
|
let is_local = specifier.scheme() == "file";
|
2020-11-16 02:19:31 +00:00
|
|
|
if is_local {
|
|
|
|
if let Ok(file) = fetch_local(specifier) {
|
|
|
|
return Some(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
maybe_file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00: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 14:29:06 +00:00
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-11-06 00:38:21 +00:00
|
|
|
use deno_core::error::get_custom_error_class;
|
2021-02-17 18:47:18 +00:00
|
|
|
use deno_core::resolve_url;
|
|
|
|
use deno_core::resolve_url_or_path;
|
2020-11-06 00:38:21 +00:00
|
|
|
use std::rc::Rc;
|
2019-07-31 11:58:41 +00:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
fn setup(
|
|
|
|
cache_setting: CacheSetting,
|
|
|
|
maybe_temp_dir: Option<Rc<TempDir>>,
|
|
|
|
) -> (FileFetcher, Rc<TempDir>) {
|
|
|
|
let temp_dir = maybe_temp_dir.unwrap_or_else(|| {
|
|
|
|
Rc::new(TempDir::new().expect("failed to create temp directory"))
|
|
|
|
});
|
|
|
|
let location = temp_dir.path().join("deps");
|
|
|
|
let file_fetcher =
|
|
|
|
FileFetcher::new(HttpCache::new(&location), cache_setting, true, None)
|
|
|
|
.expect("setup failed");
|
|
|
|
(file_fetcher, temp_dir)
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! file_url {
|
|
|
|
($path:expr) => {
|
|
|
|
if cfg!(target_os = "windows") {
|
|
|
|
concat!("file:///C:", $path)
|
|
|
|
} else {
|
|
|
|
concat!("file://", $path)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch(specifier: &ModuleSpecifier) -> (File, FileFetcher) {
|
|
|
|
let (file_fetcher, _) = setup(CacheSetting::ReloadAll, None);
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(specifier, &Permissions::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
(result.unwrap(), file_fetcher)
|
|
|
|
}
|
2019-10-17 14:29:06 +00:00
|
|
|
|
2020-11-06 00:38:21 +00: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
|
|
|
|
.fetch_remote(specifier, &Permissions::allow_all(), 1)
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2021-02-17 18:47:18 +00:00
|
|
|
let (_, headers) = file_fetcher.http_cache.get(specifier).unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
(result.unwrap(), headers)
|
|
|
|
}
|
2019-10-17 14:29:06 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_remote_encoded(
|
|
|
|
fixture: &str,
|
|
|
|
charset: &str,
|
|
|
|
expected: &str,
|
|
|
|
) {
|
|
|
|
let url_str =
|
|
|
|
format!("http://127.0.0.1:4545/cli/tests/encoding/{}", fixture);
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url(&url_str).unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let (file, headers) = test_fetch_remote(&specifier).await;
|
|
|
|
assert_eq!(file.source, expected);
|
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("content-type").unwrap(),
|
|
|
|
&format!("application/typescript;charset={}", charset)
|
|
|
|
);
|
|
|
|
}
|
2019-10-17 14:29:06 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_local_encoded(charset: &str, expected: String) {
|
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join(format!("tests/encoding/{}.ts", charset));
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url_or_path(p.to_str().unwrap()).unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let (file, _) = test_fetch(&specifier).await;
|
|
|
|
assert_eq!(file.source, expected);
|
|
|
|
}
|
2019-10-17 14:29:06 +00:00
|
|
|
|
2021-01-06 02:22:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_get_source_from_data_url() {
|
|
|
|
let fixtures = vec![
|
|
|
|
("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=", true, MediaType::TypeScript, "application/typescript;base64", "export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n"),
|
|
|
|
("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=?a=b&b=c", true, MediaType::TypeScript, "application/typescript;base64", "export const a = \"a\";\n\nexport enum A {\n A,\n B,\n C,\n}\n"),
|
|
|
|
("data:text/plain,Hello%2C%20Deno!", true, MediaType::Unknown, "text/plain", "Hello, Deno!"),
|
|
|
|
("data:,Hello%2C%20Deno!", true, MediaType::Unknown, "", "Hello, Deno!"),
|
|
|
|
("data:application/javascript,console.log(\"Hello, Deno!\");%0A", true, MediaType::JavaScript, "application/javascript", "console.log(\"Hello, Deno!\");\n"),
|
2021-03-25 18:17:37 +00:00
|
|
|
("data:text/jsx;base64,ZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24oKSB7CiAgcmV0dXJuIDxkaXY+SGVsbG8gRGVubyE8L2Rpdj4KfQo=", true, MediaType::Jsx, "text/jsx;base64", "export default function() {\n return <div>Hello Deno!</div>\n}\n"),
|
|
|
|
("data:text/tsx;base64,ZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24oKSB7CiAgcmV0dXJuIDxkaXY+SGVsbG8gRGVubyE8L2Rpdj4KfQo=", true, MediaType::Tsx, "text/tsx;base64", "export default function() {\n return <div>Hello Deno!</div>\n}\n"),
|
2021-01-06 02:22:38 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (
|
|
|
|
url_str,
|
|
|
|
expected_ok,
|
|
|
|
expected_media_type,
|
|
|
|
expected_media_type_str,
|
|
|
|
expected,
|
|
|
|
) in fixtures
|
|
|
|
{
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url(url_str).unwrap();
|
2021-01-06 02:22:38 +00:00
|
|
|
let actual = get_source_from_data_url(&specifier);
|
|
|
|
assert_eq!(actual.is_ok(), expected_ok);
|
|
|
|
if expected_ok {
|
|
|
|
let (actual, actual_media_type, actual_media_type_str) =
|
|
|
|
actual.unwrap();
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
assert_eq!(actual_media_type, expected_media_type);
|
|
|
|
assert_eq!(actual_media_type_str, expected_media_type_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:38:21 +00: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-06 02:22:38 +00:00
|
|
|
("data:,some%20text", true, "data"),
|
2020-11-06 00:38:21 +00:00
|
|
|
("ftp://a/b/c.ts", false, ""),
|
|
|
|
("mailto:dino@deno.land", false, ""),
|
|
|
|
];
|
2019-10-17 14:29:06 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
for (specifier, is_ok, expected) in fixtures {
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url_or_path(specifier).unwrap();
|
2020-11-06 00:38:21 +00: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 14:29:06 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_strip_shebang() {
|
|
|
|
let value =
|
|
|
|
"#!/usr/bin/env deno\n\nconsole.log(\"hello deno!\");\n".to_string();
|
|
|
|
assert_eq!(strip_shebang(value), "\n\nconsole.log(\"hello deno!\");\n");
|
2019-10-17 14:29:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 01:52:03 +00:00
|
|
|
#[test]
|
2020-11-06 00:38:21 +00:00
|
|
|
fn test_map_content_type() {
|
|
|
|
let fixtures = vec![
|
|
|
|
// Extension only
|
|
|
|
(file_url!("/foo/bar.ts"), None, MediaType::TypeScript, None),
|
2021-03-25 18:17:37 +00:00
|
|
|
(file_url!("/foo/bar.tsx"), None, MediaType::Tsx, None),
|
2020-11-06 00:38:21 +00:00
|
|
|
(file_url!("/foo/bar.d.ts"), None, MediaType::Dts, None),
|
|
|
|
(file_url!("/foo/bar.js"), None, MediaType::JavaScript, None),
|
2021-03-25 18:17:37 +00:00
|
|
|
(file_url!("/foo/bar.jsx"), None, MediaType::Jsx, None),
|
2020-11-06 00:38:21 +00:00
|
|
|
(file_url!("/foo/bar.json"), None, MediaType::Json, None),
|
|
|
|
(file_url!("/foo/bar.wasm"), None, MediaType::Wasm, None),
|
|
|
|
(file_url!("/foo/bar.cjs"), None, MediaType::JavaScript, None),
|
|
|
|
(file_url!("/foo/bar.mjs"), None, MediaType::JavaScript, None),
|
|
|
|
(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-06 02:22:38 +00:00
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/jsx".to_string()),
|
2021-03-25 18:17:37 +00:00
|
|
|
MediaType::Jsx,
|
2021-01-06 02:22:38 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod",
|
|
|
|
Some("text/tsx".to_string()),
|
2021-03-25 18:17:37 +00:00
|
|
|
MediaType::Tsx,
|
2021-01-06 02:22:38 +00:00
|
|
|
None,
|
|
|
|
),
|
2020-11-06 00:38:21 +00: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 18:17:37 +00:00
|
|
|
MediaType::Tsx,
|
2020-11-06 00:38:21 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.tsx",
|
|
|
|
Some("application/javascript".to_string()),
|
2021-03-25 18:17:37 +00:00
|
|
|
MediaType::Tsx,
|
2020-11-06 00:38:21 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.jsx",
|
|
|
|
Some("application/javascript".to_string()),
|
2021-03-25 18:17:37 +00:00
|
|
|
MediaType::Jsx,
|
2020-11-06 00:38:21 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.jsx",
|
|
|
|
Some("application/x-typescript".to_string()),
|
2021-03-25 18:17:37 +00:00
|
|
|
MediaType::Jsx,
|
2020-11-06 00:38:21 +00:00
|
|
|
None,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"https://deno.land/x/mod.d.ts",
|
|
|
|
Some("application/javascript".to_string()),
|
|
|
|
MediaType::JavaScript,
|
|
|
|
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 {
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url_or_path(specifier).unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
map_content_type(&specifier, maybe_content_type),
|
|
|
|
(media_type, maybe_charset)
|
|
|
|
);
|
2019-08-13 01:52:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_insert_cached() {
|
|
|
|
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
|
|
|
|
let local = temp_dir.path().join("a.ts");
|
|
|
|
let specifier =
|
2021-02-17 18:47:18 +00:00
|
|
|
resolve_url_or_path(local.as_os_str().to_str().unwrap()).unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let file = File {
|
|
|
|
local,
|
|
|
|
maybe_types: None,
|
|
|
|
media_type: MediaType::TypeScript,
|
|
|
|
source: "some source code".to_string(),
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
};
|
|
|
|
file_fetcher.insert_cached(file.clone());
|
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-06 00:38:21 +00:00
|
|
|
let result_file = result.unwrap();
|
|
|
|
assert_eq!(result_file, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-16 02:19:31 +00:00
|
|
|
async fn test_get_source() {
|
2020-11-06 00:38:21 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url(
|
2020-11-06 00:38:21 +00:00
|
|
|
"http://localhost:4548/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-16 02:19:31 +00:00
|
|
|
let maybe_file = file_fetcher.get_source(&specifier);
|
2020-11-06 00:38:21 +00:00
|
|
|
assert!(maybe_file.is_some());
|
|
|
|
let file = maybe_file.unwrap();
|
|
|
|
assert_eq!(file.source, "export const redirect = 1;\n");
|
|
|
|
assert_eq!(
|
|
|
|
file.specifier,
|
2021-02-17 18:47:18 +00:00
|
|
|
resolve_url(
|
2020-11-06 00:38:21 +00:00
|
|
|
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js"
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_http_cache_location() {
|
|
|
|
let (file_fetcher, temp_dir) = setup(CacheSetting::Use, None);
|
|
|
|
let expected = temp_dir.path().join("deps");
|
|
|
|
let actual = file_fetcher.get_http_cache_location();
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
2021-01-06 02:22:38 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_data_url() {
|
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url("data:application/typescript;base64,ZXhwb3J0IGNvbnN0IGEgPSAiYSI7CgpleHBvcnQgZW51bSBBIHsKICBBLAogIEIsCiAgQywKfQo=").unwrap();
|
2021-01-06 02:22:38 +00:00
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
file.source,
|
|
|
|
"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-06 00:38:21 +00: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 18:47:18 +00:00
|
|
|
let specifier =
|
|
|
|
resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
file.source,
|
2020-10-07 11:43:44 +00:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-03 13:53:50 +00:00
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
2020-02-28 18:15:31 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let cache_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2020-02-28 18:15:31 +00:00
|
|
|
let mut metadata =
|
|
|
|
crate::http_cache::Metadata::read(&cache_filename).unwrap();
|
|
|
|
metadata.headers = HashMap::new();
|
|
|
|
metadata
|
|
|
|
.headers
|
2020-09-16 11:34:16 +00:00
|
|
|
.insert("content-type".to_string(), "text/javascript".to_string());
|
2020-02-28 18:15:31 +00:00
|
|
|
metadata.write(&cache_filename).unwrap();
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher_01
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
2020-11-06 00:38:21 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(
|
2020-11-06 00:38:21 +00:00
|
|
|
file.source,
|
2020-10-07 11:43:44 +00:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-03 13:53:50 +00:00
|
|
|
);
|
2020-11-06 00:38:21 +00: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 13:17:13 +00:00
|
|
|
|
2021-02-17 18:47:18 +00:00
|
|
|
let (_, headers) = file_fetcher_02.http_cache.get(&specifier).unwrap();
|
2020-09-16 11:34:16 +00:00
|
|
|
assert_eq!(headers.get("content-type").unwrap(), "text/javascript");
|
2020-02-28 18:15:31 +00:00
|
|
|
metadata.headers = HashMap::new();
|
|
|
|
metadata
|
|
|
|
.headers
|
2020-09-16 11:34:16 +00:00
|
|
|
.insert("content-type".to_string(), "application/json".to_string());
|
2020-02-28 18:15:31 +00:00
|
|
|
metadata.write(&cache_filename).unwrap();
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher_02
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
2020-11-06 00:38:21 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(
|
2020-11-06 00:38:21 +00:00
|
|
|
file.source,
|
2020-10-07 11:43:44 +00:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-03 13:53:50 +00:00
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
assert_eq!(file.media_type, MediaType::Json);
|
|
|
|
|
|
|
|
// This creates a totally new instance, simulating another Deno process
|
|
|
|
// invocation and indicates to "cache bust".
|
|
|
|
let location = temp_dir.path().join("deps");
|
|
|
|
let file_fetcher = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::ReloadAll,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("setup failed");
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-06 00:38:21 +00:00
|
|
|
let file = result.unwrap();
|
2020-02-19 13:17:13 +00:00
|
|
|
assert_eq!(
|
2020-11-06 00:38:21 +00:00
|
|
|
file.source,
|
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
2020-02-19 13:17:13 +00:00
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
assert_eq!(file.media_type, MediaType::TypeScript);
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_uses_cache() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let temp_dir = TempDir::new()
|
|
|
|
.expect("could not create temp dir")
|
|
|
|
.into_path();
|
|
|
|
let location = temp_dir.join("deps");
|
|
|
|
let file_fetcher_01 = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("could not create file fetcher");
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4545/cli/tests/subdir/mismatch_ext.ts")
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let cache_filename = file_fetcher_01
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher_01
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-05-11 11:13:27 +00:00
|
|
|
.await;
|
2020-11-06 00:38:21 +00:00
|
|
|
assert!(result.is_ok());
|
2019-11-22 17:46:57 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let metadata_filename =
|
2020-02-28 18:15:31 +00:00
|
|
|
crate::http_cache::Metadata::filename(&cache_filename);
|
2020-11-06 00:38:21 +00:00
|
|
|
let metadata_file =
|
|
|
|
fs::File::open(metadata_filename).expect("could not open metadata file");
|
|
|
|
let metadata_file_metadata = metadata_file.metadata().unwrap();
|
|
|
|
let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap();
|
|
|
|
|
|
|
|
let file_fetcher_02 = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("could not create file fetcher");
|
|
|
|
let result = file_fetcher_02
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-05-11 11:13:27 +00:00
|
|
|
.await;
|
2019-11-22 17:46:57 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let metadata_filename =
|
|
|
|
crate::http_cache::Metadata::filename(&cache_filename);
|
|
|
|
let metadata_file =
|
|
|
|
fs::File::open(metadata_filename).expect("could not open metadata file");
|
|
|
|
let metadata_file_metadata = metadata_file.metadata().unwrap();
|
|
|
|
let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(metadata_file_modified_01, metadata_file_modified_02);
|
|
|
|
// because we converted to a "fixed" directory, we need to cleanup after
|
|
|
|
// ourselves.
|
|
|
|
let _ = fs::remove_dir_all(temp_dir);
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_redirected() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url(
|
2020-02-02 21:55:22 +00:00
|
|
|
"http://localhost:4546/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let cached_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2021-02-17 18:47:18 +00:00
|
|
|
let redirected_specifier = resolve_url(
|
2020-02-02 21:55:22 +00:00
|
|
|
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let redirected_cached_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&redirected_specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-06 00:38:21 +00:00
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.specifier, redirected_specifier);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(cached_filename).unwrap(),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
let (_, headers) = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get(&specifier)
|
2020-11-06 00:38:21 +00:00
|
|
|
.expect("could not get file");
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(
|
2020-09-16 11:34:16 +00:00
|
|
|
headers.get("location").unwrap(),
|
2020-02-03 13:53:50 +00:00
|
|
|
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js"
|
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(
|
2020-11-06 00:38:21 +00:00
|
|
|
fs::read_to_string(redirected_cached_filename).unwrap(),
|
2020-02-03 13:53:50 +00:00
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
let (_, headers) = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get(&redirected_specifier)
|
2020-11-06 00:38:21 +00:00
|
|
|
.expect("could not get file");
|
2020-02-19 13:17:13 +00:00
|
|
|
assert!(headers.get("location").is_none());
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_multiple_redirects() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url(
|
2020-02-02 21:55:22 +00:00
|
|
|
"http://localhost:4548/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let cached_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2021-02-17 18:47:18 +00:00
|
|
|
let redirected_01_specifier = resolve_url(
|
2020-02-02 21:55:22 +00:00
|
|
|
"http://localhost:4546/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let redirected_01_cached_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&redirected_01_specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2021-02-17 18:47:18 +00:00
|
|
|
let redirected_02_specifier = resolve_url(
|
2020-02-02 21:55:22 +00:00
|
|
|
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let redirected_02_cached_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&redirected_02_specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-06 00:38:21 +00:00
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.specifier, redirected_02_specifier);
|
2020-02-03 13:53:50 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(cached_filename).unwrap(),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
let (_, headers) = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get(&specifier)
|
2020-11-06 00:38:21 +00:00
|
|
|
.expect("could not get file");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("location").unwrap(),
|
|
|
|
"http://localhost:4546/cli/tests/subdir/redirects/redirect1.js"
|
|
|
|
);
|
2020-02-19 13:17:13 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(redirected_01_cached_filename).unwrap(),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
let (_, headers) = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get(&redirected_01_specifier)
|
2020-11-06 00:38:21 +00:00
|
|
|
.expect("could not get file");
|
|
|
|
assert_eq!(
|
|
|
|
headers.get("location").unwrap(),
|
|
|
|
"http://localhost:4545/cli/tests/subdir/redirects/redirect1.js"
|
|
|
|
);
|
2020-02-03 13:53:50 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-11-06 00:38:21 +00:00
|
|
|
fs::read_to_string(redirected_02_cached_filename).unwrap(),
|
2020-02-03 13:53:50 +00:00
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
let (_, headers) = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get(&redirected_02_specifier)
|
2020-11-06 00:38:21 +00:00
|
|
|
.expect("could not get file");
|
2020-02-19 13:17:13 +00:00
|
|
|
assert!(headers.get("location").is_none());
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_uses_cache_with_redirects() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let temp_dir = TempDir::new()
|
|
|
|
.expect("could not create temp dir")
|
|
|
|
.into_path();
|
|
|
|
let location = temp_dir.join("deps");
|
|
|
|
let file_fetcher_01 = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("could not create file fetcher");
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4548/cli/tests/subdir/mismatch_ext.ts")
|
|
|
|
.unwrap();
|
|
|
|
let redirected_specifier =
|
|
|
|
resolve_url("http://localhost:4546/cli/tests/subdir/mismatch_ext.ts")
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let redirected_cache_filename = file_fetcher_01
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&redirected_specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher_01
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-06 00:38:21 +00:00
|
|
|
|
|
|
|
let metadata_filename =
|
|
|
|
crate::http_cache::Metadata::filename(&redirected_cache_filename);
|
|
|
|
let metadata_file =
|
|
|
|
fs::File::open(metadata_filename).expect("could not open metadata file");
|
|
|
|
let metadata_file_metadata = metadata_file.metadata().unwrap();
|
|
|
|
let metadata_file_modified_01 = metadata_file_metadata.modified().unwrap();
|
|
|
|
|
|
|
|
let file_fetcher_02 = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("could not create file fetcher");
|
|
|
|
let result = file_fetcher_02
|
|
|
|
.fetch(&redirected_specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let metadata_filename =
|
|
|
|
crate::http_cache::Metadata::filename(&redirected_cache_filename);
|
|
|
|
let metadata_file =
|
|
|
|
fs::File::open(metadata_filename).expect("could not open metadata file");
|
|
|
|
let metadata_file_metadata = metadata_file.metadata().unwrap();
|
|
|
|
let metadata_file_modified_02 = metadata_file_metadata.modified().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(metadata_file_modified_01, metadata_file_modified_02);
|
|
|
|
// because we converted to a "fixed" directory, we need to cleanup after
|
|
|
|
// ourselves.
|
|
|
|
let _ = fs::remove_dir_all(temp_dir);
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetcher_limits_redirects() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url(
|
2020-02-02 21:55:22 +00:00
|
|
|
"http://localhost:4548/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-10-06 19:03:30 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher
|
|
|
|
.fetch_remote(&specifier, &Permissions::allow_all(), 2)
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher
|
|
|
|
.fetch_remote(&specifier, &Permissions::allow_all(), 1)
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_err());
|
2020-06-15 21:46:48 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher.fetch_cached(&specifier, 2);
|
2020-06-15 21:46:48 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher.fetch_cached(&specifier, 1);
|
2020-06-15 21:46:48 +00:00
|
|
|
assert!(result.is_err());
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 14:04:18 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_same_host_redirect() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let (file_fetcher, _) = setup(CacheSetting::Use, None);
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier = resolve_url(
|
2020-02-28 14:04:18 +00:00
|
|
|
"http://localhost:4550/REDIRECT/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let cached_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2021-02-17 18:47:18 +00:00
|
|
|
let redirected_specifier = resolve_url(
|
2020-02-28 14:04:18 +00:00
|
|
|
"http://localhost:4550/cli/tests/subdir/redirects/redirect1.js",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let redirected_cached_filename = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get_cache_filename(&redirected_specifier)
|
2021-02-15 09:32:06 +00:00
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-28 14:04:18 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-11-06 00:38:21 +00:00
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.specifier, redirected_specifier);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
fs::read_to_string(cached_filename).unwrap(),
|
|
|
|
"",
|
|
|
|
"redirected files should have empty cached contents"
|
|
|
|
);
|
|
|
|
let (_, headers) = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get(&specifier)
|
2020-11-06 00:38:21 +00:00
|
|
|
.expect("could not get file");
|
2020-02-28 14:04:18 +00:00
|
|
|
assert_eq!(
|
2020-09-16 11:34:16 +00:00
|
|
|
headers.get("location").unwrap(),
|
2020-02-28 14:04:18 +00:00
|
|
|
"/cli/tests/subdir/redirects/redirect1.js"
|
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
|
2020-02-28 14:04:18 +00:00
|
|
|
assert_eq!(
|
2020-11-06 00:38:21 +00:00
|
|
|
fs::read_to_string(redirected_cached_filename).unwrap(),
|
2020-02-28 14:04:18 +00:00
|
|
|
"export const redirect = 1;\n"
|
|
|
|
);
|
2020-11-06 00:38:21 +00:00
|
|
|
let (_, headers) = file_fetcher
|
|
|
|
.http_cache
|
2021-02-17 18:47:18 +00:00
|
|
|
.get(&redirected_specifier)
|
2020-11-06 00:38:21 +00:00
|
|
|
.expect("could not get file");
|
2020-02-28 14:04:18 +00:00
|
|
|
assert!(headers.get("location").is_none());
|
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_no_remote() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let temp_dir = TempDir::new().expect("could not create temp dir");
|
|
|
|
let location = temp_dir.path().join("deps");
|
|
|
|
let file_fetcher = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::Use,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("could not create file fetcher");
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4545/cli/tests/002_hello.ts").unwrap();
|
2019-12-03 22:48:53 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_err());
|
2020-11-06 00:38:21 +00:00
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(get_custom_error_class(&err), Some("NoRemote"));
|
|
|
|
assert_eq!(err.to_string(), "A remote specifier was requested: \"http://localhost:4545/cli/tests/002_hello.ts\", but --no-remote is specified.");
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_cache_only() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2020-11-06 00:38:21 +00:00
|
|
|
let temp_dir = TempDir::new()
|
|
|
|
.expect("could not create temp dir")
|
|
|
|
.into_path();
|
|
|
|
let location = temp_dir.join("deps");
|
|
|
|
let file_fetcher_01 = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::Only,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("could not create file fetcher");
|
|
|
|
let file_fetcher_02 = FileFetcher::new(
|
|
|
|
HttpCache::new(&location),
|
|
|
|
CacheSetting::Use,
|
|
|
|
true,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("could not create file fetcher");
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier =
|
|
|
|
resolve_url("http://localhost:4545/cli/tests/002_hello.ts").unwrap();
|
2019-07-31 11:58:41 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher_01
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
2020-11-06 00:38:21 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
let err = result.unwrap_err();
|
|
|
|
assert_eq!(get_custom_error_class(&err), Some("NotFound"));
|
|
|
|
assert_eq!(err.to_string(), "Specifier not found in cache: \"http://localhost:4545/cli/tests/002_hello.ts\", --cached-only is specified.");
|
2020-06-24 18:12:04 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher_02
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-02-19 13:17:13 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
let result = file_fetcher_01
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
2020-02-03 13:53:50 +00:00
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
2020-08-03 21:39:48 +00:00
|
|
|
|
2020-11-06 00:38:21 +00:00
|
|
|
// because we converted to a "fixed" directory, we need to cleanup after
|
|
|
|
// ourselves.
|
|
|
|
let _ = fs::remove_dir_all(temp_dir);
|
2020-08-03 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 02:19:31 +00: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");
|
|
|
|
let specifier =
|
2021-02-17 18:47:18 +00:00
|
|
|
resolve_url_or_path(&fixture_path.to_string_lossy()).unwrap();
|
2020-11-16 02:19:31 +00:00
|
|
|
fs::write(fixture_path.clone(), r#"console.log("hello deno");"#)
|
|
|
|
.expect("could not write file");
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.source, r#"console.log("hello deno");"#);
|
|
|
|
|
|
|
|
fs::write(fixture_path, r#"console.log("goodbye deno");"#)
|
|
|
|
.expect("could not write file");
|
|
|
|
let result = file_fetcher
|
|
|
|
.fetch(&specifier, &Permissions::allow_all())
|
|
|
|
.await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
let file = result.unwrap();
|
|
|
|
assert_eq!(file.source, r#"console.log("goodbye deno");"#);
|
|
|
|
}
|
|
|
|
|
2020-08-03 21:39:48 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00: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 21:39:48 +00:00
|
|
|
)
|
2020-11-06 00:38:21 +00:00
|
|
|
.unwrap();
|
|
|
|
test_fetch_local_encoded("utf-16be", expected).await;
|
2020-08-03 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00: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 21:39:48 +00:00
|
|
|
)
|
2020-11-06 00:38:21 +00:00
|
|
|
.unwrap();
|
|
|
|
test_fetch_local_encoded("utf-16le", expected).await;
|
2020-08-03 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00: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 21:39:48 +00:00
|
|
|
)
|
2020-11-06 00:38:21 +00:00
|
|
|
.unwrap();
|
|
|
|
test_fetch_local_encoded("utf-8", expected).await;
|
2020-01-11 10:11:05 +00:00
|
|
|
}
|
2020-01-26 18:59:41 +00:00
|
|
|
|
2020-02-03 13:53:50 +00:00
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_remote_with_types() {
|
2021-02-17 18:47:18 +00:00
|
|
|
let specifier =
|
|
|
|
resolve_url_or_path("http://127.0.0.1:4545/xTypeScriptTypes.js").unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
let (file, _) = test_fetch_remote(&specifier).await;
|
2020-02-03 13:53:50 +00:00
|
|
|
assert_eq!(
|
2020-11-06 00:38:21 +00:00
|
|
|
file.maybe_types,
|
2020-06-19 10:27:15 +00:00
|
|
|
Some("./xTypeScriptTypes.d.ts".to_string())
|
2020-02-03 13:53:50 +00:00
|
|
|
);
|
2020-01-26 18:59:41 +00:00
|
|
|
}
|
2020-08-03 21:39:48 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_remote_utf16_le() {
|
|
|
|
let expected =
|
2020-08-03 21:39:48 +00:00
|
|
|
std::str::from_utf8(b"\xEF\xBB\xBFconsole.log(\"Hello World\");\x0A")
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
test_fetch_remote_encoded("utf-16le.ts", "utf-16le", expected).await;
|
2020-08-03 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_remote_utf16_be() {
|
|
|
|
let expected =
|
2020-08-03 21:39:48 +00:00
|
|
|
std::str::from_utf8(b"\xEF\xBB\xBFconsole.log(\"Hello World\");\x0A")
|
|
|
|
.unwrap();
|
2020-11-06 00:38:21 +00:00
|
|
|
test_fetch_remote_encoded("utf-16be.ts", "utf-16be", expected).await;
|
2020-08-03 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-11-06 00:38:21 +00:00
|
|
|
async fn test_fetch_remote_window_1255() {
|
|
|
|
let expected = "console.log(\"\u{5E9}\u{5DC}\u{5D5}\u{5DD} \
|
2020-08-03 21:39:48 +00:00
|
|
|
\u{5E2}\u{5D5}\u{5DC}\u{5DD}\");\u{A}";
|
2020-11-06 00:38:21 +00:00
|
|
|
test_fetch_remote_encoded("windows-1255", "windows-1255", expected).await;
|
2020-08-03 21:39:48 +00:00
|
|
|
}
|
2019-07-31 11:58:41 +00:00
|
|
|
}
|