[优化] 添加服务器升级日志的WebSocket流

This commit is contained in:
ryan
2026-03-15 11:57:52 +08:00
parent ae70ba1cce
commit eb9a2a8814
9 changed files with 255 additions and 5 deletions
+41 -1
View File
@@ -4,6 +4,7 @@ import type { ApiEnvelope } from '@/types/api';
import type {
LatestReleaseInfo,
ReleaseChannel,
UpgradeStreamSnapshot,
UploadedServerBinaryInfo,
} from '@/features/update/types';
@@ -48,7 +49,9 @@ export function uploadServerBinary(
xhr.addEventListener('load', () => {
let payload: ApiEnvelope<UploadedServerBinaryInfo> | null = null;
try {
payload = JSON.parse(xhr.responseText) as ApiEnvelope<UploadedServerBinaryInfo>;
payload = JSON.parse(
xhr.responseText,
) as ApiEnvelope<UploadedServerBinaryInfo>;
} catch {
payload = null;
}
@@ -86,3 +89,40 @@ export function confirmManualServerUpgrade(uploadToken: string) {
body: JSON.stringify({ upload_token: uploadToken }),
});
}
export function createUpgradeLogsWebSocket() {
if (typeof window === 'undefined') {
return null;
}
const apiUrl = getApiUrl('/update/logs/ws');
const resolvedUrl = apiUrl.startsWith('http://')
? `ws://${apiUrl.slice('http://'.length)}`
: apiUrl.startsWith('https://')
? `wss://${apiUrl.slice('https://'.length)}`
: `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${window.location.host}${apiUrl}`;
return new WebSocket(resolvedUrl);
}
export function parseUpgradeStreamSnapshot(
rawMessage: string,
): UpgradeStreamSnapshot | null {
try {
const parsed = JSON.parse(rawMessage) as Partial<UpgradeStreamSnapshot>;
if (
typeof parsed.in_progress !== 'boolean' ||
typeof parsed.upgrade_status !== 'string' ||
!Array.isArray(parsed.upgrade_logs)
) {
return null;
}
return {
in_progress: parsed.in_progress,
upgrade_status: parsed.upgrade_status,
upgrade_logs: parsed.upgrade_logs,
};
} catch {
return null;
}
}