mafintosh / mafintosh/duplexify

Catch the status of a child process as a stream error

Open
#31 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
191
Forks
35
PR merge metrics
No merged PRs in 30d

Description

I am using `duplexify` to create a duplex stream out of a child process. This enables me to create complex pipelines, combining processes with other streams. So, I wrote a simple function that wraps `duplexify` and `child_process.spawn` as follows:

function run(program, args) {
const child = spawn(program, args, {
stdio: ["pipe", "pipe", "inherit"],
});
const stream = duplexify(child.stdin, child.stdout);
child.on("error", (err) => {
stream.destroy(err);
});
return stream;
}

Now I can use the stream returned by `run` with `stream.pipeline`. The only issue that I have with this solution is that I would like `run` to wait for the child processes' exit status, and if it is not zero, forward an error to the duplex stream. I tried to address this by adding a `final` method as follows:

function run(program, args) {
const child = spawn(program, args, {
stdio: ["pipe", "pipe", "inherit"],
});
const final = (stream, callback) => {
child.on("exit", (code, signal) => {
if (code !== 0)
callback(new Error("child process failed"));
else
callback();
});
};
const stream = duplexify(child.stdin, child.stdout, {final});
child.on("error", (err) => {
stream.destroy(err);
});
return stream;
}

but the `final` function never gets called by `duplexify`. Is there a way to solve this issue with `duplexify`?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading duplexify's finalization behavior and the Node.js child_process exit and error events used in the example. Determine how a nonzero child exit can be surfaced through the duplex stream, and verify that successful exits complete normally while failures reach stream.pipeline as errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.