Skip to content

Commit 840c819

Browse files
committed
fixing wasm
1 parent 138c3f3 commit 840c819

File tree

7 files changed

+170
-37
lines changed

7 files changed

+170
-37
lines changed

Cargo.lock

Lines changed: 122 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ edition = "2021"
1313
resolver = "2"
1414

1515
[features]
16-
default = ["tokio-comp", "compression"]
16+
default = ["compression"]
1717
compression = ["libflate"]
18-
tokio-comp = ["tokio"]
19-
async-std-comp = ["async-std"]
2018

2119
[dependencies]
2220
anyhow = "1"
@@ -32,21 +30,25 @@ reqwest = { version = "0.11", default-features = false, features = ["json", "rus
3230
serde_json = "1"
3331
serde = { version = "1", features = ["derive"] }
3432
sha2 = "0.10"
35-
tonic = "0.10"
3633
tracing = "0.1"
3734
tracing-futures = { version = "0.2.5", default-features = false, features = ["futures-03", "std"] }
38-
uuid = { version = "1.7", features = ["v4"] } # A library to generate and parse UUIDs.
35+
uuid = { version = "1.7", features = ["v4", "js"] } # A library to generate and parse UUIDs.
36+
wasm-bindgen-futures = "0.4.18"
3937

4038
# Non-feature optional dependencies
4139
libflate = { version = "2", optional = true }
42-
async-std = { version = "1", features = ["default"], optional = true }
4340

4441
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
45-
tokio = { version = "1", features = ["full"], optional = true }
42+
tonic = "0.10"
43+
tokio = { version = "1", features = ["full"] }
4644

4745
[build-dependencies]
46+
async-std = { version = "1", default-features = false, features = ["default", "tokio1"] }
47+
cfg-if = "1.0.0"
4848
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }
49+
prost-build = "0.12.3"
50+
protox = "0.6.0"
4951
tonic-build = "0.10"
50-
cfg-if = "1.0.0"
51-
tokio = { version = "1", features = ["full"], optional = true }
52-
async-std = { version = "1", features = ["default", "tokio1"], optional = true }
52+
53+
[target.'cfg(not(target_arch = "wasm32"))'.build-dependencies]
54+
tokio = { version = "1", features = ["full"] }

build.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
// Derived from https://github.com/pellizzetti/router/blob/cc0ebcaf1d68184e1fe06f16534fddff76286b40/apollo-spaceport/build.rs
2+
use protox::prost::Message;
3+
use std::path::PathBuf;
24
use std::{
5+
env,
36
error::Error,
7+
fs,
48
fs::File,
59
io::{copy, Read},
610
};
711

812
fn main() -> Result<(), Box<dyn Error>> {
913
// Skip building online from docs.rs
10-
if std::env::var_os("DOCS_RS").is_some() {} else {
14+
if std::env::var_os("DOCS_RS").is_some() {
15+
} else {
1116
// Retrieve a live version of the reports.proto file
1217
let proto_url = "https://usage-reporting.api.apollographql.com/proto/reports.proto";
1318
let fut = reqwest::get(proto_url);
1419

1520
cfg_if::cfg_if! {
16-
if #[cfg(feature = "tokio-comp")] {
21+
if #[cfg(not(target_arch = "wasm32"))] {
1722
let rt = tokio::runtime::Runtime::new().unwrap();
1823
let response = rt.block_on(fut)?;
1924
let mut content = rt.block_on(response.text())?;
20-
} else if #[cfg(feature = "async-std-comp")] {
25+
} else {
2126
let response = async_std::task::block_on(fut)?;
2227
let mut content = async_std::task::block_on(response.text())?;
23-
} else {
24-
compile_error!("tokio-comp or async-std-comp features required");
2528
}
2629
}
2730

@@ -58,6 +61,12 @@ fn main() -> Result<(), Box<dyn Error>> {
5861
// Process the proto files
5962
let proto_files = vec!["proto/agents.proto", "proto/reports.proto"];
6063

64+
let file_descriptors = protox::compile(&proto_files, ["."]).unwrap();
65+
66+
let file_descriptor_path =
67+
PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("file_descriptor_set.bin");
68+
fs::write(&file_descriptor_path, file_descriptors.encode_to_vec())?;
69+
6170
tonic_build::configure()
6271
.type_attribute("ContextualizedStats", "#[derive(serde::Serialize)]")
6372
.type_attribute("StatsContext", "#[derive(serde::Serialize)]")
@@ -68,6 +77,8 @@ fn main() -> Result<(), Box<dyn Error>> {
6877
.type_attribute("ReferencedFieldsForType", "#[derive(serde::Serialize)]")
6978
.type_attribute("StatsContext", "#[derive(Eq, Hash)]")
7079
.build_server(true)
80+
.file_descriptor_set_path(&file_descriptor_path)
81+
.skip_protoc_run()
7182
.compile(&proto_files, &["."])?;
7283

7384
for file in proto_files {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ use runtime::spawn;
4040
#[macro_use]
4141
extern crate tracing;
4242

43-
use std::sync::RwLock;
4443
use std::collections::HashMap;
4544
use std::sync::Arc;
45+
use std::sync::RwLock;
4646

4747
use async_graphql::QueryPathSegment;
4848
use chrono::{DateTime, Utc};

src/proto.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#![allow(rustdoc::all)]
22
#![allow(clippy::all)]
33

4-
pub mod report {
5-
tonic::include_proto!("report");
6-
}
7-
8-
pub mod agent {
9-
tonic::include_proto!("agent");
4+
cfg_if::cfg_if! {
5+
if #[cfg(not(target_arch = "wasm32"))] {
6+
pub mod report {
7+
tonic::include_proto!("report");
8+
}
9+
} else {
10+
pub mod report {
11+
include!(concat!(env!("OUT_DIR"), "/report.rs"));
12+
}
13+
}
1014
}

src/report_aggregator/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use futures::{
88
use crate::{
99
packages::uname::Uname,
1010
proto::report::{Report, ReportHeader, Trace, TracesAndStats},
11-
runtime::{spawn, JoinHandle, abort, Instant},
11+
runtime::{abort, spawn, Instant, JoinHandle},
1212
};
1313

1414
/// The [ReportAggregator] is the structure which control the background task spawned to aggregate

0 commit comments

Comments
 (0)