Spaces:
Running
Running
| ; | |
| Object.defineProperty(exports, "__esModule", { value: true }); | |
| exports.registerServer = registerServer; | |
| exports.heartbeat = heartbeat; | |
| exports.unregisterServer = unregisterServer; | |
| exports.listPublicServers = listPublicServers; | |
| exports.listAllServers = listAllServers; | |
| exports.getServer = getServer; | |
| exports.listServersByHost = listServersByHost; | |
| exports.sanitizeEntry = sanitizeEntry; | |
| const TTL_MS = 120_000; | |
| const registry = new Map(); | |
| setInterval(() => { | |
| const now = Date.now(); | |
| for (const [id, entry] of registry) { | |
| if (entry.expiresAt < now) | |
| registry.delete(id); | |
| } | |
| }, 30_000).unref(); | |
| function registerServer(entry) { | |
| // Evict any existing rooms from the same host so a user can only appear | |
| // once in the server list — prevents ghost/duplicate entries. | |
| for (const [id, existing] of registry) { | |
| if (existing.hosting === entry.hosting && id !== entry.roomId) { | |
| registry.delete(id); | |
| } | |
| } | |
| const now = Date.now(); | |
| const full = { ...entry, createdAt: now, expiresAt: now + TTL_MS }; | |
| registry.set(entry.roomId, full); | |
| return full; | |
| } | |
| function heartbeat(roomId, playerCount) { | |
| const entry = registry.get(roomId); | |
| if (!entry) | |
| return false; | |
| entry.expiresAt = Date.now() + TTL_MS; | |
| if (typeof playerCount === "number") | |
| entry.playerCount = playerCount; | |
| return true; | |
| } | |
| function unregisterServer(roomId) { | |
| return registry.delete(roomId); | |
| } | |
| function listPublicServers() { | |
| const now = Date.now(); | |
| return Array.from(registry.values()) | |
| .filter((e) => e.expiresAt > now && e.visibility === "public") | |
| .sort((a, b) => b.createdAt - a.createdAt); | |
| } | |
| function listAllServers() { | |
| const now = Date.now(); | |
| return Array.from(registry.values()) | |
| .filter((e) => e.expiresAt > now) | |
| .sort((a, b) => b.createdAt - a.createdAt); | |
| } | |
| function getServer(roomId) { | |
| const entry = registry.get(roomId); | |
| if (!entry || entry.expiresAt < Date.now()) | |
| return undefined; | |
| return entry; | |
| } | |
| function listServersByHost(hosting) { | |
| const now = Date.now(); | |
| const norm = hosting.toLowerCase().replace(/^@/, ""); | |
| return Array.from(registry.values()) | |
| .filter((e) => e.expiresAt > now && e.hosting.toLowerCase().replace(/^@/, "") === norm) | |
| .sort((a, b) => b.createdAt - a.createdAt); | |
| } | |
| function sanitizeEntry(e) { | |
| const { pinHash, ...rest } = e; | |
| return { ...rest, hasPin: !!pinHash }; | |
| } | |