Skip to content

Commit cdc0fc7

Browse files
fix(browse): chain reads piped stdin synchronously, so Windows runs the flow (#3039)
On Windows, an awaited Bun.stdin.text() inside the CLI's un-awaited main() let the process exit 0 before reading stdin: a replayed flow reported success with nothing sent to the daemon. Read fd 0 synchronously (skipped for a terminal); empty input now gets the server's usage error and a non-zero exit. Co-authored-by: VaibDeveloper <[email protected]>
1 parent 2328cd3 commit cdc0fc7

2 files changed

Lines changed: 79 additions & 2 deletions

File tree

‎browse/src/cli.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,9 +2003,12 @@ Refs: After 'snapshot', use @e1, @e2... as selectors:
20032003
await handleTunnel(commandArgs); // always exits
20042004
}
20052005

2006-
// Special case: chain reads from stdin
2006+
// Special case: chain reads from stdin. Synchronously: on Windows an awaited
2007+
// Bun.stdin.text() inside this un-awaited main() did not keep the event loop
2008+
// alive, so a piped flow exited 0 with nothing sent to the daemon (#3039).
2009+
// A terminal has no flow to read; the empty argument gets the server's usage.
20072010
if (command === 'chain' && commandArgs.length === 0) {
2008-
const stdin = await Bun.stdin.text();
2011+
const stdin = process.stdin.isTTY ? '' : fs.readFileSync(0, 'utf8');
20092012
commandArgs.push(stdin.trim());
20102013
}
20112014

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* #3039: `chain` with no arguments reads its JSON flow from stdin. On Windows
3+
* an awaited Bun.stdin.text() inside the CLI's un-awaited main() let the
4+
* process exit 0 before reading, so a replayed flow "passed" with nothing
5+
* sent to the daemon. The CLI reads stdin synchronously; this drives the real
6+
* CLI against a stub daemon (the Windows free lane runs it natively).
7+
*/
8+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
9+
import * as fs from 'node:fs';
10+
import * as os from 'node:os';
11+
import * as path from 'node:path';
12+
13+
const CLI = path.resolve(import.meta.dir, '../src/cli.ts');
14+
15+
describe('#3039: chain reads its flow from piped stdin', () => {
16+
let scratch: string;
17+
let daemon: ReturnType<typeof Bun.serve>;
18+
let received: Array<{ command: string; args: string[] }>;
19+
20+
beforeEach(() => {
21+
scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'browse-chain-stdin-'));
22+
received = [];
23+
daemon = Bun.serve({
24+
hostname: '127.0.0.1',
25+
port: 0,
26+
fetch: async (req) => {
27+
const url = new URL(req.url);
28+
if (url.pathname === '/health') return Response.json({ status: 'healthy' });
29+
if (url.pathname === '/command') {
30+
const body = await req.json();
31+
received.push(body);
32+
return body.args?.[0] ? new Response('chain ran') : new Response('Usage: echo \'[["goto","url"]]\' | browse chain', { status: 400 });
33+
}
34+
return new Response('not found', { status: 404 });
35+
},
36+
});
37+
});
38+
39+
afterEach(() => {
40+
try { daemon.stop(true); } catch {}
41+
fs.rmSync(scratch, { recursive: true, force: true });
42+
});
43+
44+
async function runChain(stdin: string) {
45+
const stateFile = path.join(scratch, 'browse.json');
46+
fs.writeFileSync(stateFile, JSON.stringify({ pid: process.pid, port: daemon.port, token: 'chain-stdin-test', mode: 'launched' }));
47+
const env: Record<string, string> = {};
48+
for (const [key, value] of Object.entries(process.env)) {
49+
if (value !== undefined && !/^(BROWSE_|GSTACK_)/.test(key)) env[key] = value;
50+
}
51+
Object.assign(env, { HOME: scratch, GSTACK_HOME: path.join(scratch, '.gstack'), BROWSE_STATE_FILE: stateFile });
52+
const cli = Bun.spawn([process.execPath, CLI, 'chain'], {
53+
cwd: scratch, env, stdin: new TextEncoder().encode(stdin), stdout: 'pipe', stderr: 'pipe',
54+
});
55+
const timer = setTimeout(() => cli.kill('SIGKILL'), 30_000);
56+
const [code, stdout, stderr] = await Promise.all([cli.exited, new Response(cli.stdout).text(), new Response(cli.stderr).text()]);
57+
clearTimeout(timer);
58+
return { code, out: stdout + stderr };
59+
}
60+
61+
test('a piped flow reaches the daemon as the chain argument', async () => {
62+
const flow = '[["js","1+1"]]';
63+
const r = await runChain(`${flow}\n`);
64+
expect(received).toEqual([{ command: 'chain', args: [flow] }]);
65+
expect(r.out).toContain('chain ran');
66+
expect(r.code).toBe(0);
67+
}, 45_000);
68+
69+
test('empty stdin is a usage error, never a silent exit 0', async () => {
70+
const r = await runChain('');
71+
expect(r.code).not.toBe(0);
72+
expect(r.out).toContain('Usage');
73+
}, 45_000);
74+
});

0 commit comments

Comments
 (0)