1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const cp = require('child_process'); 5 6function runChecks(err, stdio, streamName, expected) { 7 assert.strictEqual(err.message, `${streamName} maxBuffer length exceeded`); 8 assert(err instanceof RangeError); 9 assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER'); 10 assert.deepStrictEqual(stdio[streamName], expected); 11} 12 13// default value 14{ 15 const cmd = 16 `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`; 17 18 cp.exec(cmd, common.mustCall((err) => { 19 assert(err instanceof RangeError); 20 assert.strictEqual(err.message, 'stdout maxBuffer length exceeded'); 21 assert.strictEqual(err.code, 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER'); 22 })); 23} 24 25// default value 26{ 27 const cmd = 28 `${process.execPath} -e "console.log('a'.repeat(1024 * 1024 - 1))"`; 29 30 cp.exec(cmd, common.mustSucceed((stdout, stderr) => { 31 assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1)); 32 assert.strictEqual(stderr, ''); 33 })); 34} 35 36{ 37 const cmd = `"${process.execPath}" -e "console.log('hello world');"`; 38 const options = { maxBuffer: Infinity }; 39 40 cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => { 41 assert.strictEqual(stdout.trim(), 'hello world'); 42 assert.strictEqual(stderr, ''); 43 })); 44} 45 46{ 47 const cmd = 'echo hello world'; 48 49 cp.exec( 50 cmd, 51 { maxBuffer: 5 }, 52 common.mustCall((err, stdout, stderr) => { 53 runChecks(err, { stdout, stderr }, 'stdout', 'hello'); 54 }) 55 ); 56} 57 58// default value 59{ 60 const cmd = 61 `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"`; 62 63 cp.exec( 64 cmd, 65 common.mustCall((err, stdout, stderr) => { 66 runChecks( 67 err, 68 { stdout, stderr }, 69 'stdout', 70 'a'.repeat(1024 * 1024) 71 ); 72 }) 73 ); 74} 75 76// default value 77{ 78 const cmd = 79 `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"`; 80 81 cp.exec(cmd, common.mustSucceed((stdout, stderr) => { 82 assert.strictEqual(stdout.trim(), 'a'.repeat(1024 * 1024 - 1)); 83 assert.strictEqual(stderr, ''); 84 })); 85} 86 87const unicode = '中文测试'; // length = 4, byte length = 12 88 89{ 90 const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`; 91 92 cp.exec( 93 cmd, 94 { maxBuffer: 10 }, 95 common.mustCall((err, stdout, stderr) => { 96 runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n'); 97 }) 98 ); 99} 100 101{ 102 const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`; 103 104 cp.exec( 105 cmd, 106 { maxBuffer: 3 }, 107 common.mustCall((err, stdout, stderr) => { 108 runChecks(err, { stdout, stderr }, 'stderr', '中文测'); 109 }) 110 ); 111} 112 113{ 114 const cmd = `"${process.execPath}" -e "console.log('${unicode}');"`; 115 116 const child = cp.exec( 117 cmd, 118 { encoding: null, maxBuffer: 10 }, 119 common.mustCall((err, stdout, stderr) => { 120 runChecks(err, { stdout, stderr }, 'stdout', '中文测试\n'); 121 }) 122 ); 123 124 child.stdout.setEncoding('utf-8'); 125} 126 127{ 128 const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`; 129 130 const child = cp.exec( 131 cmd, 132 { encoding: null, maxBuffer: 3 }, 133 common.mustCall((err, stdout, stderr) => { 134 runChecks(err, { stdout, stderr }, 'stderr', '中文测'); 135 }) 136 ); 137 138 child.stderr.setEncoding('utf-8'); 139} 140 141{ 142 const cmd = `"${process.execPath}" -e "console.error('${unicode}');"`; 143 144 cp.exec( 145 cmd, 146 { encoding: null, maxBuffer: 5 }, 147 common.mustCall((err, stdout, stderr) => { 148 const buf = Buffer.from(unicode).slice(0, 5); 149 runChecks(err, { stdout, stderr }, 'stderr', buf); 150 }) 151 ); 152} 153