-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change to purely use remote sparse index
- Loading branch information
1 parent
012d3e9
commit 145bb50
Showing
10 changed files
with
203 additions
and
1,156 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,75 @@ | ||
pub(crate) fn open_crates_io_index() -> Result<tame_index::index::ComboIndex, crate::error::CliError> | ||
{ | ||
let index = tame_index::index::ComboIndexCache::new(tame_index::IndexLocation::new( | ||
tame_index::IndexUrl::crates_io(None, None, None)?, | ||
))?; | ||
|
||
let index = match index { | ||
tame_index::index::ComboIndexCache::Git(gi) => { | ||
tame_index::index::RemoteGitIndex::new(gi)?.into() | ||
} | ||
tame_index::index::ComboIndexCache::Sparse(si) => { | ||
tame_index::index::RemoteSparseIndex::new( | ||
si, | ||
tame_index::external::reqwest::blocking::Client::builder() | ||
.http2_prior_knowledge() | ||
.build() | ||
.map_err(tame_index::Error::from)?, | ||
) | ||
.into() | ||
pub struct CratesIoIndex { | ||
index: tame_index::SparseIndex, | ||
client: reqwest::blocking::Client, | ||
etags: Vec<(String, String)>, | ||
} | ||
|
||
impl CratesIoIndex { | ||
#[inline] | ||
pub(crate) fn open() -> Result<Self, crate::error::CliError> { | ||
let index = tame_index::SparseIndex::new(tame_index::IndexLocation::new( | ||
tame_index::IndexUrl::CratesIoSparse, | ||
))?; | ||
let client = reqwest::blocking::ClientBuilder::new() | ||
.http2_prior_knowledge() | ||
.build()?; | ||
|
||
Ok(Self { | ||
index, | ||
client, | ||
etags: Vec::new(), | ||
}) | ||
} | ||
|
||
/// Determines if the specified crate exists in the crates.io index | ||
pub(crate) fn has_krate(&mut self, name: &str) -> Result<bool, crate::error::CliError> { | ||
Ok(self.has_krate_version(name, "")?.unwrap_or(false)) | ||
} | ||
|
||
/// Determines if the specified crate version exists in the crates.io index | ||
pub(crate) fn has_krate_version( | ||
&mut self, | ||
name: &str, | ||
version: &str, | ||
) -> Result<Option<bool>, crate::error::CliError> { | ||
let etag = self | ||
.etags | ||
.iter() | ||
.find_map(|(krate, etag)| (krate == name).then_some(etag.as_str())) | ||
.unwrap_or(""); | ||
|
||
let krate_name = name.try_into()?; | ||
let req = self.index.make_remote_request(krate_name, Some(etag))?; | ||
let res = self.client.execute(req.try_into()?)?; | ||
|
||
// Grab the etag if it exists for future requests | ||
if let Some(etag) = res.headers().get(reqwest::header::ETAG) { | ||
if let Ok(etag) = etag.to_str() { | ||
if let Some(i) = self.etags.iter().position(|(krate, _)| krate == name) { | ||
self.etags[i].1 = etag.to_owned(); | ||
} else { | ||
self.etags.push((name.to_owned(), etag.to_owned())); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
Ok(index) | ||
let mut builder = tame_index::external::http::Response::builder() | ||
.status(res.status()) | ||
.version(res.version()); | ||
|
||
builder | ||
.headers_mut() | ||
.unwrap() | ||
.extend(res.headers().iter().map(|(k, v)| (k.clone(), v.clone()))); | ||
|
||
let body = res.bytes()?; | ||
let response = builder | ||
.body(body.to_vec()) | ||
.map_err(|e| tame_index::Error::from(tame_index::error::HttpError::from(e)))?; | ||
|
||
let index_krate = self | ||
.index | ||
.parse_remote_response(krate_name, response, false)?; | ||
Ok(index_krate.map(|ik| ik.versions.iter().any(|iv| iv.version == version))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters