Files
WT-Offline/scripts/r2/download-page.ts
T
N0VAN0M4LY de967dd993 feat(repo): add R2 release staging, download page, and S3 publish script
Add scripts/publish-r2-releases.ts with a zero-dependency SigV4 R2 client and bilingual static index.html generator. Staged launcher releases and adaptation packs are verified against the committed Ed25519 public key and SHA-256 digests before publishing, assets are uploaded ahead of the releases feed with SHA-256 skip checks, and .env*.local is gitignored for local R2 credentials. Verified with root typecheck, local staging, and live R2 upload.
2026-12-03 03:03:14 +08:00

466 lines
16 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Generates a self-contained bilingual (zh-CN / EN) static download page (`index.html`)
* from the GitHub-compatible release list served by the R2 distribution mirror.
*/
import { compareAppVersions, comparePackVersions, packVersionOfTag } from '@wto/patcher';
export interface ReleaseAssetEntry {
readonly name: string;
readonly browser_download_url: string;
readonly size?: number;
readonly sha256?: string;
}
export interface ReleaseEntry {
readonly tag_name: string;
readonly name: string;
readonly draft: boolean;
readonly prerelease: boolean;
readonly published_at: string;
readonly body: string;
readonly game_versions?: readonly string[];
readonly min_app_version?: string;
readonly assets: readonly ReleaseAssetEntry[];
}
export interface DownloadPageOptions {
readonly releases: readonly ReleaseEntry[];
readonly publicUrl: string;
readonly repoUrl: string;
readonly generatedAt: string;
}
export function renderDownloadPage(options: DownloadPageOptions): string {
const launchers = options.releases
.filter((entry) => !entry.draft && !entry.prerelease && entry.tag_name.startsWith('v'))
.sort((a, b) => compareAppVersions(b.tag_name.slice(1), a.tag_name.slice(1)));
const packs = options.releases
.filter((entry) => !entry.draft && packVersionOfTag(entry.tag_name) !== null)
.sort((a, b) => comparePackVersions(packVersionOfTag(b.tag_name)!, packVersionOfTag(a.tag_name)!));
const latestLauncher = launchers[0] ?? null;
const latestPack = packs[0] ?? null;
const olderLaunchers = launchers.slice(1);
const olderPacks = packs.slice(1);
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WTOffline — Download / 下载站</title>
<style>
:root {
color-scheme: dark;
--bg: #0d1117;
--panel: #161b22;
--panel-2: #1c222b;
--border: #30363d;
--text: #e6edf3;
--muted: #8b949e;
--accent: #d97706;
--accent-hover: #f59e0b;
--ok: #3fb950;
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
--sans: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: var(--sans);
background: radial-gradient(circle at 20% 0%, #1b2230 0%, var(--bg) 55%);
color: var(--text);
line-height: 1.6;
min-height: 100vh;
}
.wrap { max-width: 960px; margin: 0 auto; padding: 32px 20px 64px; }
header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 16px;
padding-bottom: 24px;
border-bottom: 1px solid var(--border);
}
.brand h1 { margin: 0; font-size: 28px; letter-spacing: 0.04em; }
.brand p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.top-actions { display: flex; align-items: center; gap: 10px; }
.btn-ghost {
display: inline-flex;
align-items: center;
padding: 8px 14px;
border: 1px solid var(--border);
border-radius: 6px;
color: var(--text);
background: var(--panel);
text-decoration: none;
font-size: 13px;
cursor: pointer;
}
.btn-ghost:hover { border-color: var(--accent); color: #fff; }
section { margin-top: 28px; }
.card {
background: var(--panel);
border: 1px solid var(--border);
border-radius: 10px;
padding: 22px;
}
.card-header {
display: flex;
flex-wrap: wrap;
align-items: baseline;
justify-content: space-between;
gap: 8px;
margin-bottom: 14px;
}
.card-header h2 { margin: 0; font-size: 20px; }
.badge {
display: inline-block;
padding: 2px 8px;
border-radius: 999px;
font-size: 12px;
font-family: var(--mono);
background: rgba(217, 119, 6, 0.16);
color: #fbbf24;
border: 1px solid rgba(217, 119, 6, 0.4);
margin-left: 8px;
vertical-align: middle;
}
.meta { color: var(--muted); font-size: 13px; }
.dl-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 14px;
margin-top: 14px;
}
.dl-box {
background: var(--panel-2);
border: 1px solid var(--border);
border-radius: 8px;
padding: 16px;
display: flex;
flex-direction: column;
gap: 10px;
}
.dl-box h3 { margin: 0; font-size: 15px; }
.dl-btn {
display: inline-flex;
justify-content: center;
align-items: center;
min-height: 44px;
padding: 10px 16px;
border-radius: 6px;
background: var(--accent);
color: #111;
font-weight: 600;
text-decoration: none;
font-size: 14px;
text-align: center;
white-space: nowrap;
}
.dl-btn:hover { background: var(--accent-hover); }
.dl-btn.secondary { background: #2b3441; color: var(--text); }
.dl-btn.secondary:hover { background: #374354; }
.file-name {
font-family: var(--mono);
font-size: 12px;
color: var(--text);
word-break: break-all;
margin: 0;
}
.hash {
font-family: var(--mono);
font-size: 11px;
color: var(--muted);
word-break: break-all;
margin: 0;
}
.notes {
margin-top: 18px;
padding-top: 16px;
border-top: 1px solid var(--border);
font-size: 14px;
}
.notes h3, .notes h4 { margin: 12px 0 6px; font-size: 15px; }
.notes ul { margin: 6px 0; padding-left: 20px; }
.notes li { margin: 4px 0; }
.notes code {
font-family: var(--mono);
font-size: 12px;
background: #0d1117;
padding: 1px 5px;
border-radius: 4px;
}
.tip {
margin-top: 14px;
padding: 12px 14px;
border-radius: 8px;
background: rgba(63, 185, 80, 0.08);
border: 1px solid rgba(63, 185, 80, 0.3);
font-size: 13px;
color: #c9f2cf;
}
details {
margin-top: 14px;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 8px;
padding: 12px 16px;
}
summary { cursor: pointer; font-weight: 600; font-size: 14px; }
table { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 13px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); }
td a { color: #fbbf24; text-decoration: none; margin-right: 10px; }
td a:hover { text-decoration: underline; }
footer {
margin-top: 40px;
padding-top: 16px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 12px;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 10px;
}
footer a { color: var(--muted); }
[data-lang="en"] .lang-zh { display: none; }
[data-lang="zh"] .lang-en { display: none; }
</style>
</head>
<body data-lang="zh">
<div class="wrap">
<header>
<div class="brand">
<h1>WTOffline</h1>
<p>
<span class="lang-zh">官方分发镜像站 · 全部安装包与适配包均经 Ed25519 签名校验</span>
<span class="lang-en">Official Release Mirror · All installers and adaptation packs are Ed25519 signed</span>
</p>
</div>
<div class="top-actions">
<a class="btn-ghost" href="${escapeHtml(options.repoUrl)}" target="_blank" rel="noopener">
<span class="lang-zh">源码仓库</span>
<span class="lang-en">Source Repository</span>
</a>
<a class="btn-ghost" href="${escapeHtml(options.repoUrl)}/releases" target="_blank" rel="noopener">
<span class="lang-zh">历史归档</span>
<span class="lang-en">Release Archive</span>
</a>
<button class="btn-ghost" id="lang-toggle" type="button">English / 中文</button>
</div>
</header>
<section>
${latestLauncher ? renderLatestLauncher(latestLauncher) : renderEmptyState('launcher')}
</section>
<section>
${latestPack ? renderLatestPack(latestPack) : renderEmptyState('pack')}
</section>
${olderLaunchers.length > 0 || olderPacks.length > 0 ? renderHistory(olderLaunchers, olderPacks) : ''}
<footer>
<span>
<span class="lang-zh">启动器自动更新接口:</span>
<span class="lang-en">Launcher update feed: </span>
<a href="${escapeHtml(options.publicUrl.replace(/\/$/, ''))}/releases">${escapeHtml(options.publicUrl.replace(/\/$/, ''))}/releases</a>
</span>
<span>Updated ${escapeHtml(options.generatedAt.slice(0, 19).replace('T', ' '))} UTC</span>
</footer>
</div>
<script>
(function () {
var saved = localStorage.getItem('wto-dl-lang');
var initial = saved || ((navigator.language || '').toLowerCase().startsWith('zh') ? 'zh' : 'en');
document.body.setAttribute('data-lang', initial);
document.getElementById('lang-toggle').addEventListener('click', function () {
var next = document.body.getAttribute('data-lang') === 'zh' ? 'en' : 'zh';
document.body.setAttribute('data-lang', next);
localStorage.setItem('wto-dl-lang', next);
});
})();
</script>
</body>
</html>
`;
}
function renderLatestLauncher(release: ReleaseEntry): string {
const installer = release.assets.find((asset) => asset.name.endsWith('.exe'));
const portable = release.assets.find((asset) => asset.name.endsWith('-portable.zip'));
const date = release.published_at.slice(0, 10);
return `<div class="card">
<div class="card-header">
<h2>
<span class="lang-zh">启动器最新版</span>
<span class="lang-en">Latest Launcher</span>
<span class="badge">${escapeHtml(release.tag_name)}</span>
</h2>
<span class="meta">Windows x64 · ${escapeHtml(date)}</span>
</div>
<div class="dl-grid">
${installer ? renderDownloadBox(installer, '安装版(推荐)', 'Windows Installer (Recommended)', '下载安装版', 'Download Installer', false) : ''}
${portable ? renderDownloadBox(portable, '便携压缩包(免安装)', 'Portable Archive (No Install)', '下载便携包', 'Download Portable', true) : ''}
</div>
<div class="tip">
<span class="lang-zh">已安装的启动器会在启动时自动从此镜像检查并验签更新;本程序不包含任何原版游戏文件,需配合你自己的客户端使用。</span>
<span class="lang-en">Installed launchers check this mirror automatically and verify Ed25519 signatures. No stock game files are distributed; your own client is required.</span>
</div>
${release.body.trim() ? `<div class="notes">${renderSimpleMarkdown(release.body)}</div>` : ''}
</div>`;
}
function renderDownloadBox(
asset: ReleaseAssetEntry,
titleZh: string,
titleEn: string,
btnZh: string,
btnEn: string,
secondary: boolean,
): string {
const sizeLabel = asset.size !== undefined ? ` (${formatBytes(asset.size)})` : '';
return `<div class="dl-box">
<h3>
<span class="lang-zh">${escapeHtml(titleZh)}</span>
<span class="lang-en">${escapeHtml(titleEn)}</span>
</h3>
<a class="dl-btn${secondary ? ' secondary' : ''}" href="${escapeHtml(asset.browser_download_url)}">
<span class="lang-zh">${escapeHtml(btnZh)}${escapeHtml(sizeLabel)}</span>
<span class="lang-en">${escapeHtml(btnEn)}${escapeHtml(sizeLabel)}</span>
</a>
<p class="file-name">${escapeHtml(asset.name)}</p>
${asset.sha256 ? `<p class="hash">SHA-256: ${escapeHtml(asset.sha256)}</p>` : ''}
</div>`;
}
function renderLatestPack(release: ReleaseEntry): string {
const zip = release.assets.find((asset) => asset.name.endsWith('.zip'));
const date = release.published_at.slice(0, 10);
const games = release.game_versions?.join(', ') ?? '';
const minApp = release.min_app_version ?? '';
return `<div class="card">
<div class="card-header">
<h2>
<span class="lang-zh">最新游戏版本适配包</span>
<span class="lang-en">Latest Adaptation Pack</span>
<span class="badge">${escapeHtml(release.tag_name)}</span>
</h2>
<span class="meta">${escapeHtml(date)}</span>
</div>
<p class="meta">
<span class="lang-zh">正常情况下启动器会自动拉取并启用适配包,无需手动下载。</span>
<span class="lang-en">The launcher fetches and activates adaptation packs automatically; manual download is normally unnecessary.</span>
</p>
${games ? `<p class="meta"><span class="lang-zh">已核对客户端版本:</span><span class="lang-en">Verified game versions: </span><code>${escapeHtml(games)}</code></p>` : ''}
${minApp ? `<p class="meta"><span class="lang-zh">最低启动器版本:</span><span class="lang-en">Minimum launcher version: </span><code>v${escapeHtml(minApp)}</code></p>` : ''}
${zip ? `<p><a class="btn-ghost" href="${escapeHtml(zip.browser_download_url)}">${escapeHtml(zip.name)}${zip.size !== undefined ? ` (${escapeHtml(formatBytes(zip.size))})` : ''}</a></p>` : ''}
</div>`;
}
function renderEmptyState(kind: 'launcher' | 'pack'): string {
const zh = kind === 'launcher' ? '暂未发布启动器安装包。' : '暂未发布适配包。';
const en = kind === 'launcher' ? 'No launcher releases published yet.' : 'No adaptation packs published yet.';
return `<div class="card"><p class="meta"><span class="lang-zh">${zh}</span><span class="lang-en">${en}</span></p></div>`;
}
function renderHistory(launchers: readonly ReleaseEntry[], packs: readonly ReleaseEntry[]): string {
const rows = [...launchers, ...packs].map((release) => {
const links = release.assets
.filter((asset) => asset.name.endsWith('.exe') || asset.name.endsWith('.zip'))
.map((asset) => `<a href="${escapeHtml(asset.browser_download_url)}">${escapeHtml(asset.name)}</a>`)
.join('');
return `<tr>
<td><code>${escapeHtml(release.tag_name)}</code></td>
<td>${escapeHtml(release.published_at.slice(0, 10))}</td>
<td>${links}</td>
</tr>`;
}).join('');
return `<details>
<summary>
<span class="lang-zh">历史版本(${launchers.length + packs.length})</span>
<span class="lang-en">Previous Releases (${launchers.length + packs.length})</span>
</summary>
<table>
<thead>
<tr>
<th>Tag</th>
<th>Date</th>
<th>Files</th>
</tr>
</thead>
<tbody>${rows}</tbody>
</table>
</details>`;
}
function renderSimpleMarkdown(source: string): string {
const lines = source.replace(/\r\n/g, '\n').split('\n');
const html: string[] = [];
let inList = false;
const closeList = (): void => {
if (inList) {
html.push('</ul>');
inList = false;
}
};
for (const raw of lines) {
const line = raw.trim();
if (!line) {
closeList();
continue;
}
if (line.startsWith('### ')) {
closeList();
html.push(`<h4>${inlineMarkdown(line.slice(4))}</h4>`);
continue;
}
if (line.startsWith('## ')) {
closeList();
html.push(`<h3>${inlineMarkdown(line.slice(3))}</h3>`);
continue;
}
if (line.startsWith('- ') || line.startsWith('* ')) {
if (!inList) {
html.push('<ul>');
inList = true;
}
html.push(`<li>${inlineMarkdown(line.slice(2))}</li>`);
continue;
}
closeList();
html.push(`<p>${inlineMarkdown(line)}</p>`);
}
closeList();
return html.join('\n');
}
function inlineMarkdown(text: string): string {
return escapeHtml(text)
.replace(/`([^`]+)`/g, '<code>$1</code>')
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
}
function formatBytes(bytes: number): string {
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${bytes} B`;
}
function escapeHtml(value: string): string {
return value
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}