mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
feat(plugins): activation, HTTP route proxy + instance settings (M2)
Wires the isolated runtime into TREK. Admins can now activate a plugin from the panel and its HTTP routes work end to end, still behind the kill switch. - PluginRuntimeService owns the supervisor: activate spawns the child with its granted permissions + decrypted instance config, deactivate kills it, status and errors are persisted to the plugins / plugin_error_log tables, and active plugins are booted on startup (OnModuleInit). - Bidirectional RPC: the child now handles host→child invokes (routes/jobs) and reports its declared routes on load; the supervisor gained invoke()/routesOf(). - /api/plugins/:id/* proxy controller — a single static route that matches the plugin's declared routes, enforces per-route auth (auth:false routes are public for OAuth callbacks/webhooks), forwards only a whitelisted request view (never the session cookie), and strips unsafe response headers. - Admin endpoints: POST :id/activate, POST :id/deactivate, GET/PUT :id/config — instance settings with secret fields encrypted (apiKeyCrypto) and masked. - Kill switch moved to its own module so it never collides with test config mocks. Photo/calendar hook consumers are deferred to a later slice.
This commit is contained in:
@@ -51,7 +51,11 @@ async function boot(config: Record<string, unknown>): Promise<void> {
|
||||
def = mod && mod.default ? (mod.default as PluginDefinition) : (mod as PluginDefinition);
|
||||
ctx = createPluginContext(pluginId, config, transport);
|
||||
if (typeof def.onLoad === 'function') await def.onLoad(ctx);
|
||||
send({ k: 'evt', topic: 'loaded', data: {} });
|
||||
// Report the declared routes (with their index = routeId) and job ids so the
|
||||
// host can proxy HTTP and schedule jobs without re-parsing the manifest.
|
||||
const routes = (def.routes ?? []).map((r, i) => ({ i, method: r.method, path: r.path, auth: r.auth !== false }));
|
||||
const jobs = (def.jobs ?? []).map((j) => j.id);
|
||||
send({ k: 'evt', topic: 'loaded', data: { routes, jobs } });
|
||||
// An immediate first heartbeat confirms liveness without waiting a full interval.
|
||||
send({ k: 'evt', topic: 'heartbeat', data: { rss: process.memoryUsage().rss } });
|
||||
} catch (e) {
|
||||
@@ -59,6 +63,37 @@ async function boot(config: Record<string, unknown>): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle a host→child request: run a declared route or job with the plugin ctx. */
|
||||
async function handleInvoke(req: { id: string; method: string; params: Record<string, unknown> }): Promise<void> {
|
||||
const respond = (ok: boolean, payload: unknown) =>
|
||||
send(
|
||||
ok
|
||||
? { k: 'res', id: req.id, ok: true, result: payload }
|
||||
: { k: 'res', id: req.id, ok: false, error: { code: 'PLUGIN_ERROR', message: String(payload) } },
|
||||
);
|
||||
try {
|
||||
if (!def || !ctx) throw new Error('plugin not loaded');
|
||||
if (req.method === 'invoke.route') {
|
||||
const routeId = req.params.routeId as number;
|
||||
const route = def.routes?.[routeId];
|
||||
if (!route) throw new Error(`no route ${routeId}`);
|
||||
const pluginReq = req.params.req as Parameters<NonNullable<typeof route.handler>>[0];
|
||||
const result = await route.handler(pluginReq, ctx);
|
||||
respond(true, result);
|
||||
} else if (req.method === 'invoke.job') {
|
||||
const jobId = req.params.jobId as string;
|
||||
const job = def.jobs?.find((j) => j.id === jobId);
|
||||
if (!job) throw new Error(`no job ${jobId}`);
|
||||
await job.handler(ctx);
|
||||
respond(true, { ok: true });
|
||||
} else {
|
||||
respond(false, `unknown invoke ${req.method}`);
|
||||
}
|
||||
} catch (e) {
|
||||
respond(false, errMsg(e));
|
||||
}
|
||||
}
|
||||
|
||||
async function shutdown(): Promise<void> {
|
||||
try {
|
||||
if (def && typeof def.onUnload === 'function' && ctx) await def.onUnload(ctx);
|
||||
@@ -72,6 +107,11 @@ async function shutdown(): Promise<void> {
|
||||
process.on('message', (raw: unknown) => {
|
||||
const msg = raw as Envelope;
|
||||
if (!msg || typeof msg !== 'object') return;
|
||||
if (msg.k === 'req') {
|
||||
// A host→child invoke (route / job).
|
||||
void handleInvoke({ id: msg.id, method: msg.method, params: (msg.params ?? {}) as Record<string, unknown> });
|
||||
return;
|
||||
}
|
||||
if (msg.k === 'res') {
|
||||
const p = pending.get(msg.id);
|
||||
if (!p) return;
|
||||
|
||||
Reference in New Issue
Block a user