api/stream: use Map for storing info about internal streams

This commit is contained in:
dumbmoron 2024-09-09 14:29:06 +00:00
parent 57c9836f56
commit 15a90e9b11
No known key found for this signature in database

View file

@ -22,7 +22,7 @@ streamCache.on("expired", (key) => {
streamCache.del(key); streamCache.del(key);
}) })
const internalStreamCache = {}; const internalStreamCache = new Map();
const hmacSalt = randomBytes(64).toString('hex'); const hmacSalt = randomBytes(64).toString('hex');
export function createStream(obj) { export function createStream(obj) {
@ -71,7 +71,7 @@ export function createStream(obj) {
} }
export function getInternalStream(id) { export function getInternalStream(id) {
return internalStreamCache[id]; return internalStreamCache.get(id);
} }
export function createInternalStream(url, obj = {}) { export function createInternalStream(url, obj = {}) {
@ -95,13 +95,13 @@ export function createInternalStream(url, obj = {}) {
headers = new Map(Object.entries(obj.headers)); headers = new Map(Object.entries(obj.headers));
} }
internalStreamCache[streamID] = { internalStreamCache.set(streamID, {
url, url,
service: obj.service, service: obj.service,
headers, headers,
controller, controller,
dispatcher dispatcher
}; });
let streamLink = new URL('/itunnel', `http://127.0.0.1:${env.apiPort}`); let streamLink = new URL('/itunnel', `http://127.0.0.1:${env.apiPort}`);
streamLink.searchParams.set('id', streamID); streamLink.searchParams.set('id', streamID);
@ -124,9 +124,9 @@ export function destroyInternalStream(url) {
const id = url.searchParams.get('id'); const id = url.searchParams.get('id');
if (internalStreamCache[id]) { if (internalStreamCache.has(id)) {
closeRequest(internalStreamCache[id].controller); closeRequest(getInternalStream(id)?.controller);
delete internalStreamCache[id]; internalStreamCache.delete(id);
} }
} }