storebound 0.8.3: prompts read stdin with rt.readLine (node:readline) instead of Bun-only for await (const line of console), which crashed npx storebound desktop on Node/Windows; node-compat test bans Bun-only stdin iteration; Node prompt smoke, also on the Windows release runner

This commit is contained in:
CRTkafa 2026-10-14 08:07:40 +08:00
parent c3fed8efa2
commit 3a27c78f49
8 changed files with 111 additions and 9 deletions

View file

@ -1,6 +1,7 @@
import type { Args } from './args';
import type { Language, Loaded } from './config';
import { playCodes } from './config';
import * as rt from './rt';
export interface Ctx { loaded: Loaded; args: Args }
@ -42,8 +43,8 @@ export async function confirm(ctx: Ctx, question: string): Promise<boolean> {
return declined('not a terminal and no --yes (re-run with --yes to confirm)');
}
process.stdout.write(`${question} [y/N] `);
for await (const line of console) return /^y(es)?$/i.test(line.trim()) || declined('the owner answered no');
return declined('no answer (stdin closed)');
const line = await rt.readLine();
return line !== null && /^y(es)?$/i.test(line.trim()) || declined(line === null ? 'no answer (stdin closed)' : 'the owner answered no');
}
/** Exit code of a command whose confirmation was declined or could not be asked. */

View file

@ -8,6 +8,7 @@ import { spawn as nodeSpawn, spawnSync as nodeSpawnSync, type StdioOptions } fro
import { createHash } from 'node:crypto';
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
import { createServer } from 'node:http';
import { createInterface } from 'node:readline';
import * as nodeModule from 'node:module';
import { constants } from 'node:os';
import { delimiter, dirname, isAbsolute, join, resolve } from 'node:path';
@ -323,6 +324,34 @@ export const stdinStream = (): ReadableStream<Uint8Array> => webStream(process.s
/** All of stdin as text (Bun.stdin.text()). */
export const stdinText = (): Promise<string> => new Response(stdinStream()).text();
/** Streams a readLine saw end: a new interface on them would never emit close. */
const endedInputs = new WeakSet<object>();
/**
* One line from stdin, without the line break; null at EOF (so a [Y/n] prompt never reads a closed stdin as Enter). For prompts on Node and Bun (`for await (const line of
* console)` is Bun-only: on Node it throws "console is not async iterable"). The interface is closed after the line
* and stdin is paused and unref'd, so no handle stays open (an open stdin handle tripped a libuv assertion on Windows
* at exit).
*/
export function readLine(input: NodeJS.ReadableStream & { ref?: () => void; unref?: () => void; pause?: () => void } = process.stdin): Promise<string | null> {
if ((input as { readableEnded?: boolean }).readableEnded || endedInputs.has(input)) return Promise.resolve(null);
return new Promise(res => {
input.ref?.(); // an earlier readLine unref'd it
const rl = createInterface({ input, terminal: false, crlfDelay: Infinity });
let done = false;
const finish = (line: string | null) => {
if (done) return;
done = true;
rl.close();
input.pause?.();
input.unref?.();
res(line === null ? null : line.replace(/\r$/, ''));
};
rl.once('line', finish);
rl.once('close', () => { if (!done) endedInputs.add(input); finish(null); });
});
}
// ---- a small HTTP server (Bun.serve) ----
export interface Server { readonly port: number; readonly hostname: string; stop(closeActive?: boolean): void }