ccusage 20.0.20 → 20.0.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ccusage",
3
- "version": "20.0.20",
3
+ "version": "20.0.21",
4
4
  "description": "Analyze coding (agent) CLI token usage and costs from local data",
5
5
  "homepage": "https://github.com/ccusage/ccusage#readme",
6
6
  "bugs": {
@@ -26,12 +26,12 @@
26
26
  "@types/node": "^24.13.3"
27
27
  },
28
28
  "optionalDependencies": {
29
- "@ccusage/ccusage-darwin-arm64": "20.0.20",
30
- "@ccusage/ccusage-darwin-x64": "20.0.20",
31
- "@ccusage/ccusage-linux-arm64": "20.0.20",
32
- "@ccusage/ccusage-linux-x64": "20.0.20",
33
- "@ccusage/ccusage-win32-x64": "20.0.20",
34
- "@ccusage/ccusage-win32-arm64": "20.0.20"
29
+ "@ccusage/ccusage-darwin-arm64": "20.0.21",
30
+ "@ccusage/ccusage-darwin-x64": "20.0.21",
31
+ "@ccusage/ccusage-linux-arm64": "20.0.21",
32
+ "@ccusage/ccusage-linux-x64": "20.0.21",
33
+ "@ccusage/ccusage-win32-arm64": "20.0.21",
34
+ "@ccusage/ccusage-win32-x64": "20.0.21"
35
35
  },
36
36
  "scripts": {
37
37
  "build": "scripts/ensure-native-binary.nu && publint"
package/src/cli.js CHANGED
@@ -14,6 +14,9 @@ const require = createRequire(import.meta.url);
14
14
  * @typedef {{ error?: Error; signal?: NodeJS.Signals | null; status: number | null }} NativeSpawnResult
15
15
  * @typedef {(command: string, args: string[]) => Promise<NativeSpawnResult>} NativeSpawner
16
16
  * @typedef {{ argvEntry?: string; moduleUrl: string; realpathPath?: (path: string) => string }} MainModuleOptions
17
+ * @typedef {{ off: (signal: NodeJS.Signals, handler: () => void) => unknown; on: (signal: NodeJS.Signals, handler: () => void) => unknown }} SignalSource
18
+ * @typedef {{ kill: (signal: NodeJS.Signals) => unknown; on: import('node:events').EventEmitter['on'] }} NativeChild
19
+ * @typedef {(command: string, args: string[], options: { stdio: 'inherit' }) => NativeChild} NativeSpawnProcess
17
20
  */
18
21
 
19
22
  /**
@@ -157,13 +160,47 @@ function isMainModule({ argvEntry = process.argv[1], moduleUrl, realpathPath = r
157
160
  }
158
161
 
159
162
  /**
163
+ * @param {string} [platform]
164
+ * @returns {NodeJS.Signals[]}
165
+ */
166
+ function getForwardedSignals(platform = process.platform) {
167
+ if (platform === 'win32') {
168
+ return ['SIGINT', 'SIGBREAK', 'SIGHUP'];
169
+ }
170
+
171
+ return ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT'];
172
+ }
173
+
174
+ /**
175
+ * @param {{ signalSource?: SignalSource; spawnProcess?: NativeSpawnProcess; platform?: string }} [options]
160
176
  * @returns {NativeSpawner}
161
177
  */
162
- function createNativeSpawner() {
178
+ function createNativeSpawner({
179
+ platform = process.platform,
180
+ signalSource = process,
181
+ spawnProcess = spawn,
182
+ } = {}) {
183
+ const forwardedSignals = getForwardedSignals(platform);
184
+
163
185
  return async (command, args) =>
164
186
  new Promise((resolve) => {
165
- const child = spawn(command, args, { stdio: 'inherit' });
187
+ const child = spawnProcess(command, args, { stdio: 'inherit' });
188
+ const signalHandlers = new Map();
189
+ const cleanup = () => {
190
+ for (const [signal, handler] of signalHandlers) {
191
+ signalSource.off(signal, handler);
192
+ }
193
+ signalHandlers.clear();
194
+ };
195
+ /**
196
+ * @param {NodeJS.Signals} signal
197
+ */
198
+ const handleSignal = (signal) => {
199
+ child.kill(signal);
200
+ };
201
+
166
202
  child.on('error', (error) => {
203
+ cleanup();
167
204
  resolve({
168
205
  error,
169
206
  signal: null,
@@ -171,11 +208,20 @@ function createNativeSpawner() {
171
208
  });
172
209
  });
173
210
  child.on('exit', (status, signal) => {
211
+ cleanup();
174
212
  resolve({
175
213
  signal,
176
214
  status,
177
215
  });
178
216
  });
217
+
218
+ for (const signal of forwardedSignals) {
219
+ const handler = () => handleSignal(signal);
220
+ try {
221
+ signalSource.on(signal, handler);
222
+ signalHandlers.set(signal, handler);
223
+ } catch {}
224
+ }
179
225
  });
180
226
  }
181
227
 
@@ -215,4 +261,10 @@ if (isMainModule({ moduleUrl: import.meta.url })) {
215
261
  process.exitCode = await runCli(process.argv.slice(2));
216
262
  }
217
263
 
218
- export { ensureNativeBinaryExecutable, isMainModule, resolveCliRuntime, resolveNativeBinary };
264
+ export {
265
+ createNativeSpawner,
266
+ ensureNativeBinaryExecutable,
267
+ isMainModule,
268
+ resolveCliRuntime,
269
+ resolveNativeBinary,
270
+ };