Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process.fork: add string-shortcut for stdio #10866

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ added: v0.5.0
piped to the parent, otherwise they will be inherited from the parent, see
the `'pipe'` and `'inherit'` options for [`child_process.spawn()`][]'s
[`stdio`][] for more details (Default: `false`)
* `stdio` {Array} Supports the array version of [`child_process.spawn()`][]'s
[`stdio`][] option. When this option is provided, it overrides `silent`.
The array must contain exactly one item with value `'ipc'` or an error will
be thrown. For instance `[0, 1, 2, 'ipc']`.
* `stdio` {Array|String} See [`child_process.spawn()`][]'s [`stdio`][].
When this option is provided, it overrides `silent`. If the array variant
is used, it must contain exactly one item with value `'ipc'` or an error
will be thrown. For instance `[0, 1, 2, 'ipc']`.
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
* Returns: {ChildProcess}
Expand Down
20 changes: 17 additions & 3 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const _validateStdio = child_process._validateStdio;
const setupChannel = child_process.setupChannel;
const ChildProcess = exports.ChildProcess = child_process.ChildProcess;

function stdioStringToArray(option) {
return [option, option, option, 'ipc'];
}

exports.fork = function(modulePath /*, args, options*/) {

// Get options and args arguments.
Expand Down Expand Up @@ -50,11 +54,21 @@ exports.fork = function(modulePath /*, args, options*/) {

args = execArgv.concat([modulePath], args);

if (!Array.isArray(options.stdio)) {
if (typeof options.stdio === 'string') {
switch (options.stdio) {
case 'ignore':
case 'pipe':
case 'inherit':
options.stdio = stdioStringToArray(options.stdio);
break;
default:
throw new TypeError('Unknown stdio option');
Copy link
Contributor

@mscdex mscdex Jan 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use the same error string used by _validateStdio() which is used by spawn().

}
} else if (!Array.isArray(options.stdio)) {
// Use a separate fd=3 for the IPC channel. Inherit stdin, stdout,
// and stderr from the parent if silent isn't set.
options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] :
[0, 1, 2, 'ipc'];
options.stdio = options.silent ? stdioStringToArray('pipe') :
stdioStringToArray('inherit');
} else if (options.stdio.indexOf('ipc') === -1) {
throw new TypeError('Forked processes must have an IPC channel');
}
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-child-process-fork-stdio-string-variant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');

// Ensures that child_process.fork can accept string
// variant of stdio parameter in options object and
// throws a TypeError when given an unexpected string

const assert = require('assert');
const fork = require('child_process').fork;

const childScript = `${common.fixturesDir}/child-process-spawn-node`;
const errorRegexp = /^TypeError: Unknown stdio option$/;
const malFormedOpts = {stdio: '33'};
const payload = {hello: 'world'};
const stringOpts = {stdio: 'pipe'};

assert.throws(() => fork(childScript, malFormedOpts), errorRegexp);

const child = fork(childScript, stringOpts);

child.on('message', (message) => {
assert.deepStrictEqual(message, {foo: 'bar'});
});

child.send(payload);

child.on('exit', common.mustCall((code) => assert.strictEqual(code, 0)));