2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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::CachedUrlMetadata;
|
|
|
|
use crate::cache::HttpCache;
|
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;
|
2023-06-10 11:09:45 -04:00
|
|
|
use std::path::PathBuf;
|
2021-11-08 20:26:39 -05:00
|
|
|
use std::sync::Arc;
|
2022-02-01 21:04:26 -05:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
/// 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-11-28 17:28:54 -05:00
|
|
|
cache: HttpCache,
|
2022-02-01 21:04:26 -05:00
|
|
|
metadata: Arc<Mutex<HashMap<ModuleSpecifier, Metadata>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CacheMetadata {
|
2023-01-25 16:51:04 -05:00
|
|
|
pub fn new(cache: 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-01-24 09:05:54 -05:00
|
|
|
if matches!(specifier.scheme(), "file" | "npm" | "node") {
|
2022-02-01 21:04:26 -05:00
|
|
|
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> {
|
2023-02-22 14:15:25 -05:00
|
|
|
if matches!(specifier.scheme(), "file" | "npm" | "node") {
|
2022-02-01 21:04:26 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let cache_filename = self.cache.get_cache_filename(specifier)?;
|
2022-11-28 17:28:54 -05:00
|
|
|
let specifier_metadata = CachedUrlMetadata::read(&cache_filename).ok()?;
|
2022-02-01 21:04:26 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-06-10 11:09:45 -04:00
|
|
|
pub fn set_location(&mut self, location: PathBuf) {
|
2022-11-28 17:28:54 -05:00
|
|
|
self.cache = HttpCache::new(location);
|
2022-02-01 21:04:26 -05:00
|
|
|
self.metadata.lock().clear();
|
|
|
|
}
|
|
|
|
}
|