Skip to content
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

Trying to fix js build #5137

Merged
merged 4 commits into from
Oct 10, 2024
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
35 changes: 23 additions & 12 deletions lib/wasix/src/http/reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, time::Duration};
use std::time::Duration;

use anyhow::Context;
use futures::{future::BoxFuture, TryStreamExt};
Expand Down Expand Up @@ -44,9 +44,10 @@ impl ReqwestHttpClient {
// TODO: use persistent client?
let builder = {
let _guard = Handle::try_current().map_err(|_| self.handle.enter());
let mut builder = reqwest::ClientBuilder::new().connect_timeout(self.connect_timeout);
if let Some(proxy) = get_proxy()? {
builder = builder.proxy(proxy);
let mut builder = reqwest::ClientBuilder::new();
#[cfg(not(feature = "js"))]
{
builder = builder.connect_timeout(self.connect_timeout);
}
builder
};
Expand All @@ -71,6 +72,7 @@ impl ReqwestHttpClient {
let status = response.status();

// Download the body.
#[cfg(not(feature = "js"))]
let data = if let Some(timeout) = self.response_body_chunk_timeout {
// Download the body with a chunk timeout.
// The timeout prevents long stalls.
Expand Down Expand Up @@ -126,6 +128,8 @@ impl ReqwestHttpClient {
} else {
response.bytes().await?.to_vec()
};
#[cfg(feature = "js")]
let data = response.bytes().await?.to_vec();

Ok(HttpResponse {
status,
Expand All @@ -137,19 +141,26 @@ impl ReqwestHttpClient {
}

impl super::HttpClient for ReqwestHttpClient {
#[cfg(not(feature = "js"))]
fn request(&self, request: HttpRequest) -> BoxFuture<'_, Result<HttpResponse, anyhow::Error>> {
let client = self.clone();
let f = async move { client.request(request).await };
Box::pin(f)
}
}

pub fn get_proxy() -> Result<Option<reqwest::Proxy>, anyhow::Error> {
if let Ok(scheme) = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY")) {
let proxy = reqwest::Proxy::all(scheme)?;

Ok(Some(proxy))
} else {
Ok(None)
#[cfg(feature = "js")]
fn request(&self, request: HttpRequest) -> BoxFuture<'_, Result<HttpResponse, anyhow::Error>> {
let client = self.clone();
let (sender, receiver) = futures::channel::oneshot::channel();
wasm_bindgen_futures::spawn_local(async move {
let result = client.request(request).await;
let _ = sender.send(result);
});
Box::pin(async move {
match receiver.await {
Ok(result) => result,
Err(e) => Err(anyhow::Error::new(e)),
}
})
}
}
3 changes: 3 additions & 0 deletions lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ impl WasiRunner {
if self.wasi.is_home_mapped {
builder.set_current_dir(MAPPED_CURRENT_DIR_DEFAULT_PATH);
}
if let Some(current_dir) = &self.wasi.current_dir {
builder.set_current_dir(current_dir.clone());
}

Ok(builder)
}
Expand Down
11 changes: 10 additions & 1 deletion lib/wasix/tests/runners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use reqwest::Client;
use tokio::runtime::Handle;
use wasmer::Engine;
use wasmer_wasix::{
http::{reqwest::get_proxy, HttpClient},
http::HttpClient,
runners::Runner,
runtime::{
module_cache::{FileSystemCache, ModuleCache, SharedCache},
Expand Down Expand Up @@ -228,6 +228,15 @@ async fn download_cached(url: &str) -> bytes::Bytes {
body
}

pub fn get_proxy() -> Result<Option<reqwest::Proxy>, anyhow::Error> {
if let Ok(scheme) = std::env::var("http_proxy").or_else(|_| std::env::var("HTTP_PROXY")) {
let proxy = reqwest::Proxy::all(scheme)?;
Ok(Some(proxy))
} else {
Ok(None)
}
}

fn client() -> Client {
let builder = {
let mut builder = reqwest::ClientBuilder::new().connect_timeout(Duration::from_secs(30));
Expand Down
Loading