mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
child_process: revise argument processing
execFile() and fork() have complicated argument processing. Clarify code and avoid using `arguments`. PR-URL: https://github.com/nodejs/node/pull/41280 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
1 parent
198528426d
commit
ed41fd110d
1 file changed
+39
-37
+39
-37
@@ -111,28 +111,27 @@ const MAX_BUFFER = 1024 * 1024;
|
||||
* }} [options]
|
||||
* @returns {ChildProcess}
|
||||
*/
|
||||
function fork(modulePath /* , args, options */) {
|
||||
function fork(modulePath, args = [], options) {
|
||||
modulePath = getValidatedPath(modulePath, 'modulePath');
|
||||
|
||||
// Get options and args arguments.
|
||||
let execArgv;
|
||||
let options = {};
|
||||
let args = [];
|
||||
let pos = 1;
|
||||
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
|
||||
args = arguments[pos++];
|
||||
|
||||
if (args == null) {
|
||||
args = [];
|
||||
} else if (typeof args !== 'object') {
|
||||
throw new ERR_INVALID_ARG_VALUE('args', args);
|
||||
} else if (!ArrayIsArray(args)) {
|
||||
options = args;
|
||||
args = [];
|
||||
}
|
||||
|
||||
if (pos < arguments.length && arguments[pos] == null) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
if (pos < arguments.length && arguments[pos] != null) {
|
||||
if (typeof arguments[pos] !== 'object') {
|
||||
throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]);
|
||||
}
|
||||
|
||||
options = { ...arguments[pos++] };
|
||||
if (options == null) {
|
||||
options = {};
|
||||
} else if (typeof options !== 'object') {
|
||||
throw new ERR_INVALID_ARG_VALUE('options', options);
|
||||
} else {
|
||||
options = { ...options };
|
||||
}
|
||||
|
||||
// Prepare arguments for fork:
|
||||
@@ -276,31 +275,34 @@ ObjectDefineProperty(exec, promisify.custom, {
|
||||
* ) => any} [callback]
|
||||
* @returns {ChildProcess}
|
||||
*/
|
||||
function execFile(file /* , args, options, callback */) {
|
||||
let args = [];
|
||||
let callback;
|
||||
let options;
|
||||
|
||||
// Parse the optional positional parameters.
|
||||
let pos = 1;
|
||||
if (pos < arguments.length && ArrayIsArray(arguments[pos])) {
|
||||
args = arguments[pos++];
|
||||
} else if (pos < arguments.length && arguments[pos] == null) {
|
||||
pos++;
|
||||
function execFile(file, args = [], options, callback) {
|
||||
if (args == null) {
|
||||
args = [];
|
||||
} else if (typeof args === 'object') {
|
||||
if (!ArrayIsArray(args)) {
|
||||
callback = options;
|
||||
options = args;
|
||||
args = [];
|
||||
}
|
||||
} else if (typeof args === 'function') {
|
||||
callback = args;
|
||||
options = {};
|
||||
args = [];
|
||||
} else {
|
||||
throw new ERR_INVALID_ARG_VALUE('args', args);
|
||||
}
|
||||
|
||||
if (pos < arguments.length && typeof arguments[pos] === 'object') {
|
||||
options = arguments[pos++];
|
||||
} else if (pos < arguments.length && arguments[pos] == null) {
|
||||
pos++;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
} else if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
} else if (typeof options !== 'object') {
|
||||
throw new ERR_INVALID_ARG_VALUE('options', options);
|
||||
}
|
||||
|
||||
if (pos < arguments.length && typeof arguments[pos] === 'function') {
|
||||
callback = arguments[pos++];
|
||||
}
|
||||
|
||||
if (!callback && pos < arguments.length && arguments[pos] != null) {
|
||||
throw new ERR_INVALID_ARG_VALUE('args', arguments[pos]);
|
||||
if (callback && typeof callback !== 'function') {
|
||||
throw new ERR_INVALID_ARG_VALUE('callback', callback);
|
||||
}
|
||||
|
||||
options = {
|
||||
|
||||
Reference in new issue
Block a user