From 02dd7c7bb36cfd930eaf7124e560013074ccf01e Mon Sep 17 00:00:00 2001 From: Reinis Muiznieks Date: Thu, 16 Nov 2023 17:52:51 +0200 Subject: [PATCH] prefetch-npm-deps: add support for npm alias schema in version spec --- .../node/fetch-npm-deps/src/main.rs | 4 +- .../node/fetch-npm-deps/src/parse/lock.rs | 77 ++++++++++++++----- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/pkgs/build-support/node/fetch-npm-deps/src/main.rs b/pkgs/build-support/node/fetch-npm-deps/src/main.rs index 9d86bd8091a7..c3d817ebe8c9 100644 --- a/pkgs/build-support/node/fetch-npm-deps/src/main.rs +++ b/pkgs/build-support/node/fetch-npm-deps/src/main.rs @@ -241,7 +241,9 @@ fn main() -> anyhow::Result<()> { packages.into_par_iter().try_for_each(|package| { eprintln!("{}", package.name); - let tarball = package.tarball()?; + let tarball = package + .tarball() + .map_err(|e| anyhow!("couldn't fetch {} at {}: {e:?}", package.name, package.url))?; let integrity = package.integrity().map(ToString::to_string); cache diff --git a/pkgs/build-support/node/fetch-npm-deps/src/parse/lock.rs b/pkgs/build-support/node/fetch-npm-deps/src/parse/lock.rs index f50a31651d0e..77839e67aaec 100644 --- a/pkgs/build-support/node/fetch-npm-deps/src/parse/lock.rs +++ b/pkgs/build-support/node/fetch-npm-deps/src/parse/lock.rs @@ -216,29 +216,35 @@ fn to_new_packages( } if let UrlOrString::Url(v) = &package.version { - for (scheme, host) in [ - ("github", "github.com"), - ("bitbucket", "bitbucket.org"), - ("gitlab", "gitlab.com"), - ] { - if v.scheme() == scheme { - package.version = { - let mut new_url = initial_url.clone(); + if v.scheme() == "npm" { + if let Some(UrlOrString::Url(ref url)) = &package.resolved { + package.version = UrlOrString::Url(url.clone()); + } + } else { + for (scheme, host) in [ + ("github", "github.com"), + ("bitbucket", "bitbucket.org"), + ("gitlab", "gitlab.com"), + ] { + if v.scheme() == scheme { + package.version = { + let mut new_url = initial_url.clone(); - new_url.set_host(Some(host))?; + new_url.set_host(Some(host))?; - if v.path().ends_with(".git") { - new_url.set_path(v.path()); - } else { - new_url.set_path(&format!("{}.git", v.path())); - } + if v.path().ends_with(".git") { + new_url.set_path(v.path()); + } else { + new_url.set_path(&format!("{}.git", v.path())); + } - new_url.set_fragment(v.fragment()); + new_url.set_fragment(v.fragment()); - UrlOrString::Url(new_url) - }; + UrlOrString::Url(new_url) + }; - break; + break; + } } } } @@ -268,7 +274,8 @@ fn get_initial_url() -> anyhow::Result { #[cfg(test)] mod tests { use super::{ - get_initial_url, to_new_packages, Hash, HashCollection, OldPackage, Package, UrlOrString, + get_initial_url, packages, to_new_packages, Hash, HashCollection, OldPackage, Package, + UrlOrString, }; use std::{ cmp::Ordering, @@ -330,4 +337,36 @@ mod tests { Some(Hash(String::from("sha512-foo"))) ); } + + #[test] + fn parse_lockfile_correctly() { + let packages = packages( + r#"{ + "name": "node-ddr", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "string-width-cjs": { + "version": "npm:string-width@4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "requires": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + } + } + } + }"#).unwrap(); + + assert_eq!(packages.len(), 1); + assert_eq!( + packages[0].resolved, + Some(UrlOrString::Url( + Url::parse("https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz") + .unwrap() + )) + ); + } }