cliprobe 0.3.0 → 0.4.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.
Files changed (3) hide show
  1. package/README.md +30 -4
  2. package/dist/cli.js +116 -9
  3. package/package.json +5 -3
package/README.md CHANGED
@@ -7,7 +7,7 @@ npx cliprobe -- esbuild
7
7
  ```
8
8
 
9
9
  ```
10
- cliprobe 0.1.0: closed-pipe, color, ctrl-c, control-chars checks for esbuild
10
+ cliprobe 0.4.0: closed-pipe, color, pipe-escapes, stdin, ctrl-c, control-chars checks for esbuild
11
11
 
12
12
  closed-pipe
13
13
  FAIL esbuild --version crashed when stdout was closed (exit 0 -> 1)
@@ -19,20 +19,28 @@ color
19
19
  PASS esbuild --version no color in a pipe or in a terminal
20
20
  PASS esbuild --help colors only in a terminal, and NO_COLOR turns it off
21
21
 
22
+ pipe-escapes
23
+ PASS esbuild --version no cursor, erase, hyperlink, title or redraw codes in a pipe
24
+ PASS esbuild --help no cursor, erase, hyperlink, title or redraw codes in a pipe
25
+
26
+ stdin
27
+ PASS esbuild --version finished without waiting for stdin
28
+ PASS esbuild --help finished without waiting for stdin
29
+
22
30
  ctrl-c
23
31
  SKIP esbuild needs probe args that keep it running, such as a watch or serve command
24
32
 
25
33
  control-chars
26
34
  SKIP esbuild no {fixture} in the arguments; put it where the command takes a path, e.g. ls {fixture}
27
35
 
28
- 2 failed, 0 warned, 2 passed, 2 skipped
36
+ 2 failed, 0 warned, 6 passed, 2 skipped
29
37
  ```
30
38
 
31
39
  ## Why
32
40
 
33
41
  When a program's output goes into a reader that stops early (`| head`, `grep -q`, a closed agent pipe), its next write to stdout fails. A well-behaved program exits quietly. Many Node.js CLIs instead crash with `Error: write EPIPE`, a stack trace, and exit code 1. That breaks scripts running under `set -o pipefail` and confuses agents that read stderr.
34
42
 
35
- In a census of the 100 most-downloaded npm packages that ship a command (119 commands, run offline in a throwaway container), **9 crashed this way every time**: esbuild, browserslist, update-browserslist-db, autoprefixer, husky, tsconfck, ts-jest, rolldown, and tsx on `--version`. The best-known existing CLI auditor rated the first 8 as passing its SIGPIPE check. The same census found two commands that print color codes into a pipe (`giget --help`, `tsx --help`), and no false failures: every `FAIL` reproduced by hand. (A census of `--help` and `--version` can't exercise ctrl-c or control-chars, which need a command that keeps running or takes a path; their fixtures and real-tool tests are in `test/`.)
43
+ In a census of the 100 most-downloaded npm packages that ship a command (119 commands, run offline in a throwaway container), **9 crashed this way every time**: esbuild, browserslist, update-browserslist-db, autoprefixer, husky, tsconfck, ts-jest, rolldown, and tsx on `--version`. The best-known existing CLI auditor rated the first 8 as passing its SIGPIPE check. The same census found two commands that print color codes into a pipe (`giget --help`, `tsx --help`), five whose `--help` or `--version` waits for stdin (he, cssesc, ansi-html, webpack and webpack-dev-server), and no false failures: every `FAIL` reproduced by hand. (A census of `--help` and `--version` can't exercise ctrl-c or control-chars, which need a command that keeps running or takes a path; their fixtures and real-tool tests are in `test/`.)
36
44
 
37
45
  The other checks cover the rest of what goes wrong between a program and the thing reading it: color codes leaking into logs and agent transcripts, Ctrl-C that is ignored or ends in a stack trace, and file names that can send escape sequences to your terminal.
38
46
 
@@ -84,7 +92,7 @@ In JUnit, each check is a test suite and each probe a test case: `FAIL` is a fai
84
92
 
85
93
  ## Checks
86
94
 
87
- A default run does the five checks below: closed-pipe, color, pipe-escapes, ctrl-c and control-chars. Each one reports evidence, never a score. Two info checks, startup and baseline, run only when you name them with `--check`.
95
+ A default run does the six checks below: closed-pipe, color, pipe-escapes, stdin, ctrl-c and control-chars. Each one reports evidence, never a score. Two info checks, startup and baseline, run only when you name them with `--check`.
88
96
 
89
97
  **closed-pipe**. cliprobe runs the command twice: once normally, then with stdout set to a pipe whose reading end is already closed, so the first write fails every time, even for fast native programs. Results:
90
98
 
@@ -116,6 +124,17 @@ Many classic tools (git, BSD `ls` and `grep`) color correctly but don't implemen
116
124
 
117
125
  It only warns for now, never fails. In the census of the top 100 npm CLIs, no `--help` or `--version` printed any of these codes, which says nothing about their spinners: probe a command that does real work, such as an install or a build.
118
126
 
127
+ **stdin**. Agents and CI runners often start a command with stdin as a pipe that stays open and never receives anything. A program that reads stdin, or asks a question, then waits forever. cliprobe runs the command three ways: with stdin closed (`/dev/null`), with an open pipe, and, if that hangs for 3 seconds, with a pipe it closes after 1 second. Finishing right after the close proves stdin was the cause. Results:
128
+
129
+ | Verdict | Meaning |
130
+ | ------- | ------------------------------------------------------------------------------------------------------------------------- |
131
+ | `PASS` | Finished without waiting for stdin |
132
+ | `FAIL` | `--version` or `--help` waited for stdin before answering: they must never need input |
133
+ | `WARN` | A probe you wrote printed an unfinished line, like a question, then waited: it prompts even though stdin isn't a terminal |
134
+ | `SKIP` | A probe you wrote waited silently, like a filter (`grep pattern`), or the hang wasn't caused by stdin |
135
+
136
+ In the census of the top 100 npm CLIs, 9 of 238 `--help` and `--version` runs failed, each confirmed by hand. he, cssesc and ansi-html read stdin before looking at their arguments. webpack and webpack-dev-server, when webpack-cli is missing, ask whether to install it and wait for an answer without checking for a terminal. With no arguments, 17 tools waited on stdin as filters by design, which is why probes you write never fail this check.
137
+
119
138
  **ctrl-c**. cliprobe lets the command run for 1 second, then sends SIGINT to its process group, which is what a terminal does when you press Ctrl-C, and gives it 3 seconds to exit. Results:
120
139
 
121
140
  | Verdict | Meaning |
@@ -165,6 +184,13 @@ const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
165
184
 
166
185
  `util.styleText` makes that decision for you on Node 22.13 and later (its `validateStream` option, on by default), and most color libraries check `isTTY` already, but some turn color on whenever `TERM` allows it or `CI` is set.
167
186
 
187
+ **stdin:** look at your arguments before reading stdin, so `--help` and `--version` answer at once, and only prompt when stdin is a terminal:
188
+
189
+ ```js
190
+ if (process.stdin.isTTY) askTheQuestion();
191
+ else useTheDefault(); // or fail with a message saying which flag to pass
192
+ ```
193
+
168
194
  **ctrl-c:** Node's default already does the right thing, dying by SIGINT. If you add a handler to clean up, finish by exiting 130 rather than 0, and don't throw from it:
169
195
 
170
196
  ```js
package/dist/cli.js CHANGED
@@ -29,7 +29,7 @@ const STDOUT_CAP = 1048576;
29
29
  * whose read end is closed before the program starts, so its first write to
30
30
  * stdout fails (EPIPE, or SIGPIPE for programs that keep the default handler).
31
31
  */
32
- function run$1(command, args, { closedStdout = false, timeout, env: overrides = {}, beforeKill, signal }) {
32
+ function run$1(command, args, { closedStdout = false, timeout, env: overrides = {}, beforeKill, signal, stdin = "null" }) {
33
33
  const { dir, env: base } = sandbox();
34
34
  const env = { ...base };
35
35
  for (const [key, value] of Object.entries(overrides)) if (value === void 0) delete env[key];
@@ -79,7 +79,7 @@ function run$1(command, args, { closedStdout = false, timeout, env: overrides =
79
79
  child = spawn(command, [...args], {
80
80
  env,
81
81
  stdio: [
82
- "ignore",
82
+ stdin === "null" ? "ignore" : "pipe",
83
83
  stdoutFd,
84
84
  "pipe"
85
85
  ],
@@ -108,9 +108,12 @@ function run$1(command, args, { closedStdout = false, timeout, env: overrides =
108
108
  process.kill(-child.pid, signal.name);
109
109
  } catch {}
110
110
  }, signal.afterMs);
111
+ const closeTimer = typeof stdin === "object" ? setTimeout(() => child.stdin?.destroy(), stdin.closeAfterMs) : void 0;
111
112
  child.on("exit", () => {
112
113
  exited = true;
113
114
  clearTimeout(signalTimer);
115
+ clearTimeout(closeTimer);
116
+ child.stdin?.destroy();
114
117
  });
115
118
  child.stdout?.on("data", (chunk) => {
116
119
  if (stdoutBytes < STDOUT_CAP) stdoutChunks.push(chunk);
@@ -433,6 +436,11 @@ function findControls(text) {
433
436
  });
434
437
  return hits.sort((a, b) => a.index - b.index).map((h) => h.hit);
435
438
  }
439
+ const ANY_ESCAPE = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g;
440
+ /** The text with CSI and OSC escape sequences (color, cursor, links, titles) removed. */
441
+ function stripEscapes(text) {
442
+ return text.replace(ANY_ESCAPE, "");
443
+ }
436
444
  let cached;
437
445
  /** Whether this machine can run a program under a PTY, detected once. */
438
446
  function ptySupport() {
@@ -1116,23 +1124,118 @@ async function startup(command, args, timeout) {
1116
1124
  medianMs: mid
1117
1125
  };
1118
1126
  }
1127
+ const startupCheck = {
1128
+ name: "startup",
1129
+ kind: "info",
1130
+ summary: "median wall time of --version, or of the given probe, over 5 runs",
1131
+ async run(target, ctx) {
1132
+ const args = target.explicit ? target.invocations[0] ?? [] : ["--version"];
1133
+ return [await startup(target.command, args, ctx.timeout)];
1134
+ }
1135
+ };
1136
+ // src/checks/stdin.ts
1137
+ /** How long the program gets with stdin left open before it counts as waiting. */
1138
+ const STDIN_WAIT_MS = 3e3;
1139
+ /** When the confirming run closes stdin, and how long it then gets to finish. */
1140
+ const STDIN_CLOSE_AFTER_MS = 1e3;
1141
+ /** The last line printed before waiting, when it reads like a question left on the line. */
1142
+ function promptLine(r) {
1143
+ const last = stripEscapes(r.stdout.toString("utf8") + r.stderr).split("\n").at(-1) ?? "";
1144
+ if (last.includes("\r") || last.trim() === "") return void 0;
1145
+ return last;
1146
+ }
1147
+ /**
1148
+ * `defaultProbe` is true for cliprobe's own `--version` and `--help` probes, which
1149
+ * must never wait for input. For probes the user wrote, waiting can be the point
1150
+ * (a filter such as `grep pattern`), so only a prompt-like wait warns.
1151
+ */
1152
+ function judgeStdin(runs, defaultProbe) {
1153
+ const { closed, open, closing } = runs;
1154
+ if (closed.spawnError) return {
1155
+ verdict: "error",
1156
+ reason: `could not start: ${closed.spawnError}`
1157
+ };
1158
+ if (closed.timedOut) return {
1159
+ verdict: "skip",
1160
+ reason: "doesn't finish even with stdin closed, so stdin isn't what it waits for"
1161
+ };
1162
+ if (!open || !open.timedOut) return {
1163
+ verdict: "pass",
1164
+ reason: "finished without waiting for stdin"
1165
+ };
1166
+ if (!closing || closing.timedOut) return {
1167
+ verdict: "skip",
1168
+ reason: "hung with stdin open, but closing stdin didn't end it, so the cause isn't proven"
1169
+ };
1170
+ const evidence = `${closed.durationMs} ms with stdin closed; still running after ${STDIN_WAIT_MS / 1e3} s with stdin open; finished ${closing.durationMs - STDIN_CLOSE_AFTER_MS} ms after stdin closed`;
1171
+ if (defaultProbe) return {
1172
+ verdict: "fail",
1173
+ reason: "waited for stdin before answering, so an agent or CI step that leaves stdin open hangs forever",
1174
+ evidence
1175
+ };
1176
+ const prompt = promptLine(open);
1177
+ if (prompt !== void 0) return {
1178
+ verdict: "warn",
1179
+ reason: "seems to prompt for input even though stdin isn't a terminal",
1180
+ evidence: `prompt: ${prompt.slice(0, 120)}; ${evidence}`
1181
+ };
1182
+ return {
1183
+ verdict: "skip",
1184
+ reason: "reads stdin like a filter and waits until it closes; give it input or </dev/null",
1185
+ evidence
1186
+ };
1187
+ }
1188
+ async function stdinRuns(command, args, timeout) {
1189
+ const closed = await run$1(command, args, { timeout });
1190
+ if (closed.spawnError || closed.timedOut) return { closed };
1191
+ const open = await run$1(command, args, {
1192
+ timeout: STDIN_WAIT_MS,
1193
+ stdin: "open"
1194
+ });
1195
+ if (!open.timedOut) return {
1196
+ closed,
1197
+ open
1198
+ };
1199
+ return {
1200
+ closed,
1201
+ open,
1202
+ closing: await run$1(command, args, {
1203
+ timeout: 4e3,
1204
+ stdin: { closeAfterMs: STDIN_CLOSE_AFTER_MS }
1205
+ })
1206
+ };
1207
+ }
1208
+ async function stdinCheck(command, args, defaultProbe, timeout) {
1209
+ const runs = await stdinRuns(command, args, timeout);
1210
+ const proven = runs.open?.timedOut === true && runs.closing !== void 0 && !runs.closing.timedOut;
1211
+ return {
1212
+ check: "stdin",
1213
+ invocation: [command, ...args],
1214
+ ...judgeStdin(runs, defaultProbe),
1215
+ closedMs: runs.closed.spawnError ? null : runs.closed.durationMs,
1216
+ waitedForStdin: proven,
1217
+ finishedAfterCloseMs: proven && runs.closing ? runs.closing.durationMs - STDIN_CLOSE_AFTER_MS : null
1218
+ };
1219
+ }
1119
1220
  // src/checks/index.ts
1120
1221
  /** Every check, in the order they run and are reported. */
1121
1222
  const CHECKS = [
1122
1223
  closedPipeCheck,
1123
1224
  colorCheck,
1124
1225
  pipeEscapesCheck,
1125
- ctrlCCheck,
1126
- controlCharsCheck,
1127
1226
  {
1128
- name: "startup",
1129
- kind: "info",
1130
- summary: "median wall time of --version, or of the given probe, over 5 runs",
1227
+ name: "stdin",
1228
+ kind: "check",
1229
+ summary: "waits for stdin when it is an open pipe, as agents and CI runners leave it",
1131
1230
  async run(target, ctx) {
1132
- const args = target.explicit ? target.invocations[0] ?? [] : ["--version"];
1133
- return [await startup(target.command, args, ctx.timeout)];
1231
+ const results = [];
1232
+ for (const args of target.invocations) results.push(await stdinCheck(target.command, args, !target.explicit, ctx.timeout));
1233
+ return results;
1134
1234
  }
1135
1235
  },
1236
+ ctrlCCheck,
1237
+ controlCharsCheck,
1238
+ startupCheck,
1136
1239
  baselineCheck
1137
1240
  ];
1138
1241
  /**
@@ -1439,6 +1542,10 @@ Checks:
1439
1542
  pipe-escapes Runs the command into a pipe and warns if cursor movement,
1440
1543
  erase, hyperlink, window title or carriage-return redraw
1441
1544
  codes (spinners, progress bars) reach stdout or stderr.
1545
+ stdin Runs the command with stdin as an open pipe nobody writes to,
1546
+ as agents and CI runners often leave it. Fails if --version or
1547
+ --help waits for input; for your own probes, warns if it
1548
+ prompts and skips if it reads stdin like a filter.
1442
1549
  ctrl-c Sends Ctrl-C (SIGINT) after 1 s. Fails if the command is still
1443
1550
  running 3 s later or prints a stack trace; warns if it exits 0.
1444
1551
  Skipped when the command finishes first, so probe a command
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cliprobe",
3
- "version": "0.3.0",
4
- "description": "Black-box test any command-line program for terminal-edge bugs: crashes on a closed pipe (| head), color codes leaking into pipes, broken Ctrl-C handling, and control characters from file names reaching your terminal.",
3
+ "version": "0.4.0",
4
+ "description": "Black-box test any command-line program for terminal-edge bugs: crashes on a closed pipe (| head), color and control codes leaking into pipes, hangs waiting on stdin, broken Ctrl-C handling, and control characters from file names reaching your terminal.",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "cliprobe": "dist/cli.js"
@@ -39,7 +39,9 @@
39
39
  "sigint",
40
40
  "ctrl-c",
41
41
  "terminal-injection",
42
- "pty"
42
+ "pty",
43
+ "stdin",
44
+ "hang"
43
45
  ],
44
46
  "author": {
45
47
  "name": "Khaled Saeed",