Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaslyang authored and nicholaslyang committed Jul 11, 2023
1 parent dfdf425 commit 58542df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
10 changes: 5 additions & 5 deletions crates/turborepo-api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub struct UserResponse {

pub struct PreflightResponse {
location: Url,
allow_auth: bool,
allow_authorization_header: bool,
}

pub struct APIClient {
Expand Down Expand Up @@ -272,11 +272,11 @@ impl APIClient {
token,
&request_url,
"PUT",
"Content-Type, x-artifact-duration, Authorization, User-Agent, x-artifact-tag",
"Authorization, Content-Type, User-Agent, x-artifact-duration, x-artifact-tag",
)
.await?;

allow_auth = preflight_response.allow_auth;
allow_auth = preflight_response.allow_authorization_header;
request_url = preflight_response.location.to_string();
}

Expand Down Expand Up @@ -346,7 +346,7 @@ impl APIClient {
.do_preflight(token, &request_url, "GET", "Authorization, User-Agent")
.await?;

allow_auth = preflight_response.allow_auth;
allow_auth = preflight_response.allow_authorization_header;
request_url = preflight_response.location.to_string();
};

Expand Down Expand Up @@ -401,7 +401,7 @@ impl APIClient {

Ok(PreflightResponse {
location,
allow_auth,
allow_authorization_header: allow_auth,
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/turborepo-cache/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ impl HttpCache {
}

fn get_duration_from_response(response: &Response) -> Result<u32, CacheError> {
if let Some(duration) = response.headers().get("x-artifact-duration") {
let duration = duration
if let Some(duration_value) = response.headers().get("x-artifact-duration") {
let duration = duration_value
.to_str()
.map_err(|_| CacheError::InvalidDuration(Backtrace::capture()))?;

Expand Down
35 changes: 23 additions & 12 deletions crates/turborepo-vercel-api-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ pub const EXPECTED_SSO_TEAM_ID: &str = "expected_sso_team_id";
pub const EXPECTED_SSO_TEAM_SLUG: &str = "expected_sso_team_slug";

pub async fn start_test_server(port: u16) -> Result<()> {
let durations = Arc::new(Mutex::new(HashMap::new()));
let durations2 = durations.clone();
let durations3 = durations.clone();
let tempdir = Arc::new(tempfile::tempdir()?);
let tempdir2 = tempdir.clone();
let get_durations_ref = Arc::new(Mutex::new(HashMap::new()));
let head_durations_ref = get_durations_ref.clone();
let put_durations_ref = get_durations_ref.clone();
let put_tempdir_ref = Arc::new(tempfile::tempdir()?);
let get_tempdir_ref = put_tempdir_ref.clone();

let app = Router::new()
.route(
"/v2/user",
Expand Down Expand Up @@ -99,7 +100,7 @@ pub async fn start_test_server(port: u16) -> Result<()> {
"/v8/artifacts/:hash",
put(
|Path(hash): Path<String>, headers: HeaderMap, mut body: BodyStream| async move {
let root_path = tempdir.path();
let root_path = put_tempdir_ref.path();
let file_path = root_path.join(&hash);
let mut file = OpenOptions::new()
.append(true)
Expand All @@ -110,10 +111,10 @@ pub async fn start_test_server(port: u16) -> Result<()> {
let duration = headers
.get("x-artifact-duration")
.and_then(|header_value| header_value.to_str().ok())
.and_then(|duration| duration.parse::<u64>().ok())
.unwrap_or(0);
.and_then(|duration| duration.parse::<u32>().ok())
.expect("x-artifact-duration header is missing");

let mut durations_map = durations.lock().await;
let mut durations_map = put_durations_ref.lock().await;
durations_map.insert(hash.clone(), duration);

while let Some(item) = body.next().await {
Expand All @@ -128,10 +129,15 @@ pub async fn start_test_server(port: u16) -> Result<()> {
.route(
"/v8/artifacts/:hash",
get(|Path(hash): Path<String>| async move {
let root_path = tempdir2.path();
let root_path = get_tempdir_ref.path();
let file_path = root_path.join(&hash);
let buffer = std::fs::read(file_path).unwrap();
let duration = durations2.lock().await.get(&hash).cloned().unwrap_or(0);
let duration = get_durations_ref
.lock()
.await
.get(&hash)
.cloned()
.unwrap_or(0);
let mut headers = HeaderMap::new();

headers.insert(
Expand All @@ -145,7 +151,12 @@ pub async fn start_test_server(port: u16) -> Result<()> {
.route(
"/v8/artifacts/:hash",
head(|Path(hash): Path<String>| async move {
let duration = durations3.lock().await.get(&hash).cloned().unwrap_or(0);
let duration = head_durations_ref
.lock()
.await
.get(&hash)
.cloned()
.unwrap_or(0);
let mut headers = HeaderMap::new();

headers.insert(
Expand Down

0 comments on commit 58542df

Please sign in to comment.