Skip to content

Commit b380c0f

Browse files
zero1fivetargos
authored andcommitted
child_process: refactor stdioStringToArray function
reduce the function in both files to one. PR-URL: #27657 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent d2dad0b commit b380c0f

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

lib/child_process.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const {
3737
ERR_CHILD_PROCESS_IPC_REQUIRED,
3838
ERR_CHILD_PROCESS_STDIO_MAXBUFFER,
3939
ERR_INVALID_ARG_TYPE,
40-
ERR_INVALID_OPT_VALUE,
4140
ERR_OUT_OF_RANGE
4241
} = require('internal/errors').codes;
4342
const { clearTimeout, setTimeout } = require('timers');
@@ -46,24 +45,14 @@ const child_process = require('internal/child_process');
4645
const {
4746
getValidStdio,
4847
setupChannel,
49-
ChildProcess
48+
ChildProcess,
49+
stdioStringToArray
5050
} = child_process;
5151

5252
const MAX_BUFFER = 1024 * 1024;
5353

5454
exports.ChildProcess = ChildProcess;
5555

56-
function stdioStringToArray(option) {
57-
switch (option) {
58-
case 'ignore':
59-
case 'pipe':
60-
case 'inherit':
61-
return [option, option, option, 'ipc'];
62-
default:
63-
throw new ERR_INVALID_OPT_VALUE('stdio', option);
64-
}
65-
}
66-
6756
exports.fork = function fork(modulePath /* , args, options */) {
6857
validateString(modulePath, 'modulePath');
6958

@@ -104,12 +93,13 @@ exports.fork = function fork(modulePath /* , args, options */) {
10493
args = execArgv.concat([modulePath], args);
10594

10695
if (typeof options.stdio === 'string') {
107-
options.stdio = stdioStringToArray(options.stdio);
96+
options.stdio = stdioStringToArray(options.stdio, 'ipc');
10897
} else if (!Array.isArray(options.stdio)) {
10998
// Use a separate fd=3 for the IPC channel. Inherit stdin, stdout,
11099
// and stderr from the parent if silent isn't set.
111-
options.stdio = options.silent ? stdioStringToArray('pipe') :
112-
stdioStringToArray('inherit');
100+
options.stdio = stdioStringToArray(
101+
options.silent ? 'pipe' : 'inherit',
102+
'ipc');
113103
} else if (!options.stdio.includes('ipc')) {
114104
throw new ERR_CHILD_PROCESS_IPC_REQUIRED('options.stdio');
115105
}

lib/internal/child_process.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ const handleConversion = {
214214
}
215215
};
216216

217+
function stdioStringToArray(stdio, channel) {
218+
const options = [];
219+
220+
switch (stdio) {
221+
case 'ignore':
222+
case 'pipe': options.push(stdio, stdio, stdio); break;
223+
case 'inherit': options.push(0, 1, 2); break;
224+
default:
225+
throw new ERR_INVALID_OPT_VALUE('stdio', stdio);
226+
}
227+
228+
if (channel) options.push(channel);
229+
230+
return options;
231+
}
217232

218233
function ChildProcess() {
219234
EventEmitter.call(this);
@@ -892,13 +907,7 @@ function getValidStdio(stdio, sync) {
892907

893908
// Replace shortcut with an array
894909
if (typeof stdio === 'string') {
895-
switch (stdio) {
896-
case 'ignore': stdio = ['ignore', 'ignore', 'ignore']; break;
897-
case 'pipe': stdio = ['pipe', 'pipe', 'pipe']; break;
898-
case 'inherit': stdio = [0, 1, 2]; break;
899-
default:
900-
throw new ERR_INVALID_OPT_VALUE('stdio', stdio);
901-
}
910+
stdio = stdioStringToArray(stdio);
902911
} else if (!Array.isArray(stdio)) {
903912
throw new ERR_INVALID_OPT_VALUE('stdio', inspect(stdio));
904913
}
@@ -1042,5 +1051,6 @@ module.exports = {
10421051
ChildProcess,
10431052
setupChannel,
10441053
getValidStdio,
1054+
stdioStringToArray,
10451055
spawnSync
10461056
};

0 commit comments

Comments
 (0)