Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(plugins): bind a Buffer a plugin passes to its own database as bytes

Mauriceac5c91bcaff1
diff --git a/server/src/nest-rpc/rpc-params.ts b/server/src/nest-rpc/rpc-params.ts
index 7c4c3ce54..c71c41594 100644
--- a/server/src/nest-rpc/rpc-params.ts
+++ b/server/src/nest-rpc/rpc-params.ts
@@ -39,9 +39,23 @@ export const str = (v: unknown, name: string): string => {
return v;
};
+/**
+ * A Buffer a plugin binds crosses the JSON IPC as its toJSON form,
+ * { type: 'Buffer', data: [bytes] }; it is bound as those bytes again.
+ */
+function reviveBuffer(v: unknown): unknown {
+ if (v === null || typeof v !== 'object' || Array.isArray(v)) return v;
+ const o = v as Record<string, unknown>;
+ if (o.type !== 'Buffer' || !Array.isArray(o.data) || Object.keys(o).length !== 2) return v;
+ const bytes: unknown[] = o.data;
+ return bytes.every((b) => Number.isInteger(b) && (b as number) >= 0 && (b as number) <= 255)
+ ? Buffer.from(bytes as number[])
+ : v;
+}
+
export function asArgs(v: unknown): unknown[] {
if (v == null) return [];
- if (Array.isArray(v)) return v;
+ if (Array.isArray(v)) return v.map(reviveBuffer);
throw new BadParams('args must be an array');
}
diff --git a/server/tests/integration/plugins/supervisor.test.ts b/server/tests/integration/plugins/supervisor.test.ts
index 5973a9c94..10e4ba71a 100644
--- a/server/tests/integration/plugins/supervisor.test.ts
+++ b/server/tests/integration/plugins/supervisor.test.ts
@@ -142,6 +142,30 @@ describe('PluginSupervisor — isolated runtime', () => {
expect(events.some((e) => e.topic === '__log')).toBe(true);
});
+ it('binds a Buffer a plugin passes to ctx.db as its bytes across the fork, in query, exec and tx', async () => {
+ const events: Array<{ topic: string; data: unknown }> = [];
+ sup = makeSupervisor(events);
+ writePlugin(
+ 'binkeys',
+ `module.exports = {
+ async onLoad(ctx) {
+ await ctx.db.migrate('001', 'CREATE TABLE kv (k BLOB PRIMARY KEY, v TEXT)');
+ await ctx.db.exec('INSERT INTO kv (k, v) VALUES (?, ?)', Buffer.from([1, 0, 2]), 'exec');
+ await ctx.db.tx([{ sql: 'INSERT INTO kv (k, v) VALUES (?, ?)', args: [Buffer.from([3, 0, 4]), 'tx'] }]);
+ const rows = await ctx.db.query('SELECT v, hex(k) AS h FROM kv WHERE k IN (?, ?) ORDER BY v', Buffer.from([1, 0, 2]), Buffer.from([3, 0, 4]));
+ ctx.log.info('bin', { rows });
+ }
+ };`,
+ );
+
+ await sup.activate('binkeys', new Set(['db:own']), {});
+
+ expect(logMeta<{ rows: unknown[] }>(events, 'bin').rows).toEqual([
+ { v: 'exec', h: '010002' },
+ { v: 'tx', h: '030004' },
+ ]);
+ });
+
it('dispatches invoke.hook + invoke.event across the fork (provider hooks + event subscriptions)', async () => {
const events: Array<{ topic: string; data: unknown }> = [];
sup = makeSupervisor(events);
diff --git a/server/tests/unit/nest-rpc/rpc-params.test.ts b/server/tests/unit/nest-rpc/rpc-params.test.ts
index 0091403d0..76ce3624e 100644
--- a/server/tests/unit/nest-rpc/rpc-params.test.ts
+++ b/server/tests/unit/nest-rpc/rpc-params.test.ts
@@ -1,5 +1,5 @@
import { BadParams } from '../../../src/nest-rpc/rpc-errors';
-import { optionalRowIdParam, rowIdParam } from '../../../src/nest-rpc/rpc-params';
+import { asArgs, asTxOps, optionalRowIdParam, rowIdParam } from '../../../src/nest-rpc/rpc-params';
import { describe, expect, it } from 'vitest';
@@ -24,3 +24,15 @@ describe('rowIdParam', () => {
}
});
});
+
+describe('asArgs', () => {
+ it('RPCPARAM-004: a Buffer that crossed the JSON IPC is bound as its bytes again, in query args and tx ops', () => {
+ const wire = JSON.parse(JSON.stringify([Buffer.from([1, 0, 255]), 'k', { type: 'Buffer', data: [1, 300] }]));
+ const [bytes, key, odd] = asArgs(wire);
+ expect(Buffer.isBuffer(bytes) && [...bytes]).toEqual([1, 0, 255]);
+ expect(key).toBe('k');
+ expect(odd).toEqual({ type: 'Buffer', data: [1, 300] });
+ const [op] = asTxOps([{ sql: 'SELECT ?', args: wire }]);
+ expect(Buffer.isBuffer(op?.args?.[0])).toBe(true);
+ });
+});