fix(update): recover from GitHub rate limits with configurable mirrors
This commit is contained in:
1 parent
68deb48811
commit
20cf7394e5
21 files changed
+1423
-93
No files matched your search
@@ -1,8 +1,10 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { isObject } from './coerce';
|
||||
import { normalizeUpdateSourceConfig, type UpdateSourceConfig } from './update-source';
|
||||
|
||||
export interface RuntimeConfig {
|
||||
updateSource?: UpdateSourceConfig;
|
||||
webuiPort?: number;
|
||||
/** When true, every newly-discovered QQ process gets auto-injected by
|
||||
* the HookManager. Also overridable at runtime via SNOWLUMA_HOOK_AUTOLOAD.
|
||||
@@ -43,6 +45,9 @@ export const MAX_LOG_RETAIN_DAYS = Math.floor(Number.MAX_SAFE_INTEGER / (24 * 60
|
||||
export function normalizeRuntimeConfig(parsed: unknown): RuntimeConfig {
|
||||
const obj = isObject(parsed) ? parsed : {};
|
||||
return {
|
||||
...(obj.updateSource === undefined
|
||||
? {}
|
||||
: { updateSource: normalizeUpdateSourceConfig(obj.updateSource) }),
|
||||
webuiPort: normalizePort(obj.webuiPort ?? DEFAULT_WEBUI_PORT, DEFAULT_WEBUI_PORT),
|
||||
hookAutoLoad: normalizeBool(obj.hookAutoLoad, false),
|
||||
webuiHost: normalizeHost(obj.webuiHost),
|
||||
@@ -217,7 +222,8 @@ function sameRuntimeConfig(parsed: Record<string, unknown>, n: RuntimeConfig): b
|
||||
parsed.trustProxy === n.trustProxy &&
|
||||
parsed.logMaxTotalMb === n.logMaxTotalMb &&
|
||||
parsed.logRetainDays === n.logRetainDays &&
|
||||
parsed.logPerUin === n.logPerUin
|
||||
parsed.logPerUin === n.logPerUin &&
|
||||
JSON.stringify(parsed.updateSource) === JSON.stringify(n.updateSource)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
export interface UpdateSourceConfig {
|
||||
mode: 'auto' | 'direct' | 'mirror';
|
||||
mirror: string;
|
||||
mirrors: string[];
|
||||
}
|
||||
|
||||
export function normalizeMirrorUrl(value: unknown): string {
|
||||
if (typeof value !== 'string' || value.length > 2048) {
|
||||
throw new Error('镜像地址必须是 HTTP(S) URL');
|
||||
}
|
||||
|
||||
let url: URL;
|
||||
|
||||
try {
|
||||
url = new URL(value.trim());
|
||||
} catch {
|
||||
throw new Error('镜像地址必须是 HTTP(S) URL');
|
||||
}
|
||||
|
||||
if (
|
||||
!['http:', 'https:'].includes(url.protocol) ||
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.search ||
|
||||
url.hash
|
||||
) {
|
||||
throw new Error('镜像地址仅支持 HTTP(S),不能包含账号、密码、查询参数或片段');
|
||||
}
|
||||
|
||||
return url.href.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
export function normalizeUpdateSourceConfig(value: unknown): UpdateSourceConfig {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
throw new Error('更新来源必须是对象');
|
||||
}
|
||||
|
||||
if (
|
||||
!('mode' in value) ||
|
||||
(value.mode !== 'auto' && value.mode !== 'direct' && value.mode !== 'mirror')
|
||||
) {
|
||||
throw new Error('更新来源模式无效');
|
||||
}
|
||||
|
||||
if (!('mirrors' in value) || !Array.isArray(value.mirrors) || value.mirrors.length > 16) {
|
||||
throw new Error('自动筛选最多支持 16 个镜像');
|
||||
}
|
||||
|
||||
const mirror = 'mirror' in value && value.mirror !== '' ? normalizeMirrorUrl(value.mirror) : '';
|
||||
|
||||
if (value.mode === 'mirror' && !mirror) {
|
||||
throw new Error('请指定镜像地址');
|
||||
}
|
||||
|
||||
return {
|
||||
mode: value.mode,
|
||||
mirror,
|
||||
mirrors: [...new Set(value.mirrors.map(normalizeMirrorUrl))],
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user