sse bug fix

This commit is contained in:
2025-02-04 13:19:05 +05:30
parent 6a57fed0dc
commit 9f337655a0
4 changed files with 61 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { ChangeEvent, dbEvents } from "../realtime";
import { hasValidClaims } from "../auth";
import { Claim } from "../utils/claims";
export async function realTimeRoutes(fastify: FastifyInstance) {
fastify.get("/events", async (req: FastifyRequest, res: FastifyReply) => {
@@ -13,9 +14,8 @@ export async function realTimeRoutes(fastify: FastifyInstance) {
res.raw.setHeader("Connection", "keep-alive");
res.raw.flushHeaders();
dbEvents.on("change", (event: ChangeEvent) => {
if (hasValidClaims(req.user, event.requiredClaims)) {
delete event.requiredClaims;
dbEvents.on("change", (event: ChangeEvent, requiredClaims: Claim[]) => {
if (hasValidClaims(req.user, requiredClaims)) {
res.raw.write(JSON.stringify(event));
}
});