Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "child_process: measure buffer length in bytes" #7377

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ exports.execFile = function(file /*, args, options, callback*/) {
});

var encoding;
var stdoutState;
var stderrState;
var _stdout = [];
var _stderr = [];
var _stdout;
var _stderr;
if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) {
encoding = options.encoding;
_stdout = '';
_stderr = '';
} else {
_stdout = [];
_stderr = [];
encoding = null;
}
var stdoutLen = 0;
Expand All @@ -174,23 +176,16 @@ exports.execFile = function(file /*, args, options, callback*/) {

if (!callback) return;

var stdout = Buffer.concat(_stdout, stdoutLen);
var stderr = Buffer.concat(_stderr, stderrLen);

var stdoutEncoding = encoding;
var stderrEncoding = encoding;

if (stdoutState && stdoutState.decoder)
stdoutEncoding = stdoutState.decoder.encoding;

if (stderrState && stderrState.decoder)
stderrEncoding = stderrState.decoder.encoding;

if (stdoutEncoding)
stdout = stdout.toString(stdoutEncoding);

if (stderrEncoding)
stderr = stderr.toString(stderrEncoding);
// merge chunks
var stdout;
var stderr;
if (!encoding) {
stdout = Buffer.concat(_stdout);
stderr = Buffer.concat(_stderr);
} else {
stdout = _stdout;
stderr = _stderr;
}

if (ex) {
// Will be handled later
Expand Down Expand Up @@ -250,45 +245,39 @@ exports.execFile = function(file /*, args, options, callback*/) {
}

if (child.stdout) {
stdoutState = child.stdout._readableState;
if (encoding)
child.stdout.setEncoding(encoding);

child.stdout.addListener('data', function(chunk) {
// If `child.stdout.setEncoding()` happened in userland, convert string to
// Buffer.
if (stdoutState.decoder) {
chunk = Buffer.from(chunk, stdoutState.decoder.encoding);
}

stdoutLen += chunk.byteLength;
stdoutLen += chunk.length;

if (stdoutLen > options.maxBuffer) {
ex = new Error('stdout maxBuffer exceeded');
stdoutLen -= chunk.byteLength;
kill();
} else {
_stdout.push(chunk);
if (!encoding)
_stdout.push(chunk);
else
_stdout += chunk;
}
});
}

if (child.stderr) {
stderrState = child.stderr._readableState;
if (encoding)
child.stderr.setEncoding(encoding);

child.stderr.addListener('data', function(chunk) {
// If `child.stderr.setEncoding()` happened in userland, convert string to
// Buffer.
if (stderrState.decoder) {
chunk = Buffer.from(chunk, stderrState.decoder.encoding);
}

stderrLen += chunk.byteLength;
stderrLen += chunk.length;

if (stderrLen > options.maxBuffer) {
ex = new Error('stderr maxBuffer exceeded');
stderrLen -= chunk.byteLength;
kill();
} else {
_stderr.push(chunk);
if (!encoding)
_stderr.push(chunk);
else
_stderr += chunk;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Refs: https://github.com/nodejs/node/issues/1901
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
Expand All @@ -9,7 +10,7 @@ if (process.argv[2] === 'child') {
} else {
const cmd = `${process.execPath} ${__filename} child`;

cp.exec(cmd, {maxBuffer: 10}, common.mustCall((err, stdout, stderr) => {
cp.exec(cmd, { maxBuffer: 10 }, common.mustCall((err, stdout, stderr) => {
assert.strictEqual(err.message, 'stdout maxBuffer exceeded');
}));
}
15 changes: 15 additions & 0 deletions test/parallel/test-child-process-exec-stdout-data-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
// Refs: https://github.com/nodejs/node/issues/7342
const common = require('../common');
const assert = require('assert');
const exec = require('child_process').exec;

const keepAlive = setInterval(() => {}, 9999);

const command = common.isWindows ? 'dir' : 'ls';
const e = exec(command);

e.stdout.on('data', function(data) {
assert.strictEqual(typeof data, 'string');
clearInterval(keepAlive);
});
15 changes: 0 additions & 15 deletions test/parallel/test-child-process-maxBuffer-stderr.js

This file was deleted.