From ef6f98c2e342bf0ea26ac9f1374bb018519dc12d Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 19 Dec 2021 23:40:53 +0100 Subject: [PATCH] child_process: add support for URL to `cp.fork` PR-URL: https://github.com/nodejs/node/pull/41225 Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: James M Snell --- doc/api/child_process.md | 7 ++++++- lib/child_process.js | 4 ++-- test/parallel/test-child-process-fork-url.mjs | 11 +++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-child-process-fork-url.mjs diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 360ece1095ee2a..c457ac8aa34c8e 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -391,6 +391,11 @@ controller.abort(); -* `modulePath` {string} The module to run in the child. +* `modulePath` {string|URL} The module to run in the child. * `args` {string\[]} List of string arguments. * `options` {Object} * `cwd` {string|URL} Current working directory of the child process. diff --git a/lib/child_process.js b/lib/child_process.js index 62c552d567eaad..829e5fe5b9ff66 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -91,7 +91,7 @@ const MAX_BUFFER = 1024 * 1024; /** * Spawns a new Node.js process + fork. - * @param {string} modulePath + * @param {string|URL} modulePath * @param {string[]} [args] * @param {{ * cwd?: string; @@ -112,7 +112,7 @@ const MAX_BUFFER = 1024 * 1024; * @returns {ChildProcess} */ function fork(modulePath /* , args, options */) { - validateString(modulePath, 'modulePath'); + modulePath = getValidatedPath(modulePath, 'modulePath'); // Get options and args arguments. let execArgv; diff --git a/test/parallel/test-child-process-fork-url.mjs b/test/parallel/test-child-process-fork-url.mjs new file mode 100644 index 00000000000000..9261b875638e91 --- /dev/null +++ b/test/parallel/test-child-process-fork-url.mjs @@ -0,0 +1,11 @@ +import { mustCall } from '../common/index.mjs'; +import { fork } from 'child_process'; + +if (process.argv[2] === 'child') { + process.disconnect(); +} else { + const child = fork(new URL(import.meta.url), ['child']); + + child.on('disconnect', mustCall()); + child.once('exit', mustCall()); +}