Skip to content

Commit

Permalink
Merge pull request #4920 from wasmerio/backend-api-wasm32
Browse files Browse the repository at this point in the history
Compile `wasmer-api` crate to `wasm32-unknown-unknown`
  • Loading branch information
theduke authored Jul 12, 2024
2 parents 9ef28e9 + 17b7758 commit 53b5c3b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion lib/backend-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ repository.workspace = true
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# Wasmer dependencies.
edge-schema.workspace = true
Expand All @@ -34,6 +33,11 @@ pin-project-lite = "0.2.10"
serde_path_to_error = "0.1.14"
harsh = "0.2.2"
reqwest = { version = "0.11.13", default-features = false, features = ["json"] }
merge-streams = "0.1.2"

[target.'cfg(target_family = "wasm")'.dependencies.getrandom]
version = "0.2.14"
features = ["js"]

[dev-dependencies]
base64 = "0.13.1"
Expand Down
2 changes: 1 addition & 1 deletion lib/backend-api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ type Namespace implements Node & PackageOwner & Owner {
name: String!
displayName: String
description: String!
avatar: String!
avatarUpdatedAt: DateTime
twitterHandle: String
githubHandle: String
Expand All @@ -209,6 +208,7 @@ type Namespace implements Node & PackageOwner & Owner {
userSet(offset: Int, before: String, after: String, first: Int, last: Int): UserConnection!
globalName: String!
globalId: ID!
avatar: String!
packages(offset: Int, before: String, after: String, first: Int, last: Int): PackageConnection!
apps(sortBy: DeployAppsSortBy, offset: Int, before: String, after: String, first: Int, last: Int): DeployAppConnection!
packageVersions(offset: Int, before: String, after: String, first: Int, last: Int): PackageVersionConnection!
Expand Down
11 changes: 9 additions & 2 deletions lib/backend-api/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(not(target_family = "wasm"))]
use std::time::Duration;

use crate::GraphQLApiFailure;
use anyhow::{bail, Context as _};
use cynic::{http::CynicReqwestError, GraphQlResponse, Operation};
use url::Url;

use crate::GraphQLApiFailure;

/// API client for the Wasmer API.
///
/// Use the queries in [`crate::queries`] to interact with the API.
Expand Down Expand Up @@ -53,11 +53,18 @@ impl WasmerClient {
}

pub fn new(graphql_endpoint: Url, user_agent: &str) -> Result<Self, anyhow::Error> {
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
let client = reqwest::Client::builder()
.build()
.context("could not construct http client")?;

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
let client = reqwest::Client::builder()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(90))
.build()
.context("could not construct http client")?;

Self::new_with_client(client, graphql_endpoint, user_agent)
}

Expand Down
31 changes: 16 additions & 15 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{collections::HashSet, pin::Pin, time::Duration};
use std::{collections::HashSet, time::Duration};

use anyhow::{bail, Context};
use cynic::{MutationBuilder, QueryBuilder};
use edge_schema::schema::NetworkTokenV1;
use futures::{Stream, StreamExt};
use futures::StreamExt;
use merge_streams::MergeStreams;
use time::OffsetDateTime;
use tracing::Instrument;
use url::Url;
Expand Down Expand Up @@ -958,8 +959,7 @@ pub async fn user_accessible_apps(
impl futures::Stream<Item = Result<Vec<types::DeployApp>, anyhow::Error>> + '_,
anyhow::Error,
> {
let apps: Pin<Box<dyn Stream<Item = Result<Vec<DeployApp>, anyhow::Error>> + Send + Sync>> =
Box::pin(user_apps(client).await);
let user_apps = user_apps(client).await;

// Get all aps in user-accessible namespaces.
let namespace_res = client
Expand All @@ -979,17 +979,13 @@ pub async fn user_accessible_apps(
.map(|node| node.name.clone())
.collect::<Vec<_>>();

let mut all_apps = vec![apps];
let mut ns_apps = vec![];
for ns in namespace_names {
let apps: Pin<Box<dyn Stream<Item = Result<Vec<DeployApp>, anyhow::Error>> + Send + Sync>> =
Box::pin(namespace_apps(client, ns).await);

all_apps.push(apps);
let apps = namespace_apps(client, ns).await;
ns_apps.push(apps);
}

let apps = futures::stream::select_all(all_apps);

Ok(apps)
Ok((user_apps, ns_apps.merge()).merge())
}

/// Get apps for a specific namespace.
Expand Down Expand Up @@ -1351,10 +1347,15 @@ fn get_app_logs(
if page.is_empty() {
if watch {
/*
TODO: the resolution of watch should be configurable
TODO: should this be async?
*/
* [TODO]: The resolution here should be configurable.
*/

#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
std::thread::sleep(Duration::from_secs(1));

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
tokio::time::sleep(Duration::from_secs(1)).await;

continue;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/backend-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod queries {
pub viewer: Option<UserWithNamespaces>,
}

#[derive(cynic::QueryFragment, Debug)]
#[derive(cynic::QueryFragment, Debug, serde::Serialize)]
pub struct User {
pub id: cynic::Id,
pub username: String,
Expand Down

0 comments on commit 53b5c3b

Please sign in to comment.