Skip to content

Commit 4a0fb6f

Browse files
TrottFishrock123
authored andcommitted
Revert "child_process: measure buffer length in bytes"
This reverts commit c9a5990. PR-URL: #7391 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Jackson Tian <shyvo1987@gmail.com>
1 parent 7a7b8f7 commit 4a0fb6f

4 files changed

Lines changed: 33 additions & 57 deletions

File tree

‎lib/child_process.js‎

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ exports.execFile = function(file /*, args, options, callback*/) {
146146
});
147147

148148
varencoding;
149-
varstdoutState;
150-
varstderrState;
151-
var_stdout=[];
152-
var_stderr=[];
149+
var_stdout;
150+
var_stderr;
153151
if(options.encoding!=='buffer'&&Buffer.isEncoding(options.encoding)){
154152
encoding=options.encoding;
153+
_stdout='';
154+
_stderr='';
155155
}else{
156+
_stdout=[];
157+
_stderr=[];
156158
encoding=null;
157159
}
158160
varstdoutLen=0;
@@ -174,23 +176,16 @@ exports.execFile = function(file /*, args, options, callback*/) {
174176

175177
if(!callback)return;
176178

177-
varstdout=Buffer.concat(_stdout,stdoutLen);
178-
varstderr=Buffer.concat(_stderr,stderrLen);
179-
180-
varstdoutEncoding=encoding;
181-
varstderrEncoding=encoding;
182-
183-
if(stdoutState&&stdoutState.decoder)
184-
stdoutEncoding=stdoutState.decoder.encoding;
185-
186-
if(stderrState&&stderrState.decoder)
187-
stderrEncoding=stderrState.decoder.encoding;
188-
189-
if(stdoutEncoding)
190-
stdout=stdout.toString(stdoutEncoding);
191-
192-
if(stderrEncoding)
193-
stderr=stderr.toString(stderrEncoding);
179+
// merge chunks
180+
varstdout;
181+
varstderr;
182+
if(!encoding){
183+
stdout=Buffer.concat(_stdout);
184+
stderr=Buffer.concat(_stderr);
185+
}else{
186+
stdout=_stdout;
187+
stderr=_stderr;
188+
}
194189

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

252247
if(child.stdout){
253-
stdoutState=child.stdout._readableState;
248+
if(encoding)
249+
child.stdout.setEncoding(encoding);
254250

255251
child.stdout.addListener('data',function(chunk){
256-
// If `child.stdout.setEncoding()` happened in userland, convert string to
257-
// Buffer.
258-
if(stdoutState.decoder){
259-
chunk=Buffer.from(chunk,stdoutState.decoder.encoding);
260-
}
261-
262-
stdoutLen+=chunk.byteLength;
252+
stdoutLen+=chunk.length;
263253

264254
if(stdoutLen>options.maxBuffer){
265255
ex=newError('stdout maxBuffer exceeded');
266-
stdoutLen-=chunk.byteLength;
267256
kill();
268257
}else{
269-
_stdout.push(chunk);
258+
if(!encoding)
259+
_stdout.push(chunk);
260+
else
261+
_stdout+=chunk;
270262
}
271263
});
272264
}
273265

274266
if(child.stderr){
275-
stderrState=child.stderr._readableState;
267+
if(encoding)
268+
child.stderr.setEncoding(encoding);
276269

277270
child.stderr.addListener('data',function(chunk){
278-
// If `child.stderr.setEncoding()` happened in userland, convert string to
279-
// Buffer.
280-
if(stderrState.decoder){
281-
chunk=Buffer.from(chunk,stderrState.decoder.encoding);
282-
}
283-
284-
stderrLen+=chunk.byteLength;
271+
stderrLen+=chunk.length;
285272

286273
if(stderrLen>options.maxBuffer){
287274
ex=newError('stderr maxBuffer exceeded');
288-
stderrLen-=chunk.byteLength;
289275
kill();
290276
}else{
291-
_stderr.push(chunk);
277+
if(!encoding)
278+
_stderr.push(chunk);
279+
else
280+
_stderr+=chunk;
292281
}
293282
});
294283
}

test/parallel/test-child-process-maxBuffer-stdout.js renamed to test/known_issues/test-child-process-max-buffer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
// Refs: https://github.com/nodejs/node/issues/1901
23
constcommon=require('../common');
34
constassert=require('assert');
45
constcp=require('child_process');
@@ -9,7 +10,7 @@ if (process.argv[2] === 'child') {
910
}else{
1011
constcmd=`${process.execPath}${__filename} child`;
1112

12-
cp.exec(cmd,{maxBuffer: 10},common.mustCall((err,stdout,stderr)=>{
13+
cp.exec(cmd,{maxBuffer: 10},common.mustCall((err,stdout,stderr)=>{
1314
assert.strictEqual(err.message,'stdout maxBuffer exceeded');
1415
}));
1516
}

test/known_issues/test-child-process-exec-stdout-data-string.js renamed to test/parallel/test-child-process-exec-stdout-data-string.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ const command = common.isWindows ? 'dir' : 'ls';
1414
exec(command).stdout.on('data',cb);
1515

1616
exec('fhqwhgads').stderr.on('data',cb);
17+

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)