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
|
|
|
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::cache::HttpCache;
|
2023-07-08 16:06:45 -04:00
|
|
|
use crate::util::path::specifier_to_file_path;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
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;
|
2022-02-01 21:04:26 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
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;
|
|
|
|
|
2024-02-15 14:49:35 -05:00
|
|
|
/// In the LSP, we disallow the cache from automatically copying from
|
|
|
|
/// the global cache to the local cache for technical reasons.
|
|
|
|
///
|
|
|
|
/// 1. We need to verify the checksums from the lockfile are correct when
|
|
|
|
/// moving from the global to the local cache.
|
|
|
|
/// 2. We need to verify the checksums for JSR https specifiers match what
|
|
|
|
/// is found in the package's manifest.
|
|
|
|
pub const LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY: deno_cache_dir::GlobalToLocalCopy =
|
|
|
|
deno_cache_dir::GlobalToLocalCopy::Disallow;
|
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
pub fn calculate_fs_version(
|
2023-08-01 20:49:09 -04:00
|
|
|
cache: &Arc<dyn HttpCache>,
|
2023-07-08 16:06:45 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
|
|
|
match specifier.scheme() {
|
|
|
|
"npm" | "node" | "data" | "blob" => None,
|
|
|
|
"file" => specifier_to_file_path(specifier)
|
|
|
|
.ok()
|
|
|
|
.and_then(|path| calculate_fs_version_at_path(&path)),
|
|
|
|
_ => calculate_fs_version_in_cache(cache, specifier),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
2023-08-01 20:49:09 -04:00
|
|
|
cache: &Arc<dyn HttpCache>,
|
2023-07-08 16:06:45 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<String> {
|
2023-08-01 20:49:09 -04:00
|
|
|
let Ok(cache_key) = cache.cache_item_key(specifier) else {
|
|
|
|
return Some("1".to_string());
|
|
|
|
};
|
|
|
|
match 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
|
|
|
/// 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>,
|
|
|
|
}
|
|
|
|
|
2023-07-08 16:06:45 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct CacheMetadata {
|
2023-08-01 20:49:09 -04:00
|
|
|
cache: Arc<dyn HttpCache>,
|
2022-02-01 21:04:26 -05:00
|
|
|
metadata: Arc<Mutex<HashMap<ModuleSpecifier, Metadata>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheMetadata {
|
2023-08-01 20:49:09 -04:00
|
|
|
pub fn new(cache: Arc<dyn HttpCache>) -> Self {
|
2022-02-01 21:04:26 -05:00
|
|
|
Self {
|
2023-01-25 16:51:04 -05:00
|
|
|
cache,
|
2022-02-01 21:04:26 -05:00
|
|
|
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>>> {
|
2023-07-01 18:52:30 -04:00
|
|
|
if matches!(
|
|
|
|
specifier.scheme(),
|
|
|
|
"file" | "npm" | "node" | "data" | "blob"
|
|
|
|
) {
|
2022-02-01 21:04:26 -05:00
|
|
|
return None;
|
|
|
|
}
|
2023-07-08 16:06:45 -04:00
|
|
|
let version = calculate_fs_version_in_cache(&self.cache, specifier);
|
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> {
|
2023-07-01 18:52:30 -04:00
|
|
|
if matches!(
|
|
|
|
specifier.scheme(),
|
|
|
|
"file" | "npm" | "node" | "data" | "blob"
|
|
|
|
) {
|
2022-02-01 21:04:26 -05:00
|
|
|
return None;
|
|
|
|
}
|
2023-08-01 20:49:09 -04:00
|
|
|
let cache_key = self.cache.cache_item_key(specifier).ok()?;
|
2024-02-15 14:49:35 -05:00
|
|
|
let headers = self.cache.read_headers(&cache_key).ok()??;
|
|
|
|
let values = Arc::new(parse_metadata(&headers));
|
2023-07-08 16:06:45 -04:00
|
|
|
let version = calculate_fs_version_in_cache(&self.cache, specifier);
|
2022-02-01 21:04:26 -05:00
|
|
|
let mut metadata_map = self.metadata.lock();
|
|
|
|
let metadata = Metadata { values, version };
|
|
|
|
metadata_map.insert(specifier.clone(), metadata.clone());
|
|
|
|
Some(metadata)
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:49:09 -04:00
|
|
|
pub fn set_cache(&mut self, cache: Arc<dyn HttpCache>) {
|
|
|
|
self.cache = cache;
|
2022-02-01 21:04:26 -05:00
|
|
|
self.metadata.lock().clear();
|
|
|
|
}
|
|
|
|
}
|