本页还没有译文
本页 7 段还没译成简体中文,下面显示的是英文原文。Read from stdin
还没翻译译自英文(bun.com)本页 7 段都还没译
In Bun, the console object is an AsyncIterable that yields lines from stdin.
const prompt = "Type something: ";
process.stdout.write(prompt);
for await (const line of console) {
console.log(`You typed: ${line}`);
process.stdout.write(prompt);
}Running this file starts a never-ending interactive prompt that echoes whatever you type.
bun run index.tsType something: hello
You typed: hello
Type something: hello again
You typed: hello againBun also exposes stdin as a BunFile, Bun.stdin. Use it to incrementally read large inputs piped into the bun process.
Chunks aren't guaranteed to be split line-by-line.
for await (const chunk of Bun.stdin.stream()) {
// chunk is Uint8Array
// this converts it to text (assumes UTF-8 encoding)
const chunkText = Buffer.from(chunk).toString();
console.log(`Chunk: ${chunkText}`);
}Running stdin.ts prints whatever is piped into it.
echo "hello" | bun run stdin.tsChunk: helloSee Utils for more utilities.
AI 翻译查看英文原文