Skip to content

Commit

Permalink
build torii-grpc for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Sep 20, 2023
1 parent 2be9b58 commit 0bfc367
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 32 deletions.
170 changes: 154 additions & 16 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ prost = "0.12"
tonic = "0.10"
tonic-build = "0.10"

# WASM-compatible gRPC deps
tonic-web-wasm-client = "0.4.0"
wasm-prost = { version = "0.11.9", package = "prost" }
wasm-tonic = { version = "0.9.2", default-features = false, features = [ "codegen", "gzip", "prost" ], package = "tonic" }
wasm-tonic-build = { version = "0.9.2", default-features = false, features = [ "prost" ], package = "tonic-build" }

[patch."https://github.com/starkware-libs/blockifier"]
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "c794d1b" }

Expand Down
5 changes: 1 addition & 4 deletions crates/torii/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ starknet-crypto.workspace = true
starknet.workspace = true
thiserror.workspace = true
tonic.workspace = true
torii-grpc = { path = "../grpc" }
torii-grpc = { path = "../grpc", features = [ "client" ] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.16", features = [ "time" ], default-features = false }
Expand All @@ -36,6 +36,3 @@ camino.workspace = true
dojo-test-utils = { path = "../../dojo-test-utils", features = [ "build-examples" ] }
dojo-world = { path = "../../dojo-world" }
tokio.workspace = true

[build-dependencies]
tonic-build.workspace = true
1 change: 1 addition & 0 deletions crates/torii/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct ClientBuilder {
}

impl ClientBuilder {
#[must_use]
pub fn new() -> Self {
Self { initial_entities_to_sync: None }
}
Expand Down
24 changes: 17 additions & 7 deletions crates/torii/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,31 @@ version.workspace = true
[dependencies]
anyhow.workspace = true
dojo-types = { path = "../../dojo-types" }
futures-util = "0.3.28"
futures.workspace = true
parking_lot.workspace = true
prost.workspace = true
rayon.workspace = true
serde_json.workspace = true
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "sqlite", "uuid" ] }
starknet-crypto.workspace = true
starknet.workspace = true
thiserror.workspace = true
tokio = { version = "1.32.0", features = [ "sync" ], default-features = true }
tokio-stream = { version = "0.1.14", default-features = false }

[target.'cfg(target_arch = "wasm32")'.dependencies]
tonic-web-wasm-client.workspace = true
wasm-prost.workspace = true
wasm-tonic.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
futures-util = "0.3.28"
prost.workspace = true
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "sqlite", "uuid" ] }
tokio-stream = "0.1.14"
tokio.workspace = true
tonic.workspace = true
tracing.workspace = true
url.workspace = true

[build-dependencies]
tonic-build.workspace = true
wasm-tonic-build.workspace = true

[features]
client = [ ]
server = [ ] # this feature can't be build on wasm32
20 changes: 19 additions & 1 deletion crates/torii/grpc/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("proto/world.proto")?;
let target = std::env::var("TARGET").expect("failed to get TARGET environment variable");
let feature_client = std::env::var("CARGO_FEATURE_CLIENT");
let feature_server = std::env::var("CARGO_FEATURE_SERVER");

if target.contains("wasm32") {
if feature_server.is_ok() {
panic!("feature `server` is not supported on target `{}`", target);
}

wasm_tonic_build::configure()
.build_server(false)
.build_client(feature_client.is_ok())
.compile(&["proto/world.proto"], &["proto"])?;
} else {
tonic_build::configure()
.build_server(feature_server.is_ok())
.build_client(feature_client.is_ok())
.compile(&["proto/world.proto"], &["proto"])?;
}
Ok(())
}
Loading

0 comments on commit 0bfc367

Please sign in to comment.