0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

child_process: Callback with Buffers from exec

Only return strings when encoding is not null.
This commit is contained in:
Seth Fitzsimmons 2013-07-13 16:34:52 -07:00 committed by isaacs
parent 26bc8db33f
commit 711d1934ea
2 changed files with 74 additions and 9 deletions

View File

@ -611,8 +611,10 @@ exports.execFile = function(file /* args, options, callback */) {
windowsVerbatimArguments: !!options.windowsVerbatimArguments
});
var stdout = '';
var stderr = '';
var _stdout = [];
var _stderr = [];
var stdoutLen = 0;
var stderrLen = 0;
var killed = false;
var exited = false;
var timeoutId;
@ -630,6 +632,16 @@ exports.execFile = function(file /* args, options, callback */) {
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) {
callback(ex, stdout, stderr);
} else if (code === 0 && signal === null) {
@ -670,20 +682,21 @@ exports.execFile = function(file /* args, options, callback */) {
}, options.timeout);
}
child.stdout.setEncoding(options.encoding);
child.stderr.setEncoding(options.encoding);
child.stdout.addListener('data', function(chunk) {
stdout += chunk;
if (stdout.length > options.maxBuffer) {
_stdout.push(chunk);
stdoutLen += chunk.length;
if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded.');
kill();
}
});
child.stderr.addListener('data', function(chunk) {
stderr += chunk;
if (stderr.length > options.maxBuffer) {
_stderr.push(chunk);
stderrLen += chunk.length;
if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded.');
kill();
}

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