Skip to content

Commit b03d9cc

Browse files
authored
Improve version fetching from crates.io (#572)
Updated version fetching method to use crates.io index for reliability. crates.io is rate limited to 1 rps Tested: - [x] `curl -o- https://raw.githubusercontent.com/graphql-hive/router/kamilkisiela-patch-1/install.sh | sh -s v0.0.17` - [x] `curl -o- https://raw.githubusercontent.com/graphql-hive/router/kamilkisiela-patch-1/install.sh | sh -s v0.0.16` - [x] `curl -o- https://raw.githubusercontent.com/graphql-hive/router/kamilkisiela-patch-1/install.sh | sh`
1 parent 7a61e46 commit b03d9cc

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

install.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ get_version() {
8080
VERSION="$1"
8181
info "Installing specified version: $VERSION"
8282
else
83-
info "No version specified. Fetching the latest version from crates.io..."
84-
CRATES_API_URL="https://crates.io/api/v1/crates/${CARGO_PKG_NAME}"
85-
VERSION="v$(curl -sL "$CRATES_API_URL" | grep -o '"max_version":"[^"]*"' | head -1 | sed 's/"max_version":"\([^"]*\)"/\1/')"
83+
info "No version specified. Fetching the latest version from crates.io index..."
8684

87-
if [ -z "$VERSION" ]; then
88-
error "Could not determine the latest version from crates.io. Please check the crate name."
85+
# Uses index.crates.io which is more reliable than the rate-limited Crates API
86+
# Path structure: /first_two_chars/next_two_chars/full_name
87+
CRATE_FIRST_TWO=$(echo "${CARGO_PKG_NAME}" | cut -c1-2)
88+
CRATE_NEXT_TWO=$(echo "${CARGO_PKG_NAME}" | cut -c3-4)
89+
90+
INDEX_URL="https://index.crates.io/${CRATE_FIRST_TWO}/${CRATE_NEXT_TWO}/${CARGO_PKG_NAME}"
91+
92+
# The index returns newline-delimited JSON; extract the version from the last line
93+
VERSION="v$(curl -sL "$INDEX_URL" | tail -1 | grep -o '"vers":"[^"]*"' | sed 's/"vers":"\([^"]*\)"/\1/')"
94+
95+
if [ -z "$VERSION" ] || [ "$VERSION" = "v" ]; then
96+
error "Could not determine the latest version from crates.io index. Please check the crate name or specify a version manually."
8997
fi
90-
info "Latest version found on crates.io: $VERSION"
98+
info "Latest version found: $VERSION"
9199
fi
92100
}
93101

0 commit comments

Comments
 (0)