@yoyo-org/progressive-json
Version:
Stream and render JSON data as it arrives - perfect for AI responses, large datasets, and real-time updates
24 lines (23 loc) • 718 B
JavaScript
export function writeSSEHeaders(res) {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
});
}
export function writeSSE(res) {
return (data) => {
const message = typeof data === "string" ? data : JSON.stringify(data);
res.write(`data: ${message}\n\n`);
};
}
export function writeSSEEvent(res, event, data) {
res.write(`event: ${event}\n`);
res.write(`data: ${JSON.stringify(data)}\n\n`);
}
export function closeSSE(res) {
res.write("data: [DONE]\n\n");
res.end();
}