admin管理员组

文章数量:1287086

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.

.html

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

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/api/child_process.html

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

Share Improve this question edited Jun 19, 2015 at 12:15 Joe 42.7k20 gold badges108 silver badges128 bronze badges asked Jun 19, 2015 at 12:06 John SmithJohn Smith 6,26919 gold badges61 silver badges113 bronze badges 2
  • What if the child process cannot understand the buffers or objects? – thefourtheye Commented Jun 19, 2015 at 12:12
  • 2 Can't you just send it via the child streams (stdin, ipc etc.)? Streams in nodejs can accept either strings or buffers – slebetman Commented Jun 19, 2015 at 12:15
Add a ment  | 

4 Answers 4

Reset to default 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.

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);

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/api/child_process.html#child_processexecsyncmand-options

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);

本文标签: javascriptPass a Buffer to a Nodejs Child ProcessStack Overflow