feat(transport): introduce transport abstraction and socket-mode support for RustBridge

This commit is contained in:
2026-02-26 08:44:28 +00:00
parent 0c39e157c2
commit deda8cc4ee
14 changed files with 1064 additions and 150 deletions

View File

@@ -0,0 +1,124 @@
#!/usr/bin/env node
/**
* Mock "Rust daemon" for testing the SocketTransport and RustBridge socket mode.
* Creates a Unix socket server, accepts connections, and speaks the same
* JSON-over-newline IPC protocol as mock-rust-binary.mjs.
*
* Usage: node mock-socket-server.mjs <socket-path>
* Signals readiness by writing a JSON line to stdout: {"socketPath":"...","ready":true}
*/
import * as net from 'net';
import * as fs from 'fs';
const socketPath = process.argv[2];
if (!socketPath) {
process.stderr.write('Usage: mock-socket-server.mjs <socket-path>\n');
process.exit(1);
}
// Remove stale socket file
try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
/**
* Backpressure-aware write to a socket.
*/
function writeResponse(conn, data) {
const json = JSON.stringify(data) + '\n';
if (!conn.write(json)) {
conn.once('drain', () => {});
}
}
function handleLine(line, conn) {
let request;
try {
request = JSON.parse(line);
} catch {
return;
}
const { id, method, params } = request;
if (method === 'echo') {
writeResponse(conn, { id, success: true, result: params });
} else if (method === 'largeEcho') {
writeResponse(conn, { id, success: true, result: params });
} else if (method === 'error') {
writeResponse(conn, { id, success: false, error: 'Test error message' });
} else if (method === 'emitEvent') {
writeResponse(conn, { event: params.eventName, data: params.eventData });
writeResponse(conn, { id, success: true, result: null });
} else if (method === 'slow') {
setTimeout(() => {
writeResponse(conn, { id, success: true, result: { delayed: true } });
}, 100);
} else if (method === 'streamEcho') {
const count = params.count || 0;
let sent = 0;
const interval = setInterval(() => {
if (sent < count) {
writeResponse(conn, { id, stream: true, data: { index: sent, value: `chunk_${sent}` } });
sent++;
} else {
clearInterval(interval);
writeResponse(conn, { id, success: true, result: { totalChunks: count } });
}
}, 10);
} else if (method === 'streamError') {
writeResponse(conn, { id, stream: true, data: { index: 0, value: 'before_error' } });
setTimeout(() => {
writeResponse(conn, { id, success: false, error: 'Stream error after chunk' });
}, 20);
} else if (method === 'streamEmpty') {
writeResponse(conn, { id, success: true, result: { totalChunks: 0 } });
} else if (method === 'exit') {
writeResponse(conn, { id, success: true, result: null });
// In socket mode, 'exit' just closes this connection, not the server
setTimeout(() => conn.end(), 50);
} else {
writeResponse(conn, { id, success: false, error: `Unknown method: ${method}` });
}
}
const server = net.createServer((conn) => {
// Send ready event on each new connection
writeResponse(conn, { event: 'ready', data: { version: '1.0.0' } });
// Buffer-based newline scanner for incoming data
let buffer = Buffer.alloc(0);
conn.on('data', (chunk) => {
buffer = Buffer.concat([buffer, chunk]);
let idx;
while ((idx = buffer.indexOf(0x0A)) !== -1) {
const lineBuffer = buffer.subarray(0, idx);
buffer = buffer.subarray(idx + 1);
const line = lineBuffer.toString('utf8').trim();
if (line) {
handleLine(line, conn);
}
}
});
conn.on('error', () => { /* ignore client errors */ });
});
server.listen(socketPath, () => {
// Signal to parent that the server is ready
process.stdout.write(JSON.stringify({ socketPath, ready: true }) + '\n');
});
// Handle SIGTERM gracefully
process.on('SIGTERM', () => {
server.close();
try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
process.exit(0);
});
// Handle SIGINT
process.on('SIGINT', () => {
server.close();
try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
process.exit(0);
});

View File

@@ -0,0 +1,89 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { LineScanner } from '../ts/classes.linescanner.js';
const noopLogger = { log() {} };
tap.test('should parse a single complete line', async () => {
const scanner = new LineScanner(1024 * 1024, noopLogger);
const lines: string[] = [];
scanner.push(Buffer.from('{"hello":"world"}\n'), (line) => lines.push(line));
expect(lines.length).toEqual(1);
expect(lines[0]).toEqual('{"hello":"world"}');
});
tap.test('should parse multiple lines in one chunk', async () => {
const scanner = new LineScanner(1024 * 1024, noopLogger);
const lines: string[] = [];
scanner.push(Buffer.from('{"a":1}\n{"b":2}\n{"c":3}\n'), (line) => lines.push(line));
expect(lines.length).toEqual(3);
expect(lines[0]).toEqual('{"a":1}');
expect(lines[1]).toEqual('{"b":2}');
expect(lines[2]).toEqual('{"c":3}');
});
tap.test('should handle a line split across chunks', async () => {
const scanner = new LineScanner(1024 * 1024, noopLogger);
const lines: string[] = [];
scanner.push(Buffer.from('{"hel'), (line) => lines.push(line));
expect(lines.length).toEqual(0);
scanner.push(Buffer.from('lo":"world"}\n'), (line) => lines.push(line));
expect(lines.length).toEqual(1);
expect(lines[0]).toEqual('{"hello":"world"}');
});
tap.test('should drop oversized lines', async () => {
const scanner = new LineScanner(100, noopLogger);
const lines: string[] = [];
// Line is 200 chars + newline, exceeds maxPayloadSize of 100
const oversized = 'x'.repeat(200) + '\n';
scanner.push(Buffer.from(oversized), (line) => lines.push(line));
expect(lines.length).toEqual(0);
});
tap.test('should clear buffer on OOM (no newline, exceeds max)', async () => {
const scanner = new LineScanner(100, noopLogger);
const lines: string[] = [];
// Push 200 bytes without any newline — exceeds maxPayloadSize
scanner.push(Buffer.from('x'.repeat(200)), (line) => lines.push(line));
expect(lines.length).toEqual(0);
// After clearing, should work normally again
scanner.push(Buffer.from('{"ok":true}\n'), (line) => lines.push(line));
expect(lines.length).toEqual(1);
expect(lines[0]).toEqual('{"ok":true}');
});
tap.test('should skip empty lines', async () => {
const scanner = new LineScanner(1024 * 1024, noopLogger);
const lines: string[] = [];
scanner.push(Buffer.from('\n\n{"a":1}\n\n'), (line) => lines.push(line));
expect(lines.length).toEqual(1);
expect(lines[0]).toEqual('{"a":1}');
});
tap.test('should handle mixed complete and partial lines', async () => {
const scanner = new LineScanner(1024 * 1024, noopLogger);
const lines: string[] = [];
// First chunk: one complete line + start of second line
scanner.push(Buffer.from('{"first":1}\n{"sec'), (line) => lines.push(line));
expect(lines.length).toEqual(1);
// Second chunk: end of second line + complete third line
scanner.push(Buffer.from('ond":2}\n{"third":3}\n'), (line) => lines.push(line));
expect(lines.length).toEqual(3);
expect(lines[1]).toEqual('{"second":2}');
expect(lines[2]).toEqual('{"third":3}');
});
tap.test('clear should reset the buffer', async () => {
const scanner = new LineScanner(1024 * 1024, noopLogger);
const lines: string[] = [];
// Push partial data
scanner.push(Buffer.from('{"partial":'), (line) => lines.push(line));
// Clear
scanner.clear();
// Now push a complete new line — old partial should not affect it
scanner.push(Buffer.from('{"fresh":true}\n'), (line) => lines.push(line));
expect(lines.length).toEqual(1);
expect(lines[0]).toEqual('{"fresh":true}');
});
export default tap.start();

View File

@@ -0,0 +1,312 @@
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as path from 'path';
import * as fs from 'fs';
import * as childProcess from 'child_process';
import * as os from 'os';
import { RustBridge } from '../ts/classes.rustbridge.js';
import type { ICommandDefinition } from '../ts/interfaces/index.js';
const testDir = path.resolve(path.dirname(new URL(import.meta.url).pathname));
const mockServerPath = path.join(testDir, 'helpers/mock-socket-server.mjs');
// Define the command types for our mock binary
type TMockCommands = {
echo: { params: Record<string, any>; result: Record<string, any> };
largeEcho: { params: Record<string, any>; result: Record<string, any> };
error: { params: {}; result: never };
emitEvent: { params: { eventName: string; eventData: any }; result: null };
slow: { params: {}; result: { delayed: boolean } };
exit: { params: {}; result: null };
streamEcho: { params: { count: number }; chunk: { index: number; value: string }; result: { totalChunks: number } };
streamError: { params: {}; chunk: { index: number; value: string }; result: never };
streamEmpty: { params: {}; chunk: never; result: { totalChunks: number } };
};
/**
* Start the mock socket server and return the socket path.
* Returns { proc, socketPath }.
*/
async function startMockServer(testName: string): Promise<{ proc: childProcess.ChildProcess; socketPath: string }> {
const socketPath = path.join(os.tmpdir(), `smartrust-test-${Date.now()}-${testName}.sock`);
const proc = childProcess.spawn('node', [mockServerPath, socketPath], {
stdio: ['pipe', 'pipe', 'pipe'],
});
// Wait for the server to signal readiness via stdout
return new Promise((resolve, reject) => {
let stdoutData = '';
const timeout = setTimeout(() => {
proc.kill();
reject(new Error('Mock server did not start within 5s'));
}, 5000);
proc.stdout!.on('data', (data: Buffer) => {
stdoutData += data.toString();
const lines = stdoutData.split('\n');
for (const line of lines) {
if (line.trim()) {
try {
const parsed = JSON.parse(line.trim());
if (parsed.ready) {
clearTimeout(timeout);
resolve({ proc, socketPath: parsed.socketPath });
return;
}
} catch { /* not JSON yet */ }
}
}
});
proc.on('error', (err) => {
clearTimeout(timeout);
reject(err);
});
proc.on('exit', (code) => {
clearTimeout(timeout);
reject(new Error(`Mock server exited with code ${code}`));
});
});
}
function stopMockServer(proc: childProcess.ChildProcess, socketPath: string) {
try { proc.kill('SIGTERM'); } catch { /* ignore */ }
try { fs.unlinkSync(socketPath); } catch { /* ignore */ }
}
// === Socket Transport Tests via RustBridge.connect() ===
tap.test('socket: should connect and receive ready event', async () => {
const { proc, socketPath } = await startMockServer('connect-ready');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
const result = await bridge.connect(socketPath);
expect(result).toBeTrue();
expect(bridge.running).toBeTrue();
bridge.kill();
expect(bridge.running).toBeFalse();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should send command and receive response', async () => {
const { proc, socketPath } = await startMockServer('send-command');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
const result = await bridge.sendCommand('echo', { hello: 'world', num: 42 });
expect(result).toEqual({ hello: 'world', num: 42 });
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should handle error responses', async () => {
const { proc, socketPath } = await startMockServer('error-response');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
let threw = false;
try {
await bridge.sendCommand('error', {});
} catch (err: any) {
threw = true;
expect(err.message).toInclude('Test error message');
}
expect(threw).toBeTrue();
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should receive custom events', async () => {
const { proc, socketPath } = await startMockServer('custom-events');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
const eventPromise = new Promise<any>((resolve) => {
bridge.once('management:testEvent', (data) => resolve(data));
});
await bridge.sendCommand('emitEvent', {
eventName: 'testEvent',
eventData: { key: 'value' },
});
const eventData = await eventPromise;
expect(eventData).toEqual({ key: 'value' });
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should handle multiple concurrent commands', async () => {
const { proc, socketPath } = await startMockServer('concurrent');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge.connect(socketPath);
const results = await Promise.all([
bridge.sendCommand('echo', { id: 1 }),
bridge.sendCommand('echo', { id: 2 }),
bridge.sendCommand('echo', { id: 3 }),
]);
expect(results[0]).toEqual({ id: 1 });
expect(results[1]).toEqual({ id: 2 });
expect(results[2]).toEqual({ id: 3 });
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should handle 1MB payload round-trip', async () => {
const { proc, socketPath } = await startMockServer('large-payload');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
requestTimeoutMs: 30000,
});
await bridge.connect(socketPath);
const largeString = 'x'.repeat(1024 * 1024);
const result = await bridge.sendCommand('largeEcho', { data: largeString });
expect(result.data).toEqual(largeString);
expect(result.data.length).toEqual(1024 * 1024);
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should disconnect without killing the daemon', async () => {
const { proc, socketPath } = await startMockServer('no-kill-daemon');
try {
// First connection
const bridge1 = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
await bridge1.connect(socketPath);
expect(bridge1.running).toBeTrue();
// Disconnect
bridge1.kill();
expect(bridge1.running).toBeFalse();
// Second connection — daemon should still be alive
const bridge2 = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
});
const result = await bridge2.connect(socketPath);
expect(result).toBeTrue();
expect(bridge2.running).toBeTrue();
// Verify the daemon is functional
const echoResult = await bridge2.sendCommand('echo', { reconnected: true });
expect(echoResult).toEqual({ reconnected: true });
bridge2.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should stream responses via socket', async () => {
const { proc, socketPath } = await startMockServer('streaming');
try {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 5000,
requestTimeoutMs: 10000,
});
await bridge.connect(socketPath);
const stream = bridge.sendCommandStreaming('streamEcho', { count: 5 });
const chunks: Array<{ index: number; value: string }> = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
expect(chunks.length).toEqual(5);
for (let i = 0; i < 5; i++) {
expect(chunks[i].index).toEqual(i);
expect(chunks[i].value).toEqual(`chunk_${i}`);
}
const result = await stream.result;
expect(result.totalChunks).toEqual(5);
bridge.kill();
} finally {
stopMockServer(proc, socketPath);
}
});
tap.test('socket: should return false when socket path does not exist', async () => {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
readyTimeoutMs: 3000,
});
const result = await bridge.connect('/tmp/nonexistent-smartrust-test.sock');
expect(result).toBeFalse();
expect(bridge.running).toBeFalse();
});
tap.test('socket: should throw when sending command while not connected', async () => {
const bridge = new RustBridge<TMockCommands>({
binaryName: 'mock-daemon',
});
let threw = false;
try {
await bridge.sendCommand('echo', {});
} catch (err: any) {
threw = true;
expect(err.message).toInclude('not running');
}
expect(threw).toBeTrue();
});
export default tap.start();