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

chore: apply small refactoring #1715

Merged
merged 4 commits into from May 13, 2024
Merged

Conversation

tottoto
Copy link
Contributor

@tottoto tottoto commented Mar 18, 2024

Applies small refactoring.

Copy link
Contributor

@charlespierce charlespierce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I really appreciate cleanups like this! A couple small comments to make sure the behavior is correct.

Separately, is there a way we can confirm the value that we're writing is parseable by httpdate? While the current approach is convoluted, it is making sure to compare apples to apples by using the Expires struct directly for the comparison.

It actually looks like the Expires header provides an implementation of From<Expires> for SystemTime, so we might be able access the time directly, rather than needing to parse the string value.

Another important piece is we want to make sure any comparison is backwards-compatible with the existing expiry file.

@@ -18,7 +18,7 @@ pub enum ArchiveError {
HttpError(attohttpc::StatusCode),

#[error("HTTP header '{0}' not found")]
MissingHeaderError(String),
MissingHeaderError(attohttpc::header::HeaderName),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the Header::name associated function returns a &'static reference to the HeaderName object. Given that, we can probably use the same here (&'static HeaderName) instead of needing to convert it to an owned value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use &'static HeaderName.

Comment on lines +163 to +165
.is_some_and(|expiry_date| SystemTime::now() < expiry_date)
{
return Ok(None);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely possible I'm misreading, but I believe this might be inverting the condition. In the current code, when SystemTime::now() < expiry_date, then we read the cached file. However, in this one we're returning no cache with that same condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think the behavior would be the same as the code before this change as the negation of this condition is evaluated.

@tottoto
Copy link
Contributor Author

tottoto commented Mar 21, 2024

Separately, is there a way we can confirm the value that we're writing is parseable by httpdate? While the current approach is convoluted, it is making sure to compare apples to apples by using the Expires struct directly for the comparison.

As headers::Expires and httpdate both support HTTP-date format defined in rfc7231, they are compatible.

It actually looks like the Expires header provides an implementation of From for SystemTime, so we might be able access the time directly, rather than needing to parse the string value.

Yes. When using Expires, it is possible to evaluate like this SystemTime::now() < expiry_date.into().

Another important piece is we want to make sure any comparison is backwards-compatible with the existing expiry file.

Since headers crate actually uses httpdate to parse and format date and delegate the comparison to internal httpdate implementation, and the httpdate uses SystemTime for its comparison, it behaves exactly the same.

Copy link
Contributor

@charlespierce charlespierce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@charlespierce charlespierce merged commit ada4962 into volta-cli:main May 13, 2024
11 checks passed
@tottoto tottoto deleted the small-refactoring branch May 13, 2024 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants