Skip to content

Commit 6cbd518

Browse files
authored
Fix downloads for @yarnpkg/cli-dist (#647)
* Fix downloads for @yarnpkg/cli-dist The changes to make this download script work for both `npm` and `yarn` distributions [#612](#612) has a bug where the requested distribution (`yarn`) differs from its package name if the version is `>=2` (`@yarnpkg/cli-dist`) and that wasn't accounted for in the name of the download file. The `curl` command that downloads the tarball should have been saving to `yarn-v{x.y.z}.tar.gz` but was instead saving to `@yarnpkg/cli-dist-v{x.y.z}.tar.gz`. This is causing automation failures (e.g.; https://github.com/heroku/buildpacks-nodejs/actions/runs/6179740345/job/16775160336) This PR keeps the `distribution_name` and `package_name` separate so this mismatch will no longer happen. It also: - adds some extra logging which would have made the error more obvious - DRYS up the `npm_url`, ``tarball_url`, and `downloaded_tarball` which previously were being inlined into commands - adds retries and timeouts to `curl`
1 parent 9dd679d commit 6cbd518

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

common/bin/download-verify-npm-package

+14-10
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
set -o pipefail
88
set -e
99

10-
package_name=$1
11-
if [ "npm" != "${package_name}" ] && [ "yarn" != "${package_name}" ]; then
12-
echo "Unrecognized distribution - ${package_name}"
10+
distribution_name=$1
11+
if [ "npm" != "${distribution_name}" ] && [ "yarn" != "${distribution_name}" ]; then
12+
echo "Unrecognized distribution - ${distribution_name}"
1313
exit 1
1414
fi
1515

@@ -19,28 +19,32 @@ if [ -z "$package_version" ]; then
1919
exit 1
2020
fi
2121

22+
package_name="${distribution_name}"
2223
if [ "yarn" = "${package_name}" ]; then
2324
# Yarn 2+ (aka: "berry") is hosted under a different npm package.
2425
major_version=$(echo "$package_version" | cut -d "." -f 1)
2526
package_name=$([ "$major_version" -ge 2 ] && echo "@yarnpkg/cli-dist" || echo "yarn")
2627
fi
2728

2829
npm_url="https://registry.npmjs.com/${package_name}/${package_version}"
29-
3030
echo "Determining dist url from ${npm_url}" >&2
31-
url=$(curl -sSf "${npm_url}" | jq -r '.dist.tarball')
3231

33-
echo "Downloading ${package_name} tarball from ${url} ..." >&2
34-
curl -sSf -o "./${package_name}-v${package_version}.tar.gz" "${url}"
32+
tarball_url=$(curl --silent --show-error --fail --retry 5 --retry-all-errors --connect-timeout 10 --max-time 60 "${npm_url}" | jq -r '.dist.tarball')
33+
echo "Downloading ${package_name} tarball from ${tarball_url} ..." >&2
34+
35+
downloaded_tarball="./${distribution_name}-v${package_version}.tar.gz"
36+
echo "Saving as ${downloaded_tarball}" >&2
37+
38+
curl --silent --show-error --fail --retry 5 --retry-all-errors --connect-timeout 10 --max-time 60 --output "${downloaded_tarball}" "${tarball_url}"
3539

3640
# Check the file's sha against npm's published sha. This section assumes all
3741
# packages are published with sha512. That was true at the time of writing,
3842
# but if npmjs.org starts using additional checksum algorithms, this section
3943
# will need to be changed.
4044
echo "Checking ${package_name} tarball integrity..." >&2
41-
shasum=$(shasum -b -a 512 "${package_name}"-v"${package_version}".tar.gz | awk '{ print $1 }' | xxd -r -p | base64 | tr -d "\n")
45+
shasum=$(shasum -b -a 512 "${downloaded_tarball}" | awk '{ print $1 }' | xxd -r -p | base64 | tr -d "\n")
4246
actual_integrity="sha512-${shasum}"
43-
published_integrity=$(curl -sSf "https://registry.npmjs.com/${package_name}/${package_version}" | jq -r '.dist.integrity')
47+
published_integrity=$(curl --silent --show-error --fail --retry 5 --retry-all-errors --connect-timeout 10 --max-time 60 "${npm_url}" | jq -r '.dist.integrity')
4448
if [ "$actual_integrity" != "$published_integrity" ]; then
4549
echo "Couldn't verify package integrity. Expected '$published_integrity', got '$actual_integrity'." >&2
4650
exit 1
@@ -56,7 +60,7 @@ npm_pubkey="MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1Olb3zMAFFxXKHiIkQO5cJ3Yhl5i6UPp
5660
printf -- '-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----\n' "$npm_pubkey" >npm-pubkey.pem
5761

5862
# Fetch the signature from the published package data
59-
curl -sSf "https://registry.npmjs.com/${package_name}/${package_version}" | jq -r '.dist.signatures[0].sig' | base64 -d >npm-signature.bin
63+
curl --silent --show-error --fail --retry 5 --retry-all-errors --connect-timeout 10 --max-time 60 "${npm_url}" | jq -r '.dist.signatures[0].sig' | base64 -d >npm-signature.bin
6064

6165
# Build the signing message
6266
echo -n "${package_name}@${package_version}:${published_integrity}" >message.txt

0 commit comments

Comments
 (0)