CLI 도구의 경우 stdin 에서 읽는 것이 유용할 수 있습니다. Bun 에서 console 객체는 stdin 의 라인을 생성하는 AsyncIterable 입니다.
ts
const prompt = "Type something: ";
process.stdout.write(prompt);
for await (const line of console) {
console.log(`You typed: ${line}`);
process.stdout.write(prompt);
}이 파일을 실행하면 사용자가 입력한 내용을 에코하는 무한 대화형 프롬프트가 실행됩니다.
sh
bun run index.tstxt
Type something: hello
You typed: hello
Type something: hello again
You typed: hello againBun 은 Bun.stdin 을 통해 stdin 을 BunFile 로도 노출합니다. 이는 bun 프로세스로 파이프되는 대용량 입력을 점진적으로 읽을 때 유용합니다.
청커가 라인별로 분할된다는 보장은 없습니다.
ts
for await (const chunk of Bun.stdin.stream()) {
// chunk 는 Uint8Array 입니다
// 이것은 텍스트로 변환합니다 (ASCII 인코딩 가정)
const chunkText = Buffer.from(chunk).toString();
console.log(`Chunk: ${chunkText}`);
}이것은 bun 프로세스로 파이프되는 입력을 출력합니다.
sh
echo "hello" | bun run stdin.tstxt
Chunk: hello더 유용한 유틸리티는 문서 > API > 유틸리티 를 참조하세요.