summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorHeyang Zhou <zhy20000919@hotmail.com>2023-03-22 12:13:24 +0800
committerGitHub <noreply@github.com>2023-03-22 12:13:24 +0800
commit92ebf4afe5d55135b3ba39616bcb77106c07c597 (patch)
treef79fe65811c7449f5b50c093852eceaad228d39f /runtime
parent8bcffff9dc517aa93dea2816b2a854f65d24eccc (diff)
feat(ext/kv): key-value store (#18232)
This commit adds unstable "Deno.openKv()" API that allows to open a key-value database at a specified path. --------- Co-authored-by: Luca Casonato <hello@lcas.dev> Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/Cargo.toml2
-rw-r--r--runtime/build.rs22
-rw-r--r--runtime/js/90_deno_ns.js5
-rw-r--r--runtime/lib.rs1
-rw-r--r--runtime/permissions/mod.rs12
-rw-r--r--runtime/web_worker.rs5
-rw-r--r--runtime/worker.rs7
7 files changed, 54 insertions, 0 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml
index e3a98e9af..c729e70f6 100644
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -47,6 +47,7 @@ deno_http.workspace = true
deno_io.workspace = true
deno_net.workspace = true
deno_node.workspace = true
+deno_kv.workspace = true
deno_tls.workspace = true
deno_url.workspace = true
deno_web.workspace = true
@@ -71,6 +72,7 @@ deno_flash.workspace = true
deno_fs.workspace = true
deno_http.workspace = true
deno_io.workspace = true
+deno_kv.workspace = true
deno_napi.workspace = true
deno_net.workspace = true
deno_node.workspace = true
diff --git a/runtime/build.rs b/runtime/build.rs
index ec7c9642c..5d0ba0cc7 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -200,6 +200,24 @@ mod startup_snapshot {
}
}
+ impl deno_kv::sqlite::SqliteDbHandlerPermissions for Permissions {
+ fn check_read(
+ &mut self,
+ _path: &Path,
+ _api_name: &str,
+ ) -> Result<(), AnyError> {
+ unreachable!("snapshotting!")
+ }
+
+ fn check_write(
+ &mut self,
+ _path: &Path,
+ _api_name: &str,
+ ) -> Result<(), AnyError> {
+ unreachable!("snapshotting!")
+ }
+ }
+
deno_core::extension!(runtime,
deps = [
deno_webidl,
@@ -289,6 +307,10 @@ mod startup_snapshot {
None,
),
deno_tls::deno_tls::init_ops_and_esm(),
+ deno_kv::deno_kv::init_ops_and_esm(
+ deno_kv::sqlite::SqliteDbHandler::<Permissions>::new(None),
+ false, // No --unstable
+ ),
deno_napi::deno_napi::init_ops_and_esm::<Permissions>(),
deno_http::deno_http::init_ops_and_esm(),
deno_io::deno_io::init_ops_and_esm(Default::default()),
diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js
index 7c3a9226d..54480c9c7 100644
--- a/runtime/js/90_deno_ns.js
+++ b/runtime/js/90_deno_ns.js
@@ -23,6 +23,7 @@ import * as signals from "ext:runtime/40_signals.js";
import * as tty from "ext:runtime/40_tty.js";
// TODO(bartlomieju): this is funky we have two `http` imports
import * as httpRuntime from "ext:runtime/40_http.js";
+import * as kv from "ext:deno_kv/01_db.ts";
const denoNs = {
metrics: core.metrics,
@@ -169,6 +170,10 @@ const denoNsUnstable = {
funlockSync: fs.funlockSync,
upgradeHttp: http.upgradeHttp,
upgradeHttpRaw: flash.upgradeHttpRaw,
+ openKv: kv.openKv,
+ Kv: kv.Kv,
+ KvU64: kv.KvU64,
+ KvListIterator: kv.KvListIterator,
};
export { denoNs, denoNsUnstable };
diff --git a/runtime/lib.rs b/runtime/lib.rs
index f55833831..02d52cd5a 100644
--- a/runtime/lib.rs
+++ b/runtime/lib.rs
@@ -11,6 +11,7 @@ pub use deno_flash;
pub use deno_fs;
pub use deno_http;
pub use deno_io;
+pub use deno_kv;
pub use deno_napi;
pub use deno_net;
pub use deno_node;
diff --git a/runtime/permissions/mod.rs b/runtime/permissions/mod.rs
index c985295a7..2093b08f9 100644
--- a/runtime/permissions/mod.rs
+++ b/runtime/permissions/mod.rs
@@ -1967,6 +1967,18 @@ impl deno_ffi::FfiPermissions for PermissionsContainer {
}
}
+impl deno_kv::sqlite::SqliteDbHandlerPermissions for PermissionsContainer {
+ #[inline(always)]
+ fn check_read(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError> {
+ self.0.lock().read.check(p, Some(api_name))
+ }
+
+ #[inline(always)]
+ fn check_write(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError> {
+ self.0.lock().write.check(p, Some(api_name))
+ }
+}
+
fn unit_permission_from_flag_bool(
flag: bool,
name: &'static str,
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs
index 0aa142da8..ab06ab649 100644
--- a/runtime/web_worker.rs
+++ b/runtime/web_worker.rs
@@ -34,6 +34,7 @@ use deno_core::SharedArrayBufferStore;
use deno_core::Snapshot;
use deno_core::SourceMapGetter;
use deno_io::Stdio;
+use deno_kv::sqlite::SqliteDbHandler;
use deno_node::RequireNpmResolver;
use deno_tls::rustls::RootCertStore;
use deno_web::create_entangled_message_port;
@@ -431,6 +432,10 @@ impl WebWorker {
options.unsafely_ignore_certificate_errors.clone(),
),
deno_tls::deno_tls::init_ops(),
+ deno_kv::deno_kv::init_ops(
+ SqliteDbHandler::<PermissionsContainer>::new(None),
+ unstable,
+ ),
deno_napi::deno_napi::init_ops::<PermissionsContainer>(),
deno_http::deno_http::init_ops(),
deno_io::deno_io::init_ops(Some(options.stdio)),
diff --git a/runtime/worker.rs b/runtime/worker.rs
index a24a22c96..42874f209 100644
--- a/runtime/worker.rs
+++ b/runtime/worker.rs
@@ -30,6 +30,7 @@ use deno_core::SharedArrayBufferStore;
use deno_core::Snapshot;
use deno_core::SourceMapGetter;
use deno_io::Stdio;
+use deno_kv::sqlite::SqliteDbHandler;
use deno_node::RequireNpmResolver;
use deno_tls::rustls::RootCertStore;
use deno_web::BlobStore;
@@ -253,6 +254,12 @@ impl MainWorker {
options.unsafely_ignore_certificate_errors.clone(),
),
deno_tls::deno_tls::init_ops(),
+ deno_kv::deno_kv::init_ops(
+ SqliteDbHandler::<PermissionsContainer>::new(
+ options.origin_storage_dir.clone(),
+ ),
+ unstable,
+ ),
deno_napi::deno_napi::init_ops::<PermissionsContainer>(),
deno_http::deno_http::init_ops(),
deno_io::deno_io::init_ops(Some(options.stdio)),