Skip to content

Commit

Permalink
Local cache metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 22, 2024
1 parent 385f20d commit babc6bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/snm_atom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ snm_utils = { path = "../snm_utils" }
snm_config = { path = "../snm_config" }
snm_tarball = { path = "../snm_tarball" }
snm_package_json = { path = "../snm_package_json" }
snm_download_builder ={ path = "../snm_download_builder" }
reqwest = { version = "0.12.3", features = ["json", "blocking", "stream","native-tls-vendored"] }
25 changes: 23 additions & 2 deletions crates/snm_atom/src/package_manager_atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde_json::Value;
use sha1::Digest;
use sha1::Sha1;
use snm_config::SnmConfig;
use snm_download_builder::{DownloadBuilder, WriteStrategy};
use snm_package_json::parse_package_json;
use snm_tarball::decompress;
use snm_utils::snm_error::SnmError;
Expand Down Expand Up @@ -117,12 +118,32 @@ impl AtomTrait for PackageManagerAtom {
v: &'a str,
) -> Pin<Box<dyn Future<Output = Result<Option<String>, SnmError>> + Send + 'a>> {
Box::pin(async move {
let cache_dir = self.snm_config.get_cache_dir()?;

if cache_dir.exists().not() {
fs::create_dir_all(&cache_dir)?;
}

let cache_file = cache_dir.join(format!("{}.json", &self.library_name));

let npm_registry = self.snm_config.get_npm_registry();

let download_url = format!("{}/{}", npm_registry, &self.library_name);

let value: Value = reqwest::get(&download_url).await?.json().await?;
if cache_file.exists().not() {
DownloadBuilder::new()
.retries(3)
.timeout(self.snm_config.get_download_timeout_secs())
.write_strategy(WriteStrategy::WriteAfterDelete)
.download(&download_url, &cache_file)
.await?;
}

let file = File::open(&cache_file)?;
let reader = BufReader::new(&file);
let raw = serde_json::from_reader::<_, Value>(reader)?;

let shasum = value
let shasum = raw
.get("versions")
.and_then(|item| item.get(v))
.and_then(|item| item.get("dist"))
Expand Down

0 comments on commit babc6bd

Please sign in to comment.