esamatti
esamatti

Reputation: 18944

Custom stdin for Node.js child process

Is it possible to execute a child process in Node.js with a custom stdin under Linux?

The documention refers to stdinStream which is for internal use only:

There are several internal options. In particular stdinStream, stdoutStream, stderrStream. They are for INTERNAL USE ONLY. As with all undocumented APIs in Node, they should not be used.

Why it's for internal use only? I also tried to give my custom readableStream to it, but it yielded a very nasty V8 exception. No surprise after using an internal API...

Some background: I need to execute a git commit after the Node.js process has closed its stdin (this is something I cannot workaround currently). The issue is that Git gets very angry at you if you try to commit with a closed stdin.

UPDATE: My issue actually originates from a Node.js bug, but I'll keep this question here since I think I'm not only one wondering this.

Upvotes: 1

Views: 1312

Answers (2)

Linus Thiel
Linus Thiel

Reputation: 39223

Rather than using stdinStream, I think you should use customFds instead. From the same documentation:

There is a deprecated option called customFds which allows one to specify specific file descriptors for the stdio of the child process. This API was not portable to all platforms and therefore removed. With customFds it was possible to hook up the new process' [stdin, stdout, stderr] to existing streams; -1 meant that a new stream should be created. Use at your own risk.

As it says, this was deprecated because of platform compatibility (I'm guessing Windows). So if you don't want/need to support Windows, I think customFds is a safer choice, since, although it's also deprecated, it was part of the API, while stdinStream never was.

Upvotes: 1

schaermu
schaermu

Reputation: 13460

Well, you can always pipe any ReadableStream object to the stdin property of your ChildProcess object (it's exactly what i do in my library node-fluent-ffmpeg).

Since i was setting the input stream some calls before actually piping it to the process, i actually had to pause the input stream as soon as it was set in my options to prevent data loss until the process was spawned (see here):

inputstream.pause();

As soon as i start the actual processing, i simply resume the stream (see here):

inputstream.resume();
inputstream.pipe(yourProc.stdin);

Earlier versions of node used to support customFd's, but this feature is not supported under MacOS and probably Windows too. That's why customFd's were dropped as deprecated.

Upvotes: 1

Related Questions