GeezerVison is showing github.com — child_process: preserve argument type · nodejs/node@2aa06b9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/child_process.js +12-12Lines changed: 12 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -179,12 +179,12 @@ exports.execFile = function(file /*, args, options, callback*/) { | 179 | 179 | // merge chunks | 180 | 180 | var stdout; | 181 | 181 | var stderr; | 182 | | - if (!encoding) { | 183 | | - stdout = Buffer.concat(_stdout); | 184 | | - stderr = Buffer.concat(_stderr); | 185 | | - } else { | | 182 | + if (encoding) { | 186 | 183 | stdout = _stdout; | 187 | 184 | stderr = _stderr; | | 185 | + } else { | | 186 | + stdout = Buffer.concat(_stdout); | | 187 | + stderr = Buffer.concat(_stderr); | 188 | 188 | } | 189 | 189 |
| 190 | 190 | if (ex) { | @@ -249,16 +249,16 @@ exports.execFile = function(file /*, args, options, callback*/) { | 249 | 249 | child.stdout.setEncoding(encoding); | 250 | 250 |
| 251 | 251 | child.stdout.addListener('data', function(chunk) { | 252 | | - stdoutLen += chunk.length; | | 252 | + stdoutLen += encoding ? Buffer.byteLength(chunk, encoding) : chunk.length; | 253 | 253 |
| 254 | 254 | if (stdoutLen > options.maxBuffer) { | 255 | 255 | ex = new Error('stdout maxBuffer exceeded'); | 256 | 256 | kill(); | 257 | 257 | } else { | 258 | | - if (!encoding) | 259 | | - _stdout.push(chunk); | 260 | | - else | | 258 | + if (encoding) | 261 | 259 | _stdout += chunk; | | 260 | + else | | 261 | + _stdout.push(chunk); | 262 | 262 | } | 263 | 263 | }); | 264 | 264 | } | @@ -268,16 +268,16 @@ exports.execFile = function(file /*, args, options, callback*/) { | 268 | 268 | child.stderr.setEncoding(encoding); | 269 | 269 |
| 270 | 270 | child.stderr.addListener('data', function(chunk) { | 271 | | - stderrLen += chunk.length; | | 271 | + stderrLen += encoding ? Buffer.byteLength(chunk, encoding) : chunk.length; | 272 | 272 |
| 273 | 273 | if (stderrLen > options.maxBuffer) { | 274 | 274 | ex = new Error('stderr maxBuffer exceeded'); | 275 | 275 | kill(); | 276 | 276 | } else { | 277 | | - if (!encoding) | 278 | | - _stderr.push(chunk); | 279 | | - else | | 277 | + if (encoding) | 280 | 278 | _stderr += chunk; | | 279 | + else | | 280 | + _stderr.push(chunk); | 281 | 281 | } | 282 | 282 | }); | 283 | 283 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/known_issues/test-child-process-max-buffer.js -16Lines changed: 0 additions & 16 deletions Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +const assert = require('assert'); | | 4 | +const cp = require('child_process'); | | 5 | + | | 6 | +function checkFactory(streamName) { | | 7 | + return common.mustCall((err) => { | | 8 | + const message = `${streamName} maxBuffer exceeded`; | | 9 | + assert.strictEqual(err.message, message); | | 10 | + }); | | 11 | +} | | 12 | + | | 13 | +{ | | 14 | + const cmd = 'echo "hello world"'; | | 15 | + | | 16 | + cp.exec(cmd, { maxBuffer: 5 }, checkFactory('stdout')); | | 17 | +} | | 18 | + | | 19 | +const unicode = '中文测试'; // length = 4, byte length = 12 | | 20 | + | | 21 | +{ | | 22 | + const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`; | | 23 | + | | 24 | + cp.exec(cmd, {maxBuffer: 10}, checkFactory('stdout')); | | 25 | +} | | 26 | + | | 27 | +{ | | 28 | + const cmd = `"${process.execPath}" -e "console.('${unicode}');"`; | | 29 | + | | 30 | + cp.exec(cmd, {maxBuffer: 10}, checkFactory('stderr')); | | 31 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-child-process-exec-stdout-stderr-data-string.js +13-7Lines changed: 13 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,14 +4,20 @@ const common = require('../common'); | 4 | 4 | const assert = require('assert'); | 5 | 5 | const exec = require('child_process').exec; | 6 | 6 |
| 7 | | -const expectedCalls = 2; | 8 | | - | 9 | | -const cb = common.mustCall((data) => { | 10 | | - assert.strictEqual(typeof data, 'string'); | 11 | | -}, expectedCalls); | | 7 | +var stdoutCalls = 0; | | 8 | +var stderrCalls = 0; | 12 | 9 |
| 13 | 10 | const command = common.isWindows ? 'dir' : 'ls'; | 14 | | -exec(command).stdout.on('data', cb); | | 11 | +exec(command).stdout.on('data', (data) => { | | 12 | + stdoutCalls += 1; | | 13 | +}); | 15 | 14 |
| 16 | | -exec('fhqwhgads').stderr.on('data', cb); | | 15 | +exec('fhqwhgads').stderr.on('data', (data) => { | | 16 | + assert.strictEqual(typeof data, 'string'); | | 17 | + stderrCalls += 1; | | 18 | +}); | 17 | 19 |
| | 20 | +process.on('exit', () => { | | 21 | + assert(stdoutCalls > 0); | | 22 | + assert(stderrCalls > 0); | | 23 | +}); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-exec-max-buffer.js -11Lines changed: 0 additions & 11 deletions
|