| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -157,13 +157,15 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
| 157 | 157 | }); | |
| 158 | 158 | ||
| 159 | 159 | var encoding; | |
| 160 | - var stdoutState; | ||
| 161 | - var stderrState; | ||
| 162 | - var _stdout = []; | ||
| 163 | - var _stderr = []; | ||
| 160 | + var _stdout; | ||
| 161 | + var _stderr; | ||
| 164 | 162 | if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) { | |
| 165 | 163 | encoding = options.encoding; | |
| 164 | + _stdout = ''; | ||
| 165 | + _stderr = ''; | ||
| 166 | 166 | } else { | |
| 167 | + _stdout = []; | ||
| 168 | + _stderr = []; | ||
| 167 | 169 | encoding = null; | |
| 168 | 170 | } | |
| 169 | 171 | var stdoutLen = 0; | |
@@ -185,23 +187,16 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
| 185 | 187 | ||
| 186 | 188 | if (!callback) return; | |
| 187 | 189 | ||
| 188 | - var stdout = Buffer.concat(_stdout, stdoutLen); | ||
| 189 | - var stderr = Buffer.concat(_stderr, stderrLen); | ||
| 190 | - | ||
| 191 | - var stdoutEncoding = encoding; | ||
| 192 | - var stderrEncoding = encoding; | ||
| 193 | - | ||
| 194 | - if (stdoutState && stdoutState.decoder) | ||
| 195 | - stdoutEncoding = stdoutState.decoder.encoding; | ||
| 196 | - | ||
| 197 | - if (stderrState && stderrState.decoder) | ||
| 198 | - stderrEncoding = stderrState.decoder.encoding; | ||
| 199 | - | ||
| 200 | - if (stdoutEncoding) | ||
| 201 | - stdout = stdout.toString(stdoutEncoding); | ||
| 202 | - | ||
| 203 | - if (stderrEncoding) | ||
| 204 | - stderr = stderr.toString(stderrEncoding); | ||
| 190 | + // merge chunks | ||
| 191 | + var stdout; | ||
| 192 | + var stderr; | ||
| 193 | + if (!encoding) { | ||
| 194 | + stdout = Buffer.concat(_stdout); | ||
| 195 | + stderr = Buffer.concat(_stderr); | ||
| 196 | + } else { | ||
| 197 | + stdout = _stdout; | ||
| 198 | + stderr = _stderr; | ||
| 199 | + } | ||
| 205 | 200 | ||
| 206 | 201 | if (ex) { | |
| 207 | 202 | // Will be handled later | |
@@ -261,45 +256,39 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
| 261 | 256 | } | |
| 262 | 257 | ||
| 263 | 258 | if (child.stdout) { | |
| 264 | - stdoutState = child.stdout._readableState; | ||
| 259 | + if (encoding) | ||
| 260 | + child.stdout.setEncoding(encoding); | ||
| 265 | 261 | ||
| 266 | 262 | child.stdout.addListener('data', function(chunk) { | |
| 267 | - // If `child.stdout.setEncoding()` happened in userland, convert string to | ||
| 268 | - // Buffer. | ||
| 269 | - if (stdoutState.decoder) { | ||
| 270 | - chunk = Buffer.from(chunk, stdoutState.decoder.encoding); | ||
| 271 | - } | ||
| 272 | - | ||
| 273 | - stdoutLen += chunk.byteLength; | ||
| 263 | + stdoutLen += chunk.length; | ||
| 274 | 264 | ||
| 275 | 265 | if (stdoutLen > options.maxBuffer) { | |
| 276 | 266 | ex = new Error('stdout maxBuffer exceeded'); | |
| 277 | - stdoutLen -= chunk.byteLength; | ||
| 278 | 267 | kill(); | |
| 279 | 268 | } else { | |
| 280 | - _stdout.push(chunk); | ||
| 269 | + if (!encoding) | ||
| 270 | + _stdout.push(chunk); | ||
| 271 | + else | ||
| 272 | + _stdout += chunk; | ||
| 281 | 273 | } | |
| 282 | 274 | }); | |
| 283 | 275 | } | |
| 284 | 276 | ||
| 285 | 277 | if (child.stderr) { | |
| 286 | - stderrState = child.stderr._readableState; | ||
| 278 | + if (encoding) | ||
| 279 | + child.stderr.setEncoding(encoding); | ||
| 287 | 280 | ||
| 288 | 281 | child.stderr.addListener('data', function(chunk) { | |
| 289 | - // If `child.stderr.setEncoding()` happened in userland, convert string to | ||
| 290 | - // Buffer. | ||
| 291 | - if (stderrState.decoder) { | ||
| 292 | - chunk = Buffer.from(chunk, stderrState.decoder.encoding); | ||
| 293 | - } | ||
| 294 | - | ||
| 295 | - stderrLen += chunk.byteLength; | ||
| 282 | + stderrLen += chunk.length; | ||
| 296 | 283 | ||
| 297 | 284 | if (stderrLen > options.maxBuffer) { | |
| 298 | 285 | ex = new Error('stderr maxBuffer exceeded'); | |
| 299 | - stderrLen -= chunk.byteLength; | ||
| 300 | 286 | kill(); | |
| 301 | 287 | } else { | |
| 302 | - _stderr.push(chunk); | ||
| 288 | + if (!encoding) | ||
| 289 | + _stderr.push(chunk); | ||
| 290 | + else | ||
| 291 | + _stderr += chunk; | ||
| 303 | 292 | } | |
| 304 | 293 | }); | |
| 305 | 294 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | + // Refs: https://github.com/nodejs/node/issues/1901 | ||
| 2 | 3 | const common = require('../common'); | |
| 3 | 4 | const assert = require('assert'); | |
| 4 | 5 | const cp = require('child_process'); | |
@@ -9,7 +10,7 @@ if (process.argv[2] === 'child') { | |||
| 9 | 10 | } else { | |
| 10 | 11 | const cmd = `${process.execPath} ${__filename} child`; | |
| 11 | 12 | ||
| 12 | - cp.exec(cmd, {maxBuffer: 10}, common.mustCall((err, stdout, stderr) => { | ||
| 13 | + cp.exec(cmd, { maxBuffer: 10 }, common.mustCall((err, stdout, stderr) => { | ||
| 13 | 14 | assert.strictEqual(err.message, 'stdout maxBuffer exceeded'); | |
| 14 | 15 | })); | |
| 15 | 16 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments