1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5// Verify that stdout is never read from. 6const net = require('net'); 7const read = net.Socket.prototype.read; 8 9net.Socket.prototype.read = function() { 10 if (this.fd === 1) 11 throw new Error('reading from stdout!'); 12 if (this.fd === 2) 13 throw new Error('reading from stderr!'); 14 return read.apply(this, arguments); 15}; 16 17if (process.argv[2] === 'child') 18 child(); 19else 20 parent(); 21 22function parent() { 23 const spawn = require('child_process').spawn; 24 const node = process.execPath; 25 26 const c1 = spawn(node, [__filename, 'child']); 27 let c1out = ''; 28 c1.stdout.setEncoding('utf8'); 29 c1.stdout.on('data', function(chunk) { 30 c1out += chunk; 31 }); 32 c1.stderr.setEncoding('utf8'); 33 c1.stderr.on('data', function(chunk) { 34 console.error(`c1err: ${chunk.split('\n').join('\nc1err: ')}`); 35 }); 36 c1.on('close', common.mustCall(function(code, signal) { 37 assert(!code); 38 assert(!signal); 39 assert.strictEqual(c1out, 'ok\n'); 40 console.log('ok'); 41 })); 42 43 const c2 = spawn(node, ['-e', 'console.log("ok")']); 44 let c2out = ''; 45 c2.stdout.setEncoding('utf8'); 46 c2.stdout.on('data', function(chunk) { 47 c2out += chunk; 48 }); 49 c1.stderr.setEncoding('utf8'); 50 c1.stderr.on('data', function(chunk) { 51 console.error(`c1err: ${chunk.split('\n').join('\nc1err: ')}`); 52 }); 53 c2.on('close', common.mustCall(function(code, signal) { 54 assert(!code); 55 assert(!signal); 56 assert.strictEqual(c2out, 'ok\n'); 57 console.log('ok'); 58 })); 59} 60 61function child() { 62 // Should not be reading *ever* in here. 63 net.Socket.prototype.read = function() { 64 throw new Error('no reading allowed in child'); 65 }; 66 console.log('ok'); 67} 68