0

sorry if I am not descriptive enough, If I need to be please let me know.

I'm trying to have one node.js script use child_process.spawn() on another node.js script. I am successfully able to make it execute but I am unable to retrieve data from pipes other than stdin, stdout, and stderr.

When I do a spawn() command I have the option of accessing "stdio" but I don't have that option with process. process.stdio is undefined.

When I spawn() an ffmpeg process I can easily get data from more than the 3 default pipes.

So, my question is : How can I get access to more pipes from a regular node.js script?

Essentially I would like to access things like process.stdio[1] instead of process.stdout because then I can use process.stdio with numbers a lot higher than 2.

any help is appreciated, thank you

moeiscool
  • 1,318
  • 11
  • 14

1 Answers1

2

Per the child process docs and this SO question you can do this by creating a write stream to { fd: 3 }:

On parent:

const opts = {
    stdio: ['pipe', 'pipe', 'pipe', 'pipe']
};
const child = child_process.spawn('node', ['./child.js'], opts);

//read data
child.stdio[3].pipe(myHandler);
//or
child.stdio[3].on('data', data => {
  //handle data
});

On child:

const writeStream = fs.createWriteStream(null, {fd: 3});
dataStream.pipe(writeStream);
//or write data chunks directly
writeStream.write(data);

This is untested, but should work OK.

Klaycon
  • 10,599
  • 18
  • 35
  • i love you, you saved me hours of my life. sincerely, thank you so much. If you are a Shinobi user, contact me on Discord and ill hook you up a free lifetime subscription key. thank you!!! – moeiscool Dec 05 '19 at 00:07