Skip to content

Commit

Permalink
[trick] to unlock toriiClient with safari/firefox (#2038)
Browse files Browse the repository at this point in the history
* trick: to unlock toriiClient with safari/firefox

* same trick for other subscribers

* refactor: send initial stream message directly

* chore: avoid sender clone

---------

Co-authored-by: Nasr <[email protected]>
  • Loading branch information
notV4l and Larkooo committed Jun 13, 2024
1 parent c402ad6 commit a753f11
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
32 changes: 25 additions & 7 deletions crates/torii/grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::num::ParseIntError;

use futures_util::stream::MapOk;
use futures_util::{Stream, StreamExt, TryStreamExt};
use starknet::core::types::{FromStrError, StateUpdate};
use starknet::core::types::{FromStrError, StateDiff, StateUpdate};
use starknet_crypto::FieldElement;

use crate::proto::world::{
Expand Down Expand Up @@ -105,9 +105,9 @@ impl WorldClient {
.map_err(Error::Grpc)
.map(|res| res.into_inner())?;

Ok(EntityUpdateStreaming(stream.map_ok(Box::new(|res| {
let entity = res.entity.expect("entity must exist");
entity.try_into().expect("must able to serialize")
Ok(EntityUpdateStreaming(stream.map_ok(Box::new(|res| match res.entity {
Some(entity) => entity.try_into().expect("must able to serialize"),
None => Entity { hashed_keys: FieldElement::ZERO, models: vec![] },

Check warning on line 110 in crates/torii/grpc/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/client.rs#L108-L110

Added lines #L108 - L110 were not covered by tests
}))))
}

Expand Down Expand Up @@ -144,9 +144,11 @@ impl WorldClient {
.map_err(Error::Grpc)
.map(|res| res.into_inner())?;

Ok(ModelDiffsStreaming(stream.map_ok(Box::new(|res| {
let update = res.model_update.expect("qed; state update must exist");
TryInto::<StateUpdate>::try_into(update).expect("must able to serialize")
Ok(ModelDiffsStreaming(stream.map_ok(Box::new(|res| match res.model_update {
Some(update) => {
TryInto::<StateUpdate>::try_into(update).expect("must able to serialize")

Check warning on line 149 in crates/torii/grpc/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/client.rs#L147-L149

Added lines #L147 - L149 were not covered by tests
}
None => empty_state_update(),

Check warning on line 151 in crates/torii/grpc/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/client.rs#L151

Added line #L151 was not covered by tests
}))))
}
}
Expand Down Expand Up @@ -184,3 +186,19 @@ impl Stream for EntityUpdateStreaming {
self.0.poll_next_unpin(cx)
}
}

fn empty_state_update() -> StateUpdate {
StateUpdate {
block_hash: FieldElement::ZERO,
new_root: FieldElement::ZERO,
old_root: FieldElement::ZERO,
state_diff: StateDiff {
declared_classes: vec![],
deployed_contracts: vec![],
deprecated_declared_classes: vec![],
nonces: vec![],
replaced_classes: vec![],
storage_diffs: vec![],
},
}
}

Check warning on line 204 in crates/torii/grpc/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/client.rs#L190-L204

Added lines #L190 - L204 were not covered by tests
6 changes: 6 additions & 0 deletions crates/torii/grpc/src/server/subscriptions/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use torii_core::types::Entity;
use tracing::{error, trace};

use crate::proto;
use crate::proto::world::SubscribeEntityResponse;

pub(crate) const LOG_TARGET: &str = "torii::grpc::server::subscriptions::entity";

Expand All @@ -43,6 +44,11 @@ impl EntityManager {
let id = rand::thread_rng().gen::<usize>();
let (sender, receiver) = channel(1);

// NOTE: unlock issue with firefox/safari
// initially send empty stream message to return from
// initial subscribe call
let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await;

Check warning on line 50 in crates/torii/grpc/src/server/subscriptions/entity.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/subscriptions/entity.rs#L47-L50

Added lines #L47 - L50 were not covered by tests

self.subscribers.write().await.insert(
id,
EntitiesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender },
Expand Down
6 changes: 6 additions & 0 deletions crates/torii/grpc/src/server/subscriptions/event_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use torii_core::types::EventMessage;
use tracing::{error, trace};

use crate::proto;
use crate::proto::world::SubscribeEntityResponse;

pub(crate) const LOG_TARGET: &str = "torii::grpc::server::subscriptions::event_message";
pub struct EventMessagesSubscriber {
Expand All @@ -42,6 +43,11 @@ impl EventMessageManager {
let id = rand::thread_rng().gen::<usize>();
let (sender, receiver) = channel(1);

// NOTE: unlock issue with firefox/safari
// initially send empty stream message to return from
// initial subscribe call
let _ = sender.send(Ok(SubscribeEntityResponse { entity: None })).await;

Check warning on line 49 in crates/torii/grpc/src/server/subscriptions/event_message.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/subscriptions/event_message.rs#L46-L49

Added lines #L46 - L49 were not covered by tests

self.subscribers.write().await.insert(
id,
EventMessagesSubscriber { hashed_keys: hashed_keys.iter().cloned().collect(), sender },
Expand Down
6 changes: 6 additions & 0 deletions crates/torii/grpc/src/server/subscriptions/model_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use tracing::{debug, error, trace};

use super::error::SubscriptionError;
use crate::proto;
use crate::proto::world::SubscribeModelsResponse;
use crate::types::KeysClause;

pub(crate) const LOG_TARGET: &str = "torii::grpc::server::subscriptions::model_diff";
Expand Down Expand Up @@ -82,6 +83,11 @@ impl StateDiffManager {
.flatten()
.collect::<HashSet<FieldElement>>();

// NOTE: unlock issue with firefox/safari
// initially send empty stream message to return from
// initial subscribe call
let _ = sender.send(Ok(SubscribeModelsResponse { model_update: None })).await;

Check warning on line 89 in crates/torii/grpc/src/server/subscriptions/model_diff.rs

View check run for this annotation

Codecov / codecov/patch

crates/torii/grpc/src/server/subscriptions/model_diff.rs#L86-L89

Added lines #L86 - L89 were not covered by tests

self.subscribers
.write()
.await
Expand Down
3 changes: 1 addition & 2 deletions crates/torii/server/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tower::ServiceBuilder;
use tower_http::cors::{AllowOrigin, CorsLayer};
use tracing::error;

const DEFAULT_ALLOW_HEADERS: [&str; 12] = [
const DEFAULT_ALLOW_HEADERS: [&str; 11] = [
"accept",
"origin",
"content-type",
Expand All @@ -27,7 +27,6 @@ const DEFAULT_ALLOW_HEADERS: [&str; 12] = [
"x-grpc-timeout",
"x-user-agent",
"connection",
"upgrade",
"sec-websocket-key",
"sec-websocket-version",
];
Expand Down

0 comments on commit a753f11

Please sign in to comment.