theseus/bundled-addons/pithos/core/admin.js
Local Dev 4ad95b3c7a Theseus: bundle the Pithos add-on
Ships Pithos (the s3d control panel) as a built-in extension: the dock
menu opens it in a tab served from a loopback port, and "Stop s3d"
shuts the gateway down. Generated by Pithos/scripts/build-theseus-addon.mjs.

yaml is vendored under vendor/yaml/lib rather than node_modules/ or
dist/, because Theseus git-ignores both and a clean-worktree release
build would otherwise ship the add-on without its only dependency.
2026-10-03 19:03:05 +02:00

34 lines
1.2 KiB
JavaScript

// s3d's admin API (Basic auth, empty user): upload stats, flush, snapshots.
function authHeader(password) {
return 'Basic ' + Buffer.from(':' + password).toString('base64');
}
async function adminFetch(cfg, method, route, { timeout = 10_000 } = {}) {
if (!cfg.adminAddress || !cfg.adminPassword) throw new Error('adminAddress and adminPassword must be set in s3d.yml');
const ctl = new AbortController();
const timer = timeout ? setTimeout(() => ctl.abort(), timeout) : null;
try {
const res = await fetch(`http://${cfg.adminAddress}${route}`, {
method, headers: { authorization: authHeader(cfg.adminPassword) }, signal: ctl.signal,
});
const text = await res.text();
if (!res.ok) throw new Error(`admin API ${route}: ${res.status} ${text.trim()}`);
return text;
} finally {
if (timer) clearTimeout(timer);
}
}
export async function uploadStats(cfg) {
return JSON.parse(await adminFetch(cfg, 'GET', '/stats/uploads'));
}
// Blocks until every pending object is on Sia, so no timeout.
export async function flush(cfg) {
await adminFetch(cfg, 'POST', '/objects/flush', { timeout: 0 });
}
export async function snapshot(cfg) {
return adminFetch(cfg, 'POST', '/snapshots', { timeout: 0 });
}