2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
// NOTE to all: use **cached** prepared statements when interfacing with SQLite.
|
|
|
|
|
2023-01-05 14:29:50 -05:00
|
|
|
use std::fmt;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-05-10 06:02:47 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::include_js_files;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-05-10 06:02:47 -04:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use rusqlite::params;
|
|
|
|
use rusqlite::Connection;
|
|
|
|
use rusqlite::OptionalExtension;
|
|
|
|
|
2022-04-19 22:14:00 -04:00
|
|
|
pub use rusqlite;
|
|
|
|
|
2021-05-10 06:02:47 -04:00
|
|
|
#[derive(Clone)]
|
2021-05-27 01:23:12 -04:00
|
|
|
struct OriginStorageDir(PathBuf);
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-06-23 05:59:08 -04:00
|
|
|
const MAX_STORAGE_BYTES: u32 = 10 * 1024 * 1024;
|
|
|
|
|
2021-05-27 01:23:12 -04:00
|
|
|
pub fn init(origin_storage_dir: Option<PathBuf>) -> Extension {
|
2023-01-08 17:48:46 -05:00
|
|
|
Extension::builder(env!("CARGO_PKG_NAME"))
|
|
|
|
.dependencies(vec!["deno_webidl"])
|
2023-02-07 16:09:50 -05:00
|
|
|
.esm(include_js_files!("01_webstorage.js",))
|
2021-05-10 06:02:47 -04:00
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_webstorage_length::decl(),
|
|
|
|
op_webstorage_key::decl(),
|
|
|
|
op_webstorage_set::decl(),
|
|
|
|
op_webstorage_get::decl(),
|
|
|
|
op_webstorage_remove::decl(),
|
|
|
|
op_webstorage_clear::decl(),
|
|
|
|
op_webstorage_iterate_keys::decl(),
|
2021-05-10 06:02:47 -04:00
|
|
|
])
|
|
|
|
.state(move |state| {
|
2021-10-26 20:10:27 -04:00
|
|
|
if let Some(origin_storage_dir) = &origin_storage_dir {
|
|
|
|
state.put(OriginStorageDir(origin_storage_dir.clone()));
|
2021-05-10 06:02:47 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:25:44 -04:00
|
|
|
pub fn get_declaration() -> PathBuf {
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_webstorage.d.ts")
|
|
|
|
}
|
|
|
|
|
2021-05-19 08:28:50 -04:00
|
|
|
struct LocalStorage(Connection);
|
|
|
|
struct SessionStorage(Connection);
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-05-19 08:28:50 -04:00
|
|
|
fn get_webstorage(
|
2021-05-10 06:02:47 -04:00
|
|
|
state: &mut OpState,
|
|
|
|
persistent: bool,
|
2021-05-19 08:28:50 -04:00
|
|
|
) -> Result<&Connection, AnyError> {
|
|
|
|
let conn = if persistent {
|
|
|
|
if state.try_borrow::<LocalStorage>().is_none() {
|
2021-05-27 01:23:12 -04:00
|
|
|
let path = state.try_borrow::<OriginStorageDir>().ok_or_else(|| {
|
2021-05-19 08:28:50 -04:00
|
|
|
DomExceptionNotSupportedError::new(
|
|
|
|
"LocalStorage is not supported in this context.",
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
std::fs::create_dir_all(&path.0)?;
|
|
|
|
let conn = Connection::open(path.0.join("local_storage"))?;
|
2021-08-14 16:39:35 -04:00
|
|
|
// Enable write-ahead-logging and tweak some other stuff.
|
|
|
|
let initial_pragmas = "
|
|
|
|
-- enable write-ahead-logging mode
|
|
|
|
PRAGMA journal_mode=WAL;
|
|
|
|
PRAGMA synchronous=NORMAL;
|
|
|
|
PRAGMA temp_store=memory;
|
|
|
|
PRAGMA page_size=4096;
|
|
|
|
PRAGMA mmap_size=6000000;
|
|
|
|
PRAGMA optimize;
|
|
|
|
";
|
|
|
|
|
|
|
|
conn.execute_batch(initial_pragmas)?;
|
|
|
|
conn.set_prepared_statement_cache_capacity(128);
|
|
|
|
{
|
|
|
|
let mut stmt = conn.prepare_cached(
|
|
|
|
"CREATE TABLE IF NOT EXISTS data (key VARCHAR UNIQUE, value VARCHAR)",
|
|
|
|
)?;
|
|
|
|
stmt.execute(params![])?;
|
|
|
|
}
|
2021-05-19 08:28:50 -04:00
|
|
|
state.put(LocalStorage(conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
&state.borrow::<LocalStorage>().0
|
2021-05-10 06:02:47 -04:00
|
|
|
} else {
|
2021-05-19 08:28:50 -04:00
|
|
|
if state.try_borrow::<SessionStorage>().is_none() {
|
|
|
|
let conn = Connection::open_in_memory()?;
|
2021-08-14 16:39:35 -04:00
|
|
|
{
|
|
|
|
let mut stmt = conn.prepare_cached(
|
|
|
|
"CREATE TABLE data (key VARCHAR UNIQUE, value VARCHAR)",
|
|
|
|
)?;
|
|
|
|
stmt.execute(params![])?;
|
|
|
|
}
|
2021-05-19 08:28:50 -04:00
|
|
|
state.put(SessionStorage(conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
&state.borrow::<SessionStorage>().0
|
|
|
|
};
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-05-19 08:28:50 -04:00
|
|
|
Ok(conn)
|
2021-05-10 06:02:47 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-05-10 06:02:47 -04:00
|
|
|
pub fn op_webstorage_length(
|
|
|
|
state: &mut OpState,
|
2021-05-19 08:28:50 -04:00
|
|
|
persistent: bool,
|
2021-05-10 06:02:47 -04:00
|
|
|
) -> Result<u32, AnyError> {
|
2021-05-19 08:28:50 -04:00
|
|
|
let conn = get_webstorage(state, persistent)?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt = conn.prepare_cached("SELECT COUNT(*) FROM data")?;
|
2021-05-10 06:02:47 -04:00
|
|
|
let length: u32 = stmt.query_row(params![], |row| row.get(0))?;
|
|
|
|
|
|
|
|
Ok(length)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-05-10 06:02:47 -04:00
|
|
|
pub fn op_webstorage_key(
|
|
|
|
state: &mut OpState,
|
2021-05-19 08:28:50 -04:00
|
|
|
index: u32,
|
|
|
|
persistent: bool,
|
2021-05-10 06:02:47 -04:00
|
|
|
) -> Result<Option<String>, AnyError> {
|
2021-05-19 08:28:50 -04:00
|
|
|
let conn = get_webstorage(state, persistent)?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt =
|
|
|
|
conn.prepare_cached("SELECT key FROM data LIMIT 1 OFFSET ?")?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
|
|
|
let key: Option<String> = stmt
|
2021-05-19 08:28:50 -04:00
|
|
|
.query_row(params![index], |row| row.get(0))
|
2021-05-10 06:02:47 -04:00
|
|
|
.optional()?;
|
|
|
|
|
|
|
|
Ok(key)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-05-10 06:02:47 -04:00
|
|
|
pub fn op_webstorage_set(
|
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
key: String,
|
|
|
|
value: String,
|
2021-05-19 08:28:50 -04:00
|
|
|
persistent: bool,
|
2021-05-10 06:02:47 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-05-19 08:28:50 -04:00
|
|
|
let conn = get_webstorage(state, persistent)?;
|
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt = conn
|
|
|
|
.prepare_cached("SELECT SUM(pgsize) FROM dbstat WHERE name = 'data'")?;
|
2021-05-10 06:02:47 -04:00
|
|
|
let size: u32 = stmt.query_row(params![], |row| row.get(0))?;
|
|
|
|
|
2021-06-23 05:59:08 -04:00
|
|
|
if size >= MAX_STORAGE_BYTES {
|
2021-05-10 06:02:47 -04:00
|
|
|
return Err(
|
2021-06-05 13:30:20 -04:00
|
|
|
deno_web::DomExceptionQuotaExceededError::new(
|
|
|
|
"Exceeded maximum storage size",
|
|
|
|
)
|
|
|
|
.into(),
|
2021-05-10 06:02:47 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt = conn
|
|
|
|
.prepare_cached("INSERT OR REPLACE INTO data (key, value) VALUES (?, ?)")?;
|
2022-04-04 05:37:26 -04:00
|
|
|
stmt.execute(params![key, value])?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-05-10 06:02:47 -04:00
|
|
|
pub fn op_webstorage_get(
|
|
|
|
state: &mut OpState,
|
2021-05-19 08:28:50 -04:00
|
|
|
key_name: String,
|
|
|
|
persistent: bool,
|
2021-05-10 06:02:47 -04:00
|
|
|
) -> Result<Option<String>, AnyError> {
|
2021-05-19 08:28:50 -04:00
|
|
|
let conn = get_webstorage(state, persistent)?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt = conn.prepare_cached("SELECT value FROM data WHERE key = ?")?;
|
2021-05-10 06:02:47 -04:00
|
|
|
let val = stmt
|
2021-05-19 08:28:50 -04:00
|
|
|
.query_row(params![key_name], |row| row.get(0))
|
2021-05-10 06:02:47 -04:00
|
|
|
.optional()?;
|
|
|
|
|
|
|
|
Ok(val)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-05-10 06:02:47 -04:00
|
|
|
pub fn op_webstorage_remove(
|
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
key_name: String,
|
2021-05-19 08:28:50 -04:00
|
|
|
persistent: bool,
|
2021-05-10 06:02:47 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-05-19 08:28:50 -04:00
|
|
|
let conn = get_webstorage(state, persistent)?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt = conn.prepare_cached("DELETE FROM data WHERE key = ?")?;
|
|
|
|
stmt.execute(params![key_name])?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-05-10 06:02:47 -04:00
|
|
|
pub fn op_webstorage_clear(
|
|
|
|
state: &mut OpState,
|
2021-05-19 08:28:50 -04:00
|
|
|
persistent: bool,
|
2021-05-10 06:02:47 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2021-05-19 08:28:50 -04:00
|
|
|
let conn = get_webstorage(state, persistent)?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt = conn.prepare_cached("DELETE FROM data")?;
|
|
|
|
stmt.execute(params![])?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-05-10 06:02:47 -04:00
|
|
|
pub fn op_webstorage_iterate_keys(
|
|
|
|
state: &mut OpState,
|
2021-05-19 08:28:50 -04:00
|
|
|
persistent: bool,
|
2021-05-10 06:02:47 -04:00
|
|
|
) -> Result<Vec<String>, AnyError> {
|
2021-05-19 08:28:50 -04:00
|
|
|
let conn = get_webstorage(state, persistent)?;
|
2021-05-10 06:02:47 -04:00
|
|
|
|
2021-08-14 16:39:35 -04:00
|
|
|
let mut stmt = conn.prepare_cached("SELECT key FROM data")?;
|
2021-05-10 06:02:47 -04:00
|
|
|
let keys = stmt
|
|
|
|
.query_map(params![], |row| row.get::<_, String>(0))?
|
|
|
|
.map(|r| r.unwrap())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DomExceptionNotSupportedError {
|
|
|
|
pub msg: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DomExceptionNotSupportedError {
|
|
|
|
pub fn new(msg: &str) -> Self {
|
|
|
|
DomExceptionNotSupportedError {
|
|
|
|
msg: msg.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DomExceptionNotSupportedError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.pad(&self.msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for DomExceptionNotSupportedError {}
|
|
|
|
|
|
|
|
pub fn get_not_supported_error_class_name(
|
|
|
|
e: &AnyError,
|
|
|
|
) -> Option<&'static str> {
|
|
|
|
e.downcast_ref::<DomExceptionNotSupportedError>()
|
|
|
|
.map(|_| "DOMExceptionNotSupportedError")
|
|
|
|
}
|