Skip to content

Fix wasm failing build due to getrandom in uuid library #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ serde_json = "1.0"
time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing"] }
jsonwebtoken = { version = "8", default-features = false }
yaup = "0.2.0"
uuid = { version = "1.1.2", features = ["v4"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
futures = "0.3"
isahc = { version = "1.0", features = ["http2", "text-decoding"], default_features = false }
uuid = { version = "1.1.2", features = ["v4"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.47"
Expand Down
1 change: 1 addition & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ impl Client {
/// let client = client::Client::new(MEILISEARCH_HOST, token);
/// # });
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub fn generate_tenant_token(
&self,
api_key_uid: String,
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub enum Error {
// The library formating the query parameters encountered an error.
Yaup(yaup::Error),
// The library validating the format of an uuid.
#[cfg(not(target_arch = "wasm32"))]
Uuid(uuid::Error),
// Error thrown in case the version of the Uuid is not v4.
InvalidUuid4Version,
Expand Down Expand Up @@ -79,6 +80,7 @@ impl From<yaup::Error> for Error {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<uuid::Error> for Error {
fn from(error: uuid::Error) -> Error {
Error::Uuid(error)
Expand Down Expand Up @@ -205,6 +207,7 @@ impl std::fmt::Display for Error {
Error::TenantTokensExpiredSignature => write!(fmt, "The provided expires_at is already expired."),
Error::InvalidTenantToken(e) => write!(fmt, "Impossible to generate the token, jsonwebtoken encountered an error: {}", e),
Error::Yaup(e) => write!(fmt, "Internal Error: could not parse the query parameters: {}", e),
#[cfg(not(target_arch = "wasm32"))]
Error::Uuid(e) => write!(fmt, "The uid of the token has bit an uuid4 format: {}", e),
Error::InvalidUuid4Version => write!(fmt, "The uid provided to the token is not of version uuidv4")
}
Expand Down
27 changes: 18 additions & 9 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
let user_agent = qualified_version();

// The 2 following unwraps should not be able to fail

let mut mut_url = url.clone().to_string();
let headers = Headers::new().unwrap();
headers.append("Authorization: Bearer", apikey).unwrap();
headers.append("User-Agent", &user_agent).unwrap();
Expand All @@ -121,7 +121,15 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
request.headers(&headers);

match &method {
Method::Get => {
Method::Get(query) => {
let query = yaup::to_string(query)?;

mut_url = if query.is_empty() {
mut_url.to_string()
} else {
format!("{}?{}", mut_url, query)
};

request.method("GET");
}
Method::Delete => {
Expand All @@ -145,13 +153,14 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
}

let window = web_sys::window().unwrap(); // TODO remove this unwrap
let response = match JsFuture::from(window.fetch_with_str_and_init(url, &request)).await {
Ok(response) => Response::from(response),
Err(e) => {
error!("Network error: {:?}", e);
return Err(Error::UnreachableServer);
}
};
let response =
match JsFuture::from(window.fetch_with_str_and_init(mut_url.as_str(), &request)).await {
Ok(response) => Response::from(response),
Err(e) => {
error!("Network error: {:?}", e);
return Err(Error::UnreachableServer);
}
};
let status = response.status() as u16;
let text = match response.text() {
Ok(text) => match JsFuture::from(text).await {
Expand Down
3 changes: 3 additions & 0 deletions src/tenant_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use jsonwebtoken::{encode, EncodingKey, Header};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::OffsetDateTime;
#[cfg(not(target_arch = "wasm32"))]
use uuid::Uuid;

#[derive(Debug, Serialize, Deserialize)]
#[cfg(not(target_arch = "wasm32"))]
#[serde(rename_all = "camelCase")]
struct TenantTokenClaim {
api_key_uid: String,
Expand All @@ -14,6 +16,7 @@ struct TenantTokenClaim {
exp: Option<OffsetDateTime>,
}

#[cfg(not(target_arch = "wasm32"))]
pub fn generate_tenant_token(
api_key_uid: String,
search_rules: Value,
Expand Down