Skip to content

Commit

Permalink
wip: fix ipc on stdio
Browse files Browse the repository at this point in the history
  • Loading branch information
bzoz committed Dec 4, 2017
1 parent 4924fb3 commit 7157c64
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/internal/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
28 changes: 26 additions & 2 deletions test/parallel/test-child-process-stdout-ipc.js
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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');
})

0 comments on commit 7157c64

Please sign in to comment.