Skip to content

Commit

Permalink
child_process: add string shortcut for fork stdio
Browse files Browse the repository at this point in the history
Add string shortcut option for stdio parameter.

Fixes: #10793
PR-URL: #10866
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Brian White <[email protected]>
  • Loading branch information
trendsetter37 authored and sam-github committed Jan 27, 2017
1 parent b6d2fc9 commit 3268863
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
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');
}
} 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)));

0 comments on commit 3268863

Please sign in to comment.