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::cache::FetchCacher;
|
2021-11-08 20:26:39 -05:00
|
|
|
use crate::config_file::ConfigFile;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::flags::Flags;
|
2021-12-16 05:45:41 -05:00
|
|
|
use crate::graph_util::graph_valid;
|
2022-02-01 21:04:26 -05:00
|
|
|
use crate::http_cache;
|
2021-10-10 17:26:22 -04:00
|
|
|
use crate::proc_state::ProcState;
|
|
|
|
use crate::resolver::ImportMapResolver;
|
2021-11-08 20:26:39 -05:00
|
|
|
use crate::resolver::JsxResolver;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-02-01 21:04:26 -05:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-10-21 07:05:43 -04:00
|
|
|
use deno_runtime::tokio_util::create_basic_runtime;
|
2021-10-10 17:26:22 -04:00
|
|
|
use import_map::ImportMap;
|
2022-02-01 21:04:26 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
2021-10-10 17:26:22 -04:00
|
|
|
use std::path::PathBuf;
|
2021-11-08 20:26:39 -05:00
|
|
|
use std::sync::Arc;
|
2021-10-10 17:26:22 -04:00
|
|
|
use std::thread;
|
2022-02-01 21:04:26 -05:00
|
|
|
use std::time::SystemTime;
|
2021-10-10 17:26:22 -04:00
|
|
|
use tokio::sync::mpsc;
|
|
|
|
use tokio::sync::oneshot;
|
|
|
|
|
2022-01-31 17:33:57 -05:00
|
|
|
type Request = (
|
|
|
|
Vec<(ModuleSpecifier, deno_graph::ModuleKind)>,
|
|
|
|
oneshot::Sender<Result<(), AnyError>>,
|
|
|
|
);
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
/// A "server" that handles requests from the language server to cache modules
|
|
|
|
/// in its own thread.
|
|
|
|
#[derive(Debug)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct CacheServer(mpsc::UnboundedSender<Request>);
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
impl CacheServer {
|
|
|
|
pub async fn new(
|
|
|
|
maybe_cache_path: Option<PathBuf>,
|
2021-11-08 20:26:39 -05:00
|
|
|
maybe_import_map: Option<Arc<ImportMap>>,
|
|
|
|
maybe_config_file: Option<ConfigFile>,
|
2022-01-23 19:27:52 -05:00
|
|
|
maybe_ca_stores: Option<Vec<String>>,
|
|
|
|
maybe_ca_file: Option<String>,
|
|
|
|
unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
2021-10-10 17:26:22 -04:00
|
|
|
) -> Self {
|
|
|
|
let (tx, mut rx) = mpsc::unbounded_channel::<Request>();
|
|
|
|
let _join_handle = thread::spawn(move || {
|
|
|
|
let runtime = create_basic_runtime();
|
|
|
|
runtime.block_on(async {
|
2022-02-11 14:04:31 -05:00
|
|
|
let ps = ProcState::build(Arc::new(Flags {
|
2021-10-10 17:26:22 -04:00
|
|
|
cache_path: maybe_cache_path,
|
2022-01-23 19:27:52 -05:00
|
|
|
ca_stores: maybe_ca_stores,
|
|
|
|
ca_file: maybe_ca_file,
|
|
|
|
unsafely_ignore_certificate_errors,
|
2021-10-10 17:26:22 -04:00
|
|
|
..Default::default()
|
2022-02-11 14:04:31 -05:00
|
|
|
}))
|
2021-10-10 17:26:22 -04:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-11-08 20:26:39 -05:00
|
|
|
let maybe_import_map_resolver =
|
|
|
|
maybe_import_map.map(ImportMapResolver::new);
|
2022-02-24 20:03:12 -05:00
|
|
|
let maybe_jsx_resolver = maybe_config_file.as_ref().and_then(|cf| {
|
|
|
|
cf.to_maybe_jsx_import_source_module()
|
|
|
|
.map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()))
|
|
|
|
});
|
2021-11-08 20:26:39 -05:00
|
|
|
let maybe_resolver = if maybe_jsx_resolver.is_some() {
|
|
|
|
maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
|
|
|
|
} else {
|
|
|
|
maybe_import_map_resolver
|
|
|
|
.as_ref()
|
|
|
|
.map(|im| im.as_resolver())
|
|
|
|
};
|
|
|
|
let maybe_imports = maybe_config_file
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|cf| cf.to_maybe_imports().ok())
|
2021-11-08 20:26:39 -05:00
|
|
|
.flatten();
|
2021-10-10 17:26:22 -04:00
|
|
|
let mut cache = FetchCacher::new(
|
|
|
|
ps.dir.gen_cache.clone(),
|
|
|
|
ps.file_fetcher.clone(),
|
|
|
|
Permissions::allow_all(),
|
|
|
|
Permissions::allow_all(),
|
|
|
|
);
|
|
|
|
|
|
|
|
while let Some((roots, tx)) = rx.recv().await {
|
|
|
|
let graph = deno_graph::create_graph(
|
|
|
|
roots,
|
|
|
|
false,
|
2021-11-08 20:26:39 -05:00
|
|
|
maybe_imports.clone(),
|
2022-06-20 17:59:52 -04:00
|
|
|
&mut cache,
|
2021-11-08 20:26:39 -05:00
|
|
|
maybe_resolver,
|
2021-10-10 17:26:22 -04:00
|
|
|
None,
|
|
|
|
None,
|
2022-01-13 11:58:00 -05:00
|
|
|
None,
|
2021-10-10 17:26:22 -04:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2021-12-22 08:25:06 -05:00
|
|
|
if tx.send(graph_valid(&graph, true, false)).is_err() {
|
2021-10-10 17:26:22 -04:00
|
|
|
log::warn!("cannot send to client");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
Self(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to cache the supplied module specifiers and their dependencies in
|
|
|
|
/// the current DENO_DIR, returning any errors, so they can be returned to the
|
|
|
|
/// client.
|
|
|
|
pub async fn cache(
|
|
|
|
&self,
|
2022-01-31 17:33:57 -05:00
|
|
|
roots: Vec<(ModuleSpecifier, deno_graph::ModuleKind)>,
|
2021-10-10 17:26:22 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let (tx, rx) = oneshot::channel::<Result<(), AnyError>>();
|
|
|
|
if self.0.send((roots, tx)).is_err() {
|
|
|
|
return Err(anyhow!("failed to send request to cache thread"));
|
|
|
|
}
|
|
|
|
rx.await?
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 21:04:26 -05:00
|
|
|
|
|
|
|
/// Calculate a version for for a given path.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn calculate_fs_version(path: &Path) -> Option<String> {
|
2022-02-01 21:04:26 -05:00
|
|
|
let metadata = fs::metadata(path).ok()?;
|
|
|
|
if let Ok(modified) = metadata.modified() {
|
|
|
|
if let Ok(n) = modified.duration_since(SystemTime::UNIX_EPOCH) {
|
|
|
|
Some(n.as_millis().to_string())
|
|
|
|
} else {
|
|
|
|
Some("1".to_string())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some("1".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Populate the metadata map based on the supplied headers
|
|
|
|
fn parse_metadata(
|
|
|
|
headers: &HashMap<String, String>,
|
|
|
|
) -> HashMap<MetadataKey, String> {
|
|
|
|
let mut metadata = HashMap::new();
|
|
|
|
if let Some(warning) = headers.get("x-deno-warning").cloned() {
|
|
|
|
metadata.insert(MetadataKey::Warning, warning);
|
|
|
|
}
|
|
|
|
metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub enum MetadataKey {
|
2022-02-01 21:04:26 -05:00
|
|
|
/// Represent the `x-deno-warning` header associated with the document
|
|
|
|
Warning,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Metadata {
|
|
|
|
values: Arc<HashMap<MetadataKey, String>>,
|
|
|
|
version: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct CacheMetadata {
|
2022-02-01 21:04:26 -05:00
|
|
|
cache: http_cache::HttpCache,
|
|
|
|
metadata: Arc<Mutex<HashMap<ModuleSpecifier, Metadata>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheMetadata {
|
|
|
|
pub fn new(location: &Path) -> Self {
|
|
|
|
Self {
|
|
|
|
cache: http_cache::HttpCache::new(location),
|
|
|
|
metadata: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the meta data associated with the specifier. Unlike the `get()`
|
|
|
|
/// method, redirects of the supplied specifier will not be followed.
|
|
|
|
pub fn get(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<Arc<HashMap<MetadataKey, String>>> {
|
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let version = self
|
|
|
|
.cache
|
|
|
|
.get_cache_filename(specifier)
|
2022-02-24 20:03:12 -05:00
|
|
|
.and_then(|ref path| calculate_fs_version(path));
|
2022-02-01 21:04:26 -05:00
|
|
|
let metadata = self.metadata.lock().get(specifier).cloned();
|
2022-02-24 20:03:12 -05:00
|
|
|
if metadata.as_ref().and_then(|m| m.version.clone()) != version {
|
2022-02-01 21:04:26 -05:00
|
|
|
self.refresh(specifier).map(|m| m.values)
|
|
|
|
} else {
|
|
|
|
metadata.map(|m| m.values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn refresh(&self, specifier: &ModuleSpecifier) -> Option<Metadata> {
|
|
|
|
if specifier.scheme() == "file" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let cache_filename = self.cache.get_cache_filename(specifier)?;
|
|
|
|
let specifier_metadata =
|
|
|
|
http_cache::Metadata::read(&cache_filename).ok()?;
|
|
|
|
let values = Arc::new(parse_metadata(&specifier_metadata.headers));
|
|
|
|
let version = calculate_fs_version(&cache_filename);
|
|
|
|
let mut metadata_map = self.metadata.lock();
|
|
|
|
let metadata = Metadata { values, version };
|
|
|
|
metadata_map.insert(specifier.clone(), metadata.clone());
|
|
|
|
Some(metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_location(&mut self, location: &Path) {
|
|
|
|
self.cache = http_cache::HttpCache::new(location);
|
|
|
|
self.metadata.lock().clear();
|
|
|
|
}
|
|
|
|
}
|