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

A Buffer crosses the plugin IPC as its JSON form, so db.query, db.exec and db.tx now turn that form back into the bytes before binding, as the plugin docs promise.
This commit is contained in:
Maurice
2026-11-23 15:36:29 +01:00
parent 10f0cb7d04
commit ac5c91bcaf
3 changed files with 52 additions and 2 deletions
+15 -1
View File
@@ -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');
}
@@ -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);
+13 -1
View File
@@ -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);
});
});