lots of changes

This commit is contained in:
NoLogicAlan committed 2026-05-16 20:30:56 +03:00
1 parent ba225c914a
commit 6fa8f3b540
20 files changed
+296 -144

No files matched your search

+58 -47
View File
@@ -32,56 +32,67 @@ import { joinVoiceChannel, getVoiceManager } from "@fluxerjs/voice";
import { logger } from "./Logger.mjs";
{
const _originalConsoleLog = console.log;
console.log = function (...args) {
// Fast path: if the first arg is a string that looks like a pino JSON log
if (args.length === 1 && typeof args[0] === "string" && args[0].startsWith("{")) {
try {
const parsed = JSON.parse(args[0]);
// Drop pino logs from LiveKit SDK ("lk-rtc" name)
if (parsed.name === "lk-rtc") return;
} catch (_) {
// Not valid JSON — pass through
}
}
// Suppress LiveKit SDK's human-readable log lines:
// [voice LiveKitRtc] connected to room
// [voice LiveKitRtc] Room disconnected
// [voice LiveKitRtc] emitting disconnect
// [voice LiveKitRtc] Room reconnecting...
// [voice LiveKitRtc] Room reconnected
if (args.length >= 1 && typeof args[0] === "string" && args[0].includes("[voice LiveKitRtc]")) {
return; // silently drop
}
_originalConsoleLog.apply(console, args);
};
// ── LiveKit SDK log suppression ──────────────────────────────────────────
// The LiveKit Node.js SDK (via pino) emits verbose JSON and human-readable
// log lines that flood the console. We monkey-patch console.log and
// process.stdout.write to silently drop lines from the "lk-rtc" logger.
//
// Guard: only patch once even if the module is imported multiple times
// (e.g. during hot-reload). The global symbol ensures idempotency.
if (!globalThis.__fluxerLiveKitLogPatchApplied) {
globalThis.__fluxerLiveKitLogPatchApplied = true;
// ── Also monkey-patch process.stdout.write ──────────────────────────────
// Pino writes directly to process.stdout.write, bypassing console.log
// entirely. This is the primary path for the JSON log lines like:
// {"level":20,"time":1778784504183,"pid":961949,"name":"lk-rtc",...}
// We intercept these and silently drop them.
const _originalStdoutWrite = process.stdout.write.bind(process.stdout);
process.stdout.write = function (chunk, encoding, callback) {
// Only intercept string chunks that look like pino JSON logs
if (typeof chunk === "string" && chunk.startsWith("{")) {
try {
// Fast check: does it contain lk-rtc before full parse?
if (chunk.includes('"lk-rtc"')) {
const parsed = JSON.parse(chunk);
if (parsed.name === "lk-rtc") {
// Silently drop — call callback to signal "written" so pino doesn't stall
if (typeof encoding === "function") { encoding(); }
else if (typeof callback === "function") { callback(); }
return true;
}
const _originalConsoleLog = console.log;
console.log = function (...args) {
// Fast path: if the first arg is a string that looks like a pino JSON log
if (args.length === 1 && typeof args[0] === "string" && args[0].startsWith("{")) {
try {
const parsed = JSON.parse(args[0]);
// Drop pino logs from LiveKit SDK ("lk-rtc" name)
if (parsed.name === "lk-rtc") return;
} catch (_) {
// Not valid JSON — pass through
}
} catch (_) {
// Not valid JSON — pass through
}
}
return _originalStdoutWrite(chunk, encoding, callback);
};
// Suppress LiveKit SDK's human-readable log lines:
// [voice LiveKitRtc] connected to room
// [voice LiveKitRtc] Room disconnected
// [voice LiveKitRtc] emitting disconnect
// [voice LiveKitRtc] Room reconnecting...
// [voice LiveKitRtc] Room reconnected
if (args.length >= 1 && typeof args[0] === "string" && args[0].includes("[voice LiveKitRtc]")) {
return; // silently drop
}
_originalConsoleLog.apply(console, args);
};
// ── Also monkey-patch process.stdout.write ──────────────────────────────
// Pino writes directly to process.stdout.write, bypassing console.log
// entirely. This is the primary path for the JSON log lines like:
// {"level":20,"time":1778784504183,"pid":961949,"name":"lk-rtc",...}
// We intercept these and silently drop them.
const _originalStdoutWrite = process.stdout.write.bind(process.stdout);
process.stdout.write = function (chunk, encoding, callback) {
// Only intercept string chunks that look like pino JSON logs
if (typeof chunk === "string" && chunk.startsWith("{")) {
try {
// Fast check: does it contain lk-rtc before full parse?
if (chunk.includes('"lk-rtc"')) {
const parsed = JSON.parse(chunk);
if (parsed.name === "lk-rtc") {
// Silently drop — call callback to signal "written" so pino doesn't stall
if (typeof encoding === "function") { encoding(); }
else if (typeof callback === "function") { callback(); }
return true;
}
}
} catch (_) {
// Not valid JSON — pass through
}
}
return _originalStdoutWrite(chunk, encoding, callback);
};
}
}
// ConnectionState enum values for reference:
+24 -7
View File
@@ -105,15 +105,29 @@ export class VoiceStateCache {
// ── Update location map ────────────────────────────────────────────
locations.set(uKey, { channelId: cleanChannel, guildId: cleanGuild, userId: cleanUser });
// ── LRU: move to front ─────────────────────────────────────────────
const idx = lruKeys.indexOf(uKey);
if (idx !== -1) lruKeys.splice(idx, 1);
lruKeys.unshift(uKey);
// ── LRU: move to most-recently-used position ──────────────────────
// JavaScript Maps maintain insertion order, so delete + re-insert
// is O(1) amortized instead of O(n) with indexOf + splice on an array.
// We keep the _lruUserKeys / _lruBotKeys arrays as fast eviction queues
// but use the Map's own key order for O(1) move-to-front.
if (locations.has(uKey)) {
// Re-insert to move to end (most recently used) — O(1)
locations.delete(uKey);
locations.set(uKey, { channelId: cleanChannel, guildId: cleanGuild, userId: cleanUser });
}
// ── Evict if over cap ──────────────────────────────────────────────
while (lruKeys.length > maxEntries) {
const evictKey = lruKeys.pop();
if (evictKey === uKey) continue; // don't evict the entry we just added
// Use the Map's insertion order to find the oldest entry (first key).
// This is O(1) per eviction instead of popping from an array.
while (locations.size > maxEntries) {
const evictKey = locations.keys().next().value;
if (evictKey === uKey) {
// Don't evict the entry we just added — re-insert at end and move to next
const evictVal = locations.get(evictKey);
locations.delete(evictKey);
locations.set(evictKey, evictVal);
continue;
}
const evicted = locations.get(evictKey);
if (evicted) {
const evictCKey = VoiceStateCache.channelKey(evicted.guildId, evicted.channelId);
@@ -124,6 +138,9 @@ export class VoiceStateCache {
}
locations.delete(evictKey);
}
// Also remove from LRU key list if present
const lruIdx = lruKeys.indexOf(evictKey);
if (lruIdx !== -1) lruKeys.splice(lruIdx, 1);
}
} else {
// ── User left voice — remove from location map ─────────────────────