|
9 | 9 | CLI_RELEASES_PAGE_URL, |
10 | 10 | type CliReleaseChannel, |
11 | 11 | cliReleaseChannelOf, |
| 12 | + cliReleaseIndexPageUrl, |
12 | 13 | newestCliReleaseVersion, |
13 | 14 | } from "@t3tools/shared/cliRelease"; |
14 | 15 | import { compareSemverVersions } from "@t3tools/shared/semver"; |
@@ -47,38 +48,63 @@ export function canUpdateEnvironmentProvider(provider: ServerProvider) { |
47 | 48 | } |
48 | 49 |
|
49 | 50 | const decodeLatestRelease = Schema.decodeUnknownSync(Schema.Struct({ tag_name: Schema.String })); |
| 51 | +const decodeReleaseIndex = Schema.decodeUnknownSync( |
| 52 | + Schema.Array( |
| 53 | + Schema.Struct({ tag_name: Schema.String, draft: Schema.optionalKey(Schema.Boolean) }), |
| 54 | + ), |
| 55 | +); |
50 | 56 | const FEED_TAG = /<id>tag:github\.com,2008:Repository\/\d+\/([^<]+)<\/id>/g; |
51 | 57 |
|
52 | | -// Reads github.com rather than api.github.com, whose 60 anonymous requests an |
53 | | -// hour per IP are shared with every other device on the phone's network. |
54 | | -async function fetchReleases(path: "/latest" | ".atom", accept: string, signal: AbortSignal) { |
55 | | - const response = await fetch(`${CLI_RELEASES_PAGE_URL}${path}`, { |
56 | | - signal, |
57 | | - headers: { Accept: accept }, |
58 | | - }); |
| 58 | +async function fetchReleases(url: string, accept: string, signal: AbortSignal) { |
| 59 | + const response = await fetch(url, { signal, headers: { Accept: accept } }); |
59 | 60 | if (!response.ok) throw new Error(`Could not check releases (${response.status}). Try again.`); |
60 | 61 | return response; |
61 | 62 | } |
62 | 63 |
|
63 | 64 | /** |
64 | | - * Stable hosts read the latest release. Nightly and preview are never marked |
65 | | - * latest, so they read the feed of the ten newest releases, which nightly's |
66 | | - * several-a-day cadence keeps current. |
| 65 | + * Reads github.com release pages, which unlike api.github.com do not share 60 anonymous |
| 66 | + * requests an hour with every other device on the phone's network. Stable hosts read the |
| 67 | + * latest release. Nightly and preview are never marked latest, so they read the feed of |
| 68 | + * the ten newest releases. |
67 | 69 | */ |
68 | 70 | async function channelReleases(channel: CliReleaseChannel, signal: AbortSignal) { |
69 | 71 | if (channel === "stable") { |
70 | | - const response = await fetchReleases("/latest", "application/json", signal); |
| 72 | + const response = await fetchReleases( |
| 73 | + `${CLI_RELEASES_PAGE_URL}/latest`, |
| 74 | + "application/json", |
| 75 | + signal, |
| 76 | + ); |
71 | 77 | return [decodeLatestRelease(await response.json())]; |
72 | 78 | } |
73 | | - const response = await fetchReleases(".atom", "application/atom+xml", signal); |
| 79 | + const response = await fetchReleases( |
| 80 | + `${CLI_RELEASES_PAGE_URL}.atom`, |
| 81 | + "application/atom+xml", |
| 82 | + signal, |
| 83 | + ); |
74 | 84 | return Array.from((await response.text()).matchAll(FEED_TAG), ([, tag_name = ""]) => ({ |
75 | 85 | tag_name, |
76 | 86 | })); |
77 | 87 | } |
78 | 88 |
|
| 89 | +/** Pages the API release index, for a preview that later nightlies pushed out of the feed. */ |
| 90 | +async function newestInReleaseIndex(channel: CliReleaseChannel, signal: AbortSignal) { |
| 91 | + for (let page = 1; ; page++) { |
| 92 | + const response = await fetchReleases( |
| 93 | + cliReleaseIndexPageUrl(page), |
| 94 | + "application/vnd.github+json", |
| 95 | + signal, |
| 96 | + ); |
| 97 | + const releases = decodeReleaseIndex(await response.json()); |
| 98 | + const version = newestCliReleaseVersion(releases, channel); |
| 99 | + if (version !== undefined || releases.length < 100) return version; |
| 100 | + } |
| 101 | +} |
| 102 | + |
79 | 103 | export async function findEnvironmentUpdate(currentVersion: string, signal: AbortSignal) { |
80 | 104 | const channel = cliReleaseChannelOf(currentVersion); |
81 | | - const version = newestCliReleaseVersion(await channelReleases(channel, signal), channel); |
| 105 | + const version = |
| 106 | + newestCliReleaseVersion(await channelReleases(channel, signal), channel) ?? |
| 107 | + (await newestInReleaseIndex(channel, signal)); |
82 | 108 | if (version === undefined) throw new Error(`No ${channel} release was found.`); |
83 | 109 | return compareSemverVersions(version, currentVersion) > 0 ? version : null; |
84 | 110 | } |
0 commit comments