feat(launcher): add configurable update mirror

Let a saved HTTPS repository serve as the fallback for launcher and adaptation pack releases. Retry downloads for the selected version and validate against the project signing key. Verified type checks, the launcher build, and a simulated primary outage.
This commit is contained in:
N0VAN0M4LY committed 2026-12-01 20:04:29 +08:00
1 parent ca9843c304
commit 152be6af0e
13 files changed
+416 -257

No files matched your search

@@ -1,5 +1,5 @@
/**
* Adaptation pack updates: which patch set the builds use, whether GitHub has a newer pack, and
* Adaptation pack updates: which patch set the builds use, whether a source has a newer pack, and
* installing it. The launcher checks at start (when the setting allows) and from the About page;
* a pack is installed only when the player confirms.
*
@@ -20,12 +20,10 @@ import {
import type { ActivePatchSet, PackApplyResult, PackCheck, PackProblem, PackRelease } from '@shared/schemas/patch-pack.ts';
import { bundledResourcePaths, usesDownloadedPacks } from '../resources/resource-paths.ts';
import { activeDownloadedPack, bundledPatchVersion, installPack, useBundledPatchSet } from './pack-store.ts';
import { downloadReleaseFile, FeedUnreachableError, newestProjectRelease, ReleasePageLimitError, type PackReleaseFiles } from './release-feed.ts';
/** The manifest copy beside a release is a few hundred bytes. */
const MANIFEST_COPY_MAX_BYTES = 64 * 1024;
/** A signature file is 89 bytes. */
const SIGNATURE_MAX_BYTES = 1024;
import {
newestProjectRelease, projectReleaseVersion, ReleasePageLimitError, withReleaseSources, type PackReleaseFiles, type ReleaseSource,
} from './release-feed.ts';
import { FeedUnreachableError, readReleaseAsset, RELEASE_MANIFEST_MAX_BYTES, RELEASE_SIGNATURE_MAX_BYTES } from './release-assets.ts';
export function activePatchSet(): ActivePatchSet {
const root = bundledResourcePaths().patches;
@@ -38,7 +36,7 @@ export async function checkPackUpdate(): Promise<PackCheck> {
const active = activePatchSet();
if (!packSigningPublicKey()) return { status: 'failed', active, latest: null, problem: 'no_signing_key' };
try {
const found = await newestRelease(active.version);
const found = await withReleaseSources((source) => newestRelease(active.version, source));
if (!found || !isNewer(found.manifest.version, active.version)) return { status: 'up_to_date', active, latest: found ? release(found) : null, problem: null };
const status = manifestIncompatibility(found.manifest, app.getVersion()) ? 'needs_app_update' : 'available';
return { status, active, latest: release(found), problem: null };
@@ -48,18 +46,26 @@ export async function checkPackUpdate(): Promise<PackCheck> {
}
}
/** Download and activate the newest pack, when it is newer than the active set and usable. */
export async function applyPackUpdate(): Promise<PackApplyResult> {
/** Download and activate the selected pack when it is newer than the active set and usable. */
export async function applyPackUpdate(version: string): Promise<PackApplyResult> {
if (!packSigningPublicKey()) return { ok: false, problem: 'no_signing_key' };
if (!isNewer(version, activePatchSet().version)) return { ok: false, problem: 'up_to_date' };
try {
const found = await newestRelease(activePatchSet().version);
if (!found || !isNewer(found.manifest.version, activePatchSet().version)) return { ok: false, problem: 'up_to_date' };
if (manifestIncompatibility(found.manifest, app.getVersion())) return { ok: false, problem: 'needs_app_update' };
const tuning = patchPackTuning();
const zip = await downloadReleaseFile(found.files.zipUrl, tuning.maxPackBytes);
const signature = new TextDecoder().decode(await downloadReleaseFile(found.files.signatureUrl, SIGNATURE_MAX_BYTES));
await installPack(found.files.version, zip, signature);
console.info(`[patch-pack] pack ${found.files.version} installed and active`);
const downloaded = await withReleaseSources(async (source) => {
const files = await projectReleaseVersion('pack', version, source);
const manifest = await readPackManifest(files);
if (manifestIncompatibility(manifest, app.getVersion())) return null;
const tuning = patchPackTuning();
const zip = await readReleaseAsset(files.zipUrl, tuning.maxPackBytes, AbortSignal.timeout(tuning.requestTimeoutMs), source);
const signature = new TextDecoder().decode(await readReleaseAsset(
files.signatureUrl, RELEASE_SIGNATURE_MAX_BYTES, AbortSignal.timeout(tuning.requestTimeoutMs), source,
));
return { zip, signature };
});
if (!downloaded) return { ok: false, problem: 'needs_app_update' };
if (!isNewer(version, activePatchSet().version)) return { ok: false, problem: 'up_to_date' };
await installPack(version, downloaded.zip, downloaded.signature);
console.info(`[patch-pack] pack ${version} installed and active`);
return { ok: true, active: activePatchSet() };
} catch (error) {
console.warn('[patch-pack] update failed:', error);
@@ -78,13 +84,17 @@ interface FoundRelease {
readonly manifest: PatchPackManifest;
}
async function newestRelease(installedVersion: string | null): Promise<FoundRelease | null> {
const files = await newestProjectRelease('pack', installedVersion);
if (!files) return null;
const text = new TextDecoder().decode(await downloadReleaseFile(files.manifestUrl, MANIFEST_COPY_MAX_BYTES));
async function newestRelease(installedVersion: string | null, source: ReleaseSource): Promise<FoundRelease | null> {
const files = await newestProjectRelease('pack', installedVersion, source);
return files ? { files, manifest: await readPackManifest(files) } : null;
}
async function readPackManifest(files: PackReleaseFiles): Promise<PatchPackManifest> {
const deadline = AbortSignal.timeout(patchPackTuning().requestTimeoutMs);
const text = new TextDecoder().decode(await readReleaseAsset(files.manifestUrl, RELEASE_MANIFEST_MAX_BYTES, deadline, files.source));
const manifest = parseManifest(text);
if (manifest.version !== files.version) throw new PatchPackError('contents', `release ${files.version} carries the manifest of ${manifest.version}`);
return { files, manifest };
return manifest;
}
function release(found: FoundRelease): PackRelease {