- Add streamd serve start|stop|restart|status|update with auto-written streamd.yaml - Panel modules: STREAMD_MODULES_DIR for describe, YAML channel fallback, textarea value fix - Per-stream WireGuard/SOCKS egress profiles + Settings UI - Drop drm-* from release builds; track streamd-bg.sh / streamd-update.sh wrappers - Docs: streamd-server-setup lifecycle and update flow
314 lines
No EOL
9.2 KiB
JavaScript
314 lines
No EOL
9.2 KiB
JavaScript
// Provider modules: drag-drop zip upload, list, YAML editor.
|
|
|
|
import {
|
|
el, pill, toast, confirmDialog, fmtDateTime, clear, copyText,
|
|
} from "../util.js";
|
|
import { canEdit, subscribe } from "../store.js";
|
|
import { api } from "../api.js";
|
|
|
|
function shortHash(h) {
|
|
if (!h) return "—";
|
|
return h.slice(0, 10);
|
|
}
|
|
|
|
function fmtBytes(n) {
|
|
if (!n) return "—";
|
|
if (n < 1024) return `${n} B`;
|
|
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
|
|
return `${(n / (1024 * 1024)).toFixed(1)} MB`;
|
|
}
|
|
|
|
export function modulesView(root, editName) {
|
|
const stack = el("div.stack");
|
|
root.append(stack);
|
|
|
|
let unsub = () => {};
|
|
let disposed = false;
|
|
|
|
async function load() {
|
|
if (disposed) return;
|
|
clear(stack);
|
|
if (editName) {
|
|
await mountEditor(stack, editName, () => {
|
|
location.hash = "#/modules";
|
|
});
|
|
return;
|
|
}
|
|
mountList(stack, load);
|
|
}
|
|
|
|
load();
|
|
unsub = subscribe((_s, reason) => {
|
|
if (reason === "modules_changed" && !editName) load();
|
|
});
|
|
|
|
return () => {
|
|
disposed = true;
|
|
unsub();
|
|
if (stack._modulesUnload) stack._modulesUnload();
|
|
};
|
|
}
|
|
|
|
function mountList(stack, reload) {
|
|
const body = el("tbody");
|
|
const drop = el("div.dropzone", {
|
|
tabindex: "0",
|
|
role: "button",
|
|
"aria-label": "Upload module zip",
|
|
},
|
|
el("div.dropzone-title", { text: "Drop a module .zip here" }),
|
|
el("div.dropzone-sub", {
|
|
text: "Release assets such as bbc.zip from modules-v* tags. Fill secrets in the YAML editor after upload.",
|
|
}),
|
|
el("div.row", { style: { marginTop: "10px", justifyContent: "center" } },
|
|
el("button.sm", {
|
|
type: "button",
|
|
text: "Choose file…",
|
|
onclick: (ev) => {
|
|
ev.stopPropagation();
|
|
fileInput.click();
|
|
},
|
|
}),
|
|
),
|
|
);
|
|
const fileInput = el("input", {
|
|
type: "file",
|
|
accept: ".zip,application/zip",
|
|
hidden: true,
|
|
onchange: async () => {
|
|
const f = fileInput.files && fileInput.files[0];
|
|
fileInput.value = "";
|
|
if (f) await uploadFile(f, reload);
|
|
},
|
|
});
|
|
|
|
if (canEdit()) {
|
|
wireDropzone(drop, fileInput, reload);
|
|
stack.append(el("div.card", drop, fileInput));
|
|
}
|
|
|
|
stack.append(
|
|
el("div.card",
|
|
el("div.card-head",
|
|
el("h2", { text: "Installed modules" }),
|
|
el("span.spacer"),
|
|
el("button.ghost.sm", { text: "Reload", onclick: () => reload() }),
|
|
),
|
|
el("p.hint", {
|
|
text: "Agents pull package.zip and module.yaml from this panel. " +
|
|
"Edit YAML here so every agent gets the same config.",
|
|
}),
|
|
el("div.table-wrap", el("table",
|
|
el("thead", el("tr",
|
|
el("th", { text: "Name" }),
|
|
el("th", { text: "YAML" }),
|
|
el("th", { text: "Package" }),
|
|
el("th", { text: "Arches" }),
|
|
el("th", { text: "Size" }),
|
|
el("th", { text: "Updated" }),
|
|
el("th", { text: "" }),
|
|
)),
|
|
body,
|
|
)),
|
|
),
|
|
);
|
|
|
|
(async () => {
|
|
try {
|
|
const d = await api.modules();
|
|
const list = d.modules || [];
|
|
clear(body);
|
|
if (!list.length) {
|
|
body.append(el("tr", el("td.empty", {
|
|
colspan: "7",
|
|
text: canEdit()
|
|
? "No modules yet — drop a release zip above."
|
|
: "No modules uploaded yet.",
|
|
})));
|
|
return;
|
|
}
|
|
for (const m of list) {
|
|
body.append(moduleRow(m, reload));
|
|
}
|
|
} catch (err) {
|
|
clear(body);
|
|
body.append(el("tr", el("td.empty", { colspan: "7", text: err.message })));
|
|
}
|
|
})();
|
|
}
|
|
|
|
function moduleRow(m, reload) {
|
|
const arches = (m.arches || []).join(", ") || "—";
|
|
const updated = m.yaml_updated_at || m.uploaded_at || "";
|
|
return el("tr",
|
|
el("td",
|
|
el("div", { text: m.name }),
|
|
el("div.sub.mono", { text: shortHash(m.package_sha256 || m.yaml_sha256) }),
|
|
),
|
|
el("td", m.has_yaml
|
|
? el("span", { title: m.yaml_sha256 },
|
|
pill("ok", "ok"),
|
|
el("span.sub.mono", { text: " " + shortHash(m.yaml_sha256) }),
|
|
)
|
|
: pill("missing", "down")),
|
|
el("td", m.has_package
|
|
? el("span", { title: m.package_sha256 },
|
|
pill("ok", "ok"),
|
|
el("span.sub.mono", { text: " " + shortHash(m.package_sha256) }),
|
|
)
|
|
: pill("missing", "warn")),
|
|
el("td.mono", { text: arches }),
|
|
el("td", { text: fmtBytes(m.size_bytes) }),
|
|
el("td", { text: updated ? fmtDateTime(updated) : "—" }),
|
|
el("td.actions", el("div.row",
|
|
canEdit() ? el("button.tiny.ghost", {
|
|
text: "Edit YAML",
|
|
onclick: () => { location.hash = `#/modules/${encodeURIComponent(m.name)}`; },
|
|
}) : null,
|
|
canEdit() ? el("button.tiny.danger", {
|
|
text: "Delete",
|
|
onclick: async () => {
|
|
if (!(await confirmDialog(`Delete module ${m.name}?`,
|
|
"Agents will stop receiving this module on the next sync.", "Delete"))) return;
|
|
try {
|
|
await api.deleteModule(m.name);
|
|
toast(`Deleted ${m.name}`);
|
|
reload();
|
|
} catch (err) {
|
|
toast(err.message, true);
|
|
}
|
|
},
|
|
}) : null,
|
|
)),
|
|
);
|
|
}
|
|
|
|
function wireDropzone(drop, fileInput, reload) {
|
|
const setActive = (on) => drop.classList.toggle("active", on);
|
|
drop.addEventListener("click", () => fileInput.click());
|
|
drop.addEventListener("keydown", (ev) => {
|
|
if (ev.key === "Enter" || ev.key === " ") {
|
|
ev.preventDefault();
|
|
fileInput.click();
|
|
}
|
|
});
|
|
drop.addEventListener("dragenter", (ev) => { ev.preventDefault(); setActive(true); });
|
|
drop.addEventListener("dragover", (ev) => { ev.preventDefault(); setActive(true); });
|
|
drop.addEventListener("dragleave", () => setActive(false));
|
|
drop.addEventListener("drop", async (ev) => {
|
|
ev.preventDefault();
|
|
setActive(false);
|
|
const f = ev.dataTransfer && ev.dataTransfer.files && ev.dataTransfer.files[0];
|
|
if (f) await uploadFile(f, reload);
|
|
});
|
|
}
|
|
|
|
async function uploadFile(file, reload) {
|
|
if (!canEdit()) {
|
|
toast("Operator role required to upload", true);
|
|
return;
|
|
}
|
|
const lower = (file.name || "").toLowerCase();
|
|
if (!lower.endsWith(".zip")) {
|
|
toast("Upload a .zip module package", true);
|
|
return;
|
|
}
|
|
toast(`Uploading ${file.name}…`);
|
|
try {
|
|
// Pass stem of rte.zip → "rte" so flat release zips never become "darwin-arm64".
|
|
let hint = "";
|
|
const base = (file.name || "").replace(/\.zip$/i, "").trim();
|
|
if (/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$/.test(base)) hint = base;
|
|
const res = await api.uploadModuleZip(file, hint || undefined);
|
|
const name = (res.module && res.module.name) || hint || file.name;
|
|
toast(`Uploaded ${name}`);
|
|
reload();
|
|
} catch (err) {
|
|
toast(err.message || "Upload failed", true);
|
|
}
|
|
}
|
|
|
|
async function mountEditor(stack, name, onDone) {
|
|
let data;
|
|
try {
|
|
data = await api.module(name, true);
|
|
} catch (err) {
|
|
stack.append(el("div.card",
|
|
el("h2", { text: name }),
|
|
el("p.msg.err", { text: err.message }),
|
|
el("button.ghost", { text: "Back", onclick: onDone }),
|
|
));
|
|
return;
|
|
}
|
|
const m = data.module || { name };
|
|
const ta = el("textarea.module-yaml", {
|
|
value: data.yaml || "",
|
|
spellcheck: "false",
|
|
rows: "28",
|
|
});
|
|
let saved = data.yaml || "";
|
|
const dirty = () => ta.value !== saved;
|
|
|
|
const form = el("div.card",
|
|
el("div.card-head",
|
|
el("h2", { text: `module.yaml — ${m.name}` }),
|
|
el("span.spacer"),
|
|
el("button.ghost.sm", { text: "Back", onclick: onDone }),
|
|
),
|
|
el("p.hint", {
|
|
text: "Secrets and channel config live here. Saving updates every agent on the next sync (or immediately over websocket).",
|
|
}),
|
|
el("div.row", { style: { marginBottom: "8px", gap: "8px", flexWrap: "wrap" } },
|
|
m.yaml_sha256 ? el("span.sub.mono", { text: `yaml ${shortHash(m.yaml_sha256)}` }) : null,
|
|
m.package_sha256 ? el("span.sub.mono", { text: `pkg ${shortHash(m.package_sha256)}` }) : null,
|
|
(m.arches || []).length ? el("span.sub", { text: (m.arches || []).join(", ") }) : null,
|
|
),
|
|
ta,
|
|
el("div.row.end", { style: { marginTop: "14px" } },
|
|
el("button.ghost", {
|
|
text: "Copy",
|
|
onclick: () => copyText(ta.value, "YAML"),
|
|
}),
|
|
el("button.ghost", {
|
|
text: "Revert",
|
|
onclick: () => {
|
|
ta.value = saved;
|
|
},
|
|
}),
|
|
el("button", {
|
|
text: "Save YAML",
|
|
onclick: async () => {
|
|
if (!canEdit()) {
|
|
toast("Operator role required", true);
|
|
return;
|
|
}
|
|
try {
|
|
const res = await api.saveModuleYAML(m.name, ta.value);
|
|
saved = ta.value;
|
|
toast("YAML saved");
|
|
if (res.module) {
|
|
m.yaml_sha256 = res.module.yaml_sha256;
|
|
}
|
|
} catch (err) {
|
|
toast(err.message, true);
|
|
}
|
|
},
|
|
}),
|
|
),
|
|
);
|
|
stack.append(form);
|
|
|
|
const onBeforeUnload = (ev) => {
|
|
if (dirty()) {
|
|
ev.preventDefault();
|
|
ev.returnValue = "";
|
|
}
|
|
};
|
|
window.addEventListener("beforeunload", onBeforeUnload);
|
|
// Caller teardown should remove this; modulesView dispose covers navigation away.
|
|
const prev = stack._modulesUnload;
|
|
stack._modulesUnload = () => {
|
|
window.removeEventListener("beforeunload", onBeforeUnload);
|
|
if (prev) prev();
|
|
};
|
|
} |