Repository navigation
Expand file tree
/
Copy pathpanel-ui.js
More file actions
64 lines (58 loc) · 2.67 KB
/
Copy pathpanel-ui.js
File metadata and controls
64 lines (58 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory();
else root.PanelUI = factory();
})(typeof globalThis === 'object' ? globalThis : this, function () {
function matchesBot(bot, query) {
const text = String(query || '').trim().toLowerCase();
return !text || [bot.name, bot.app].some(value => String(value || '').toLowerCase().includes(text));
}
function fleetSummary(bots, apps) {
const counts = { total: bots.length, online: 0, offline: 0, unknown: 0 };
for (const bot of bots) {
if (!apps) counts.unknown++;
else if (apps.find(app => app.name === bot.app)?.status === 'online') counts.online++;
else counts.offline++;
}
return counts;
}
function sameStatusTarget(before, after) {
return ['host', 'port', 'user', 'key', 'app', 'botId'].every(key => (before[key] || '') === (after[key] || ''));
}
function outputLevel(text) {
return /error|✖|fail|denied|fatal|ERR!|exception|timed out/i.test(text) ? 'e'
: /warn|⚠/i.test(text) ? 'w'
: /✔|success|online/i.test(text) ? 'o' : '';
}
function matchesOutput(text, query, errorsOnly) {
return (!errorsOnly || outputLevel(text) === 'e') && String(text).toLowerCase().includes(String(query || '').trim().toLowerCase());
}
async function readActionResponse(response, onText) {
if (!response.ok) {
let message = 'Request failed (HTTP ' + response.status + ').';
try {
const text = await response.text();
if ((response.headers.get('content-type') || '').includes('application/json')) message = JSON.parse(text).error || message;
else if (text.trim()) message = text.trim();
} catch {}
throw new Error(message);
}
if (!response.body) throw new Error('The panel returned no action output.');
const reader = response.body.getReader(), decoder = new TextDecoder();
let tail = '';
const emit = text => { if (text) { tail = (tail + text).slice(-8192); onText(text); } };
try {
for (;;) {
const { done, value } = await reader.read();
if (done) break;
emit(decoder.decode(value, { stream: true }));
}
emit(decoder.decode());
} finally { reader.releaseLock(); }
const last = tail.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, '').replace(/\r/g, '').trim().split('\n').pop();
if (last === '✔ Done') return { status: 'success', code: 0 };
const failure = last.match(/^✖ Failed \(exit (\d+)\)$/);
if (failure) return { status: 'failed', code: Number(failure[1]) };
return { status: 'unknown', code: null };
}
return { matchesBot, fleetSummary, sameStatusTarget, outputLevel, matchesOutput, readActionResponse };
});