Skip to content

Commit

Permalink
trick: to unlock toriiClient with safari/firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Jun 10, 2024
1 parent 21fe88d commit 29a3a7e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
26 changes: 22 additions & 4 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 @@ -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")
}
None => empty_state_update(),
}))))
}
}
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![],
},
}
}
8 changes: 8 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 @@ -87,6 +88,13 @@ impl StateDiffManager {
.await
.insert(id, ModelDiffSubscriber { storage_addresses, sender });

// unlock issue with firefox/safari
// send empty model update to unlock browsers ...
let subscribers = self.subscribers.write().await;
let _ = subscribers.get(&id).unwrap().sender.send(Ok(SubscribeModelsResponse {
model_update: None
})).await;

Ok(receiver)
}

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 29a3a7e

Please sign in to comment.