2026-01-09 00:28:43 +09:00
|
|
|
// Copyright 2018-2026 the Deno authors. MIT license.
|
2025-02-04 21:59:13 +05:30
|
|
|
|
|
|
|
|
use std::cell::Cell;
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use std::ffi::c_void;
|
2026-01-10 12:53:49 +08:00
|
|
|
use std::rc::Rc;
|
2025-02-04 21:59:13 +05:30
|
|
|
|
2025-07-02 17:59:39 -07:00
|
|
|
use deno_core::FromV8;
|
|
|
|
|
use deno_core::GarbageCollected;
|
2025-02-04 21:59:13 +05:30
|
|
|
use deno_core::op2;
|
2025-06-02 10:44:52 -07:00
|
|
|
use deno_core::v8;
|
|
|
|
|
use deno_core::v8_static_strings;
|
2025-03-18 22:03:35 +05:30
|
|
|
use rusqlite::ffi;
|
2025-02-04 21:59:13 +05:30
|
|
|
|
|
|
|
|
use super::SqliteError;
|
2025-07-02 17:59:39 -07:00
|
|
|
use super::validators;
|
2025-02-04 21:59:13 +05:30
|
|
|
|
2025-06-02 10:44:52 -07:00
|
|
|
#[derive(Default)]
|
2025-02-04 21:59:13 +05:30
|
|
|
pub struct SessionOptions {
|
|
|
|
|
pub table: Option<String>,
|
|
|
|
|
pub db: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 10:44:52 -07:00
|
|
|
impl FromV8<'_> for SessionOptions {
|
|
|
|
|
type Error = validators::Error;
|
|
|
|
|
|
|
|
|
|
fn from_v8(
|
2025-09-23 17:54:14 -07:00
|
|
|
scope: &mut v8::PinScope<'_, '_>,
|
2025-06-02 10:44:52 -07:00
|
|
|
value: v8::Local<v8::Value>,
|
|
|
|
|
) -> Result<Self, validators::Error> {
|
|
|
|
|
use validators::Error;
|
|
|
|
|
|
|
|
|
|
if value.is_undefined() {
|
|
|
|
|
return Ok(SessionOptions::default());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let obj = v8::Local::<v8::Object>::try_from(value).map_err(|_| {
|
2026-04-29 13:41:22 +07:00
|
|
|
Error::InvalidArgType(
|
|
|
|
|
"The \"options\" argument must be an object.".into(),
|
|
|
|
|
)
|
2025-06-02 10:44:52 -07:00
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let mut options = SessionOptions::default();
|
|
|
|
|
|
|
|
|
|
v8_static_strings! {
|
|
|
|
|
TABLE_STRING = "table",
|
|
|
|
|
DB_STRING = "db",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let table_string = TABLE_STRING.v8_string(scope).unwrap();
|
2025-08-09 19:11:48 +08:00
|
|
|
if let Some(table_value) = obj.get(scope, table_string.into())
|
|
|
|
|
&& !table_value.is_undefined()
|
|
|
|
|
{
|
|
|
|
|
if !table_value.is_string() {
|
|
|
|
|
return Err(Error::InvalidArgType(
|
2026-04-29 13:41:22 +07:00
|
|
|
"The \"options.table\" argument must be a string.".into(),
|
2025-08-09 19:11:48 +08:00
|
|
|
));
|
2025-06-02 10:44:52 -07:00
|
|
|
}
|
2025-08-09 19:11:48 +08:00
|
|
|
let table =
|
|
|
|
|
v8::Local::<v8::String>::try_from(table_value).map_err(|_| {
|
|
|
|
|
Error::InvalidArgType(
|
2026-04-29 13:41:22 +07:00
|
|
|
"The \"options.table\" argument must be a string.".into(),
|
2025-08-09 19:11:48 +08:00
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
options.table = Some(table.to_rust_string_lossy(scope).to_string());
|
2025-06-02 10:44:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let db_string = DB_STRING.v8_string(scope).unwrap();
|
2025-08-09 19:11:48 +08:00
|
|
|
if let Some(db_value) = obj.get(scope, db_string.into())
|
|
|
|
|
&& !db_value.is_undefined()
|
|
|
|
|
{
|
|
|
|
|
if !db_value.is_string() {
|
|
|
|
|
return Err(Error::InvalidArgType(
|
2026-04-29 13:41:22 +07:00
|
|
|
"The \"options.db\" argument must be a string.".into(),
|
2025-08-09 19:11:48 +08:00
|
|
|
));
|
2025-06-02 10:44:52 -07:00
|
|
|
}
|
2025-08-09 19:11:48 +08:00
|
|
|
let db = v8::Local::<v8::String>::try_from(db_value).map_err(|_| {
|
2026-04-29 13:41:22 +07:00
|
|
|
Error::InvalidArgType(
|
|
|
|
|
"The \"options.db\" argument must be a string.".into(),
|
|
|
|
|
)
|
2025-08-09 19:11:48 +08:00
|
|
|
})?;
|
|
|
|
|
options.db = Some(db.to_rust_string_lossy(scope).to_string());
|
2025-06-02 10:44:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(options)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 21:59:13 +05:30
|
|
|
pub struct Session {
|
2026-08-21 14:20:50 -07:00
|
|
|
pub(crate) inner: InnerSessionPtr,
|
2025-02-04 21:59:13 +05:30
|
|
|
|
2025-05-10 09:21:53 +05:30
|
|
|
// Hold a weak reference to the database.
|
2026-01-10 12:53:49 +08:00
|
|
|
pub(crate) db: Rc<RefCell<Option<rusqlite::Connection>>>,
|
2026-08-21 14:20:50 -07:00
|
|
|
pub(crate) sessions: Rc<RefCell<Vec<InnerSessionPtr>>>,
|
2025-02-04 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
|
2026-08-21 14:20:50 -07:00
|
|
|
pub(crate) type InnerSessionPtr = Rc<Cell<Option<*mut ffi::sqlite3_session>>>;
|
|
|
|
|
|
2025-09-08 22:12:54 +02:00
|
|
|
// SAFETY: we're sure this can be GCed
|
|
|
|
|
unsafe impl GarbageCollected for Session {
|
|
|
|
|
fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}
|
|
|
|
|
|
2025-05-05 13:58:34 -05:00
|
|
|
fn get_name(&self) -> &'static std::ffi::CStr {
|
|
|
|
|
c"Session"
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-04 21:59:13 +05:30
|
|
|
|
|
|
|
|
impl Drop for Session {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
let _ = self.delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Session {
|
|
|
|
|
fn delete(&self) -> Result<(), SqliteError> {
|
2026-08-21 14:20:50 -07:00
|
|
|
let ptr = self.inner.take().ok_or(SqliteError::SessionClosed)?;
|
|
|
|
|
let mut sessions = self.sessions.borrow_mut();
|
|
|
|
|
if let Some(pos) = sessions
|
|
|
|
|
.iter()
|
|
|
|
|
.position(|session| Rc::ptr_eq(session, &self.inner))
|
|
|
|
|
{
|
|
|
|
|
sessions.remove(pos);
|
2025-02-04 21:59:13 +05:30
|
|
|
}
|
2026-01-20 18:01:48 +05:30
|
|
|
if self.db.borrow().is_none() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2026-08-21 14:20:50 -07:00
|
|
|
// Safety: `ptr` is a valid session. Double free is prevented by taking
|
|
|
|
|
// the pointer from `inner` before deleting it.
|
2025-02-04 21:59:13 +05:30
|
|
|
unsafe {
|
2026-08-21 14:20:50 -07:00
|
|
|
ffi::sqlite3session_delete(ptr);
|
2025-02-04 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[op2]
|
|
|
|
|
impl Session {
|
2025-09-01 19:33:25 +09:00
|
|
|
#[constructor]
|
|
|
|
|
#[cppgc]
|
|
|
|
|
fn create(_: bool) -> Session {
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 21:59:13 +05:30
|
|
|
// Closes the session.
|
|
|
|
|
#[fast]
|
2025-06-02 08:00:23 -07:00
|
|
|
#[undefined]
|
2025-02-04 21:59:13 +05:30
|
|
|
fn close(&self) -> Result<(), SqliteError> {
|
2026-01-10 12:53:49 +08:00
|
|
|
if self.db.borrow().is_none() {
|
2025-06-02 10:44:52 -07:00
|
|
|
return Err(SqliteError::AlreadyClosed);
|
2025-02-06 08:52:49 +05:30
|
|
|
}
|
|
|
|
|
|
2025-02-04 21:59:13 +05:30
|
|
|
self.delete()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieves a changeset containing all changes since the changeset
|
|
|
|
|
// was created. Can be called multiple times.
|
|
|
|
|
//
|
|
|
|
|
// This method is a wrapper around `sqlite3session_changeset()`.
|
|
|
|
|
#[buffer]
|
|
|
|
|
fn changeset(&self) -> Result<Box<[u8]>, SqliteError> {
|
2026-01-10 12:53:49 +08:00
|
|
|
if self.db.borrow().is_none() {
|
2025-06-02 10:44:52 -07:00
|
|
|
return Err(SqliteError::AlreadyClosed);
|
2025-02-06 08:52:49 +05:30
|
|
|
}
|
2026-08-21 14:20:50 -07:00
|
|
|
let ptr = self.inner.get().ok_or(SqliteError::SessionClosed)?;
|
2025-02-04 21:59:13 +05:30
|
|
|
|
2026-08-21 14:20:50 -07:00
|
|
|
session_buffer_op(ptr, ffi::sqlite3session_changeset)
|
2025-02-04 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Similar to the method above, but generates a more compact patchset.
|
|
|
|
|
//
|
|
|
|
|
// This method is a wrapper around `sqlite3session_patchset()`.
|
|
|
|
|
#[buffer]
|
|
|
|
|
fn patchset(&self) -> Result<Box<[u8]>, SqliteError> {
|
2026-01-10 12:53:49 +08:00
|
|
|
if self.db.borrow().is_none() {
|
2025-06-02 10:44:52 -07:00
|
|
|
return Err(SqliteError::AlreadyClosed);
|
2025-02-06 08:52:49 +05:30
|
|
|
}
|
2026-08-21 14:20:50 -07:00
|
|
|
let ptr = self.inner.get().ok_or(SqliteError::SessionClosed)?;
|
2025-02-04 21:59:13 +05:30
|
|
|
|
2026-08-21 14:20:50 -07:00
|
|
|
session_buffer_op(ptr, ffi::sqlite3session_patchset)
|
2025-02-04 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn session_buffer_op(
|
|
|
|
|
s: *mut ffi::sqlite3_session,
|
|
|
|
|
f: unsafe extern "C" fn(
|
|
|
|
|
*mut ffi::sqlite3_session,
|
|
|
|
|
*mut i32,
|
|
|
|
|
*mut *mut c_void,
|
|
|
|
|
) -> i32,
|
|
|
|
|
) -> Result<Box<[u8]>, SqliteError> {
|
|
|
|
|
let mut n_buffer = 0;
|
|
|
|
|
let mut p_buffer = std::ptr::null_mut();
|
|
|
|
|
|
|
|
|
|
// Safety: `s` is a valid session and the buffer is allocated
|
|
|
|
|
// by sqlite3 and will be freed later.
|
|
|
|
|
let r = unsafe { f(s, &mut n_buffer, &mut p_buffer) };
|
|
|
|
|
if r != ffi::SQLITE_OK {
|
2025-06-02 10:44:52 -07:00
|
|
|
return Err(SqliteError::SessionChangesetFailed);
|
2025-02-04 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n_buffer == 0 {
|
|
|
|
|
return Ok(Default::default());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Safety: n_buffer is the size of the buffer.
|
|
|
|
|
let buffer = unsafe {
|
|
|
|
|
std::slice::from_raw_parts(p_buffer as *const u8, n_buffer as usize)
|
|
|
|
|
}
|
|
|
|
|
.to_vec()
|
|
|
|
|
.into_boxed_slice();
|
|
|
|
|
|
|
|
|
|
// Safety: free sqlite allocated buffer, we copied it into the JS buffer.
|
|
|
|
|
unsafe {
|
|
|
|
|
ffi::sqlite3_free(p_buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(buffer)
|
|
|
|
|
}
|