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:
parent
c3fed8efa2
commit
3a27c78f49
8 changed files with 111 additions and 9 deletions
|
|
@ -18,6 +18,7 @@ import { APPLE_SPEC } from './api/apple.gen';
|
|||
import { PLAY_APIS } from './api/play.gen';
|
||||
import { WRAPPED } from './api/wrapped.gen';
|
||||
import type { ParamValue } from './api/types';
|
||||
import * as rt from './lib/rt';
|
||||
|
||||
const STORES: Store[] = ['apple', 'play'];
|
||||
|
||||
|
|
@ -95,8 +96,7 @@ export function describeRequest(req: BuiltRequest): string {
|
|||
}
|
||||
|
||||
async function readLine(): Promise<string> {
|
||||
for await (const line of console) return line.trim();
|
||||
return '';
|
||||
return ((await rt.readLine()) ?? '').trim();
|
||||
}
|
||||
|
||||
export async function run(args: Args): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -128,8 +128,8 @@ function canWrite(dir: string): boolean {
|
|||
|
||||
async function askYes(text: string): Promise<boolean> {
|
||||
process.stdout.write(text);
|
||||
for await (const line of console) return !/^n(o)?$/i.test(line.trim());
|
||||
return false;
|
||||
const line = await rt.readLine();
|
||||
return line !== null && !/^n(o)?$/i.test(line.trim());
|
||||
}
|
||||
|
||||
const tilde = (p: string) => (p.startsWith(homedir()) ? '~' + p.slice(homedir().length) : p);
|
||||
|
|
|
|||
|
|
@ -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. */
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue