| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
I'm very much surprised to see that #42590 was merged, despite the very valid discussion there ... can we not make the same mistake again? If a utility method is useful, then I see no reason not to make it composable; e.g., a readLines(stream: Readable): AsyncIterable<string>. This could be used as for await (const line of readLines(subprocess.stdout)) { ... }, but, and this is the important bit, also with any other stream. |
Sorry, something went wrong.
You can already do that with import { createInterface as readLines } from 'node:readline'. However, with that you are still lacking support for catching an 'error' event on the ChildProcess instance, and the exit code not being 0. |
Sorry, something went wrong.
For errors from spawning, this seems reasonable, for the other causes of ChildProcess errors (kill()ing the child process fails/sending messages to it fails), I would be surprised if a user expected that type of error to cause the async iterable to fail.
Again… would you want this? Wouldn’t you be more likely to want to check the exit code yourself after the stream has finished out? It seems like a bit of a footgun that the async iterable can fail at the end of the stream.
To be clear, by “mistake” I was referring to merging a PR with outstanding discussion, even if not in the form of “Requested Changes”, in the way #42590 was merged. |
Sorry, something went wrong.
| this.on('error', () => {}); | ||
| } else { | ||
| const errorPromise = new Promise((_, reject) => this.once('error', reject)); | ||
| const exitPromise = new Promise((resolve, reject) => this.once('exit', (code) => { |
There was a problem hiding this comment.
You would need to listen for close, not exit here, otherwise you’d run the risk of dropping data.
Sorry, something went wrong.
I guess in this case, folks should use readLines(cp.stdout) instead of cp.readLines(). The fact that it doesn't cover all the use cases doesn't strike me as a sign we should add this method. Worth noting that execa does consider a non-zero exit code as a failure or if it's killed by a signal, and given the popularity of this package (65M/week on npm), I think it's fair to assume it's a quite common use case and makes sense as a default.
Ah yes, sorry for the misunderstanding, in that case of course I agree. |
Sorry, something went wrong.
=== release test-child-process-readLines ===
Path: parallel/test-child-process-readLines
Error: --- stderr ---
file:///home/runner/work/node/node/test/parallel/test-child-process-readLines.mjs:18
for await (const line of spawn(process.execPath, ['-p', 42]).readLines()) {
^
TypeError: Iterator result 42 is not an object
at file:///home/runner/work/node/node/test/parallel/test-child-process-readLines.mjs:18:20
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Node.js v21.0.0-pre
Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-child-process-readLines.mjs
|
Sorry, something went wrong.
|
This pull request has been marked as stale due to 90 days of inactivity. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Similarly to fileHandle.readLines(), iterating line by line over the output of a command is a somewhat regular thing when writing scripts. This PR adds a util method to cover this use case.