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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// Sites and services are views of the same 9web application. File/log links
// use app.mjs navigation; control writes use the normal HTTP-to-9P mapping.
const $ = id => document.getElementById(id);
const element = (tag, text = '') => { const e = document.createElement(tag); e.textContent = text; return e; };
function url(address, port, protocol = 'http') {
let host = address;
if (!host || host === '0.0.0.0' || host === '::') host = location.hostname;
host = host.replace(/^\[|\]$/g, '');
return `${protocol}://${host.includes(':') ? `[${host}]` : host}:${port}/`;
}
export function catalogViews(pathLink) {
let active = 'files', timer = null, request = null, revision = 0, pending = new Set();
const rows = new Map();
let namespace = '';
function status(view, text, error = false) {
const p = $(`${view}-status`); p.textContent = text; p.classList.toggle('error', error);
}
async function control(site, command) {
if (pending.has(site.name)) return;
pending.add(site.name); updateSite(site);
try {
const r = await fetch(`/fs${namespace}/sites/${encodeURIComponent(site.name)}/ctl`, { method: 'PUT', body: command });
if (!r.ok) throw new Error(await r.text());
status('sites', `${site.title}: ${command} requested.`);
} catch (error) { status('sites', error.message, true); }
finally { pending.delete(site.name); updateSite(site); if (active === 'sites') await refresh(false); }
}
function updateSite(site) {
let row = rows.get(site.name);
if (!row) {
const tr = element('tr'); tr.dataset.site = site.name;
const name = element('td'), phase = element('td'), finished = element('td'), actions = element('td');
const link = element('a'); link.className = 'site-name';
const root = element('span'); root.className = 'site-root'; name.append(link, root);
const tools = element('div'); tools.className = 'site-actions';
const build = element('button', 'build'), watch = element('button', 'watch'), cancel = element('button', 'cancel');
for (const b of [build, watch, cancel]) b.type = 'button';
tools.append(build, watch, cancel, pathLink('files', `${namespace}/sites/${site.name}/public`), pathLink('log', `${namespace}/sites/${site.name}/log`));
actions.append(tools); tr.append(name, phase, finished, actions);
row = { tr, link, root, phase, finished, build, watch, cancel }; rows.set(site.name, row);
$('sites-entries').append(tr);
}
const s = site.status;
row.link.textContent = site.title; row.link.href = url('', site.port); row.root.textContent = site.root;
row.phase.textContent = `${s.phase}${s.watching ? ' · watch on' : ''}${s.queued ? ' · queued' : ''}${s.exit_code !== null ? ` · exit ${s.exit_code}` : ''}`;
row.phase.className = `site-phase${s.phase === 'failed' ? ' failed' : ''}`;
row.finished.textContent = s.finished ? new Date(s.finished * 1000).toLocaleString() : '—';
row.build.disabled = pending.has(site.name) || !site.build;
row.watch.disabled = pending.has(site.name) || !site.watch;
row.watch.textContent = s.watching ? 'stop watching' : 'watch';
row.cancel.disabled = pending.has(site.name) || (!s.pid && !s.queued && !s.watching);
row.build.onclick = () => control(site, 'build');
row.watch.onclick = () => control(site, s.watching ? 'watch off' : 'watch on');
row.cancel.onclick = () => control(site, 'cancel');
}
function listenerRow(item, sites) {
const tr = element('tr'), name = element('td');
if (item.protocol !== 'unknown') {
const site = item.service === '9web' && sites.find(s => s.port === item.port);
const a = element('a', site ? site.title : item.service); a.href = url(item.address, item.port, item.protocol); name.append(a);
} else name.textContent = item.process || 'Unknown';
const address = element('td', `${item.address.includes(':') ? `[${item.address}]` : item.address}:${item.port}${item.local ? ' · local only' : ''}`);
address.className = 'listener-address';
tr.append(name, address, element('td', item.pid ? `${item.process || ''} (${item.pid})` : 'Unavailable'), element('td', item.evidence));
return tr;
}
async function refresh(showCount = true) {
if (active === 'files' || document.hidden) return;
request?.abort(); request = new AbortController();
const ticket = ++revision, view = active;
$(`${view}-reload`).disabled = true;
try {
const r = await fetch('/_cloud9/catalog.json', { signal: request.signal });
if (!r.ok) throw new Error(`Unable to load ${view}: HTTP ${r.status}`);
const data = await r.json();
if (ticket !== revision || active !== view) return;
namespace = data.namespace || '';
if (view === 'sites') {
const names = new Set(data.sites.map(s => s.name));
for (const [name, row] of rows) if (!names.has(name)) { row.tr.remove(); rows.delete(name); }
for (const site of data.sites) updateSite(site);
$('sites-empty').hidden = data.sites.length !== 0;
if (showCount) status(view, `${data.sites.length} ${data.sites.length === 1 ? 'site' : 'sites'} · generated files are served continuously`);
} else {
const known = data.listeners.filter(x => x.protocol !== 'unknown'), unknown = data.listeners.filter(x => x.protocol === 'unknown');
$('services-entries').replaceChildren(...known.map(x => listenerRow(x, data.sites)));
$('other-listeners-entries').replaceChildren(...unknown.map(x => listenerRow(x, data.sites)));
$('services-empty').hidden = known.length !== 0;
$('other-listeners-title').textContent = `Other listeners (${unknown.length})`;
status(view, data.discovery_error || `${known.length} HTTP services · refreshed every 5 seconds`, Boolean(data.discovery_error));
}
} catch (error) { if (error.name !== 'AbortError' && ticket === revision) status(view, error.message, true); }
finally { if (ticket === revision) $(`${view}-reload`).disabled = false; }
}
function show(view) {
clearInterval(timer); request?.abort(); revision++;
active = view;
for (const name of ['sites', 'services']) $(`${name}-reload`).disabled = false;
if (view !== 'files') { refresh(); timer = setInterval(refresh, 5000); }
}
for (const name of ['sites', 'services']) $(`${name}-reload`).onclick = () => refresh();
document.addEventListener('visibilitychange', () => { if (!document.hidden) refresh(); });
return { show };
}
|