File size: 986 Bytes
d330ba1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const crypto = require('crypto');

const SESSION_TTL_MS = 20 * 60 * 1000;
const sessions = new Map();

function cleanupExpiredSessions() {
  const now = Date.now();
  for (const [id, session] of sessions.entries()) {
    if (session.expiresAt <= now) {
      sessions.delete(id);
    }
  }
}

function createStreamSession({ targetUrl, headers = {}, sourceUrl = null }) {
  cleanupExpiredSessions();

  const id = crypto.randomBytes(16).toString('hex');
  const expiresAt = Date.now() + SESSION_TTL_MS;

  sessions.set(id, {
    id,
    targetUrl,
    sourceUrl,
    headers,
    expiresAt,
    createdAt: Date.now()
  });

  return sessions.get(id);
}

function getStreamSession(id) {
  cleanupExpiredSessions();

  const session = sessions.get(id);
  if (!session || session.expiresAt <= Date.now()) {
    sessions.delete(id);
    return null;
  }

  session.expiresAt = Date.now() + SESSION_TTL_MS;
  return session;
}

module.exports = {
  createStreamSession,
  getStreamSession
};