90 lines
3.6 KiB
TypeScript
90 lines
3.6 KiB
TypeScript
import { GetObjectCommand, HeadObjectCommand, S3Client } from '@aws-sdk/client-s3'
|
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
|
|
import { s3Client } from '../s3'
|
|
|
|
export const semver = require('semver') as {
|
|
valid(version: unknown): string | null
|
|
gt(left: string, right: string): boolean
|
|
}
|
|
|
|
export function validVersion(value: unknown): value is string {
|
|
const normalized = semver.valid(value)
|
|
// semver.valid 也接受 v 前缀,并在返回值中省略构建元数据。
|
|
return normalized !== null && typeof value === 'string' && value.split('+')[0] === normalized
|
|
}
|
|
|
|
export function validChannel(value: string): boolean {
|
|
return /^[a-z0-9-]{1,32}$/.test(value)
|
|
}
|
|
|
|
export function releasePrefix(channel: string): string {
|
|
return `xiaoyi-client/${channel}`
|
|
}
|
|
|
|
export type ClientRelease = {
|
|
version: string
|
|
channel: string
|
|
commit: string
|
|
builtAt: string
|
|
publishedAt: string
|
|
notes: string
|
|
minimumVersion: string
|
|
file: { name: string; size: number; sha256: string }
|
|
}
|
|
|
|
export function parseRelease(value: ClientRelease, channel: string, version: string): ClientRelease {
|
|
if (!value || value.version !== version || value.channel !== channel ||
|
|
!validVersion(version) || !validVersion(value.minimumVersion) ||
|
|
![value.commit, value.builtAt, value.publishedAt, value.notes].every(field => typeof field === 'string') ||
|
|
typeof value.file?.name !== 'string' || !/^[^/\\\r\n]+\.zip$/i.test(value.file.name) ||
|
|
!Number.isSafeInteger(value.file.size) || value.file.size <= 0 ||
|
|
typeof value.file.sha256 !== 'string' || !/^[a-f0-9]{64}$/i.test(value.file.sha256)) {
|
|
throw new Error('发布清单无效')
|
|
}
|
|
return value
|
|
}
|
|
|
|
export async function readReleaseJson<T>(key: string): Promise<T | null> {
|
|
try {
|
|
const object = await s3Client.send(new GetObjectCommand({ Bucket: process.env.S3_BUCKET, Key: key }))
|
|
return JSON.parse(await object.Body!.transformToString()) as T
|
|
} catch (error) {
|
|
if (error instanceof Error && error.name === 'NoSuchKey') return null
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function readRelease(channel: string, version: string): Promise<ClientRelease | null> {
|
|
const release = await readReleaseJson<ClientRelease>(`${releasePrefix(channel)}/${version}/release.json`)
|
|
return release === null ? null : parseRelease(release, channel, version)
|
|
}
|
|
|
|
export async function verifyReleaseFile(channel: string, release: ClientRelease): Promise<void> {
|
|
const object = await s3Client.send(new HeadObjectCommand({
|
|
Bucket: process.env.S3_BUCKET,
|
|
Key: `${releasePrefix(channel)}/${release.version}/${release.file.name}`,
|
|
}))
|
|
if (object.ContentLength !== release.file.size) throw new Error('存储中的安装包大小与清单不一致')
|
|
}
|
|
|
|
export async function signReleaseDownload(channel: string, release: ClientRelease): Promise<string> {
|
|
const command = new GetObjectCommand({
|
|
Bucket: process.env.S3_BUCKET,
|
|
Key: `${releasePrefix(channel)}/${release.version}/${release.file.name}`,
|
|
ResponseContentDisposition: `attachment; filename*=UTF-8''${encodeURIComponent(release.file.name).replace(/['()*]/g, char => '%' + char.charCodeAt(0).toString(16))}`,
|
|
})
|
|
return getSignedUrl(publicS3Client(), command, { expiresIn: 600 })
|
|
}
|
|
|
|
// 下载地址按公网存储域名签名,客户端在内网和外网都能直接下载。
|
|
function publicS3Client(): S3Client {
|
|
const domain = process.env.S3_PUBLIC_DOMAIN?.trim()
|
|
if (!domain) return s3Client
|
|
return new S3Client({
|
|
endpoint: new URL(domain).origin,
|
|
region: process.env.S3_REGION || 'us-east-1',
|
|
credentials: { accessKeyId: process.env.S3_ACCESS_KEY_ID!, secretAccessKey: process.env.S3_SECRET_ACCESS_KEY! },
|
|
forcePathStyle: true,
|
|
})
|
|
}
|