1'use strict'; 2require('../common'); 3 4// This test checks that the maxBuffer option for child_process.execFileSync() 5// works as expected. 6 7const assert = require('assert'); 8const { getSystemErrorName } = require('util'); 9const { execFileSync } = 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 execFileSync(process.execPath, args, { 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 = execFileSync(process.execPath, args, { maxBuffer: Infinity }); 36 37 assert.deepStrictEqual(ret, msgOutBuf); 38} 39 40// Default maxBuffer size is 1024 * 1024. 41{ 42 assert.throws(() => { 43 execFileSync( 44 process.execPath, 45 ['-e', "console.log('a'.repeat(1024 * 1024))"] 46 ); 47 }, (e) => { 48 assert.ok(e, 'maxBuffer should error'); 49 assert.strictEqual(e.code, 'ENOBUFS'); 50 assert.strictEqual(getSystemErrorName(e.errno), 'ENOBUFS'); 51 return true; 52 }); 53} 54