Skip to content

Commit 20f96d7

Browse files
committed
Remove the dependency of sqlite-wasm-vfs on sqlite-wasm-rs
1 parent 4961d22 commit 20f96d7

File tree

9 files changed

+591
-54
lines changed

9 files changed

+591
-54
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ include = [
6565
]
6666

6767
[patch.crates-io]
68+
rsqlite-vfs = { path = "./crates/rsqlite-vfs" }
6869
sqlite-wasm-rs = { path = "./" }
6970
sqlite-wasm-vfs = { path = "./crates/sqlite-wasm-vfs" }

crates/rsqlite-vfs/src/ffi.rs

Lines changed: 508 additions & 5 deletions
Large diffs are not rendered by default.

crates/sqlite-wasm-vfs/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlite-wasm-vfs"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2021"
55
authors = ["Spxg <unsafe@outlook.es>"]
66
readme = "README.md"
@@ -11,7 +11,7 @@ categories = ["development-tools::ffi", "wasm", "database"]
1111
keywords = ["sqlite", "sqlite-wasm", "wasm", "webassembly", "javascript"]
1212

1313
[dependencies]
14-
sqlite-wasm-rs = { version = "0.5.1", default-features = false }
14+
rsqlite-vfs = "0.1"
1515

1616
wasm-bindgen = "0.2.104"
1717
js-sys = "0.3.81"
@@ -33,6 +33,7 @@ indexed_db_futures = "0.6.4"
3333

3434
[dev-dependencies]
3535
wasm-bindgen-test = "0.3.54"
36+
sqlite-wasm-rs = { version = "0.5.1", default-features = false }
3637

3738
[package.metadata.docs.rs]
3839
targets = ["wasm32-unknown-unknown"]

crates/sqlite-wasm-vfs/src/relaxed_idb.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//!
1010
//! async fn open_db() {
1111
//! // install relaxed-idb persistent vfs and set as default vfs
12-
//! install_idb_vfs(&RelaxedIdbCfg::default(), true)
12+
//! install_idb_vfs::<ffi::WasmOsCallback>(&RelaxedIdbCfg::default(), true)
1313
//! .await
1414
//! .unwrap();
1515
//!
@@ -44,19 +44,18 @@
4444
//! It is particularly important to note that using it on multiple pages may cause DB corruption.
4545
//! It is recommended to use it in SharedWorker.
4646
47-
use sqlite_wasm_rs::{
48-
utils::{
49-
bail, check_db_and_page_size, check_import_db, check_option, check_result, register_vfs,
50-
registered_vfs, sqlite3_file, sqlite3_vfs, ImportDbError, MemChunksFile, OsCallback,
51-
RegisterVfsError, SQLiteIoMethods, SQLiteVfs, SQLiteVfsFile, VfsAppData, VfsError, VfsFile,
52-
VfsResult, VfsStore,
47+
use rsqlite_vfs::{
48+
bail, check_db_and_page_size, check_import_db, check_option, check_result,
49+
ffi::{
50+
sqlite3_file, sqlite3_vfs, SQLITE_ERROR, SQLITE_FCNTL_COMMIT_PHASETWO, SQLITE_FCNTL_PRAGMA,
51+
SQLITE_FCNTL_SYNC, SQLITE_IOERR, SQLITE_IOERR_DELETE, SQLITE_NOTFOUND, SQLITE_OK,
52+
SQLITE_OPEN_MAIN_DB,
5353
},
54-
WasmOsCallback, SQLITE_ERROR, SQLITE_FCNTL_COMMIT_PHASETWO, SQLITE_FCNTL_PRAGMA,
55-
SQLITE_FCNTL_SYNC, SQLITE_IOERR, SQLITE_IOERR_DELETE, SQLITE_NOTFOUND, SQLITE_OK,
56-
SQLITE_OPEN_MAIN_DB,
54+
register_vfs, registered_vfs, ImportDbError, MemChunksFile, OsCallback, RegisterVfsError,
55+
SQLiteIoMethods, SQLiteVfs, SQLiteVfsFile, VfsAppData, VfsError, VfsFile, VfsResult, VfsStore,
5756
};
58-
use std::cell::RefCell;
5957
use std::time::Duration;
58+
use std::{cell::RefCell, marker::PhantomData};
6059

6160
use indexed_db_futures::database::Database;
6261
use indexed_db_futures::prelude::*;
@@ -717,21 +716,24 @@ impl SQLiteIoMethods for RelaxedIdbIoMethods {
717716
}
718717
}
719718

720-
struct RelaxedIdbVfs;
719+
struct RelaxedIdbVfs<C>(PhantomData<C>);
721720

722-
impl SQLiteVfs<RelaxedIdbIoMethods> for RelaxedIdbVfs {
721+
impl<C> SQLiteVfs<RelaxedIdbIoMethods> for RelaxedIdbVfs<C>
722+
where
723+
C: OsCallback,
724+
{
723725
const VERSION: ::std::os::raw::c_int = 1;
724726

725727
fn sleep(dur: Duration) {
726-
WasmOsCallback::sleep(dur);
728+
C::sleep(dur);
727729
}
728730

729731
fn random(buf: &mut [u8]) {
730-
WasmOsCallback::random(buf);
732+
C::random(buf);
731733
}
732734

733735
fn epoch_timestamp_in_ms() -> i64 {
734-
WasmOsCallback::epoch_timestamp_in_ms()
736+
C::epoch_timestamp_in_ms()
735737
}
736738
}
737739

@@ -907,7 +909,10 @@ impl RelaxedIdbUtil {
907909
///
908910
/// If the vfs corresponding to `options.vfs_name` has been registered,
909911
/// only return a management tool without register.
910-
pub async fn install(options: &RelaxedIdbCfg, default_vfs: bool) -> Result<RelaxedIdbUtil> {
912+
pub async fn install<C: OsCallback>(
913+
options: &RelaxedIdbCfg,
914+
default_vfs: bool,
915+
) -> Result<RelaxedIdbUtil> {
911916
static REGISTER_GUARD: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
912917
let _guard = REGISTER_GUARD.lock().await;
913918

@@ -916,7 +921,7 @@ pub async fn install(options: &RelaxedIdbCfg, default_vfs: bool) -> Result<Relax
916921
} else {
917922
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
918923
let pool = RelaxedIdb::new(options, tx).await?;
919-
let vfs = register_vfs::<RelaxedIdbIoMethods, RelaxedIdbVfs>(
924+
let vfs = register_vfs::<RelaxedIdbIoMethods, RelaxedIdbVfs<C>>(
920925
&options.vfs_name,
921926
pool,
922927
default_vfs,
@@ -933,7 +938,7 @@ pub async fn install(options: &RelaxedIdbCfg, default_vfs: bool) -> Result<Relax
933938
#[cfg(test)]
934939
mod tests {
935940
use super::{IdbFile, RelaxedIdb, RelaxedIdbCfgBuilder, RelaxedIdbStore};
936-
use sqlite_wasm_rs::utils::{test_suite::test_vfs_store, VfsAppData};
941+
use rsqlite_vfs::{test_suite::test_vfs_store, VfsAppData};
937942
use wasm_bindgen_test::wasm_bindgen_test;
938943

939944
#[wasm_bindgen_test]

crates/sqlite-wasm-vfs/src/sahpool.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//!
99
//! async fn open_db() {
1010
//! // install opfs-sahpool persistent vfs and set as default vfs
11-
//! install_opfs_sahpool(&OpfsSAHPoolCfg::default(), true)
11+
//! install_opfs_sahpool::<ffi::WasmOsCallback>(&OpfsSAHPoolCfg::default(), true)
1212
//! .await
1313
//! .unwrap();
1414
//!
@@ -32,20 +32,23 @@
3232
//! [`opfs-explorer`](https://chromewebstore.google.com/detail/opfs-explorer/acndjpgkpaclldomagafnognkcgjignd)
3333
//! plugin to browse files.
3434
35-
use sqlite_wasm_rs::{
36-
utils::{
37-
check_import_db, register_vfs, registered_vfs, sqlite3_file, sqlite3_filename, sqlite3_vfs,
38-
sqlite3_vfs_register, sqlite3_vfs_unregister, ImportDbError, OsCallback, RegisterVfsError,
39-
SQLiteIoMethods, SQLiteVfs, SQLiteVfsFile, VfsAppData, VfsError, VfsFile, VfsResult,
40-
VfsStore,
35+
use rsqlite_vfs::{
36+
check_import_db,
37+
ffi::{
38+
sqlite3_file, sqlite3_filename, sqlite3_vfs, sqlite3_vfs_register, sqlite3_vfs_unregister,
39+
SQLITE_CANTOPEN, SQLITE_ERROR, SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN, SQLITE_IOERR,
40+
SQLITE_IOERR_DELETE, SQLITE_OK, SQLITE_OPEN_DELETEONCLOSE, SQLITE_OPEN_MAIN_DB,
41+
SQLITE_OPEN_MAIN_JOURNAL, SQLITE_OPEN_SUPER_JOURNAL, SQLITE_OPEN_WAL,
4142
},
42-
WasmOsCallback, SQLITE_CANTOPEN, SQLITE_ERROR, SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN,
43-
SQLITE_IOERR, SQLITE_IOERR_DELETE, SQLITE_OK, SQLITE_OPEN_DELETEONCLOSE, SQLITE_OPEN_MAIN_DB,
44-
SQLITE_OPEN_MAIN_JOURNAL, SQLITE_OPEN_SUPER_JOURNAL, SQLITE_OPEN_WAL,
43+
register_vfs, registered_vfs, ImportDbError, OsCallback, RegisterVfsError, SQLiteIoMethods,
44+
SQLiteVfs, SQLiteVfsFile, VfsAppData, VfsError, VfsFile, VfsResult, VfsStore,
4545
};
46-
use std::cell::{Cell, RefCell};
4746
use std::collections::{HashMap, HashSet};
4847
use std::time::Duration;
48+
use std::{
49+
cell::{Cell, RefCell},
50+
marker::PhantomData,
51+
};
4952

5053
use js_sys::{Array, DataView, IteratorNext, Reflect, Uint8Array};
5154
use wasm_bindgen::{JsCast, JsValue};
@@ -97,10 +100,11 @@ struct OpfsSAHPool {
97100
open_files: RefCell<HashSet<String>>,
98101
/// A tuple holding the raw pointer to the `sqlite3_vfs` struct and whether it was registered as the default.
99102
vfs: Cell<(*mut sqlite3_vfs, bool)>,
103+
random: fn(&mut [u8]),
100104
}
101105

102106
impl OpfsSAHPool {
103-
async fn new(options: &OpfsSAHPoolCfg) -> Result<OpfsSAHPool> {
107+
async fn new<C: OsCallback>(options: &OpfsSAHPoolCfg) -> Result<OpfsSAHPool> {
104108
const OPAQUE_DIR_NAME: &str = ".opaque";
105109

106110
let vfs_dir = &options.directory;
@@ -154,6 +158,7 @@ impl OpfsSAHPool {
154158
is_paused: Cell::new(false),
155159
open_files: RefCell::new(HashSet::new()),
156160
vfs: Cell::new((std::ptr::null_mut(), false)),
161+
random: C::random,
157162
};
158163

159164
pool.acquire_access_handles(clear_files).await?;
@@ -164,7 +169,7 @@ impl OpfsSAHPool {
164169

165170
async fn add_capacity(&self, n: u32) -> Result<u32> {
166171
for _ in 0..n {
167-
let opaque = sqlite_wasm_rs::utils::random_name(WasmOsCallback::random);
172+
let opaque = rsqlite_vfs::random_name(self.random);
168173
let handle: FileSystemFileHandle =
169174
JsFuture::from(self.dh_opaque.get_file_handle_with_options(&opaque, &{
170175
let options = FileSystemGetFileOptions::new();
@@ -670,9 +675,12 @@ impl SQLiteIoMethods for SyncAccessHandleIoMethods {
670675
}
671676
}
672677

673-
struct SyncAccessHandleVfs;
678+
struct SyncAccessHandleVfs<C>(PhantomData<C>);
674679

675-
impl SQLiteVfs<SyncAccessHandleIoMethods> for SyncAccessHandleVfs {
680+
impl<C> SQLiteVfs<SyncAccessHandleIoMethods> for SyncAccessHandleVfs<C>
681+
where
682+
C: OsCallback,
683+
{
676684
const VERSION: ::std::os::raw::c_int = 2;
677685
const MAX_PATH_SIZE: ::std::os::raw::c_int = HEADER_MAX_FILENAME_SIZE as _;
678686

@@ -698,15 +706,15 @@ impl SQLiteVfs<SyncAccessHandleIoMethods> for SyncAccessHandleVfs {
698706
}
699707

700708
fn sleep(dur: Duration) {
701-
WasmOsCallback::sleep(dur);
709+
C::sleep(dur);
702710
}
703711

704712
fn random(buf: &mut [u8]) {
705-
WasmOsCallback::random(buf);
713+
C::random(buf);
706714
}
707715

708716
fn epoch_timestamp_in_ms() -> i64 {
709-
WasmOsCallback::epoch_timestamp_in_ms()
717+
C::epoch_timestamp_in_ms()
710718
}
711719
}
712720

@@ -946,15 +954,18 @@ impl OpfsSAHPoolUtil {
946954
///
947955
/// If the vfs corresponding to `options.vfs_name` has been registered,
948956
/// only return a management tool without register.
949-
pub async fn install(options: &OpfsSAHPoolCfg, default_vfs: bool) -> Result<OpfsSAHPoolUtil> {
957+
pub async fn install<C: OsCallback>(
958+
options: &OpfsSAHPoolCfg,
959+
default_vfs: bool,
960+
) -> Result<OpfsSAHPoolUtil> {
950961
static REGISTER_GUARD: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
951962
let _guard = REGISTER_GUARD.lock().await;
952963

953964
let vfs = match registered_vfs(&options.vfs_name)? {
954965
Some(vfs) => vfs,
955-
None => register_vfs::<SyncAccessHandleIoMethods, SyncAccessHandleVfs>(
966+
None => register_vfs::<SyncAccessHandleIoMethods, SyncAccessHandleVfs<C>>(
956967
&options.vfs_name,
957-
OpfsSAHPool::new(options).await?,
968+
OpfsSAHPool::new::<C>(options).await?,
958969
default_vfs,
959970
)?,
960971
};
@@ -971,12 +982,12 @@ mod tests {
971982
OpfsSAHPool, OpfsSAHPoolCfgBuilder, SyncAccessFile, SyncAccessHandleAppData,
972983
SyncAccessHandleStore,
973984
};
974-
use sqlite_wasm_rs::utils::{test_suite::test_vfs_store, VfsAppData};
985+
use rsqlite_vfs::{test_suite::test_vfs_store, VfsAppData};
975986
use wasm_bindgen_test::wasm_bindgen_test;
976987

977988
#[wasm_bindgen_test]
978989
async fn test_opfs_vfs_store() {
979-
let data = OpfsSAHPool::new(
990+
let data = OpfsSAHPool::new::<sqlite_wasm_rs::WasmOsCallback>(
980991
&OpfsSAHPoolCfgBuilder::new()
981992
.directory("test_opfs_suite")
982993
.build(),

tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8-
sqlite-wasm-vfs = "0.1.0"
8+
sqlite-wasm-vfs = "0.2.0"
99
sqlite-wasm-rs = "0.5.0"
1010
wasm-bindgen-test = "0.3.54"
1111

tests/tests/full/sqlite3mc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ unsafe fn test_memvfs_cipher(cipher: &str) {
7272
}
7373

7474
async unsafe fn test_relaxed_idb_vfs_cipher(cipher: &str) {
75-
let util = sqlite_wasm_vfs::relaxed_idb::install(
75+
let util = sqlite_wasm_vfs::relaxed_idb::install::<sqlite_wasm_rs::WasmOsCallback>(
7676
&sqlite_wasm_vfs::relaxed_idb::RelaxedIdbCfgBuilder::new()
7777
.vfs_name("relaxed-db-cipher")
7878
.clear_on_init(true)
@@ -126,7 +126,7 @@ async unsafe fn test_relaxed_idb_vfs_cipher(cipher: &str) {
126126
}
127127

128128
async unsafe fn test_opfs_sah_vfs_cipher(cipher: &str) {
129-
let util = sqlite_wasm_vfs::sahpool::install(
129+
let util = sqlite_wasm_vfs::sahpool::install::<sqlite_wasm_rs::WasmOsCallback>(
130130
&OpfsSAHPoolCfgBuilder::new()
131131
.vfs_name("sah-cipher")
132132
.directory("sah-cipher")

tests/tests/full/vfs/relaxed_idb.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
use sqlite_wasm_rs::*;
22
use sqlite_wasm_vfs::relaxed_idb::{
3-
install as install_idb_vfs, Preload, RelaxedIdbCfg, RelaxedIdbCfgBuilder,
3+
install, Preload, RelaxedIdbCfg, RelaxedIdbCfgBuilder, RelaxedIdbError, RelaxedIdbUtil,
44
};
55
use wasm_bindgen_test::wasm_bindgen_test;
66

77
use crate::full::{check_persistent, prepare_simple_db};
88

9+
pub async fn install_idb_vfs(
10+
options: &RelaxedIdbCfg,
11+
default_vfs: bool,
12+
) -> Result<RelaxedIdbUtil, RelaxedIdbError> {
13+
install::<sqlite_wasm_rs::WasmOsCallback>(options, default_vfs).await
14+
}
15+
916
#[wasm_bindgen_test]
1017
async fn test_idb_vfs_default() {
1118
install_idb_vfs(&RelaxedIdbCfg::default(), true)

tests/tests/full/vfs/sahpool.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
use sqlite_wasm_rs::*;
2-
use sqlite_wasm_vfs::sahpool::{install as install_opfs_sahpool, *};
2+
use sqlite_wasm_vfs::sahpool::{
3+
install, OpfsSAHError, OpfsSAHPoolCfg, OpfsSAHPoolCfgBuilder, OpfsSAHPoolUtil,
4+
};
35
use wasm_bindgen_test::wasm_bindgen_test;
46

7+
pub async fn install_opfs_sahpool(
8+
options: &OpfsSAHPoolCfg,
9+
default_vfs: bool,
10+
) -> Result<OpfsSAHPoolUtil, OpfsSAHError> {
11+
install::<sqlite_wasm_rs::WasmOsCallback>(options, default_vfs).await
12+
}
13+
514
use crate::full::{check_persistent, prepare_simple_db};
615

716
#[wasm_bindgen_test]

0 commit comments

Comments
 (0)