mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 15:40:06 +00:00
feat(plugins): store plugin data on the engine TREK runs on
PLUGIN_DATA binds the plugin data backend to the engine (the same
{ sqlite, postgres }[engine.id] binding the backup port uses): the SQLite
file per plugin, unchanged, or the Postgres schema per plugin. The host
opens a plugin's store through it and an uninstall that deletes data erases
through it, which drops the schema on Postgres. Nothing outside db/engine
asks which engine runs.
A plugin's SQL is passed through as written, so a plugin needs to know the
dialect: db.engine (db:own) answers 'sqlite' or 'postgres', and the SDK
exposes it as ctx.db.engine(), in the mock host (dbEngine option) and in
dev. The plugin facts are regenerated and the SDK goes to 1.10.0.
The Postgres lane's PGPLUG suite installs, uses and erases plugin data on
Postgres and shows that a plugin reaches neither TREK's users table nor
another plugin's schema.
This commit is contained in:
@@ -28,6 +28,7 @@ import type { LlmConfigResolver } from '../../../src/nest/llm-parse/llm-config.r
|
||||
import type { NotificationsService } from '../../../src/nest/notifications/notifications.service';
|
||||
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
|
||||
import { verifyChain } from '../../../src/nest/plugins/host/plugin-audit';
|
||||
import { SqlitePluginDataBackend } from '../../../src/nest/plugins/host/plugin-data.backend';
|
||||
import { getPluginDataDb, closePluginDataDb } from '../../../src/nest/plugins/host/plugin-host-state';
|
||||
import { PluginRpcHostFactory, type PluginCallRouter } from '../../../src/nest/plugins/host/plugin-rpc-host.factory';
|
||||
import { DbRpc } from '../../../src/nest/plugins/host/rpc/db.rpc';
|
||||
@@ -416,7 +417,8 @@ const registry = createTestPluginRegistry([
|
||||
uow,
|
||||
),
|
||||
]);
|
||||
const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService);
|
||||
const sqliteData = new SqlitePluginDataBackend();
|
||||
const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService, sqliteData);
|
||||
const stubRouter: PluginCallRouter = { callPlugin: async () => undefined, emitPluginEvent: async () => {} };
|
||||
const makeHost = (id: string, ...perms: string[]) => factory.create(id, new Set(perms), stubRouter);
|
||||
/**
|
||||
@@ -457,7 +459,7 @@ afterAll(() => {
|
||||
|
||||
describe("DbRpc — the plugin's own sqlite", () => {
|
||||
it('HOSTRPC-001 caches one data db per plugin id', () => {
|
||||
expect(getPluginDataDb('wired')).toBe(getPluginDataDb('wired'));
|
||||
expect(getPluginDataDb('wired', sqliteData)).toBe(getPluginDataDb('wired', sqliteData));
|
||||
});
|
||||
|
||||
it('HOSTRPC-002 a granted db:own call runs against the plugin db', async () => {
|
||||
@@ -482,13 +484,19 @@ describe("DbRpc — the plugin's own sqlite", () => {
|
||||
closePluginDataDb('wtx');
|
||||
});
|
||||
|
||||
it('HOSTRPC-004 without db:own not one of the four is reachable', async () => {
|
||||
it('HOSTRPC-004 without db:own not one of the five is reachable', async () => {
|
||||
const host = makeHost('wnogrant');
|
||||
for (const method of ['db.query', 'db.exec', 'db.migrate', 'db.tx']) {
|
||||
for (const method of ['db.query', 'db.exec', 'db.migrate', 'db.tx', 'db.engine']) {
|
||||
expect((await call(host, method, { sql: 'SELECT 1', id: '1', ops: [] })).error?.code).toBe('PERMISSION_DENIED');
|
||||
}
|
||||
});
|
||||
|
||||
it('HOSTRPC-004b db.engine names the engine the plugin SQL runs on', async () => {
|
||||
const host = makeHost('wengine', 'db:own');
|
||||
expect((await call(host, 'db.engine', {})).result).toEqual({ engine: 'sqlite' });
|
||||
closePluginDataDb('wengine');
|
||||
});
|
||||
|
||||
it("HOSTRPC-005 two plugins never see each other's tables", async () => {
|
||||
// The regression this guards: DbRpc is a SINGLETON that serves every plugin, so a
|
||||
// handle captured in a field or a closure would hand plugin B the rows of whichever
|
||||
@@ -508,20 +516,20 @@ describe("DbRpc — the plugin's own sqlite", () => {
|
||||
});
|
||||
|
||||
it('HOSTRPC-006 a handle closed WITHOUT eviction is recreated, not reused dead', () => {
|
||||
const a = getPluginDataDb('closedcache');
|
||||
const a = getPluginDataDb('closedcache', sqliteData);
|
||||
expect(a.isOpen()).toBe(true);
|
||||
// The supervisor's dispose() path: close the handle but leave it cached.
|
||||
a.close();
|
||||
const b = getPluginDataDb('closedcache');
|
||||
const b = getPluginDataDb('closedcache', sqliteData);
|
||||
expect(b).not.toBe(a);
|
||||
expect(b.isOpen()).toBe(true);
|
||||
closePluginDataDb('closedcache');
|
||||
});
|
||||
|
||||
it('HOSTRPC-007 closePluginDataDb drops the cached handle', () => {
|
||||
const a = getPluginDataDb('transient');
|
||||
const a = getPluginDataDb('transient', sqliteData);
|
||||
closePluginDataDb('transient');
|
||||
const b = getPluginDataDb('transient');
|
||||
const b = getPluginDataDb('transient', sqliteData);
|
||||
expect(a).not.toBe(b);
|
||||
closePluginDataDb('transient');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user