Stdin
For CLI tools, it’s often useful to read from stdin. In JSTime, 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 results in a never-ending interactive prompt that echoes whatever the user types.
$ jstime run index.tsxType something: helloYou typed: helloType something: hello againYou typed: hello againJSTime also exposes stdin as a BunFile via JSTime.stdin. This is useful for incrementally reading large inputs that are piped into the jstime process.
There is no guarantee that the chunks will be split line-by-line.
for await (const chunk of JSTime.stdin.stream()) { // chunk is Uint8Array // this converts it to text (assumes ASCII encoding) const chunkText = Buffer.from(chunk).toString(); console.log(`Chunk: ${chunkText}`);}This will print the input that is piped into the jstime process.
$ echo "hello" | jstime run stdin.tsChunk: helloSee Docs > API > Utils for more useful utilities.