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
|
|
|
|
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;
|
2022-08-20 11:31:33 -04:00
|
|
|
use crate::npm;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
use deno_core::futures;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
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;
|
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
mod check;
|
|
|
|
mod common;
|
|
|
|
mod disk_cache;
|
|
|
|
mod emit;
|
|
|
|
mod incremental;
|
2022-08-22 12:14:59 -04:00
|
|
|
mod parsed_source;
|
2022-07-12 18:58:39 -04:00
|
|
|
|
|
|
|
pub use check::TypeCheckCache;
|
2022-07-19 11:58:18 -04:00
|
|
|
pub use common::FastInsecureHasher;
|
2022-07-12 18:58:39 -04:00
|
|
|
pub use disk_cache::DiskCache;
|
|
|
|
pub use emit::EmitCache;
|
|
|
|
pub use incremental::IncrementalCache;
|
2022-08-22 12:14:59 -04:00
|
|
|
pub use parsed_source::ParsedSourceCache;
|
2022-07-12 18:58:39 -04:00
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
/// 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 {
|
2022-07-19 11:58:18 -04:00
|
|
|
emit_cache: EmitCache,
|
2021-10-10 17:26:22 -04:00
|
|
|
dynamic_permissions: Permissions,
|
|
|
|
file_fetcher: Arc<FileFetcher>,
|
|
|
|
root_permissions: Permissions,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FetchCacher {
|
|
|
|
pub fn new(
|
2022-07-19 11:58:18 -04:00
|
|
|
emit_cache: EmitCache,
|
2021-10-10 17:26:22 -04:00
|
|
|
file_fetcher: FileFetcher,
|
|
|
|
root_permissions: Permissions,
|
|
|
|
dynamic_permissions: Permissions,
|
|
|
|
) -> Self {
|
|
|
|
let file_fetcher = Arc::new(file_fetcher);
|
|
|
|
|
|
|
|
Self {
|
2022-07-19 11:58:18 -04:00
|
|
|
emit_cache,
|
2021-10-10 17:26:22 -04:00
|
|
|
dynamic_permissions,
|
|
|
|
file_fetcher,
|
|
|
|
root_permissions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Loader for FetchCacher {
|
|
|
|
fn get_cache_info(&self, specifier: &ModuleSpecifier) -> Option<CacheInfo> {
|
2022-08-20 11:31:33 -04:00
|
|
|
if specifier.scheme() == "npm" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
let local = self.file_fetcher.get_local_path(specifier)?;
|
|
|
|
if local.is_file() {
|
|
|
|
let emit = self
|
2022-07-19 11:58:18 -04:00
|
|
|
.emit_cache
|
|
|
|
.get_emit_filepath(specifier)
|
2021-10-10 17:26:22 -04:00
|
|
|
.filter(|p| p.is_file());
|
|
|
|
Some(CacheInfo {
|
|
|
|
local: Some(local),
|
|
|
|
emit,
|
2022-07-19 11:58:18 -04:00
|
|
|
map: None,
|
2021-10-10 17:26:22 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
is_dynamic: bool,
|
|
|
|
) -> LoadFuture {
|
2022-08-20 11:31:33 -04:00
|
|
|
if specifier.scheme() == "npm" {
|
|
|
|
return Box::pin(futures::future::ready(
|
|
|
|
match npm::NpmPackageReference::from_specifier(specifier) {
|
|
|
|
Ok(_) => Ok(Some(deno_graph::source::LoadResponse::External {
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
})),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|