Skip to content

Commit

Permalink
child_process: adds normalization for fork function arguments
Browse files Browse the repository at this point in the history
Closes: nodejs#20749
  • Loading branch information
shobhitchittora committed Aug 20, 2018
1 parent 4e7d829 commit a0d6b15
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,38 @@ function stdioStringToArray(option) {
}
}

function normalizeForkArguments(args, options) {
if (Array.isArray(args)) {
args = args.slice(0);
} else if (options !== undefined &&
(args === undefined ||
args === null ||
typeof args !== 'object')
) {
throw new ERR_INVALID_ARG_TYPE('args', 'array', args);
} else {
options = args;
args = [];
}

if (options === undefined)
options = {};
else if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

return {
args,
options
};
}

exports.fork = function fork(modulePath /* , args, options */) {

// Get options and args arguments.
var execArgv;
var options = {};
var args = [];
var pos = 1;
if (pos < arguments.length && Array.isArray(arguments[pos])) {
args = arguments[pos++];
}

if (pos < arguments.length && arguments[pos] != null) {
if (typeof arguments[pos] !== 'object') {
throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]);
}

options = util._extend({}, arguments[pos++]);
}
var opts = normalizeForkArguments(arguments[1], arguments[2]);
var options = opts.options;
var args = opts.args;

// Prepare arguments for fork:
execArgv = options.execArgv || process.execArgv;
Expand Down

0 comments on commit a0d6b15

Please sign in to comment.