From 973dfc6d4a538ed925a6fd1bb8822ff150852663 Mon Sep 17 00:00:00 2001 From: Peter Dave Hello Date: Thu, 23 Mar 2017 12:32:36 +0800 Subject: [PATCH] [New] Dynamically detect if curl supports `--compressed` --- nvm.sh | 17 ++++++++-- test/fast/Unit tests/nvm_curl_libz_support | 39 ++++++++++++++++++++++ test/slow/nvm_get_latest/nvm_get_latest | 7 +++- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100755 test/fast/Unit tests/nvm_curl_libz_support diff --git a/nvm.sh b/nvm.sh index 67ef6df..dda7d6e 100644 --- a/nvm.sh +++ b/nvm.sh @@ -57,10 +57,18 @@ nvm_has_colors() { [ "${NVM_COLORS:--1}" -ge 8 ] } +nvm_curl_libz_support() { + curl -V | grep "^Features:" | grep -q "libz" +} + nvm_get_latest() { local NVM_LATEST_URL + local CURL_COMPRESSED_FLAG + if nvm_curl_libz_support; then + CURL_COMPRESSED_FLAG="--compressed" + fi if nvm_has "curl"; then - NVM_LATEST_URL="$(curl --compressed -q -w "%{url_effective}\n" -L -s -S http://latest.nvm.sh -o /dev/null)" + NVM_LATEST_URL="$(curl "${CURL_COMPRESSED_FLAG:-}" -q -w "%{url_effective}\n" -L -s -S http://latest.nvm.sh -o /dev/null)" elif nvm_has "wget"; then NVM_LATEST_URL="$(wget http://latest.nvm.sh --server-response -O /dev/null 2>&1 | command awk '/^ Location: /{DEST=$2} END{ print DEST }')" else @@ -75,8 +83,12 @@ nvm_get_latest() { } nvm_download() { + local CURL_COMPRESSED_FLAG + if nvm_curl_libz_support; then + CURL_COMPRESSED_FLAG="--compressed" + fi if nvm_has "curl"; then - curl --compressed -q "$@" + curl "${CURL_COMPRESSED_FLAG:-}" -q "$@" elif nvm_has "wget"; then # Emulate curl with wget ARGS=$(nvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \ @@ -3192,6 +3204,7 @@ nvm() { nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \ nvm_sanitize_path nvm_has_colors nvm_process_parameters \ node_version_has_solaris_binary iojs_version_has_solaris_binary \ + nvm_curl_libz_support \ > /dev/null 2>&1 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \ NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \ diff --git a/test/fast/Unit tests/nvm_curl_libz_support b/test/fast/Unit tests/nvm_curl_libz_support new file mode 100755 index 0000000..34602be --- /dev/null +++ b/test/fast/Unit tests/nvm_curl_libz_support @@ -0,0 +1,39 @@ +#!/bin/sh + +cleanup() { + unset -f curl +} + +die() { cleanup; echo "$@" ; exit 1; } + +\. ../../../nvm.sh + +curl() { + # curl with libz feature + if [ $# -ne 1 ] || [ "$1" != "-V" ]; then + die "This fake curl only takes one parameter -V" + fi + echo " +curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3 +Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp +Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets" +} + +nvm_curl_libz_support || die "nvm_curl_libz_support should return 0" + +unset -f curl + +curl() { + # curl without libz feature + if [ "$#" -ne 1 ] || [ "$1" != "-V" ]; then + die "This fake curl only takes one parameter -V" + fi + echo " +curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.32 +Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp +Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL TLS-SRP UnixSockets" +} + +! nvm_curl_libz_support || die "nvm_curl_libz_support should return 1" + +unset -f curl diff --git a/test/slow/nvm_get_latest/nvm_get_latest b/test/slow/nvm_get_latest/nvm_get_latest index 9f2f53c..306d371 100755 --- a/test/slow/nvm_get_latest/nvm_get_latest +++ b/test/slow/nvm_get_latest/nvm_get_latest @@ -14,7 +14,12 @@ EXPECTED_CURL_ARGS="--compressed -q -w %{url_effective}\n -L -s -S http://latest EXPECTED_WGET_ARGS="http://latest.nvm.sh --server-response -O /dev/null" curl() { - if [ "_$*" != "_$EXPECTED_CURL_ARGS" ]; then + if [ $# -eq 1 ] && [ "$1" = "-V" ]; then + echo " +curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3 +Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp +Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets" + elif [ "_$*" != "_$EXPECTED_CURL_ARGS" ]; then echo >&2 "expected args ($EXPECTED_CURL_ARGS), got ($*)" return 1 else