Skip to content

Commit

Permalink
refactor: flatten nested structure
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Mar 18, 2024
1 parent c83aca4 commit 79e69cd
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions crates/volta-core/src/tool/node/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,28 @@ fn read_cached_opt(url: &str) -> Fallible<Option<RawNodeIndex>> {
file: expiry_file.to_owned(),
})?;

if let Some(date) = &expiry {
let expiry_date = httpdate::parse_http_date(date)
.with_context(|| ErrorKind::ParseNodeIndexExpiryError)?;

let current_date = SystemTime::now();
if !expiry
.map(|ref date| httpdate::parse_http_date(date))
.transpose()
.with_context(|| ErrorKind::ParseNodeIndexExpiryError)?
.is_some_and(|expiry_date| SystemTime::now() < expiry_date)
{
return Ok(None);
};

if current_date < expiry_date {
let index_file = volta_home()?.node_index_file();
let cached =
read_file(index_file).with_context(|| ErrorKind::ReadNodeIndexCacheError {
file: index_file.to_owned(),
})?;
let index_file = volta_home()?.node_index_file();
let cached = read_file(index_file).with_context(|| ErrorKind::ReadNodeIndexCacheError {
file: index_file.to_owned(),
})?;

if let Some(content) = cached {
if let Some(json) = content.strip_prefix(url) {
return serde_json::de::from_str(json)
.with_context(|| ErrorKind::ParseNodeIndexCacheError);
}
}
}
}
let Some(json) = cached
.as_ref()
.and_then(|content| content.strip_prefix(url))
else {
return Ok(None);
};

Ok(None)
serde_json::de::from_str(json).with_context(|| ErrorKind::ParseNodeIndexCacheError)
}

/// Get the cache max-age of an HTTP response.
Expand Down

0 comments on commit 79e69cd

Please sign in to comment.