Skip to content
Myr

[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

6 files+59 added−15 removed

modified:test/fast/Unit tests/nvm install --offline+2 added-1 removed
28 unmodified lines
29 || die "nvm install --offline error should mention 'not found locally or in cache'; got '$CAPTURED_STDERR'"3031# --offline should not require curl or wget32nvm_has() { return 1; }33nvm_has_executable() { return 1; }34try_err nvm install --offline 999.999.99935# Should fail with "not found" not "nvm needs curl or wget"36nvm_echo "$CAPTURED_STDERR" | nvm_grep -q "curl or wget" \37 && die "nvm install --offline should not require curl or wget"38alias nvm_has='\nvm_has'38unset -f nvm_has39unset -f nvm_has nvm_has_executable
modified:test/fast/Unit tests/nvm_download and curl helpers bypass shadowed curl+18 added
43 unmodified lines
4445nvm_download -s 'http://example.com/' -o /dev/null || die 'nvm_download used the shadowing curl function and failed'46grep -Fxq 'http://example.com/' "$ARGV_LOG" || die "nvm_download did not invoke the curl executable; got: $(cat "$ARGV_LOG")"4748# when no curl executable exists, a shadowing curl shell function must not fool49# downloader selection: nvm_download must fall back to the wget executable50WGET_ONLY_BIN="$WORK/wget-only-bin"51mkdir -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"57chmod +x "$WGET_ONLY_BIN/wget"5859(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'64grep -Fxq 'http://wget-fallback.example.com/' "$ARGV_LOG" || die "nvm_download did not invoke the wget executable; got: $(cat "$ARGV_LOG")"6566cleanup
modified:test/fast/Unit tests/nvm_download wget Authorization header+2 added-2 removed
4 unmodified lines
5TEST_BIN="$WORK/bin"6ARGV_LOG="$WORK/argv.log"78cleanup() {9 unset -f die cleanup nvm_has9 unset -f die cleanup nvm_has_executable10 rm -rf "$WORK"11 export PATH="$OLDPATH"12}13die () { echo "$@" ; cleanup ; exit 1; }
15 unmodified lines
2930export ARGV_LOG31export PATH="$TEST_BIN:$OLDPATH"32# force the wget path while keeping system tools (sed) available for sanitization33nvm_has() { [ "$1" != curl ] && command -v "$1" >/dev/null 2>&1; }33nvm_has_executable() { [ "$1" != curl ] && command -v "$1" >/dev/null 2>&1; }3435# given an Authorization credential in NVM_AUTH_HEADER36# when nvm_download uses the wget path37NVM_AUTH_HEADER='Bearer test-token' nvm_download "https://nodejs.org/dist/x" -o - || die 'nvm_download (wget) returned nonzero'
6 unmodified lines
modified:test/fast/Unit tests/nvm_get_latest missing curl or wget+2 added-1 removed
1 unmodified line
23die () { echo "$@" ; cleanup ; exit 1; }45cleanup() {6 unset -f nvm_has6 unset -f nvm_has nvm_has_executable7}89: nvm.sh10\. ../../../nvm.sh1112\. ../../common.sh1314nvm_has() { return 1 ; }15nvm_has_executable() { return 1 ; }1617try_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 lines
modified:install.sh+17 added-5 removed
4 unmodified lines
5nvm_has() {6 type "$1" > /dev/null 2>&17}89# resolves like `command "${1}"` does: only executables on the PATH,10# ignoring shell functions and aliases11nvm_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}2021nvm_echo() {22 command printf %s\\n "$*" 2>/dev/null23}24
91 unmodified lines
116 nvm_echo "$NODE_VERSION"117}118119nvm_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 lines
412 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 1
4 unmodified lines
425 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_script
73 unmodified lines
507# Unsets the various functions defined508# during the execution of the install script509#510nvm_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 lines
modified:nvm.sh+18 added-6 removed
47 unmodified lines
48nvm_has() {49 type "${1-}" >/dev/null 2>&150}5152# resolves like `command "${1}"` does: only executables on the PATH,53# ignoring shell functions and aliases54nvm_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}6364nvm_has_non_aliased() {65 nvm_has "${1-}" && ! nvm_is_alias "${1-}"66}67
40 unmodified lines
108109nvm_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 1
16 unmodified lines
138 fi139140 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 lines
3580 ;;3581 esac3582 done35833572 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 lines
4699 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