diff --git a/src/server.js b/src/server.js index 5c37bda..c09a8e0 100644 --- a/src/server.js +++ b/src/server.js @@ -438,8 +438,10 @@ function startServer(host, port, openBrowser = true) { } } - // Anonymous heartbeat — count active installs - sendHeartbeat(); + // Delayed heartbeat + auto-sync (don't block startup) + setTimeout(sendHeartbeat, 5000); + setTimeout(autoSync, 15000); // first sync 15s after start + setInterval(autoSync, 300000); // then every 5 min }); } @@ -448,15 +450,11 @@ function sendHeartbeat() { const { getOrCreateAnonId } = require('./data'); const anon = getOrCreateAnonId(); const pkg = require('../package.json'); - const sessions = require('./data').loadSessions(); - const agentCounts = {}; - sessions.forEach(s => { agentCounts[s.tool] = (agentCounts[s.tool] || 0) + 1; }); const body = JSON.stringify({ anonId: anon.id, version: pkg.version, platform: process.platform, - agents: agentCounts, }); const req = https.request({ @@ -465,12 +463,22 @@ function sendHeartbeat() { headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }, timeout: 5000, }); - req.on('error', () => {}); // silent fail + req.on('error', () => {}); req.write(body); req.end(); } catch {} } +function autoSync() { + try { + const profile = loadGitHubProfile(); + if (!profile || !profile.authenticated) return; // not connected — skip + syncLeaderboard().then(() => { + log('SYNC', 'Auto-sync OK'); + }).catch(() => {}); + } catch {} +} + // ── Helpers ───────────────────────────────── function json(res, data, status = 200) { res.writeHead(status, { 'Content-Type': 'application/json' });