child_process: refactor stdioStringToArray function
reduce the function in both files to one. PR-URL: https://github.com/nodejs/node/pull/27657 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
03d43539f9
commit
9f99d4e252
@ -37,7 +37,6 @@ const {
|
|||||||
ERR_CHILD_PROCESS_IPC_REQUIRED,
|
ERR_CHILD_PROCESS_IPC_REQUIRED,
|
||||||
ERR_CHILD_PROCESS_STDIO_MAXBUFFER,
|
ERR_CHILD_PROCESS_STDIO_MAXBUFFER,
|
||||||
ERR_INVALID_ARG_TYPE,
|
ERR_INVALID_ARG_TYPE,
|
||||||
ERR_INVALID_OPT_VALUE,
|
|
||||||
ERR_OUT_OF_RANGE
|
ERR_OUT_OF_RANGE
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
const { clearTimeout, setTimeout } = require('timers');
|
const { clearTimeout, setTimeout } = require('timers');
|
||||||
@ -46,24 +45,14 @@ const child_process = require('internal/child_process');
|
|||||||
const {
|
const {
|
||||||
getValidStdio,
|
getValidStdio,
|
||||||
setupChannel,
|
setupChannel,
|
||||||
ChildProcess
|
ChildProcess,
|
||||||
|
stdioStringToArray
|
||||||
} = child_process;
|
} = child_process;
|
||||||
|
|
||||||
const MAX_BUFFER = 1024 * 1024;
|
const MAX_BUFFER = 1024 * 1024;
|
||||||
|
|
||||||
exports.ChildProcess = ChildProcess;
|
exports.ChildProcess = ChildProcess;
|
||||||
|
|
||||||
function stdioStringToArray(option) {
|
|
||||||
switch (option) {
|
|
||||||
case 'ignore':
|
|
||||||
case 'pipe':
|
|
||||||
case 'inherit':
|
|
||||||
return [option, option, option, 'ipc'];
|
|
||||||
default:
|
|
||||||
throw new ERR_INVALID_OPT_VALUE('stdio', option);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.fork = function fork(modulePath /* , args, options */) {
|
exports.fork = function fork(modulePath /* , args, options */) {
|
||||||
validateString(modulePath, 'modulePath');
|
validateString(modulePath, 'modulePath');
|
||||||
|
|
||||||
@ -104,12 +93,13 @@ exports.fork = function fork(modulePath /* , args, options */) {
|
|||||||
args = execArgv.concat([modulePath], args);
|
args = execArgv.concat([modulePath], args);
|
||||||
|
|
||||||
if (typeof options.stdio === 'string') {
|
if (typeof options.stdio === 'string') {
|
||||||
options.stdio = stdioStringToArray(options.stdio);
|
options.stdio = stdioStringToArray(options.stdio, 'ipc');
|
||||||
} else if (!Array.isArray(options.stdio)) {
|
} 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 ? stdioStringToArray('pipe') :
|
options.stdio = stdioStringToArray(
|
||||||
stdioStringToArray('inherit');
|
options.silent ? 'pipe' : 'inherit',
|
||||||
|
'ipc');
|
||||||
} else if (!options.stdio.includes('ipc')) {
|
} else if (!options.stdio.includes('ipc')) {
|
||||||
throw new ERR_CHILD_PROCESS_IPC_REQUIRED('options.stdio');
|
throw new ERR_CHILD_PROCESS_IPC_REQUIRED('options.stdio');
|
||||||
}
|
}
|
||||||
|
@ -214,6 +214,21 @@ const handleConversion = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function stdioStringToArray(stdio, channel) {
|
||||||
|
const options = [];
|
||||||
|
|
||||||
|
switch (stdio) {
|
||||||
|
case 'ignore':
|
||||||
|
case 'pipe': options.push(stdio, stdio, stdio); break;
|
||||||
|
case 'inherit': options.push(0, 1, 2); break;
|
||||||
|
default:
|
||||||
|
throw new ERR_INVALID_OPT_VALUE('stdio', stdio);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channel) options.push(channel);
|
||||||
|
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
function ChildProcess() {
|
function ChildProcess() {
|
||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
@ -892,13 +907,7 @@ function getValidStdio(stdio, sync) {
|
|||||||
|
|
||||||
// Replace shortcut with an array
|
// Replace shortcut with an array
|
||||||
if (typeof stdio === 'string') {
|
if (typeof stdio === 'string') {
|
||||||
switch (stdio) {
|
stdio = stdioStringToArray(stdio);
|
||||||
case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
|
|
||||||
case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
|
|
||||||
case 'inherit': stdio = [0, 1, 2]; break;
|
|
||||||
default:
|
|
||||||
throw new ERR_INVALID_OPT_VALUE('stdio', stdio);
|
|
||||||
}
|
|
||||||
} else if (!Array.isArray(stdio)) {
|
} else if (!Array.isArray(stdio)) {
|
||||||
throw new ERR_INVALID_OPT_VALUE('stdio', inspect(stdio));
|
throw new ERR_INVALID_OPT_VALUE('stdio', inspect(stdio));
|
||||||
}
|
}
|
||||||
@ -1042,5 +1051,6 @@ module.exports = {
|
|||||||
ChildProcess,
|
ChildProcess,
|
||||||
setupChannel,
|
setupChannel,
|
||||||
getValidStdio,
|
getValidStdio,
|
||||||
|
stdioStringToArray,
|
||||||
spawnSync
|
spawnSync
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user