gongxiangp2/scripts/verify-xiaoyi-client.ts

46 lines
2.0 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.

import { readRelease, readReleaseJson, releasePrefix, signReleaseDownload, validVersion, verifyReleaseFile } from '../lib/desktop-client/client-release'
async function verifyStableRelease(version: string, commit?: string) {
const channel = 'stable'
const pointer = await readReleaseJson<{ version: string }>(`${releasePrefix(channel)}/latest.json`)
if (pointer?.version !== version) throw new Error('稳定通道版本与预期不一致')
const release = await readRelease(channel, pointer.version)
if (!release || (commit && release.commit !== commit)) throw new Error('线上清单与目标提交不一致')
await verifyReleaseFile(channel, release)
const url = new URL(await signReleaseDownload(channel, release))
if (url.protocol !== 'https:') throw new Error('下载地址需使用 HTTPS')
const response = await fetch(url, {
headers: { Range: 'bytes=0-3' },
redirect: 'error',
signal: AbortSignal.timeout(20_000),
})
if (response.status !== 206 || response.headers.get('content-range') !== `bytes 0-3/${release.file.size}`) {
await response.body?.cancel()
throw new Error('公网 HTTPS 下载范围与清单不一致')
}
const signature = Buffer.from(await response.arrayBuffer()).toString('hex')
if (signature !== '504b0304') throw new Error('线上对象缺少 ZIP 文件头')
return {
version: release.version,
commit: release.commit,
size: release.file.size,
sha256: release.file.sha256,
downloadStatus: response.status,
downloadHost: url.host,
}
}
const [version, commit] = process.argv.slice(2)
if (!version || !validVersion(version) || (commit && !/^[a-f0-9]{40}$/.test(commit))) {
console.error('用法:verify-xiaoyi-client.ts <稳定通道版本号> [完整提交 SHA]')
process.exitCode = 1
} else {
verifyStableRelease(version, commit).then((result) => console.log(JSON.stringify(result))).catch(() => {
console.error('线上验证失败,请核对稳定通道、版本清单、文件大小和公网 HTTPS 下载。')
process.exitCode = 1
})
}