Files
bare-operating-system/packages/bare-os-booter/lib/resolve-stdio.js
T
2026-04-03 20:46:09 -04:00

55 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Pear often runs without `process.stdin` / `process.stdout`; Holepunch uses `bare-stdio`
* on fds 02 (same idea as pear-terminal's stdio singleton).
*/
import { bindReplDebugStream, isReplDebug, replDbg } from './debug-repl.js'
/**
* @returns {Promise<{ stdin: import('stream').Readable | null, stdout: import('stream').Writable | null }>}
*/
export async function resolveStdio() {
const p = globalThis.process
if (p?.stdin && p?.stdout) {
if (isReplDebug()) {
bindReplDebugStream(p.stdout)
replDbg(
'resolve-stdio',
'process stdin/stdout',
JSON.stringify({
stdinIsTTY: p.stdin.isTTY,
stdoutColumns: p.stdout.columns,
hasCursorTo: typeof p.stdout.cursorTo,
hasClearLine: typeof p.stdout.clearLine
})
)
}
return { stdin: p.stdin, stdout: p.stdout }
}
try {
const mod = await import('bare-stdio')
const io = mod.default?.in ? mod.default : mod
if (io?.in && io?.out) {
if (isReplDebug()) {
bindReplDebugStream(io.out)
replDbg(
'resolve-stdio',
'bare-stdio',
JSON.stringify({
stdinIsTTY: io.in.isTTY,
stdoutColumns: io.out.columns,
hasCursorTo: typeof io.out.cursorTo,
hasClearLine: typeof io.out.clearLine
})
)
}
return { stdin: io.in, stdout: io.out }
}
} catch (e) {
if (isReplDebug())
replDbg('resolve-stdio', 'bare-stdio unavailable', String(e))
}
if (isReplDebug()) replDbg('resolve-stdio', 'no stdin/stdout')
return { stdin: null, stdout: null }
}