From b8e49176469e90c93d6d202839edc403b8721731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Bergstr=C3=B6m?= Date: Wed, 9 Sep 2015 12:15:38 +1000 Subject: [PATCH] Use `xz` tarballs if available Saves us ~25% bandwidth while downloading the payload. This only applies to hosts that has the `xz` binary and attempts to use iojs 2.3.2 or newer (this includes nodejs 4.0+ as well). Older targets are unaffected. --- nvm.sh | 43 +++++++++++++++++++++------- test/fast/Unit tests/nvm_supports_xz | 36 +++++++++++++++++++++++ test/fast/teardown | 2 +- 3 files changed, 69 insertions(+), 12 deletions(-) create mode 100755 test/fast/Unit tests/nvm_supports_xz diff --git a/nvm.sh b/nvm.sh index 4db7319..d49acdb 100755 --- a/nvm.sh +++ b/nvm.sh @@ -999,16 +999,24 @@ nvm_install_merged_node_binary() { local url local sum local NODE_PREFIX + local compression + compression="gz" + local tar_compression_flag + tar_compression_flag="x" + if nvm_supports_xz "$VERSION"; then + compression="xz" + tar_compression_flag="J" + fi NODE_PREFIX="$(nvm_node_prefix)" if [ -n "$NVM_OS" ]; then t="$VERSION-$NVM_OS-$(nvm_get_arch)" - url="$MIRROR/$VERSION/$NODE_PREFIX-${t}.tar.gz" - sum="$(nvm_download -L -s $MIRROR/$VERSION/SHASUMS256.txt -o - | command grep $NODE_PREFIX-${t}.tar.gz | command awk '{print $1}')" + url="$MIRROR/$VERSION/$NODE_PREFIX-${t}.tar.${compression}" + sum="$(nvm_download -L -s $MIRROR/$VERSION/SHASUMS256.txt -o - | command grep $NODE_PREFIX-${t}.tar.${compression} | command awk '{print $1}')" local tmpdir tmpdir="$NVM_DIR/bin/node-${t}" local tmptarball - tmptarball="$tmpdir/node-${t}.tar.gz" + tmptarball="$tmpdir/node-${t}.tar.${compression}" local NVM_INSTALL_ERRORED command mkdir -p "$tmpdir" && \ echo "Downloading $url..." && \ @@ -1022,7 +1030,7 @@ nvm_install_merged_node_binary() { [ "$NVM_INSTALL_ERRORED" != true ] && \ echo "WARNING: checksums are currently disabled for node.js v4.0 and later" >&2 && \ # nvm_checksum "$tmptarball" $sum && \ - command tar -xzf "$tmptarball" -C "$tmpdir" --strip-components 1 && \ + command tar -x${tar_compression_flag}f "$tmptarball" -C "$tmpdir" --strip-components 1 && \ command rm -f "$tmptarball" && \ command mkdir -p "$VERSION_PATH" && \ command mv "$tmpdir"/* "$VERSION_PATH" @@ -1066,16 +1074,24 @@ nvm_install_iojs_binary() { local t local url local sum + local compression + compression="gz" + local tar_compression_flag + tar_compression_flag="x" + if nvm_supports_xz "$VERSION"; then + compression="xz" + tar_compression_flag="J" + fi if [ -n "$NVM_OS" ]; then if nvm_binary_available "$VERSION"; then t="$VERSION-$NVM_OS-$(nvm_get_arch)" - url="$MIRROR/$VERSION/$(nvm_iojs_prefix)-${t}.tar.gz" - sum="$(nvm_download -L -s $MIRROR/$VERSION/SHASUMS256.txt -o - | command grep $(nvm_iojs_prefix)-${t}.tar.gz | command awk '{print $1}')" + url="$MIRROR/$VERSION/$(nvm_iojs_prefix)-${t}.tar.${compression}" + sum="$(nvm_download -L -s $MIRROR/$VERSION/SHASUMS256.txt -o - | command grep $(nvm_iojs_prefix)-${t}.tar.${compression} | command awk '{print $1}')" local tmpdir tmpdir="$NVM_DIR/bin/iojs-${t}" local tmptarball - tmptarball="$tmpdir/iojs-${t}.tar.gz" + tmptarball="$tmpdir/iojs-${t}.tar.${compression}" local NVM_INSTALL_ERRORED command mkdir -p "$tmpdir" && \ echo "Downloading $url..." && \ @@ -1089,7 +1105,7 @@ nvm_install_iojs_binary() { [ "$NVM_INSTALL_ERRORED" != true ] && \ echo "WARNING: checksums are currently disabled for io.js" >&2 && \ # nvm_checksum "$tmptarball" $sum && \ - command tar -xzf "$tmptarball" -C "$tmpdir" --strip-components 1 && \ + command tar -x${tar_compression_flag}f "$tmptarball" -C "$tmpdir" --strip-components 1 && \ command rm -f "$tmptarball" && \ command mkdir -p "$VERSION_PATH" && \ command mv "$tmpdir"/* "$VERSION_PATH" @@ -1662,9 +1678,9 @@ nvm() { fi # Delete all files related to target version. command rm -rf "$NVM_DIR/src/$NVM_PREFIX-$VERSION" \ - "$NVM_DIR/src/$NVM_PREFIX-$VERSION.tar.gz" \ + "$NVM_DIR/src/$NVM_PREFIX-$VERSION.tar.*" \ "$NVM_DIR/bin/$NVM_PREFIX-${t}" \ - "$NVM_DIR/bin/$NVM_PREFIX-${t}.tar.gz" \ + "$NVM_DIR/bin/$NVM_PREFIX-${t}.tar.*" \ "$VERSION_PATH" 2>/dev/null echo "$NVM_SUCCESS_MSG" @@ -2200,7 +2216,7 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | command grep -v "N/A" | command sed '/^$/d' nvm_print_npm_version nvm_npm_global_modules \ nvm_has_system_node nvm_has_system_iojs \ nvm_download nvm_get_latest nvm_has nvm_get_latest \ - nvm_supports_source_options > /dev/null 2>&1 + nvm_supports_source_options nvm_supports_xz > /dev/null 2>&1 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1 ;; * ) @@ -2214,6 +2230,11 @@ nvm_supports_source_options() { [ "_$(echo 'echo $1' | . /dev/stdin yes 2> /dev/null)" = "_yes" ] } +nvm_supports_xz() { + command which xz 2>&1 >/dev/null && \ + nvm_version_greater_than_or_equal_to "$1" "2.3.2" +} + NVM_VERSION="$(nvm_alias default 2>/dev/null || echo)" if nvm_supports_source_options && [ "_$1" = "_--install" ]; then if [ -n "$NVM_VERSION" ]; then diff --git a/test/fast/Unit tests/nvm_supports_xz b/test/fast/Unit tests/nvm_supports_xz new file mode 100755 index 0000000..c7e5a36 --- /dev/null +++ b/test/fast/Unit tests/nvm_supports_xz @@ -0,0 +1,36 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +OLDPATH=$PATH +TEST_PATH=../../xz-test + +mkdir $TEST_PATH +touch ../../xz-test/xz +chmod +x ../../xz-test/xz + +export PATH=$TEST_PATH:$PATH + +$(nvm_supports_xz "v2.3.2") || \ + die "expected 'nvm_supports_xz v2.3.2' to exit with 0" + +$(nvm_supports_xz "v0.12.7") && \ + die "expected 'nvm_supports_xz v0.12.7' to exit with 1" + + +# set up for a failure by having a minimal toolset available +# but remove xz +ln -s /usr/bin/which $TEST_PATH/which +ln -s /usr/bin/command $TEST_PATH/command +ln -s /usr/bin/awk $TEST_PATH/awk +ln -s $(which rm) $TEST_PATH/rm + +export PATH=$TEST_PATH +rm $TEST_PATH/xz + +$(nvm_supports_xz "v2.3.2") && \ + die "expected 'nvm_supports_xz v2.3.2' with a missing xz binary to exit with 1" + +export PATH=$OLDPATH diff --git a/test/fast/teardown b/test/fast/teardown index bd269eb..a1c73df 100755 --- a/test/fast/teardown +++ b/test/fast/teardown @@ -6,5 +6,5 @@ type setopt >/dev/null 2>&1 && setopt NULL_GLOB type shopt >/dev/null 2>&1 && shopt -s nullglob - rm -fR v* src alias + rm -fR v* src alias test/test-xz )