Skip to content

Commit

Permalink
fix check hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 22, 2024
1 parent bc28161 commit 2ffc4cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
12 changes: 7 additions & 5 deletions crates/snm_atom/src/package_manager_atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,17 @@ impl AtomTrait for PackageManagerAtom {
) -> Pin<Box<dyn Future<Output = Result<Option<String>, SnmError>> + Send + 'a>> {
Box::pin(async move {
let npm_registry = self.snm_config.get_npm_registry();
let download_url = format!("{}/{}/{}", npm_registry, &self.library_name, &v);
let download_url = format!("{}/{}", npm_registry, &self.library_name);

let value: Value = reqwest::get(&download_url).await?.json().await?;

let shasum = value
.get("dist")
.and_then(|dist| dist.get("shasum"))
.and_then(|shasum| shasum.as_str())
.map(|shasum| shasum.to_string());
.get("versions")
.and_then(|item| item.get(v))
.and_then(|item| item.get("dist"))
.and_then(|item| item.get("shasum"))
.and_then(|item| item.as_str())
.map(|item| item.to_string());

Ok(shasum)
})
Expand Down
48 changes: 25 additions & 23 deletions crates/snm_shim/src/ensure_binary_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,54 @@ use snm_atom::atom::AtomTrait;
use snm_download_builder::{DownloadBuilder, WriteStrategy};
use snm_utils::snm_error::SnmError;

pub async fn ensure_binary_path<T>(manage: &T, version: &String) -> Result<String, SnmError>
pub async fn ensure_binary_path<T>(atom: &T, version: &String) -> Result<String, SnmError>
where
T: AtomTrait,
{
if manage
if atom
.get_anchor_file_path_buf(version.as_str())?
.exists()
.not()
{
if manage.get_snm_config().get_strict() {
if atom.get_snm_config().get_strict() {
return Err(SnmError::UnsupportedNodeVersionError {
version: version.to_string(),
});
} else {
download(version.as_str(), manage).await?;
let download_url = atom.get_download_url(version);

let downloaded_file_path_buf = atom.get_downloaded_file_path_buf(version)?;

DownloadBuilder::new()
.retries(3)
.timeout(atom.get_snm_config().get_download_timeout_secs())
.write_strategy(WriteStrategy::WriteAfterDelete)
.download(&download_url, &downloaded_file_path_buf)
.await?;

let runtime_dir_path_buf = atom.get_runtime_dir_path_buf(version)?;

check(version, atom).await?;

atom.decompress_download_file(&downloaded_file_path_buf, &runtime_dir_path_buf)?;

if let Some(parent) = downloaded_file_path_buf.parent() {
fs::remove_dir_all(parent)?;
}
}
}

let binary = manage.get_runtime_binary_dir_string(version.as_str())?;
let binary = atom.get_runtime_binary_dir_string(version.as_str())?;

return Ok(binary);
}

pub async fn download<T>(version: &str, atom: &T) -> Result<(), SnmError>
async fn check<T>(version: &str, atom: &T) -> Result<(), SnmError>
where
T: AtomTrait,
{
let download_url = atom.get_download_url(version);

let downloaded_file_path_buf = atom.get_downloaded_file_path_buf(version)?;

DownloadBuilder::new()
.retries(3)
.timeout(atom.get_snm_config().get_download_timeout_secs())
.write_strategy(WriteStrategy::WriteAfterDelete)
.download(&download_url, &downloaded_file_path_buf)
.await?;

let runtime_dir_path_buf = atom.get_runtime_dir_path_buf(version)?;

let expect = atom.get_expect_shasum(version).await?;

let actual = atom.get_actual_shasum(&downloaded_file_path_buf).await?;
Expand All @@ -66,11 +74,5 @@ where
});
}

atom.decompress_download_file(&downloaded_file_path_buf, &runtime_dir_path_buf)?;

if let Some(parent) = downloaded_file_path_buf.parent() {
fs::remove_dir_all(parent)?;
}

Ok(())
}

0 comments on commit 2ffc4cc

Please sign in to comment.