child_process: Callback with Buffers from exec
Only return strings when encoding is not null.
This commit is contained in:
parent
26bc8db33f
commit
711d1934ea
@ -611,8 +611,10 @@ exports.execFile = function(file /* args, options, callback */) {
|
|||||||
windowsVerbatimArguments: !!options.windowsVerbatimArguments
|
windowsVerbatimArguments: !!options.windowsVerbatimArguments
|
||||||
});
|
});
|
||||||
|
|
||||||
var stdout = '';
|
var _stdout = [];
|
||||||
var stderr = '';
|
var _stderr = [];
|
||||||
|
var stdoutLen = 0;
|
||||||
|
var stderrLen = 0;
|
||||||
var killed = false;
|
var killed = false;
|
||||||
var exited = false;
|
var exited = false;
|
||||||
var timeoutId;
|
var timeoutId;
|
||||||
@ -630,6 +632,16 @@ exports.execFile = function(file /* args, options, callback */) {
|
|||||||
|
|
||||||
if (!callback) return;
|
if (!callback) return;
|
||||||
|
|
||||||
|
// merge chunks
|
||||||
|
var stdout = Buffer.concat(_stdout);
|
||||||
|
var stderr = Buffer.concat(_stderr);
|
||||||
|
|
||||||
|
if (Buffer.isEncoding(options.encoding)) {
|
||||||
|
// convert to strings
|
||||||
|
stdout = stdout.toString(options.encoding);
|
||||||
|
stderr = stderr.toString(options.encoding);
|
||||||
|
}
|
||||||
|
|
||||||
if (ex) {
|
if (ex) {
|
||||||
callback(ex, stdout, stderr);
|
callback(ex, stdout, stderr);
|
||||||
} else if (code === 0 && signal === null) {
|
} else if (code === 0 && signal === null) {
|
||||||
@ -670,20 +682,21 @@ exports.execFile = function(file /* args, options, callback */) {
|
|||||||
}, options.timeout);
|
}, options.timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
child.stdout.setEncoding(options.encoding);
|
|
||||||
child.stderr.setEncoding(options.encoding);
|
|
||||||
|
|
||||||
child.stdout.addListener('data', function(chunk) {
|
child.stdout.addListener('data', function(chunk) {
|
||||||
stdout += chunk;
|
_stdout.push(chunk);
|
||||||
if (stdout.length > options.maxBuffer) {
|
stdoutLen += chunk.length;
|
||||||
|
|
||||||
|
if (stdoutLen > options.maxBuffer) {
|
||||||
ex = new Error('stdout maxBuffer exceeded.');
|
ex = new Error('stdout maxBuffer exceeded.');
|
||||||
kill();
|
kill();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
child.stderr.addListener('data', function(chunk) {
|
child.stderr.addListener('data', function(chunk) {
|
||||||
stderr += chunk;
|
_stderr.push(chunk);
|
||||||
if (stderr.length > options.maxBuffer) {
|
stderrLen += chunk.length;
|
||||||
|
|
||||||
|
if (stderrLen > options.maxBuffer) {
|
||||||
ex = new Error('stderr maxBuffer exceeded.');
|
ex = new Error('stderr maxBuffer exceeded.');
|
||||||
kill();
|
kill();
|
||||||
}
|
}
|
||||||
|
52
test/simple/test-child-process-exec-buffer.js
Normal file
52
test/simple/test-child-process-exec-buffer.js
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Copyright Joyent, Inc. and other Node contributors.
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
// copy of this software and associated documentation files (the
|
||||||
|
// "Software"), to deal in the Software without restriction, including
|
||||||
|
// without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
// persons to whom the Software is furnished to do so, subject to the
|
||||||
|
// following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included
|
||||||
|
// in all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
require('../common');
|
||||||
|
var assert = require('assert');
|
||||||
|
var exec = require('child_process').exec;
|
||||||
|
|
||||||
|
var success_count = 0;
|
||||||
|
|
||||||
|
var str = 'hello';
|
||||||
|
|
||||||
|
// default encoding
|
||||||
|
var child = exec("echo " + str, function(err, stdout, stderr) {
|
||||||
|
assert.ok('string', typeof(stdout), 'Expected stdout to be a string');
|
||||||
|
assert.ok('string', typeof(stderr), 'Expected stderr to be a string');
|
||||||
|
assert.equal(str + '\n', stdout);
|
||||||
|
|
||||||
|
success_count++;
|
||||||
|
});
|
||||||
|
|
||||||
|
// no encoding (Buffers expected)
|
||||||
|
var child = exec("echo " + str, {
|
||||||
|
encoding: null
|
||||||
|
}, function(err, stdout, stderr) {
|
||||||
|
assert.ok(stdout instanceof Buffer, 'Expected stdout to be a Buffer');
|
||||||
|
assert.ok(stderr instanceof Buffer, 'Expected stderr to be a Buffer');
|
||||||
|
assert.equal(str + '\n', stdout.toString());
|
||||||
|
|
||||||
|
success_count++;
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('exit', function() {
|
||||||
|
assert.equal(2, success_count);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user