mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 04:52:26 -05:00
need to add a flag
This commit is contained in:
parent
d62e31af02
commit
cb1cfb8442
7 changed files with 268 additions and 186 deletions
|
@ -303,6 +303,7 @@ impl<TSys: DenoLibSys> LibWorkerFactorySharedState<TSys> {
|
|||
worker_type: args.worker_type,
|
||||
stdio: stdio.clone(),
|
||||
cache_storage_dir,
|
||||
use_lsc_cache: false,
|
||||
strace_ops: shared.options.strace_ops.clone(),
|
||||
close_on_idle: args.close_on_idle,
|
||||
maybe_worker_metadata: args.maybe_worker_metadata,
|
||||
|
@ -462,6 +463,7 @@ impl<TSys: DenoLibSys> LibMainWorkerFactory<TSys> {
|
|||
should_wait_for_inspector_session: shared.options.inspect_wait,
|
||||
strace_ops: shared.options.strace_ops.clone(),
|
||||
cache_storage_dir,
|
||||
use_lsc_cache: false,
|
||||
origin_storage_dir,
|
||||
stdio,
|
||||
skip_op_registration: shared.options.skip_op_registration,
|
||||
|
|
215
ext/cache/lib.rs
vendored
215
ext/cache/lib.rs
vendored
|
@ -1,19 +1,27 @@
|
|||
// Copyright 2018-2025 the Deno authors. MIT license.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::path::PathBuf;
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use bytes::Bytes;
|
||||
use deno_core::op2;
|
||||
use deno_core::serde::Deserialize;
|
||||
use deno_core::serde::Serialize;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::ByteString;
|
||||
use deno_core::OpState;
|
||||
use deno_core::Resource;
|
||||
use deno_core::ResourceId;
|
||||
use deno_error::JsErrorBox;
|
||||
use futures::Stream;
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
mod lsc_shard;
|
||||
mod lscache;
|
||||
|
@ -22,6 +30,7 @@ mod sqlite;
|
|||
pub use lsc_shard::CacheShard;
|
||||
pub use lscache::LscBackend;
|
||||
pub use sqlite::SqliteBackedCache;
|
||||
use tokio_util::io::StreamReader;
|
||||
|
||||
#[derive(Debug, thiserror::Error, deno_error::JsError)]
|
||||
pub enum CacheError {
|
||||
|
@ -83,24 +92,21 @@ pub enum CacheError {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CreateCache<C: Cache + 'static>(
|
||||
pub Arc<dyn Fn() -> Result<C, CacheError>>,
|
||||
);
|
||||
pub struct CreateCache(pub Arc<dyn Fn() -> Result<CacheImpl, CacheError>>);
|
||||
|
||||
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>,
|
||||
op_cache_match<CA>,
|
||||
op_cache_delete<CA>,
|
||||
op_cache_storage_open,
|
||||
op_cache_storage_has,
|
||||
op_cache_storage_delete,
|
||||
op_cache_put,
|
||||
op_cache_match,
|
||||
op_cache_delete,
|
||||
],
|
||||
esm = [ "01_cache.js" ],
|
||||
options = {
|
||||
maybe_create_cache: Option<CreateCache<CA>>,
|
||||
maybe_create_cache: Option<CreateCache>,
|
||||
},
|
||||
state = |state, options| {
|
||||
if let Some(create_cache) = options.maybe_create_cache {
|
||||
|
@ -184,52 +190,160 @@ pub trait Cache: Clone + 'static {
|
|||
) -> Result<bool, CacheError>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum CacheImpl {
|
||||
Sqlite(SqliteBackedCache),
|
||||
Lsc(LscBackend),
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Cache for CacheImpl {
|
||||
type CacheMatchResourceType = CacheResponseResource;
|
||||
|
||||
async fn storage_open(&self, cache_name: String) -> Result<i64, CacheError> {
|
||||
match self {
|
||||
Self::Sqlite(cache) => cache.storage_open(cache_name).await,
|
||||
Self::Lsc(cache) => cache.storage_open(cache_name).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn storage_has(&self, cache_name: String) -> Result<bool, CacheError> {
|
||||
match self {
|
||||
Self::Sqlite(cache) => cache.storage_has(cache_name).await,
|
||||
Self::Lsc(cache) => cache.storage_has(cache_name).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn storage_delete(
|
||||
&self,
|
||||
cache_name: String,
|
||||
) -> Result<bool, CacheError> {
|
||||
match self {
|
||||
Self::Sqlite(cache) => cache.storage_delete(cache_name).await,
|
||||
Self::Lsc(cache) => cache.storage_delete(cache_name).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn put(
|
||||
&self,
|
||||
request_response: CachePutRequest,
|
||||
resource: Option<Rc<dyn Resource>>,
|
||||
) -> Result<(), CacheError> {
|
||||
match self {
|
||||
Self::Sqlite(cache) => cache.put(request_response, resource).await,
|
||||
Self::Lsc(cache) => cache.put(request_response, resource).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn r#match(
|
||||
&self,
|
||||
request: CacheMatchRequest,
|
||||
) -> Result<
|
||||
Option<(CacheMatchResponseMeta, Option<Self::CacheMatchResourceType>)>,
|
||||
CacheError,
|
||||
> {
|
||||
match self {
|
||||
Self::Sqlite(cache) => cache.r#match(request).await,
|
||||
Self::Lsc(cache) => cache.r#match(request).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete(
|
||||
&self,
|
||||
request: CacheDeleteRequest,
|
||||
) -> Result<bool, CacheError> {
|
||||
match self {
|
||||
Self::Sqlite(cache) => cache.delete(request).await,
|
||||
Self::Lsc(cache) => cache.delete(request).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum CacheResponseResource {
|
||||
Sqlite(AsyncRefCell<tokio::fs::File>),
|
||||
Lsc(AsyncRefCell<Pin<Box<dyn AsyncRead>>>),
|
||||
}
|
||||
|
||||
impl CacheResponseResource {
|
||||
fn sqlite(file: tokio::fs::File) -> Self {
|
||||
Self::Sqlite(AsyncRefCell::new(file))
|
||||
}
|
||||
|
||||
fn lsc(
|
||||
body: impl Stream<Item = Result<Bytes, std::io::Error>> + 'static,
|
||||
) -> Self {
|
||||
Self::Lsc(AsyncRefCell::new(Box::pin(StreamReader::new(body))))
|
||||
}
|
||||
|
||||
async fn read(
|
||||
self: Rc<Self>,
|
||||
data: &mut [u8],
|
||||
) -> Result<usize, std::io::Error> {
|
||||
let nread = match &*self {
|
||||
CacheResponseResource::Sqlite(_) => {
|
||||
let resource = deno_core::RcRef::map(&self, |r| match r {
|
||||
Self::Sqlite(r) => r,
|
||||
_ => unreachable!(),
|
||||
});
|
||||
let mut file = resource.borrow_mut().await;
|
||||
file.read(data).await?
|
||||
}
|
||||
CacheResponseResource::Lsc(_) => {
|
||||
let resource = deno_core::RcRef::map(&self, |r| match r {
|
||||
Self::Lsc(r) => r,
|
||||
_ => unreachable!(),
|
||||
});
|
||||
let mut file = resource.borrow_mut().await;
|
||||
file.read(data).await?
|
||||
}
|
||||
};
|
||||
|
||||
Ok(nread)
|
||||
}
|
||||
}
|
||||
|
||||
impl Resource for CacheResponseResource {
|
||||
deno_core::impl_readable_byob!();
|
||||
|
||||
fn name(&self) -> Cow<str> {
|
||||
"CacheResponseResource".into()
|
||||
}
|
||||
}
|
||||
|
||||
#[op2(async)]
|
||||
#[number]
|
||||
pub async fn op_cache_storage_open<CA>(
|
||||
pub async fn op_cache_storage_open(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
#[string] cache_name: String,
|
||||
) -> Result<i64, CacheError>
|
||||
where
|
||||
CA: Cache,
|
||||
{
|
||||
let cache = get_cache::<CA>(&state)?;
|
||||
) -> Result<i64, CacheError> {
|
||||
let cache = get_cache(&state)?;
|
||||
cache.storage_open(cache_name).await
|
||||
}
|
||||
|
||||
#[op2(async)]
|
||||
pub async fn op_cache_storage_has<CA>(
|
||||
pub async fn op_cache_storage_has(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
#[string] cache_name: String,
|
||||
) -> Result<bool, CacheError>
|
||||
where
|
||||
CA: Cache,
|
||||
{
|
||||
let cache = get_cache::<CA>(&state)?;
|
||||
) -> Result<bool, CacheError> {
|
||||
let cache = get_cache(&state)?;
|
||||
cache.storage_has(cache_name).await
|
||||
}
|
||||
|
||||
#[op2(async)]
|
||||
pub async fn op_cache_storage_delete<CA>(
|
||||
pub async fn op_cache_storage_delete(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
#[string] cache_name: String,
|
||||
) -> Result<bool, CacheError>
|
||||
where
|
||||
CA: Cache,
|
||||
{
|
||||
let cache = get_cache::<CA>(&state)?;
|
||||
) -> Result<bool, CacheError> {
|
||||
let cache = get_cache(&state)?;
|
||||
cache.storage_delete(cache_name).await
|
||||
}
|
||||
|
||||
#[op2(async)]
|
||||
pub async fn op_cache_put<CA>(
|
||||
pub async fn op_cache_put(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
#[serde] request_response: CachePutRequest,
|
||||
) -> Result<(), CacheError>
|
||||
where
|
||||
CA: Cache,
|
||||
{
|
||||
let cache = get_cache::<CA>(&state)?;
|
||||
) -> Result<(), CacheError> {
|
||||
let cache = get_cache(&state)?;
|
||||
let resource = match request_response.response_rid {
|
||||
Some(rid) => Some(
|
||||
state
|
||||
|
@ -245,14 +359,11 @@ where
|
|||
|
||||
#[op2(async)]
|
||||
#[serde]
|
||||
pub async fn op_cache_match<CA>(
|
||||
pub async fn op_cache_match(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
#[serde] request: CacheMatchRequest,
|
||||
) -> Result<Option<CacheMatchResponse>, CacheError>
|
||||
where
|
||||
CA: Cache,
|
||||
{
|
||||
let cache = get_cache::<CA>(&state)?;
|
||||
) -> Result<Option<CacheMatchResponse>, CacheError> {
|
||||
let cache = get_cache(&state)?;
|
||||
match cache.r#match(request).await? {
|
||||
Some((meta, None)) => Ok(Some(CacheMatchResponse(meta, None))),
|
||||
Some((meta, Some(resource))) => {
|
||||
|
@ -264,28 +375,24 @@ where
|
|||
}
|
||||
|
||||
#[op2(async)]
|
||||
pub async fn op_cache_delete<CA>(
|
||||
pub async fn op_cache_delete(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
#[serde] request: CacheDeleteRequest,
|
||||
) -> Result<bool, CacheError>
|
||||
where
|
||||
CA: Cache,
|
||||
{
|
||||
let cache = get_cache::<CA>(&state)?;
|
||||
) -> Result<bool, CacheError> {
|
||||
let cache = get_cache(&state)?;
|
||||
cache.delete(request).await
|
||||
}
|
||||
|
||||
pub fn get_cache<CA>(state: &Rc<RefCell<OpState>>) -> Result<CA, CacheError>
|
||||
where
|
||||
CA: Cache,
|
||||
{
|
||||
pub fn get_cache(
|
||||
state: &Rc<RefCell<OpState>>,
|
||||
) -> Result<CacheImpl, CacheError> {
|
||||
let mut state = state.borrow_mut();
|
||||
if let Some(cache) = state.try_borrow::<CA>() {
|
||||
if let Some(cache) = state.try_borrow::<CacheImpl>() {
|
||||
Ok(cache.clone())
|
||||
} else if let Some(create_cache) = state.try_borrow::<CreateCache<CA>>() {
|
||||
} else if let Some(create_cache) = state.try_borrow::<CreateCache>() {
|
||||
let cache = create_cache.0()?;
|
||||
state.put(cache);
|
||||
Ok(state.borrow::<CA>().clone())
|
||||
Ok(state.borrow::<CacheImpl>().clone())
|
||||
} else {
|
||||
Err(CacheError::ContextUnsupported)
|
||||
}
|
||||
|
|
69
ext/cache/lscache.rs
vendored
69
ext/cache/lscache.rs
vendored
|
@ -1,21 +1,15 @@
|
|||
// Copyright 2018-2025 the Deno authors. MIT license.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
|
||||
use async_stream::try_stream;
|
||||
use async_trait::async_trait;
|
||||
use base64::Engine;
|
||||
use bytes::Bytes;
|
||||
use deno_core::unsync::spawn;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::BufMutView;
|
||||
use deno_core::ByteString;
|
||||
use deno_core::Resource;
|
||||
use futures::Stream;
|
||||
use futures::StreamExt;
|
||||
use futures::TryStreamExt;
|
||||
use http::header::VARY;
|
||||
|
@ -23,19 +17,16 @@ use http::HeaderMap;
|
|||
use http::HeaderName;
|
||||
use http::HeaderValue;
|
||||
use slab::Slab;
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio_util::io::StreamReader;
|
||||
|
||||
use crate::get_header;
|
||||
use crate::get_headers_from_vary_header;
|
||||
use crate::lsc_shard::CacheShard;
|
||||
use crate::Cache;
|
||||
use crate::CacheDeleteRequest;
|
||||
use crate::CacheError;
|
||||
use crate::CacheMatchRequest;
|
||||
use crate::CacheMatchResponseMeta;
|
||||
use crate::CachePutRequest;
|
||||
use crate::CacheResponseResource;
|
||||
|
||||
const REQHDR_PREFIX: &str = "x-lsc-meta-reqhdr-";
|
||||
|
||||
|
@ -51,13 +42,14 @@ impl LscBackend {
|
|||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Cache for LscBackend {
|
||||
type CacheMatchResourceType = CacheResponseResource;
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
impl LscBackend {
|
||||
/// Open a cache storage. Internally, this allocates an id and maps it
|
||||
/// to the provided cache name.
|
||||
async fn storage_open(&self, cache_name: String) -> Result<i64, CacheError> {
|
||||
pub async fn storage_open(
|
||||
&self,
|
||||
cache_name: String,
|
||||
) -> Result<i64, CacheError> {
|
||||
if cache_name.is_empty() {
|
||||
return Err(CacheError::EmptyName);
|
||||
}
|
||||
|
@ -66,12 +58,15 @@ impl Cache for LscBackend {
|
|||
}
|
||||
|
||||
/// Check if a cache with the provided name exists. Always returns `true`.
|
||||
async fn storage_has(&self, _cache_name: String) -> Result<bool, CacheError> {
|
||||
pub async fn storage_has(
|
||||
&self,
|
||||
_cache_name: String,
|
||||
) -> Result<bool, CacheError> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Delete a cache storage. Not yet implemented.
|
||||
async fn storage_delete(
|
||||
pub async fn storage_delete(
|
||||
&self,
|
||||
_cache_name: String,
|
||||
) -> Result<bool, CacheError> {
|
||||
|
@ -79,7 +74,7 @@ impl Cache for LscBackend {
|
|||
}
|
||||
|
||||
/// Writes an entry to the cache.
|
||||
async fn put(
|
||||
pub async fn put(
|
||||
&self,
|
||||
request_response: CachePutRequest,
|
||||
resource: Option<Rc<dyn Resource>>,
|
||||
|
@ -150,7 +145,7 @@ impl Cache for LscBackend {
|
|||
}
|
||||
|
||||
/// Matches a request against the cache.
|
||||
async fn r#match(
|
||||
pub async fn r#match(
|
||||
&self,
|
||||
request: CacheMatchRequest,
|
||||
) -> Result<
|
||||
|
@ -246,7 +241,7 @@ impl Cache for LscBackend {
|
|||
response_headers,
|
||||
};
|
||||
|
||||
let body = CacheResponseResource::new(
|
||||
let body = CacheResponseResource::lsc(
|
||||
res
|
||||
.bytes_stream()
|
||||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e)),
|
||||
|
@ -255,7 +250,7 @@ impl Cache for LscBackend {
|
|||
Ok(Some((meta, Some(body))))
|
||||
}
|
||||
|
||||
async fn delete(
|
||||
pub async fn delete(
|
||||
&self,
|
||||
request: CacheDeleteRequest,
|
||||
) -> Result<bool, CacheError> {
|
||||
|
@ -300,38 +295,6 @@ impl deno_core::Resource for LscBackend {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct CacheResponseResource {
|
||||
body: AsyncRefCell<Pin<Box<dyn AsyncRead>>>,
|
||||
}
|
||||
|
||||
impl CacheResponseResource {
|
||||
fn new(
|
||||
body: impl Stream<Item = Result<Bytes, std::io::Error>> + 'static,
|
||||
) -> Self {
|
||||
Self {
|
||||
body: AsyncRefCell::new(Box::pin(StreamReader::new(body))),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read(
|
||||
self: Rc<Self>,
|
||||
data: &mut [u8],
|
||||
) -> Result<usize, std::io::Error> {
|
||||
let resource = deno_core::RcRef::map(&self, |r| &r.body);
|
||||
let mut body = resource.borrow_mut().await;
|
||||
let nread = body.read(data).await?;
|
||||
Ok(nread)
|
||||
}
|
||||
}
|
||||
|
||||
impl Resource for CacheResponseResource {
|
||||
deno_core::impl_readable_byob!();
|
||||
|
||||
fn name(&self) -> Cow<str> {
|
||||
"CacheResponseResource".into()
|
||||
}
|
||||
}
|
||||
|
||||
fn vary_header_matches(
|
||||
vary_header: &[u8],
|
||||
query_request_headers: &[(ByteString, ByteString)],
|
||||
|
|
65
ext/cache/sqlite.rs
vendored
65
ext/cache/sqlite.rs
vendored
|
@ -1,5 +1,4 @@
|
|||
// Copyright 2018-2025 the Deno authors. MIT license.
|
||||
use std::borrow::Cow;
|
||||
use std::path::PathBuf;
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
|
@ -7,19 +6,15 @@ use std::sync::Arc;
|
|||
use std::time::SystemTime;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use deno_core::futures::future::poll_fn;
|
||||
use deno_core::parking_lot::Mutex;
|
||||
use deno_core::unsync::spawn_blocking;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::BufMutView;
|
||||
use deno_core::ByteString;
|
||||
use deno_core::Resource;
|
||||
use rusqlite::params;
|
||||
use rusqlite::Connection;
|
||||
use rusqlite::OptionalExtension;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::AsyncWrite;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
|
@ -27,12 +22,12 @@ use crate::deserialize_headers;
|
|||
use crate::get_header;
|
||||
use crate::serialize_headers;
|
||||
use crate::vary_header_matches;
|
||||
use crate::Cache;
|
||||
use crate::CacheDeleteRequest;
|
||||
use crate::CacheError;
|
||||
use crate::CacheMatchRequest;
|
||||
use crate::CacheMatchResponseMeta;
|
||||
use crate::CachePutRequest;
|
||||
use crate::CacheResponseResource;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SqliteBackedCache {
|
||||
|
@ -94,14 +89,14 @@ impl SqliteBackedCache {
|
|||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl Cache for SqliteBackedCache {
|
||||
type CacheMatchResourceType = CacheResponseResource;
|
||||
|
||||
impl SqliteBackedCache {
|
||||
/// Open a cache storage. Internally, this creates a row in the
|
||||
/// sqlite db if the cache doesn't exist and returns the internal id
|
||||
/// of the cache.
|
||||
async fn storage_open(&self, cache_name: String) -> Result<i64, CacheError> {
|
||||
pub async fn storage_open(
|
||||
&self,
|
||||
cache_name: String,
|
||||
) -> Result<i64, CacheError> {
|
||||
let db = self.connection.clone();
|
||||
let cache_storage_dir = self.cache_storage_dir.clone();
|
||||
spawn_blocking(move || {
|
||||
|
@ -127,7 +122,10 @@ impl Cache for SqliteBackedCache {
|
|||
|
||||
/// Check if a cache with the provided name exists.
|
||||
/// Note: this doesn't check the disk, it only checks the sqlite db.
|
||||
async fn storage_has(&self, cache_name: String) -> Result<bool, CacheError> {
|
||||
pub async fn storage_has(
|
||||
&self,
|
||||
cache_name: String,
|
||||
) -> Result<bool, CacheError> {
|
||||
let db = self.connection.clone();
|
||||
spawn_blocking(move || {
|
||||
let db = db.lock();
|
||||
|
@ -145,7 +143,7 @@ impl Cache for SqliteBackedCache {
|
|||
}
|
||||
|
||||
/// Delete a cache storage. Internally, this deletes the row in the sqlite db.
|
||||
async fn storage_delete(
|
||||
pub async fn storage_delete(
|
||||
&self,
|
||||
cache_name: String,
|
||||
) -> Result<bool, CacheError> {
|
||||
|
@ -174,7 +172,7 @@ impl Cache for SqliteBackedCache {
|
|||
.await?
|
||||
}
|
||||
|
||||
async fn put(
|
||||
pub async fn put(
|
||||
&self,
|
||||
request_response: CachePutRequest,
|
||||
resource: Option<Rc<dyn Resource>>,
|
||||
|
@ -227,7 +225,7 @@ impl Cache for SqliteBackedCache {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn r#match(
|
||||
pub async fn r#match(
|
||||
&self,
|
||||
request: CacheMatchRequest,
|
||||
) -> Result<
|
||||
|
@ -298,14 +296,17 @@ impl Cache for SqliteBackedCache {
|
|||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
Ok(Some((cache_meta, Some(CacheResponseResource::new(file)))))
|
||||
Ok(Some((
|
||||
cache_meta,
|
||||
Some(CacheResponseResource::sqlite(file)),
|
||||
)))
|
||||
}
|
||||
Some((cache_meta, None)) => Ok(Some((cache_meta, None))),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete(
|
||||
pub async fn delete(
|
||||
&self,
|
||||
request: CacheDeleteRequest,
|
||||
) -> Result<bool, CacheError> {
|
||||
|
@ -370,36 +371,6 @@ impl deno_core::Resource for SqliteBackedCache {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct CacheResponseResource {
|
||||
file: AsyncRefCell<tokio::fs::File>,
|
||||
}
|
||||
|
||||
impl CacheResponseResource {
|
||||
fn new(file: tokio::fs::File) -> Self {
|
||||
Self {
|
||||
file: AsyncRefCell::new(file),
|
||||
}
|
||||
}
|
||||
|
||||
async fn read(
|
||||
self: Rc<Self>,
|
||||
data: &mut [u8],
|
||||
) -> Result<usize, std::io::Error> {
|
||||
let resource = deno_core::RcRef::map(&self, |r| &r.file);
|
||||
let mut file = resource.borrow_mut().await;
|
||||
let nread = file.read(data).await?;
|
||||
Ok(nread)
|
||||
}
|
||||
}
|
||||
|
||||
impl Resource for CacheResponseResource {
|
||||
deno_core::impl_readable_byob!();
|
||||
|
||||
fn name(&self) -> Cow<str> {
|
||||
"CacheResponseResource".into()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hash(token: &str) -> String {
|
||||
use sha2::Digest;
|
||||
format!("{:x}", sha2::Sha256::digest(token.as_bytes()))
|
||||
|
|
|
@ -7,7 +7,6 @@ use std::path::PathBuf;
|
|||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use deno_cache::SqliteBackedCache;
|
||||
use deno_core::snapshot::*;
|
||||
use deno_core::v8;
|
||||
use deno_core::Extension;
|
||||
|
@ -283,7 +282,7 @@ pub fn create_runtime_snapshot(
|
|||
deno_webgpu::deno_webgpu::init_ops_and_esm(),
|
||||
deno_canvas::deno_canvas::init_ops_and_esm(),
|
||||
deno_fetch::deno_fetch::init_ops_and_esm::<Permissions>(Default::default()),
|
||||
deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(None),
|
||||
deno_cache::deno_cache::init_ops_and_esm(None),
|
||||
deno_websocket::deno_websocket::init_ops_and_esm::<Permissions>(
|
||||
"".to_owned(),
|
||||
None,
|
||||
|
|
|
@ -11,6 +11,7 @@ use std::task::Context;
|
|||
use std::task::Poll;
|
||||
|
||||
use deno_broadcast_channel::InMemoryBroadcastChannel;
|
||||
use deno_cache::CacheImpl;
|
||||
use deno_cache::CreateCache;
|
||||
use deno_cache::SqliteBackedCache;
|
||||
use deno_core::error::CoreError;
|
||||
|
@ -454,28 +455,39 @@ impl WebWorker {
|
|||
|
||||
// Permissions: many ops depend on this
|
||||
let enable_testing_features = options.bootstrap.enable_testing_features;
|
||||
let create_cache = if options.use_lsc_cache {
|
||||
use deno_cache::CacheShard;
|
||||
let Ok(endpoint) = std::env::var("LSC_ENDPOINT") else {
|
||||
return None;
|
||||
};
|
||||
let Ok(token) = std::env::var("LSC_TOKEN") else {
|
||||
return None;
|
||||
};
|
||||
let shard = Rc::new(CacheShard::new(endpoint, token));
|
||||
let create_cache_fn = move || {
|
||||
let x = deno_cache::LscBackend::default();
|
||||
x.set_shard(shard.clone());
|
||||
|
||||
x
|
||||
};
|
||||
Some(CreateCache(Arc::new(create_cache_fn)))
|
||||
} else if let Some(storage_dir) = options.cache_storage_dir {
|
||||
let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone());
|
||||
Some(CreateCache(Arc::new(create_cache_fn)))
|
||||
} else {
|
||||
fn create_cache_inner(options: &WebWorkerOptions) -> Option<CreateCache> {
|
||||
if let Some(storage_dir) = &options.cache_storage_dir {
|
||||
let storage_dir = storage_dir.clone();
|
||||
let create_cache_fn = move || {
|
||||
let s = SqliteBackedCache::new(storage_dir.clone())?;
|
||||
Ok(CacheImpl::Sqlite(s))
|
||||
};
|
||||
return Some(CreateCache(Arc::new(create_cache_fn)));
|
||||
}
|
||||
|
||||
if options.use_lsc_cache {
|
||||
use deno_cache::CacheShard;
|
||||
let Ok(endpoint) = std::env::var("LSC_ENDPOINT") else {
|
||||
return None;
|
||||
};
|
||||
let Ok(token) = std::env::var("LSC_TOKEN") else {
|
||||
return None;
|
||||
};
|
||||
let shard = Rc::new(CacheShard::new(endpoint, token));
|
||||
let create_cache_fn = move || {
|
||||
let x = deno_cache::LscBackend::default();
|
||||
x.set_shard(shard.clone());
|
||||
|
||||
Ok(CacheImpl::Lsc(x))
|
||||
};
|
||||
#[allow(clippy::arc_with_non_send_sync)]
|
||||
return Some(CreateCache(Arc::new(create_cache_fn)));
|
||||
}
|
||||
|
||||
None
|
||||
};
|
||||
}
|
||||
let create_cache = create_cache_inner(&options);
|
||||
|
||||
// NOTE(bartlomieju): ordering is important here, keep it in sync with
|
||||
// `runtime/worker.rs` and `runtime/snapshot.rs`!
|
||||
|
@ -503,9 +515,7 @@ impl WebWorker {
|
|||
..Default::default()
|
||||
},
|
||||
),
|
||||
deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(
|
||||
create_cache,
|
||||
),
|
||||
deno_cache::deno_cache::init_ops_and_esm(create_cache),
|
||||
deno_websocket::deno_websocket::init_ops_and_esm::<PermissionsContainer>(
|
||||
options.bootstrap.user_agent.clone(),
|
||||
services.root_cert_store_provider.clone(),
|
||||
|
|
|
@ -8,6 +8,7 @@ use std::time::Duration;
|
|||
use std::time::Instant;
|
||||
|
||||
use deno_broadcast_channel::InMemoryBroadcastChannel;
|
||||
use deno_cache::CacheImpl;
|
||||
use deno_cache::CreateCache;
|
||||
use deno_cache::SqliteBackedCache;
|
||||
use deno_core::error::CoreError;
|
||||
|
@ -200,6 +201,7 @@ pub struct WorkerOptions {
|
|||
pub strace_ops: Option<Vec<String>>,
|
||||
|
||||
pub cache_storage_dir: Option<std::path::PathBuf>,
|
||||
pub use_lsc_cache: bool,
|
||||
pub origin_storage_dir: Option<std::path::PathBuf>,
|
||||
pub stdio: Stdio,
|
||||
pub enable_stack_trace_arg_in_ops: bool,
|
||||
|
@ -221,6 +223,7 @@ impl Default for WorkerOptions {
|
|||
format_js_error_fn: Default::default(),
|
||||
origin_storage_dir: Default::default(),
|
||||
cache_storage_dir: Default::default(),
|
||||
use_lsc_cache: false,
|
||||
extensions: Default::default(),
|
||||
startup_snapshot: Default::default(),
|
||||
create_params: Default::default(),
|
||||
|
@ -341,6 +344,39 @@ impl MainWorker {
|
|||
},
|
||||
);
|
||||
|
||||
fn create_cache_inner(options: &WorkerOptions) -> Option<CreateCache> {
|
||||
if let Some(storage_dir) = &options.cache_storage_dir {
|
||||
let storage_dir = storage_dir.clone();
|
||||
let create_cache_fn = move || {
|
||||
let s = SqliteBackedCache::new(storage_dir.clone())?;
|
||||
Ok(CacheImpl::Sqlite(s))
|
||||
};
|
||||
return Some(CreateCache(Arc::new(create_cache_fn)));
|
||||
}
|
||||
|
||||
if options.use_lsc_cache {
|
||||
use deno_cache::CacheShard;
|
||||
let Ok(endpoint) = std::env::var("LSC_ENDPOINT") else {
|
||||
return None;
|
||||
};
|
||||
let Ok(token) = std::env::var("LSC_TOKEN") else {
|
||||
return None;
|
||||
};
|
||||
let shard = Rc::new(CacheShard::new(endpoint, token));
|
||||
let create_cache_fn = move || {
|
||||
let x = deno_cache::LscBackend::default();
|
||||
x.set_shard(shard.clone());
|
||||
|
||||
Ok(CacheImpl::Lsc(x))
|
||||
};
|
||||
#[allow(clippy::arc_with_non_send_sync)]
|
||||
return Some(CreateCache(Arc::new(create_cache_fn)));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
let create_cache = create_cache_inner(&options);
|
||||
|
||||
// Get our op metrics
|
||||
let (op_summary_metrics, op_metrics_factory_fn) = create_op_metrics(
|
||||
options.bootstrap.enable_op_summary_metrics,
|
||||
|
@ -350,10 +386,6 @@ impl MainWorker {
|
|||
// Permissions: many ops depend on this
|
||||
let enable_testing_features = options.bootstrap.enable_testing_features;
|
||||
let exit_code = ExitCode::default();
|
||||
let create_cache = options.cache_storage_dir.map(|storage_dir| {
|
||||
let create_cache_fn = move || SqliteBackedCache::new(storage_dir.clone());
|
||||
CreateCache(Arc::new(create_cache_fn))
|
||||
});
|
||||
|
||||
// NOTE(bartlomieju): ordering is important here, keep it in sync with
|
||||
// `runtime/web_worker.rs` and `runtime/snapshot.rs`!
|
||||
|
@ -381,9 +413,7 @@ impl MainWorker {
|
|||
..Default::default()
|
||||
},
|
||||
),
|
||||
deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(
|
||||
create_cache,
|
||||
),
|
||||
deno_cache::deno_cache::init_ops_and_esm(create_cache),
|
||||
deno_websocket::deno_websocket::init_ops_and_esm::<PermissionsContainer>(
|
||||
options.bootstrap.user_agent.clone(),
|
||||
services.root_cert_store_provider.clone(),
|
||||
|
|
Loading…
Add table
Reference in a new issue