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-01-22 05:03:16 -05:00
|
|
|
use super::text::LineIndex;
|
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;
|
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-07 05:46:39 -05:00
|
|
|
use crate::media_type::MediaType;
|
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-01-07 21:08:51 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2020-12-29 23:17:17 -05:00
|
|
|
use deno_core::error::AnyError;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
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;
|
|
|
|
use std::sync::Mutex;
|
2020-12-07 05:46:39 -05:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
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>,
|
2020-12-29 23:17:17 -05:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let program_state = Arc::new(ProgramState::new(Default::default())?);
|
|
|
|
let handler = Arc::new(Mutex::new(FetchHandler::new(
|
|
|
|
&program_state,
|
|
|
|
Permissions::allow_all(),
|
|
|
|
)?));
|
2021-02-11 23:17:48 -05:00
|
|
|
let mut builder = GraphBuilder::new(handler, maybe_import_map.clone(), None);
|
|
|
|
builder.add(specifier, false).await
|
2020-12-29 23:17:17 -05:00
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
struct Metadata {
|
|
|
|
dependencies: Option<HashMap<String, analysis::Dependency>>,
|
2021-01-22 05:03:16 -05:00
|
|
|
line_index: LineIndex,
|
2020-12-24 05:53:03 -05:00
|
|
|
maybe_types: Option<analysis::ResolvedDependency>,
|
2020-12-07 05:46:39 -05:00
|
|
|
media_type: MediaType,
|
|
|
|
source: String,
|
|
|
|
version: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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))))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains(&self, specifier: &ModuleSpecifier) -> bool {
|
|
|
|
self.0.lock().unwrap().contains(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_length_utf16(&self, specifier: &ModuleSpecifier) -> Option<usize> {
|
|
|
|
self.0.lock().unwrap().get_length_utf16(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_line_index(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<LineIndex> {
|
|
|
|
self.0.lock().unwrap().get_line_index(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_maybe_types(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<analysis::ResolvedDependency> {
|
|
|
|
self.0.lock().unwrap().get_maybe_types(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_media_type(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<MediaType> {
|
|
|
|
self.0.lock().unwrap().get_media_type(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_script_version(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
|
|
|
self.0.lock().unwrap().get_script_version(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_text(&self, specifier: &ModuleSpecifier) -> Option<String> {
|
|
|
|
self.0.lock().unwrap().get_text(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_import(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
|
|
|
self.0.lock().unwrap().resolve_import(specifier, referrer)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn resolve_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
self.0.lock().unwrap().resolve_specifier(specifier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11 23:17:48 -05:00
|
|
|
fn contains(&mut self, specifier: &ModuleSpecifier) -> bool {
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Some(specifier) = self.resolve_specifier(specifier) {
|
|
|
|
if self.get_metadata(&specifier).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-02-06 07:39:01 -05:00
|
|
|
/// Provides the length of the source content, calculated in a way that should
|
|
|
|
/// match the behavior of JavaScript, where strings are stored effectively as
|
|
|
|
/// `&[u16]` and when counting "chars" we need to represent the string as a
|
|
|
|
/// UTF-16 string in Rust.
|
2021-02-11 23:17:48 -05:00
|
|
|
fn get_length_utf16(&mut self, specifier: &ModuleSpecifier) -> Option<usize> {
|
2020-12-07 05:46:39 -05:00
|
|
|
let specifier = self.resolve_specifier(specifier)?;
|
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
2021-01-22 05:03:16 -05:00
|
|
|
Some(metadata.source.encode_utf16().count())
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
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> {
|
2020-12-07 05:46:39 -05:00
|
|
|
let specifier = self.resolve_specifier(specifier)?;
|
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
2021-01-22 05:03:16 -05:00
|
|
|
Some(metadata.line_index)
|
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> {
|
|
|
|
let metadata = self.get_metadata(specifier)?;
|
|
|
|
metadata.maybe_types
|
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
let specifier = self.resolve_specifier(specifier)?;
|
|
|
|
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-11 23:17:48 -05:00
|
|
|
if metadata.version == self.get_script_version(specifier)? {
|
|
|
|
return Some(metadata);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
|
2021-02-09 17:46:12 -05:00
|
|
|
// TODO(@kitsonk) this needs to be refactored, lots of duplicate logic and
|
|
|
|
// is really difficult to follow.
|
2020-12-07 05:46:39 -05:00
|
|
|
let version = self.get_script_version(specifier)?;
|
|
|
|
let path = self.get_path(specifier)?;
|
|
|
|
if let Ok(bytes) = fs::read(path) {
|
|
|
|
if specifier.as_url().scheme() == "file" {
|
|
|
|
let charset = text_encoding::detect_charset(&bytes).to_string();
|
|
|
|
if let Ok(source) = get_source_from_bytes(bytes, Some(charset)) {
|
|
|
|
let media_type = MediaType::from(specifier);
|
|
|
|
let mut maybe_types = None;
|
2021-02-09 17:46:12 -05:00
|
|
|
let maybe_import_map = self.maybe_import_map.clone();
|
2020-12-07 05:46:39 -05:00
|
|
|
let dependencies = if let Some((dependencies, mt)) =
|
|
|
|
analysis::analyze_dependencies(
|
|
|
|
&specifier,
|
|
|
|
&source,
|
|
|
|
&media_type,
|
2021-02-09 17:46:12 -05:00
|
|
|
&maybe_import_map,
|
2020-12-07 05:46:39 -05:00
|
|
|
) {
|
|
|
|
maybe_types = mt;
|
|
|
|
Some(dependencies)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-01-22 05:03:16 -05:00
|
|
|
let line_index = LineIndex::new(&source);
|
2020-12-07 05:46:39 -05:00
|
|
|
let metadata = Metadata {
|
|
|
|
dependencies,
|
2021-01-22 05:03:16 -05:00
|
|
|
line_index,
|
2020-12-07 05:46:39 -05:00
|
|
|
maybe_types,
|
|
|
|
media_type,
|
|
|
|
source,
|
|
|
|
version,
|
|
|
|
};
|
|
|
|
self.metadata.insert(specifier.clone(), metadata.clone());
|
|
|
|
Some(metadata)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let headers = self.get_remote_headers(specifier)?;
|
|
|
|
let maybe_content_type = headers.get("content-type").cloned();
|
|
|
|
let (media_type, maybe_charset) =
|
|
|
|
map_content_type(specifier, maybe_content_type);
|
|
|
|
if let Ok(source) = get_source_from_bytes(bytes, maybe_charset) {
|
|
|
|
let mut maybe_types =
|
|
|
|
if let Some(types) = headers.get("x-typescript-types") {
|
2020-12-09 14:50:47 -05:00
|
|
|
Some(analysis::resolve_import(
|
|
|
|
types,
|
|
|
|
&specifier,
|
2020-12-21 08:44:26 -05:00
|
|
|
&self.maybe_import_map,
|
2020-12-09 14:50:47 -05:00
|
|
|
))
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-02-09 17:46:12 -05:00
|
|
|
let maybe_import_map = self.maybe_import_map.clone();
|
2020-12-07 05:46:39 -05:00
|
|
|
let dependencies = if let Some((dependencies, mt)) =
|
|
|
|
analysis::analyze_dependencies(
|
|
|
|
&specifier,
|
|
|
|
&source,
|
|
|
|
&media_type,
|
2021-02-09 17:46:12 -05:00
|
|
|
&maybe_import_map,
|
2020-12-07 05:46:39 -05:00
|
|
|
) {
|
|
|
|
if maybe_types.is_none() {
|
|
|
|
maybe_types = mt;
|
|
|
|
}
|
|
|
|
Some(dependencies)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-01-22 05:03:16 -05:00
|
|
|
let line_index = LineIndex::new(&source);
|
2020-12-07 05:46:39 -05:00
|
|
|
let metadata = Metadata {
|
|
|
|
dependencies,
|
2021-01-22 05:03:16 -05:00
|
|
|
line_index,
|
2020-12-07 05:46:39 -05:00
|
|
|
maybe_types,
|
|
|
|
media_type,
|
|
|
|
source,
|
|
|
|
version,
|
|
|
|
};
|
|
|
|
self.metadata.insert(specifier.clone(), metadata.clone());
|
|
|
|
Some(metadata)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_path(&mut self, specifier: &ModuleSpecifier) -> Option<PathBuf> {
|
|
|
|
let specifier = self.resolve_specifier(specifier)?;
|
|
|
|
if specifier.as_url().scheme() == "file" {
|
|
|
|
if let Ok(path) = specifier.as_url().to_file_path() {
|
|
|
|
Some(path)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else if let Some(path) = self.remotes.get(&specifier) {
|
|
|
|
Some(path.clone())
|
|
|
|
} else {
|
|
|
|
let path = self.http_cache.get_cache_filename(&specifier.as_url());
|
|
|
|
if path.is_file() {
|
|
|
|
self.remotes.insert(specifier.clone(), path.clone());
|
|
|
|
Some(path)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_remote_headers(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<HashMap<String, String>> {
|
|
|
|
let cache_filename = self.http_cache.get_cache_filename(specifier.as_url());
|
|
|
|
let metadata_path = http_cache::Metadata::filename(&cache_filename);
|
|
|
|
if let Ok(metadata) = fs::read_to_string(metadata_path) {
|
|
|
|
if let Ok(metadata) =
|
|
|
|
serde_json::from_str::<'_, http_cache::Metadata>(&metadata)
|
|
|
|
{
|
|
|
|
return Some(metadata.headers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-11 23:17:48 -05:00
|
|
|
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())
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
} else {
|
|
|
|
Some("1".to_string())
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
fn get_text(&mut self, specifier: &ModuleSpecifier) -> Option<String> {
|
2020-12-07 05:46:39 -05:00
|
|
|
let specifier = self.resolve_specifier(specifier)?;
|
|
|
|
let metadata = self.get_metadata(&specifier)?;
|
|
|
|
Some(metadata.source)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolution_result(
|
|
|
|
&mut self,
|
|
|
|
resolved_specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<(ModuleSpecifier, MediaType)> {
|
|
|
|
let resolved_specifier = self.resolve_specifier(resolved_specifier)?;
|
|
|
|
let media_type =
|
|
|
|
if let Some(metadata) = self.metadata.get(&resolved_specifier) {
|
|
|
|
metadata.media_type
|
|
|
|
} else {
|
|
|
|
MediaType::from(&resolved_specifier)
|
|
|
|
};
|
|
|
|
Some((resolved_specifier, media_type))
|
|
|
|
}
|
|
|
|
|
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)> {
|
|
|
|
let referrer = self.resolve_specifier(referrer)?;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
self.resolution_result(resolved_specifier)
|
|
|
|
} 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
|
|
|
|
{
|
|
|
|
self.resolution_result(resolved_specifier)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_specifier(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
2020-12-09 19:12:46 -05:00
|
|
|
let scheme = specifier.as_url().scheme();
|
|
|
|
if !SUPPORTED_SCHEMES.contains(&scheme) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if scheme == "file" {
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Ok(path) = specifier.as_url().to_file_path() {
|
|
|
|
if path.is_file() {
|
|
|
|
return Some(specifier.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Some(specifier) = self.redirects.get(specifier) {
|
|
|
|
return Some(specifier.clone());
|
|
|
|
}
|
|
|
|
if let Some(redirect) = self.resolve_remote_specifier(specifier, 10) {
|
|
|
|
self.redirects.insert(specifier.clone(), redirect.clone());
|
|
|
|
return Some(redirect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_remote_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
redirect_limit: isize,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
let cached_filename =
|
|
|
|
self.http_cache.get_cache_filename(specifier.as_url());
|
|
|
|
if redirect_limit >= 0 && cached_filename.is_file() {
|
|
|
|
if let Some(headers) = self.get_remote_headers(specifier) {
|
|
|
|
if let Some(redirect_to) = headers.get("location") {
|
|
|
|
if let Ok(redirect) =
|
|
|
|
ModuleSpecifier::resolve_import(redirect_to, specifier.as_str())
|
|
|
|
{
|
|
|
|
return self
|
|
|
|
.resolve_remote_specifier(&redirect, redirect_limit - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Some(specifier.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::env;
|
|
|
|
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();
|
2020-12-07 05:46:39 -05:00
|
|
|
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
let tests = c.join("tests");
|
|
|
|
let specifier = ModuleSpecifier::resolve_path(
|
|
|
|
&tests.join("001_hello.js").to_string_lossy(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
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();
|
2020-12-07 05:46:39 -05:00
|
|
|
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
let tests = c.join("tests");
|
|
|
|
let specifier = ModuleSpecifier::resolve_path(
|
|
|
|
&tests.join("001_hello.js").to_string_lossy(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let actual = sources.get_text(&specifier);
|
|
|
|
assert!(actual.is_some());
|
|
|
|
let actual = actual.unwrap();
|
|
|
|
assert_eq!(actual, "console.log(\"Hello World\");\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-22 05:03:16 -05:00
|
|
|
fn test_sources_get_length_utf16() {
|
2021-02-11 23:17:48 -05:00
|
|
|
let (sources, _) = setup();
|
2020-12-07 05:46:39 -05:00
|
|
|
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
|
|
|
let tests = c.join("tests");
|
|
|
|
let specifier = ModuleSpecifier::resolve_path(
|
|
|
|
&tests.join("001_hello.js").to_string_lossy(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-01-22 05:03:16 -05:00
|
|
|
let actual = sources.get_length_utf16(&specifier);
|
2020-12-07 05:46:39 -05:00
|
|
|
assert!(actual.is_some());
|
|
|
|
let actual = actual.unwrap();
|
|
|
|
assert_eq!(actual, 28);
|
|
|
|
}
|
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();
|
2020-12-09 19:12:46 -05:00
|
|
|
let specifier = ModuleSpecifier::resolve_url("foo://a/b/c.ts")
|
|
|
|
.expect("could not create specifier");
|
2021-02-06 07:39:01 -05:00
|
|
|
let actual = sources.resolve_specifier(&specifier);
|
2020-12-09 19:12:46 -05:00
|
|
|
assert!(actual.is_none());
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|