2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
use crate::disk_cache::DiskCache;
|
2021-12-22 08:25:06 -05:00
|
|
|
use crate::errors::get_error_class_name;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_graph::source::CacheInfo;
|
|
|
|
use deno_graph::source::LoadFuture;
|
|
|
|
use deno_graph::source::LoadResponse;
|
|
|
|
use deno_graph::source::Loader;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct EmitMetadata {
|
|
|
|
pub version_hash: String,
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub enum CacheType {
|
2021-10-10 17:26:22 -04:00
|
|
|
Emit,
|
|
|
|
SourceMap,
|
|
|
|
TypeScriptBuildInfo,
|
|
|
|
Version,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A trait which provides a concise implementation to getting and setting
|
|
|
|
/// values in a cache.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub trait Cacher {
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Get a value from the cache.
|
|
|
|
fn get(
|
|
|
|
&self,
|
|
|
|
cache_type: CacheType,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String>;
|
|
|
|
/// Set a value in the cache.
|
|
|
|
fn set(
|
|
|
|
&mut self,
|
|
|
|
cache_type: CacheType,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
value: String,
|
|
|
|
) -> Result<(), AnyError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combines the cacher trait along with the deno_graph Loader trait to provide
|
|
|
|
/// a single interface to be able to load and cache modules when building a
|
|
|
|
/// graph.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub trait CacherLoader: Cacher + Loader {
|
2021-10-10 17:26:22 -04:00
|
|
|
fn as_cacher(&self) -> &dyn Cacher;
|
|
|
|
fn as_mut_loader(&mut self) -> &mut dyn Loader;
|
|
|
|
fn as_mut_cacher(&mut self) -> &mut dyn Cacher;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A "wrapper" for the FileFetcher and DiskCache for the Deno CLI that provides
|
|
|
|
/// a concise interface to the DENO_DIR when building module graphs.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct FetchCacher {
|
2021-10-10 17:26:22 -04:00
|
|
|
disk_cache: DiskCache,
|
|
|
|
dynamic_permissions: Permissions,
|
|
|
|
file_fetcher: Arc<FileFetcher>,
|
|
|
|
root_permissions: Permissions,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FetchCacher {
|
|
|
|
pub fn new(
|
|
|
|
disk_cache: DiskCache,
|
|
|
|
file_fetcher: FileFetcher,
|
|
|
|
root_permissions: Permissions,
|
|
|
|
dynamic_permissions: Permissions,
|
|
|
|
) -> Self {
|
|
|
|
let file_fetcher = Arc::new(file_fetcher);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
disk_cache,
|
|
|
|
dynamic_permissions,
|
|
|
|
file_fetcher,
|
|
|
|
root_permissions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_emit_metadata(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<EmitMetadata> {
|
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, "meta")?;
|
|
|
|
let bytes = self.disk_cache.get(&filename).ok()?;
|
|
|
|
serde_json::from_slice(&bytes).ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_emit_metadata(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
data: EmitMetadata,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, "meta")
|
|
|
|
.unwrap();
|
|
|
|
let bytes = serde_json::to_vec(&data)?;
|
|
|
|
self.disk_cache.set(&filename, &bytes).map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Loader for FetchCacher {
|
|
|
|
fn get_cache_info(&self, specifier: &ModuleSpecifier) -> Option<CacheInfo> {
|
|
|
|
let local = self.file_fetcher.get_local_path(specifier)?;
|
|
|
|
if local.is_file() {
|
|
|
|
let location = &self.disk_cache.location;
|
|
|
|
let emit = self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, "js")
|
|
|
|
.map(|p| location.join(p))
|
|
|
|
.filter(|p| p.is_file());
|
|
|
|
let map = self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, "js.map")
|
|
|
|
.map(|p| location.join(p))
|
|
|
|
.filter(|p| p.is_file());
|
|
|
|
Some(CacheInfo {
|
|
|
|
local: Some(local),
|
|
|
|
emit,
|
|
|
|
map,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
is_dynamic: bool,
|
|
|
|
) -> LoadFuture {
|
|
|
|
let specifier = specifier.clone();
|
|
|
|
let mut permissions = if is_dynamic {
|
|
|
|
self.dynamic_permissions.clone()
|
|
|
|
} else {
|
|
|
|
self.root_permissions.clone()
|
|
|
|
};
|
|
|
|
let file_fetcher = self.file_fetcher.clone();
|
|
|
|
|
|
|
|
async move {
|
2022-01-13 11:58:00 -05:00
|
|
|
file_fetcher
|
2021-10-10 17:26:22 -04:00
|
|
|
.fetch(&specifier, &mut permissions)
|
|
|
|
.await
|
|
|
|
.map_or_else(
|
|
|
|
|err| {
|
|
|
|
if let Some(err) = err.downcast_ref::<std::io::Error>() {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2021-12-22 08:25:06 -05:00
|
|
|
} else if get_error_class_name(&err) == "NotFound" {
|
|
|
|
return Ok(None);
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
Err(err)
|
|
|
|
},
|
|
|
|
|file| {
|
2022-02-15 07:33:46 -05:00
|
|
|
Ok(Some(LoadResponse::Module {
|
2021-10-10 17:26:22 -04:00
|
|
|
specifier: file.specifier,
|
|
|
|
maybe_headers: file.maybe_headers,
|
|
|
|
content: file.source,
|
|
|
|
}))
|
|
|
|
},
|
2022-01-13 11:58:00 -05:00
|
|
|
)
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cacher for FetchCacher {
|
|
|
|
fn get(
|
|
|
|
&self,
|
|
|
|
cache_type: CacheType,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
|
|
|
let extension = match cache_type {
|
|
|
|
CacheType::Emit => "js",
|
|
|
|
CacheType::SourceMap => "js.map",
|
|
|
|
CacheType::TypeScriptBuildInfo => "buildinfo",
|
|
|
|
CacheType::Version => {
|
|
|
|
return self.get_emit_metadata(specifier).map(|d| d.version_hash)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, extension)?;
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.get(&filename)
|
|
|
|
.ok()
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|b| String::from_utf8(b).ok())
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set(
|
|
|
|
&mut self,
|
|
|
|
cache_type: CacheType,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
value: String,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let extension = match cache_type {
|
|
|
|
CacheType::Emit => "js",
|
|
|
|
CacheType::SourceMap => "js.map",
|
|
|
|
CacheType::TypeScriptBuildInfo => "buildinfo",
|
|
|
|
CacheType::Version => {
|
|
|
|
let data = if let Some(mut data) = self.get_emit_metadata(specifier) {
|
|
|
|
data.version_hash = value;
|
|
|
|
data
|
|
|
|
} else {
|
|
|
|
EmitMetadata {
|
|
|
|
version_hash: value,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return self.set_emit_metadata(specifier, data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let filename = self
|
|
|
|
.disk_cache
|
|
|
|
.get_cache_filename_with_extension(specifier, extension)
|
|
|
|
.unwrap();
|
|
|
|
self
|
|
|
|
.disk_cache
|
|
|
|
.set(&filename, value.as_bytes())
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacherLoader for FetchCacher {
|
|
|
|
fn as_cacher(&self) -> &dyn Cacher {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_mut_loader(&mut self) -> &mut dyn Loader {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_mut_cacher(&mut self) -> &mut dyn Cacher {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|