Skip to content

Commit c73fbe2

Browse files
committed
NODE_BUILD_MIRROR_CMD generate's mirror URL
Not all mirrors will use the identical scheme of matching the nodejs.org url structure with `/dist` omitted. Now users may specify `NODE_BUILD_MIRROR_CMD` to be the name of a command or function which constructs the full mirrored package URL when given the original package URL and checksum as arguments. If omitted, the default scheme is used wherein NODE_BUILD_MIRROR_URL is substituted for `https://nodejs.org/dist/` The presence of *either* NODE_BUILD_MIRROR_URL or NODE_BUILD_MIRROR_CMD is sufficient to trigger a mirror attempt.
1 parent df193f3 commit c73fbe2

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ You can set certain environment variables to control the build process.
132132
built. By default, this is a subdirectory of `TMPDIR`.
133133
* `NODE_BUILD_CACHE_PATH`, if set, specifies a directory to use for caching
134134
downloaded package files.
135+
* `NODE_BUILD_MIRROR_CMD` provide a command to construct the package mirror
136+
URL.
135137
* `NODE_BUILD_MIRROR_URL` select a mirror from which to download packages
136138
instead of their original source URLs.
137139
* `NODE_BUILD_SKIP_MIRROR`, if set, forces node-build to download packages from
@@ -182,8 +184,11 @@ definition. (All bundled definitions include checksums.)
182184

183185
You can point node-build to another mirror by specifying the
184186
`NODE_BUILD_MIRROR_URL` environment variable--useful if you'd like to run your
185-
own local mirror, for example. Package mirror URLs are constructed by joining
186-
this variable with the SHA2 checksum of the package file.
187+
own local mirror, for example. Package mirror URLs are constructed by invoking
188+
`NODE_BUILD_MIRROR_CMD` with two arguments: `package_url` and `checksum`. The
189+
provided command should print the desired mirror's package URL. If
190+
`NODE_BUILD_MIRROR_CMD` is unset, package mirror URL construction defaults to
191+
simply replacing `https://nodejs.org/dist` with `NODE_BUILD_MIRROR_URL`.
187192

188193
If you don't have an SHA2 program installed, node-build will skip the download
189194
mirror and use official URLs instead. You can force node-build to bypass the

bin/node-build

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ matches_platform() {
202202
[ "${platform[2]}-${platform[0]}" = "$match" ]
203203
}
204204

205+
mirror() {
206+
local package_url="$1"
207+
local checksum="$2"
208+
209+
echo "${package_url/https:\/\/nodejs.org\/dist/$NODE_BUILD_MIRROR_URL}"
210+
}
211+
205212
try_binary(){
206213
[ -n "$BINARY_URL" ] && [ -z "$SKIP_BINARY" ] && [ -z "$BINARY_ATTEMPT" ]
207214
}
@@ -443,8 +450,8 @@ fetch_tarball() {
443450
checksum="${package_url#*#}"
444451
package_url="${package_url%%#*}"
445452

446-
if [ -n "$NODE_BUILD_MIRROR_URL" ]; then
447-
mirror_url="${package_url/https:\/\/nodejs.org\/dist/$NODE_BUILD_MIRROR_URL}"
453+
if [ -z "$NODE_BUILD_SKIP_MIRROR" ]; then
454+
mirror_url="$("${NODE_BUILD_MIRROR_CMD:=mirror}" $package_url $checksum 2>&4)" || unset mirror_url
448455
fi
449456
fi
450457

@@ -466,9 +473,7 @@ fetch_tarball() {
466473
fi
467474

468475
if ! reuse_existing_tarball "$package_filename" "$checksum"; then
469-
local tarball_filename=$(basename $package_url)
470-
echo "Downloading ${tarball_filename}..." >&2
471-
http head "$mirror_url" &&
476+
echo "Downloading $(basename $package_url)..." >&2
472477
download_tarball "$mirror_url" "$package_filename" "$checksum" ||
473478
download_tarball "$package_url" "$package_filename" "$checksum"
474479
fi
@@ -905,14 +910,11 @@ WGET_OPTS="${NODE_BUILD_WGET_OPTS} ${IPV4+--inet4-only} ${IPV6+--inet6-only}"
905910

906911
NODE_BUILD_MIRROR_URL="${NODE_BUILD_MIRROR_URL%/}"
907912

908-
if ! has_checksum_support compute_sha2; then
913+
if ! has_checksum_support compute_sha2 ||
914+
{ [ -z "$NODE_BUILD_MIRROR_URL" ] && [ -z "$NODE_BUILD_MIRROR_CMD" ]; } then
909915
NODE_BUILD_SKIP_MIRROR=true
910916
fi
911917

912-
if [ -n "$NODE_BUILD_SKIP_MIRROR" ] || [ -z "$NODE_BUILD_MIRROR_URL" ]; then
913-
unset NODE_BUILD_MIRROR_URL
914-
fi
915-
916918
SEED="$(date "+%Y%m%d%H%M%S").$$"
917919
LOG_PATH="${TMP}/node-build.${SEED}.log"
918920
NODE_BIN="${PREFIX_PATH}/bin/node"

test/mirror.bats

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load test_helper
44
export NODE_BUILD_SKIP_MIRROR=
55
export NODE_BUILD_CACHE_PATH=
66
export NODE_BUILD_MIRROR_URL=http://mirror.example.com
7+
export NODE_BUILD_MIRROR_CMD=mirror_stub
78
export NODE_BUILD_CURL_OPTS=
89

910
setup() {
@@ -15,7 +16,6 @@ setup() {
1516
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
1617

1718
install_fixture definitions/without-checksum
18-
echo "$output" >&2
1919

2020
assert_success
2121
assert [ -x "${INSTALL_ROOT}/bin/package" ]
@@ -43,14 +43,15 @@ setup() {
4343
local mirror_url="${NODE_BUILD_MIRROR_URL}/$checksum"
4444

4545
stub shasum true "echo $checksum"
46-
stub curl "-*I* $mirror_url : true" \
47-
"-q -o * -*S* $mirror_url : cp $FIXTURE_ROOT/package-1.0.0.tar.gz \$3"
46+
stub curl "-q -o * -*S* $mirror_url : cp $FIXTURE_ROOT/package-1.0.0.tar.gz \$3"
47+
stub mirror_stub ": echo $mirror_url"
4848

4949
install_fixture definitions/with-checksum
5050

5151
assert_success
5252
assert [ -x "${INSTALL_ROOT}/bin/package" ]
5353

54+
unstub mirror_stub
5455
unstub curl
5556
unstub shasum
5657
}
@@ -61,14 +62,16 @@ setup() {
6162
local mirror_url="${NODE_BUILD_MIRROR_URL}/$checksum"
6263

6364
stub shasum true "echo $checksum"
64-
stub curl "-*I* $mirror_url : false" \
65-
"-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
65+
stub curl "-q -o * -*S* $mirror_url : false"\
66+
"-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
67+
stub mirror_stub ": echo $mirror_url"
6668

6769
install_fixture definitions/with-checksum
6870

6971
assert_success
7072
assert [ -x "${INSTALL_ROOT}/bin/package" ]
7173

74+
unstub mirror_stub
7275
unstub curl
7376
unstub shasum
7477
}
@@ -79,16 +82,16 @@ setup() {
7982
local mirror_url="${NODE_BUILD_MIRROR_URL}/$checksum"
8083

8184
stub shasum true "echo invalid" "echo $checksum"
82-
stub curl "-*I* $mirror_url : true" \
83-
"-q -o * -*S* $mirror_url : cp $FIXTURE_ROOT/package-1.0.0.tar.gz \$3" \
85+
stub curl "-q -o * -*S* $mirror_url : cp $FIXTURE_ROOT/package-1.0.0.tar.gz \$3" \
8486
"-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
87+
stub mirror_stub ": echo $mirror_url"
8588

8689
install_fixture definitions/with-checksum
87-
echo "$output" >&2
8890

8991
assert_success
9092
assert [ -x "${INSTALL_ROOT}/bin/package" ]
9193

94+
unstub mirror_stub
9295
unstub curl
9396
unstub shasum
9497
}

0 commit comments

Comments
 (0)