Skip to content

Commit

Permalink
Merge pull request #5137 from wasmerio/fix-js-build
Browse files Browse the repository at this point in the history
Trying to fix js build
  • Loading branch information
syrusakbary authored Oct 10, 2024
2 parents 713e02d + 6ca0817 commit 8fccddd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
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

0 comments on commit 8fccddd

Please sign in to comment.