Browse Source

child_process: add string shortcut for fork stdio

Add string shortcut option for stdio parameter.

Fixes: https://github.com/nodejs/node/issues/10793
PR-URL: https://github.com/nodejs/node/pull/10866
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Brian White <mscdex@mscdex.net>
v6
Javis Sullivan 8 years ago
committed by Sam Roberts
parent
commit
3268863ebc
  1. 8
      doc/api/child_process.md
  2. 20
      lib/child_process.js
  3. 27
      test/parallel/test-child-process-fork-stdio-string-variant.js

8
doc/api/child_process.md

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

20
lib/child_process.js

@ -17,6 +17,10 @@ const _validateStdio = child_process._validateStdio;
const setupChannel = child_process.setupChannel; const setupChannel = child_process.setupChannel;
const ChildProcess = exports.ChildProcess = child_process.ChildProcess; const ChildProcess = exports.ChildProcess = child_process.ChildProcess;
function stdioStringToArray(option) {
return [option, option, option, 'ipc'];
}
exports.fork = function(modulePath /*, args, options*/) { exports.fork = function(modulePath /*, args, options*/) {
// Get options and args arguments. // Get options and args arguments.
@ -50,11 +54,21 @@ exports.fork = function(modulePath /*, args, options*/) {
args = execArgv.concat([modulePath], args); 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, // Use a separate fd=3 for the IPC channel. Inherit stdin, stdout,
// and stderr from the parent if silent isn't set. // and stderr from the parent if silent isn't set.
options.stdio = options.silent ? ['pipe', 'pipe', 'pipe', 'ipc'] : options.stdio = options.silent ? stdioStringToArray('pipe') :
[0, 1, 2, 'ipc']; stdioStringToArray('inherit');
} else if (options.stdio.indexOf('ipc') === -1) { } else if (options.stdio.indexOf('ipc') === -1) {
throw new TypeError('Forked processes must have an IPC channel'); throw new TypeError('Forked processes must have an IPC channel');
} }

27
test/parallel/test-child-process-fork-stdio-string-variant.js

@ -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)));
Loading…
Cancel
Save