diff --git a/lib/internal/process.js b/lib/internal/process.js index 21a74abba7e313..1008afcac71eeb 100644 --- a/lib/internal/process.js +++ b/lib/internal/process.js @@ -224,6 +224,12 @@ function setupChannel() { const fd = parseInt(process.env.NODE_CHANNEL_FD, 10); assert(fd >= 0); + Object.defineProperty(process, 'ipcChannelFd', { + configurable: true, + enumerable: true, + value: fd + }); + // Make sure it's not accidentally inherited by child processes. delete process.env.NODE_CHANNEL_FD; diff --git a/lib/net.js b/lib/net.js index 78e12fac2084c4..78da89ee898cb3 100644 --- a/lib/net.js +++ b/lib/net.js @@ -59,7 +59,11 @@ function noop() {} function createHandle(fd) { const type = TTYWrap.guessHandleType(fd); - if (type === 'PIPE') return new Pipe(); + if (type === 'PIPE') { + const useIPCPipe = process.platform === 'win32' && + process.ipcChannelFd === fd; + return new Pipe(useIPCPipe); + } if (type === 'TCP') return new TCP(); throw new errors.TypeError('ERR_INVALID_FD_TYPE', type); } diff --git a/test/parallel/test-child-process-stdout-ipc.js b/test/parallel/test-child-process-stdout-ipc.js index c916be95b73fc0..f3645526bd437c 100644 --- a/test/parallel/test-child-process-stdout-ipc.js +++ b/test/parallel/test-child-process-stdout-ipc.js @@ -1,11 +1,25 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); - +const util = require('util'); +const fs = require('fs'); const spawn = require('child_process').spawn; if (process.argv[2] === 'child') { - process.send('hahah'); + const consoleLog = common.mustCall(() => { + console.log('{"method": "console.log"}'); + }) + const processSend = common.mustCall(() => { + process.send({method: 'process.send'}, consoleLog); + }); + const fsWrite = common.mustCall(() => { + fs.write(1, '{"method": "process.fs.write"}\n', processSend); + }); + const stdoutWrite = () => { + process.stdout.write('{"method": "process.stdout.write"}\n', fsWrite); + }; + + stdoutWrite(); return; } @@ -16,3 +30,13 @@ const proc = spawn(process.execPath, [__filename, 'child'], { proc.on('exit', common.mustCall(function(code) { assert.strictEqual(code, 0); })); + +let messagesReceived = 0; +proc.on('message', (message) => { + console.log(`Received: ${util.inspect(message)}`); + ++messagesReceived; +}); + +process.on('exit', () => { + assert.equal(messagesReceived, 4, 'Not all messages has been received'); +})