Skip to content

Commit 3268863

Browse files
trendsetter37sam-github
authored andcommitted
child_process: add string shortcut for fork stdio
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]>
1 parent b6d2fc9 commit 3268863

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

doc/api/child_process.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ added: v0.5.0
265265
piped to the parent, otherwise they will be inherited from the parent, see
266266
the `'pipe'` and `'inherit'` options for [`child_process.spawn()`][]'s
267267
[`stdio`][] for more details (Default: `false`)
268-
* `stdio` {Array} Supports the array version of [`child_process.spawn()`][]'s
269-
[`stdio`][] option. When this option is provided, it overrides `silent`.
270-
The array must contain exactly one item with value `'ipc'` or an error will
271-
be thrown. For instance `[0, 1, 2, 'ipc']`.
268+
* `stdio` {Array|String} See [`child_process.spawn()`][]'s [`stdio`][].
269+
When this option is provided, it overrides `silent`. If the array variant
270+
is used, it must contain exactly one item with value `'ipc'` or an error
271+
will be thrown. For instance `[0, 1, 2, 'ipc']`.
272272
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
273273
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
274274
* Returns: {ChildProcess}

lib/child_process.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const _validateStdio = child_process._validateStdio;
1717
const setupChannel = child_process.setupChannel;
1818
const ChildProcess = exports.ChildProcess = child_process.ChildProcess;
1919

20+
function stdioStringToArray(option) {
21+
return [option, option, option, 'ipc'];
22+
}
23+
2024
exports.fork = function(modulePath /*, args, options*/) {
2125

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

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

53-
if (!Array.isArray(options.stdio)) {
57+
if (typeof options.stdio === 'string') {
58+
switch (options.stdio) {
59+
case 'ignore':
60+
case 'pipe':
61+
case 'inherit':
62+
options.stdio = stdioStringToArray(options.stdio);
63+
break;
64+
default:
65+
throw new TypeError('Unknown stdio option');
66+
}
67+
} else if (!Array.isArray(options.stdio)) {
5468
// Use a separate fd=3 for the IPC channel. Inherit stdin, stdout,
5569
// and stderr from the parent if silent isn't set.
56-
options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] :
57-
[0, 1, 2, 'ipc'];
70+
options.stdio = options.silent ? stdioStringToArray('pipe') :
71+
stdioStringToArray('inherit');
5872
} else if (options.stdio.indexOf('ipc') === -1) {
5973
throw new TypeError('Forked processes must have an IPC channel');
6074
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Ensures that child_process.fork can accept string
5+
// variant of stdio parameter in options object and
6+
// throws a TypeError when given an unexpected string
7+
8+
const assert = require('assert');
9+
const fork = require('child_process').fork;
10+
11+
const childScript = `${common.fixturesDir}/child-process-spawn-node`;
12+
const errorRegexp = /^TypeError: Unknown stdio option$/;
13+
const malFormedOpts = {stdio: '33'};
14+
const payload = {hello: 'world'};
15+
const stringOpts = {stdio: 'pipe'};
16+
17+
assert.throws(() => fork(childScript, malFormedOpts), errorRegexp);
18+
19+
const child = fork(childScript, stringOpts);
20+
21+
child.on('message', (message) => {
22+
assert.deepStrictEqual(message, {foo: 'bar'});
23+
});
24+
25+
child.send(payload);
26+
27+
child.on('exit', common.mustCall((code) => assert.strictEqual(code, 0)));

0 commit comments

Comments
 (0)