Files
WT-Offline/apps/launcher/src/main/services/patch-pack/release-feed.ts
T
N0VAN0M4LY 746b74c564 feat(updates): verify and install launcher releases
Check the versioned release channel, verify the signed manifest and installer digest, and open the installer only after confirmation.

Verified type checks, the Windows package, and an upgrade handoff from a local release fixture, including tampering and interrupted downloads.
2026-11-30 11:12:44 +08:00

91 lines
4.5 KiB
TypeScript

/**
* Adaptation pack releases on GitHub: the newest release tagged `patches-<version>` that carries
* all three files (zip, signature, manifest copy), and downloads of those files.
*
* Requests send no account, no cookie and nothing about the player; GitHub sees an anonymous
* request with a generic user agent.
*/
import { net } from 'electron';
import { comparePackVersions, packFileNames, packVersionOfTag, PatchPackError, patchPackTuning } from '@wto/patcher';
export interface PackReleaseFiles {
readonly version: string;
readonly publishedAt: string | null;
readonly zipUrl: string;
readonly signatureUrl: string;
readonly manifestUrl: string;
}
/** Thrown when GitHub cannot be reached or answers with an error. */
export class FeedUnreachableError extends Error {
override readonly name = 'FeedUnreachableError';
}
const HEADERS = { Accept: 'application/vnd.github+json', 'User-Agent': 'WTOffline-Launcher', 'X-GitHub-Api-Version': '2022-11-28' };
let listing: Promise<readonly unknown[]> | null = null;
/** Share the in-flight request when the launcher checks both release channels at startup. */
export function listProjectReleases(): Promise<readonly unknown[]> {
return listing ??= readProjectReleases().finally(() => { listing = null; });
}
async function readProjectReleases(): Promise<readonly unknown[]> {
const tuning = patchPackTuning();
const url = `https://api.github.com/repos/${tuning.repository}/releases?per_page=${tuning.releasesPerCheck}`;
const response = await request(url, HEADERS, tuning.requestTimeoutMs);
if (response.status === 404) return [];
if (!response.ok) throw new FeedUnreachableError(`GitHub answered HTTP ${response.status} for the release list`);
const releases = (await response.json()) as unknown;
if (!Array.isArray(releases)) throw new PatchPackError('contents', 'the release list is not a list');
return releases;
}
/** The newest complete pack release, or null when none is released. */
export async function newestPackRelease(): Promise<PackReleaseFiles | null> {
const packs = (await listProjectReleases()).map(packRelease).filter((release): release is PackReleaseFiles => release !== null);
packs.sort((a, b) => comparePackVersions(b.version, a.version));
return packs[0] ?? null;
}
function packRelease(value: unknown): PackReleaseFiles | null {
if (typeof value !== 'object' || value === null) return null;
const release = value as Record<string, unknown>;
if (release['draft'] === true || release['prerelease'] === true || typeof release['tag_name'] !== 'string') return null;
const version = packVersionOfTag(release['tag_name']);
if (!version || !Array.isArray(release['assets'])) return null;
const urls = new Map<string, string>();
for (const asset of release['assets'] as unknown[]) {
if (typeof asset !== 'object' || asset === null) continue;
const { name, browser_download_url: download } = asset as Record<string, unknown>;
if (typeof name === 'string' && typeof download === 'string' && download.startsWith('https://')) urls.set(name, download);
}
const names = packFileNames(version);
const zipUrl = urls.get(names.zip);
const signatureUrl = urls.get(names.signature);
const manifestUrl = urls.get(names.manifest);
if (!zipUrl || !signatureUrl || !manifestUrl) return null;
const publishedAt = typeof release['published_at'] === 'string' ? release['published_at'] : null;
return { version, publishedAt, zipUrl, signatureUrl, manifestUrl };
}
/** Download a release file, refusing anything larger than `maxBytes`. */
export async function downloadReleaseFile(url: string, maxBytes: number): Promise<Uint8Array> {
const tuning = patchPackTuning();
const response = await request(url, { 'User-Agent': HEADERS['User-Agent'] }, tuning.requestTimeoutMs);
if (!response.ok) throw new FeedUnreachableError(`HTTP ${response.status} for ${url}`);
const declared = Number(response.headers.get('content-length') ?? '0');
if (declared > maxBytes) throw new PatchPackError('size', `${url} is ${declared} bytes, more than ${maxBytes}`);
const content = new Uint8Array(await response.arrayBuffer());
if (content.length > maxBytes) throw new PatchPackError('size', `${url} is more than ${maxBytes} bytes`);
return content;
}
async function request(url: string, headers: Record<string, string>, timeoutMs: number): Promise<Response> {
try {
return await net.fetch(url, { credentials: 'omit', headers, signal: AbortSignal.timeout(timeoutMs) });
} catch (error) {
throw new FeedUnreachableError(`${url}: ${(error as Error).message}`);
}
}