claude-code-cost 0.24.0 → 0.26.0

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.
package/lib/net-guard.js CHANGED
@@ -245,7 +245,7 @@ function createNetGuard(config = {}) {
245
245
  * @param {{maxHeaderSize?: number, onUpgrade?: (req: any, socket: any, head: Buffer) => void}} [opts]
246
246
  */
247
247
  function listenLoopback(app, port, onReady, opts = {}) {
248
- const http = require('http');
248
+ const http = require('node:http');
249
249
  const serverOpts = opts.maxHeaderSize ? { maxHeaderSize: opts.maxHeaderSize } : {};
250
250
  const makeServer = (handler) => {
251
251
  const s = http.createServer(serverOpts, handler);
@@ -8,7 +8,7 @@
8
8
  // The call runs no model turn, and total_cost_usd comes back 0. It still spawns a full CLI process
9
9
  // and fires that config dir's SessionStart hooks, so callers must cache rather than poll.
10
10
 
11
- const { spawn } = require('child_process');
11
+ const { spawn } = require('node:child_process');
12
12
 
13
13
  // The answer is one HTTPS call plus a scan of every transcript touched in the last seven days.
14
14
  const PROBE_TIMEOUT_MS = 60000;
@@ -54,8 +54,7 @@ function probeUsage({ claudeDir } = {}) {
54
54
 
55
55
  child.stdout.on('data', (d) => {
56
56
  buf += d.toString();
57
- let nl;
58
- while ((nl = buf.indexOf('\n')) >= 0) {
57
+ for (let nl = buf.indexOf('\n'); nl >= 0; nl = buf.indexOf('\n')) {
59
58
  const line = buf.slice(0, nl).trim();
60
59
  buf = buf.slice(nl + 1);
61
60
  if (!line) continue;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "claude-code-cost",
3
- "version": "0.24.0",
3
+ "version": "0.26.0",
4
4
  "description": "Cost dashboard for Claude Code usage",
5
5
  "main": "server.js",
6
+ "type": "commonjs",
6
7
  "bin": {
7
8
  "claude-code-cost": "server.js"
8
9
  },
package/public/app.js CHANGED
@@ -488,11 +488,28 @@ function purgeLegacyLocalCache() {
488
488
  keys.forEach((k) => localStorage.removeItem(k));
489
489
  }
490
490
 
491
+ // A re-render can ask for a URL whose first request is still in flight (the hub's theme message
492
+ // arrives during the first load and re-renders the view), so it joins that request.
493
+ const pendingFetches = new Map();
494
+
491
495
  async function fetchJSON(url, skipCache) {
492
- if (!skipCache) {
493
- const cached = getCached(url);
494
- if (cached) return cached;
496
+ if (skipCache) return requestJSON(url);
497
+ const cached = getCached(url);
498
+ if (cached) return cached;
499
+ const pending = pendingFetches.get(url);
500
+ if (pending) return pending;
501
+ const request = requestJSON(url);
502
+ pendingFetches.set(url, request);
503
+ try {
504
+ const data = await request;
505
+ cacheData(url, data);
506
+ return data;
507
+ } finally {
508
+ pendingFetches.delete(url);
495
509
  }
510
+ }
511
+
512
+ async function requestJSON(url) {
496
513
  const res = await fetch(url);
497
514
  if (!res.ok) {
498
515
  const err = new Error(`HTTP ${res.status}`);
@@ -501,7 +518,6 @@ async function fetchJSON(url, skipCache) {
501
518
  }
502
519
  const data = await res.json();
503
520
  dataFetchedAt = Date.now();
504
- if (!skipCache) cacheData(url, data);
505
521
  return data;
506
522
  }
507
523
 
@@ -3365,24 +3381,36 @@ async function refreshIfStale() {
3365
3381
  // 'π'), so the key alone cannot identify the binding. The hub owns the keymap and normalizes;
3366
3382
  // these tests only decide whether a press is the hub's to handle.
3367
3383
  document.addEventListener('keydown', (e) => {
3368
- const fwd = () => {
3369
- e.preventDefault();
3370
- hubPost({ type: 'hub:keydown', key: e.key, code: e.code, ctrl: e.ctrlKey, alt: e.altKey, shift: e.shiftKey });
3371
- };
3372
- if (e.ctrlKey && e.altKey && (e.key === 'ArrowLeft' || e.key === 'ArrowRight')) {
3373
- fwd();
3374
- }
3375
- // Own branch: the Alt+digit case below requires !ctrlKey. The hub owns the Ctrl+Alt+letter
3376
- // keymap and ignores unbound letters.
3377
- if (e.ctrlKey && e.altKey && !e.shiftKey && !e.metaKey && (/^[a-z]$/i.test(e.key) || /^Key[A-Z]$/.test(e.code))) {
3378
- fwd();
3379
- }
3380
- if (e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey && (/^[1-9]$/.test(e.key) || /^Digit[1-9]$/.test(e.code))) {
3381
- fwd();
3382
- }
3384
+ if (!isHubKey(e)) return;
3385
+ e.preventDefault();
3386
+ hubPost({ type: 'hub:keydown', key: e.key, code: e.code, ctrl: e.ctrlKey, alt: e.altKey, shift: e.shiftKey });
3383
3387
  });
3384
3388
  })();
3385
3389
 
3390
+ // The combos the hub binds, from its hub:keys message. Null until one arrives: a hub from before
3391
+ // hub:keys sends none, and the fallback filter below is what such a hub expects.
3392
+ let hubKeys = null;
3393
+
3394
+ // A copy of the hub's comboOf(): its names must match the hub:keys list.
3395
+ function hubCombo(e) {
3396
+ const lower = (e.key || '').toLowerCase();
3397
+ const m = /^(?:Key|Digit)([A-Z1-9])$/.exec(e.code || '');
3398
+ const key = /^[a-z1-9]$/.test(lower) ? lower : m ? m[1].toLowerCase() : e.key;
3399
+ const mods = [e.ctrlKey && 'ctrl', e.altKey && 'alt', e.shiftKey && 'shift', e.metaKey && 'meta'];
3400
+ return [...mods, key].filter(Boolean).join('+');
3401
+ }
3402
+
3403
+ function isHubKey(e) {
3404
+ if (hubKeys) return hubKeys.has(hubCombo(e));
3405
+ if (e.ctrlKey && e.altKey && (e.key === 'ArrowLeft' || e.key === 'ArrowRight')) return true;
3406
+ // Own branch: the Alt+digit case below requires !ctrlKey. The hub owns the Ctrl+Alt+letter
3407
+ // keymap and ignores unbound letters.
3408
+ if (e.ctrlKey && e.altKey && !e.shiftKey && !e.metaKey && (/^[a-z]$/i.test(e.key) || /^Key[A-Z]$/.test(e.code))) {
3409
+ return true;
3410
+ }
3411
+ return e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey && (/^[1-9]$/.test(e.key) || /^Digit[1-9]$/.test(e.code));
3412
+ }
3413
+
3386
3414
  // Hoisted out of initHubTheme so initHubProject can share it.
3387
3415
  const hubOrigin = () => (window.__HUB__?.url ? new URL(window.__HUB__.url).origin : null);
3388
3416
 
@@ -3430,6 +3458,14 @@ window.hubNavigate = function hubNavigate(app, url) {
3430
3458
  });
3431
3459
  })();
3432
3460
 
3461
+ (function initHubKeys() {
3462
+ window.addEventListener('message', (e) => {
3463
+ if (e.source !== window.parent || e.origin !== hubOrigin()) return;
3464
+ if (e.data?.type !== 'hub:keys' || !Array.isArray(e.data.keys)) return;
3465
+ hubKeys = new Set(e.data.keys.filter((k) => typeof k === 'string'));
3466
+ });
3467
+ })();
3468
+
3433
3469
  (function initHubActive() {
3434
3470
  window.addEventListener('message', (e) => {
3435
3471
  if (e.source !== window.parent || e.origin !== hubOrigin()) return;
package/public/style.css CHANGED
@@ -12,7 +12,7 @@
12
12
  --text-muted: #7d808a;
13
13
  --accent: #e86f33;
14
14
  --accent-text: #f0a070;
15
- --accent-dim: rgba(232, 111, 51, 0.22);
15
+ --accent-dim: rgba(232, 111, 51, 0.25);
16
16
  --accent-glow: rgba(232, 111, 51, 0.55);
17
17
  --chart-fill: rgba(232, 111, 51, 0.32);
18
18
  --success: #3ecf8e;
@@ -2185,17 +2185,17 @@ kbd {
2185
2185
  body.light {
2186
2186
  --bg-deep: #e8e6e3;
2187
2187
  --bg-surface: #efede9;
2188
- --bg-elevated: #f7f4f0;
2188
+ --bg-elevated: #fbfaf9;
2189
2189
  --bg-hover: #d2d0cc;
2190
2190
  --border: #cfcbc4;
2191
2191
  --text-primary: #0a0a0a;
2192
- --text-secondary: #444444;
2193
- --text-tertiary: #666666;
2194
- --text-muted: #888888;
2192
+ --text-secondary: #2c2c2c;
2193
+ --text-tertiary: #565656;
2194
+ --text-muted: #767676;
2195
2195
  --accent-text: #a0501c;
2196
- --accent-dim: rgba(232, 111, 51, 0.18);
2196
+ --accent-dim: rgba(184, 90, 32, 0.18);
2197
2197
  --accent-glow: rgba(232, 111, 51, 0.5);
2198
- --chart-fill: rgba(232, 111, 51, 0.28);
2198
+ --chart-fill: rgba(232, 111, 51, 0.32);
2199
2199
  --success: #1a8a5a;
2200
2200
  --success-dim: rgba(26, 138, 90, 0.15);
2201
2201
  --warning: #b07d0a;
package/public/themes.css CHANGED
@@ -38,6 +38,40 @@ body.light .theme-swatch-vitesse { --sw-bg: #f7f7f7; --sw-accent: #1c6b48; --sw-
38
38
  .theme-swatch-synthwave { --sw-bg: #262335; --sw-accent: #ff7edb; --sw-ink: #ffffff; --sw-border: #495495; }
39
39
  body.light .theme-swatch-synthwave { --sw-bg: #efecf6; --sw-accent: #c936a6; --sw-ink: #241b2f; --sw-border: #c5bcdd; }
40
40
 
41
+ /* Ember */
42
+ body:not([data-color-theme]):not(.light) {
43
+ --accent: #e86f33;
44
+ --accent-text: #f0a070;
45
+ --accent-dim: rgba(232,111,51,0.25);
46
+ --accent-glow: rgba(232, 111, 51, 0.55);
47
+ --bg-deep: #101114;
48
+ --bg-surface: #16181c;
49
+ --bg-elevated: #1e2025;
50
+ --bg-hover: #282a30;
51
+ --border: #363840;
52
+ --text-primary: #f0f1f3;
53
+ --text-secondary: #c2c4c9;
54
+ --text-tertiary: #9a9da5;
55
+ --text-muted: #7d808a;
56
+ --chart-fill: rgba(232, 111, 51, 0.32);
57
+ }
58
+ body.light:not([data-color-theme]) {
59
+ --accent: #e86f33;
60
+ --accent-text: #a0501c;
61
+ --accent-dim: rgba(184,90,32,0.18);
62
+ --accent-glow: rgba(232, 111, 51, 0.5);
63
+ --bg-deep: #e8e6e3;
64
+ --bg-surface: #efede9;
65
+ --bg-elevated: #fbfaf9;
66
+ --bg-hover: #d2d0cc;
67
+ --border: #cfcbc4;
68
+ --text-primary: #0a0a0a;
69
+ --text-secondary: #2c2c2c;
70
+ --text-tertiary: #565656;
71
+ --text-muted: #767676;
72
+ --chart-fill: rgba(232, 111, 51, 0.32);
73
+ }
74
+
41
75
  /* Gruvbox */
42
76
  body[data-color-theme="gruvbox"]:not(.light) {
43
77
  --accent: #fe8019;
package/server.js CHANGED
@@ -2,11 +2,11 @@
2
2
  'use strict';
3
3
 
4
4
  const express = require('express');
5
- const path = require('path');
6
- const fs = require('fs');
7
- const { createReadStream } = require('fs');
8
- const { createInterface } = require('readline');
9
- const os = require('os');
5
+ const path = require('node:path');
6
+ const fs = require('node:fs');
7
+ const { createReadStream } = fs;
8
+ const { createInterface } = require('node:readline');
9
+ const os = require('node:os');
10
10
  const { createNetGuard } = require('./lib/net-guard');
11
11
  const { probeUsage, normalizeUsage } = require('./lib/usage-probe');
12
12
 
@@ -327,7 +327,8 @@ function rangeBucket(range) {
327
327
  // bucket total, unlike modelDistribution which drops them entirely.
328
328
  function addBucketModelCost(acc, key, model, cost) {
329
329
  const mk = isRealModel(model) ? model : 'other';
330
- (acc[key] ||= {})[mk] = (acc[key][mk] || 0) + cost;
330
+ acc[key] ||= {};
331
+ acc[key][mk] = (acc[key][mk] || 0) + cost;
331
332
  }
332
333
 
333
334
  // Gap-filled so empty buckets stay visible in the chart. The bucket total is derived from