Skip to content

Commit 202867e

Browse files
jorda0megaclaude
andcommitted
fix(mobile): check for server updates without GitHub's API rate limit
The environment screen's "Check for updates" listed releases through api.github.com without credentials. GitHub allows 60 such requests an hour per IP, shared with every other device on the same network, so the check could fail with "Could not check releases (403)" until the hourly reset. Read the same github.com release pages the desktop updater uses instead: `releases/latest` for stable hosts and `releases.atom` for nightly and preview hosts. Neither counts against the API quota. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 61eb4af commit 202867e

3 files changed

Lines changed: 75 additions & 43 deletions

File tree

‎apps/mobile/src/features/settings/environment-maintenance.test.ts‎

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -159,45 +159,53 @@ describe("environment maintenance access", () => {
159159

160160
describe("environment release checks", () => {
161161
const signal = new AbortController().signal;
162-
163-
it("keeps stable hosts on stable releases and ignores drafts", async () => {
164-
vi.stubGlobal(
165-
"fetch",
166-
vi
167-
.fn()
168-
.mockImplementation(async () =>
169-
Response.json([
170-
{ tag_name: "v2.0.0-nightly.20260923.1" },
171-
{ tag_name: "v1.2.0", draft: true },
172-
{ tag_name: "v1.1.0" },
173-
]),
174-
),
162+
const feed = (...tags: ReadonlyArray<string>) =>
163+
new Response(
164+
`<feed><id>tag:github.com,2008:https://github.com/pingdotgg/t3code/releases</id>${tags
165+
.map((tag) => `<entry><id>tag:github.com,2008:Repository/1/${tag}</id></entry>`)
166+
.join("")}</feed>`,
175167
);
168+
169+
it("offers the latest stable release to stable hosts", async () => {
170+
const fetchMock = vi.fn().mockImplementation(async () => Response.json({ tag_name: "v1.1.0" }));
171+
vi.stubGlobal("fetch", fetchMock);
176172
expect(await findEnvironmentUpdate("1.0.0", signal)).toBe("1.1.0");
177173
expect(await findEnvironmentUpdate("1.1.0", signal)).toBeNull();
178174
expect(await findEnvironmentUpdate("1.3.0", signal)).toBeNull();
175+
expect(fetchMock.mock.calls[0]?.[0]).toBe(
176+
"https://github.com/pingdotgg/t3code/releases/latest",
177+
);
179178
});
180179

181-
it("walks release pages to find the host's channel", async () => {
180+
it("finds the newest release on the host's prerelease channel in the feed", async () => {
182181
const fetchMock = vi
183182
.fn()
184-
.mockResolvedValueOnce(
185-
Response.json(Array.from({ length: 100 }, () => ({ tag_name: "v2.0.0" }))),
186-
)
187-
.mockResolvedValueOnce(Response.json([{ tag_name: "v1.0.0-preview.20260923.2" }]));
183+
.mockImplementation(async () =>
184+
feed(
185+
"v1.0.0-preview.20260924.3",
186+
"v1.0.0-nightly.20260924.2",
187+
"v1.0.0",
188+
"v1.0.0-nightly.20260923.1",
189+
),
190+
);
188191
vi.stubGlobal("fetch", fetchMock);
192+
expect(await findEnvironmentUpdate("1.0.0-nightly.20260923.1", signal)).toBe(
193+
"1.0.0-nightly.20260924.2",
194+
);
195+
expect(await findEnvironmentUpdate("1.0.0-nightly.20260924.2", signal)).toBeNull();
189196
expect(await findEnvironmentUpdate("1.0.0-preview.20260923.1", signal)).toBe(
190-
"1.0.0-preview.20260923.2",
197+
"1.0.0-preview.20260924.3",
191198
);
192-
expect(fetchMock.mock.calls[1]?.[0]).toContain("page=2");
193-
expect(fetchMock.mock.calls[1]?.[1]).toEqual({ signal });
199+
expect(fetchMock.mock.calls[0]?.[0]).toBe("https://github.com/pingdotgg/t3code/releases.atom");
194200
});
195201

196202
it("reports failed checks instead of claiming the server is current", async () => {
197-
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })));
198-
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("403");
199-
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json([])));
200-
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("No stable release");
203+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 429 })));
204+
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow("429");
205+
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(feed("v1.0.0")));
206+
await expect(findEnvironmentUpdate("1.0.0-nightly.20260923.1", signal)).rejects.toThrow(
207+
"No nightly release",
208+
);
201209
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(Response.json({ error: "invalid" })));
202210
await expect(findEnvironmentUpdate("1.0.0", signal)).rejects.toThrow();
203211
});

‎apps/mobile/src/features/settings/environment-maintenance.ts‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
type ServerProvider,
77
} from "@t3tools/contracts";
88
import {
9+
CLI_RELEASES_PAGE_URL,
10+
type CliReleaseChannel,
911
cliReleaseChannelOf,
10-
cliReleaseIndexPageUrl,
1112
newestCliReleaseVersion,
1213
} from "@t3tools/shared/cliRelease";
1314
import { compareSemverVersions } from "@t3tools/shared/semver";
@@ -45,25 +46,40 @@ export function canUpdateEnvironmentProvider(provider: ServerProvider) {
4546
);
4647
}
4748

48-
const Releases = Schema.Array(
49-
Schema.Struct({
50-
tag_name: Schema.String,
51-
draft: Schema.optionalKey(Schema.Boolean),
52-
}),
53-
);
54-
const decodeReleases = Schema.decodeUnknownSync(Releases);
49+
const decodeLatestRelease = Schema.decodeUnknownSync(Schema.Struct({ tag_name: Schema.String }));
50+
const FEED_TAG = /<id>tag:github\.com,2008:Repository\/\d+\/([^<]+)<\/id>/g;
51+
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+
});
59+
if (!response.ok) throw new Error(`Could not check releases (${response.status}). Try again.`);
60+
return response;
61+
}
62+
63+
/**
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.
67+
*/
68+
async function channelReleases(channel: CliReleaseChannel, signal: AbortSignal) {
69+
if (channel === "stable") {
70+
const response = await fetchReleases("/latest", "application/json", signal);
71+
return [decodeLatestRelease(await response.json())];
72+
}
73+
const response = await fetchReleases(".atom", "application/atom+xml", signal);
74+
return Array.from((await response.text()).matchAll(FEED_TAG), ([, tag_name = ""]) => ({
75+
tag_name,
76+
}));
77+
}
5578

5679
/** Preserve the host's release channel and never offer a downgrade. */
5780
export async function findEnvironmentUpdate(currentVersion: string, signal: AbortSignal) {
5881
const channel = cliReleaseChannelOf(currentVersion);
59-
for (let page = 1; ; page++) {
60-
const response = await fetch(cliReleaseIndexPageUrl(page), { signal });
61-
if (!response.ok) throw new Error(`Could not check releases (${response.status}). Try again.`);
62-
const releases = decodeReleases(await response.json());
63-
const version = newestCliReleaseVersion(releases, channel);
64-
if (version !== undefined) {
65-
return compareSemverVersions(version, currentVersion) > 0 ? version : null;
66-
}
67-
if (releases.length < 100) throw new Error(`No ${channel} release was found.`);
68-
}
82+
const version = newestCliReleaseVersion(await channelReleases(channel, signal), channel);
83+
if (version === undefined) throw new Error(`No ${channel} release was found.`);
84+
return compareSemverVersions(version, currentVersion) > 0 ? version : null;
6985
}

‎packages/shared/src/cliRelease.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ export function cliReleaseIndexPageUrl(page: number): string {
101101
return `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=100&page=${page}`;
102102
}
103103

104+
/**
105+
* github.com release pages, which the desktop updater also reads. Unlike the
106+
* API they have no anonymous hourly quota shared by everything behind one IP.
107+
* `/latest` answers JSON for the newest stable release; `.atom` lists the ten
108+
* newest releases on every channel.
109+
*/
110+
export const CLI_RELEASES_PAGE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases`;
111+
104112
/**
105113
* Picks the newest version on a channel from the release index. Tags are
106114
* `v<version>`; the channel is decided by the same rule the runtime uses, so

0 commit comments

Comments
 (0)