bun
bun copied to clipboard
Add `process.stdin`, `process.stdout` and `process.stderr`
What is the problem this feature will solve?
It will allow more compatibility with node.js programs and make it easy(or maybe even stop it from being impossible) to receive user input.
What is the feature you are proposing to solve the problem?
Adding More support for process.stdin and process.stdout (specifically, process.stdin.on("data", ()=>{...} and process.stdout.write("..."))
What alternatives have you considered?
I have no fix for stdin so far, stdout I could maybe store all my lines in a string and log them whenever (would still always end in a newline, but would fix writing chars without an automatic newline)
I'd be happy to provide a polyfill for process.stdin and process.stdout, but I'm blocked (which in turns blocks me on https://github.com/oven-sh/bun/issues/311#issuecomment-1186720781) in trying to get process.stdout behave like a WritableStream at all see original:
console.log(Bun.stdin.stream(), Bun.stdout.stream());
// ReadableStream { } ReadableStream { }
I have tried many things in JS-land to no avail, so would love some input about how to make Bun.stdout behave somehow like a WritableStream instead of a ReadableStream, or how to write data to it in any way. Unfortunately the Zig source code is illegible for me, so I've been trying things in JS-land only.
@franciscop You need to use Bun.write and Bun.stdout together:
// log a file to stdout
await Bun.write(Bun.stdout, Bun.file("input.txt"));
Shimming my global process.stderr does the trick in my case: (your mileage might vary)
if (typeof process.stderr == "undefined") {
process.stderr = Bun.stderr;
}
Do not repeat the same restrictive pattern as Node.js where 'readable' event must be set with handler for process.stdin.read() to work https://github.com/nodejs/node/discussions/43918#discussioncomment-3199654
The implementation is such that the
readableevent must be used it has nothing to do with thereadablestate indicator.
Use a pattern the similar to QuickJS
const header = new Uint32Array(1);
std.in.read(header.buffer, 0, 4);
or Deno
const header = new Uint32Array(1);
await Deno.stdin.read(header);
@franciscop You need to use
Bun.writeandBun.stdouttogether:// log a file to stdout await Bun.write(Bun.stdout, Bun.file("input.txt"));
Unfortunately I can't get Bun.read() or Bun.write() to work within a Native Messaging host https://github.com/oven-sh/bun/issues/1297. I didn't locate any documentation nor tests for Bun.write() and Bun.read(), particularly missing as TypedArray input to Bun.write() and getting stdin from a non-TTY application. It would be helpful to write out what is being shipped now, with tests, even if the implementation is temporary.
I believe this issue can be closed with the release of v0.3.0.
Yep! This is now supported in Bun v0.3.0 🎉