6

After I went through the documentation for Node.js Child Processes, I was curious If it would be possible to pass a Buffer to this Process.

https://nodejs.org/api/child_process.html

For me it seems like I only can pass Strings? How can I pass Buffers or Objects? Thanks!

Joe
  • 41,484
  • 20
  • 104
  • 125
John Smith
  • 6,105
  • 16
  • 58
  • 109

4 Answers4

4

You can pass only Buffer or string.

var node = require('child_process').spawn('node',['-i']);
node.stdout.on('data',function(data) {
    console.log('child:: '+String(data));
});

var buf = new Buffer('console.log("Woof!") || "Osom\x05";\x0dprocess.exit();\x0d');
console.log('OUT:: ',buf.toString())
node.stdin.write(buf);

Output:

OUT::  console.log("Woof!") || "Osom♣";
process.exit();
child:: >
child:: Woof!

child:: 'Osom\u0005'

child:: >

Because .stdin is writable stream.

\x0d(CR) is an 'Enter' simulation in interactive mode.

befzz
  • 1,232
  • 13
  • 11
  • How would you pass as well options with the data? Is there for example something with that I can send two argumenst `write(buf, options)` ? Thanks – John Smith Jun 19 '15 at 15:49
  • @JohnSmith Run interactive `node -i` or just open console(cmd). You able to pass only characters. Only. Node.exe (and all other console apps) reads input char by char. Nothing more. `stdin` and `stdout` is a *character stream*. Read about this. ;) https://en.wikipedia.org/wiki/Standard_streams So. You can convert object to string(`JSON.stringify()`) and parse it in child, if he can `JSON.parse()` – befzz Jun 19 '15 at 15:57
  • Thanks @befzz for this Information. But does it mean that I cannot pass a Filestream? – John Smith Jun 19 '15 at 16:34
  • Maybe you can help me with this question: http://stackoverflow.com/questions/30943250/pass-buffer-to-childprocess-node-js – John Smith Jun 19 '15 at 16:47
2

You can use streams...

     var term=require('child_process').spawn('sh');

     term.stdout.on('data',function(data) {
     console.log(data.toString());
     });

     var stream = require('stream');

     var stringStream = new stream.Readable;
     var str="echo 'Foo Str' \n";
     stringStream.push(str);
     stringStream.push(null);
     stringStream.pipe(term.stdin);

     var bufferStream= new stream.PassThrough;
     var buffer=new Buffer("echo 'Foo Buff' \n");
     bufferStream.end(buffer);
     bufferStream.pipe(term.stdin);
cswl
  • 443
  • 7
  • 14
  • Maybe you can help me with this question: http://stackoverflow.com/questions/30943250/pass-buffer-to-childprocess-node-js – John Smith Jun 19 '15 at 16:47
0

git diff | git apply --reverse

const { execSync } = require('child_process')

const patch = execSync(`git diff -- "${fileName}"`, { cwd: __dirname }
//patch is a Buffer
execSync(`git apply --reverse`, { cwd: __dirname, input: thePatch })

echo Hello, World! | cat

const { execSync } = require('child_process')

const output = execSync(`cat`, { cwd: __dirname, input: "Hello, World!" })
console.log(output) //Buffer
console.log(output.toString()) //string

input <string> | <Buffer> | <TypedArray> | <DataView> The value which will be passed as stdin to the spawned process. Supplying this value will override stdio[0].

https://nodejs.org/api/child_process.html#child_processexecsynccommand-options

Mr. Doge
  • 796
  • 5
  • 11
0

If you use child_process.fork() you can send Buffer from parent to child in such way:

const message = JSON.stringify(buffer);
child.send(message);

and parse it

const buffer = Buffer.from(JSON.parse(message).data);