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:
Yaroslav Rogov 2021-04-07 17:56:03 +03:00 committed by Augusto Moura
parent b87aca1b9a
commit e0f49386c9
3 changed files with 26 additions and 9 deletions

View File

@ -87,7 +87,7 @@ install_aliased_version() {
fi
>&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="$(
# Find the first candidate which the alias match, then print it version
print_index_tab \
| filter_version_candidates \
| awk -F'\t' -v "alias=$version_query" '$1 == alias { print $2; exit }'
)"

View File

@ -15,5 +15,5 @@ source "$(dirname "$0")/../lib/utils.sh"
# Print
echo $(
# Only print the first column of candidates
print_index_tab | filter_version_candidates | cut -f1 | tac
print_index_tab | cut -f1 | tac
)

View File

@ -21,12 +21,6 @@ die() {
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
# 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"
@ -63,3 +57,27 @@ filter_version_candidates() {
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"
}