feat(node): new child_process.fork (#2700)

This commit is contained in:
Yoshiya Hinosawa 2022-09-30 01:31:37 +09:00 committed by GitHub
parent e0bd4346fa
commit 1bbcae8cdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 10 deletions

4
.gitignore vendored
View File

@ -1,9 +1,5 @@
.DS_Store
.idea
deno.d.ts
/node_modules
package.json
package-lock.json
.vscode/settings.json
**/cov/
/crypto/_wasm_crypto/target

View File

@ -37,20 +37,23 @@ import { getSystemErrorName, promisify } from "./util.ts";
import { createDeferredPromise } from "./internal/util.mjs";
import { process } from "./process.ts";
import { Buffer } from "./buffer.ts";
import { notImplemented } from "./_utils.ts";
import { convertToValidSignal, kEmptyObject } from "./internal/util.mjs";
const MAX_BUFFER = 1024 * 1024;
type ForkOptions = ChildProcessOptions;
/**
* Spawns a new Node.js process + fork. Not implmeneted yet.
* Spawns a new Node.js process + fork.
* @param modulePath
* @param args
* @param option
* @returns
*/
export function fork(
modulePath: string, /* args?: string[], options?: ForkOptions*/
modulePath: string,
_args?: string[],
_options?: ForkOptions,
) {
validateString(modulePath, "modulePath");
@ -109,8 +112,10 @@ export function fork(
stringifiedV8Flags.push("--v8-flags=" + v8Flags.join(","));
}
args = [
// TODO(kt3k): Find corrct args for `fork` execution
...[],
"run",
"--unstable", // TODO(kt3k): Remove when npm: is stable
"--node-modules-dir",
"-A",
...stringifiedV8Flags,
...execArgv,
modulePath,
@ -133,7 +138,13 @@ export function fork(
options.execPath = options.execPath || Deno.execPath();
options.shell = false;
notImplemented("child_process.fork");
Object.assign(options.env ??= {}, {
// deno-lint-ignore no-explicit-any
DENO_DONT_USE_INTERNAL_NODE_COMPAT_STATE: (Deno as any).core.ops
.op_npm_process_state(),
});
return spawn(options.execPath, args, options);
}
// deno-lint-ignore no-empty-interface

View File

@ -507,3 +507,28 @@ Deno.test({
await p;
},
});
Deno.test({
name: "[node/child_process] child_process.fork",
async fn() {
const testdataDir = path.join(
path.dirname(path.fromFileUrl(import.meta.url)),
"testdata",
);
const script = path.join(
testdataDir,
"node_modules",
"foo",
"index.js",
);
const p = deferred();
const cp = CP.fork(script, [], { cwd: testdataDir, stdio: "pipe" });
let output = "";
cp.on("exit", () => p.resolve());
cp.stdout?.on("data", (data) => {
output += data;
});
await p;
assertEquals(output, "foo\ntrue\ntrue\ntrue\n");
},
});

3
node/testdata/deno.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"//": "this file is necessary for child_process.fork test case"
}

4
node/testdata/node_modules/foo/index.js generated vendored Normal file
View File

@ -0,0 +1,4 @@
console.log("foo");
console.log(typeof require === "function");
console.log(typeof module === "object");
console.log(typeof exports === "object");

3
node/testdata/node_modules/foo/package.json generated vendored Normal file
View File

@ -0,0 +1,3 @@
{
"name": "foo"
}