Skip to content

Commit 5239f87

Browse files
committed
fix(core): don't drop piped stdin answers in first-render deferral
1 parent 69b4eec commit 5239f87

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

‎packages/core/core.vitest.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ it('allow aborting the prompt using signals', async () => {
903903
await expect(answer).rejects.toThrow(AbortPromptError);
904904
});
905905

906-
it('should ignore keypresses buffered before prompt creation', async () => {
906+
it('should ignore keypresses buffered before prompt creation on a TTY', async () => {
907907
const prompt = createPrompt(
908908
(_config: { message: string }, done: (value: string) => void) => {
909909
useKeypress((key: KeypressEvent) => {
@@ -917,9 +917,11 @@ it('should ignore keypresses buffered before prompt creation', async () => {
917917
);
918918

919919
// Use PassThrough which has proper stream buffering (like real process.stdin)
920-
const input = new PassThrough();
920+
const input = Object.assign(new PassThrough(), { isTTY: true });
921921
const output = new PassThrough();
922922
output.write = () => true; // suppress output
923+
// On a terminal, keys typed before the prompt started are stale user input
924+
// (see issue #1303): the first render defers one tick and discards them.
923925

924926
// Buffer an Enter keypress BEFORE the prompt exists
925927
input.write('\r');
@@ -938,6 +940,34 @@ it('should ignore keypresses buffered before prompt creation', async () => {
938940
await expect(answer).resolves.toBe('submitted');
939941
});
940942

943+
it('should consume input piped into non-TTY stdin before the prompt starts', async () => {
944+
const prompt = createPrompt(
945+
(_config: { message: string }, done: (value: string) => void) => {
946+
useKeypress((key: KeypressEvent) => {
947+
if (isEnterKey(key)) {
948+
done('submitted');
949+
}
950+
});
951+
952+
return 'Question?';
953+
},
954+
);
955+
956+
const input = new PassThrough();
957+
const output = new PassThrough();
958+
output.write = () => true; // suppress output
959+
960+
// Piped stdin (no isTTY) is intentional input, e.g. answers written to a
961+
// child process's stdin the moment it is spawned. It must not be dropped
962+
// by the first-render deferral, or the prompt waits forever — the cause
963+
// of the intermittent 30s timeouts in the Deno integration suite.
964+
input.write('\r');
965+
966+
const answer = prompt({ message: 'test' }, { input, output });
967+
968+
await expect(answer).resolves.toBe('submitted');
969+
});
970+
941971
it('fail on aborted signals', async () => {
942972
const Prompt = (config: { message: string }, done: (value: string) => void) => {
943973
useKeypress((key: KeypressEvent) => {

‎packages/core/src/lib/create-prompt.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,18 @@ export function createPrompt<Value, Config>(
228228
// poll) lets that stale data flow through readline harmlessly—no keypress
229229
// handlers are registered yet and the output is muted, so the stale
230230
// keystrokes are silently discarded.
231+
// This only applies to TTY inputs, where early data is stale keystrokes
232+
// the user typed before the prompt started. Piped input (stdin from a
233+
// script or test harness) is intentional: deferring there risks dropping
234+
// the answer itself, since data already waiting on the pipe flows
235+
// through readline during the deferral tick and is then discarded,
236+
// leaving the prompt waiting forever (seen as intermittent 30s timeouts
237+
// in the Deno integration suite).
231238
// Old-style streams (like MuteStream) have no such buffering, so the
232239
// render cycle starts immediately.
233240
//
234241
// @see https://github.com/SBoudrias/Inquirer.js/issues/1303
235-
if ('readableFlowing' in input) {
242+
if ('readableFlowing' in input && input.isTTY) {
236243
const immediate = nativeSetImmediate(startCycle);
237244
cleanups.add(() => nativeClearImmediate(immediate));
238245
} else {

0 commit comments

Comments
 (0)