From 9b8992d4b4acb3a54ca7d988d181a266841013d9 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 22 Feb 2023 13:16:40 -0500 Subject: [PATCH] feat: start caching npm package version's "bin" entry from npm registry (#17881) --- cli/npm/registry.rs | 67 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/cli/npm/registry.rs b/cli/npm/registry.rs index 510f581322..bcdada30dc 100644 --- a/cli/npm/registry.rs +++ b/cli/npm/registry.rs @@ -89,17 +89,25 @@ impl Ord for NpmDependencyEntry { } } -#[derive(Debug, Default, Deserialize, Serialize, Clone)] +#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct NpmPeerDependencyMeta { #[serde(default)] optional: bool, } -#[derive(Debug, Default, Deserialize, Serialize, Clone)] +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] +#[serde(untagged)] +pub enum NpmPackageVersionBinEntry { + String(String), + Map(HashMap), +} + +#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct NpmPackageVersionInfo { pub version: String, pub dist: NpmPackageVersionDistInfo, + pub bin: Option, // Bare specifier to version (ex. `"typescript": "^3.0.1") or possibly // package and version (ex. `"typescript-3.0.1": "npm:typescript@3.0.1"`). #[serde(default)] @@ -639,3 +647,58 @@ impl NpmRegistryApiInner for TestNpmRegistryApiInner { NpmRegistryApi::default_url() } } + +#[cfg(test)] +mod test { + use std::collections::HashMap; + + use deno_core::serde_json; + + use crate::npm::registry::NpmPackageVersionBinEntry; + use crate::npm::NpmPackageVersionDistInfo; + + use super::NpmPackageVersionInfo; + + #[test] + fn deserializes_minimal_pkg_info() { + let text = r#"{ "version": "1.0.0", "dist": { "tarball": "value", "shasum": "test" } }"#; + let info: NpmPackageVersionInfo = serde_json::from_str(text).unwrap(); + assert_eq!( + info, + NpmPackageVersionInfo { + version: "1.0.0".to_string(), + dist: NpmPackageVersionDistInfo { + tarball: "value".to_string(), + shasum: "test".to_string(), + integrity: None, + }, + bin: None, + dependencies: Default::default(), + peer_dependencies: Default::default(), + peer_dependencies_meta: Default::default() + } + ); + } + + #[test] + fn deserializes_bin_entry() { + // string + let text = r#"{ "version": "1.0.0", "bin": "bin-value", "dist": { "tarball": "value", "shasum": "test" } }"#; + let info: NpmPackageVersionInfo = serde_json::from_str(text).unwrap(); + assert_eq!( + info.bin, + Some(NpmPackageVersionBinEntry::String("bin-value".to_string())) + ); + + // map + let text = r#"{ "version": "1.0.0", "bin": { "a": "a-value", "b": "b-value" }, "dist": { "tarball": "value", "shasum": "test" } }"#; + let info: NpmPackageVersionInfo = serde_json::from_str(text).unwrap(); + assert_eq!( + info.bin, + Some(NpmPackageVersionBinEntry::Map(HashMap::from([ + ("a".to_string(), "a-value".to_string()), + ("b".to_string(), "b-value".to_string()), + ]))) + ); + } +}