cc-costline 0.3.1 → 0.3.2

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/dist/statusline.js +25 -5
  2. package/package.json +1 -1
@@ -59,6 +59,10 @@ function getCcclubRank(sessionId) {
59
59
  staleData = cached.data ?? null;
60
60
  if (cached.sessionId === sessionId)
61
61
  return staleData;
62
+ // If cache is less than 2 minutes old, reuse it to avoid rate limits
63
+ const cacheAge = Date.now() - (cached.timestamp || 0);
64
+ if (cacheAge < 600_000)
65
+ return staleData;
62
66
  }
63
67
  catch { }
64
68
  }
@@ -79,10 +83,14 @@ function getCcclubRank(sessionId) {
79
83
  if (!me)
80
84
  return staleData;
81
85
  const result = { rank: me.rank, total: rankings.length, cost: me.costUSD };
82
- writeFileSync(cacheFile, JSON.stringify({ sessionId, data: result }), "utf-8");
86
+ writeFileSync(cacheFile, JSON.stringify({ sessionId, data: result, timestamp: Date.now() }), "utf-8");
83
87
  return result;
84
88
  }
85
89
  catch {
90
+ try {
91
+ writeFileSync(cacheFile, JSON.stringify({ sessionId, data: staleData, timestamp: Date.now() }), "utf-8");
92
+ }
93
+ catch { }
86
94
  return staleData;
87
95
  }
88
96
  }
@@ -107,6 +115,10 @@ function getClaudeUsage(sessionId) {
107
115
  staleData = cached.data ?? null;
108
116
  if (cached.sessionId === sessionId)
109
117
  return staleData;
118
+ // If cache is less than 2 minutes old, reuse it to avoid rate limits
119
+ const cacheAge = now - (cached.timestamp || 0);
120
+ if (cacheAge < 600_000)
121
+ return staleData;
110
122
  }
111
123
  catch { }
112
124
  }
@@ -124,10 +136,13 @@ function getClaudeUsage(sessionId) {
124
136
  if (expiresAt && Date.now() / 1000 > expiresAt)
125
137
  return null;
126
138
  const apiUrl = "https://api.anthropic.com/api/oauth/usage";
127
- const curlCmd = `curl -sf "${apiUrl}" -H "Authorization: Bearer ${accessToken}" -H "anthropic-beta: oauth-2025-04-20" -H "User-Agent: claude-code/2.1.5"`;
139
+ const curlCmd = `curl -sf "${apiUrl}" -H "Authorization: Bearer ${accessToken}" -H "anthropic-beta: oauth-2025-04-20"`;
128
140
  const response = execSync(curlCmd, { encoding: "utf-8", timeout: 5000 });
129
- if (!response)
130
- return null;
141
+ if (!response) {
142
+ // API failed — write cache with null data to prevent retry flood
143
+ writeFileSync(cacheFile, JSON.stringify({ sessionId, data: null, timestamp: now }), "utf-8");
144
+ return staleData;
145
+ }
131
146
  const data = JSON.parse(response);
132
147
  const parseUtil = (val) => {
133
148
  if (typeof val === "number")
@@ -172,10 +187,15 @@ function getClaudeUsage(sessionId) {
172
187
  const result = { fiveHour, sevenDay };
173
188
  if (fiveHourResetsAt)
174
189
  result.fiveHourResetsAt = fiveHourResetsAt;
175
- writeFileSync(cacheFile, JSON.stringify({ sessionId, data: result }), "utf-8");
190
+ writeFileSync(cacheFile, JSON.stringify({ sessionId, data: result, timestamp: now }), "utf-8");
176
191
  return result;
177
192
  }
178
193
  catch {
194
+ // Write cache with stale/null data to prevent retry flood on persistent failures
195
+ try {
196
+ writeFileSync(cacheFile, JSON.stringify({ sessionId, data: staleData, timestamp: now }), "utf-8");
197
+ }
198
+ catch { }
179
199
  return staleData;
180
200
  }
181
201
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cc-costline",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Enhanced statusline for Claude Code with cost tracking, usage limits, and leaderboard",
5
5
  "type": "module",
6
6
  "bin": {