2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
use crate::cache::DenoDir;
|
|
|
|
use crate::cache::GlobalHttpCache;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::cache::HttpCache;
|
2024-05-09 15:22:27 -04:00
|
|
|
use crate::cache::LocalLspHttpCache;
|
|
|
|
use crate::lsp::config::Config;
|
|
|
|
use crate::lsp::logging::lsp_log;
|
|
|
|
use crate::lsp::logging::lsp_warn;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
use deno_core::url::Url;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2024-09-28 07:55:01 -04:00
|
|
|
use deno_path_util::url_to_file_path;
|
2024-06-17 16:54:23 -04:00
|
|
|
use std::collections::BTreeMap;
|
2022-02-01 21:04:26 -05:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
2021-11-08 20:26:39 -05:00
|
|
|
use std::sync::Arc;
|
2022-02-01 21:04:26 -05:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
pub fn calculate_fs_version(
|
2024-05-09 15:22:27 -04:00
|
|
|
cache: &LspCache,
|
2023-07-08 16:06:45 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2023-07-08 16:06:45 -04:00
|
|
|
) -> Option<String> {
|
|
|
|
match specifier.scheme() {
|
|
|
|
"npm" | "node" | "data" | "blob" => None,
|
2024-09-28 07:55:01 -04:00
|
|
|
"file" => url_to_file_path(specifier)
|
2023-07-08 16:06:45 -04:00
|
|
|
.ok()
|
|
|
|
.and_then(|path| calculate_fs_version_at_path(&path)),
|
2024-06-17 16:54:23 -04:00
|
|
|
_ => calculate_fs_version_in_cache(cache, specifier, file_referrer),
|
2023-07-08 16:06:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 21:04:26 -05:00
|
|
|
/// Calculate a version for for a given path.
|
2023-07-08 16:06:45 -04:00
|
|
|
pub fn calculate_fs_version_at_path(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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
fn calculate_fs_version_in_cache(
|
2024-05-09 15:22:27 -04:00
|
|
|
cache: &LspCache,
|
2023-07-08 16:06:45 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-06-17 16:54:23 -04:00
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
2023-07-08 16:06:45 -04:00
|
|
|
) -> Option<String> {
|
2024-06-17 16:54:23 -04:00
|
|
|
let http_cache = cache.for_specifier(file_referrer);
|
2024-05-09 15:22:27 -04:00
|
|
|
let Ok(cache_key) = http_cache.cache_item_key(specifier) else {
|
2023-08-01 20:49:09 -04:00
|
|
|
return Some("1".to_string());
|
|
|
|
};
|
2024-05-09 15:22:27 -04:00
|
|
|
match http_cache.read_modified_time(&cache_key) {
|
2023-07-08 16:06:45 -04:00
|
|
|
Ok(Some(modified)) => {
|
|
|
|
match modified.duration_since(SystemTime::UNIX_EPOCH) {
|
|
|
|
Ok(n) => Some(n.as_millis().to_string()),
|
|
|
|
Err(_) => Some("1".to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None) => None,
|
|
|
|
Err(_) => Some("1".to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 21:04:26 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2024-05-09 15:22:27 -04:00
|
|
|
pub struct LspCache {
|
|
|
|
deno_dir: DenoDir,
|
|
|
|
global: Arc<GlobalHttpCache>,
|
2024-06-17 16:54:23 -04:00
|
|
|
vendors_by_scope: BTreeMap<ModuleSpecifier, Option<Arc<LocalLspHttpCache>>>,
|
2022-02-01 21:04:26 -05:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
impl Default for LspCache {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(None)
|
|
|
|
}
|
2022-02-01 21:04:26 -05:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
impl LspCache {
|
|
|
|
pub fn new(global_cache_url: Option<Url>) -> Self {
|
|
|
|
let global_cache_path = global_cache_url.and_then(|s| {
|
2024-09-28 07:55:01 -04:00
|
|
|
url_to_file_path(&s)
|
2024-05-09 15:22:27 -04:00
|
|
|
.inspect(|p| {
|
|
|
|
lsp_log!("Resolved global cache path: \"{}\"", p.to_string_lossy());
|
|
|
|
})
|
|
|
|
.inspect_err(|err| {
|
|
|
|
lsp_warn!("Failed to resolve custom cache path: {err}");
|
|
|
|
})
|
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
let deno_dir = DenoDir::new(global_cache_path)
|
|
|
|
.expect("should be infallible with absolute custom root");
|
|
|
|
let global = Arc::new(GlobalHttpCache::new(
|
2024-10-01 14:05:40 -04:00
|
|
|
deno_dir.remote_folder_path(),
|
2024-05-09 15:22:27 -04:00
|
|
|
crate::cache::RealDenoCacheEnv,
|
|
|
|
));
|
2022-02-01 21:04:26 -05:00
|
|
|
Self {
|
2024-05-09 15:22:27 -04:00
|
|
|
deno_dir,
|
|
|
|
global,
|
2024-06-17 16:54:23 -04:00
|
|
|
vendors_by_scope: Default::default(),
|
2022-02-01 21:04:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
pub fn update_config(&mut self, config: &Config) {
|
2024-06-17 16:54:23 -04:00
|
|
|
self.vendors_by_scope = config
|
|
|
|
.tree
|
|
|
|
.data_by_scope()
|
|
|
|
.iter()
|
|
|
|
.map(|(scope, config_data)| {
|
|
|
|
(
|
|
|
|
scope.clone(),
|
|
|
|
config_data.vendor_dir.as_ref().map(|v| {
|
|
|
|
Arc::new(LocalLspHttpCache::new(v.clone(), self.global.clone()))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
2022-02-01 21:04:26 -05:00
|
|
|
}
|
|
|
|
|
2024-05-09 15:22:27 -04:00
|
|
|
pub fn deno_dir(&self) -> &DenoDir {
|
|
|
|
&self.deno_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn global(&self) -> &Arc<GlobalHttpCache> {
|
|
|
|
&self.global
|
|
|
|
}
|
|
|
|
|
2024-06-17 16:54:23 -04:00
|
|
|
pub fn for_specifier(
|
|
|
|
&self,
|
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> Arc<dyn HttpCache> {
|
|
|
|
let Some(file_referrer) = file_referrer else {
|
|
|
|
return self.global.clone();
|
|
|
|
};
|
2024-05-09 15:22:27 -04:00
|
|
|
self
|
2024-06-17 16:54:23 -04:00
|
|
|
.vendors_by_scope
|
|
|
|
.iter()
|
|
|
|
.rfind(|(s, _)| file_referrer.as_str().starts_with(s.as_str()))
|
|
|
|
.and_then(|(_, v)| v.clone().map(|v| v as _))
|
2024-05-09 15:22:27 -04:00
|
|
|
.unwrap_or(self.global.clone() as _)
|
2022-02-01 21:04:26 -05:00
|
|
|
}
|
2024-06-17 16:54:23 -04:00
|
|
|
|
|
|
|
pub fn vendored_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
file_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
|
|
|
let file_referrer = file_referrer?;
|
|
|
|
if !matches!(specifier.scheme(), "http" | "https") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let vendor = self
|
|
|
|
.vendors_by_scope
|
|
|
|
.iter()
|
|
|
|
.rfind(|(s, _)| file_referrer.as_str().starts_with(s.as_str()))?
|
|
|
|
.1
|
|
|
|
.as_ref()?;
|
|
|
|
vendor.get_file_url(specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unvendored_specifier(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<ModuleSpecifier> {
|
2024-09-28 07:55:01 -04:00
|
|
|
let path = url_to_file_path(specifier).ok()?;
|
2024-06-17 16:54:23 -04:00
|
|
|
let vendor = self
|
|
|
|
.vendors_by_scope
|
|
|
|
.iter()
|
|
|
|
.rfind(|(s, _)| specifier.as_str().starts_with(s.as_str()))?
|
|
|
|
.1
|
|
|
|
.as_ref()?;
|
|
|
|
vendor.get_remote_url(&path)
|
|
|
|
}
|
2024-06-28 13:18:21 -04:00
|
|
|
|
|
|
|
pub fn is_valid_file_referrer(&self, specifier: &ModuleSpecifier) -> bool {
|
2024-09-28 07:55:01 -04:00
|
|
|
if let Ok(path) = url_to_file_path(specifier) {
|
2024-06-28 13:18:21 -04:00
|
|
|
if !path.starts_with(&self.deno_dir().root) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2022-02-01 21:04:26 -05:00
|
|
|
}
|