no more typescript parser. only rust!

SSE Changed a bit to fit the Rust Parser
This commit is contained in:
2025-06-29 17:57:59 +02:00
parent 24ef59f0ed
commit 00fe8e7107
4 changed files with 510 additions and 1221 deletions

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
import { watchPosts, stopWatching } from '@/lib/markdown';
import { spawn } from 'child_process';
// Prevent static generation of this route
export const dynamic = 'force-dynamic';
@@ -37,35 +37,87 @@ export async function GET(request: NextRequest) {
return;
}
// Set up file watcher if not already set up
// Set up Rust file watcher if not already set up
if (clients.size === 1) {
try {
watchPosts(() => {
// Notify all connected clients about the update
const message = JSON.stringify({ type: 'update', timestamp: new Date().toISOString() });
const clientsToRemove: ReadableStreamDefaultController[] = [];
const rustWatcher = spawn(
process.cwd() + '/markdown_backend/target/release/markdown_backend',
['watch'],
{ stdio: ['pipe', 'pipe', 'pipe'] }
);
rustWatcher.stdout.on('data', (data) => {
const message = data.toString().trim();
console.log('Rust watcher output:', message);
clients.forEach(client => {
try {
client.enqueue(`data: ${message}\n\n`);
} catch (error) {
// Mark client for removal
clientsToRemove.push(client);
if (message.includes('Posts directory changed!')) {
// Notify all connected clients about the update
const updateMessage = JSON.stringify({ type: 'update', timestamp: new Date().toISOString() });
const clientsToRemove: ReadableStreamDefaultController[] = [];
clients.forEach(client => {
try {
client.enqueue(`data: ${updateMessage}\n\n`);
} catch (error) {
// Mark client for removal
clientsToRemove.push(client);
}
});
// Remove disconnected clients
clientsToRemove.forEach(client => {
clients.delete(client);
});
// Stop watching if no clients are connected
if (clients.size === 0) {
console.log('No clients connected, stopping watcher');
rustWatcher.kill();
}
});
// Remove disconnected clients
clientsToRemove.forEach(client => {
clients.delete(client);
});
// Stop watching if no clients are connected
if (clients.size === 0) {
stopWatching();
}
});
rustWatcher.stderr.on('data', (data) => {
const errorMessage = data.toString().trim();
console.error('Rust watcher error:', errorMessage);
// Don't treat RecvError as a real error - it's expected when the process is terminated
if (!errorMessage.includes('RecvError')) {
// Send error to clients
const errorData = JSON.stringify({ type: 'error', message: errorMessage });
const clientsToRemove: ReadableStreamDefaultController[] = [];
clients.forEach(client => {
try {
client.enqueue(`data: ${errorData}\n\n`);
} catch (error) {
clientsToRemove.push(client);
}
});
clientsToRemove.forEach(client => {
clients.delete(client);
});
}
});
rustWatcher.on('error', (error) => {
console.error('Rust watcher spawn error:', error);
});
rustWatcher.on('close', (code) => {
console.log('Rust watcher closed with code:', code);
// Only restart if we still have clients
if (clients.size > 0) {
console.log('Restarting watcher due to unexpected close');
// The watcher will be restarted when the next client connects
}
});
// Store the watcher process for cleanup
(controller as any).rustWatcher = rustWatcher;
} catch (error) {
console.error('Error setting up file watcher:', error);
console.error('Error setting up Rust file watcher:', error);
}
}
@@ -75,16 +127,17 @@ export async function GET(request: NextRequest) {
// Stop watching if no clients are connected
if (clients.size === 0) {
stopWatching();
const rustWatcher = (controller as any).rustWatcher;
if (rustWatcher) {
console.log('Last client disconnected, stopping watcher');
rustWatcher.kill();
}
}
});
},
cancel() {
// Handle stream cancellation - we can't access the specific controller here
// The abort event handler will handle cleanup for the specific controller
if (clients.size === 0) {
stopWatching();
}
// Handle stream cancellation - this is called when the stream is cancelled
// We can't access the specific controller here, so we'll handle cleanup in the abort event
}
});