From 5195317c0c22578bb95dd76eeaa7cc540af5a6f5 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Sat, 28 Feb 2026 21:51:28 -0500 Subject: [PATCH] Expire live caption state after inactivity and tune stream limits --- index.js | 1 + routes/liveCaptions.js | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index a9c4741..9a072c7 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,7 @@ const limiter = rateLimit({ limit: 500, // Limit each IP to 100 requests per `window` (here, per 15 minutes). standardHeaders: 'draft-8', // draft-6: `RateLimit-*` headers; draft-7 & draft-8: combined `RateLimit` header legacyHeaders: false, // Disable the `X-RateLimit-*` headers. + skip: (req) => req.path.startsWith("/live-captions"), keyGenerator: (req) => { const forwarded = req.headers["x-forwarded-for"]?.split(",")[0]; // Take the first IP in the list const ip = forwarded || req.ip; // Fallback to req.ip diff --git a/routes/liveCaptions.js b/routes/liveCaptions.js index 07d6537..caa01ad 100644 --- a/routes/liveCaptions.js +++ b/routes/liveCaptions.js @@ -1,19 +1,36 @@ var express = require('express'); var router = express.Router(); +const { rateLimit } = require("express-rate-limit"); const sessionChecker = require("../middleware/sessionChecker.js"); const MAX_BUFFER_SIZE = 300; const DEFAULT_INITIAL_LIMIT = 40; const MAX_INITIAL_LIMIT = 120; +const INACTIVITY_RESET_MS = 10 * 60 * 1000; const CAPTION_META_KEYS = new Set(["sequence", "createdAt", "original", "draft", "sourceLang", "lang", "isDraft", "status", "translations"]); const liveCaptionState = { startedAt: Date.now(), + lastIngestAt: 0, latestSequence: 0, captions: [], }; +const liveCaptionsLimiter = rateLimit({ + windowMs: 10 * 60 * 1000, + limit: 6000, + standardHeaders: "draft-8", + legacyHeaders: false, + keyGenerator: (req) => { + const forwarded = req.headers["x-forwarded-for"]?.split(",")[0]; + const ip = forwarded || req.ip || ""; + return ip.includes(":") ? ip.split(":")[0] : ip; + }, +}); + +router.use(liveCaptionsLimiter); + const normalizeLang = (lang = "") => { const value = String(lang || "").trim().toLowerCase(); if (!value) return ""; @@ -82,8 +99,22 @@ const getAvailableLanguages = () => { return Array.from(langs).filter(Boolean).sort(); }; +const resetLiveCaptionState = () => { + liveCaptionState.startedAt = Date.now(); + liveCaptionState.lastIngestAt = 0; + liveCaptionState.latestSequence = 0; + liveCaptionState.captions = []; +}; + +const maybeResetForInactivity = () => { + if (!liveCaptionState.lastIngestAt) return; + if ((Date.now() - liveCaptionState.lastIngestAt) < INACTIVITY_RESET_MS) return; + resetLiveCaptionState(); +}; + router.get("/stream", sessionChecker, async (req, res) => { try { + maybeResetForInactivity(); const sinceSequence = Number.parseInt(req.query?.sinceSequence, 10); const requestedLimit = Number.parseInt(req.query?.limit, 10); const initialLimit = Number.isFinite(requestedLimit) @@ -150,6 +181,7 @@ router.post("/ingest", async (req, res) => { }; liveCaptionState.latestSequence = sequence; + liveCaptionState.lastIngestAt = Date.now(); liveCaptionState.captions.push(caption); if (liveCaptionState.captions.length > MAX_BUFFER_SIZE) { liveCaptionState.captions.splice(0, liveCaptionState.captions.length - MAX_BUFFER_SIZE); @@ -170,9 +202,7 @@ router.post("/ingest", async (req, res) => { router.post("/reset", async (_, res) => { try { // TODO: Add admin authorization before exposing this endpoint. - liveCaptionState.startedAt = Date.now(); - liveCaptionState.latestSequence = 0; - liveCaptionState.captions = []; + resetLiveCaptionState(); return res.json({ status: "ok" }); } catch (error) { console.error("Error resetting live captions state", error);