2023-08-01 20:49:09 -04:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
|
|
|
use deno_core::url::Url;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
use crate::http_util::HeadersMap;
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
mod global;
|
|
|
|
mod local;
|
|
|
|
|
|
|
|
pub use global::url_to_filename;
|
|
|
|
pub use global::GlobalHttpCache;
|
|
|
|
pub use local::LocalHttpCache;
|
2023-08-02 16:57:25 -04:00
|
|
|
pub use local::LocalLspHttpCache;
|
2023-08-01 20:49:09 -04:00
|
|
|
|
|
|
|
/// Cached metadata about a url.
|
|
|
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
pub struct CachedUrlMetadata {
|
|
|
|
pub headers: HeadersMap,
|
|
|
|
pub url: String,
|
|
|
|
#[serde(default = "SystemTime::now", rename = "now")]
|
|
|
|
pub time: SystemTime,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CachedUrlMetadata {
|
|
|
|
pub fn is_redirect(&self) -> bool {
|
|
|
|
self.headers.contains_key("location")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computed cache key, which can help reduce the work of computing the cache key multiple times.
|
|
|
|
pub struct HttpCacheItemKey<'a> {
|
|
|
|
// The key is specific to the implementation of HttpCache,
|
|
|
|
// so keep these private to the module. For example, the
|
|
|
|
// fact that these may be stored in a file is an implementation
|
|
|
|
// detail.
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
pub(super) is_local_key: bool,
|
|
|
|
pub(super) url: &'a Url,
|
|
|
|
/// This will be set all the time for the global cache, but it
|
|
|
|
/// won't ever be set for the local cache because that also needs
|
|
|
|
/// header information to determine the final path.
|
|
|
|
pub(super) file_path: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait HttpCache: Send + Sync + std::fmt::Debug {
|
|
|
|
/// A pre-computed key for looking up items in the cache.
|
|
|
|
fn cache_item_key<'a>(
|
|
|
|
&self,
|
|
|
|
url: &'a Url,
|
|
|
|
) -> Result<HttpCacheItemKey<'a>, AnyError>;
|
|
|
|
|
|
|
|
fn contains(&self, url: &Url) -> bool;
|
|
|
|
fn set(
|
|
|
|
&self,
|
|
|
|
url: &Url,
|
|
|
|
headers: HeadersMap,
|
|
|
|
content: &[u8],
|
|
|
|
) -> Result<(), AnyError>;
|
|
|
|
fn read_modified_time(
|
|
|
|
&self,
|
|
|
|
key: &HttpCacheItemKey,
|
|
|
|
) -> Result<Option<SystemTime>, AnyError>;
|
|
|
|
fn read_file_bytes(
|
|
|
|
&self,
|
|
|
|
key: &HttpCacheItemKey,
|
|
|
|
) -> Result<Option<Vec<u8>>, AnyError>;
|
|
|
|
fn read_metadata(
|
|
|
|
&self,
|
|
|
|
key: &HttpCacheItemKey,
|
|
|
|
) -> Result<Option<CachedUrlMetadata>, AnyError>;
|
|
|
|
}
|