opencode-statboard 2.10.34 → 2.10.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +25 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-statboard",
3
- "version": "2.10.34",
3
+ "version": "2.10.35",
4
4
  "description": "Real-time dashboard for opencode token usage and cost statistics",
5
5
  "main": "server.js",
6
6
  "bin": {
package/server.js CHANGED
@@ -14,6 +14,21 @@ let Database;
14
14
  try { Database = require('better-sqlite3'); } catch (_) { Database = null; }
15
15
  const pricing = require('./pricing');
16
16
 
17
+ // The report/trend endpoints only ever have four real scopes (the four pills in
18
+ // the UI). `days` used to be taken straight from the query string, so
19
+ // `?days=abc` fell through `parseInt(...) || 0` to 0 (= Daily) and every distinct
20
+ // value minted its own reportCache/trendCache entry that nothing ever evicted
21
+ // (cleanup runs every CACHE_CLEANUP_MS = 6h by default). Normalize to the known
22
+ // set so an arbitrary query param cannot pick a scope or grow the cache.
23
+ const SCOPE_DAYS = [-1, 1, 7, 30];
24
+ const DEFAULT_SCOPE = 7;
25
+ // Upper bound on reportCache/trendCache entries (see cleanExpiredCache).
26
+ const CACHE_MAX_KEYS = 16;
27
+ function normalizeScope(days) {
28
+ const n = parseInt(days, 10);
29
+ return SCOPE_DAYS.includes(n) ? n : DEFAULT_SCOPE;
30
+ }
31
+
17
32
  const PORT = parseInt(process.env.OC_PORT || '4868', 10);
18
33
  const REFRESH_MS = 15000;
19
34
  const TREND_MS = 60 * 1000;
@@ -214,6 +229,14 @@ function cleanExpiredCache(cache, ttl) {
214
229
  delete cache[key];
215
230
  }
216
231
  }
232
+ // Hard cap as a backstop: expiry alone is not a bound if a caller keeps
233
+ // minting keys faster than the (6h by default) cleanup interval. Drop the
234
+ // oldest entries first.
235
+ const keys = Object.keys(cache);
236
+ if (keys.length > CACHE_MAX_KEYS) {
237
+ keys.sort((a, b) => (cache[a].generatedAt || 0) - (cache[b].generatedAt || 0));
238
+ for (const k of keys.slice(0, keys.length - CACHE_MAX_KEYS)) delete cache[k];
239
+ }
217
240
  }
218
241
 
219
242
  async function generateTrend(days) {
@@ -1427,7 +1450,7 @@ const server = http.createServer(async (req, res) => {
1427
1450
  };
1428
1451
 
1429
1452
  if (u === '/api/stats') {
1430
- const days = parseInt(url.searchParams.get('days') || '7', 10) || 7;
1453
+ const days = normalizeScope(url.searchParams.get('days') || DEFAULT_SCOPE);
1431
1454
  const r = refreshTrend(days);
1432
1455
  respond({
1433
1456
  ok: !!state.data,
@@ -1441,7 +1464,7 @@ const server = http.createServer(async (req, res) => {
1441
1464
  });
1442
1465
  } else if (u === '/api/report') {
1443
1466
  const daysParam = url.searchParams.get('days');
1444
- const days = daysParam !== null ? (parseInt(daysParam, 10) || 0) : 0;
1467
+ const days = daysParam !== null ? normalizeScope(daysParam) : 0;
1445
1468
  const r = refreshReport(days);
1446
1469
  respond({
1447
1470
  ok: true,