Skip to content

Fix wasm panic when trying to change the user-agent #330

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 3 commits into from
Sep 7, 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 CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Also, the WASM example compilation should be checked:

```bash
rustup target add wasm32-unknown-unknown
cargo check --example web_app --target wasm32-unknown-unknown --features=sync
cargo check -p web_app --target wasm32-unknown-unknown
```

Each PR should pass the tests to be accepted.
Expand Down
4 changes: 2 additions & 2 deletions examples/web_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The Rust source files are compiled into WebAssembly and so can be readable by th
If you only want to check if this example compiles, you can run:

```console
cargo build --example web_app
cargo build
```

## Building
Expand All @@ -23,7 +23,7 @@ curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
```

```console
wasm-pack build examples/web_app/ --target=web --no-typescript
wasm-pack build . --target=web --no-typescript
```

The compiled files will be stored in the `examples/web_app/pkg` folder.
Expand Down
5 changes: 1 addition & 4 deletions examples/web_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ mod document;
use crate::document::{display, Crate};

lazy_static! {
static ref CLIENT: Client = Client::new(
"https://finding-demos.meilisearch.com",
"2b902cce4f868214987a9f3c7af51a69fa660d74a785bed258178b96e3480bb3",
);
static ref CLIENT: Client = Client::new("http://localhost:7700", "masterKey",);
}

struct Model {
Expand Down
15 changes: 8 additions & 7 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static

const CONTENT_TYPE: &str = "Content-Type";
const JSON: &str = "application/json";
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();
headers
.append("Authorization", format!("Bearer {}", apikey).as_str())
.unwrap();
headers
.append("X-Meilisearch-Client", qualified_version().as_str())
.unwrap();

let mut request: RequestInit = RequestInit::new();
request.headers(&headers);
Expand All @@ -124,10 +127,8 @@ pub(crate) async fn request<Input: Serialize, Output: DeserializeOwned + 'static
Method::Get(query) => {
let query = yaup::to_string(query)?;

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

request.method("GET");
Expand Down