1

I am using

 child_process.fork(scriptFile) 

to fork a child process and to execute the JS in scriptFile. The problem is that I want to optimize it because the JS code I want to run is already available in a buffer in the parent. I am now writing it to a file and then specifying that path to child_process.fork. This involves two redundant I/O. First I write the JS to a file in the parent. It is then read by the child process.

Any way to prevent this?

2
  • 1
    I don't think that exists. You could start a generic node.js app that was already on disk where that app is coded to get its code from stdinput and then you could feed your code to stdinput. No extra reads or writes of your code that way. Commented Jun 11, 2017 at 18:01
  • @jfriend00 Sounds like a good idea. Can you just provide this as an answer with some very simple code though I think I got the point... I would like to accept it as an answer. Surely would help others too... I think. Commented Jun 11, 2017 at 18:58

2 Answers 2

1

Putting my comment into answer as requested...

I don't think what you ask for exists.

You could start a generic node.js app that was already on disk where that app is coded to get its code from stdinput and then you could feed your code to stdinput for the stub app to read. No extra reads or writes of your code that way.


Here's an example of two simple apps that do this:

First, the app that just reads from stdin and executes it as Javascript:

// read-from-stdin.js let input = ""; process.stdin.on('data', function(chunk) { input += chunk; }); process.stdin.on('end', function() { eval(input); }); process.stdin.on('error', function(err) { console.log("err:", err); }); 

Then, an app that launches that app and passes it some JS to execute:

const spawn = require('child_process').spawn; let child = spawn('node', ['read-from-stdin.js'], {stdio: ['pipe', 'inherit', 'inherit']}); child.on('error', function(err) { console.log("err on spawn ", err); }); child.stdin.write("console.log('Hello from your parent')"); child.stdin.end(); 

When I run the second code, it launches the first code and sends it a console.log() statement via stdin which the code in the first app reads from stdin and then executes.

Sign up to request clarification or add additional context in comments.

1 Comment

can you provide some basic code? Now I am a bit confused as to how a generic node.js app can read code from stdin to be executed?
1

As of NodeJS v14.2.0, there exists an undocumented option which evaluates scripts instead of executing a module, see test-cli-eval.js

const { fork } = require('child_process'); const script = ` process.on('message', (message) => { if (message === 'ping') process.send('pong'); if (message === 'exit') process.exit(0); }); `; const child = fork('-e', [script]); child.on('exit', (exitCode) => { console.log(`Child process exited with ${exitCode}`) }); child.on('message', (message) => { if (message === 'pong') child.send('exit'); }); child.send('ping'); 

3 Comments

Are there any limitations compared to saving the code in a file and executing it. I mean will an existing code be impacted in any way after the suggested change. For example, any code which references its own (child's) PID will need to be changed as it will now be dealing with only one PID and not a separate one for the child. Are there any security issues which are normally associated with evals? I want to be cautious before I get excited and start changing my code.
Very thoughtful comment, unfortunately, I'm not sure about the security vulnerabilities, I assume it should be similar to the --eval option of node. Per my test, it seems the -e evaluates the script in the cwd. You can import modules relative to the current working directory in the script. Note that for Electron applications, the cwd is root /.
after consideration I have decided to defer any changes for now. This is more a reflection of lack of time to look into it now. I need to be very sure that there is a clear sandbox-ed separation between child and parent which is currently provided, albeit inefficiently. An eval will be certainly more efficient but that separation will not be present.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.