Skip to content

Commit 47c0dbf

Browse files
committed
Add curl.exe fallback when GitHub API unreachable for specific version
When installing a specific version (-Version v1.x.y) and the GitHub API is blocked or rate-limited, set $ReleaseAssets to $null instead of failing hard, then fall back to probing candidate artifact URLs with 'curl.exe -fsI' (HEAD requests). curl.exe ships with Windows 10 1803+ and is already used for the binary download, so the dependency is not new. This preserves the behaviour of the original script in API-restricted environments while avoiding Invoke-WebRequest (which triggers the PS5.1 security warning). Addresses code-review feedback on PR #122.
1 parent 70a50b0 commit 47c0dbf

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

install.ps1

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ if ($Version -eq "latest") {
125125
$Response = Invoke-RestMethod -Uri $ApiUrl -Headers @{ "Accept" = "application/vnd.github.v3+json" }
126126
$ReleaseAssets = $Response.assets | ForEach-Object { $_.name }
127127
} catch {
128-
Write-Output "Install Failed:"
129-
Write-Output " Could not fetch release ${Tag} from the GitHub API."
130-
Write-Output " URL: $ApiUrl"
131-
Write-Output " $_"
132-
exit 1
128+
# GitHub API unreachable (blocked, rate-limited, …) — fall back to probing
129+
# candidate artifacts directly with curl.exe HEAD requests. curl.exe ships
130+
# with Windows 10 1803+ and is already used for the binary download below.
131+
Write-Output " GitHub API unavailable ($ApiUrl); falling back to direct artifact probing..."
132+
$ReleaseAssets = $null
133133
}
134134
}
135135

@@ -144,6 +144,8 @@ if ($Version -eq "latest") {
144144
# Artifact availability is checked against the GitHub release asset list already
145145
# fetched above — no extra network request needed, and no Invoke-WebRequest
146146
# which triggers a security warning on Windows PowerShell 5.1.
147+
# When the API was unreachable ($ReleaseAssets is $null), we fall back to
148+
# probing candidate URLs with curl.exe HEAD requests instead.
147149

148150
$CandidateTargets = @($Target)
149151
if ($Target -eq "x64-modern") {
@@ -156,15 +158,24 @@ if ($Target -eq "x64-modern") {
156158
$Artifact = $null
157159
foreach ($Candidate in $CandidateTargets) {
158160
$CandidateArtifact = "${BinaryName}-windows-${Candidate}.exe"
159-
if ($ReleaseAssets -contains $CandidateArtifact) {
161+
$Found = $false
162+
if ($null -ne $ReleaseAssets) {
163+
$Found = $ReleaseAssets -contains $CandidateArtifact
164+
} else {
165+
# Fallback: probe via curl.exe HEAD (no Invoke-WebRequest, no PS5.1 warning).
166+
$CheckUrl = "https://github.com/${Repo}/releases/download/${Tag}/${CandidateArtifact}"
167+
$null = curl.exe -fsI $CheckUrl 2>$null
168+
$Found = $LASTEXITCODE -eq 0
169+
}
170+
if ($Found) {
160171
$Artifact = $CandidateArtifact
161172
$Target = $Candidate
162173
break
163174
}
164175
}
165176

166177
if ($null -eq $Artifact) {
167-
$AvailableWindows = $ReleaseAssets | Where-Object { $_ -like "*windows*" }
178+
$AvailableWindows = if ($null -ne $ReleaseAssets) { $ReleaseAssets | Where-Object { $_ -like "*windows*" } } else { @() }
168179
Write-Output "Install Failed:"
169180
Write-Output " No compatible Windows binary found for ${Tag}."
170181
Write-Output " Tried: $($CandidateTargets -join ', ')"

0 commit comments

Comments
 (0)