Browse Source

Revert "child_process: measure buffer length in bytes"

This reverts commit c9a5990a76.

PR-URL: https://github.com/nodejs/node/pull/7391
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jackson Tian <shyvo1987@gmail.com>
v7.x
Rich Trott 9 years ago
parent
commit
8da541bf5f
  1. 67
      lib/child_process.js
  2. 1
      test/known_issues/test-child-process-max-buffer.js
  3. 1
      test/parallel/test-child-process-exec-stdout-data-string.js
  4. 15
      test/parallel/test-child-process-maxBuffer-stderr.js

67
lib/child_process.js

@ -146,13 +146,15 @@ exports.execFile = function(file /*, args, options, callback*/) {
}); });
var encoding; var encoding;
var stdoutState; var _stdout;
var stderrState; var _stderr;
var _stdout = [];
var _stderr = [];
if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) { if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) {
encoding = options.encoding; encoding = options.encoding;
_stdout = '';
_stderr = '';
} else { } else {
_stdout = [];
_stderr = [];
encoding = null; encoding = null;
} }
var stdoutLen = 0; var stdoutLen = 0;
@ -174,23 +176,16 @@ exports.execFile = function(file /*, args, options, callback*/) {
if (!callback) return; if (!callback) return;
var stdout = Buffer.concat(_stdout, stdoutLen); // merge chunks
var stderr = Buffer.concat(_stderr, stderrLen); var stdout;
var stderr;
var stdoutEncoding = encoding; if (!encoding) {
var stderrEncoding = encoding; stdout = Buffer.concat(_stdout);
stderr = Buffer.concat(_stderr);
if (stdoutState && stdoutState.decoder) } else {
stdoutEncoding = stdoutState.decoder.encoding; stdout = _stdout;
stderr = _stderr;
if (stderrState && stderrState.decoder) }
stderrEncoding = stderrState.decoder.encoding;
if (stdoutEncoding)
stdout = stdout.toString(stdoutEncoding);
if (stderrEncoding)
stderr = stderr.toString(stderrEncoding);
if (ex) { if (ex) {
// Will be handled later // Will be handled later
@ -250,45 +245,39 @@ exports.execFile = function(file /*, args, options, callback*/) {
} }
if (child.stdout) { if (child.stdout) {
stdoutState = child.stdout._readableState; if (encoding)
child.stdout.setEncoding(encoding);
child.stdout.addListener('data', function(chunk) { child.stdout.addListener('data', function(chunk) {
// If `child.stdout.setEncoding()` happened in userland, convert string to stdoutLen += chunk.length;
// Buffer.
if (stdoutState.decoder) {
chunk = Buffer.from(chunk, stdoutState.decoder.encoding);
}
stdoutLen += chunk.byteLength;
if (stdoutLen > options.maxBuffer) { if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded'); ex = new Error('stdout maxBuffer exceeded');
stdoutLen -= chunk.byteLength;
kill(); kill();
} else { } else {
if (!encoding)
_stdout.push(chunk); _stdout.push(chunk);
else
_stdout += chunk;
} }
}); });
} }
if (child.stderr) { if (child.stderr) {
stderrState = child.stderr._readableState; if (encoding)
child.stderr.setEncoding(encoding);
child.stderr.addListener('data', function(chunk) { child.stderr.addListener('data', function(chunk) {
// If `child.stderr.setEncoding()` happened in userland, convert string to stderrLen += chunk.length;
// Buffer.
if (stderrState.decoder) {
chunk = Buffer.from(chunk, stderrState.decoder.encoding);
}
stderrLen += chunk.byteLength;
if (stderrLen > options.maxBuffer) { if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded'); ex = new Error('stderr maxBuffer exceeded');
stderrLen -= chunk.byteLength;
kill(); kill();
} else { } else {
if (!encoding)
_stderr.push(chunk); _stderr.push(chunk);
else
_stderr += chunk;
} }
}); });
} }

1
test/parallel/test-child-process-maxBuffer-stdout.js → test/known_issues/test-child-process-max-buffer.js

@ -1,4 +1,5 @@
'use strict'; 'use strict';
// Refs: https://github.com/nodejs/node/issues/1901
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const cp = require('child_process'); const cp = require('child_process');

1
test/known_issues/test-child-process-exec-stdout-data-string.js → test/parallel/test-child-process-exec-stdout-data-string.js

@ -14,3 +14,4 @@ const command = common.isWindows ? 'dir' : 'ls';
exec(command).stdout.on('data', cb); exec(command).stdout.on('data', cb);
exec('fhqwhgads').stderr.on('data', cb); exec('fhqwhgads').stderr.on('data', cb);

15
test/parallel/test-child-process-maxBuffer-stderr.js

@ -1,15 +0,0 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const unicode = '中文测试'; // Length = 4, Byte length = 13
if (process.argv[2] === 'child') {
console.error(unicode);
} else {
const cmd = `${process.execPath} ${__filename} child`;
cp.exec(cmd, {maxBuffer: 10}, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err.message, 'stderr maxBuffer exceeded');
}));
}
Loading…
Cancel
Save