将缓冲区传递给 Node.js 子进程
Pass a Buffer to a Node.js Child Process
在阅读 Node.js 子进程的文档后,我很好奇是否可以将缓冲区传递给此进程。
https://nodejs.org/api/child_process.html
我好像只能传字符串?如何传递缓冲区或对象?谢谢!
您可以使用流...
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);
您只能传递缓冲区或字符串。
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);
输出:
OUT:: console.log("Woof!") || "Osom♣";
process.exit();
child:: >
child:: Woof!
child:: 'Osom\u0005'
child:: >
因为.stdin
是writable stream.
\x0d
(CR) 是交互模式下的'Enter'模拟。
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
在阅读 Node.js 子进程的文档后,我很好奇是否可以将缓冲区传递给此进程。
https://nodejs.org/api/child_process.html
我好像只能传字符串?如何传递缓冲区或对象?谢谢!
您可以使用流...
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);
您只能传递缓冲区或字符串。
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);
输出:
OUT:: console.log("Woof!") || "Osom♣";
process.exit();
child:: >
child:: Woof!
child:: 'Osom\u0005'
child:: >
因为.stdin
是writable stream.
\x0d
(CR) 是交互模式下的'Enter'模拟。
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