2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
use super::analysis;
|
2021-09-07 10:39:32 -04:00
|
|
|
use super::document_source::DocumentSource;
|
2021-01-22 05:03:16 -05:00
|
|
|
use super::text::LineIndex;
|
2021-06-04 17:31:44 -04:00
|
|
|
use super::tsc;
|
2021-07-25 01:33:42 -04:00
|
|
|
use super::urls::INVALID_SPECIFIER;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-06-21 17:18:32 -04:00
|
|
|
use crate::config_file::ConfigFile;
|
2020-12-07 05:46:39 -05:00
|
|
|
use crate::file_fetcher::get_source_from_bytes;
|
|
|
|
use crate::file_fetcher::map_content_type;
|
2020-12-09 19:12:46 -05:00
|
|
|
use crate::file_fetcher::SUPPORTED_SCHEMES;
|
2021-07-27 17:25:09 -04:00
|
|
|
use crate::flags::Flags;
|
2020-12-07 05:46:39 -05:00
|
|
|
use crate::http_cache;
|
|
|
|
use crate::http_cache::HttpCache;
|
2020-12-09 14:50:47 -05:00
|
|
|
use crate::import_map::ImportMap;
|
2020-12-29 23:17:17 -05:00
|
|
|
use crate::module_graph::GraphBuilder;
|
|
|
|
use crate::program_state::ProgramState;
|
|
|
|
use crate::specifier_handler::FetchHandler;
|
2020-12-07 05:46:39 -05:00
|
|
|
use crate::text_encoding;
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2021-06-04 17:31:44 -04:00
|
|
|
use deno_core::error::anyhow;
|
2020-12-29 23:17:17 -05:00
|
|
|
use deno_core::error::AnyError;
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2021-05-18 06:19:52 -04:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-12-07 05:46:39 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2020-12-29 23:17:17 -05:00
|
|
|
use std::sync::Arc;
|
2020-12-07 05:46:39 -05:00
|
|
|
use std::time::SystemTime;
|
2021-06-04 17:31:44 -04:00
|
|
|
use tsc::NavigationTree;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2020-12-29 23:17:17 -05:00
|
|
|
pub async fn cache(
|
2021-02-11 23:17:48 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
maybe_import_map: &Option<ImportMap>,
|
2021-06-21 17:18:32 -04:00
|
|
|
maybe_config_file: &Option<ConfigFile>,
|
2021-07-27 17:25:09 -04:00
|
|
|
maybe_cache_path: &Option<PathBuf>,
|
2020-12-29 23:17:17 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2021-07-27 17:25:09 -04:00
|
|
|
let program_state = Arc::new(
|
|
|
|
ProgramState::build(Flags {
|
|
|
|
cache_path: maybe_cache_path.clone(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await?,
|
|
|
|
);
|
2020-12-29 23:17:17 -05:00
|
|
|
let handler = Arc::new(Mutex::new(FetchHandler::new(
|
|
|
|
&program_state,
|
|
|
|
Permissions::allow_all(),
|
2021-05-17 03:44:38 -04:00
|
|
|
Permissions::allow_all(),
|
2020-12-29 23:17:17 -05:00
|
|
|
)?));
|
2021-02-11 23:17:48 -05:00
|
|
|
let mut builder = GraphBuilder::new(handler, maybe_import_map.clone(), None);
|
2021-06-21 17:18:32 -04:00
|
|
|
builder.analyze_config_file(maybe_config_file).await?;
|
2021-02-11 23:17:48 -05:00
|
|
|
builder.add(specifier, false).await
|
2020-12-29 23:17:17 -05:00
|
|
|
}
|
|
|
|
|
2021-02-15 04:32:06 -05:00
|
|
|
fn get_remote_headers(
|
|
|
|
cache_filename: &Path,
|
|
|
|
) -> Option<HashMap<String, String>> {
|
|
|
|
let metadata_path = http_cache::Metadata::filename(cache_filename);
|
|
|
|
let metadata_str = fs::read_to_string(metadata_path).ok()?;
|
|
|
|
let metadata: http_cache::Metadata =
|
|
|
|
serde_json::from_str(&metadata_str).ok()?;
|
|
|
|
Some(metadata.headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_remote_specifier(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
http_cache: &HttpCache,
|
|
|
|
redirect_limit: isize,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
2021-02-17 13:47:18 -05:00
|
|
|
let cache_filename = http_cache.get_cache_filename(specifier)?;
|
2021-02-15 04:32:06 -05:00
|
|
|
if redirect_limit >= 0 && cache_filename.is_file() {
|
|
|
|
let headers = get_remote_headers(&cache_filename)?;
|
|
|
|
if let Some(location) = headers.get("location") {
|
|
|
|
let redirect =
|
2021-02-17 13:47:18 -05:00
|
|
|
deno_core::resolve_import(location, specifier.as_str()).ok()?;
|
2021-02-15 04:32:06 -05:00
|
|
|
resolve_remote_specifier(&redirect, http_cache, redirect_limit - 1)
|
|
|
|
} else {
|
|
|
|
Some(specifier.clone())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_specifier(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
redirects: &mut HashMap<ModuleSpecifier, ModuleSpecifier>,
|
|
|
|
http_cache: &HttpCache,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
2021-02-17 13:47:18 -05:00
|
|
|
let scheme = specifier.scheme();
|
2021-02-15 04:32:06 -05:00
|
|
|
if !SUPPORTED_SCHEMES.contains(&scheme) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scheme == "data" {
|
|
|
|
Some(specifier.clone())
|
|
|
|
} else if scheme == "file" {
|
2021-02-17 13:47:18 -05:00
|
|
|
let path = specifier.to_file_path().ok()?;
|
2021-02-15 04:32:06 -05:00
|
|
|
if path.is_file() {
|
|
|
|
Some(specifier.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else if let Some(specifier) = redirects.get(specifier) {
|
|
|
|
Some(specifier.clone())
|
|
|
|
} else {
|
|
|
|
let redirect = resolve_remote_specifier(specifier, http_cache, 10)?;
|
|
|
|
redirects.insert(specifier.clone(), redirect.clone());
|
|
|
|
Some(redirect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 01:33:42 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2020-12-07 05:46:39 -05:00
|
|
|
struct Metadata {
|
|
|
|
dependencies: Option<HashMap<String, analysis::Dependency>>,
|
2021-02-15 04:32:06 -05:00
|
|
|
length_utf16: usize,
|
2021-06-04 17:31:44 -04:00
|
|
|
maybe_navigation_tree: Option<tsc::NavigationTree>,
|
2020-12-24 05:53:03 -05:00
|
|
|
maybe_types: Option<analysis::ResolvedDependency>,
|
2021-05-18 06:19:52 -04:00
|
|
|
maybe_warning: Option<String>,
|
2020-12-07 05:46:39 -05:00
|
|
|
media_type: MediaType,
|
2021-09-07 10:39:32 -04:00
|
|
|
source: DocumentSource,
|
2021-07-25 01:33:42 -04:00
|
|
|
specifier: ModuleSpecifier,
|
2020-12-07 05:46:39 -05:00
|
|
|
version: String,
|
|
|
|
}
|
|
|
|
|
2021-07-25 01:33:42 -04:00
|
|
|
impl Default for Metadata {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
dependencies: None,
|
|
|
|
length_utf16: 0,
|
|
|
|
maybe_navigation_tree: None,
|
|
|
|
maybe_types: None,
|
|
|
|
maybe_warning: None,
|
|
|
|
media_type: MediaType::default(),
|
2021-09-07 10:39:32 -04:00
|
|
|
source: DocumentSource::new(
|
|
|
|
&INVALID_SPECIFIER,
|
|
|
|
MediaType::default(),
|
|
|
|
Arc::new(String::default()),
|
|
|
|
LineIndex::default(),
|
|
|
|
),
|
2021-07-25 01:33:42 -04:00
|
|
|
specifier: INVALID_SPECIFIER.clone(),
|
|
|
|
version: String::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 04:32:06 -05:00
|
|
|
impl Metadata {
|
|
|
|
fn new(
|
|
|
|
specifier: &ModuleSpecifier,
|
2021-09-07 10:39:32 -04:00
|
|
|
source: Arc<String>,
|
2021-02-15 04:32:06 -05:00
|
|
|
version: &str,
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type: MediaType,
|
2021-05-18 06:19:52 -04:00
|
|
|
maybe_warning: Option<String>,
|
2021-02-15 04:32:06 -05:00
|
|
|
maybe_import_map: &Option<ImportMap>,
|
|
|
|
) -> Self {
|
2021-09-07 10:39:32 -04:00
|
|
|
let line_index = LineIndex::new(&source);
|
|
|
|
let document_source =
|
|
|
|
DocumentSource::new(specifier, media_type, source, line_index);
|
|
|
|
let (dependencies, maybe_types) =
|
|
|
|
if let Some(Ok(parsed_module)) = document_source.module() {
|
|
|
|
let (deps, maybe_types) = analysis::analyze_dependencies(
|
|
|
|
specifier,
|
|
|
|
media_type,
|
|
|
|
parsed_module,
|
|
|
|
maybe_import_map,
|
|
|
|
);
|
|
|
|
(Some(deps), maybe_types)
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
2021-02-15 04:32:06 -05:00
|
|
|
|
|
|
|
Self {
|
|
|
|
dependencies,
|
2021-09-07 10:39:32 -04:00
|
|
|
length_utf16: document_source
|
|
|
|
.text_info()
|
|
|
|
.text_str()
|
|
|
|
.encode_utf16()
|
|
|
|
.count(),
|
2021-06-04 17:31:44 -04:00
|
|
|
maybe_navigation_tree: None,
|
2021-02-15 04:32:06 -05:00
|
|
|
maybe_types,
|
2021-05-18 06:19:52 -04:00
|
|
|
maybe_warning,
|
2021-02-15 04:32:06 -05:00
|
|
|
media_type: media_type.to_owned(),
|
2021-09-07 10:39:32 -04:00
|
|
|
source: document_source,
|
2021-07-25 01:33:42 -04:00
|
|
|
specifier: specifier.clone(),
|
2021-02-15 04:32:06 -05:00
|
|
|
version: version.to_string(),
|
|
|
|
}
|
|
|
|
}
|
2021-07-25 01:33:42 -04:00
|
|
|
|
|
|
|
fn refresh(&mut self, maybe_import_map: &Option<ImportMap>) {
|
2021-09-07 10:39:32 -04:00
|
|
|
let (dependencies, maybe_types) =
|
|
|
|
if let Some(Ok(parsed_module)) = self.source.module() {
|
|
|
|
let (deps, maybe_types) = analysis::analyze_dependencies(
|
|
|
|
&self.specifier,
|
|
|
|
self.media_type,
|
|
|
|
parsed_module,
|
|
|
|
maybe_import_map,
|
|
|
|
);
|
|
|
|
(Some(deps), maybe_types)
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
2021-07-25 01:33:42 -04:00
|
|
|
self.dependencies = dependencies;
|
|
|
|
self.maybe_types = maybe_types;
|
|
|
|
}
|
2021-02-15 04:32:06 -05:00
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2021-02-11 23:17:48 -05:00
|
|
|
struct Inner {
|
2020-12-07 05:46:39 -05:00
|
|
|
http_cache: HttpCache,
|
2020-12-21 08:44:26 -05:00
|
|
|
maybe_import_map: Option<ImportMap>,
|
2020-12-07 05:46:39 -05:00
|
|
|
metadata: HashMap<ModuleSpecifier, Metadata>,
|
|
|
|
redirects: HashMap<ModuleSpecifier, ModuleSpecifier>,
|
|
|
|
remotes: HashMap<ModuleSpecifier, PathBuf>,
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct Sources(Arc<Mutex<Inner>>);
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
impl Sources {
|
|
|
|
pub fn new(location: &Path) -> Self {
|
2021-02-11 23:17:48 -05:00
|
|
|
Self(Arc::new(Mutex::new(Inner::new(location))))
|
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
pub fn contains_key(&self, specifier: &ModuleSpecifier) -> bool {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().contains_key(specifier)
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_line_index(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<LineIndex> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().get_line_index(specifier)
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_maybe_types(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<analysis::ResolvedDependency> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().get_maybe_types(specifier)
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
|
2021-05-18 06:19:52 -04:00
|
|
|
pub fn get_maybe_warning(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().get_maybe_warning(specifier)
|
2021-05-18 06:19:52 -04:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
pub fn get_media_type(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<MediaType> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().get_media_type(specifier)
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
|
2021-06-04 17:31:44 -04:00
|
|
|
pub fn get_navigation_tree(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<tsc::NavigationTree> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().get_navigation_tree(specifier)
|
2021-06-04 17:31:44 -04:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
pub fn get_script_version(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().get_script_version(specifier)
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
pub fn get_source(&self, specifier: &ModuleSpecifier) -> Option<Arc<String>> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().get_source(specifier)
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
|
2021-04-19 17:10:43 -04:00
|
|
|
pub fn len(&self) -> usize {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().metadata.len()
|
2021-04-19 17:10:43 -04:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
pub fn resolve_import(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().resolve_import(specifier, referrer)
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
2021-03-24 20:13:37 -04:00
|
|
|
|
|
|
|
pub fn specifiers(&self) -> Vec<ModuleSpecifier> {
|
2021-07-06 23:48:01 -04:00
|
|
|
self.0.lock().metadata.keys().cloned().collect()
|
2021-03-24 20:13:37 -04:00
|
|
|
}
|
2021-06-04 17:31:44 -04:00
|
|
|
|
2021-07-25 01:33:42 -04:00
|
|
|
pub fn set_import_map(&self, maybe_import_map: Option<ImportMap>) {
|
|
|
|
self.0.lock().set_import_map(maybe_import_map)
|
|
|
|
}
|
|
|
|
|
2021-06-04 17:31:44 -04:00
|
|
|
pub fn set_navigation_tree(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
navigation_tree: tsc::NavigationTree,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self
|
|
|
|
.0
|
|
|
|
.lock()
|
|
|
|
.set_navigation_tree(specifier, navigation_tree)
|
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Inner {
|
|
|
|
fn new(location: &Path) -> Self {
|
2020-12-07 05:46:39 -05:00
|
|
|
Self {
|
|
|
|
http_cache: HttpCache::new(location),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 04:32:06 -05:00
|
|
|
fn calculate_script_version(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
|
|
|
let path = self.get_path(specifier)?;
|
|
|
|
let metadata = fs::metadata(path).ok()?;
|
|
|
|
if let Ok(modified) = metadata.modified() {
|
|
|
|
if let Ok(n) = modified.duration_since(SystemTime::UNIX_EPOCH) {
|
|
|
|
Some(format!("{}", n.as_millis()))
|
|
|
|
} else {
|
|
|
|
Some("1".to_string())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some("1".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
fn contains_key(&mut self, specifier: &ModuleSpecifier) -> bool {
|
2021-02-15 04:32:06 -05:00
|
|
|
if let Some(specifier) =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)
|
|
|
|
{
|
2020-12-07 05:46:39 -05:00
|
|
|
if self.get_metadata(&specifier).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
fn get_line_index(
|
2020-12-07 05:46:39 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2021-01-22 05:03:16 -05:00
|
|
|
) -> Option<LineIndex> {
|
2021-02-15 04:32:06 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
2020-12-07 05:46:39 -05:00
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
2021-09-07 10:39:32 -04:00
|
|
|
Some(metadata.source.line_index().clone())
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
fn get_maybe_types(
|
2021-02-09 17:46:12 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<analysis::ResolvedDependency> {
|
2021-02-15 04:32:06 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
2021-02-09 17:46:12 -05:00
|
|
|
metadata.maybe_types
|
|
|
|
}
|
|
|
|
|
2021-05-18 06:19:52 -04:00
|
|
|
fn get_maybe_warning(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
2021-07-30 09:03:41 -04:00
|
|
|
let metadata = self.get_metadata(specifier)?;
|
2021-05-18 06:19:52 -04:00
|
|
|
metadata.maybe_warning
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
fn get_media_type(
|
2020-12-07 05:46:39 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<MediaType> {
|
2021-02-15 04:32:06 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
2020-12-07 05:46:39 -05:00
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
|
|
|
Some(metadata.media_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_metadata(&mut self, specifier: &ModuleSpecifier) -> Option<Metadata> {
|
|
|
|
if let Some(metadata) = self.metadata.get(specifier).cloned() {
|
2021-02-15 04:32:06 -05:00
|
|
|
if metadata.version == self.calculate_script_version(specifier)? {
|
2021-02-11 23:17:48 -05:00
|
|
|
return Some(metadata);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
|
2021-02-15 04:32:06 -05:00
|
|
|
let version = self.calculate_script_version(specifier)?;
|
2020-12-07 05:46:39 -05:00
|
|
|
let path = self.get_path(specifier)?;
|
2021-02-15 04:32:06 -05:00
|
|
|
let bytes = fs::read(path).ok()?;
|
2021-02-17 13:47:18 -05:00
|
|
|
let scheme = specifier.scheme();
|
2021-05-18 06:19:52 -04:00
|
|
|
let (source, media_type, maybe_types, maybe_warning) = if scheme == "file" {
|
2021-02-15 04:32:06 -05:00
|
|
|
let maybe_charset =
|
|
|
|
Some(text_encoding::detect_charset(&bytes).to_string());
|
|
|
|
let source = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
2021-05-18 06:19:52 -04:00
|
|
|
(source, MediaType::from(specifier), None, None)
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
2021-02-17 13:47:18 -05:00
|
|
|
let cache_filename = self.http_cache.get_cache_filename(specifier)?;
|
2021-02-15 04:32:06 -05:00
|
|
|
let headers = get_remote_headers(&cache_filename)?;
|
|
|
|
let maybe_content_type = headers.get("content-type").cloned();
|
|
|
|
let (media_type, maybe_charset) =
|
|
|
|
map_content_type(specifier, maybe_content_type);
|
|
|
|
let source = get_source_from_bytes(bytes, maybe_charset).ok()?;
|
|
|
|
let maybe_types = headers.get("x-typescript-types").map(|s| {
|
2021-07-30 09:03:41 -04:00
|
|
|
analysis::resolve_import(s, specifier, &self.maybe_import_map)
|
2021-02-15 04:32:06 -05:00
|
|
|
});
|
2021-05-18 06:19:52 -04:00
|
|
|
let maybe_warning = headers.get("x-deno-warning").cloned();
|
|
|
|
(source, media_type, maybe_types, maybe_warning)
|
2021-02-15 04:32:06 -05:00
|
|
|
};
|
|
|
|
let mut metadata = Metadata::new(
|
|
|
|
specifier,
|
2021-09-07 10:39:32 -04:00
|
|
|
Arc::new(source),
|
2021-02-15 04:32:06 -05:00
|
|
|
&version,
|
2021-09-07 10:39:32 -04:00
|
|
|
media_type,
|
2021-05-18 06:19:52 -04:00
|
|
|
maybe_warning,
|
2021-02-15 04:32:06 -05:00
|
|
|
&self.maybe_import_map,
|
|
|
|
);
|
2021-04-20 08:22:22 -04:00
|
|
|
if maybe_types.is_some() {
|
2021-02-15 04:32:06 -05:00
|
|
|
metadata.maybe_types = maybe_types;
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-02-15 04:32:06 -05:00
|
|
|
self.metadata.insert(specifier.clone(), metadata.clone());
|
|
|
|
Some(metadata)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-06-04 17:31:44 -04:00
|
|
|
fn get_navigation_tree(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<tsc::NavigationTree> {
|
|
|
|
let specifier =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
|
|
|
metadata.maybe_navigation_tree
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
fn get_path(&mut self, specifier: &ModuleSpecifier) -> Option<PathBuf> {
|
2021-02-17 13:47:18 -05:00
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
specifier.to_file_path().ok()
|
2021-07-30 09:03:41 -04:00
|
|
|
} else if let Some(path) = self.remotes.get(specifier) {
|
2020-12-07 05:46:39 -05:00
|
|
|
Some(path.clone())
|
|
|
|
} else {
|
2021-07-30 09:03:41 -04:00
|
|
|
let path = self.http_cache.get_cache_filename(specifier)?;
|
2020-12-07 05:46:39 -05:00
|
|
|
if path.is_file() {
|
|
|
|
self.remotes.insert(specifier.clone(), path.clone());
|
|
|
|
Some(path)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
fn get_script_version(
|
2020-12-07 05:46:39 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
2021-02-15 04:32:06 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
|
|
|
Some(metadata.version)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
fn get_source(&mut self, specifier: &ModuleSpecifier) -> Option<Arc<String>> {
|
2021-02-15 04:32:06 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
2020-12-07 05:46:39 -05:00
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
2021-09-07 10:39:32 -04:00
|
|
|
Some(metadata.source.text_info().text())
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolution_result(
|
|
|
|
&mut self,
|
2021-02-15 04:32:06 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2020-12-07 05:46:39 -05:00
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2021-02-15 04:32:06 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_specifier(specifier, &mut self.redirects, &self.http_cache)?;
|
|
|
|
let media_type = if let Some(metadata) = self.metadata.get(&specifier) {
|
|
|
|
metadata.media_type
|
|
|
|
} else {
|
|
|
|
MediaType::from(&specifier)
|
|
|
|
};
|
|
|
|
Some((specifier, media_type))
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
fn resolve_import(
|
2020-12-07 05:46:39 -05:00
|
|
|
&mut self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
2021-02-15 04:32:06 -05:00
|
|
|
let referrer =
|
|
|
|
resolve_specifier(referrer, &mut self.redirects, &self.http_cache)?;
|
2020-12-07 05:46:39 -05:00
|
|
|
let metadata = self.get_metadata(&referrer)?;
|
|
|
|
let dependencies = &metadata.dependencies?;
|
|
|
|
let dependency = dependencies.get(specifier)?;
|
|
|
|
if let Some(type_dependency) = &dependency.maybe_type {
|
2020-12-24 05:53:03 -05:00
|
|
|
if let analysis::ResolvedDependency::Resolved(resolved_specifier) =
|
2020-12-07 05:46:39 -05:00
|
|
|
type_dependency
|
|
|
|
{
|
2021-04-20 08:22:22 -04:00
|
|
|
// even if we have a module in the maybe_types slot, it doesn't mean
|
|
|
|
// that it is the actual module we should be using based on headers,
|
|
|
|
// so we check here and update properly.
|
|
|
|
if let Some(type_dependency) = self.get_maybe_types(resolved_specifier)
|
|
|
|
{
|
|
|
|
self.set_maybe_type(specifier, &referrer, &type_dependency);
|
|
|
|
if let analysis::ResolvedDependency::Resolved(type_specifier) =
|
|
|
|
type_dependency
|
|
|
|
{
|
|
|
|
self.resolution_result(&type_specifier)
|
|
|
|
} else {
|
|
|
|
self.resolution_result(resolved_specifier)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.resolution_result(resolved_specifier)
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let code_dependency = &dependency.maybe_code.clone()?;
|
2020-12-24 05:53:03 -05:00
|
|
|
if let analysis::ResolvedDependency::Resolved(resolved_specifier) =
|
2020-12-07 05:46:39 -05:00
|
|
|
code_dependency
|
|
|
|
{
|
2021-02-15 04:32:06 -05:00
|
|
|
if let Some(type_dependency) = self.get_maybe_types(resolved_specifier)
|
|
|
|
{
|
|
|
|
self.set_maybe_type(specifier, &referrer, &type_dependency);
|
|
|
|
if let analysis::ResolvedDependency::Resolved(type_specifier) =
|
|
|
|
type_dependency
|
|
|
|
{
|
|
|
|
self.resolution_result(&type_specifier)
|
|
|
|
} else {
|
|
|
|
self.resolution_result(resolved_specifier)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.resolution_result(resolved_specifier)
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 01:33:42 -04:00
|
|
|
fn set_import_map(&mut self, maybe_import_map: Option<ImportMap>) {
|
|
|
|
for (_, metadata) in self.metadata.iter_mut() {
|
|
|
|
metadata.refresh(&maybe_import_map);
|
|
|
|
}
|
|
|
|
self.maybe_import_map = maybe_import_map;
|
|
|
|
}
|
|
|
|
|
2021-02-15 04:32:06 -05:00
|
|
|
fn set_maybe_type(
|
2020-12-07 05:46:39 -05:00
|
|
|
&mut self,
|
2021-02-15 04:32:06 -05:00
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
dependency: &analysis::ResolvedDependency,
|
|
|
|
) {
|
|
|
|
if let Some(metadata) = self.metadata.get_mut(referrer) {
|
|
|
|
if let Some(dependencies) = &mut metadata.dependencies {
|
|
|
|
if let Some(dep) = dependencies.get_mut(specifier) {
|
|
|
|
dep.maybe_type = Some(dependency.clone());
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 17:31:44 -04:00
|
|
|
|
|
|
|
fn set_navigation_tree(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
navigation_tree: NavigationTree,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let mut metadata = self
|
|
|
|
.metadata
|
|
|
|
.get_mut(specifier)
|
|
|
|
.ok_or_else(|| anyhow!("Specifier not found {}"))?;
|
|
|
|
metadata.maybe_navigation_tree = Some(navigation_tree);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_path;
|
|
|
|
use deno_core::resolve_url;
|
2021-07-25 01:33:42 -04:00
|
|
|
use deno_core::serde_json::json;
|
2020-12-07 05:46:39 -05:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
|
|
|
fn setup() -> (Sources, PathBuf) {
|
|
|
|
let temp_dir = TempDir::new().expect("could not create temp dir");
|
|
|
|
let location = temp_dir.path().join("deps");
|
|
|
|
let sources = Sources::new(&location);
|
|
|
|
(sources, location)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sources_get_script_version() {
|
2021-02-11 23:17:48 -05:00
|
|
|
let (sources, _) = setup();
|
2021-08-11 10:20:47 -04:00
|
|
|
let tests = test_util::testdata_path();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap();
|
2020-12-07 05:46:39 -05:00
|
|
|
let actual = sources.get_script_version(&specifier);
|
|
|
|
assert!(actual.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sources_get_text() {
|
2021-02-11 23:17:48 -05:00
|
|
|
let (sources, _) = setup();
|
2021-08-11 10:20:47 -04:00
|
|
|
let tests = test_util::testdata_path();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_path(&tests.join("001_hello.js").to_string_lossy()).unwrap();
|
2021-02-15 04:32:06 -05:00
|
|
|
let actual = sources.get_source(&specifier);
|
2020-12-07 05:46:39 -05:00
|
|
|
assert!(actual.is_some());
|
2021-09-07 10:39:32 -04:00
|
|
|
let actual = actual.unwrap().to_string();
|
2020-12-07 05:46:39 -05:00
|
|
|
assert_eq!(actual, "console.log(\"Hello World\");\n");
|
|
|
|
}
|
|
|
|
|
2021-02-15 04:32:06 -05:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_dependency_types() {
|
|
|
|
let (sources, location) = setup();
|
|
|
|
let cache = HttpCache::new(&location);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier_dep = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
2021-02-15 04:32:06 -05:00
|
|
|
cache
|
|
|
|
.set(
|
2021-02-17 13:47:18 -05:00
|
|
|
&specifier_dep,
|
2021-02-15 04:32:06 -05:00
|
|
|
Default::default(),
|
|
|
|
b"export * from \"https://deno.land/x/lib.js\";",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier_code = resolve_url("https://deno.land/x/lib.js").unwrap();
|
2021-02-15 04:32:06 -05:00
|
|
|
let mut headers_code = HashMap::new();
|
|
|
|
headers_code
|
|
|
|
.insert("x-typescript-types".to_string(), "./lib.d.ts".to_string());
|
|
|
|
cache
|
2021-02-17 13:47:18 -05:00
|
|
|
.set(&specifier_code, headers_code, b"export const a = 1;")
|
2021-02-15 04:32:06 -05:00
|
|
|
.unwrap();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier_type = resolve_url("https://deno.land/x/lib.d.ts").unwrap();
|
2021-02-15 04:32:06 -05:00
|
|
|
cache
|
|
|
|
.set(
|
2021-02-17 13:47:18 -05:00
|
|
|
&specifier_type,
|
2021-02-15 04:32:06 -05:00
|
|
|
Default::default(),
|
|
|
|
b"export const a: number;",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let actual =
|
|
|
|
sources.resolve_import("https://deno.land/x/lib.js", &specifier_dep);
|
|
|
|
assert_eq!(actual, Some((specifier_type, MediaType::Dts)))
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:22:22 -04:00
|
|
|
#[test]
|
|
|
|
/// This is a regression test for https://github.com/denoland/deno/issues/10031
|
|
|
|
fn test_resolve_dependency_import_types() {
|
|
|
|
let (sources, location) = setup();
|
|
|
|
let cache = HttpCache::new(&location);
|
|
|
|
let specifier_dep = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&specifier_dep,
|
|
|
|
Default::default(),
|
|
|
|
b"import type { A } from \"https://deno.land/x/lib.js\";\nconst a: A = { a: \"a\" };",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let specifier_code = resolve_url("https://deno.land/x/lib.js").unwrap();
|
|
|
|
let mut headers_code = HashMap::new();
|
|
|
|
headers_code
|
|
|
|
.insert("x-typescript-types".to_string(), "./lib.d.ts".to_string());
|
|
|
|
cache
|
|
|
|
.set(&specifier_code, headers_code, b"export const a = 1;")
|
|
|
|
.unwrap();
|
|
|
|
let specifier_type = resolve_url("https://deno.land/x/lib.d.ts").unwrap();
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&specifier_type,
|
|
|
|
Default::default(),
|
|
|
|
b"export const a: number;\nexport interface A { a: number; }\n",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let actual =
|
|
|
|
sources.resolve_import("https://deno.land/x/lib.js", &specifier_dep);
|
|
|
|
assert_eq!(actual, Some((specifier_type, MediaType::Dts)))
|
|
|
|
}
|
|
|
|
|
2021-05-18 06:19:52 -04:00
|
|
|
#[test]
|
|
|
|
fn test_warning_header() {
|
|
|
|
let (sources, location) = setup();
|
|
|
|
let cache = HttpCache::new(&location);
|
|
|
|
let specifier = resolve_url("https://deno.land/x/lib.js").unwrap();
|
|
|
|
let mut headers = HashMap::new();
|
|
|
|
headers.insert(
|
|
|
|
"x-deno-warning".to_string(),
|
|
|
|
"this is a warning".to_string(),
|
|
|
|
);
|
|
|
|
cache
|
|
|
|
.set(&specifier, headers, b"export const a = 1;")
|
|
|
|
.unwrap();
|
|
|
|
let actual = sources.get_maybe_warning(&specifier);
|
|
|
|
assert_eq!(actual, Some("this is a warning".to_string()));
|
|
|
|
}
|
|
|
|
|
2021-02-15 04:32:06 -05:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_dependency_evil_redirect() {
|
|
|
|
let (sources, location) = setup();
|
|
|
|
let cache = HttpCache::new(&location);
|
2021-02-17 13:47:18 -05:00
|
|
|
let evil_specifier = resolve_url("https://deno.land/x/evil.ts").unwrap();
|
2021-02-15 04:32:06 -05:00
|
|
|
let mut evil_headers = HashMap::new();
|
|
|
|
evil_headers
|
|
|
|
.insert("location".to_string(), "file:///etc/passwd".to_string());
|
2021-02-17 13:47:18 -05:00
|
|
|
cache.set(&evil_specifier, evil_headers, b"").unwrap();
|
|
|
|
let remote_specifier = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
2021-02-15 04:32:06 -05:00
|
|
|
cache
|
|
|
|
.set(
|
2021-02-17 13:47:18 -05:00
|
|
|
&remote_specifier,
|
2021-02-15 04:32:06 -05:00
|
|
|
Default::default(),
|
|
|
|
b"export * from \"./evil.ts\";",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let actual = sources.resolve_import("./evil.ts", &remote_specifier);
|
|
|
|
assert_eq!(actual, None);
|
|
|
|
}
|
|
|
|
|
2021-07-25 01:33:42 -04:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_with_import_map() {
|
|
|
|
let (sources, location) = setup();
|
|
|
|
let import_map_json = json!({
|
|
|
|
"imports": {
|
|
|
|
"mylib": "https://deno.land/x/myLib/index.js"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let import_map = ImportMap::from_json(
|
|
|
|
"https://deno.land/x/",
|
|
|
|
&import_map_json.to_string(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
sources.set_import_map(Some(import_map));
|
|
|
|
let cache = HttpCache::new(&location);
|
|
|
|
let mylib_specifier =
|
|
|
|
resolve_url("https://deno.land/x/myLib/index.js").unwrap();
|
|
|
|
let mut mylib_headers_map = HashMap::new();
|
|
|
|
mylib_headers_map.insert(
|
|
|
|
"content-type".to_string(),
|
|
|
|
"application/javascript".to_string(),
|
|
|
|
);
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&mylib_specifier,
|
|
|
|
mylib_headers_map,
|
|
|
|
b"export const a = \"a\";\n",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let referrer = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&referrer,
|
|
|
|
Default::default(),
|
|
|
|
b"export { a } from \"mylib\";",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let actual = sources.resolve_import("mylib", &referrer);
|
|
|
|
assert_eq!(actual, Some((mylib_specifier, MediaType::JavaScript)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_update_import_map() {
|
|
|
|
let (sources, location) = setup();
|
|
|
|
let import_map_json = json!({
|
|
|
|
"imports": {
|
|
|
|
"otherlib": "https://deno.land/x/otherlib/index.js"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let import_map = ImportMap::from_json(
|
|
|
|
"https://deno.land/x/",
|
|
|
|
&import_map_json.to_string(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
sources.set_import_map(Some(import_map));
|
|
|
|
let cache = HttpCache::new(&location);
|
|
|
|
let mylib_specifier =
|
|
|
|
resolve_url("https://deno.land/x/myLib/index.js").unwrap();
|
|
|
|
let mut mylib_headers_map = HashMap::new();
|
|
|
|
mylib_headers_map.insert(
|
|
|
|
"content-type".to_string(),
|
|
|
|
"application/javascript".to_string(),
|
|
|
|
);
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&mylib_specifier,
|
|
|
|
mylib_headers_map,
|
|
|
|
b"export const a = \"a\";\n",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let referrer = resolve_url("https://deno.land/x/mod.ts").unwrap();
|
|
|
|
cache
|
|
|
|
.set(
|
|
|
|
&referrer,
|
|
|
|
Default::default(),
|
|
|
|
b"export { a } from \"mylib\";",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let actual = sources.resolve_import("mylib", &referrer);
|
|
|
|
assert_eq!(actual, None);
|
|
|
|
let import_map_json = json!({
|
|
|
|
"imports": {
|
|
|
|
"otherlib": "https://deno.land/x/otherlib/index.js",
|
|
|
|
"mylib": "https://deno.land/x/myLib/index.js"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let import_map = ImportMap::from_json(
|
|
|
|
"https://deno.land/x/",
|
|
|
|
&import_map_json.to_string(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
sources.set_import_map(Some(import_map));
|
|
|
|
let actual = sources.resolve_import("mylib", &referrer);
|
|
|
|
assert_eq!(actual, Some((mylib_specifier, MediaType::JavaScript)));
|
|
|
|
}
|
|
|
|
|
2020-12-09 19:12:46 -05:00
|
|
|
#[test]
|
|
|
|
fn test_sources_resolve_specifier_non_supported_schema() {
|
2021-02-11 23:17:48 -05:00
|
|
|
let (sources, _) = setup();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_url("foo://a/b/c.ts").expect("could not create specifier");
|
2021-07-06 23:48:01 -04:00
|
|
|
let sources = sources.0.lock();
|
2021-02-15 04:32:06 -05:00
|
|
|
let mut redirects = sources.redirects.clone();
|
|
|
|
let http_cache = sources.http_cache.clone();
|
|
|
|
let actual = resolve_specifier(&specifier, &mut redirects, &http_cache);
|
2020-12-09 19:12:46 -05:00
|
|
|
assert!(actual.is_none());
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|