From babc6bdef29925306915df536821121a22a4aec1 Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Mon, 22 Jul 2024 16:06:23 +0800 Subject: [PATCH] Local cache metadata --- crates/snm_atom/Cargo.toml | 1 + crates/snm_atom/src/package_manager_atom.rs | 25 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/snm_atom/Cargo.toml b/crates/snm_atom/Cargo.toml index d942dff7..5a39fbeb 100644 --- a/crates/snm_atom/Cargo.toml +++ b/crates/snm_atom/Cargo.toml @@ -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"] } diff --git a/crates/snm_atom/src/package_manager_atom.rs b/crates/snm_atom/src/package_manager_atom.rs index d8cb97ba..eea2f502 100644 --- a/crates/snm_atom/src/package_manager_atom.rs +++ b/crates/snm_atom/src/package_manager_atom.rs @@ -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; @@ -117,12 +118,32 @@ impl AtomTrait for PackageManagerAtom { v: &'a str, ) -> Pin, 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"))