Skip to content
Myr

[Perf] `nvm_is_version_installed`: check the modern layout's path before the full lookup

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Cassidy Marble · committed by Jordan Harband · parent 913b8cb · Browse files · 2 files+57 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+57 added−0 removed

  • tests:+41 added−0 removed
  • other:+16 added−0 removed

2 files+57 added−0 removed

modified:test/fast/Unit tests/nvm_is_version_installed+41 added
43 unmodified lines
44! nvm_is_version_installed || die 'nvm_is_version_installed without parameter should fail'4546check_version '12.0.0' 'node'4748# the pre-0.12 layout is still found via the fallback49OLD_VERSION='0.11.15'50mkdir -p "$NVM_DIR/$OLD_VERSION/bin"51touch "$NVM_DIR/$OLD_VERSION/bin/node"52chmod +x "$NVM_DIR/$OLD_VERSION/bin/node"53nvm_is_version_installed "$OLD_VERSION" || die "nvm_is_version_installed $OLD_VERSION should work with the pre-0.12 layout"5455# a pre-0.12 version does not count as installed from the modern layout56for STRAY_VERSION in '0.10.48' 'v0.10.48'; do57 mkdir -p "$NODE_PATH/$STRAY_VERSION/bin"58 touch "$NODE_PATH/$STRAY_VERSION/bin/node"59 chmod +x "$NODE_PATH/$STRAY_VERSION/bin/node"60 ! nvm_is_version_installed "$STRAY_VERSION" || die "nvm_is_version_installed $STRAY_VERSION should not find a pre-0.12 version in the modern layout"61done6263# io.js versions live in a separate tree and are still found via the fallback64IOJS_VERSION='v2.0.0'65mkdir -p "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin"66touch "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin/node"67chmod +x "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin/node"68nvm_is_version_installed "iojs-$IOJS_VERSION" || die "nvm_is_version_installed iojs-$IOJS_VERSION should work via the fallback"6970# the modern layout is found without shelling out to detect the OS71FAST_VERSION='14.0.0'72! nvm_is_version_installed "$FAST_VERSION" || die "nvm_is_version_installed $FAST_VERSION should fail before it is installed"73nvm_get_os() {74 die 'nvm_is_version_installed must not call nvm_get_os when the modern-layout binary exists'75}76mkdir -p "$NODE_PATH/$FAST_VERSION/bin"77touch "$NODE_PATH/$FAST_VERSION/bin/node"78chmod +x "$NODE_PATH/$FAST_VERSION/bin/node"79nvm_is_version_installed "$FAST_VERSION" || die "nvm_is_version_installed $FAST_VERSION should succeed via the fast path"80# 0.12.x is the first version in the modern layout81for FAST_VERSION in '0.12.18' 'v0.12.18'; do82 mkdir -p "$NODE_PATH/$FAST_VERSION/bin"83 touch "$NODE_PATH/$FAST_VERSION/bin/node"84 chmod +x "$NODE_PATH/$FAST_VERSION/bin/node"85 nvm_is_version_installed "$FAST_VERSION" || die "nvm_is_version_installed $FAST_VERSION should succeed via the fast path"86done87unset -f nvm_get_os8889# Checking for Windows90nvm_get_os() {91 echo "win"92}
4 unmodified lines
modified:nvm.sh+16 added
231 unmodified lines
232nvm_is_version_installed() {233 if [ -z "${1-}" ]; then234 return 1235 fi236237 # Fast path: versions in the modern layout live at a directly-computable238 # path. Checking it first avoids forking `nvm_version_path` (and the `awk`239 # version comparison inside it) and `nvm_get_os` on shell startup. Versions240 # below `v0.12` are skipped, since they must not count from the modern241 # layout; those, io.js, and Windows binaries fall through to the full lookup242 # below, which also rejects path traversal.243 case "/${1}/" in244 */../* | /v0.[0-9].* | /v0.1[01].* | /0.[0-9].* | /0.1[01].*) ;;245 *)246 if [ -x "${NVM_DIR}/versions/node/${1}/bin/node" ]; then247 return 0248 fi249 ;;250 esac251252 local NVM_NODE_BINARY253 NVM_NODE_BINARY='node'254 if [ "_$(nvm_get_os)" = '_win' ]; then255 NVM_NODE_BINARY='node.exe'
5220 unmodified lines