Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for wasm32-unknown-unknown adoption #442

Merged
merged 77 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
b0b9729
+ ws_stream_wasm, - dev-dependency on tokio/full
bitfl0wer Nov 13, 2023
15b5eab
Format changes
bitfl0wer Nov 13, 2023
6ad5a58
compile-lock most wasm32 incompatibilities
bitfl0wer Nov 13, 2023
7f48eed
make SentryConfiguration wasm-compatible
bitfl0wer Nov 13, 2023
ba3acd8
Ensure rt and rt_multi_thread aren't both enabled
bitfl0wer Nov 13, 2023
5fff060
remove sqlx from wasm-exclusion, add futures-timer
bitfl0wer Nov 13, 2023
3124c45
Include `rand` crate in all targets
bitfl0wer Nov 13, 2023
cafd645
Use patched reqwest with multipart-headers support
bitfl0wer Nov 13, 2023
1450546
Replace tokio::time with safina-timer
bitfl0wer Nov 13, 2023
a2306eb
Several small patches (#439)
bitfl0wer Nov 14, 2023
4106758
bump version
bitfl0wer Nov 14, 2023
3f66006
Merge branch 'main' into feature/wasm32-unknown
bitfl0wer Nov 14, 2023
0c0252a
I fucked up the merge and merged gateway wrongly
bitfl0wer Nov 14, 2023
e9bb19c
Merge branch 'dev' into feature/wasm32-unknown
bitfl0wer Nov 14, 2023
7ecf443
Remove unneeded import
bitfl0wer Nov 14, 2023
80a0ab9
Factor out GatewayStore into own type
bitfl0wer Nov 14, 2023
8888b2f
Use GatewayStore instead of complex typedef
bitfl0wer Nov 14, 2023
82768c6
Start impl of Gateway Traits
bitfl0wer Nov 14, 2023
2f3927d
Add missing, least required methods to traitdef
bitfl0wer Nov 14, 2023
855518c
start converting struct impl into trait impl
bitfl0wer Nov 14, 2023
9258e0d
Impl base of GatewayCapable for Gateway
bitfl0wer Nov 14, 2023
da40d7a
Refactor Gateway module to implement traits and
bitfl0wer Nov 15, 2023
0499dac
Properly extract all extractable methods from GatewayHandle into Trait
bitfl0wer Nov 15, 2023
c6b3a28
Fix clippy warning
bitfl0wer Nov 15, 2023
82dd3d2
Remove all imports of Gateway and GatewayHandle
bitfl0wer Nov 15, 2023
497c3bd
Add prefix "Default" To Gateway, -Handler and HeartbeatHandler
bitfl0wer Nov 15, 2023
b738f4d
Refactor project to use chorus::Gateway[...]
bitfl0wer Nov 15, 2023
0c6b120
Feature lock DefaultGateway+Handle for non-wasm32
bitfl0wer Nov 15, 2023
47d09fe
re-organize files
bitfl0wer Nov 15, 2023
7f239d3
Change typedef to only compile with feature client
bitfl0wer Nov 15, 2023
c50762e
Conditionally compile wasm/default
bitfl0wer Nov 15, 2023
bacc1bf
Add wasm gateway submodule
bitfl0wer Nov 15, 2023
79b437c
Move heartbeat to shared location
bitfl0wer Nov 16, 2023
b9281b2
Create wasm gateway module
bitfl0wer Nov 16, 2023
5ffd42c
Move events to shared location
bitfl0wer Nov 16, 2023
cda9bd3
Move ws_stream_wasm to shared dependencies
bitfl0wer Nov 16, 2023
7eb5325
Make message.rs shared
bitfl0wer Nov 16, 2023
6c2d672
Add import
bitfl0wer Nov 16, 2023
1f94f29
Add Adapter GatewayMessageData
bitfl0wer Nov 16, 2023
033f2fa
Make GatewayMessage use new Adapter Type
bitfl0wer Nov 16, 2023
da944f7
Revert "Make GatewayMessage use new Adapter Type"
bitfl0wer Nov 16, 2023
36f839b
Revert "Add Adapter GatewayMessageData"
bitfl0wer Nov 16, 2023
0a8105d
Remove duplicate code
bitfl0wer Nov 16, 2023
595832e
Make heartbeathandler shared struct
bitfl0wer Nov 16, 2023
6e43b1f
pub use
bitfl0wer Nov 18, 2023
43a692b
Use ws_stream_wasm fork
bitfl0wer Nov 18, 2023
e20da50
checkpoint
bitfl0wer Nov 18, 2023
33d8adc
Fixed most errors, simplified new generic traits
bitfl0wer Nov 18, 2023
6b40840
Import trait
bitfl0wer Nov 18, 2023
70b6f3f
Remove unused code
bitfl0wer Nov 18, 2023
08e7bc5
Rename `get_handle` to `spawn`
bitfl0wer Nov 18, 2023
b03d6b4
Use upstream ws_stream_wasm
bitfl0wer Nov 18, 2023
fc6a45b
Apparently, the conditional dependency now works again. lol
bitfl0wer Nov 18, 2023
92113f3
Implement MessageCapable trait methods in gateway
bitfl0wer Nov 18, 2023
cf12923
Update Gateway types based on target architecture
bitfl0wer Nov 18, 2023
1fa1c17
Remove is_error method from MessageCapable trait
bitfl0wer Nov 18, 2023
f5b9973
Refactor WasmGateway struct and import statements
bitfl0wer Nov 18, 2023
ad76261
Add MessageCapable trait implementation for
bitfl0wer Nov 18, 2023
5785d73
Remove trait bound: From tungstenite::Message
bitfl0wer Nov 18, 2023
9d88f50
start implementing wasm gateway
bitfl0wer Nov 19, 2023
c87ad0e
Impl spawn() for wasm gateway
bitfl0wer Nov 19, 2023
f2f212a
Add todos
bitfl0wer Nov 19, 2023
129e72d
TEMP: Compile wasm dependencies&code per default
bitfl0wer Nov 19, 2023
4dc8dd9
Add kill send to WasmGateway
bitfl0wer Nov 19, 2023
f7ede92
Add TODO comment
bitfl0wer Nov 19, 2023
3598e2b
temp: impl heartbeathandlercapable
bitfl0wer Nov 19, 2023
848bfcd
Move Heartbeathandler code together
bitfl0wer Nov 19, 2023
0640144
Make HBHandler<T,S> struct instead of trait
bitfl0wer Nov 19, 2023
9a998e5
prepare for platform-dependant websockets backend
SpecificProtagonist Nov 19, 2023
7d9c839
removed outdated comment
SpecificProtagonist Nov 19, 2023
b37f487
removed leftover type aliases
SpecificProtagonist Nov 19, 2023
487bcd5
for got unwrap :3
SpecificProtagonist Nov 19, 2023
d7ec422
remove superfluous return
SpecificProtagonist Nov 19, 2023
5a9a0d1
Fix references to heartbeat_thread_communicator
bitfl0wer Nov 19, 2023
5141723
Merge branch 'websockets-backend' into feature/wasm32-unknown
bitfl0wer Nov 19, 2023
0f2d2c8
Resolve merge conflicts
bitfl0wer Nov 19, 2023
50b86d4
feature lock backend_tungstenite
bitfl0wer Nov 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
751 changes: 385 additions & 366 deletions Cargo.lock

Large diffs are not rendered by default.

51 changes: 34 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,38 @@ website = ["https://discord.com/invite/m3FpcapGDD"]


[features]
default = ["client"]
backend = ["poem", "sqlx"]
default = ["client", "rt-multi-thread"]
backend = ["dep:poem", "dep:sqlx"]
rt-multi-thread = ["tokio/rt-multi-thread"]
rt = ["tokio/rt"]
client = []

[dependencies]
tokio = { version = "1.29.1", features = ["macros"] }
tokio = { version = "1.34.0", features = ["macros", "sync"] }
serde = { version = "1.0.188", features = ["derive", "rc"] }
serde_json = { version = "1.0.105", features = ["raw_value"] }
serde-aux = "4.2.0"
serde_with = "3.3.0"
serde_repr = "0.1.16"
reqwest = { version = "0.11.20", features = ["multipart", "json"] }
reqwest = { git = "https://github.com/bitfl0wer/reqwest.git", branch = "wasm-headers", features = [
"multipart",
"json",
] }
url = "2.4.0"
chrono = { version = "0.4.26", features = ["serde"] }
regex = "1.9.4"
custom_error = "1.9.2"
tokio-tungstenite = { version = "0.20.1", features = [
"rustls-tls-native-roots",
"rustls-native-certs",
] }
futures-util = "0.3.28"
http = "0.2.9"
base64 = "0.21.3"
hostname = "0.3.1"
bitflags = { version = "2.4.0", features = ["serde"] }
lazy_static = "1.4.0"
poem = { version = "1.3.57", optional = true }
thiserror = "1.0.47"
jsonwebtoken = "8.3.0"
log = "0.4.20"
async-trait = "0.1.73"
chorus-macros = "0.2.0"
sqlx = { version = "0.7.1", features = [
"mysql",
"sqlite",
Expand All @@ -47,16 +52,28 @@ sqlx = { version = "0.7.1", features = [
"runtime-tokio-native-tls",
"any",
], optional = true }
thiserror = "1.0.47"
jsonwebtoken = "8.3.0"
log = "0.4.20"
async-trait = "0.1.73"
chorus-macros = "0.2.0"
safina-timer = "0.1.11"
rand = "0.8.5"
# TODO: Remove the below 2 imports for production!
ws_stream_wasm = "0.7.4"
pharos = "0.5.3"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rustls = "0.21.8"
rustls-native-certs = "0.6.3"
rand = "0.8.5"
tokio-tungstenite = { version = "0.20.1", features = [
"rustls-tls-native-roots",
"rustls-native-certs",
] }
native-tls = "0.2.11"
hostname = "0.3.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.11", features = ["js"] }
tokio-tungstenite = { version = "0.20.1", default-features = false }
ws_stream_wasm = "0.7.4"
pharos = "0.5.3"


[dev-dependencies]
tokio = { version = "1.32.0", features = ["full"] }
lazy_static = "1.4.0"
rusty-hook = "0.11.2"
7 changes: 4 additions & 3 deletions examples/gateway_observers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use async_trait::async_trait;
use chorus::gateway::Gateway;
use chorus::{
self,
gateway::{Gateway, Observer},
gateway::Observer,
types::{GatewayIdentifyPayload, GatewayReady},
};
use std::{sync::Arc, time::Duration};
Expand All @@ -24,13 +25,13 @@ impl Observer<GatewayReady> for ExampleObserver {
}
}

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() {
// Find the gateway websocket url of the server we want to connect to
let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string();

// Initiate the gateway connection
let gateway = Gateway::new(websocket_url_spacebar).await.unwrap();
let gateway = Gateway::spawn(websocket_url_spacebar).await.unwrap();

// Create an instance of our observer
let observer = ExampleObserver {};
Expand Down
7 changes: 4 additions & 3 deletions examples/gateway_simple.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::time::Duration;

use chorus::{self, gateway::Gateway, types::GatewayIdentifyPayload};
use chorus::gateway::Gateway;
use chorus::{self, types::GatewayIdentifyPayload};
use tokio::time::sleep;

/// This example creates a simple gateway connection and a session with an Identify event
#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() {
// Find the gateway websocket url of the server we want to connect to
let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string();

// Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another
let gateway = Gateway::new(websocket_url_spacebar).await.unwrap();
let gateway = Gateway::spawn(websocket_url_spacebar).await.unwrap();

// At this point, we are connected to the server and are sending heartbeats, however we still haven't authenticated

Expand Down
2 changes: 1 addition & 1 deletion examples/instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chorus::instance::Instance;
use chorus::UrlBundle;

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() {
let bundle = UrlBundle::new(
"https://example.com/api".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion examples/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chorus::instance::Instance;
use chorus::types::LoginSchema;
use chorus::UrlBundle;

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() {
let bundle = UrlBundle::new(
"https://example.com/api".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/api/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Instance {
self.limits_information.as_mut().unwrap().ratelimits = shell.limits.clone().unwrap();
}
let mut identify = GatewayIdentifyPayload::common();
let gateway = Gateway::new(self.urls.wss.clone()).await.unwrap();
let gateway = Gateway::spawn(self.urls.wss.clone()).await.unwrap();
identify.token = login_result.token.clone();
gateway.send_identify(identify).await;
let user = ChorusUser::new(
Expand Down
4 changes: 2 additions & 2 deletions src/api/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::sync::{Arc, RwLock};
pub use login::*;
pub use register::*;

use crate::gateway::Gateway;
use crate::{
errors::ChorusResult,
gateway::Gateway,
instance::{ChorusUser, Instance},
types::{GatewayIdentifyPayload, User},
};
Expand All @@ -25,7 +25,7 @@ impl Instance {
.await
.unwrap();
let mut identify = GatewayIdentifyPayload::common();
let gateway = Gateway::new(self.urls.wss.clone()).await.unwrap();
let gateway = Gateway::spawn(self.urls.wss.clone()).await.unwrap();
identify.token = token.clone();
gateway.send_identify(identify).await;
let user = ChorusUser::new(
Expand Down
4 changes: 2 additions & 2 deletions src/api/auth/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
use reqwest::Client;
use serde_json::to_string;

use crate::gateway::Gateway;
use crate::gateway::{Gateway, GatewayHandle};
use crate::types::GatewayIdentifyPayload;
use crate::{
errors::ChorusResult,
Expand Down Expand Up @@ -45,7 +45,7 @@ impl Instance {
let user_object = self.get_user(token.clone(), None).await.unwrap();
let settings = ChorusUser::get_settings(&token, &self.urls.api.clone(), &mut self).await?;
let mut identify = GatewayIdentifyPayload::common();
let gateway = Gateway::new(self.urls.wss.clone()).await.unwrap();
let gateway: GatewayHandle = Gateway::spawn(self.urls.wss.clone()).await.unwrap();
identify.token = token.clone();
gateway.send_identify(identify).await;
let user = ChorusUser::new(
Expand Down
65 changes: 65 additions & 0 deletions src/gateway/backend_tungstenite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use futures_util::{
stream::{SplitSink, SplitStream},
StreamExt,
};
use tokio::net::TcpStream;
use tokio_tungstenite::{
connect_async_tls_with_config, tungstenite, Connector, MaybeTlsStream, WebSocketStream,
};

use super::GatewayMessage;
use crate::errors::GatewayError;

#[derive(Debug, Clone)]
pub struct WebSocketBackend;

// These could be made into inherent associated types when that's stabilized
pub type WsSink = SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, tungstenite::Message>;
pub type WsStream = SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>;

impl WebSocketBackend {
pub async fn connect(
websocket_url: &str,
) -> Result<(WsSink, WsStream), crate::errors::GatewayError> {
let mut roots = rustls::RootCertStore::empty();
for cert in rustls_native_certs::load_native_certs().expect("could not load platform certs")
{
roots.add(&rustls::Certificate(cert.0)).unwrap();
}
let (websocket_stream, _) = match connect_async_tls_with_config(
websocket_url,
None,
false,
Some(Connector::Rustls(
rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(roots)
.with_no_client_auth()
.into(),
)),
)
.await
{
Ok(websocket_stream) => websocket_stream,
Err(e) => {
return Err(GatewayError::CannotConnect {
error: e.to_string(),
})
}
};

Ok(websocket_stream.split())
}
}

impl From<GatewayMessage> for tungstenite::Message {
fn from(message: GatewayMessage) -> Self {
Self::Text(message.0)
}
}

impl From<tungstenite::Message> for GatewayMessage {
fn from(value: tungstenite::Message) -> Self {
Self(value.to_string())
}
}
Loading