summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2022-09-28 17:41:12 +0530
committerGitHub <noreply@github.com>2022-09-28 17:41:12 +0530
commitb312279e58e51520a38e51cca317a09cdadd7cb4 (patch)
treea0c6f432042ba25b569c151bbe59f1e721788d0c /runtime
parent1156f726a92d3d3985e591327c7526cd3e2b0473 (diff)
feat: implement Web Cache API (#15829)
Diffstat (limited to 'runtime')
-rw-r--r--runtime/Cargo.toml2
-rw-r--r--runtime/build.rs2
-rw-r--r--runtime/examples/hello_runtime.rs1
-rw-r--r--runtime/js/99_main.js8
-rw-r--r--runtime/lib.rs1
-rw-r--r--runtime/web_worker.rs8
-rw-r--r--runtime/worker.rs9
7 files changed, 31 insertions, 0 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml
index 0084da3c9..fb49439a4 100644
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -23,6 +23,7 @@ path = "examples/hello_runtime.rs"
[build-dependencies]
deno_broadcast_channel = { version = "0.64.0", path = "../ext/broadcast_channel" }
+deno_cache = { version = "0.1.0", path = "../ext/cache" }
deno_console = { version = "0.70.0", path = "../ext/console" }
deno_core = { version = "0.152.0", path = "../core" }
deno_crypto = { version = "0.84.0", path = "../ext/crypto" }
@@ -48,6 +49,7 @@ winapi = "0.3.9"
[dependencies]
deno_broadcast_channel = { version = "0.64.0", path = "../ext/broadcast_channel" }
+deno_cache = { version = "0.1.0", path = "../ext/cache" }
deno_console = { version = "0.70.0", path = "../ext/console" }
deno_core = { version = "0.152.0", path = "../core" }
deno_crypto = { version = "0.84.0", path = "../ext/crypto" }
diff --git a/runtime/build.rs b/runtime/build.rs
index d45c4a2a8..55a89fb8b 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -8,6 +8,7 @@ use std::path::PathBuf;
#[cfg(not(feature = "docsrs"))]
mod not_docs {
use super::*;
+ use deno_cache::SqliteBackedCache;
use deno_core::Extension;
use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
@@ -175,6 +176,7 @@ mod not_docs {
Default::default(),
),
deno_fetch::init::<Permissions>(Default::default()),
+ deno_cache::init::<SqliteBackedCache>(None),
deno_websocket::init::<Permissions>("".to_owned(), None, None),
deno_webstorage::init(None),
deno_crypto::init(None),
diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs
index de5c2427d..b4a8d8201 100644
--- a/runtime/examples/hello_runtime.rs
+++ b/runtime/examples/hello_runtime.rs
@@ -55,6 +55,7 @@ async fn main() -> Result<(), AnyError> {
module_loader,
npm_resolver: None,
get_error_class_fn: Some(&get_error_class_name),
+ cache_storage_dir: None,
origin_storage_dir: None,
blob_store: BlobStore::default(),
broadcast_channel: InMemoryBroadcastChannel::default(),
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 755eac939..0a65cadee 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -50,6 +50,7 @@ delete Intl.v8BreakIterator;
const encoding = window.__bootstrap.encoding;
const colors = window.__bootstrap.colors;
const Console = window.__bootstrap.console.Console;
+ const caches = window.__bootstrap.caches;
const inspectArgs = window.__bootstrap.console.inspectArgs;
const quoteString = window.__bootstrap.console.quoteString;
const compression = window.__bootstrap.compression;
@@ -469,6 +470,13 @@ delete Intl.v8BreakIterator;
btoa: util.writable(base64.btoa),
clearInterval: util.writable(timers.clearInterval),
clearTimeout: util.writable(timers.clearTimeout),
+ caches: {
+ enumerable: true,
+ configurable: true,
+ get: caches.cacheStorage,
+ },
+ CacheStorage: util.nonEnumerable(caches.CacheStorage),
+ Cache: util.nonEnumerable(caches.Cache),
console: util.nonEnumerable(
new Console((msg, level) => core.print(msg, level > 1)),
),
diff --git a/runtime/lib.rs b/runtime/lib.rs
index 656662391..99813e3d8 100644
--- a/runtime/lib.rs
+++ b/runtime/lib.rs
@@ -1,6 +1,7 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
pub use deno_broadcast_channel;
+pub use deno_cache;
pub use deno_console;
pub use deno_core;
pub use deno_crypto;
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs
index 306e1da5c..09a631916 100644
--- a/runtime/web_worker.rs
+++ b/runtime/web_worker.rs
@@ -9,6 +9,8 @@ use crate::tokio_util::run_local;
use crate::worker::FormatJsErrorFn;
use crate::BootstrapOptions;
use deno_broadcast_channel::InMemoryBroadcastChannel;
+use deno_cache::CreateCache;
+use deno_cache::SqliteBackedCache;
use deno_core::error::AnyError;
use deno_core::error::JsError;
use deno_core::futures::channel::mpsc;
@@ -337,6 +339,7 @@ pub struct WebWorkerOptions {
pub broadcast_channel: InMemoryBroadcastChannel,
pub shared_array_buffer_store: Option<SharedArrayBufferStore>,
pub compiled_wasm_module_store: Option<CompiledWasmModuleStore>,
+ pub cache_storage_dir: Option<std::path::PathBuf>,
pub stdio: Stdio,
}
@@ -373,6 +376,10 @@ impl WebWorker {
Ok(())
})
.build();
+ 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))
+ });
let mut extensions: Vec<Extension> = vec![
// Web APIs
@@ -392,6 +399,7 @@ impl WebWorker {
file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler),
..Default::default()
}),
+ deno_cache::init::<SqliteBackedCache>(create_cache),
deno_websocket::init::<Permissions>(
options.bootstrap.user_agent.clone(),
options.root_cert_store.clone(),
diff --git a/runtime/worker.rs b/runtime/worker.rs
index 0723cef84..3ac3654e2 100644
--- a/runtime/worker.rs
+++ b/runtime/worker.rs
@@ -7,6 +7,8 @@ use crate::ops::io::Stdio;
use crate::permissions::Permissions;
use crate::BootstrapOptions;
use deno_broadcast_channel::InMemoryBroadcastChannel;
+use deno_cache::CreateCache;
+use deno_cache::SqliteBackedCache;
use deno_core::error::AnyError;
use deno_core::error::JsError;
use deno_core::futures::Future;
@@ -85,6 +87,7 @@ pub struct WorkerOptions {
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
pub should_break_on_first_statement: bool,
pub get_error_class_fn: Option<GetErrorClassFn>,
+ pub cache_storage_dir: Option<std::path::PathBuf>,
pub origin_storage_dir: Option<std::path::PathBuf>,
pub blob_store: BlobStore,
pub broadcast_channel: InMemoryBroadcastChannel,
@@ -131,6 +134,10 @@ impl MainWorker {
})
.build();
let exit_code = ExitCode(Arc::new(AtomicI32::new(0)));
+ 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))
+ });
// Internal modules
let mut extensions: Vec<Extension> = vec![
@@ -151,6 +158,7 @@ impl MainWorker {
file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler),
..Default::default()
}),
+ deno_cache::init::<SqliteBackedCache>(create_cache),
deno_websocket::init::<Permissions>(
options.bootstrap.user_agent.clone(),
options.root_cert_store.clone(),
@@ -527,6 +535,7 @@ mod tests {
module_loader: Rc::new(deno_core::FsModuleLoader),
npm_resolver: None,
get_error_class_fn: None,
+ cache_storage_dir: None,
origin_storage_dir: None,
blob_store: BlobStore::default(),
broadcast_channel: InMemoryBroadcastChannel::default(),