-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stdin.read interface #68
Comments
I'm really not sure how feasible it is and what an useful API would look like but I just want to make sure we have a place to follow up with the discussion, feel free to contribute more userland alternatives, desired outcomes and correct me if anything sounds wrong in the original description. |
For reference, I was creating a script that inflates the output of a git object on the command line. The idea was I could pipe the output of The solution for my original problem was utilizing
|
This isn't a pressing feature for me either, but it is an interesting use case and benefit to the userland. So I'm happy to champion if we think it is a useful addition |
Could Additional possibly related prior art: https://github.com/maxogden/concat-stream/ (lacks an |
Isn't the pipe solution both more succinct, and will not load the entire git object into memory (exploding if its larger than available memory)? Note that reading a stream of unknown length (stdin, for example) into a memory buffer involves double copying (at least) all the data, as well as potentially unconstrained memory allocation (streams don't have to have an end, Why would concat-stream need a isTTY check? |
I agree the pipe solution is probably best for what @Ethan-Arrowood was doing and reading an entire stream into memory is inefficient / has risks. I have run into situations where "just get me the complete data" has been very useful (admittedly always in tests). I don't think concat-stream needs a isTTY check, I was just commenting for the purpose of prior art you would have to guard against After reading @sam-github's comment and my own realization that I've never used this sort of functionality in production I have to ask is this something that even belongs in the node.js core? |
That is a great point @coreyfarrell I doubt I'd use the script for anything other than debugging/testing in my terminal. |
now that TLA landed on master, combined with the async/await stream support we have a more reasonable interface for consuming -cat README.md | node -e "inp=''; process.stdin.on('data',c=>inp+=c).on('end',()=>process.stdout.write(inp.replace(/[a-z]/g, 'x')))"
+cat README.md | node -e "for await (const c of process.stdin) process.stdout.write(c.toString().replace(/[a-z]/g, 'x'))" Note: Requires |
oh, it sounds like that person may be @bengl (ref: https://twitter.com/bengl/status/1402014875870765057) |
It looks like the future looks something like this:
It would be nice to get rid of the Alternatively, some kind of async If there's a worry that the promise would never resolve if the stream doesn't end: Consider that we already have this effect synchronously with |
Returns a promise fulfilling with a sensible representation of the entire stream's data, depending on the stream type. Buffer streams collect to a single contiguous Buffer. Buffer streams with encoding set collect to a single contiguous string. Object-mode streams collect to an array of objects. Limiting is allowed, enabling situations like capping request body sizes, or only consuming a limited amount of process.stdin. Ref: nodejs#35192 Ref: nodejs/tooling#68
oh thanks @bengl ! following your PR I learned about the Stream consumers API, that might be what I was looking for when I first opened this issue originally:
|
I'm pretty sure I heard someone in one of the meetings in the past mention it before but I couldn't find any issue so I'm filling this one so that we can track/discuss it.
Description
A different interface for reading from standard input, making it simpler to create scripts that just need to consume a blob of data from stdin at once.
Desired behaviors
Userland alternatives
/cc @Ethan-Arrowood
The text was updated successfully, but these errors were encountered: