Skip to content

Commit

Permalink
feat(turbo-tasks-fetch): allow specifying a proxy (#7320)
Browse files Browse the repository at this point in the history
### Description

next/font supports to setup proxy.

Closes PACK-2449
  • Loading branch information
kwonoj committed Mar 1, 2024
1 parent 4fe9ca3 commit fcf6b03
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
27 changes: 25 additions & 2 deletions crates/turbo-tasks-fetch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,34 @@ impl HttpResponseBody {
}
}

#[turbo_tasks::value(shared)]
#[derive(Debug)]
pub enum ProxyConfig {
Http(String),
Https(String),
}

#[turbo_tasks::value(transparent)]
pub struct OptionProxyConfig(Option<ProxyConfig>);

#[turbo_tasks::function]
pub async fn fetch(url: Vc<String>, user_agent: Vc<Option<String>>) -> Result<Vc<FetchResult>> {
pub async fn fetch(
url: Vc<String>,
user_agent: Vc<Option<String>>,
proxy_option: Vc<OptionProxyConfig>,
) -> Result<Vc<FetchResult>> {
let url = &*url.await?;
let user_agent = &*user_agent.await?;
let client = reqwest::Client::new();
let proxy_option = &*proxy_option.await?;

let client_builder = reqwest::Client::builder();
let client_builder = match proxy_option {
Some(ProxyConfig::Http(proxy)) => client_builder.proxy(reqwest::Proxy::http(proxy)?),
Some(ProxyConfig::Https(proxy)) => client_builder.proxy(reqwest::Proxy::https(proxy)?),
_ => client_builder,
};

let client = client_builder.build()?;

let mut builder = client.get(url);
if let Some(user_agent) = user_agent {
Expand Down
13 changes: 7 additions & 6 deletions crates/turbo-tasks-fetch/tests/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn basic_get() {
});


let result = &*fetch(Vc::cell(server.url("/foo.woff")), Vc::cell(None)).await?;
let result = &*fetch(Vc::cell(server.url("/foo.woff")), Vc::cell(None), Vc::cell(None)).await?;
resource_mock.assert();

match result {
Expand All @@ -47,7 +47,7 @@ async fn sends_user_agent() {
.body("responsebody");
});

let result = &*fetch(Vc::cell(server.url("/foo.woff")), Vc::cell(Some("foo".to_owned()))).await?;
let result = &*fetch(Vc::cell(server.url("/foo.woff")), Vc::cell(Some("foo".to_owned())), Vc::cell(None)).await?;
resource_mock.assert();

let Ok(response) = result else {
Expand Down Expand Up @@ -76,7 +76,8 @@ async fn invalidation_does_not_invalidate() {

let url = Vc::cell(server.url("/foo.woff"));
let user_agent = Vc::cell(Some("foo".to_owned()));
let result = &*fetch(url, user_agent).await?;
let proxy = Vc::cell(None);
let result = &*fetch(url, user_agent, proxy).await?;
resource_mock.assert();

let Ok(response_vc) = result else {
Expand All @@ -86,7 +87,7 @@ async fn invalidation_does_not_invalidate() {
assert_eq!(response.status, 200);
assert_eq!(*response.body.to_string().await?, "responsebody");

let second_result = &*fetch(url, user_agent).await?;
let second_result = &*fetch(url, user_agent, proxy).await?;
let Ok(second_response_vc) = second_result else {
panic!()
};
Expand All @@ -104,7 +105,7 @@ async fn errors_on_failed_connection() {
register();

let url = "https://doesnotexist/foo.woff";
let result = &*fetch(Vc::cell(url.to_owned()), Vc::cell(None)).await?;
let result = &*fetch(Vc::cell(url.to_owned()), Vc::cell(None), Vc::cell(None)).await?;
let Err(err_vc) = result else {
panic!()
};
Expand All @@ -125,7 +126,7 @@ async fn errors_on_404() {

let server = httpmock::MockServer::start();
let resource_url = server.url("/");
let result = &*fetch(Vc::cell(resource_url.clone()), Vc::cell(None)).await?;
let result = &*fetch(Vc::cell(resource_url.clone()), Vc::cell(None), Vc::cell(None)).await?;
let Err(err_vc) = result else {
panic!()
};
Expand Down

0 comments on commit fcf6b03

Please sign in to comment.