2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-24 18:31:17 -04:00
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
use crate::ast::Location;
|
2020-09-24 18:31:17 -04:00
|
|
|
use crate::deno_dir::DenoDir;
|
|
|
|
use crate::disk_cache::DiskCache;
|
2020-11-05 19:38:21 -05:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
2020-09-24 18:31:17 -04:00
|
|
|
use crate::media_type::MediaType;
|
2020-10-13 07:35:35 -04:00
|
|
|
use crate::program_state::ProgramState;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
2020-11-01 21:51:56 -05:00
|
|
|
use deno_core::error::custom_error;
|
2020-09-24 18:31:17 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-11-01 21:51:56 -05:00
|
|
|
use deno_core::futures::future;
|
2020-09-24 18:31:17 -04:00
|
|
|
use deno_core::futures::Future;
|
|
|
|
use deno_core::futures::FutureExt;
|
2021-02-25 15:18:35 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
2020-09-24 18:31:17 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2020-09-24 18:31:17 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
|
|
|
use std::fmt;
|
2020-10-11 22:25:27 -04:00
|
|
|
use std::path::PathBuf;
|
2020-09-24 18:31:17 -04:00
|
|
|
use std::pin::Pin;
|
2020-09-30 03:22:58 -04:00
|
|
|
use std::sync::Arc;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
pub type DependencyMap = HashMap<String, Dependency>;
|
2020-12-15 00:52:55 -05:00
|
|
|
pub type FetchFuture = Pin<
|
|
|
|
Box<
|
|
|
|
(dyn Future<Output = Result<CachedModule, (ModuleSpecifier, AnyError)>>
|
2020-12-29 23:17:17 -05:00
|
|
|
+ 'static
|
|
|
|
+ Send),
|
2020-12-15 00:52:55 -05:00
|
|
|
>,
|
|
|
|
>;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
/// A group of errors that represent errors that can occur with an
|
|
|
|
/// an implementation of `SpecifierHandler`.
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub enum HandlerError {
|
|
|
|
/// A fetch error, where we have a location associated with it.
|
|
|
|
FetchErrorWithLocation(String, Location),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for HandlerError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
HandlerError::FetchErrorWithLocation(ref err, ref location) => {
|
|
|
|
write!(f, "{}\n at {}", err, location)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for HandlerError {}
|
|
|
|
|
2020-09-24 18:31:17 -04:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct CachedModule {
|
2020-10-22 20:50:15 -04:00
|
|
|
pub is_remote: bool,
|
2020-09-24 18:31:17 -04:00
|
|
|
pub maybe_dependencies: Option<DependencyMap>,
|
2020-10-13 06:54:28 -04:00
|
|
|
pub maybe_emit: Option<Emit>,
|
|
|
|
pub maybe_emit_path: Option<(PathBuf, Option<PathBuf>)>,
|
2020-09-24 18:31:17 -04:00
|
|
|
pub maybe_types: Option<String>,
|
|
|
|
pub maybe_version: Option<String>,
|
|
|
|
pub media_type: MediaType,
|
2020-10-15 19:34:55 -04:00
|
|
|
pub requested_specifier: ModuleSpecifier,
|
2020-10-07 07:43:44 -04:00
|
|
|
pub source: String,
|
2020-10-11 22:25:27 -04:00
|
|
|
pub source_path: PathBuf,
|
2020-09-24 18:31:17 -04:00
|
|
|
pub specifier: ModuleSpecifier,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CachedModule {
|
|
|
|
fn default() -> Self {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = deno_core::resolve_url("file:///example.js").unwrap();
|
2020-09-24 18:31:17 -04:00
|
|
|
CachedModule {
|
2020-10-22 20:50:15 -04:00
|
|
|
is_remote: false,
|
2020-09-24 18:31:17 -04:00
|
|
|
maybe_dependencies: None,
|
2020-10-13 06:54:28 -04:00
|
|
|
maybe_emit: None,
|
2020-10-11 22:25:27 -04:00
|
|
|
maybe_emit_path: None,
|
2020-09-24 18:31:17 -04:00
|
|
|
maybe_types: None,
|
|
|
|
maybe_version: None,
|
|
|
|
media_type: MediaType::Unknown,
|
2020-10-15 19:34:55 -04:00
|
|
|
requested_specifier: specifier.clone(),
|
2020-10-07 07:43:44 -04:00
|
|
|
source: "".to_string(),
|
2020-10-11 22:25:27 -04:00
|
|
|
source_path: PathBuf::new(),
|
2020-10-15 19:34:55 -04:00
|
|
|
specifier,
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 06:54:28 -04:00
|
|
|
/// An enum to own the a specific emit.
|
|
|
|
///
|
|
|
|
/// Currently there is only one type of emit that is cacheable, but this has
|
|
|
|
/// been added to future proof the ability for the specifier handler
|
|
|
|
/// implementations to be able to handle other types of emits, like form a
|
|
|
|
/// runtime API which might have a different configuration.
|
2020-09-24 18:31:17 -04:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
2020-10-13 06:54:28 -04:00
|
|
|
pub enum Emit {
|
2020-09-24 18:31:17 -04:00
|
|
|
/// Code that was emitted for use by the CLI
|
2020-10-13 06:54:28 -04:00
|
|
|
Cli((String, Option<String>)),
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 06:54:28 -04:00
|
|
|
impl Default for Emit {
|
2020-09-24 18:31:17 -04:00
|
|
|
fn default() -> Self {
|
2020-10-13 06:54:28 -04:00
|
|
|
Emit::Cli(("".to_string(), None))
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2020-09-24 18:31:17 -04:00
|
|
|
pub struct Dependency {
|
2020-10-22 20:50:15 -04:00
|
|
|
/// Flags if the dependency is a dynamic import or not.
|
|
|
|
pub is_dynamic: bool,
|
|
|
|
/// The location in the source code where the dependency statement occurred.
|
|
|
|
pub location: Location,
|
2020-09-24 18:31:17 -04:00
|
|
|
/// The module specifier that resolves to the runtime code dependency for the
|
|
|
|
/// module.
|
|
|
|
pub maybe_code: Option<ModuleSpecifier>,
|
|
|
|
/// The module specifier that resolves to the type only dependency for the
|
|
|
|
/// module.
|
|
|
|
pub maybe_type: Option<ModuleSpecifier>,
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
impl Dependency {
|
|
|
|
pub fn new(location: Location) -> Self {
|
|
|
|
Dependency {
|
|
|
|
is_dynamic: false,
|
|
|
|
location,
|
|
|
|
maybe_code: None,
|
|
|
|
maybe_type: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 23:17:17 -05:00
|
|
|
pub trait SpecifierHandler: Sync + Send {
|
2020-09-24 18:31:17 -04:00
|
|
|
/// Instructs the handler to fetch a specifier or retrieve its value from the
|
2020-09-30 07:46:42 -04:00
|
|
|
/// cache.
|
2020-10-22 20:50:15 -04:00
|
|
|
fn fetch(
|
|
|
|
&mut self,
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
maybe_location: Option<Location>,
|
|
|
|
is_dynamic: bool,
|
|
|
|
) -> FetchFuture;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
/// Get the optional build info from the cache for a given module specifier.
|
|
|
|
/// Because build infos are only associated with the "root" modules, they are
|
|
|
|
/// not expected to be cached for each module, but are "lazily" checked when
|
|
|
|
/// a root module is identified. The `emit_type` also indicates what form
|
|
|
|
/// of the module the build info is valid for.
|
2020-10-22 20:50:15 -04:00
|
|
|
fn get_tsbuildinfo(
|
2020-09-24 18:31:17 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2020-10-07 07:43:44 -04:00
|
|
|
) -> Result<Option<String>, AnyError>;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
2020-10-13 06:54:28 -04:00
|
|
|
/// Set the emit for the module specifier.
|
2020-09-24 18:31:17 -04:00
|
|
|
fn set_cache(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2020-10-13 06:54:28 -04:00
|
|
|
emit: &Emit,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError>;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
/// When parsed out of a JavaScript module source, the triple slash reference
|
|
|
|
/// to the types should be stored in the cache.
|
|
|
|
fn set_types(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
types: String,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError>;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
/// Set the build info for a module specifier, also providing the cache type.
|
2020-10-22 20:50:15 -04:00
|
|
|
fn set_tsbuildinfo(
|
2020-09-24 18:31:17 -04:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2020-10-22 20:50:15 -04:00
|
|
|
tsbuildinfo: String,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError>;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
/// Set the graph dependencies for a given module specifier.
|
|
|
|
fn set_deps(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
dependencies: DependencyMap,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError>;
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
/// Set the version of the source for a given module, which is used to help
|
2020-09-30 07:46:42 -04:00
|
|
|
/// determine if a module needs to be re-emitted.
|
2020-09-24 18:31:17 -04:00
|
|
|
fn set_version(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
version: String,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError>;
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for dyn SpecifierHandler {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "SpecifierHandler {{ }}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A representation of meta data for a compiled file.
|
|
|
|
///
|
|
|
|
/// *Note* this is currently just a copy of what is located in `tsc.rs` but will
|
|
|
|
/// be refactored to be able to store dependencies and type information in the
|
|
|
|
/// future.
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct CompiledFileMetadata {
|
|
|
|
pub version_hash: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CompiledFileMetadata {
|
2020-09-30 07:46:42 -04:00
|
|
|
pub fn from_bytes(bytes: &[u8]) -> Result<Self, AnyError> {
|
|
|
|
let metadata_string = std::str::from_utf8(bytes)?;
|
2020-09-24 18:31:17 -04:00
|
|
|
serde_json::from_str::<Self>(metadata_string).map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2020-09-30 07:46:42 -04:00
|
|
|
pub fn to_json_string(&self) -> Result<String, AnyError> {
|
2020-09-24 18:31:17 -04:00
|
|
|
serde_json::to_string(self).map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An implementation of the `SpecifierHandler` trait that integrates with the
|
|
|
|
/// existing `file_fetcher` interface, which will eventually be refactored to
|
|
|
|
/// align it more to the `SpecifierHandler` trait.
|
|
|
|
pub struct FetchHandler {
|
2020-10-22 20:50:15 -04:00
|
|
|
/// An instance of disk where generated (emitted) files are stored.
|
2020-09-24 18:31:17 -04:00
|
|
|
disk_cache: DiskCache,
|
2021-05-17 03:44:38 -04:00
|
|
|
/// The set permissions which are used for root modules (static imports).
|
|
|
|
root_permissions: Permissions,
|
|
|
|
/// The set of permissions which are used for dynamic imports.
|
|
|
|
dynamic_permissions: Permissions,
|
2020-10-22 20:50:15 -04:00
|
|
|
/// A clone of the `program_state` file fetcher.
|
2020-11-05 19:38:21 -05:00
|
|
|
file_fetcher: FileFetcher,
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FetchHandler {
|
2020-09-30 03:22:58 -04:00
|
|
|
pub fn new(
|
2020-10-13 07:35:35 -04:00
|
|
|
program_state: &Arc<ProgramState>,
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions: Permissions,
|
|
|
|
dynamic_permissions: Permissions,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<Self, AnyError> {
|
2020-09-24 18:31:17 -04:00
|
|
|
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
|
|
|
let deno_dir = DenoDir::new(custom_root)?;
|
|
|
|
let disk_cache = deno_dir.gen_cache;
|
2020-10-13 07:35:35 -04:00
|
|
|
let file_fetcher = program_state.file_fetcher.clone();
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
Ok(FetchHandler {
|
|
|
|
disk_cache,
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2020-09-24 18:31:17 -04:00
|
|
|
file_fetcher,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpecifierHandler for FetchHandler {
|
2020-10-22 20:50:15 -04:00
|
|
|
fn fetch(
|
|
|
|
&mut self,
|
|
|
|
requested_specifier: ModuleSpecifier,
|
|
|
|
maybe_location: Option<Location>,
|
|
|
|
is_dynamic: bool,
|
|
|
|
) -> FetchFuture {
|
|
|
|
// When the module graph fetches dynamic modules, the set of dynamic
|
|
|
|
// permissions need to be applied. Other static imports have all
|
|
|
|
// permissions.
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut permissions = if is_dynamic {
|
2021-05-17 03:44:38 -04:00
|
|
|
self.dynamic_permissions.clone()
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
2021-05-17 03:44:38 -04:00
|
|
|
self.root_permissions.clone()
|
2020-10-22 20:50:15 -04:00
|
|
|
};
|
2020-09-24 18:31:17 -04:00
|
|
|
let file_fetcher = self.file_fetcher.clone();
|
|
|
|
let disk_cache = self.disk_cache.clone();
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let source_file = file_fetcher
|
2021-04-11 22:15:43 -04:00
|
|
|
.fetch(&requested_specifier, &mut permissions)
|
2020-10-22 20:50:15 -04:00
|
|
|
.await
|
|
|
|
.map_err(|err| {
|
2020-11-05 19:38:21 -05:00
|
|
|
let err = if let Some(e) = err.downcast_ref::<std::io::Error>() {
|
|
|
|
if e.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
let message = if let Some(location) = &maybe_location {
|
|
|
|
format!(
|
|
|
|
"Cannot resolve module \"{}\" from \"{}\".",
|
|
|
|
requested_specifier, location.filename
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!("Cannot resolve module \"{}\".", requested_specifier)
|
|
|
|
};
|
|
|
|
custom_error("NotFound", message)
|
|
|
|
} else {
|
|
|
|
err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err
|
|
|
|
};
|
2020-10-22 20:50:15 -04:00
|
|
|
if let Some(location) = maybe_location {
|
2020-11-05 19:38:21 -05:00
|
|
|
// Injected modules (like test and eval) come with locations, but
|
|
|
|
// they are confusing to the user to print out the location because
|
|
|
|
// they cannot actually get to the source code that is quoted, as
|
|
|
|
// it only exists in the runtime memory of Deno.
|
|
|
|
if !location.filename.contains("$deno$") {
|
2020-12-15 00:52:55 -05:00
|
|
|
(
|
|
|
|
requested_specifier.clone(),
|
|
|
|
HandlerError::FetchErrorWithLocation(err.to_string(), location)
|
|
|
|
.into(),
|
|
|
|
)
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
2020-12-15 00:52:55 -05:00
|
|
|
(requested_specifier.clone(), err)
|
2020-10-22 20:50:15 -04:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-15 00:52:55 -05:00
|
|
|
(requested_specifier.clone(), err)
|
2020-10-22 20:50:15 -04:00
|
|
|
}
|
|
|
|
})?;
|
2021-02-17 13:47:18 -05:00
|
|
|
let url = &source_file.specifier;
|
2021-04-07 09:22:14 -04:00
|
|
|
let is_remote = !(url.scheme() == "file"
|
|
|
|
|| url.scheme() == "data"
|
|
|
|
|| url.scheme() == "blob");
|
2020-11-05 19:38:21 -05:00
|
|
|
let filename = disk_cache.get_cache_filename_with_extension(url, "meta");
|
2020-12-15 00:52:55 -05:00
|
|
|
let maybe_version = if let Some(filename) = filename {
|
|
|
|
if let Ok(bytes) = disk_cache.get(&filename) {
|
|
|
|
if let Ok(compiled_file_metadata) =
|
|
|
|
CompiledFileMetadata::from_bytes(&bytes)
|
|
|
|
{
|
|
|
|
Some(compiled_file_metadata.version_hash)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-09-24 18:31:17 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-10-13 06:54:28 -04:00
|
|
|
let mut maybe_map_path = None;
|
2020-10-11 22:25:27 -04:00
|
|
|
let map_path =
|
2020-09-24 18:31:17 -04:00
|
|
|
disk_cache.get_cache_filename_with_extension(&url, "js.map");
|
2020-12-15 00:52:55 -05:00
|
|
|
let maybe_map = if let Some(map_path) = map_path {
|
|
|
|
if let Ok(map) = disk_cache.get(&map_path) {
|
|
|
|
maybe_map_path = Some(disk_cache.location.join(map_path));
|
|
|
|
Some(String::from_utf8(map).unwrap())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2020-10-07 07:43:44 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-10-13 06:54:28 -04:00
|
|
|
let mut maybe_emit = None;
|
|
|
|
let mut maybe_emit_path = None;
|
2020-10-11 22:25:27 -04:00
|
|
|
let emit_path = disk_cache.get_cache_filename_with_extension(&url, "js");
|
2020-12-15 00:52:55 -05:00
|
|
|
if let Some(emit_path) = emit_path {
|
|
|
|
if let Ok(code) = disk_cache.get(&emit_path) {
|
|
|
|
maybe_emit =
|
|
|
|
Some(Emit::Cli((String::from_utf8(code).unwrap(), maybe_map)));
|
|
|
|
maybe_emit_path =
|
|
|
|
Some((disk_cache.location.join(emit_path), maybe_map_path));
|
|
|
|
}
|
2020-09-24 18:31:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(CachedModule {
|
2020-10-22 20:50:15 -04:00
|
|
|
is_remote,
|
2020-09-24 18:31:17 -04:00
|
|
|
maybe_dependencies: None,
|
2020-10-13 06:54:28 -04:00
|
|
|
maybe_emit,
|
2020-10-11 22:25:27 -04:00
|
|
|
maybe_emit_path,
|
2020-11-05 19:38:21 -05:00
|
|
|
maybe_types: source_file.maybe_types,
|
2020-09-24 18:31:17 -04:00
|
|
|
maybe_version,
|
|
|
|
media_type: source_file.media_type,
|
2020-10-15 19:34:55 -04:00
|
|
|
requested_specifier,
|
2020-11-05 19:38:21 -05:00
|
|
|
source: source_file.source,
|
|
|
|
source_path: source_file.local,
|
|
|
|
specifier: source_file.specifier,
|
2020-09-24 18:31:17 -04:00
|
|
|
})
|
|
|
|
}
|
2020-12-29 23:17:17 -05:00
|
|
|
.boxed()
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
fn get_tsbuildinfo(
|
2020-09-24 18:31:17 -04:00
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2020-10-07 07:43:44 -04:00
|
|
|
) -> Result<Option<String>, AnyError> {
|
2020-09-24 18:31:17 -04:00
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
2021-02-17 13:47:18 -05:00
|
|
|
.get_cache_filename_with_extension(specifier, "buildinfo");
|
2020-12-15 00:52:55 -05:00
|
|
|
if let Some(filename) = filename {
|
|
|
|
if let Ok(tsbuildinfo) = self.disk_cache.get(&filename) {
|
|
|
|
Ok(Some(String::from_utf8(tsbuildinfo)?))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
fn set_tsbuildinfo(
|
2020-09-24 18:31:17 -04:00
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2020-10-22 20:50:15 -04:00
|
|
|
tsbuildinfo: String,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-09-24 18:31:17 -04:00
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
2021-02-17 13:47:18 -05:00
|
|
|
.get_cache_filename_with_extension(specifier, "buildinfo")
|
2020-12-15 00:52:55 -05:00
|
|
|
.unwrap();
|
2020-10-22 20:50:15 -04:00
|
|
|
debug!("set_tsbuildinfo - filename {:?}", filename);
|
2020-09-24 18:31:17 -04:00
|
|
|
self
|
|
|
|
.disk_cache
|
2020-10-22 20:50:15 -04:00
|
|
|
.set(&filename, tsbuildinfo.as_bytes())
|
2020-09-24 18:31:17 -04:00
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_cache(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2020-10-13 06:54:28 -04:00
|
|
|
emit: &Emit,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-10-13 06:54:28 -04:00
|
|
|
match emit {
|
|
|
|
Emit::Cli((code, maybe_map)) => {
|
2020-12-15 00:52:55 -05:00
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
2021-02-17 13:47:18 -05:00
|
|
|
.get_cache_filename_with_extension(specifier, "js")
|
2020-12-15 00:52:55 -05:00
|
|
|
.unwrap();
|
2020-10-13 06:54:28 -04:00
|
|
|
self.disk_cache.set(&filename, code.as_bytes())?;
|
|
|
|
|
|
|
|
if let Some(map) = maybe_map {
|
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
2021-02-17 13:47:18 -05:00
|
|
|
.get_cache_filename_with_extension(specifier, "js.map")
|
2020-12-15 00:52:55 -05:00
|
|
|
.unwrap();
|
2020-10-13 06:54:28 -04:00
|
|
|
self.disk_cache.set(&filename, map.as_bytes())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_deps(
|
|
|
|
&mut self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
_dependencies: DependencyMap,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-09-24 18:31:17 -04:00
|
|
|
// file_fetcher doesn't have the concept of caching dependencies
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_types(
|
|
|
|
&mut self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
_types: String,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-09-24 18:31:17 -04:00
|
|
|
// file_fetcher doesn't have the concept of caching of the types
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_version(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
version_hash: String,
|
2020-09-30 07:46:42 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-09-24 18:31:17 -04:00
|
|
|
let compiled_file_metadata = CompiledFileMetadata { version_hash };
|
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
2021-02-17 13:47:18 -05:00
|
|
|
.get_cache_filename_with_extension(specifier, "meta")
|
2020-12-15 00:52:55 -05:00
|
|
|
.unwrap();
|
2020-09-24 18:31:17 -04:00
|
|
|
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.set(
|
|
|
|
&filename,
|
|
|
|
compiled_file_metadata.to_json_string()?.as_bytes(),
|
|
|
|
)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:51:56 -05:00
|
|
|
pub struct MemoryHandler {
|
|
|
|
sources: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MemoryHandler {
|
|
|
|
pub fn new(sources: HashMap<String, String>) -> Self {
|
|
|
|
Self { sources }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpecifierHandler for MemoryHandler {
|
|
|
|
fn fetch(
|
|
|
|
&mut self,
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<Location>,
|
|
|
|
_is_dynamic: bool,
|
|
|
|
) -> FetchFuture {
|
|
|
|
let mut specifier_text = specifier.to_string();
|
|
|
|
if !self.sources.contains_key(&specifier_text) {
|
|
|
|
specifier_text = specifier_text.replace("file:///", "/");
|
|
|
|
if !self.sources.contains_key(&specifier_text) {
|
|
|
|
// Convert `C:/a/path/file.ts` to `/a/path/file.ts`
|
|
|
|
specifier_text = specifier_text[3..].to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let result = if let Some(source) = self.sources.get(&specifier_text) {
|
|
|
|
let media_type = MediaType::from(&specifier);
|
2021-02-17 13:47:18 -05:00
|
|
|
let is_remote = specifier.scheme() != "file";
|
2020-11-01 21:51:56 -05:00
|
|
|
|
|
|
|
Ok(CachedModule {
|
|
|
|
source: source.to_string(),
|
|
|
|
requested_specifier: specifier.clone(),
|
|
|
|
specifier,
|
|
|
|
media_type,
|
|
|
|
is_remote,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
} else {
|
2020-12-15 00:52:55 -05:00
|
|
|
Err((
|
|
|
|
specifier.clone(),
|
|
|
|
custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!("Unable to find specifier in sources: {}", specifier),
|
|
|
|
),
|
2020-11-01 21:51:56 -05:00
|
|
|
))
|
|
|
|
};
|
|
|
|
|
|
|
|
Box::pin(future::ready(result))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_tsbuildinfo(
|
|
|
|
&self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<Option<String>, AnyError> {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_cache(
|
|
|
|
&mut self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
_emit: &Emit,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_types(
|
|
|
|
&mut self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
_types: String,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_tsbuildinfo(
|
|
|
|
&mut self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
_tsbuildinfo: String,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_deps(
|
|
|
|
&mut self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
_dependencies: DependencyMap,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_version(
|
|
|
|
&mut self,
|
|
|
|
_specifier: &ModuleSpecifier,
|
|
|
|
_version: String,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:31:17 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
|
|
|
use super::*;
|
2020-11-05 19:38:21 -05:00
|
|
|
use crate::file_fetcher::CacheSetting;
|
2020-09-30 03:22:58 -04:00
|
|
|
use crate::http_cache::HttpCache;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url_or_path;
|
2021-04-07 09:22:14 -04:00
|
|
|
use deno_runtime::deno_file::BlobUrlStore;
|
2020-09-24 18:31:17 -04:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2020-11-01 21:51:56 -05:00
|
|
|
macro_rules! map (
|
|
|
|
{ $($key:expr => $value:expr),+ } => {
|
|
|
|
{
|
|
|
|
let mut m = ::std::collections::HashMap::new();
|
|
|
|
$(
|
|
|
|
m.insert($key, $value);
|
|
|
|
)+
|
|
|
|
m
|
|
|
|
}
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2020-09-24 18:31:17 -04:00
|
|
|
fn setup() -> (TempDir, FetchHandler) {
|
|
|
|
let temp_dir = TempDir::new().expect("could not setup");
|
|
|
|
let deno_dir = DenoDir::new(Some(temp_dir.path().to_path_buf()))
|
|
|
|
.expect("could not setup");
|
|
|
|
|
2020-11-05 19:38:21 -05:00
|
|
|
let file_fetcher = FileFetcher::new(
|
2020-09-24 18:31:17 -04:00
|
|
|
HttpCache::new(&temp_dir.path().to_path_buf().join("deps")),
|
2020-11-05 19:38:21 -05:00
|
|
|
CacheSetting::Use,
|
2020-09-24 18:31:17 -04:00
|
|
|
true,
|
|
|
|
None,
|
2021-04-07 09:22:14 -04:00
|
|
|
BlobUrlStore::default(),
|
2020-09-24 18:31:17 -04:00
|
|
|
)
|
|
|
|
.expect("could not setup");
|
|
|
|
let disk_cache = deno_dir.gen_cache;
|
|
|
|
|
|
|
|
let fetch_handler = FetchHandler {
|
|
|
|
disk_cache,
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions: Permissions::allow_all(),
|
|
|
|
dynamic_permissions: Permissions::default(),
|
2020-09-24 18:31:17 -04:00
|
|
|
file_fetcher,
|
|
|
|
};
|
|
|
|
|
|
|
|
(temp_dir, fetch_handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_handler_fetch() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let (_, mut file_fetcher) = setup();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
|
|
|
|
.unwrap();
|
2020-10-22 20:50:15 -04:00
|
|
|
let cached_module: CachedModule = file_fetcher
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-13 06:54:28 -04:00
|
|
|
assert!(cached_module.maybe_emit.is_none());
|
2020-09-24 18:31:17 -04:00
|
|
|
assert!(cached_module.maybe_dependencies.is_none());
|
|
|
|
assert_eq!(cached_module.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(
|
2020-10-07 07:43:44 -04:00
|
|
|
cached_module.source,
|
2020-09-24 18:31:17 -04:00
|
|
|
"export { printHello } from \"./print_hello.ts\";\n"
|
|
|
|
);
|
|
|
|
assert_eq!(cached_module.specifier, specifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_handler_set_cache() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let (_, mut file_fetcher) = setup();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
|
|
|
|
.unwrap();
|
2020-10-22 20:50:15 -04:00
|
|
|
let cached_module: CachedModule = file_fetcher
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-13 06:54:28 -04:00
|
|
|
assert!(cached_module.maybe_emit.is_none());
|
2020-10-07 07:43:44 -04:00
|
|
|
let code = String::from("some code");
|
2020-09-24 18:31:17 -04:00
|
|
|
file_fetcher
|
2020-10-13 06:54:28 -04:00
|
|
|
.set_cache(&specifier, &Emit::Cli((code, None)))
|
2020-09-24 18:31:17 -04:00
|
|
|
.expect("could not set cache");
|
2020-10-22 20:50:15 -04:00
|
|
|
let cached_module: CachedModule = file_fetcher
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2020-10-13 06:54:28 -04:00
|
|
|
assert_eq!(
|
|
|
|
cached_module.maybe_emit,
|
|
|
|
Some(Emit::Cli(("some code".to_string(), None)))
|
|
|
|
);
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|
2020-10-22 20:50:15 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_fetch_handler_is_remote() {
|
|
|
|
let _http_server_guard = test_util::http_server();
|
|
|
|
let (_, mut file_fetcher) = setup();
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier =
|
|
|
|
resolve_url_or_path("http://localhost:4545/cli/tests/subdir/mod2.ts")
|
|
|
|
.unwrap();
|
2020-10-22 20:50:15 -04:00
|
|
|
let cached_module: CachedModule =
|
|
|
|
file_fetcher.fetch(specifier, None, false).await.unwrap();
|
|
|
|
assert_eq!(cached_module.is_remote, true);
|
|
|
|
let c = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path(
|
2020-10-22 20:50:15 -04:00
|
|
|
c.join("tests/subdir/mod1.ts").as_os_str().to_str().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let cached_module: CachedModule =
|
|
|
|
file_fetcher.fetch(specifier, None, false).await.unwrap();
|
|
|
|
assert_eq!(cached_module.is_remote, false);
|
|
|
|
}
|
2020-11-01 21:51:56 -05:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_memory_handler_fetch() {
|
|
|
|
let a_src = r#"
|
|
|
|
import * as b from "./b.ts";
|
|
|
|
console.log(b);
|
|
|
|
"#;
|
|
|
|
let b_src = r#"
|
|
|
|
export const b = "b";
|
|
|
|
"#;
|
|
|
|
let c_src = r#"
|
|
|
|
export const c = "c";
|
|
|
|
"#;
|
|
|
|
let d_src = r#"
|
|
|
|
export const d: string;
|
|
|
|
"#;
|
|
|
|
let sources = map!(
|
|
|
|
"/a.ts" => a_src,
|
|
|
|
"/b.ts" => b_src,
|
|
|
|
"https://deno.land/x/c.js" => c_src,
|
|
|
|
"https://deno.land/x/d.d.ts" => d_src
|
|
|
|
);
|
|
|
|
let sources: HashMap<String, String> = sources
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (k.to_string(), v.to_string()))
|
|
|
|
.collect();
|
|
|
|
let mut handler = MemoryHandler::new(sources);
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("file:///a.ts").unwrap();
|
2020-11-01 21:51:56 -05:00
|
|
|
let actual: CachedModule = handler
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.expect("could not fetch module");
|
|
|
|
assert_eq!(actual.source, a_src.to_string());
|
|
|
|
assert_eq!(actual.requested_specifier, specifier);
|
|
|
|
assert_eq!(actual.specifier, specifier);
|
|
|
|
assert_eq!(actual.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(actual.is_remote, false);
|
|
|
|
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("file:///b.ts").unwrap();
|
2020-11-01 21:51:56 -05:00
|
|
|
let actual: CachedModule = handler
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.expect("could not fetch module");
|
|
|
|
assert_eq!(actual.source, b_src.to_string());
|
|
|
|
assert_eq!(actual.requested_specifier, specifier);
|
|
|
|
assert_eq!(actual.specifier, specifier);
|
|
|
|
assert_eq!(actual.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(actual.is_remote, false);
|
|
|
|
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/c.js").unwrap();
|
2020-11-01 21:51:56 -05:00
|
|
|
let actual: CachedModule = handler
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.expect("could not fetch module");
|
|
|
|
assert_eq!(actual.source, c_src.to_string());
|
|
|
|
assert_eq!(actual.requested_specifier, specifier);
|
|
|
|
assert_eq!(actual.specifier, specifier);
|
|
|
|
assert_eq!(actual.media_type, MediaType::JavaScript);
|
|
|
|
assert_eq!(actual.is_remote, true);
|
|
|
|
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/d.d.ts").unwrap();
|
2020-11-01 21:51:56 -05:00
|
|
|
let actual: CachedModule = handler
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.expect("could not fetch module");
|
|
|
|
assert_eq!(actual.source, d_src.to_string());
|
|
|
|
assert_eq!(actual.requested_specifier, specifier);
|
|
|
|
assert_eq!(actual.specifier, specifier);
|
|
|
|
assert_eq!(actual.media_type, MediaType::Dts);
|
|
|
|
assert_eq!(actual.is_remote, true);
|
|
|
|
|
|
|
|
let specifier =
|
2021-02-17 13:47:18 -05:00
|
|
|
resolve_url_or_path("https://deno.land/x/missing.ts").unwrap();
|
2020-11-01 21:51:56 -05:00
|
|
|
handler
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.expect_err("should have errored");
|
|
|
|
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("/a.ts").unwrap();
|
2020-11-01 21:51:56 -05:00
|
|
|
let actual: CachedModule = handler
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.expect("could not fetch module");
|
|
|
|
assert_eq!(actual.source, a_src.to_string());
|
|
|
|
assert_eq!(actual.requested_specifier, specifier);
|
|
|
|
assert_eq!(actual.specifier, specifier);
|
|
|
|
assert_eq!(actual.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(actual.is_remote, false);
|
|
|
|
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("file:///C:/a.ts").unwrap();
|
2020-11-01 21:51:56 -05:00
|
|
|
let actual: CachedModule = handler
|
|
|
|
.fetch(specifier.clone(), None, false)
|
|
|
|
.await
|
|
|
|
.expect("could not fetch module");
|
|
|
|
assert_eq!(actual.source, a_src.to_string());
|
|
|
|
assert_eq!(actual.requested_specifier, specifier);
|
|
|
|
assert_eq!(actual.specifier, specifier);
|
|
|
|
assert_eq!(actual.media_type, MediaType::TypeScript);
|
|
|
|
assert_eq!(actual.is_remote, false);
|
|
|
|
}
|
2020-09-24 18:31:17 -04:00
|
|
|
}
|