mirror of
https://github.com/asdf-vm/asdf-nodejs.git
synced 2024-10-06 07:42:08 +08:00
feat: Implement index tab cache (#219)
* feat: Implement index tab cache * feat: Switch to etag-based caching * Update lib/utils.sh Co-authored-by: Augusto Borges de Moura <augusto.borgesm@gmail.com> Co-authored-by: Trevor Brown <Stratus3D@users.noreply.github.com>
This commit is contained in:
parent
b87aca1b9a
commit
e0f49386c9
@ -87,7 +87,7 @@ install_aliased_version() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
>&2 echo "Linking \"$version_query\" to \"$version\""
|
>&2 echo "Linking \"$version_query\" to \"$version\""
|
||||||
ln -s "$(asdf where "$(plugin_name)" "$version")" "$install_path"
|
ln -s "$(asdf where "$(plugin_name)" "$version")" "$install_path"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -97,7 +97,6 @@ resolve_version_query() {
|
|||||||
local canon_version="$(
|
local canon_version="$(
|
||||||
# Find the first candidate which the alias match, then print it version
|
# Find the first candidate which the alias match, then print it version
|
||||||
print_index_tab \
|
print_index_tab \
|
||||||
| filter_version_candidates \
|
|
||||||
| awk -F'\t' -v "alias=$version_query" '$1 == alias { print $2; exit }'
|
| awk -F'\t' -v "alias=$version_query" '$1 == alias { print $2; exit }'
|
||||||
)"
|
)"
|
||||||
|
|
||||||
|
@ -15,5 +15,5 @@ source "$(dirname "$0")/../lib/utils.sh"
|
|||||||
# Print
|
# Print
|
||||||
echo $(
|
echo $(
|
||||||
# Only print the first column of candidates
|
# Only print the first column of candidates
|
||||||
print_index_tab | filter_version_candidates | cut -f1 | tac
|
print_index_tab | cut -f1 | tac
|
||||||
)
|
)
|
||||||
|
30
lib/utils.sh
30
lib/utils.sh
@ -21,12 +21,6 @@ die() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: implement a cache for the tab. The api supports If-None-Match and
|
|
||||||
# If-Modified-Since HTTP headers
|
|
||||||
print_index_tab() {
|
|
||||||
curl --silent "${NODEJS_ORG_MIRROR}index.tab"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Tab file needs to be piped as stdin
|
# Tab file needs to be piped as stdin
|
||||||
# Print all alias and correspondent versions in the format "$alias\t$version"
|
# Print all alias and correspondent versions in the format "$alias\t$version"
|
||||||
# Also prints versions as a alias of itself. Eg: "v10.0.0\tv10.0.0"
|
# Also prints versions as a alias of itself. Eg: "v10.0.0\tv10.0.0"
|
||||||
@ -63,3 +57,27 @@ filter_version_candidates() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
versions_cache_dir="${ASDF_DATA_DIR:-${ASDF_HOME:-$HOME/.asdf}}/tmp/$(plugin_name)/cache"
|
||||||
|
mkdir -p "$versions_cache_dir"
|
||||||
|
|
||||||
|
etag_file="$versions_cache_dir/etag"
|
||||||
|
index_file="$versions_cache_dir/index"
|
||||||
|
touch "$etag_file" "$index_file"
|
||||||
|
|
||||||
|
print_index_tab(){
|
||||||
|
local temp_headers_file="$(mktemp)"
|
||||||
|
|
||||||
|
if [ -f "$etag_file" ]; then
|
||||||
|
etag_flag='--header If-None-Match:'"$(cat "$etag_file")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
index="$(curl --fail --silent --dump-header "$temp_headers_file" $etag_flag "${NODEJS_ORG_MIRROR}index.tab")"
|
||||||
|
if [ -z "$index" ]; then
|
||||||
|
cat "$index_file"
|
||||||
|
else
|
||||||
|
cat "$temp_headers_file" | awk 'tolower($1) == "etag:" { print $2 }' > "$etag_file"
|
||||||
|
echo "$index" | filter_version_candidates | tee "$index_file"
|
||||||
|
fi
|
||||||
|
|
||||||
|
rm "$temp_headers_file"
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user