更新 lib/routes.js

This commit is contained in:
QQ 2026-11-11 11:07:25 +08:00
parent bdf63565d8
commit 9aa0af8e51
1 changed files with 110 additions and 0 deletions

View File

@ -1,5 +1,7 @@
import { describeError, isSecureLoginTarget } from "./api.js";
import { renderConfigPage } from "./page.js";
import { SkillAdminError } from "./skill-admin.js";
import { renderSkillsPage } from "./skill-page.js";
/** Route constants — kept in one place so the client half can reuse them. */
export const ROUTES = {
page: '/cloud-memory',
@ -8,6 +10,7 @@ export const ROUTES = {
login: '/cloud-memory/login',
test: '/cloud-memory/test',
flush: '/cloud-memory/flush',
skills: '/cloud-memory/skills',
};
/** Largest request body we accept (a settings patch is tiny). */
const MAX_BODY_BYTES = 64 * 1024;
@ -296,6 +299,108 @@ async function serveLogin(deps, req, res) {
},
});
}
/** HTTP status for one skill-admin failure code. */
function adminStatus(code) {
switch (code) {
case 'not_found': return 404;
case 'invalid_name':
case 'invalid_url': return 400;
case 'collision':
case 'sync_running': return 409;
case 'no_skills_found': return 422;
case 'git_failed': return 502;
default: return 500;
}
}
/**
* The skill management surface, one prefix route:
* `GET /cloud-memory/skills` the management page
* `GET /cloud-memory/skills/api` inventory JSON (skills + trash)
* `POST /cloud-memory/skills/toggle` { folder, enabled }
* `POST /cloud-memory/skills/remove` { folder }
* `POST /cloud-memory/skills/restore` { id }
* `POST /cloud-memory/skills/install` { url }
*/
async function serveSkills(deps, req, res) {
if (deps.admin === undefined) {
fail(res, 404, 'skills-admin-unavailable');
return;
}
const admin = deps.admin;
const suffix = (req.url ?? '').split('?')[0]?.slice(ROUTES.skills.length) ?? '';
if (suffix === '' || suffix === '/') {
if (req.method !== 'GET' && req.method !== 'HEAD') {
fail(res, 405, 'method-not-allowed', true);
return;
}
const html = renderSkillsPage({ version: deps.version });
res.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-store',
'content-length': Buffer.byteLength(html),
});
if (req.method === 'HEAD')
res.end();
else
res.end(html);
return;
}
if (suffix === '/api') {
if (req.method !== 'GET') {
fail(res, 405, 'method-not-allowed');
return;
}
json(res, 200, { ok: true, ...(await admin.list()) });
return;
}
const actions = {
'/toggle': async (body) => admin.setEnabled(body.folder, body.enabled === true),
'/remove': async (body) => ({ entry: await admin.remove(body.folder) }),
'/restore': async (body) => admin.restore(body.id),
'/install': async (body) => admin.installFromGit(body.url),
};
const action = actions[suffix];
if (action === undefined) {
fail(res, 404, 'unknown-skill-route');
return;
}
if (req.method !== 'POST') {
fail(res, 405, 'method-not-allowed');
return;
}
if (!sameOrigin(req)) {
fail(res, 403, 'origin-rejected');
return;
}
const type = (req.headers['content-type'] ?? '').toLowerCase();
if (!type.startsWith('application/json')) {
fail(res, 415, 'json-required');
return;
}
let body;
try {
body = JSON.parse(await readBody(req));
}
catch {
fail(res, 400, 'invalid-json');
return;
}
if (body === null || typeof body !== 'object' || Array.isArray(body)) {
fail(res, 400, 'invalid-body');
return;
}
try {
json(res, 200, { ok: true, ...(await action(body)) });
}
catch (error) {
if (error instanceof SkillAdminError) {
json(res, adminStatus(error.code), { ok: false, code: error.code, error: error.message });
}
else {
json(res, 500, { ok: false, code: 'internal', error: error instanceof Error ? error.message : String(error) });
}
}
}
/** Build the exact-route table registered with the host web server. */
export function createRoutes(deps) {
return [
@ -329,5 +434,10 @@ export function createRoutes(deps) {
path: ROUTES.flush,
handler: (req, res) => serveFlush(deps, req, res),
},
{
kind: 'prefix',
path: ROUTES.skills,
handler: (req, res) => serveSkills(deps, req, res),
},
];
}