收口 Makelore 客户端变更
This commit is contained in:
@@ -70,6 +70,7 @@ type DesktopAuthTokenPayload = {
|
||||
};
|
||||
|
||||
const DESKTOP_AUTH_TIMEOUT_MS = 10 * 60 * 1000;
|
||||
const MAX_AUTH_ERROR_LENGTH = 180;
|
||||
|
||||
function readRequiredString(value: unknown, field: string): string {
|
||||
if (typeof value !== 'string' || !value.trim()) {
|
||||
@@ -164,17 +165,29 @@ async function readResponsePayload(response: Response): Promise<unknown> {
|
||||
}
|
||||
|
||||
function getErrorMessage(payload: unknown, fallback: string): string {
|
||||
const compact = (value: string): string => {
|
||||
const normalized = value.replace(/\s+/g, ' ').trim();
|
||||
if (
|
||||
!normalized
|
||||
|| normalized.length > MAX_AUTH_ERROR_LENGTH
|
||||
|| /<!doctype\b|<html\b|<head\b|<body\b/i.test(normalized)
|
||||
) {
|
||||
return fallback;
|
||||
}
|
||||
return normalized;
|
||||
};
|
||||
|
||||
if (payload && typeof payload === 'object') {
|
||||
const record = payload as Record<string, unknown>;
|
||||
for (const field of ['msg', 'message', 'error_description', 'error']) {
|
||||
const value = record[field];
|
||||
if (typeof value === 'string' && value.trim()) {
|
||||
return value;
|
||||
return compact(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (typeof payload === 'string' && payload.trim()) {
|
||||
return payload;
|
||||
return compact(payload);
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
@@ -238,7 +251,7 @@ async function pollDesktopAuthToken(
|
||||
const payload = await readResponsePayload(response) as DesktopAuthTokenPayload;
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(getErrorMessage(payload, `Desktop authorization failed (${response.status})`));
|
||||
throw new Error(getErrorMessage(payload, '登录授权失败,请稍后重试。'));
|
||||
}
|
||||
|
||||
if (payload.status === 'approved' && payload.token) {
|
||||
@@ -248,7 +261,7 @@ async function pollDesktopAuthToken(
|
||||
await delay(pollIntervalMs);
|
||||
}
|
||||
|
||||
throw new Error('Authorization timed out');
|
||||
throw new Error('登录超时,请重新尝试。');
|
||||
}
|
||||
|
||||
async function handleBrowserAuthorization(
|
||||
@@ -264,7 +277,7 @@ async function handleBrowserAuthorization(
|
||||
if (!response.ok) {
|
||||
sendJson(res, response.status >= 400 && response.status < 500 ? response.status : 502, {
|
||||
success: false,
|
||||
error: getErrorMessage(payload, `Desktop authorization start failed (${response.status})`),
|
||||
error: getErrorMessage(payload, '登录服务暂时不可用,请稍后重试。'),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user