From ef6104bc532404de603f4a9f40f5b74933de23ee Mon Sep 17 00:00:00 2001 From: stoically Date: Fri, 8 May 2020 16:12:21 +0200 Subject: [PATCH 1/8] matrix-sdk: Support compiling to wasm32-unknown-unknown --- .travis.yml | 22 ++++ Cargo.toml | 4 + matrix_sdk/Cargo.toml | 10 +- .../examples/wasm_send_message/.gitignore | 7 ++ .../examples/wasm_send_message/Cargo.toml | 21 ++++ .../examples/wasm_send_message/README.md | 13 ++ .../examples/wasm_send_message/index.js | 7 ++ .../examples/wasm_send_message/package.json | 14 +++ .../examples/wasm_send_message/src/lib.rs | 37 ++++++ .../wasm_send_message/webpack.config.js | 26 ++++ matrix_sdk/src/client.rs | 90 +++++++------- matrix_sdk/src/lib.rs | 4 +- matrix_sdk_base/.cargo/config | 3 + matrix_sdk_base/Cargo.toml | 10 +- matrix_sdk_base/src/client.rs | 12 +- matrix_sdk_base/src/event_emitter/mod.rs | 31 ++--- matrix_sdk_base/src/lib.rs | 4 +- matrix_sdk_base/src/models/message.rs | 3 + matrix_sdk_base/src/models/room.rs | 48 +++----- matrix_sdk_base/src/models/room_member.rs | 25 ++-- matrix_sdk_base/src/state/mod.rs | 2 + matrix_sdk_base/src/state/state_store.rs | 2 +- matrix_sdk_common/Cargo.toml | 12 ++ matrix_sdk_common/src/lib.rs | 4 + matrix_sdk_common/src/locks.rs | 13 ++ matrix_sdk_crypto/Cargo.toml | 11 +- matrix_sdk_crypto/src/machine.rs | 2 +- matrix_sdk_crypto/src/memory_stores.rs | 2 +- matrix_sdk_crypto/src/olm.rs | 2 +- matrix_sdk_crypto/src/store/memorystore.rs | 2 +- matrix_sdk_crypto/src/store/mod.rs | 2 +- matrix_sdk_crypto/src/store/sqlite.rs | 2 +- matrix_sdk_test/Cargo.toml | 1 + matrix_sdk_test/src/lib.rs | 111 ++++++++++++++---- matrix_sdk_test_macros/Cargo.toml | 14 +++ matrix_sdk_test_macros/src/lib.rs | 14 +++ 36 files changed, 425 insertions(+), 162 deletions(-) create mode 100644 matrix_sdk/examples/wasm_send_message/.gitignore create mode 100644 matrix_sdk/examples/wasm_send_message/Cargo.toml create mode 100644 matrix_sdk/examples/wasm_send_message/README.md create mode 100644 matrix_sdk/examples/wasm_send_message/index.js create mode 100644 matrix_sdk/examples/wasm_send_message/package.json create mode 100644 matrix_sdk/examples/wasm_send_message/src/lib.rs create mode 100644 matrix_sdk/examples/wasm_send_message/webpack.config.js create mode 100644 matrix_sdk_base/.cargo/config create mode 100644 matrix_sdk_common/src/locks.rs create mode 100644 matrix_sdk_test_macros/Cargo.toml create mode 100644 matrix_sdk_test_macros/src/lib.rs diff --git a/.travis.yml b/.travis.yml index b210cbf8..ebb54d65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,28 @@ jobs: after_success: - bash <(curl -s https://codecov.io/bash) + - os: linux + name: wasm32-unknown-unknown + before_script: + - | + cargo install wasm-bindgen-cli + rustup target add wasm32-unknown-unknown + wget https://github.com/emscripten-core/emsdk/archive/master.zip + unzip master.zip + ./emsdk-master/emsdk install latest + ./emsdk-master/emsdk activate latest + script: + - | + source emsdk-master/emsdk_env.sh + cd matrix_sdk/examples/wasm_send_message + cargo build --target wasm32-unknown-unknown + cd - + + cd matrix_sdk_base + cargo test --target wasm32-unknown-unknown --no-default-features --features encryption + + + before_script: - rustup component add rustfmt diff --git a/Cargo.toml b/Cargo.toml index 2ec87410..d4cd4f2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,10 @@ members = [ "matrix_sdk", "matrix_sdk_base", "matrix_sdk_test", + "matrix_sdk_test_macros", "matrix_sdk_crypto", "matrix_sdk_common", ] + +[patch.crates-io] +olm-sys = { git = "https://gitlab.gnome.org/stoically/olm-sys", branch = "wasm-target" } diff --git a/matrix_sdk/Cargo.toml b/matrix_sdk/Cargo.toml index 53af0ca9..8c36b379 100644 --- a/matrix_sdk/Cargo.toml +++ b/matrix_sdk/Cargo.toml @@ -17,14 +17,14 @@ encryption = ["matrix-sdk-base/encryption"] sqlite-cryptostore = ["matrix-sdk-base/sqlite-cryptostore"] [dependencies] -futures = "0.3.4" http = "0.2.1" reqwest = "0.10.4" serde_json = "1.0.52" thiserror = "1.0.16" tracing = "0.1.13" url = "2.1.1" -uuid = { version = "0.8.1", features = ["v4"] } +futures-timer = "3.0.2" + matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } @@ -38,11 +38,6 @@ version = "0.2.4" default-features = false features = ["std", "std-future"] -[dependencies.tokio] -version = "0.2.20" -default-features = false -features = ["time"] - [dev-dependencies] async-trait = "0.1.30" dirs = "2.0.2" @@ -54,3 +49,4 @@ tracing-subscriber = "0.2.5" tempfile = "3.1.0" mockito = "0.25.1" lazy_static = "1.4.0" +futures = "0.3.4" diff --git a/matrix_sdk/examples/wasm_send_message/.gitignore b/matrix_sdk/examples/wasm_send_message/.gitignore new file mode 100644 index 00000000..dfa8ad9d --- /dev/null +++ b/matrix_sdk/examples/wasm_send_message/.gitignore @@ -0,0 +1,7 @@ +node_modules +pkg +target +Cargo.lock +*.swp +dist +package-lock.json \ No newline at end of file diff --git a/matrix_sdk/examples/wasm_send_message/Cargo.toml b/matrix_sdk/examples/wasm_send_message/Cargo.toml new file mode 100644 index 00000000..3174fff5 --- /dev/null +++ b/matrix_sdk/examples/wasm_send_message/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "wasm" +version = "0.1.0" +authors = ["stoically "] +edition = "2018" + +# Config mostly pulled from: https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/Cargo.toml + +[lib] +crate-type = ["cdylib"] + +[dependencies] +matrix-sdk = { path = "../..", default-features = false, features = ["encryption"] } +url = "2.1.1" +wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] } +wasm-bindgen-futures = "0.4.12" + +[workspace] + +[patch.crates-io] +olm-sys = { git = "https://gitlab.gnome.org/stoically/olm-sys", branch = "wasm-target" } diff --git a/matrix_sdk/examples/wasm_send_message/README.md b/matrix_sdk/examples/wasm_send_message/README.md new file mode 100644 index 00000000..84d7d87a --- /dev/null +++ b/matrix_sdk/examples/wasm_send_message/README.md @@ -0,0 +1,13 @@ +## Example usage of matrix-rust-sdk from WASM + +This requires [emscripten](https://github.com/emscripten-core/emscripten) to be installed and in PATH to compile successfully. + +You can build the example locally with: + + npm install + npm run serve + +and then visiting http://localhost:8080 in a browser should run the example! + + +This example is loosely based off of [this example](https://github.com/seanmonstar/reqwest/tree/master/examples/wasm_github_fetch), an example usage of `fetch` from `wasm-bindgen`. \ No newline at end of file diff --git a/matrix_sdk/examples/wasm_send_message/index.js b/matrix_sdk/examples/wasm_send_message/index.js new file mode 100644 index 00000000..74ac1d78 --- /dev/null +++ b/matrix_sdk/examples/wasm_send_message/index.js @@ -0,0 +1,7 @@ +const rust = import('./pkg'); + +rust + .then(m => { + return m.run() + }) + .catch(console.error); \ No newline at end of file diff --git a/matrix_sdk/examples/wasm_send_message/package.json b/matrix_sdk/examples/wasm_send_message/package.json new file mode 100644 index 00000000..2a84b1f7 --- /dev/null +++ b/matrix_sdk/examples/wasm_send_message/package.json @@ -0,0 +1,14 @@ +{ + "scripts": { + "build": "webpack", + "serve": "webpack-dev-server" + }, + "devDependencies": { + "@wasm-tool/wasm-pack-plugin": "1.0.1", + "text-encoding": "^0.7.0", + "html-webpack-plugin": "^3.2.0", + "webpack": "^4.29.4", + "webpack-cli": "^3.1.1", + "webpack-dev-server": "^3.1.0" + } +} diff --git a/matrix_sdk/examples/wasm_send_message/src/lib.rs b/matrix_sdk/examples/wasm_send_message/src/lib.rs new file mode 100644 index 00000000..7e4a58e9 --- /dev/null +++ b/matrix_sdk/examples/wasm_send_message/src/lib.rs @@ -0,0 +1,37 @@ +use matrix_sdk::{ + events::room::message::{MessageEventContent, TextMessageEventContent}, + identifiers::RoomId, + Client, ClientConfig, +}; +use std::convert::TryFrom; +use url::Url; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub async fn run() -> Result { + let homeserver_url = "http://localhost:8008"; + let username = "user"; + let password = "password"; + + let client_config = ClientConfig::new(); + let homeserver_url = Url::parse(&homeserver_url).unwrap(); + let client = Client::new_with_config(homeserver_url, None, client_config).unwrap(); + + client + .login(username, password, None, Some("rust-sdk")) + .await + .unwrap(); + + let room_id = RoomId::try_from("!KpLWMcXcHKDMfEYNqA:localhost").unwrap(); + + let content = MessageEventContent::Text(TextMessageEventContent { + body: "hello from wasm".to_string(), + format: None, + formatted_body: None, + relates_to: None, + }); + + client.room_send(&room_id, content, None).await.unwrap(); + + Ok(JsValue::NULL) +} diff --git a/matrix_sdk/examples/wasm_send_message/webpack.config.js b/matrix_sdk/examples/wasm_send_message/webpack.config.js new file mode 100644 index 00000000..c77c1420 --- /dev/null +++ b/matrix_sdk/examples/wasm_send_message/webpack.config.js @@ -0,0 +1,26 @@ +const path = require('path'); +const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpack = require('webpack'); +const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin"); + +module.exports = { + entry: './index.js', + output: { + path: path.resolve(__dirname, 'dist'), + filename: 'index.js', + }, + devtool: 'source-map', + plugins: [ + new HtmlWebpackPlugin(), + new WasmPackPlugin({ + crateDirectory: path.resolve(__dirname, ".") + }), + // Have this example work in Edge which doesn't ship `TextEncoder` or + // `TextDecoder` at this time. + new webpack.ProvidePlugin({ + TextDecoder: ['text-encoding', 'TextDecoder'], + TextEncoder: ['text-encoding', 'TextEncoder'] + }) + ], + mode: 'development' +}; \ No newline at end of file diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 27098c8a..6c3fe1f0 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -21,18 +21,18 @@ use std::result::Result as StdResult; use std::sync::Arc; use std::time::{Duration, Instant}; -use uuid::Uuid; +use matrix_sdk_common::locks::RwLock; +use matrix_sdk_common::uuid::Uuid; -use futures::future::Future; -use tokio::sync::RwLock; -use tokio::time::delay_for as sleep; +use futures_timer::Delay as sleep; +use std::future::Future; #[cfg(feature = "encryption")] use tracing::{debug, warn}; use tracing::{info, instrument, trace}; use http::Method as HttpMethod; use http::Response as HttpResponse; -use reqwest::header::{HeaderValue, InvalidHeaderValue}; +use reqwest::header::{HeaderValue, InvalidHeaderValue, AUTHORIZATION}; use url::Url; use crate::events::room::message::MessageEventContent; @@ -98,6 +98,7 @@ impl std::fmt::Debug for Client { /// .state_store(Box::new(store)); /// ``` pub struct ClientConfig { + #[cfg(not(target_arch = "wasm32"))] proxy: Option, user_agent: Option, disable_ssl_verification: bool, @@ -106,9 +107,12 @@ pub struct ClientConfig { impl std::fmt::Debug for ClientConfig { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> StdResult<(), std::fmt::Error> { - fmt.debug_struct("ClientConfig") - .field("proxy", &self.proxy) - .field("user_agent", &self.user_agent) + let mut res = fmt.debug_struct("ClientConfig"); + + #[cfg(not(target_arch = "wasm32"))] + let res = res.field("proxy", &self.proxy); + + res.field("user_agent", &self.user_agent) .field("disable_ssl_verification", &self.disable_ssl_verification) .finish() } @@ -137,6 +141,7 @@ impl ClientConfig { /// .proxy("http://localhost:8080") /// .unwrap(); /// ``` + #[cfg(not(target_arch = "wasm32"))] pub fn proxy(mut self, proxy: &str) -> Result { self.proxy = Some(reqwest::Proxy::all(proxy)?); Ok(self) @@ -262,27 +267,32 @@ impl Client { let http_client = reqwest::Client::builder(); - let http_client = if config.disable_ssl_verification { - http_client.danger_accept_invalid_certs(true) - } else { - http_client + #[cfg(not(target_arch = "wasm32"))] + let http_client = { + let http_client = if config.disable_ssl_verification { + http_client.danger_accept_invalid_certs(true) + } else { + http_client + }; + + let http_client = match config.proxy { + Some(p) => http_client.proxy(p), + None => http_client, + }; + + let mut headers = reqwest::header::HeaderMap::new(); + + let user_agent = match config.user_agent { + Some(a) => a, + None => HeaderValue::from_str(&format!("matrix-rust-sdk {}", VERSION)).unwrap(), + }; + + headers.insert(reqwest::header::USER_AGENT, user_agent); + + http_client.default_headers(headers) }; - let http_client = match config.proxy { - Some(p) => http_client.proxy(p), - None => http_client, - }; - - let mut headers = reqwest::header::HeaderMap::new(); - - let user_agent = match config.user_agent { - Some(a) => a, - None => HeaderValue::from_str(&format!("matrix-rust-sdk {}", VERSION)).unwrap(), - }; - - headers.insert(reqwest::header::USER_AGENT, user_agent); - - let http_client = http_client.default_headers(headers).build()?; + let http_client = http_client.build()?; let base_client = if let Some(store) = config.state_store { BaseClient::new_with_state_store(session, store)? @@ -317,21 +327,21 @@ impl Client { /// Returns the joined rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` - pub fn joined_rooms(&self) -> Arc>>>> { + pub fn joined_rooms(&self) -> Arc>>>> { self.base_client.joined_rooms() } /// Returns the invited rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` - pub fn invited_rooms(&self) -> Arc>>>> { + pub fn invited_rooms(&self) -> Arc>>>> { self.base_client.invited_rooms() } /// Returns the left rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` - pub fn left_rooms(&self) -> Arc>>>> { + pub fn left_rooms(&self) -> Arc>>>> { self.base_client.left_rooms() } @@ -837,7 +847,7 @@ impl Client { let response = if let Ok(r) = response { r } else { - sleep(Duration::from_secs(1)).await; + sleep::new(Duration::from_secs(1)).await; continue; }; @@ -871,7 +881,7 @@ impl Client { // the sync timeout. if let Some(t) = last_sync_time { if now - t <= Duration::from_secs(1) { - sleep(Duration::from_secs(1)).await; + sleep::new(Duration::from_secs(1)).await; } } @@ -923,7 +933,8 @@ impl Client { let session = self.base_client.session().read().await; if let Some(session) = session.as_ref() { - request_builder.bearer_auth(&session.access_token) + let header_value = format!("Bearer {}", &session.access_token); + request_builder.header(AUTHORIZATION, header_value) } else { return Err(Error::AuthenticationRequired); } @@ -980,7 +991,7 @@ impl Client { /// # let homeserver = Url::parse("http://localhost:8080").unwrap(); /// # let mut client = Client::new(homeserver, None).unwrap(); /// # let room_id = RoomId::try_from("!test:localhost").unwrap(); - /// use uuid::Uuid; + /// use matrix_sdk_common::uuid::Uuid; /// /// let content = MessageEventContent::Text(TextMessageEventContent { /// body: "Hello world".to_owned(), @@ -1203,7 +1214,7 @@ mod test { use crate::identifiers::{EventId, RoomId, UserId}; use matrix_sdk_base::JsonStore; - use matrix_sdk_test::EventBuilder; + use matrix_sdk_test::{EventBuilder, EventsFile}; use mockito::{mock, Matcher}; use std::convert::TryFrom; @@ -1250,11 +1261,8 @@ mod test { let client = Client::new(homeserver, Some(session)).unwrap(); let mut response = EventBuilder::default() - .add_room_event_from_file("../test_data/events/member.json", RoomEvent::RoomMember) - .add_room_event_from_file( - "../test_data/events/power_levels.json", - RoomEvent::RoomPowerLevels, - ) + .add_room_event(EventsFile::Member, RoomEvent::RoomMember) + .add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) .build_sync_response(); client @@ -1651,7 +1659,7 @@ mod test { #[tokio::test] async fn room_message_send() { - use uuid::Uuid; + use matrix_sdk_common::uuid::Uuid; let homeserver = Url::from_str(&mockito::server_url()).unwrap(); let user = UserId::try_from("@example:localhost").unwrap(); diff --git a/matrix_sdk/src/lib.rs b/matrix_sdk/src/lib.rs index 1b4a81d2..51d32b23 100644 --- a/matrix_sdk/src/lib.rs +++ b/matrix_sdk/src/lib.rs @@ -26,8 +26,10 @@ //! destroyed. #![deny(missing_docs)] +#[cfg(not(target_arch = "wasm32"))] +pub use matrix_sdk_base::JsonStore; pub use matrix_sdk_base::{EventEmitter, Room, Session}; -pub use matrix_sdk_base::{JsonStore, RoomState, StateStore}; +pub use matrix_sdk_base::{RoomState, StateStore}; pub use matrix_sdk_common::*; pub use reqwest::header::InvalidHeaderValue; diff --git a/matrix_sdk_base/.cargo/config b/matrix_sdk_base/.cargo/config new file mode 100644 index 00000000..6bf5aea9 --- /dev/null +++ b/matrix_sdk_base/.cargo/config @@ -0,0 +1,3 @@ +# required because of https://github.com/rustwasm/wasm-pack/issues/698 +[target.wasm32-unknown-unknown] +runner = 'wasm-bindgen-test-runner' \ No newline at end of file diff --git a/matrix_sdk_base/Cargo.toml b/matrix_sdk_base/Cargo.toml index 97ab1c65..2b98df27 100644 --- a/matrix_sdk_base/Cargo.toml +++ b/matrix_sdk_base/Cargo.toml @@ -17,7 +17,6 @@ encryption = ["matrix-sdk-crypto"] sqlite-cryptostore = ["matrix-sdk-crypto/sqlite-cryptostore"] [dependencies] -futures = "0.3.4" async-trait = "0.1.30" serde = "1.0.106" serde_json = "1.0.52" @@ -28,15 +27,20 @@ matrix-sdk-crypto = { version = "0.1.0", path = "../matrix_sdk_crypto", optional # Misc dependencies thiserror = "1.0.16" -[dependencies.tokio] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] version = "0.2.20" default-features = false features = ["sync", "fs"] [dev-dependencies] matrix-sdk-test = { version = "0.1.0", path = "../matrix_sdk_test" } -tokio = { version = "0.2.20", features = ["rt-threaded", "macros"] } http = "0.2.1" dirs = "2.0.2" tracing-subscriber = "0.2.5" tempfile = "3.1.0" + +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +tokio = { version = "0.2.20", features = ["rt-threaded", "macros"] } + +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = "0.3.12" \ No newline at end of file diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 40139cec..d1bf636a 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -39,10 +39,10 @@ use crate::session::Session; use crate::state::{ClientState, StateStore}; use crate::EventEmitter; -use std::ops::Deref; #[cfg(feature = "encryption")] -use tokio::sync::Mutex; -use tokio::sync::RwLock; +use matrix_sdk_common::locks::Mutex; +use matrix_sdk_common::locks::RwLock; +use std::ops::Deref; #[cfg(feature = "encryption")] use crate::api::r0::keys::{ @@ -297,7 +297,7 @@ impl BaseClient { /// Returns the joined rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` - pub fn joined_rooms(&self) -> Arc>>>> { + pub fn joined_rooms(&self) -> Arc>>>> { self.joined_rooms.clone() } @@ -331,7 +331,7 @@ impl BaseClient { /// Returns the invited rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` - pub fn invited_rooms(&self) -> Arc>>>> { + pub fn invited_rooms(&self) -> Arc>>>> { self.invited_rooms.clone() } @@ -365,7 +365,7 @@ impl BaseClient { /// Returns the left rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` - pub fn left_rooms(&self) -> Arc>>>> { + pub fn left_rooms(&self) -> Arc>>>> { self.left_rooms.clone() } diff --git a/matrix_sdk_base/src/event_emitter/mod.rs b/matrix_sdk_base/src/event_emitter/mod.rs index af7d179f..f212d81f 100644 --- a/matrix_sdk_base/src/event_emitter/mod.rs +++ b/matrix_sdk_base/src/event_emitter/mod.rs @@ -168,8 +168,13 @@ pub trait EventEmitter: Send + Sync { #[cfg(test)] mod test { use super::*; + use matrix_sdk_common::locks::Mutex; + use matrix_sdk_test::{async_test, sync_response, SyncResponseFile}; use std::sync::Arc; - use tokio::sync::Mutex; + + #[cfg(target_arch = "wasm32")] + pub use wasm_bindgen_test::*; + #[derive(Clone)] pub struct EvEmitterTest(Arc>>); @@ -283,22 +288,10 @@ mod test { } } - use crate::api::r0::sync::sync_events::Response as SyncResponse; use crate::identifiers::UserId; use crate::{BaseClient, Session}; - use http::Response; use std::convert::TryFrom; - use std::fs::File; - use std::io::Read; - - fn sync_response(file: &str) -> SyncResponse { - let mut file = File::open(file).unwrap(); - let mut data = vec![]; - file.read_to_end(&mut data).unwrap(); - let response = Response::builder().body(data).unwrap(); - SyncResponse::try_from(response).unwrap() - } fn get_client() -> BaseClient { let session = Session { @@ -309,7 +302,7 @@ mod test { BaseClient::new(Some(session)).unwrap() } - #[tokio::test] + #[async_test] async fn event_emitter_joined() { let vec = Arc::new(Mutex::new(Vec::new())); let test_vec = Arc::clone(&vec); @@ -318,7 +311,7 @@ mod test { let client = get_client(); client.add_event_emitter(emitter).await; - let mut response = sync_response("../test_data/sync.json"); + let mut response = sync_response(SyncResponseFile::Default); client.receive_sync_response(&mut response).await.unwrap(); let v = test_vec.lock().await; @@ -340,7 +333,7 @@ mod test { ) } - #[tokio::test] + #[async_test] async fn event_emitter_invite() { let vec = Arc::new(Mutex::new(Vec::new())); let test_vec = Arc::clone(&vec); @@ -349,7 +342,7 @@ mod test { let client = get_client(); client.add_event_emitter(emitter).await; - let mut response = sync_response("../test_data/invite_sync.json"); + let mut response = sync_response(SyncResponseFile::Invite); client.receive_sync_response(&mut response).await.unwrap(); let v = test_vec.lock().await; @@ -359,7 +352,7 @@ mod test { ) } - #[tokio::test] + #[async_test] async fn event_emitter_leave() { let vec = Arc::new(Mutex::new(Vec::new())); let test_vec = Arc::clone(&vec); @@ -368,7 +361,7 @@ mod test { let client = get_client(); client.add_event_emitter(emitter).await; - let mut response = sync_response("../test_data/leave_sync.json"); + let mut response = sync_response(SyncResponseFile::Leave); client.receive_sync_response(&mut response).await.unwrap(); let v = test_vec.lock().await; diff --git a/matrix_sdk_base/src/lib.rs b/matrix_sdk_base/src/lib.rs index c1865a3c..53228d91 100644 --- a/matrix_sdk_base/src/lib.rs +++ b/matrix_sdk_base/src/lib.rs @@ -41,4 +41,6 @@ pub use event_emitter::EventEmitter; #[cfg(feature = "encryption")] pub use matrix_sdk_crypto::{Device, TrustState}; pub use models::Room; -pub use state::{JsonStore, StateStore}; +#[cfg(not(target_arch = "wasm32"))] +pub use state::JsonStore; +pub use state::StateStore; diff --git a/matrix_sdk_base/src/models/message.rs b/matrix_sdk_base/src/models/message.rs index 2aed0ec5..3fc9e888 100644 --- a/matrix_sdk_base/src/models/message.rs +++ b/matrix_sdk_base/src/models/message.rs @@ -143,6 +143,9 @@ mod test { use std::collections::HashMap; use std::convert::TryFrom; + #[cfg(target_arch = "wasm32")] + use wasm_bindgen_test::*; + use crate::events::{collections::all::RoomEvent, EventJson}; use crate::identifiers::{RoomId, UserId}; use crate::Room; diff --git a/matrix_sdk_base/src/models/room.rs b/matrix_sdk_base/src/models/room.rs index f2de36b0..44912eb1 100644 --- a/matrix_sdk_base/src/models/room.rs +++ b/matrix_sdk_base/src/models/room.rs @@ -521,27 +521,17 @@ impl Room { #[cfg(test)] mod test { use super::*; - use crate::api::r0::sync::sync_events::Response as SyncResponse; use crate::events::room::member::MembershipState; use crate::identifiers::UserId; use crate::{BaseClient, Session}; - use matrix_sdk_test::EventBuilder; + use matrix_sdk_test::{async_test, sync_response, EventBuilder, EventsFile, SyncResponseFile}; - use http::Response; + #[cfg(target_arch = "wasm32")] + use wasm_bindgen_test::*; use std::convert::TryFrom; - use std::fs::File; - use std::io::Read; use std::ops::Deref; - fn sync_response(file: &str) -> SyncResponse { - let mut file = File::open(file).unwrap(); - let mut data = vec![]; - file.read_to_end(&mut data).unwrap(); - let response = Response::builder().body(data).unwrap(); - SyncResponse::try_from(response).unwrap() - } - fn get_client() -> BaseClient { let session = Session { access_token: "1234".to_owned(), @@ -555,11 +545,11 @@ mod test { RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap() } - #[tokio::test] + #[async_test] async fn user_presence() { let client = get_client(); - let mut response = sync_response("../test_data/sync.json"); + let mut response = sync_response(SyncResponseFile::Default); client.receive_sync_response(&mut response).await.unwrap(); @@ -579,18 +569,15 @@ mod test { assert!(room.deref().power_levels.is_some()) } - #[tokio::test] + #[async_test] async fn room_events() { let client = get_client(); let room_id = get_room_id(); let user_id = UserId::try_from("@example:localhost").unwrap(); let mut response = EventBuilder::default() - .add_room_event_from_file("../test_data/events/member.json", RoomEvent::RoomMember) - .add_room_event_from_file( - "../test_data/events/power_levels.json", - RoomEvent::RoomPowerLevels, - ) + .add_room_event(EventsFile::Member, RoomEvent::RoomMember) + .add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) .build_sync_response(); client.receive_sync_response(&mut response).await.unwrap(); @@ -611,14 +598,14 @@ mod test { ); } - #[tokio::test] + #[async_test] async fn calculate_aliases() { let client = get_client(); let room_id = get_room_id(); let mut response = EventBuilder::default() - .add_state_event_from_file("../test_data/events/aliases.json", StateEvent::RoomAliases) + .add_state_event(EventsFile::Aliases, StateEvent::RoomAliases) .build_sync_response(); client.receive_sync_response(&mut response).await.unwrap(); @@ -629,17 +616,14 @@ mod test { assert_eq!("tutorial", room.display_name()); } - #[tokio::test] + #[async_test] async fn calculate_alias() { let client = get_client(); let room_id = get_room_id(); let mut response = EventBuilder::default() - .add_state_event_from_file( - "../test_data/events/alias.json", - StateEvent::RoomCanonicalAlias, - ) + .add_state_event(EventsFile::Alias, StateEvent::RoomCanonicalAlias) .build_sync_response(); client.receive_sync_response(&mut response).await.unwrap(); @@ -650,14 +634,14 @@ mod test { assert_eq!("tutorial", room.display_name()); } - #[tokio::test] + #[async_test] async fn calculate_name() { let client = get_client(); let room_id = get_room_id(); let mut response = EventBuilder::default() - .add_state_event_from_file("../test_data/events/name.json", StateEvent::RoomName) + .add_state_event(EventsFile::Name, StateEvent::RoomName) .build_sync_response(); client.receive_sync_response(&mut response).await.unwrap(); @@ -668,9 +652,9 @@ mod test { assert_eq!("room name", room.display_name()); } - #[tokio::test] + #[async_test] async fn calculate_room_names_from_summary() { - let mut response = sync_response("../test_data/sync_with_summary.json"); + let mut response = sync_response(SyncResponseFile::DefaultWithSummary); let session = Session { access_token: "1234".to_owned(), diff --git a/matrix_sdk_base/src/models/room_member.rs b/matrix_sdk_base/src/models/room_member.rs index afedb5a2..8050a75c 100644 --- a/matrix_sdk_base/src/models/room_member.rs +++ b/matrix_sdk_base/src/models/room_member.rs @@ -199,7 +199,7 @@ impl RoomMember { #[cfg(test)] mod test { - use matrix_sdk_test::EventBuilder; + use matrix_sdk_test::{async_test, EventBuilder, EventsFile}; use crate::events::collections::all::RoomEvent; use crate::events::room::member::MembershipState; @@ -208,6 +208,9 @@ mod test { use crate::js_int::Int; + #[cfg(target_arch = "wasm32")] + use wasm_bindgen_test::*; + use std::convert::TryFrom; fn get_client() -> BaseClient { @@ -223,18 +226,15 @@ mod test { RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap() } - #[tokio::test] + #[async_test] async fn room_member_events() { let client = get_client(); let room_id = get_room_id(); let mut response = EventBuilder::default() - .add_room_event_from_file("../test_data/events/member.json", RoomEvent::RoomMember) - .add_room_event_from_file( - "../test_data/events/power_levels.json", - RoomEvent::RoomPowerLevels, - ) + .add_room_event(EventsFile::Member, RoomEvent::RoomMember) + .add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) .build_sync_response(); client.receive_sync_response(&mut response).await.unwrap(); @@ -250,19 +250,16 @@ mod test { assert_eq!(member.power_level, Int::new(100)); } - #[tokio::test] + #[async_test] async fn member_presence_events() { let client = get_client(); let room_id = get_room_id(); let mut response = EventBuilder::default() - .add_room_event_from_file("../test_data/events/member.json", RoomEvent::RoomMember) - .add_room_event_from_file( - "../test_data/events/power_levels.json", - RoomEvent::RoomPowerLevels, - ) - .add_presence_event_from_file("../test_data/events/presence.json") + .add_room_event(EventsFile::Member, RoomEvent::RoomMember) + .add_room_event(EventsFile::PowerLevels, RoomEvent::RoomPowerLevels) + .add_presence_event(EventsFile::Presence) .build_sync_response(); client.receive_sync_response(&mut response).await.unwrap(); diff --git a/matrix_sdk_base/src/state/mod.rs b/matrix_sdk_base/src/state/mod.rs index a53ec622..79bfa3c2 100644 --- a/matrix_sdk_base/src/state/mod.rs +++ b/matrix_sdk_base/src/state/mod.rs @@ -16,7 +16,9 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; +#[cfg(not(target_arch = "wasm32"))] pub mod state_store; +#[cfg(not(target_arch = "wasm32"))] pub use state_store::JsonStore; use crate::client::{BaseClient, Token}; diff --git a/matrix_sdk_base/src/state/state_store.rs b/matrix_sdk_base/src/state/state_store.rs index e26f35b6..0551bba9 100644 --- a/matrix_sdk_base/src/state/state_store.rs +++ b/matrix_sdk_base/src/state/state_store.rs @@ -6,9 +6,9 @@ use std::sync::{ Arc, }; +use matrix_sdk_common::locks::RwLock; use tokio::fs as async_fs; use tokio::io::AsyncWriteExt; -use tokio::sync::RwLock; use super::{ClientState, StateStore}; use crate::identifiers::RoomId; diff --git a/matrix_sdk_common/Cargo.toml b/matrix_sdk_common/Cargo.toml index 4dcfbb16..8d28a3e7 100644 --- a/matrix_sdk_common/Cargo.toml +++ b/matrix_sdk_common/Cargo.toml @@ -16,3 +16,15 @@ ruma-api = "0.16.0" ruma-client-api = "0.8.0" ruma-events = "0.21.0" ruma-identifiers = "0.16.1" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +uuid = { version = "0.8.1", features = ["v4"] } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] +version = "0.2.20" +default-features = false +features = ["sync", "time", "fs"] + +[target.'cfg(target_arch = "wasm32")'.dependencies] +futures-locks = { git = "https://github.com/asomers/futures-locks", default-features = false } +uuid = { version = "0.8.1", features = ["v4", "wasm-bindgen"] } \ No newline at end of file diff --git a/matrix_sdk_common/src/lib.rs b/matrix_sdk_common/src/lib.rs index da68b770..38653e22 100644 --- a/matrix_sdk_common/src/lib.rs +++ b/matrix_sdk_common/src/lib.rs @@ -6,3 +6,7 @@ pub use ruma_api::{ pub use ruma_client_api as api; pub use ruma_events as events; pub use ruma_identifiers as identifiers; + +pub use uuid; + +pub mod locks; diff --git a/matrix_sdk_common/src/locks.rs b/matrix_sdk_common/src/locks.rs new file mode 100644 index 00000000..6f648a39 --- /dev/null +++ b/matrix_sdk_common/src/locks.rs @@ -0,0 +1,13 @@ +// could switch to futures-lock completely at some point, blocker: +// https://github.com/asomers/futures-locks/issues/34 +// https://www.reddit.com/r/rust/comments/f4zldz/i_audited_3_different_implementation_of_async/ + +#[cfg(target_arch = "wasm32")] +pub use futures_locks::Mutex; +#[cfg(target_arch = "wasm32")] +pub use futures_locks::RwLock; + +#[cfg(not(target_arch = "wasm32"))] +pub use tokio::sync::Mutex; +#[cfg(not(target_arch = "wasm32"))] +pub use tokio::sync::RwLock; diff --git a/matrix_sdk_crypto/Cargo.toml b/matrix_sdk_crypto/Cargo.toml index 96b1c057..d3179baa 100644 --- a/matrix_sdk_crypto/Cargo.toml +++ b/matrix_sdk_crypto/Cargo.toml @@ -15,7 +15,6 @@ default = [] sqlite-cryptostore = ["sqlx"] [dependencies] -futures = "0.3.4" async-trait = "0.1.30" matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } @@ -23,9 +22,8 @@ matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } olm-rs = { version = "0.5.0", features = ["serde"] } serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.52" -cjson = "0.1.0" +cjson = { git = "https://github.com/stoically/cjson", rev = "24a82d94e339e27a62434493869f3dce62c60e0d" } zeroize = { version = "1.1.0", features = ["zeroize_derive"] } -uuid = { version = "0.8.1", features = ["v4"] } url = "2.1.1" # Misc dependencies @@ -39,12 +37,7 @@ version = "0.2.4" default-features = false features = ["std", "std-future"] -[dependencies.tokio] -version = "0.2.20" -default-features = false -features = ["sync", "time"] - -[dependencies.sqlx] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies.sqlx] version = "0.3.5" optional = true default-features = false diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 2c4dcd48..96a3b897 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -19,7 +19,6 @@ use std::mem; use std::path::Path; use std::result::Result as StdResult; use std::sync::atomic::{AtomicU64, Ordering}; -use uuid::Uuid; use super::error::{EventError, MegolmError, MegolmResult, OlmError, OlmResult, SignatureError}; use super::olm::{ @@ -46,6 +45,7 @@ use matrix_sdk_common::events::{ Algorithm, EventJson, EventType, }; use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId}; +use matrix_sdk_common::uuid::Uuid; use api::r0::keys; use api::r0::{ diff --git a/matrix_sdk_crypto/src/memory_stores.rs b/matrix_sdk_crypto/src/memory_stores.rs index 0735de30..2172010c 100644 --- a/matrix_sdk_crypto/src/memory_stores.rs +++ b/matrix_sdk_crypto/src/memory_stores.rs @@ -16,7 +16,7 @@ use std::collections::HashMap; use std::sync::Arc; use dashmap::{DashMap, ReadOnlyView}; -use tokio::sync::Mutex; +use matrix_sdk_common::locks::Mutex; use super::device::Device; use super::olm::{InboundGroupSession, Session}; diff --git a/matrix_sdk_crypto/src/olm.rs b/matrix_sdk_crypto/src/olm.rs index 4df9741f..e8566ce9 100644 --- a/matrix_sdk_crypto/src/olm.rs +++ b/matrix_sdk_crypto/src/olm.rs @@ -18,8 +18,8 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Arc; use std::time::Instant; +use matrix_sdk_common::locks::Mutex; use serde::Serialize; -use tokio::sync::Mutex; use zeroize::Zeroize; pub use olm_rs::account::IdentityKeys; diff --git a/matrix_sdk_crypto/src/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs index 0a921b85..08c1f83f 100644 --- a/matrix_sdk_crypto/src/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -16,7 +16,7 @@ use std::collections::HashSet; use std::sync::Arc; use async_trait::async_trait; -use tokio::sync::Mutex; +use matrix_sdk_common::locks::Mutex; use super::{Account, CryptoStore, InboundGroupSession, Result, Session}; use crate::device::Device; diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index 36d6c4e4..916a5a29 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -19,9 +19,9 @@ use std::sync::Arc; use url::ParseError; use async_trait::async_trait; +use matrix_sdk_common::locks::Mutex; use serde_json::Error as SerdeError; use thiserror::Error; -use tokio::sync::Mutex; use super::device::Device; use super::memory_stores::UserDevices; diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index 9d824032..8fd98228 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -22,9 +22,9 @@ use std::time::{Duration, Instant}; use url::Url; use async_trait::async_trait; +use matrix_sdk_common::locks::Mutex; use olm_rs::PicklingMode; use sqlx::{query, query_as, sqlite::SqliteQueryAs, Connect, Executor, SqliteConnection}; -use tokio::sync::Mutex; use zeroize::Zeroizing; use super::{Account, CryptoStore, CryptoStoreError, InboundGroupSession, Result, Session}; diff --git a/matrix_sdk_test/Cargo.toml b/matrix_sdk_test/Cargo.toml index c4cb3243..65650f76 100644 --- a/matrix_sdk_test/Cargo.toml +++ b/matrix_sdk_test/Cargo.toml @@ -14,3 +14,4 @@ version = "0.1.0" serde_json = "1.0.52" http = "0.2.1" matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } +matrix-sdk-test-macros = { path = "../matrix_sdk_test_macros" } \ No newline at end of file diff --git a/matrix_sdk_test/src/lib.rs b/matrix_sdk_test/src/lib.rs index 9fd49ddd..0f239e82 100644 --- a/matrix_sdk_test/src/lib.rs +++ b/matrix_sdk_test/src/lib.rs @@ -1,7 +1,5 @@ use std::convert::TryFrom; -use std::fs; use std::panic; -use std::path::Path; use http::Response; @@ -15,6 +13,34 @@ use matrix_sdk_common::events::{ EventJson, TryFromRaw, }; +pub use matrix_sdk_test_macros::async_test; + +/// Embedded event files +#[derive(Debug)] +pub enum EventsFile { + Alias, + Aliases, + Create, + FullyRead, + HistoryVisibility, + JoinRules, + Member, + MessageEmote, + MessageNotice, + MessageText, + Name, + PowerLevels, + Presence, + RedactedInvalid, + RedactedState, + Redacted, + Redaction, + RoomAvatar, + Tag, + Topic, + Typing, +} + /// Easily create events to stream into either a Client or a `Room` for testing. #[derive(Default)] pub struct EventBuilder { @@ -32,13 +58,16 @@ pub struct EventBuilder { impl EventBuilder { /// Add an event to the room events `Vec`. - pub fn add_ephemeral_from_file>( + pub fn add_ephemeral( mut self, - path: P, + file: EventsFile, variant: fn(Ev) -> Event, ) -> Self { - let val = fs::read_to_string(path.as_ref()) - .unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref())); + let val: &str = match file { + EventsFile::Typing => include_str!("../../test_data/events/typing.json"), + _ => panic!("unknown ephemeral event file {:?}", file), + }; + let event = serde_json::from_str::>(&val) .unwrap() .deserialize() @@ -48,13 +77,16 @@ impl EventBuilder { } /// Add an event to the room events `Vec`. - pub fn add_account_from_file>( + #[allow(clippy::match_single_binding, unused)] + pub fn add_account( mut self, - path: P, + file: EventsFile, variant: fn(Ev) -> Event, ) -> Self { - let val = fs::read_to_string(path.as_ref()) - .unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref())); + let val: &str = match file { + _ => panic!("unknown account event file {:?}", file), + }; + let event = serde_json::from_str::>(&val) .unwrap() .deserialize() @@ -64,13 +96,17 @@ impl EventBuilder { } /// Add an event to the room events `Vec`. - pub fn add_room_event_from_file>( + pub fn add_room_event( mut self, - path: P, + file: EventsFile, variant: fn(Ev) -> RoomEvent, ) -> Self { - let val = fs::read_to_string(path.as_ref()) - .unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref())); + let val = match file { + EventsFile::Member => include_str!("../../test_data/events/member.json"), + EventsFile::PowerLevels => include_str!("../../test_data/events/power_levels.json"), + _ => panic!("unknown room event file {:?}", file), + }; + let event = serde_json::from_str::>(&val) .unwrap() .deserialize() @@ -80,13 +116,18 @@ impl EventBuilder { } /// Add a state event to the state events `Vec`. - pub fn add_state_event_from_file>( + pub fn add_state_event( mut self, - path: P, + file: EventsFile, variant: fn(Ev) -> StateEvent, ) -> Self { - let val = fs::read_to_string(path.as_ref()) - .unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref())); + let val = match file { + EventsFile::Alias => include_str!("../../test_data/events/alias.json"), + EventsFile::Aliases => include_str!("../../test_data/events/aliases.json"), + EventsFile::Name => include_str!("../../test_data/events/name.json"), + _ => panic!("unknown state event file {:?}", file), + }; + let event = serde_json::from_str::>(&val) .unwrap() .deserialize() @@ -95,10 +136,13 @@ impl EventBuilder { self } - /// Add a presence event to the presence events `Vec`. - pub fn add_presence_event_from_file>(mut self, path: P) -> Self { - let val = fs::read_to_string(path.as_ref()) - .unwrap_or_else(|_| panic!("file not found {:?}", path.as_ref())); + /// Add an presence event to the presence events `Vec`. + pub fn add_presence_event(mut self, file: EventsFile) -> Self { + let val = match file { + EventsFile::Presence => include_str!("../../test_data/events/presence.json"), + _ => panic!("unknown presence event file {:?}", file), + }; + let event = serde_json::from_str::>(&val) .unwrap() .deserialize() @@ -158,3 +202,26 @@ impl EventBuilder { SyncResponse::try_from(response).unwrap() } } + +/// Embedded sync reponse files +pub enum SyncResponseFile { + Default, + DefaultWithSummary, + Invite, + Leave, +} + +/// Get specific API responses for testing +pub fn sync_response(kind: SyncResponseFile) -> SyncResponse { + let data = match kind { + SyncResponseFile::Default => include_bytes!("../../test_data/sync.json").to_vec(), + SyncResponseFile::DefaultWithSummary => { + include_bytes!("../../test_data/sync_with_summary.json").to_vec() + } + SyncResponseFile::Invite => include_bytes!("../../test_data/invite_sync.json").to_vec(), + SyncResponseFile::Leave => include_bytes!("../../test_data/leave_sync.json").to_vec(), + }; + + let response = Response::builder().body(data.to_vec()).unwrap(); + SyncResponse::try_from(response).unwrap() +} diff --git a/matrix_sdk_test_macros/Cargo.toml b/matrix_sdk_test_macros/Cargo.toml new file mode 100644 index 00000000..11a42bdc --- /dev/null +++ b/matrix_sdk_test_macros/Cargo.toml @@ -0,0 +1,14 @@ +[package] +authors = ["stoically "] +description = "Helper macros to write tests for the Matrix SDK" +edition = "2018" +homepage = "https://github.com/matrix-org/matrix-rust-sdk" +keywords = ["matrix", "chat", "messaging", "ruma"] +license = "Apache-2.0" +name = "matrix-sdk-test-macros" +readme = "README.md" +repository = "https://github.com/matrix-org/matrix-rust-sdk" +version = "0.1.0" + +[lib] +proc-macro = true diff --git a/matrix_sdk_test_macros/src/lib.rs b/matrix_sdk_test_macros/src/lib.rs new file mode 100644 index 00000000..3b0ac656 --- /dev/null +++ b/matrix_sdk_test_macros/src/lib.rs @@ -0,0 +1,14 @@ +use proc_macro::TokenStream; + +/// Attribute to use `wasm_bindgen_test` for wasm32 targets and `tokio::test` for everything else +#[proc_macro_attribute] +pub fn async_test(_attr: TokenStream, item: TokenStream) -> TokenStream { + let attrs = r#" + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] + "#; + + let mut out: TokenStream = attrs.parse().unwrap(); + out.extend(item); + out +} From 19b8a1686d2e48e54e62c044f31f32ced77d007a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 12 May 2020 14:23:57 +0200 Subject: [PATCH 2/8] client: Disable the sleeps on the wasm target for now. --- matrix_sdk/src/client.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 6c3fe1f0..9e176591 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -847,12 +847,13 @@ impl Client { let response = if let Ok(r) = response { r } else { - sleep::new(Duration::from_secs(1)).await; - continue; + #[cfg(not(target_arch = "wasm32"))] + { + sleep::new(Duration::from_secs(1)).await; + continue; + } }; - callback(response).await; - // TODO send out to-device messages here #[cfg(feature = "encryption")] @@ -874,14 +875,19 @@ impl Client { } } + callback(response).await; + let now = Instant::now(); // If the last sync happened less than a second ago, sleep for a // while to not hammer out requests if the server doesn't respect // the sync timeout. - if let Some(t) = last_sync_time { - if now - t <= Duration::from_secs(1) { - sleep::new(Duration::from_secs(1)).await; + #[cfg(not(target_arch = "wasm32"))] + { + if let Some(t) = last_sync_time { + if now - t <= Duration::from_secs(1) { + sleep::new(Duration::from_secs(1)).await; + } } } From 2a411bf5df170eef57ff51452cc1e0da5c38203b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 12 May 2020 14:28:01 +0200 Subject: [PATCH 3/8] matrix-sdk: Use the instant crate so Instant works on wasm as well. --- matrix_sdk/src/client.rs | 2 +- matrix_sdk_common/Cargo.toml | 3 ++- matrix_sdk_common/src/lib.rs | 1 + matrix_sdk_crypto/src/olm.rs | 2 +- matrix_sdk_crypto/src/store/sqlite.rs | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 9e176591..b1ad3c9a 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -19,8 +19,8 @@ use std::collections::HashMap; use std::convert::{TryFrom, TryInto}; use std::result::Result as StdResult; use std::sync::Arc; -use std::time::{Duration, Instant}; +use matrix_sdk_common::instant::{Duration, Instant}; use matrix_sdk_common::locks::RwLock; use matrix_sdk_common::uuid::Uuid; diff --git a/matrix_sdk_common/Cargo.toml b/matrix_sdk_common/Cargo.toml index 8d28a3e7..a4875493 100644 --- a/matrix_sdk_common/Cargo.toml +++ b/matrix_sdk_common/Cargo.toml @@ -16,6 +16,7 @@ ruma-api = "0.16.0" ruma-client-api = "0.8.0" ruma-events = "0.21.0" ruma-identifiers = "0.16.1" +instant = { version = "0.1.3", features = ["wasm-bindgen", "now"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] uuid = { version = "0.8.1", features = ["v4"] } @@ -27,4 +28,4 @@ features = ["sync", "time", "fs"] [target.'cfg(target_arch = "wasm32")'.dependencies] futures-locks = { git = "https://github.com/asomers/futures-locks", default-features = false } -uuid = { version = "0.8.1", features = ["v4", "wasm-bindgen"] } \ No newline at end of file +uuid = { version = "0.8.1", features = ["v4", "wasm-bindgen"] } diff --git a/matrix_sdk_common/src/lib.rs b/matrix_sdk_common/src/lib.rs index 38653e22..42cce581 100644 --- a/matrix_sdk_common/src/lib.rs +++ b/matrix_sdk_common/src/lib.rs @@ -1,3 +1,4 @@ +pub use instant; pub use js_int; pub use ruma_api::{ error::{FromHttpResponseError, IntoHttpError, ServerError}, diff --git a/matrix_sdk_crypto/src/olm.rs b/matrix_sdk_crypto/src/olm.rs index e8566ce9..5e09ead1 100644 --- a/matrix_sdk_crypto/src/olm.rs +++ b/matrix_sdk_crypto/src/olm.rs @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +use matrix_sdk_common::instant::Instant; use std::fmt; use std::mem; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Arc; -use std::time::Instant; use matrix_sdk_common::locks::Mutex; use serde::Serialize; diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index 8fd98228..922bebb1 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +use matrix_sdk_common::instant::{Duration, Instant}; use std::collections::{BTreeMap, HashSet}; use std::convert::TryFrom; use std::mem; use std::path::{Path, PathBuf}; use std::result::Result as StdResult; use std::sync::Arc; -use std::time::{Duration, Instant}; use url::Url; use async_trait::async_trait; From 0bb1be94e222ba42d0660281f46e531d3d92c055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 12 May 2020 14:30:33 +0200 Subject: [PATCH 4/8] examples: Modify the wasm example to be similar to the command bot. --- .../examples/wasm_send_message/Cargo.toml | 1 + .../examples/wasm_send_message/src/lib.rs | 61 +++++++++++++++---- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/matrix_sdk/examples/wasm_send_message/Cargo.toml b/matrix_sdk/examples/wasm_send_message/Cargo.toml index 3174fff5..397ef6c8 100644 --- a/matrix_sdk/examples/wasm_send_message/Cargo.toml +++ b/matrix_sdk/examples/wasm_send_message/Cargo.toml @@ -14,6 +14,7 @@ matrix-sdk = { path = "../..", default-features = false, features = ["encryption url = "2.1.1" wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] } wasm-bindgen-futures = "0.4.12" +web-sys = { version = "0.3.39", features = ["console"] } [workspace] diff --git a/matrix_sdk/examples/wasm_send_message/src/lib.rs b/matrix_sdk/examples/wasm_send_message/src/lib.rs index 7e4a58e9..3b1cf231 100644 --- a/matrix_sdk/examples/wasm_send_message/src/lib.rs +++ b/matrix_sdk/examples/wasm_send_message/src/lib.rs @@ -1,11 +1,50 @@ use matrix_sdk::{ - events::room::message::{MessageEventContent, TextMessageEventContent}, + api::r0::sync::sync_events::Response as SyncResponse, + events::collections::all::RoomEvent, + events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, identifiers::RoomId, - Client, ClientConfig, + Client, ClientConfig, SyncSettings, }; -use std::convert::TryFrom; use url::Url; use wasm_bindgen::prelude::*; +use web_sys::console; + +struct WasmBot(Client); + +impl WasmBot { + async fn on_room_message(&self, room_id: &RoomId, event: RoomEvent) { + let msg_body = if let RoomEvent::RoomMessage(MessageEvent { + content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), + .. + }) = event + { + msg_body.clone() + } else { + return; + }; + + console::log_1(&format!("Received message event {:?}", &msg_body).into()); + + if msg_body.starts_with("!party") { + let content = MessageEventContent::Text(TextMessageEventContent::new_plain( + "🎉🎊🥳 let's PARTY with wasm!! 🥳🎊🎉".to_string(), + )); + + self.0.room_send(&room_id, content, None).await.unwrap(); + } + } + async fn on_sync_response(&self, response: SyncResponse) { + console::log_1(&format!("Synced").into()); + + for (room_id, room) in response.rooms.join { + for event in room.timeline.events { + if let Ok(event) = event.deserialize() { + self.on_room_message(&room_id, event).await + } + } + } + } +} #[wasm_bindgen] pub async fn run() -> Result { @@ -18,20 +57,18 @@ pub async fn run() -> Result { let client = Client::new_with_config(homeserver_url, None, client_config).unwrap(); client - .login(username, password, None, Some("rust-sdk")) + .login(username, password, None, Some("rust-sdk-wasm")) .await .unwrap(); - let room_id = RoomId::try_from("!KpLWMcXcHKDMfEYNqA:localhost").unwrap(); + let bot = WasmBot(client.clone()); - let content = MessageEventContent::Text(TextMessageEventContent { - body: "hello from wasm".to_string(), - format: None, - formatted_body: None, - relates_to: None, - }); + client.sync(SyncSettings::default()).await.unwrap(); - client.room_send(&room_id, content, None).await.unwrap(); + let settings = SyncSettings::default().token(client.sync_token().await.unwrap()); + client + .sync_forever(settings, |response| bot.on_sync_response(response)) + .await; Ok(JsValue::NULL) } From e84a3e417c262267ccf21bbf3b03f8fc50da9746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 12 May 2020 14:55:44 +0200 Subject: [PATCH 5/8] matrix-sdk: Add missing newlines at the end of some files. --- matrix_sdk/examples/wasm_send_message/.gitignore | 2 +- matrix_sdk/examples/wasm_send_message/index.js | 2 +- matrix_sdk/examples/wasm_send_message/webpack.config.js | 2 +- matrix_sdk_base/.cargo/config | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/matrix_sdk/examples/wasm_send_message/.gitignore b/matrix_sdk/examples/wasm_send_message/.gitignore index dfa8ad9d..43d61f45 100644 --- a/matrix_sdk/examples/wasm_send_message/.gitignore +++ b/matrix_sdk/examples/wasm_send_message/.gitignore @@ -4,4 +4,4 @@ target Cargo.lock *.swp dist -package-lock.json \ No newline at end of file +package-lock.json diff --git a/matrix_sdk/examples/wasm_send_message/index.js b/matrix_sdk/examples/wasm_send_message/index.js index 74ac1d78..1fd262d0 100644 --- a/matrix_sdk/examples/wasm_send_message/index.js +++ b/matrix_sdk/examples/wasm_send_message/index.js @@ -4,4 +4,4 @@ rust .then(m => { return m.run() }) - .catch(console.error); \ No newline at end of file + .catch(console.error); diff --git a/matrix_sdk/examples/wasm_send_message/webpack.config.js b/matrix_sdk/examples/wasm_send_message/webpack.config.js index c77c1420..e1cee3ee 100644 --- a/matrix_sdk/examples/wasm_send_message/webpack.config.js +++ b/matrix_sdk/examples/wasm_send_message/webpack.config.js @@ -23,4 +23,4 @@ module.exports = { }) ], mode: 'development' -}; \ No newline at end of file +}; diff --git a/matrix_sdk_base/.cargo/config b/matrix_sdk_base/.cargo/config index 6bf5aea9..0e68dad1 100644 --- a/matrix_sdk_base/.cargo/config +++ b/matrix_sdk_base/.cargo/config @@ -1,3 +1,3 @@ # required because of https://github.com/rustwasm/wasm-pack/issues/698 [target.wasm32-unknown-unknown] -runner = 'wasm-bindgen-test-runner' \ No newline at end of file +runner = 'wasm-bindgen-test-runner' From 890dbdaa3991cd1b962436830488648c2ae96682 Mon Sep 17 00:00:00 2001 From: stoically Date: Tue, 12 May 2020 15:42:50 +0200 Subject: [PATCH 6/8] examples: Rename to wasm_command_bot and add note about encryption --- .travis.yml | 2 +- .../{wasm_send_message => wasm_command_bot}/.gitignore | 0 .../{wasm_send_message => wasm_command_bot}/Cargo.toml | 0 .../examples/{wasm_send_message => wasm_command_bot}/README.md | 3 +-- .../examples/{wasm_send_message => wasm_command_bot}/index.js | 0 .../{wasm_send_message => wasm_command_bot}/package.json | 0 .../{wasm_send_message => wasm_command_bot}/src/lib.rs | 0 .../{wasm_send_message => wasm_command_bot}/webpack.config.js | 0 8 files changed, 2 insertions(+), 3 deletions(-) rename matrix_sdk/examples/{wasm_send_message => wasm_command_bot}/.gitignore (100%) rename matrix_sdk/examples/{wasm_send_message => wasm_command_bot}/Cargo.toml (100%) rename matrix_sdk/examples/{wasm_send_message => wasm_command_bot}/README.md (64%) rename matrix_sdk/examples/{wasm_send_message => wasm_command_bot}/index.js (100%) rename matrix_sdk/examples/{wasm_send_message => wasm_command_bot}/package.json (100%) rename matrix_sdk/examples/{wasm_send_message => wasm_command_bot}/src/lib.rs (100%) rename matrix_sdk/examples/{wasm_send_message => wasm_command_bot}/webpack.config.js (100%) diff --git a/.travis.yml b/.travis.yml index ebb54d65..7a911250 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,7 @@ jobs: script: - | source emsdk-master/emsdk_env.sh - cd matrix_sdk/examples/wasm_send_message + cd matrix_sdk/examples/wasm_command_bot cargo build --target wasm32-unknown-unknown cd - diff --git a/matrix_sdk/examples/wasm_send_message/.gitignore b/matrix_sdk/examples/wasm_command_bot/.gitignore similarity index 100% rename from matrix_sdk/examples/wasm_send_message/.gitignore rename to matrix_sdk/examples/wasm_command_bot/.gitignore diff --git a/matrix_sdk/examples/wasm_send_message/Cargo.toml b/matrix_sdk/examples/wasm_command_bot/Cargo.toml similarity index 100% rename from matrix_sdk/examples/wasm_send_message/Cargo.toml rename to matrix_sdk/examples/wasm_command_bot/Cargo.toml diff --git a/matrix_sdk/examples/wasm_send_message/README.md b/matrix_sdk/examples/wasm_command_bot/README.md similarity index 64% rename from matrix_sdk/examples/wasm_send_message/README.md rename to matrix_sdk/examples/wasm_command_bot/README.md index 84d7d87a..49d1c688 100644 --- a/matrix_sdk/examples/wasm_send_message/README.md +++ b/matrix_sdk/examples/wasm_command_bot/README.md @@ -1,7 +1,5 @@ ## Example usage of matrix-rust-sdk from WASM -This requires [emscripten](https://github.com/emscripten-core/emscripten) to be installed and in PATH to compile successfully. - You can build the example locally with: npm install @@ -9,5 +7,6 @@ You can build the example locally with: and then visiting http://localhost:8080 in a browser should run the example! +Note: Encryption isn't supported yet This example is loosely based off of [this example](https://github.com/seanmonstar/reqwest/tree/master/examples/wasm_github_fetch), an example usage of `fetch` from `wasm-bindgen`. \ No newline at end of file diff --git a/matrix_sdk/examples/wasm_send_message/index.js b/matrix_sdk/examples/wasm_command_bot/index.js similarity index 100% rename from matrix_sdk/examples/wasm_send_message/index.js rename to matrix_sdk/examples/wasm_command_bot/index.js diff --git a/matrix_sdk/examples/wasm_send_message/package.json b/matrix_sdk/examples/wasm_command_bot/package.json similarity index 100% rename from matrix_sdk/examples/wasm_send_message/package.json rename to matrix_sdk/examples/wasm_command_bot/package.json diff --git a/matrix_sdk/examples/wasm_send_message/src/lib.rs b/matrix_sdk/examples/wasm_command_bot/src/lib.rs similarity index 100% rename from matrix_sdk/examples/wasm_send_message/src/lib.rs rename to matrix_sdk/examples/wasm_command_bot/src/lib.rs diff --git a/matrix_sdk/examples/wasm_send_message/webpack.config.js b/matrix_sdk/examples/wasm_command_bot/webpack.config.js similarity index 100% rename from matrix_sdk/examples/wasm_send_message/webpack.config.js rename to matrix_sdk/examples/wasm_command_bot/webpack.config.js From aa42951f15d4ec28da738d511cdd5af1587a1707 Mon Sep 17 00:00:00 2001 From: stoically Date: Tue, 12 May 2020 15:47:37 +0200 Subject: [PATCH 7/8] matrix-sdk: Switch to upstream crates --- .travis.yml | 2 +- Cargo.toml | 3 --- matrix_sdk/examples/wasm_command_bot/Cargo.toml | 5 +---- matrix_sdk_crypto/Cargo.toml | 2 +- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7a911250..7667149b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,7 +38,7 @@ jobs: cd - cd matrix_sdk_base - cargo test --target wasm32-unknown-unknown --no-default-features --features encryption + cargo test --target wasm32-unknown-unknown --no-default-features diff --git a/Cargo.toml b/Cargo.toml index d4cd4f2c..57e18193 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,3 @@ members = [ "matrix_sdk_crypto", "matrix_sdk_common", ] - -[patch.crates-io] -olm-sys = { git = "https://gitlab.gnome.org/stoically/olm-sys", branch = "wasm-target" } diff --git a/matrix_sdk/examples/wasm_command_bot/Cargo.toml b/matrix_sdk/examples/wasm_command_bot/Cargo.toml index 397ef6c8..14c3ab92 100644 --- a/matrix_sdk/examples/wasm_command_bot/Cargo.toml +++ b/matrix_sdk/examples/wasm_command_bot/Cargo.toml @@ -10,13 +10,10 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -matrix-sdk = { path = "../..", default-features = false, features = ["encryption"] } +matrix-sdk = { path = "../..", default-features = false } url = "2.1.1" wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] } wasm-bindgen-futures = "0.4.12" web-sys = { version = "0.3.39", features = ["console"] } [workspace] - -[patch.crates-io] -olm-sys = { git = "https://gitlab.gnome.org/stoically/olm-sys", branch = "wasm-target" } diff --git a/matrix_sdk_crypto/Cargo.toml b/matrix_sdk_crypto/Cargo.toml index d3179baa..12b9c2ab 100644 --- a/matrix_sdk_crypto/Cargo.toml +++ b/matrix_sdk_crypto/Cargo.toml @@ -22,7 +22,7 @@ matrix-sdk-common = { version = "0.1.0", path = "../matrix_sdk_common" } olm-rs = { version = "0.5.0", features = ["serde"] } serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.52" -cjson = { git = "https://github.com/stoically/cjson", rev = "24a82d94e339e27a62434493869f3dce62c60e0d" } +cjson = "0.1.0" zeroize = { version = "1.1.0", features = ["zeroize_derive"] } url = "2.1.1" From 1241fd718eed2e0947c007c323667d9851c71865 Mon Sep 17 00:00:00 2001 From: stoically Date: Tue, 12 May 2020 15:56:27 +0200 Subject: [PATCH 8/8] base: Only disable sleep in WASM but still `continue` --- matrix_sdk/src/client.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index b1ad3c9a..4d4a71e3 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -848,10 +848,9 @@ impl Client { r } else { #[cfg(not(target_arch = "wasm32"))] - { - sleep::new(Duration::from_secs(1)).await; - continue; - } + sleep::new(Duration::from_secs(1)).await; + + continue; }; // TODO send out to-device messages here