| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2a59e4e commit 8eb18e4
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common.js'); | ||
| 3 | + const bench = common.createBenchmark(main, { | ||
| 4 | + len: [64, 256, 1024, 4096, 32768], | ||
| 5 | + dur: [5] | ||
| 6 | + }); | ||
| 7 | + | ||
| 8 | + const exec = require('child_process').exec; | ||
| 9 | + function main(conf) { | ||
| 10 | + bench.start(); | ||
| 11 | + | ||
| 12 | + const dur = +conf.dur; | ||
| 13 | + const len = +conf.len; | ||
| 14 | + | ||
| 15 | + const msg = `"${'.'.repeat(len)}"`; | ||
| 16 | + msg.match(/./); | ||
| 17 | + const options = {'stdio': ['ignore', 'pipe', 'ignore']}; | ||
| 18 | + // NOTE: Command below assumes bash shell. | ||
| 19 | + const child = exec(`while\n echo ${msg}\ndo :; done\n`, options); | ||
| 20 | + | ||
| 21 | + var bytes = 0; | ||
| 22 | + child.stdout.on('data', function(msg) { | ||
| 23 | + bytes += msg.length; | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + setTimeout(function() { | ||
| 27 | + child.kill(); | ||
| 28 | + bench.end(bytes); | ||
| 29 | + }, dur * 1000); | ||
| 30 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,20 +1,20 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | - var common = require('../common.js'); | ||
| 3 | - var bench = common.createBenchmark(main, { | ||
| 2 | + const common = require('../common.js'); | ||
| 3 | + const bench = common.createBenchmark(main, { | ||
| 4 | 4 | len: [64, 256, 1024, 4096, 32768], | |
| 5 | 5 | dur: [5] | |
| 6 | 6 | }); | |
| 7 | 7 | ||
| 8 | - var spawn = require('child_process').spawn; | ||
| 8 | + const spawn = require('child_process').spawn; | ||
| 9 | 9 | function main(conf) { | |
| 10 | 10 | bench.start(); | |
| 11 | 11 | ||
| 12 | - var dur = +conf.dur; | ||
| 13 | - var len = +conf.len; | ||
| 12 | + const dur = +conf.dur; | ||
| 13 | + const len = +conf.len; | ||
| 14 | 14 | ||
| 15 | - var msg = '"' + Array(len).join('.') + '"'; | ||
| 16 | - var options = { 'stdio': ['ignore', 'ipc', 'ignore'] }; | ||
| 17 | - var child = spawn('yes', [msg], options); | ||
| 15 | + const msg = '"' + Array(len).join('.') + '"'; | ||
| 16 | + const options = {'stdio': ['ignore', 'ipc', 'ignore']}; | ||
| 17 | + const child = spawn('yes', [msg], options); | ||
| 18 | 18 | ||
| 19 | 19 | var bytes = 0; | |
| 20 | 20 | child.on('message', function(msg) { | |
@@ -23,7 +23,7 @@ function main(conf) { | |||
| 23 | 23 | ||
| 24 | 24 | setTimeout(function() { | |
| 25 | 25 | child.kill(); | |
| 26 | - var gbits = (bytes * 8) / (1024 * 1024 * 1024); | ||
| 26 | + const gbits = (bytes * 8) / (1024 * 1024 * 1024); | ||
| 27 | 27 | bench.end(gbits); | |
| 28 | 28 | }, dur * 1000); | |
| 29 | 29 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -157,15 +157,13 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
| 157 | 157 | }); | |
| 158 | 158 | ||
| 159 | 159 | var encoding; | |
| 160 | - var _stdout; | ||
| 161 | - var _stderr; | ||
| 160 | + var stdoutState; | ||
| 161 | + var stderrState; | ||
| 162 | + var _stdout = []; | ||
| 163 | + var _stderr = []; | ||
| 162 | 164 | if (options.encoding !== 'buffer' && Buffer.isEncoding(options.encoding)) { | |
| 163 | 165 | encoding = options.encoding; | |
| 164 | - _stdout = ''; | ||
| 165 | - _stderr = ''; | ||
| 166 | 166 | } else { | |
| 167 | - _stdout = []; | ||
| 168 | - _stderr = []; | ||
| 169 | 167 | encoding = null; | |
| 170 | 168 | } | |
| 171 | 169 | var stdoutLen = 0; | |
@@ -187,16 +185,23 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
| 187 | 185 | ||
| 188 | 186 | if (!callback) return; | |
| 189 | 187 | ||
| 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 | - } | ||
| 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); | ||
| 200 | 205 | ||
| 201 | 206 | if (ex) { | |
| 202 | 207 | // Will be handled later | |
@@ -256,39 +261,45 @@ exports.execFile = function(file /*, args, options, callback*/) { | |||
| 256 | 261 | } | |
| 257 | 262 | ||
| 258 | 263 | if (child.stdout) { | |
| 259 | - if (encoding) | ||
| 260 | - child.stdout.setEncoding(encoding); | ||
| 264 | + stdoutState = child.stdout._readableState; | ||
| 261 | 265 | ||
| 262 | 266 | child.stdout.addListener('data', function(chunk) { | |
| 263 | - stdoutLen += chunk.length; | ||
| 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; | ||
| 264 | 274 | ||
| 265 | 275 | if (stdoutLen > options.maxBuffer) { | |
| 266 | 276 | ex = new Error('stdout maxBuffer exceeded'); | |
| 277 | + stdoutLen -= chunk.byteLength; | ||
| 267 | 278 | kill(); | |
| 268 | 279 | } else { | |
| 269 | - if (!encoding) | ||
| 270 | - _stdout.push(chunk); | ||
| 271 | - else | ||
| 272 | - _stdout += chunk; | ||
| 280 | + _stdout.push(chunk); | ||
| 273 | 281 | } | |
| 274 | 282 | }); | |
| 275 | 283 | } | |
| 276 | 284 | ||
| 277 | 285 | if (child.stderr) { | |
| 278 | - if (encoding) | ||
| 279 | - child.stderr.setEncoding(encoding); | ||
| 286 | + stderrState = child.stderr._readableState; | ||
| 280 | 287 | ||
| 281 | 288 | child.stderr.addListener('data', function(chunk) { | |
| 282 | - stderrLen += chunk.length; | ||
| 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; | ||
| 283 | 296 | ||
| 284 | 297 | if (stderrLen > options.maxBuffer) { | |
| 285 | 298 | ex = new Error('stderr maxBuffer exceeded'); | |
| 299 | + stderrLen -= chunk.byteLength; | ||
| 286 | 300 | kill(); | |
| 287 | 301 | } else { | |
| 288 | - if (!encoding) | ||
| 289 | - _stderr.push(chunk); | ||
| 290 | - else | ||
| 291 | - _stderr += chunk; | ||
| 302 | + _stderr.push(chunk); | ||
| 292 | 303 | } | |
| 293 | 304 | }); | |
| 294 | 305 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const cp = require('child_process'); | ||
| 5 | + const unicode = '中文测试'; // Length = 4, Byte length = 13 | ||
| 6 | + | ||
| 7 | + if (process.argv[2] === 'child') { | ||
| 8 | + console.error(unicode); | ||
| 9 | + } else { | ||
| 10 | + const cmd = `${process.execPath} ${__filename} child`; | ||
| 11 | + | ||
| 12 | + cp.exec(cmd, {maxBuffer: 10}, common.mustCall((err, stdout, stderr) => { | ||
| 13 | + assert.strictEqual(err.message, 'stderr maxBuffer exceeded'); | ||
| 14 | + })); | ||
| 15 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,4 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | - // Refs: https://github.com/nodejs/node/issues/1901 | ||
| 3 | 2 | const common = require('../common'); | |
| 4 | 3 | const assert = require('assert'); | |
| 5 | 4 | const cp = require('child_process'); | |
@@ -10,7 +9,7 @@ if (process.argv[2] === 'child') { | |||
| 10 | 9 | } else { | |
| 11 | 10 | const cmd = `${process.execPath} ${__filename} child`; | |
| 12 | 11 | ||
| 13 | - cp.exec(cmd, { maxBuffer: 10 }, common.mustCall((err, stdout, stderr) => { | ||
| 12 | + cp.exec(cmd, {maxBuffer: 10}, common.mustCall((err, stdout, stderr) => { | ||
| 14 | 13 | assert.strictEqual(err.message, 'stdout maxBuffer exceeded'); | |
| 15 | 14 | })); | |
| 16 | 15 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments