[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
Provenance
- Commit
d211d5f- Tree
55f36eb- Author
Cassidy Marble <cassidy@marble.sh>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
913b8cb
2 files changed+57 added−0 removed
modified:test/fast/Unit tests/nvm_is_version_installed+41 added
43 unmodified lines44 ! nvm_is_version_installed || die 'nvm_is_version_installed without parameter should fail'45 46 check_version '12.0.0' 'node'47 48+# the pre-0.12 layout is still found via the fallback49+OLD_VERSION='0.11.15'50+mkdir -p "$NVM_DIR/$OLD_VERSION/bin"51+touch "$NVM_DIR/$OLD_VERSION/bin/node"52+chmod +x "$NVM_DIR/$OLD_VERSION/bin/node"53+nvm_is_version_installed "$OLD_VERSION" || die "nvm_is_version_installed $OLD_VERSION should work with the pre-0.12 layout"54+55+# a pre-0.12 version does not count as installed from the modern layout56+for 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"61+done62+63+# io.js versions live in a separate tree and are still found via the fallback64+IOJS_VERSION='v2.0.0'65+mkdir -p "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin"66+touch "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin/node"67+chmod +x "$NVM_DIR/versions/io.js/$IOJS_VERSION/bin/node"68+nvm_is_version_installed "iojs-$IOJS_VERSION" || die "nvm_is_version_installed iojs-$IOJS_VERSION should work via the fallback"69+70+# the modern layout is found without shelling out to detect the OS71+FAST_VERSION='14.0.0'72+! nvm_is_version_installed "$FAST_VERSION" || die "nvm_is_version_installed $FAST_VERSION should fail before it is installed"73+nvm_get_os() {74+ die 'nvm_is_version_installed must not call nvm_get_os when the modern-layout binary exists'75+}76+mkdir -p "$NODE_PATH/$FAST_VERSION/bin"77+touch "$NODE_PATH/$FAST_VERSION/bin/node"78+chmod +x "$NODE_PATH/$FAST_VERSION/bin/node"79+nvm_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 layout81+for 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"86+done87+unset -f nvm_get_os88+89 # Checking for Windows90 nvm_get_os() {91 echo "win"92 }4 unmodified linesmodified:nvm.sh+16 added
231 unmodified lines232 nvm_is_version_installed() {233 if [ -z "${1-}" ]; then234 return 1235 fi236+237+ # 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+ esac251+252 local NVM_NODE_BINARY253 NVM_NODE_BINARY='node'254 if [ "_$(nvm_get_os)" = '_win' ]; then255 NVM_NODE_BINARY='node.exe'5220 unmodified lines