Files
TREK/server/src/nest/plugins/host/rpc/db.rpc.ts
T
Maurice 6680bc7c74 Split PluginsModule into four, and gate coverage per domain
PluginsModule was 21 controllers, 24 domain imports and 10 providers in one
class, which made it the hub most of the Nest graph ran through: AdminModule
imported it for a single cascade-disable call and inherited all of it. It is now
PluginsRuntimeModule (supervisor, capability router, hook contracts),
PluginContributionsModule (the 14 read-only hook controllers, moved to
contributions/), PluginOAuthModule (the credential leaf, moved to oauth/) and a
composition root that keeps the install and delivery controllers. AdminModule
imports the runtime alone.

The three plugin files that reached for the db singleton alongside the injected
DatabaseService no longer do. plugin-host-state and signature-status take the
connection from the caller, and the three per-user settings functions that were
bare exports 'so the RPC host wiring can call them directly' are now
PluginUserSettingsService - the host wiring is providers, so it injects them.

Coverage moves from one repo-wide 80% to a per-domain ratchet. The single number
let a well-covered domain subsidise a thin one: booking-import sits at 50% and
integrations at 20%, and the aggregate still cleared the bar, so either could
have lost another ten points unnoticed. Each domain now carries its own measured
floor, regenerated by scripts/coverage-thresholds.mjs.

Two test-side findings on the way:

expect(undefined).toContain(SomeClass) PASSES in this vitest version - it only
throws for a string needle. Every '<X> is listed in its module providers' guard
was written that way, so all twelve of them silently stopped guarding the moment
their class moved. They go through expectRegisteredProvider now, which asserts
the array shape first.

The atlas region test paid for the one-off decompression of the multi-MB admin1
bundle inside its own 5s budget, which is why it failed under load and passed
alone. The bundle is warmed in beforeAll instead.
2026-08-09 18:44:59 +02:00

68 lines
3.0 KiB
TypeScript

import { PluginController, PluginMethod, PluginOpenMethod } from '../rpc-kit/decorators';
import { asArgs, asTxOps, str } from '../rpc-params';
import type { PluginRpcContext } from '../rpc-kit/types';
import { PluginUserSettingsService } from '../../plugin-user-settings.service';
/**
* A plugin's OWN sqlite, plus the three methods the router registers unconditionally.
*
* Every method reads `ctx.data` fresh. It must never be captured in a field or a
* closure: the handle is per plugin, and disable()/re-enable builds a new host while
* the old one's dispose() may still be closing the previous handle. A captured one
* would be both a stale-handle bug and, in a singleton like this, a cross-plugin data
* leak, since one instance serves every plugin.
*/
@PluginController()
export class DbRpc {
constructor(private readonly settings: PluginUserSettingsService) {}
@PluginMethod('db.query', { permission: 'db:own' })
query(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return ctx.data.query(str(params.sql, 'sql'), asArgs(params.args));
}
@PluginMethod('db.exec', { permission: 'db:own' })
exec(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return ctx.data.exec(str(params.sql, 'sql'), asArgs(params.args));
}
@PluginMethod('db.migrate', { permission: 'db:own' })
migrate(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return ctx.data.migrate(str(params.id, 'id'), str(params.sql, 'sql'));
}
@PluginMethod('db.tx', { permission: 'db:own' })
tx(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return ctx.data.tx(asTxOps(params.ops));
}
/**
* Inter-plugin call. No permission of its own: the router authorises it against
* the declared dependency edge and the target's `provides` allowlist. The acting
* user is forwarded so the target's export runs as the caller's user.
*/
@PluginOpenMethod('plugins.call')
callPlugin(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
return ctx.plugins.call(str(params.targetId, 'targetId'), str(params.fn, 'fn'), params.args, ctx.actingUserId);
}
/** Same deal: the router checks the emitter's `emits` allowlist. */
@PluginOpenMethod('events.emit')
emitEvent(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
ctx.plugins.emit(str(params.event, 'event'), params.payload);
return { ok: true };
}
/**
* The plugin's OWN per-user settings, decrypted host-side. Unconditional and not
* cross-tenant: it only ever returns THIS plugin's config for the acting user. A
* userless context (a job or onLoad) has no user, so it returns undefined and the
* plugin falls back to ctx.config.
*/
@PluginOpenMethod('settings.get')
getSetting(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
if (ctx.actingUserId === undefined) return { value: undefined };
return { value: this.settings.readOne(ctx.pluginId, ctx.actingUserId, str(params.key, 'key')) };
}
}