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
|
|
@ -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