53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
/**
|
|
* GET /api/v1/jobs/stream — stream real-time job progress for all jobs or a specific repository via SSE.
|
|
*
|
|
* Query parameters:
|
|
* repositoryId (optional) — filter to jobs for this repository
|
|
*/
|
|
|
|
import type { RequestHandler } from './$types';
|
|
import { getBroadcaster } from '$lib/server/pipeline/progress-broadcaster.js';
|
|
import { handleServiceError } from '$lib/server/utils/validation.js';
|
|
|
|
export const GET: RequestHandler = ({ url }) => {
|
|
try {
|
|
const broadcaster = getBroadcaster();
|
|
if (!broadcaster) {
|
|
return new Response('Service unavailable', { status: 503 });
|
|
}
|
|
|
|
const repositoryId = url.searchParams.get('repositoryId');
|
|
|
|
// Get the appropriate stream based on parameters
|
|
let stream;
|
|
if (repositoryId) {
|
|
stream = broadcaster.subscribeRepository(repositoryId);
|
|
} else {
|
|
stream = broadcaster.subscribeAll();
|
|
}
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
'Content-Type': 'text/event-stream',
|
|
'Cache-Control': 'no-cache',
|
|
Connection: 'keep-alive',
|
|
'X-Accel-Buffering': 'no',
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
});
|
|
} catch (err) {
|
|
return handleServiceError(err);
|
|
}
|
|
};
|
|
|
|
export const OPTIONS: RequestHandler = () => {
|
|
return new Response(null, {
|
|
status: 204,
|
|
headers: {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET, OPTIONS',
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
|
|
}
|
|
});
|
|
};
|