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

fix: shasum is stored as hex #34

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::sync::Arc;
Expand Down Expand Up @@ -239,6 +238,19 @@ impl NpmPackageVersionInfo {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NpmPackageVersionDistInfoIntegrity<'a> {
/// A string in the form `sha1-<hash>` where the hash is base64 encoded.
Integrity {
algorithm: &'a str,
base64_hash: &'a str,
},
/// The integrity could not be determined because it did not contain a dash.
UnknownIntegrity(&'a str),
/// The legacy sha1 hex hash (ex. "62afbee2ffab5e0db139450767a6125cbea50fa2").
LegacySha1Hex(&'a str),
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NpmPackageVersionDistInfo {
/// URL to the tarball.
Expand All @@ -248,12 +260,21 @@ pub struct NpmPackageVersionDistInfo {
}

impl NpmPackageVersionDistInfo {
pub fn integrity(&self) -> Cow<String> {
self
.integrity
.as_ref()
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(format!("sha1-{}", self.shasum)))
pub fn integrity(&self) -> NpmPackageVersionDistInfoIntegrity {
match &self.integrity {
Some(integrity) => match integrity.split_once('-') {
Some((algorithm, base64_hash)) => {
NpmPackageVersionDistInfoIntegrity::Integrity {
algorithm,
base64_hash,
}
}
None => NpmPackageVersionDistInfoIntegrity::UnknownIntegrity(
integrity.as_str(),
),
},
None => NpmPackageVersionDistInfoIntegrity::LegacySha1Hex(&self.shasum),
}
}
}

Expand Down Expand Up @@ -491,4 +512,35 @@ mod test {
])))
);
}

#[test]
fn itegrity() {
// integrity
let text =
r#"{ "tarball": "", "integrity": "sha512-testing", "shasum": "here" }"#;
let info: NpmPackageVersionDistInfo = serde_json::from_str(text).unwrap();
assert_eq!(
info.integrity(),
super::NpmPackageVersionDistInfoIntegrity::Integrity {
algorithm: "sha512",
base64_hash: "testing"
}
);

// no integrity
let text = r#"{ "tarball": "", "shasum": "here" }"#;
let info: NpmPackageVersionDistInfo = serde_json::from_str(text).unwrap();
assert_eq!(
info.integrity(),
super::NpmPackageVersionDistInfoIntegrity::LegacySha1Hex("here")
);

// no dash
let text = r#"{ "tarball": "", "integrity": "test", "shasum": "here" }"#;
let info: NpmPackageVersionDistInfo = serde_json::from_str(text).unwrap();
assert_eq!(
info.integrity(),
super::NpmPackageVersionDistInfoIntegrity::UnknownIntegrity("test")
);
}
}