feat(rustbridge): add streaming responses and robust large-payload/backpressure handling to RustBridge
This commit is contained in:
@@ -2,22 +2,46 @@
|
||||
|
||||
/**
|
||||
* Mock "Rust binary" for testing the RustBridge IPC protocol.
|
||||
* Reads JSON lines from stdin, writes JSON lines to stdout.
|
||||
* Reads JSON lines from stdin via Buffer-based scanner, writes JSON lines to stdout.
|
||||
* Emits a ready event on startup.
|
||||
*/
|
||||
|
||||
import { createInterface } from 'readline';
|
||||
|
||||
// Emit ready event
|
||||
const readyEvent = JSON.stringify({ event: 'ready', data: { version: '1.0.0' } });
|
||||
process.stdout.write(readyEvent + '\n');
|
||||
|
||||
const rl = createInterface({ input: process.stdin });
|
||||
// Buffer-based newline scanner for stdin (mirrors the RustBridge approach)
|
||||
let stdinBuffer = Buffer.alloc(0);
|
||||
|
||||
rl.on('line', (line) => {
|
||||
process.stdin.on('data', (chunk) => {
|
||||
stdinBuffer = Buffer.concat([stdinBuffer, chunk]);
|
||||
|
||||
let newlineIndex;
|
||||
while ((newlineIndex = stdinBuffer.indexOf(0x0A)) !== -1) {
|
||||
const lineBuffer = stdinBuffer.subarray(0, newlineIndex);
|
||||
stdinBuffer = stdinBuffer.subarray(newlineIndex + 1);
|
||||
const line = lineBuffer.toString('utf8').trim();
|
||||
if (line) {
|
||||
handleLine(line);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Backpressure-aware write to stdout.
|
||||
*/
|
||||
function writeResponse(data) {
|
||||
const json = JSON.stringify(data) + '\n';
|
||||
if (!process.stdout.write(json)) {
|
||||
// Wait for drain before continuing
|
||||
process.stdout.once('drain', () => {});
|
||||
}
|
||||
}
|
||||
|
||||
function handleLine(line) {
|
||||
let request;
|
||||
try {
|
||||
request = JSON.parse(line.trim());
|
||||
request = JSON.parse(line);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
@@ -26,35 +50,53 @@ rl.on('line', (line) => {
|
||||
|
||||
if (method === 'echo') {
|
||||
// Echo back the params as result
|
||||
const response = JSON.stringify({ id, success: true, result: params });
|
||||
process.stdout.write(response + '\n');
|
||||
writeResponse({ id, success: true, result: params });
|
||||
} else if (method === 'largeEcho') {
|
||||
// Echo back params (same as echo, named distinctly for large payload tests)
|
||||
writeResponse({ id, success: true, result: params });
|
||||
} else if (method === 'error') {
|
||||
// Return an error
|
||||
const response = JSON.stringify({ id, success: false, error: 'Test error message' });
|
||||
process.stdout.write(response + '\n');
|
||||
writeResponse({ id, success: false, error: 'Test error message' });
|
||||
} else if (method === 'emitEvent') {
|
||||
// Emit a custom event, then respond with success
|
||||
const event = JSON.stringify({ event: params.eventName, data: params.eventData });
|
||||
process.stdout.write(event + '\n');
|
||||
const response = JSON.stringify({ id, success: true, result: null });
|
||||
process.stdout.write(response + '\n');
|
||||
writeResponse({ event: params.eventName, data: params.eventData });
|
||||
writeResponse({ id, success: true, result: null });
|
||||
} else if (method === 'slow') {
|
||||
// Respond after a delay
|
||||
setTimeout(() => {
|
||||
const response = JSON.stringify({ id, success: true, result: { delayed: true } });
|
||||
process.stdout.write(response + '\n');
|
||||
writeResponse({ id, success: true, result: { delayed: true } });
|
||||
}, 100);
|
||||
} else if (method === 'streamEcho') {
|
||||
// Send params.count stream chunks, then final response
|
||||
const count = params.count || 0;
|
||||
let sent = 0;
|
||||
const interval = setInterval(() => {
|
||||
if (sent < count) {
|
||||
writeResponse({ id, stream: true, data: { index: sent, value: `chunk_${sent}` } });
|
||||
sent++;
|
||||
} else {
|
||||
clearInterval(interval);
|
||||
writeResponse({ id, success: true, result: { totalChunks: count } });
|
||||
}
|
||||
}, 10);
|
||||
} else if (method === 'streamError') {
|
||||
// Send 1 chunk, then error
|
||||
writeResponse({ id, stream: true, data: { index: 0, value: 'before_error' } });
|
||||
setTimeout(() => {
|
||||
writeResponse({ id, success: false, error: 'Stream error after chunk' });
|
||||
}, 20);
|
||||
} else if (method === 'streamEmpty') {
|
||||
// Zero chunks, immediate final response
|
||||
writeResponse({ id, success: true, result: { totalChunks: 0 } });
|
||||
} else if (method === 'exit') {
|
||||
// Graceful exit
|
||||
const response = JSON.stringify({ id, success: true, result: null });
|
||||
process.stdout.write(response + '\n');
|
||||
writeResponse({ id, success: true, result: null });
|
||||
process.exit(0);
|
||||
} else {
|
||||
// Unknown command
|
||||
const response = JSON.stringify({ id, success: false, error: `Unknown method: ${method}` });
|
||||
process.stdout.write(response + '\n');
|
||||
writeResponse({ id, success: false, error: `Unknown method: ${method}` });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Handle SIGTERM gracefully
|
||||
process.on('SIGTERM', () => {
|
||||
|
||||
Reference in New Issue
Block a user