[Fix] `nvm_download`, `nvm_get_latest`, `install`: only select a downloader that exists as an executable
Before adoption: this commit came in with the history the project adopted, so no landing records it.
`nvm_has` matches shell functions and aliases, but downloads now run via `command`, which skips them - a `curl` shell function with no curl binary on the PATH would select the curl path and fail with exit 127, instead of falling back to an available wget executable. The new `nvm_has_executable` helper resolves names the same way `command` does, so downloader selection and execution agree.
authored by Jordan Harband · committed by Jordan Harband · parent 04fef13 · Browse files · 6 files·+59 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+59 added−15 removed
- tests:+24 added−4 removed
- other:+35 added−11 removed
Provenance
- Commit
9142a92- Tree
80da059- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
04fef13
6 files changed+59 added−15 removed
modified:test/fast/Unit tests/nvm install --offline+2 added-1 removed
28 unmodified lines29 || die "nvm install --offline error should mention 'not found locally or in cache'; got '$CAPTURED_STDERR'"30 31 # --offline should not require curl or wget32 nvm_has() { return 1; }33+nvm_has_executable() { return 1; }34 try_err nvm install --offline 999.999.99935 # Should fail with "not found" not "nvm needs curl or wget"36 nvm_echo "$CAPTURED_STDERR" | nvm_grep -q "curl or wget" \37 && die "nvm install --offline should not require curl or wget"38 alias nvm_has='\nvm_has'38-unset -f nvm_has39+unset -f nvm_has nvm_has_executablemodified:test/fast/Unit tests/nvm_download and curl helpers bypass shadowed curl+18 added
43 unmodified lines44 45 nvm_download -s 'http://example.com/' -o /dev/null || die 'nvm_download used the shadowing curl function and failed'46 grep -Fxq 'http://example.com/' "$ARGV_LOG" || die "nvm_download did not invoke the curl executable; got: $(cat "$ARGV_LOG")"47 48+# when no curl executable exists, a shadowing curl shell function must not fool49+# downloader selection: nvm_download must fall back to the wget executable50+WGET_ONLY_BIN="$WORK/wget-only-bin"51+mkdir -p "$WGET_ONLY_BIN"52+{53+ echo '#!/bin/sh'54+ echo ': > "$ARGV_LOG"'55+ echo 'for a in "$@"; do printf "%s\n" "$a" >> "$ARGV_LOG"; done'56+} > "$WGET_ONLY_BIN/wget"57+chmod +x "$WGET_ONLY_BIN/wget"58+59+(60+ PATH="$WGET_ONLY_BIN"61+ export PATH62+ nvm_download -s 'http://wget-fallback.example.com/' -o /dev/null63+) || die 'nvm_download did not fall back to wget when curl is only a shell function'64+grep -Fxq 'http://wget-fallback.example.com/' "$ARGV_LOG" || die "nvm_download did not invoke the wget executable; got: $(cat "$ARGV_LOG")"65+66 cleanupmodified:test/fast/Unit tests/nvm_download wget Authorization header+2 added-2 removed
4 unmodified lines5 TEST_BIN="$WORK/bin"6 ARGV_LOG="$WORK/argv.log"7 8 cleanup() {9- unset -f die cleanup nvm_has9+ unset -f die cleanup nvm_has_executable10 rm -rf "$WORK"11 export PATH="$OLDPATH"12 }13 die () { echo "$@" ; cleanup ; exit 1; }15 unmodified lines29 30 export ARGV_LOG31 export PATH="$TEST_BIN:$OLDPATH"32 # force the wget path while keeping system tools (sed) available for sanitization33-nvm_has() { [ "$1" != curl ] && command -v "$1" >/dev/null 2>&1; }33+nvm_has_executable() { [ "$1" != curl ] && command -v "$1" >/dev/null 2>&1; }34 35 # given an Authorization credential in NVM_AUTH_HEADER36 # when nvm_download uses the wget path37 NVM_AUTH_HEADER='Bearer test-token' nvm_download "https://nodejs.org/dist/x" -o - || die 'nvm_download (wget) returned nonzero'6 unmodified linesmodified:test/fast/Unit tests/nvm_get_latest missing curl or wget+2 added-1 removed
1 unmodified line2 3 die () { echo "$@" ; cleanup ; exit 1; }4 5 cleanup() {6- unset -f nvm_has6+ unset -f nvm_has nvm_has_executable7 }8 9 : nvm.sh10 \. ../../../nvm.sh11 12 \. ../../common.sh13 14 nvm_has() { return 1 ; }15+nvm_has_executable() { return 1 ; }16 17 try_err nvm_get_latest18 [ "_$CAPTURED_STDERR" = "_nvm needs curl or wget to proceed." ] \19 || die "no curl/wget did not report correct error message, got '$CAPTURED_STDERR'"4 unmodified linesmodified:install.sh+17 added-5 removed
4 unmodified lines5 nvm_has() {6 type "$1" > /dev/null 2>&17 }8 9+# resolves like `command "${1}"` does: only executables on the PATH,10+# ignoring shell functions and aliases11+nvm_has_executable() {12+ (13+ # `|| true` so that shells with errexit-style options (eg, zsh's ERR_RETURN)14+ # do not abort the subshell when the name is not an alias or a function15+ unalias "${1-}" 2>/dev/null || true16+ unset -f "${1-}" 2>/dev/null || true17+ command -v "${1-}" > /dev/null 2>&118+ )19+}20+21 nvm_echo() {22 command printf %s\\n "$*" 2>/dev/null23 }24 91 unmodified lines116 nvm_echo "$NODE_VERSION"117 }118 119 nvm_download() {108- if nvm_has "curl"; then120+ if nvm_has_executable "curl"; then121 curl --fail --compressed -q "$@"110- elif nvm_has "wget"; then122+ elif nvm_has_executable "wget"; then123 # Emulate curl with wget124 ARGS=$(nvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \125 -e 's/--compressed //' \126 -e 's/--fail //' \285 unmodified lines412 if [ -z "${METHOD}" ]; then413 # Autodetect install method414 if nvm_has git; then415 install_nvm_from_git404- elif nvm_has curl || nvm_has wget; then416+ elif nvm_has_executable curl || nvm_has_executable wget; then417 install_nvm_as_script418 else419 nvm_echo >&2 'You need git, curl, or wget to install nvm'420 exit 14 unmodified lines425 exit 1426 fi427 install_nvm_from_git428 elif [ "${METHOD}" = 'script' ]; then417- if ! nvm_has curl && ! nvm_has wget; then429+ if ! nvm_has_executable curl && ! nvm_has_executable wget; then430 nvm_echo >&2 "You need curl or wget to install nvm"431 exit 1432 fi433 install_nvm_as_script73 unmodified lines507 # Unsets the various functions defined508 # during the execution of the install script509 #510 nvm_reset() {499- unset -f nvm_has nvm_install_dir nvm_latest_version nvm_profile_is_bash_or_zsh \511+ unset -f nvm_has nvm_has_executable nvm_install_dir nvm_latest_version nvm_profile_is_bash_or_zsh \512 nvm_source nvm_node_version nvm_download install_nvm_from_git nvm_install_node \513 install_nvm_as_script nvm_try_profile nvm_detect_profile nvm_check_global_modules \514 nvm_do_install nvm_reset nvm_default_install_dir nvm_grep515 }4 unmodified linesmodified:nvm.sh+18 added-6 removed
47 unmodified lines48 nvm_has() {49 type "${1-}" >/dev/null 2>&150 }51 52+# resolves like `command "${1}"` does: only executables on the PATH,53+# ignoring shell functions and aliases54+nvm_has_executable() {55+ (56+ # `|| true` so that shells with errexit-style options (eg, zsh's ERR_RETURN)57+ # do not abort the subshell when the name is not an alias or a function58+ unalias "${1-}" 2>/dev/null || true59+ unset -f "${1-}" 2>/dev/null || true60+ command -v "${1-}" >/dev/null 2>&161+ )62+}63+64 nvm_has_non_aliased() {65 nvm_has "${1-}" && ! nvm_is_alias "${1-}"66 }67 40 unmodified lines108 109 nvm_get_latest() {110 local NVM_LATEST_URL111 local CURL_COMPRESSED_FLAG100- if nvm_has "curl"; then112+ if nvm_has_executable "curl"; then113 if nvm_curl_use_compression; then114 CURL_COMPRESSED_FLAG="--compressed"115 fi116 NVM_LATEST_URL="$(curl ${CURL_COMPRESSED_FLAG:-} -q -w "%{url_effective}\\n" -L -s -S https://latest.nvm.sh -o /dev/null)"105- elif nvm_has "wget"; then117+ elif nvm_has_executable "wget"; then118 NVM_LATEST_URL="$(command wget -q https://latest.nvm.sh --server-response -O /dev/null 2>&1 | command awk '/^ Location: /{DEST=$2} END{ print DEST }')"119 else120 nvm_err 'nvm needs curl or wget to proceed.'121 return 116 unmodified lines138 fi139 140 local NVM_DOWNLOADER141 NVM_DOWNLOADER=''130- if nvm_has "curl"; then142+ if nvm_has_executable "curl"; then143 NVM_DOWNLOADER='curl'144 set -- -q --fail "$@"145 if nvm_curl_use_compression; then146 set -- --compressed "$@"147 fi136- elif nvm_has "wget"; then148+ elif nvm_has_executable "wget"; then149 NVM_DOWNLOADER='wget'150 # Emulate curl with wget151 local NVM_DOWNLOAD_WGET_COUNT152 NVM_DOWNLOAD_WGET_COUNT=$#3427 unmodified lines3580 ;;3581 esac3582 done3583 3572- if [ "${NVM_OFFLINE}" != 1 ] && ! nvm_has "curl" && ! nvm_has "wget"; then3584+ if [ "${NVM_OFFLINE}" != 1 ] && ! nvm_has_executable "curl" && ! nvm_has_executable "wget"; then3585 nvm_err 'nvm needs curl or wget to proceed.'3586 return 13587 fi3588 1110 unmodified lines4699 nvm_find_nvmrc nvm_find_up nvm_find_project_dir nvm_tree_contains_path \4700 nvm_version_greater nvm_version_greater_than_or_equal_to \4701 nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \4702 nvm_has_system_node nvm_has_system_iojs \4691- nvm_download nvm_get_latest nvm_has nvm_install_default_packages nvm_get_default_packages \4703+ nvm_download nvm_get_latest nvm_has nvm_has_executable nvm_install_default_packages nvm_get_default_packages \4704 nvm_curl_use_compression nvm_curl_version \4705 nvm_auto nvm_supports_xz \4706 nvm_echo nvm_err nvm_grep nvm_cd \4707 nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \187 unmodified lines