Skip to content

Commit

Permalink
Use export_room_keys_stream to reduce export memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam committed Feb 20, 2024
1 parent 9716f14 commit 331474c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ js-sys = "0.3.49"
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk", features = ["js"] }
matrix-sdk-indexeddb = { git = "https://github.com/matrix-org/matrix-rust-sdk", default-features = false, features = ["e2e-encryption"] }
matrix-sdk-qrcode = { git = "https://github.com/matrix-org/matrix-rust-sdk", optional = true }
serde = "1.0.91"
serde_json = "1.0.91"
serde-wasm-bindgen = "0.5.0"
tracing = { version = "0.1.36", default-features = false, features = ["std"] }
Expand Down
34 changes: 28 additions & 6 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use std::{
collections::{BTreeMap, HashSet},
ops::Deref,
pin::{pin, Pin},
time::Duration,
};

use futures_util::{pin_mut, StreamExt};
use futures_util::{pin_mut, Stream, StreamExt};
use js_sys::{Array, Function, JsString, Map, Promise, Set};
use matrix_sdk_common::ruma::{
self, events::secret::request::SecretName, serde::Raw, DeviceKeyAlgorithm, OwnedDeviceId,
Expand All @@ -19,8 +20,9 @@ use matrix_sdk_crypto::{
types::RoomKeyBackupInfo,
CryptoStoreError, EncryptionSyncChanges, GossippedSecret,
};
use serde::{ser::SerializeSeq, Serialize, Serializer};
use serde_json::json;
use serde_wasm_bindgen;
use serde_wasm_bindgen::{self};
use tracing::warn;
use wasm_bindgen::{convert::TryFromJsValue, prelude::*};
use wasm_bindgen_futures::{spawn_local, JsFuture};
Expand Down Expand Up @@ -883,14 +885,17 @@ impl OlmMachine {
/// `predicate` is a closure that will be called for every known
/// `InboundGroupSession`, which represents a room key. If the closure
/// returns `true`, the `InboundGroupSession` will be included in the
/// export, otherwise it won't.
/// export; otherwise it won't.
///
/// Returns a Promise containing a Result containing a String which is a
/// JSON-encoded array of ExportedRoomKey objects.
#[wasm_bindgen(js_name = "exportRoomKeys")]
pub fn export_room_keys(&self, predicate: Function) -> Promise {
let me = self.inner.clone();

future_to_promise(async move {
Ok(serde_json::to_string(
&me.export_room_keys(|session| {
stream_to_json_array(pin!(
me.export_room_keys_stream(|session| {
let session = session.clone();

predicate
Expand All @@ -900,7 +905,8 @@ impl OlmMachine {
.unwrap_or(false)
})
.await?,
)?)
))
.await
})
}

Expand Down Expand Up @@ -1571,3 +1577,19 @@ pub(crate) async fn promise_result_to_future(
}
}
}

async fn stream_to_json_array<T, S>(mut stream: Pin<&mut S>) -> Result<String, anyhow::Error>
where
T: Serialize,
S: Stream<Item = T>,
{
let mut stream_json = vec![];
let mut ser = serde_json::Serializer::new(&mut stream_json);
let mut seq = ser.serialize_seq(None).map_err(|e| anyhow::Error::msg(e.to_string()))?;
while let Some(key) = stream.next().await {
seq.serialize_element(&key).map_err(|e| anyhow::Error::msg(e.to_string()))?;
}
seq.end().map_err(|e| anyhow::Error::msg(e.to_string()))?;

Ok(String::from_utf8(stream_json)?)
}

0 comments on commit 331474c

Please sign in to comment.