63 lines
2.0 KiB
JavaScript
63 lines
2.0 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Mock "Rust binary" for testing the RustBridge IPC protocol.
|
||
|
|
* Reads JSON lines from stdin, 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 });
|
||
|
|
|
||
|
|
rl.on('line', (line) => {
|
||
|
|
let request;
|
||
|
|
try {
|
||
|
|
request = JSON.parse(line.trim());
|
||
|
|
} catch {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const { id, method, params } = request;
|
||
|
|
|
||
|
|
if (method === 'echo') {
|
||
|
|
// Echo back the params as result
|
||
|
|
const response = JSON.stringify({ id, success: true, result: params });
|
||
|
|
process.stdout.write(response + '\n');
|
||
|
|
} else if (method === 'error') {
|
||
|
|
// Return an error
|
||
|
|
const response = JSON.stringify({ id, success: false, error: 'Test error message' });
|
||
|
|
process.stdout.write(response + '\n');
|
||
|
|
} 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');
|
||
|
|
} else if (method === 'slow') {
|
||
|
|
// Respond after a delay
|
||
|
|
setTimeout(() => {
|
||
|
|
const response = JSON.stringify({ id, success: true, result: { delayed: true } });
|
||
|
|
process.stdout.write(response + '\n');
|
||
|
|
}, 100);
|
||
|
|
} else if (method === 'exit') {
|
||
|
|
// Graceful exit
|
||
|
|
const response = JSON.stringify({ id, success: true, result: null });
|
||
|
|
process.stdout.write(response + '\n');
|
||
|
|
process.exit(0);
|
||
|
|
} else {
|
||
|
|
// Unknown command
|
||
|
|
const response = JSON.stringify({ id, success: false, error: `Unknown method: ${method}` });
|
||
|
|
process.stdout.write(response + '\n');
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// Handle SIGTERM gracefully
|
||
|
|
process.on('SIGTERM', () => {
|
||
|
|
process.exit(0);
|
||
|
|
});
|