theseus/bundled-addons/pithos/core/admin.js

35 lines
1.2 KiB
JavaScript
Raw Normal View History

// 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 });
}