Refactor Mihomo WebSocket URL handling and improve Svelte data processing
Publish telemt-api gateway Docker image / test (push) Successful in 32s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 2m28s

- Updated the `mihomoWsUrl` function to handle cases where the gateway base URL may be undefined, ensuring robust WebSocket URL generation.
- Enhanced the Svelte component to read and process memory metrics more reliably by implementing a mechanism to capture multiple lines of data before concluding the read operation, improving data accuracy.
This commit is contained in:
Denozordec
2026-03-31 09:26:59 +07:00
parent 8b7233159a
commit cb6ecd46b1
2 changed files with 11 additions and 3 deletions
+5 -2
View File
@@ -27,8 +27,11 @@ export function mihomoUrl(alias: string, path: string): string {
*/
export function mihomoWsUrl(alias: string, path: string): string {
const p = path.replace(/^\/+/, '');
const base = gatewayBase();
const wsBase = base.replace(/^http/, 'ws');
const base =
gatewayBase() || (typeof window !== 'undefined' ? window.location.origin.replace(/\/$/, '') : '');
let wsBase = base;
if (wsBase.startsWith('https://')) wsBase = `wss://${wsBase.slice('https://'.length)}`;
else if (wsBase.startsWith('http://')) wsBase = `ws://${wsBase.slice('http://'.length)}`;
return `${wsBase}/api/${encodeURIComponent(alias)}/mihomo/${p}`;
}
@@ -190,6 +190,7 @@
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = '';
let seen = 0;
try {
while (!signal.aborted) {
const { done, value } = await reader.read();
@@ -198,17 +199,21 @@
const nl = buf.indexOf('\n');
if (nl < 0) continue;
const line = buf.slice(0, nl).trim();
buf = buf.slice(nl + 1);
if (line) {
try {
const o = JSON.parse(line) as { inuse?: number };
memKB = Number(o.inuse) || 0;
histMem = [...histMem, memKB];
while (histMem.length > MAX_POINTS) histMem.shift();
seen++;
} catch {
/* ignore */
}
}
break;
// В Mihomo для /memory первая строка нередко 0 после нового подключения.
// Берём хотя бы две строки в рамках одного запроса, затем завершаем.
if (seen >= 2) break;
}
} finally {
try {