Add `childProcess.readable` and `childProcess.writable`
Background: #143
New feature
We could add the following properties:
childProcess.readable: likechildProcess.stdin, but as aReadableStream. This isundefinedifchildProcess.stdinisundefined, i.e. if thestdinoption is notpipe(its default value).childProcess.writable: same but forchildProcess.stdout, as aWritableStream. IfchildProcess.stdoutisundefinedbutchildProcess.stderris not, the latter is used instead. If thealloption istrue,childProcess.allis used instead. I.e. users can choosestdout,stderror both. Any error thrown by the child process is propagated tochildProcess.writable.
This can be easily implemented using Stream.Readable.toWeb() and Stream.Writable.toWeb().
TransformStream
Having a readable and writable properties would allow childProcess to be used as a TransformStream (even if it's not an instance of it) in Stream.pipeline(), Stream.compose() and ReadableStream.pipeThrough().
This is also useful for users who want to manipulate stdin/stdout/stderr directly but prefer web streams.
Example
import { pipeline } from 'stream/promises'
await pipeline(
execa(...),
execa(...),
myTransformingStream,
createReadStream('filePath'),
)
This also works well with $.
await pipeline(
$`ls mydir`,
$`sort`,
$`head -n 20`,
)
👍
I have pushed a readable-writable branch which implements this.
However, this seems rather unstable and needs more work. Notably, the tests are randomly failing.
The main issue is about how to propagate errors between the web streams (readable/writable), the Node.js streams (process.stdin|stdout|stderr) and the child process. It is not very easy because the web streams API does not allow consumers to error a stream, only producers.
So, in that branch, childProcess can be used as a TransformStream. However, when a child process errors, the stream does not emit an error event.
This is quite a hard issue to solve. :thinking:
Closed by https://github.com/sindresorhus/execa/issues/143#issuecomment-2002306393 and https://github.com/sindresorhus/execa/pull/912