bun icon indicating copy to clipboard operation
bun copied to clipboard

Add `process.stdin`, `process.stdout` and `process.stderr`

Open Perodactyl opened this issue 3 years ago • 3 comments

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)

Perodactyl avatar Jul 28 '22 03:07 Perodactyl

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 avatar Aug 04 '22 06:08 franciscop

@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"));

samfundev avatar Aug 22 '22 23:08 samfundev

Shimming my global process.stderr does the trick in my case: (your mileage might vary)

if (typeof process.stderr == "undefined") {
  process.stderr = Bun.stderr;
}

samccone avatar Sep 04 '22 20:09 samccone

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 readable event must be used it has nothing to do with the readable state 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);

guest271314 avatar Oct 10 '22 02:10 guest271314

@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"));

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.

guest271314 avatar Oct 10 '22 02:10 guest271314

I believe this issue can be closed with the release of v0.3.0.

samfundev avatar Dec 07 '22 19:12 samfundev

Yep! This is now supported in Bun v0.3.0 🎉

Electroid avatar Dec 07 '22 19:12 Electroid