claude-code-cost 1.1.0 → 1.2.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 +41 -14
- package/package.json +1 -1
- package/public/app.js +6 -2
- package/public/vendor/claude-hub-sdk.js +13 -3
- package/server.js +9 -8
package/lib/net-guard.js
CHANGED
|
@@ -37,11 +37,14 @@ const PORT_RE = /^[0-9]{1,5}$/;
|
|
|
37
37
|
// port dynamic, so only the host is allowlisted.
|
|
38
38
|
const validPort = (p) => PORT_RE.test(p) && Number(p) >= 1 && Number(p) <= 65535;
|
|
39
39
|
const KEEP_ALIVE_MS = 65000;
|
|
40
|
+
// A whole IPv4 literal: a prefix test would let a rebinding name like 127.evil.com through.
|
|
41
|
+
const LOOPBACK_V4_RE = /^127(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$/;
|
|
40
42
|
|
|
41
43
|
function isLoopbackAddress(host) {
|
|
42
44
|
if (!host) return false;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
// An Origin or URL host carries an IPv6 literal in brackets.
|
|
46
|
+
const h = String(host).toLowerCase().replace(/^\[(.*)\]$/, '$1');
|
|
47
|
+
return LOOPBACK_HOSTS.has(h) || LOOPBACK_V4_RE.test(h);
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
// Hand-parsed rather than via new URL(), which is far more permissive than we
|
|
@@ -102,7 +105,7 @@ function getFlag(name) {
|
|
|
102
105
|
}
|
|
103
106
|
|
|
104
107
|
/**
|
|
105
|
-
* @param {{appName?: string}} [config]
|
|
108
|
+
* @param {{appName?: string, selfFramedPaths?: string[]}} [config]
|
|
106
109
|
*/
|
|
107
110
|
function createNetGuard(config = {}) {
|
|
108
111
|
const appName = config.appName || 'This app';
|
|
@@ -216,9 +219,18 @@ function createNetGuard(config = {}) {
|
|
|
216
219
|
CSP = "frame-ancestors 'none'";
|
|
217
220
|
}
|
|
218
221
|
|
|
219
|
-
|
|
222
|
+
// Alone, these pages may be framed by this app's own port under its loopback names, for a page
|
|
223
|
+
// that frames itself from its other name to get its own renderer process.
|
|
224
|
+
const selfFramed = new Set(config.selfFramedPaths || []);
|
|
225
|
+
|
|
226
|
+
function frameGuard(req, res, next) {
|
|
220
227
|
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
221
228
|
res.setHeader('Referrer-Policy', 'no-referrer');
|
|
229
|
+
if (!hubOrigin && selfFramed.has(req.path)) {
|
|
230
|
+
const port = req.socket.localPort;
|
|
231
|
+
res.setHeader('Content-Security-Policy', `frame-ancestors 'self' http://localhost:${port} http://127.0.0.1:${port}`);
|
|
232
|
+
return next();
|
|
233
|
+
}
|
|
222
234
|
res.setHeader('Content-Security-Policy', CSP);
|
|
223
235
|
if (!hubOrigin) res.setHeader('X-Frame-Options', 'DENY');
|
|
224
236
|
next();
|
|
@@ -232,7 +244,10 @@ function createNetGuard(config = {}) {
|
|
|
232
244
|
* the OS resolver orders A/AAAA records - binding only 127.0.0.1 breaks hosts
|
|
233
245
|
* where `localhost` resolves to ::1, and vice versa. Doing it on the *resolved*
|
|
234
246
|
* port means every existing URL, startup banner and postMessage origin is
|
|
235
|
-
* unchanged.
|
|
247
|
+
* unchanged. A family the host does not have is skipped. A port that another
|
|
248
|
+
* process holds on the other family is treated as busy: the primary closes and
|
|
249
|
+
* emits EADDRINUSE (port 0 picks another random port instead), because that process would answer `localhost:<port>` (and a
|
|
250
|
+
* cross-site frame on it) in our name. onReady waits for the second bind.
|
|
236
251
|
*
|
|
237
252
|
* Returns the primary server, so callers keep attaching their own 'error'
|
|
238
253
|
* handler for the EADDRINUSE fallback.
|
|
@@ -259,18 +274,30 @@ function createNetGuard(config = {}) {
|
|
|
259
274
|
};
|
|
260
275
|
const server = makeServer(app);
|
|
261
276
|
let secondary = null;
|
|
277
|
+
let randomTries = 0;
|
|
262
278
|
|
|
263
279
|
server.on('listening', () => {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
280
|
+
const boundPort = server.address().port;
|
|
281
|
+
actualPort = boundPort;
|
|
282
|
+
// Not actualPort: the hub listens several servers through one guard, and another one can bind
|
|
283
|
+
// while this one waits for its second family.
|
|
284
|
+
const ready = () => { if (onReady) onReady(boundPort); };
|
|
285
|
+
if (EXPOSED) return ready();
|
|
286
|
+
const otherFamily = BIND_HOST.includes(':') ? '127.0.0.1' : '::1';
|
|
287
|
+
try {
|
|
288
|
+
secondary = makeServer(app);
|
|
289
|
+
secondary.once('listening', ready);
|
|
290
|
+
secondary.on('error', (err) => {
|
|
291
|
+
if (err.code !== 'EADDRINUSE') return ready();
|
|
292
|
+
// A caller asking for port 0 has no fallback of its own, so it gets another random port here.
|
|
293
|
+
if (port === 0 && ++randomTries < 5) return server.close(() => server.listen(0, BIND_HOST));
|
|
294
|
+
server.close();
|
|
295
|
+
server.emit('error', err);
|
|
296
|
+
});
|
|
297
|
+
secondary.listen({ host: otherFamily, port: boundPort, ipv6Only: otherFamily === '::1' });
|
|
298
|
+
} catch {
|
|
299
|
+
ready();
|
|
272
300
|
}
|
|
273
|
-
if (onReady) onReady(actualPort);
|
|
274
301
|
});
|
|
275
302
|
server.on('close', () => { try { if (secondary) secondary.close(); } catch { /* already gone */ } });
|
|
276
303
|
|
package/package.json
CHANGED
package/public/app.js
CHANGED
|
@@ -3385,8 +3385,12 @@ hub.onActive((active) => {
|
|
|
3385
3385
|
if (hubActive) refreshIfStale();
|
|
3386
3386
|
});
|
|
3387
3387
|
|
|
3388
|
-
// The hub owns the abs->encoded transform, so cost never converts anything itself.
|
|
3389
|
-
|
|
3388
|
+
// The hub owns the abs->encoded transform, so cost never converts anything itself. A repo's
|
|
3389
|
+
// linked worktrees have their own project dirs, so the scope is the comma-joined list of all.
|
|
3390
|
+
hub.subscribe('project.changed', (p) => {
|
|
3391
|
+
const keys = p?.encoded ? [p.encoded, ...(p.worktrees ?? []).map((w) => w.encoded)] : [];
|
|
3392
|
+
applyScope(keys.join(',') || null, p?.name);
|
|
3393
|
+
});
|
|
3390
3394
|
|
|
3391
3395
|
hub.handle('session.cost', (p) => p.session && navigateToDetail(p.session));
|
|
3392
3396
|
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
// claude-hub-sdk 1.
|
|
1
|
+
// claude-hub-sdk 1.2.0 (sha256 62c7d9f477b2). Copied by npm run sdk:sync in claude-code-hub. Do not edit.
|
|
2
2
|
// Claude Code Hub SDK stub: what an app serves when no hub runs it. Under a hub, the hub hands the
|
|
3
3
|
// app its real SDK (client.js) in place of this file. The API must match client.js; a test checks it.
|
|
4
4
|
((root) => {
|
|
5
5
|
'use strict';
|
|
6
6
|
|
|
7
|
+
// A copy of client.js comboOf(); a test checks they match.
|
|
8
|
+
function comboOf(e) {
|
|
9
|
+
const lower = typeof e.key === 'string' ? e.key.toLowerCase() : '';
|
|
10
|
+
const m = /^(?:Key|Digit)([A-Z1-9])$/.exec(e.code || '');
|
|
11
|
+
const key = /^[a-z1-9]$/.test(lower) ? lower : m ? m[1].toLowerCase() : e.key;
|
|
12
|
+
const mods = [e.ctrlKey && 'ctrl', e.altKey && 'alt', e.shiftKey && 'shift', e.metaKey && 'meta'];
|
|
13
|
+
return [...mods, key].filter(Boolean).join('+');
|
|
14
|
+
}
|
|
15
|
+
|
|
7
16
|
function createClaudeHub(win) {
|
|
8
17
|
let hub = null;
|
|
9
18
|
const off = () => {};
|
|
@@ -33,6 +42,7 @@
|
|
|
33
42
|
},
|
|
34
43
|
can: (action) => !!standaloneFn(action),
|
|
35
44
|
forwards: () => false,
|
|
45
|
+
forwardCombos: () => [],
|
|
36
46
|
closeGuard() {},
|
|
37
47
|
openExternal(url) {
|
|
38
48
|
win.open(url, '_blank', 'noopener');
|
|
@@ -42,9 +52,9 @@
|
|
|
42
52
|
return hub;
|
|
43
53
|
}
|
|
44
54
|
|
|
45
|
-
return { connect };
|
|
55
|
+
return { connect, comboOf };
|
|
46
56
|
}
|
|
47
57
|
|
|
48
|
-
if (typeof module === 'object' && module.exports) module.exports = { createClaudeHub };
|
|
58
|
+
if (typeof module === 'object' && module.exports) module.exports = { createClaudeHub, comboOf };
|
|
49
59
|
else root.ClaudeHub = createClaudeHub(root);
|
|
50
60
|
})(typeof window !== 'undefined' ? window : globalThis);
|
package/server.js
CHANGED
|
@@ -354,17 +354,19 @@ function buildModelDistribution(modelCosts) {
|
|
|
354
354
|
.sort((a, b) => b.cost - a.cost);
|
|
355
355
|
}
|
|
356
356
|
|
|
357
|
-
// onlyKey narrows the scan to
|
|
358
|
-
// skips the per-project readdir
|
|
357
|
+
// onlyKey narrows the scan to encoded project dirs, comma-separated: a repo and its linked
|
|
358
|
+
// worktrees are separate dirs. The dir name *is* the key, so this skips the per-project readdir
|
|
359
|
+
// and per-file statSync for everything else.
|
|
359
360
|
function scanProjectDirs(cutoffDate, onlyKey) {
|
|
360
361
|
const projects = new Map();
|
|
361
362
|
const cutoffMs = cutoffDate ? cutoffDate.getTime() : 0;
|
|
363
|
+
const only = onlyKey ? new Set(onlyKey.split(',')) : null;
|
|
362
364
|
for (const baseDir of getProjectsDirs()) {
|
|
363
365
|
try {
|
|
364
366
|
const entries = fs.readdirSync(baseDir, { withFileTypes: true });
|
|
365
367
|
for (const entry of entries) {
|
|
366
368
|
if (!entry.isDirectory()) continue;
|
|
367
|
-
if (
|
|
369
|
+
if (only && !only.has(entry.name)) continue;
|
|
368
370
|
const projDir = path.join(baseDir, entry.name);
|
|
369
371
|
const files = [];
|
|
370
372
|
try {
|
|
@@ -984,12 +986,11 @@ async function getProjectSessionsData(encodedPath, range) {
|
|
|
984
986
|
|
|
985
987
|
const pricing = await fetchPricing();
|
|
986
988
|
const w = resolveWindow(range);
|
|
987
|
-
const projects = scanProjectDirs(w.cutoff);
|
|
988
|
-
const proj = projects.get(encodedPath);
|
|
989
|
+
const projects = scanProjectDirs(w.cutoff, encodedPath);
|
|
989
990
|
const bucket = rangeBucket(range);
|
|
990
|
-
if (!
|
|
991
|
+
if (!projects.size) return { sessions: [], costSeries: { bucket, points: [] }, modelDistribution: [] };
|
|
991
992
|
|
|
992
|
-
const sessions = await loadProjectData(
|
|
993
|
+
const sessions = await loadProjectData([...projects.values()].flatMap((p) => p.files), pricing);
|
|
993
994
|
const result = [];
|
|
994
995
|
const bucketKey = BUCKETS[bucket].key;
|
|
995
996
|
const bucketModelCosts = {};
|
|
@@ -1446,7 +1447,7 @@ function parseCustomRange(query) {
|
|
|
1446
1447
|
// dropping the scope.
|
|
1447
1448
|
function parseProject(raw) {
|
|
1448
1449
|
if (raw === undefined || raw === '') return undefined;
|
|
1449
|
-
if (typeof raw !== 'string' || !/^[A-Za-z0-9._-]
|
|
1450
|
+
if (typeof raw !== 'string' || !/^[A-Za-z0-9._-]+(,[A-Za-z0-9._-]+)*$/.test(raw)) return null;
|
|
1450
1451
|
return raw;
|
|
1451
1452
|
}
|
|
1452
1453
|
|