Warm the stream cache for likely next plays

This commit is contained in:
Claude
2026-09-30 06:53:48 +00:00
parent 67c2f6e46c
commit b24366d6c0
4 changed files with 40 additions and 1 deletions

View File

@@ -547,6 +547,21 @@ function pickFormat(formats, { formatId, wantAudio, wantHeight }) {
return null;
}
// GET /api/streams/warm?v=<id> — fire-and-forget: resolve streams into
// streamCache so the real /api/streams a moment later is instant. Bounded
// so a scrolling user can't queue dozens of yt-dlp processes.
const WARM_MAX = 2;
let warmActive = 0;
app.get('/api/streams/warm', async (c) => {
const id = (c.req.query('v') || '').trim();
if (!/^[A-Za-z0-9_-]{11}$/.test(id)) return c.body(null, 204);
if (warmActive >= WARM_MAX) return c.body(null, 204);
try { if (await media.getReady(id)) return c.body(null, 204); } catch { /* fall through */ }
warmActive++;
resolveStreams(id).catch(() => {}).finally(() => { warmActive--; });
return c.body(null, 204);
});
// GET /api/streams?v=<videoId> — meta + proxied audio/quality URLs.
app.get('/api/streams', async (c) => {
const videoId = (c.req.query('v') || '').replace(/[/\\:?<>|*"]/g, '').trim();