Merge pull request #3026 from matrix-org/rav/remove_base_dep

indexeddb, sqlite: Avoid dependency on `matrix-sdk-base`
This commit is contained in:
Richard van der Hoff
2024-01-17 09:19:15 +00:00
committed by GitHub
5 changed files with 76 additions and 85 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = ["e2e-encryption", "state-store"]
state-store = ["dep:matrix-sdk-base"]
e2e-encryption = ["matrix-sdk-base?/e2e-encryption", "dep:matrix-sdk-crypto"]
e2e-encryption = ["dep:matrix-sdk-crypto"]
testing = ["matrix-sdk-crypto?/testing"]
[dependencies]
+13 -29
View File
@@ -1,7 +1,7 @@
#![cfg_attr(not(target_arch = "wasm32"), allow(unused))]
#[cfg(feature = "state-store")]
use matrix_sdk_base::store::{StoreConfig, StoreError};
use matrix_sdk_base::store::StoreError;
use thiserror::Error;
#[cfg(feature = "e2e-encryption")]
@@ -23,7 +23,7 @@ pub use state_store::{
/// Create a [`IndexeddbStateStore`] and a [`IndexeddbCryptoStore`] that use the
/// same name and passphrase.
#[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
async fn open_stores_with_name(
pub async fn open_stores_with_name(
name: &str,
passphrase: Option<&str>,
) -> Result<(IndexeddbStateStore, IndexeddbCryptoStore), OpenStoreError> {
@@ -40,38 +40,22 @@ async fn open_stores_with_name(
Ok((state_store, crypto_store))
}
/// Create a [`StoreConfig`] with an opened indexeddb [`IndexeddbStateStore`]
/// that uses the given name and passphrase. If `encryption` is enabled, a
/// [`IndexeddbCryptoStore`] with the same parameters is also opened.
/// Create an [`IndexeddbStateStore`].
///
/// If a `passphrase` is given, the store will be encrypted using a key derived
/// from that passphrase.
#[cfg(feature = "state-store")]
pub async fn make_store_config(
pub async fn open_state_store(
name: &str,
passphrase: Option<&str>,
) -> Result<StoreConfig, OpenStoreError> {
#[cfg(target_arch = "wasm32")]
{
#[cfg(feature = "e2e-encryption")]
{
let (state_store, crypto_store) = open_stores_with_name(name, passphrase).await?;
Ok(StoreConfig::new().state_store(state_store).crypto_store(crypto_store))
}
#[cfg(not(feature = "e2e-encryption"))]
{
let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
if let Some(passphrase) = passphrase {
builder = builder.passphrase(passphrase.to_owned());
}
let state_store = builder.build().await.map_err(StoreError::from)?;
Ok(StoreConfig::new().state_store(state_store))
}
) -> Result<IndexeddbStateStore, OpenStoreError> {
let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
if let Some(passphrase) = passphrase {
builder = builder.passphrase(passphrase.to_owned());
}
let state_store = builder.build().await.map_err(StoreError::from)?;
#[cfg(not(target_arch = "wasm32"))]
panic!("the IndexedDB is only available on the 'wasm32' arch")
Ok(state_store)
}
/// All the errors that can occur when opening an IndexedDB store.
+3 -6
View File
@@ -12,17 +12,14 @@ default = ["state-store"]
testing = ["matrix-sdk-crypto?/testing"]
bundled = ["rusqlite/bundled"]
crypto-store = [
"dep:matrix-sdk-crypto",
"matrix-sdk-base/e2e-encryption",
]
state-store = []
crypto-store = ["dep:matrix-sdk-crypto"]
state-store = ["dep:matrix-sdk-base"]
[dependencies]
async-trait = { workspace = true }
deadpool-sqlite = "0.7.0"
itertools = { workspace = true }
matrix-sdk-base = { workspace = true }
matrix-sdk-base = { workspace = true, optional = true }
matrix-sdk-crypto = { workspace = true, optional = true }
matrix-sdk-store-encryption = { workspace = true }
rmp-serde = "1.1.1"
-26
View File
@@ -16,10 +16,7 @@
allow(dead_code, unused_imports)
)]
use std::path::Path;
use deadpool_sqlite::Object as SqliteConn;
use matrix_sdk_base::store::StoreConfig;
use matrix_sdk_store_encryption::StoreCipher;
#[cfg(feature = "crypto-store")]
@@ -59,26 +56,3 @@ async fn get_or_create_store_cipher(
#[cfg(test)]
matrix_sdk_test::init_tracing_for_tests!();
/// Create a [`StoreConfig`] with an opened [`SqliteStateStore`] in the given
/// directory and using the given passphrase. If the `crypto-store` feature is
/// enabled, a [`SqliteCryptoStore`] with the same parameters is also opened.
#[cfg(feature = "state-store")]
pub async fn make_store_config(
path: &Path,
passphrase: Option<&str>,
) -> Result<StoreConfig, OpenStoreError> {
let state_store = SqliteStateStore::open(path, passphrase).await?;
let config = StoreConfig::new().state_store(state_store);
#[cfg(feature = "crypto-store")]
{
let crypto_store = SqliteCryptoStore::open(path, passphrase).await?;
Ok(config.crypto_store(crypto_store))
}
#[cfg(not(feature = "crypto-store"))]
{
Ok(config)
}
}
+59 -23
View File
@@ -165,11 +165,6 @@ impl ClientBuilder {
}
/// Set up the store configuration for a SQLite store.
///
/// This is the same as
/// <code>.[store_config](Self::store_config)([matrix_sdk_sqlite]::[make_store_config](matrix_sdk_sqlite::make_store_config)(path, passphrase)?)</code>.
/// except it delegates the actual store config creation to when
/// `.build().await` is called.
#[cfg(feature = "sqlite")]
pub fn sqlite_store(
mut self,
@@ -184,11 +179,6 @@ impl ClientBuilder {
}
/// Set up the store configuration for a IndexedDB store.
///
/// This is the same as
/// <code>.[store_config](Self::store_config)([matrix_sdk_indexeddb]::[make_store_config](matrix_sdk_indexeddb::make_store_config)(path, passphrase).await?)</code>,
/// except it delegates the actual store config creation to when
/// `.build().await` is called.
#[cfg(feature = "indexeddb")]
pub fn indexeddb_store(mut self, name: &str, passphrase: Option<&str>) -> Self {
self.store_config = BuilderStoreConfig::IndexedDb {
@@ -368,19 +358,7 @@ impl ClientBuilder {
let base_client = if let Some(base_client) = self.base_client {
base_client
} else {
#[allow(clippy::infallible_destructuring_match)]
let store_config = match self.store_config {
#[cfg(feature = "sqlite")]
BuilderStoreConfig::Sqlite { path, passphrase } => {
matrix_sdk_sqlite::make_store_config(&path, passphrase.as_deref()).await?
}
#[cfg(feature = "indexeddb")]
BuilderStoreConfig::IndexedDb { name, passphrase } => {
matrix_sdk_indexeddb::make_store_config(&name, passphrase.as_deref()).await?
}
BuilderStoreConfig::Custom(config) => config,
};
BaseClient::with_store_config(store_config)
BaseClient::with_store_config(build_store_config(self.store_config).await?)
};
let http_client = HttpClient::new(inner_http_client.clone(), self.request_config);
@@ -483,6 +461,64 @@ impl ClientBuilder {
}
}
async fn build_store_config(
builder_config: BuilderStoreConfig,
) -> Result<StoreConfig, ClientBuildError> {
#[allow(clippy::infallible_destructuring_match)]
let store_config = match builder_config {
#[cfg(feature = "sqlite")]
BuilderStoreConfig::Sqlite { path, passphrase } => {
let store_config = StoreConfig::new().state_store(
matrix_sdk_sqlite::SqliteStateStore::open(&path, passphrase.as_deref()).await?,
);
#[cfg(feature = "e2e-encryption")]
let store_config = store_config.crypto_store(
matrix_sdk_sqlite::SqliteCryptoStore::open(&path, passphrase.as_deref()).await?,
);
store_config
}
#[cfg(feature = "indexeddb")]
BuilderStoreConfig::IndexedDb { name, passphrase } => {
build_indexeddb_store_config(&name, passphrase.as_deref()).await?
}
BuilderStoreConfig::Custom(config) => config,
};
Ok(store_config)
}
// The indexeddb stores only implement `IntoStateStore` and `IntoCryptoStore` on
// wasm32, so this only compiles there.
#[cfg(all(target_arch = "wasm32", feature = "indexeddb"))]
async fn build_indexeddb_store_config(
name: &str,
passphrase: Option<&str>,
) -> Result<StoreConfig, ClientBuildError> {
#[cfg(feature = "e2e-encryption")]
{
let (state_store, crypto_store) =
matrix_sdk_indexeddb::open_stores_with_name(name, passphrase).await?;
Ok(StoreConfig::new().state_store(state_store).crypto_store(crypto_store))
}
#[cfg(not(feature = "e2e-encryption"))]
{
let state_store = matrix_sdk_indexeddb::open_state_store(name, passphrase).await?;
Ok(StoreConfig::new().state_store(state_store))
}
}
#[cfg(all(not(target_arch = "wasm32"), feature = "indexeddb"))]
async fn build_indexeddb_store_config(
_name: &str,
_passphrase: Option<&str>,
) -> Result<StoreConfig, ClientBuildError> {
panic!("the IndexedDB is only available on the 'wasm32' arch")
}
#[derive(Clone, Copy, Debug)]
enum UrlScheme {
Http,