2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-09-28 08:11:12 -04:00
|
|
|
|
2023-01-14 23:06:46 -05:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
2022-09-28 08:11:12 -04:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use deno_core::error::AnyError;
|
2023-09-19 22:39:27 -04:00
|
|
|
use deno_core::op2;
|
2022-09-28 08:11:12 -04:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
2023-01-14 23:06:46 -05:00
|
|
|
use deno_core::ByteString;
|
2022-09-28 08:11:12 -04:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
2023-08-09 13:45:35 -04:00
|
|
|
|
2023-01-14 23:06:46 -05:00
|
|
|
mod sqlite;
|
|
|
|
pub use sqlite::SqliteBackedCache;
|
2022-09-28 08:11:12 -04:00
|
|
|
|
2022-10-03 01:22:54 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct CreateCache<C: Cache + 'static>(pub Arc<dyn Fn() -> C>);
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(deno_cache,
|
|
|
|
deps = [ deno_webidl, deno_web, deno_url, deno_fetch ],
|
|
|
|
parameters=[CA: Cache],
|
|
|
|
ops = [
|
|
|
|
op_cache_storage_open<CA>,
|
|
|
|
op_cache_storage_has<CA>,
|
|
|
|
op_cache_storage_delete<CA>,
|
|
|
|
op_cache_put<CA>,
|
2023-08-09 13:45:35 -04:00
|
|
|
op_cache_put_finish<CA>,
|
2023-03-17 14:22:15 -04:00
|
|
|
op_cache_match<CA>,
|
|
|
|
op_cache_delete<CA>,
|
|
|
|
],
|
|
|
|
esm = [ "01_cache.js" ],
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
2023-03-17 14:22:15 -04:00
|
|
|
maybe_create_cache: Option<CreateCache<CA>>,
|
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
|
|
|
if let Some(create_cache) = options.maybe_create_cache {
|
2023-03-17 14:22:15 -04:00
|
|
|
state.put(create_cache);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2022-10-03 01:22:54 -04:00
|
|
|
|
|
|
|
pub fn get_declaration() -> PathBuf {
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_cache.d.ts")
|
|
|
|
}
|
|
|
|
|
2022-09-28 08:11:12 -04:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CachePutRequest {
|
|
|
|
pub cache_id: i64,
|
|
|
|
pub request_url: String,
|
|
|
|
pub request_headers: Vec<(ByteString, ByteString)>,
|
|
|
|
pub response_headers: Vec<(ByteString, ByteString)>,
|
|
|
|
pub response_has_body: bool,
|
|
|
|
pub response_status: u16,
|
|
|
|
pub response_status_text: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CacheMatchRequest {
|
|
|
|
pub cache_id: i64,
|
|
|
|
pub request_url: String,
|
|
|
|
pub request_headers: Vec<(ByteString, ByteString)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CacheMatchResponse(CacheMatchResponseMeta, Option<ResourceId>);
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CacheMatchResponseMeta {
|
|
|
|
pub response_status: u16,
|
|
|
|
pub response_status_text: String,
|
|
|
|
pub request_headers: Vec<(ByteString, ByteString)>,
|
|
|
|
pub response_headers: Vec<(ByteString, ByteString)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CacheDeleteRequest {
|
|
|
|
pub cache_id: i64,
|
|
|
|
pub request_url: String,
|
|
|
|
}
|
|
|
|
|
2023-08-09 13:45:35 -04:00
|
|
|
#[async_trait(?Send)]
|
|
|
|
pub trait Cache: Clone + 'static {
|
|
|
|
type CachePutResourceType: Resource;
|
|
|
|
|
2022-09-28 08:11:12 -04:00
|
|
|
async fn storage_open(&self, cache_name: String) -> Result<i64, AnyError>;
|
|
|
|
async fn storage_has(&self, cache_name: String) -> Result<bool, AnyError>;
|
|
|
|
async fn storage_delete(&self, cache_name: String) -> Result<bool, AnyError>;
|
|
|
|
|
2023-08-09 13:45:35 -04:00
|
|
|
/// Create a put request.
|
|
|
|
async fn put_create(
|
2022-09-28 08:11:12 -04:00
|
|
|
&self,
|
|
|
|
request_response: CachePutRequest,
|
2023-08-09 13:45:35 -04:00
|
|
|
) -> Result<Option<Rc<Self::CachePutResourceType>>, AnyError>;
|
|
|
|
/// Complete a put request.
|
|
|
|
async fn put_finish(
|
|
|
|
&self,
|
|
|
|
resource: Rc<Self::CachePutResourceType>,
|
|
|
|
) -> Result<(), AnyError>;
|
2022-09-28 08:11:12 -04:00
|
|
|
async fn r#match(
|
|
|
|
&self,
|
|
|
|
request: CacheMatchRequest,
|
|
|
|
) -> Result<
|
|
|
|
Option<(CacheMatchResponseMeta, Option<Rc<dyn Resource>>)>,
|
|
|
|
AnyError,
|
|
|
|
>;
|
|
|
|
async fn delete(&self, request: CacheDeleteRequest)
|
|
|
|
-> Result<bool, AnyError>;
|
|
|
|
}
|
|
|
|
|
2023-09-23 08:04:47 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[number]
|
2022-09-28 08:11:12 -04:00
|
|
|
pub async fn op_cache_storage_open<CA>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-23 08:04:47 -04:00
|
|
|
#[string] cache_name: String,
|
2022-09-28 08:11:12 -04:00
|
|
|
) -> Result<i64, AnyError>
|
|
|
|
where
|
2023-08-09 13:45:35 -04:00
|
|
|
CA: Cache,
|
2022-09-28 08:11:12 -04:00
|
|
|
{
|
|
|
|
let cache = get_cache::<CA>(&state)?;
|
|
|
|
cache.storage_open(cache_name).await
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
2022-09-28 08:11:12 -04:00
|
|
|
pub async fn op_cache_storage_has<CA>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[string] cache_name: String,
|
2022-09-28 08:11:12 -04:00
|
|
|
) -> Result<bool, AnyError>
|
|
|
|
where
|
2023-08-09 13:45:35 -04:00
|
|
|
CA: Cache,
|
2022-09-28 08:11:12 -04:00
|
|
|
{
|
|
|
|
let cache = get_cache::<CA>(&state)?;
|
|
|
|
cache.storage_has(cache_name).await
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
2022-09-28 08:11:12 -04:00
|
|
|
pub async fn op_cache_storage_delete<CA>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[string] cache_name: String,
|
2022-09-28 08:11:12 -04:00
|
|
|
) -> Result<bool, AnyError>
|
|
|
|
where
|
2023-08-09 13:45:35 -04:00
|
|
|
CA: Cache,
|
2022-09-28 08:11:12 -04:00
|
|
|
{
|
|
|
|
let cache = get_cache::<CA>(&state)?;
|
|
|
|
cache.storage_delete(cache_name).await
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[smi]
|
2022-09-28 08:11:12 -04:00
|
|
|
pub async fn op_cache_put<CA>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[serde] request_response: CachePutRequest,
|
2022-09-28 08:11:12 -04:00
|
|
|
) -> Result<Option<ResourceId>, AnyError>
|
|
|
|
where
|
2023-08-09 13:45:35 -04:00
|
|
|
CA: Cache,
|
2022-09-28 08:11:12 -04:00
|
|
|
{
|
|
|
|
let cache = get_cache::<CA>(&state)?;
|
2023-08-09 13:45:35 -04:00
|
|
|
match cache.put_create(request_response).await? {
|
2022-09-28 08:11:12 -04:00
|
|
|
Some(resource) => {
|
|
|
|
let rid = state.borrow_mut().resource_table.add_rc_dyn(resource);
|
|
|
|
Ok(Some(rid))
|
|
|
|
}
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
2023-08-09 13:45:35 -04:00
|
|
|
pub async fn op_cache_put_finish<CA>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2023-08-09 13:45:35 -04:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
CA: Cache,
|
|
|
|
{
|
|
|
|
let cache = get_cache::<CA>(&state)?;
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<CA::CachePutResourceType>(rid)?;
|
|
|
|
cache.put_finish(resource).await
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-09-28 08:11:12 -04:00
|
|
|
pub async fn op_cache_match<CA>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[serde] request: CacheMatchRequest,
|
2022-09-28 08:11:12 -04:00
|
|
|
) -> Result<Option<CacheMatchResponse>, AnyError>
|
|
|
|
where
|
2023-08-09 13:45:35 -04:00
|
|
|
CA: Cache,
|
2022-09-28 08:11:12 -04:00
|
|
|
{
|
|
|
|
let cache = get_cache::<CA>(&state)?;
|
|
|
|
match cache.r#match(request).await? {
|
|
|
|
Some((meta, None)) => Ok(Some(CacheMatchResponse(meta, None))),
|
|
|
|
Some((meta, Some(resource))) => {
|
|
|
|
let rid = state.borrow_mut().resource_table.add_rc_dyn(resource);
|
|
|
|
Ok(Some(CacheMatchResponse(meta, Some(rid))))
|
|
|
|
}
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
2022-09-28 08:11:12 -04:00
|
|
|
pub async fn op_cache_delete<CA>(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[serde] request: CacheDeleteRequest,
|
2022-09-28 08:11:12 -04:00
|
|
|
) -> Result<bool, AnyError>
|
|
|
|
where
|
2023-08-09 13:45:35 -04:00
|
|
|
CA: Cache,
|
2022-09-28 08:11:12 -04:00
|
|
|
{
|
|
|
|
let cache = get_cache::<CA>(&state)?;
|
|
|
|
cache.delete(request).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_cache<CA>(state: &Rc<RefCell<OpState>>) -> Result<CA, AnyError>
|
|
|
|
where
|
2023-08-09 13:45:35 -04:00
|
|
|
CA: Cache,
|
2022-09-28 08:11:12 -04:00
|
|
|
{
|
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
if let Some(cache) = state.try_borrow::<CA>() {
|
|
|
|
Ok(cache.clone())
|
|
|
|
} else {
|
|
|
|
let create_cache = state.borrow::<CreateCache<CA>>().clone();
|
|
|
|
let cache = create_cache.0();
|
|
|
|
state.put(cache);
|
|
|
|
Ok(state.borrow::<CA>().clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 01:22:54 -04:00
|
|
|
/// Check if headers, mentioned in the vary header, of query request
|
|
|
|
/// and cached request are equal.
|
|
|
|
pub fn vary_header_matches(
|
|
|
|
vary_header: &ByteString,
|
|
|
|
query_request_headers: &[(ByteString, ByteString)],
|
|
|
|
cached_request_headers: &[(ByteString, ByteString)],
|
|
|
|
) -> bool {
|
|
|
|
let vary_header = match std::str::from_utf8(vary_header) {
|
|
|
|
Ok(vary_header) => vary_header,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
let headers = get_headers_from_vary_header(vary_header);
|
|
|
|
for header in headers {
|
|
|
|
let query_header = get_header(&header, query_request_headers);
|
|
|
|
let cached_header = get_header(&header, cached_request_headers);
|
|
|
|
if query_header != cached_header {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2022-09-28 08:11:12 -04:00
|
|
|
|
2022-10-03 01:22:54 -04:00
|
|
|
#[test]
|
|
|
|
fn test_vary_header_matches() {
|
|
|
|
let vary_header = ByteString::from("accept-encoding");
|
|
|
|
let query_request_headers = vec![(
|
|
|
|
ByteString::from("accept-encoding"),
|
|
|
|
ByteString::from("gzip"),
|
|
|
|
)];
|
|
|
|
let cached_request_headers = vec![(
|
|
|
|
ByteString::from("accept-encoding"),
|
|
|
|
ByteString::from("gzip"),
|
|
|
|
)];
|
|
|
|
assert!(vary_header_matches(
|
|
|
|
&vary_header,
|
|
|
|
&query_request_headers,
|
|
|
|
&cached_request_headers
|
|
|
|
));
|
|
|
|
let vary_header = ByteString::from("accept-encoding");
|
|
|
|
let query_request_headers = vec![(
|
|
|
|
ByteString::from("accept-encoding"),
|
|
|
|
ByteString::from("gzip"),
|
|
|
|
)];
|
|
|
|
let cached_request_headers =
|
|
|
|
vec![(ByteString::from("accept-encoding"), ByteString::from("br"))];
|
|
|
|
assert!(!vary_header_matches(
|
|
|
|
&vary_header,
|
|
|
|
&query_request_headers,
|
|
|
|
&cached_request_headers
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get headers from the vary header.
|
|
|
|
pub fn get_headers_from_vary_header(vary_header: &str) -> Vec<String> {
|
|
|
|
vary_header
|
|
|
|
.split(',')
|
|
|
|
.map(|s| s.trim().to_lowercase())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_headers_from_vary_header() {
|
|
|
|
let headers = get_headers_from_vary_header("accept-encoding");
|
|
|
|
assert_eq!(headers, vec!["accept-encoding"]);
|
|
|
|
let headers = get_headers_from_vary_header("accept-encoding, user-agent");
|
|
|
|
assert_eq!(headers, vec!["accept-encoding", "user-agent"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get value for the header with the given name.
|
|
|
|
pub fn get_header(
|
|
|
|
name: &str,
|
|
|
|
headers: &[(ByteString, ByteString)],
|
|
|
|
) -> Option<ByteString> {
|
|
|
|
headers
|
|
|
|
.iter()
|
|
|
|
.find(|(k, _)| {
|
|
|
|
if let Ok(k) = std::str::from_utf8(k) {
|
|
|
|
k.eq_ignore_ascii_case(name)
|
|
|
|
} else {
|
|
|
|
false
|
2022-09-28 08:11:12 -04:00
|
|
|
}
|
|
|
|
})
|
2022-10-03 01:22:54 -04:00
|
|
|
.map(|(_, v)| v.to_owned())
|
2022-09-28 08:11:12 -04:00
|
|
|
}
|
|
|
|
|
2022-10-03 01:22:54 -04:00
|
|
|
#[test]
|
|
|
|
fn test_get_header() {
|
|
|
|
let headers = vec![
|
|
|
|
(
|
|
|
|
ByteString::from("accept-encoding"),
|
|
|
|
ByteString::from("gzip"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
ByteString::from("content-type"),
|
|
|
|
ByteString::from("application/json"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
ByteString::from("vary"),
|
|
|
|
ByteString::from("accept-encoding"),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
let value = get_header("accept-encoding", &headers);
|
|
|
|
assert_eq!(value, Some(ByteString::from("gzip")));
|
|
|
|
let value = get_header("content-type", &headers);
|
|
|
|
assert_eq!(value, Some(ByteString::from("application/json")));
|
|
|
|
let value = get_header("vary", &headers);
|
|
|
|
assert_eq!(value, Some(ByteString::from("accept-encoding")));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serialize headers into bytes.
|
|
|
|
pub fn serialize_headers(headers: &[(ByteString, ByteString)]) -> Vec<u8> {
|
|
|
|
let mut serialized_headers = Vec::new();
|
|
|
|
for (name, value) in headers {
|
|
|
|
serialized_headers.extend_from_slice(name);
|
|
|
|
serialized_headers.extend_from_slice(b"\r\n");
|
|
|
|
serialized_headers.extend_from_slice(value);
|
|
|
|
serialized_headers.extend_from_slice(b"\r\n");
|
|
|
|
}
|
|
|
|
serialized_headers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserialize bytes into headers.
|
|
|
|
pub fn deserialize_headers(
|
|
|
|
serialized_headers: &[u8],
|
|
|
|
) -> Vec<(ByteString, ByteString)> {
|
|
|
|
let mut headers = Vec::new();
|
|
|
|
let mut piece = None;
|
|
|
|
let mut start = 0;
|
|
|
|
for (i, byte) in serialized_headers.iter().enumerate() {
|
|
|
|
if byte == &b'\r' && serialized_headers.get(i + 1) == Some(&b'\n') {
|
|
|
|
if piece.is_none() {
|
|
|
|
piece = Some(start..i);
|
|
|
|
} else {
|
|
|
|
let name = piece.unwrap();
|
|
|
|
let value = start..i;
|
|
|
|
headers.push((
|
|
|
|
ByteString::from(&serialized_headers[name]),
|
|
|
|
ByteString::from(&serialized_headers[value]),
|
|
|
|
));
|
|
|
|
piece = None;
|
|
|
|
}
|
|
|
|
start = i + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert!(piece.is_none());
|
|
|
|
assert_eq!(start, serialized_headers.len());
|
|
|
|
headers
|
2022-09-28 08:11:12 -04:00
|
|
|
}
|