1'use strict'; 2require('../common'); 3 4// This test checks that the maxBuffer option for child_process.spawnSync() 5// works as expected. 6 7const assert = require('assert'); 8const { getSystemErrorName } = require('util'); 9const { execSync } = require('child_process'); 10const msgOut = 'this is stdout'; 11const msgOutBuf = Buffer.from(`${msgOut}\n`); 12 13const args = [ 14 '-e', 15 `"console.log('${msgOut}')";`, 16]; 17 18// Verify that an error is returned if maxBuffer is surpassed. 19{ 20 assert.throws(() => { 21 execSync(`"${process.execPath}" ${args.join(' ')}`, { maxBuffer: 1 }); 22 }, (e) => { 23 assert.ok(e, 'maxBuffer should error'); 24 assert.strictEqual(e.code, 'ENOBUFS'); 25 assert.strictEqual(getSystemErrorName(e.errno), 'ENOBUFS'); 26 // We can have buffers larger than maxBuffer because underneath we alloc 64k 27 // that matches our read sizes. 28 assert.deepStrictEqual(e.stdout, msgOutBuf); 29 return true; 30 }); 31} 32 33// Verify that a maxBuffer size of Infinity works. 34{ 35 const ret = execSync( 36 `"${process.execPath}" ${args.join(' ')}`, 37 { maxBuffer: Infinity } 38 ); 39 40 assert.deepStrictEqual(ret, msgOutBuf); 41} 42 43// Default maxBuffer size is 1024 * 1024. 44{ 45 assert.throws(() => { 46 execSync( 47 `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024))"` 48 ); 49 }, (e) => { 50 assert.ok(e, 'maxBuffer should error'); 51 assert.strictEqual(e.code, 'ENOBUFS'); 52 assert.strictEqual(getSystemErrorName(e.errno), 'ENOBUFS'); 53 return true; 54 }); 55} 56 57// Default maxBuffer size is 1024 * 1024. 58{ 59 const ret = execSync( 60 `"${process.execPath}" -e "console.log('a'.repeat(1024 * 1024 - 1))"` 61 ); 62 63 assert.deepStrictEqual( 64 ret.toString().trim(), 65 'a'.repeat(1024 * 1024 - 1) 66 ); 67} 68