From cca97d1e7847026d70ce5d048e39a00a442b4308 Mon Sep 17 00:00:00 2001 From: Clark Jacobsohn Date: Tue, 11 May 2021 17:59:07 -0400 Subject: [PATCH] Fix: Only create `@babel/node` IPC channel when needed (#13295) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Huáng Jùnliàng --- packages/babel-node/src/babel-node.js | 10 +++++-- packages/babel-node/test/fixtures.js | 22 ++++++++++++--- .../in-files/payload.js | 2 +- .../fixtures/cli/subprocess-ipc/options.json | 6 +++++ .../cli/subprocess-no-ipc/in-files/payload.js | 3 +++ .../options.json | 2 +- .../babel-node/test/fixtures/misc/child.js | 1 - packages/babel-node/test/index.js | 27 ------------------- 8 files changed, 38 insertions(+), 35 deletions(-) rename packages/babel-node/test/fixtures/cli/{subprocess-send => subprocess-ipc}/in-files/payload.js (54%) create mode 100644 packages/babel-node/test/fixtures/cli/subprocess-ipc/options.json create mode 100644 packages/babel-node/test/fixtures/cli/subprocess-no-ipc/in-files/payload.js rename packages/babel-node/test/fixtures/cli/{subprocess-send => subprocess-no-ipc}/options.json (52%) delete mode 100644 packages/babel-node/test/fixtures/misc/child.js delete mode 100644 packages/babel-node/test/index.js diff --git a/packages/babel-node/src/babel-node.js b/packages/babel-node/src/babel-node.js index a9c9c74c63d6..8ef4607f58f2 100755 --- a/packages/babel-node/src/babel-node.js +++ b/packages/babel-node/src/babel-node.js @@ -84,8 +84,12 @@ getV8Flags(async function (err, v8Flags) { throw err; } + // passthrough IPC only if babel-node itself has an IPC channel + const shouldPassthroughIPC = process.send !== undefined; const proc = child_process.spawn(process.argv[0], args, { - stdio: ["inherit", "inherit", "inherit", "ipc"], + stdio: shouldPassthroughIPC + ? ["inherit", "inherit", "inherit", "ipc"] + : "inherit", }); proc.on("exit", function (code, signal) { process.on("exit", function () { @@ -96,7 +100,9 @@ getV8Flags(async function (err, v8Flags) { } }); }); - proc.on("message", message => process.send && process.send(message)); + if (shouldPassthroughIPC) { + proc.on("message", message => process.send(message)); + } process.on("SIGINT", () => proc.kill("SIGINT")); } }); diff --git a/packages/babel-node/test/fixtures.js b/packages/babel-node/test/fixtures.js index 463d48dc4adb..6c01363714b0 100644 --- a/packages/babel-node/test/fixtures.js +++ b/packages/babel-node/test/fixtures.js @@ -44,7 +44,7 @@ const saveInFiles = function (files) { }); }; -const assertTest = function (stdout, stderr, opts) { +const assertTest = function (stdout, stderr, ipcMessage, opts) { const expectStderr = opts.stderr.trim(); stderr = stderr.trim(); @@ -72,6 +72,10 @@ const assertTest = function (stdout, stderr, opts) { throw new Error("stdout:\n" + stdout); } + if (opts.ipc) { + expect(ipcMessage).toEqual(opts.ipcMessage); + } + if (opts.outFiles) { const actualFiles = readDir(path.join(tmpLoc)); @@ -101,10 +105,16 @@ const buildTest = function (testName, opts) { args.push("--config-file", "../config.json"); args = args.concat(opts.args); - const spawn = child.spawn(process.execPath, args); + const spawnOpts = {}; + if (opts.ipc) { + spawnOpts.stdio = ["pipe", "pipe", "pipe", "ipc"]; + } + + const spawn = child.spawn(process.execPath, args, spawnOpts); let stderr = ""; let stdout = ""; + let ipcMessage; spawn.stderr.on("data", function (chunk) { stderr += chunk; @@ -114,11 +124,17 @@ const buildTest = function (testName, opts) { stdout += chunk; }); + if (opts.ipc) { + spawn.on("message", function (message) { + ipcMessage = message; + }); + } + spawn.on("close", function () { let err; try { - assertTest(stdout, stderr, opts); + assertTest(stdout, stderr, ipcMessage, opts); } catch (e) { err = e; } diff --git a/packages/babel-node/test/fixtures/cli/subprocess-send/in-files/payload.js b/packages/babel-node/test/fixtures/cli/subprocess-ipc/in-files/payload.js similarity index 54% rename from packages/babel-node/test/fixtures/cli/subprocess-send/in-files/payload.js rename to packages/babel-node/test/fixtures/cli/subprocess-ipc/in-files/payload.js index 7791321ed51b..7e05f90ca7f3 100644 --- a/packages/babel-node/test/fixtures/cli/subprocess-send/in-files/payload.js +++ b/packages/babel-node/test/fixtures/cli/subprocess-ipc/in-files/payload.js @@ -1,2 +1,2 @@ process.send({ hello: "world" }); -console.log("sent"); +console.log("ipc enabled"); diff --git a/packages/babel-node/test/fixtures/cli/subprocess-ipc/options.json b/packages/babel-node/test/fixtures/cli/subprocess-ipc/options.json new file mode 100644 index 000000000000..8ddcc3dd1c39 --- /dev/null +++ b/packages/babel-node/test/fixtures/cli/subprocess-ipc/options.json @@ -0,0 +1,6 @@ +{ + "args": ["payload.js"], + "ipcMessage": { "hello": "world" }, + "ipc": true, + "stdout": "ipc enabled" +} diff --git a/packages/babel-node/test/fixtures/cli/subprocess-no-ipc/in-files/payload.js b/packages/babel-node/test/fixtures/cli/subprocess-no-ipc/in-files/payload.js new file mode 100644 index 000000000000..f1e068082bab --- /dev/null +++ b/packages/babel-node/test/fixtures/cli/subprocess-no-ipc/in-files/payload.js @@ -0,0 +1,3 @@ +if (process.send == null) { + console.log("ipc disabled"); +} diff --git a/packages/babel-node/test/fixtures/cli/subprocess-send/options.json b/packages/babel-node/test/fixtures/cli/subprocess-no-ipc/options.json similarity index 52% rename from packages/babel-node/test/fixtures/cli/subprocess-send/options.json rename to packages/babel-node/test/fixtures/cli/subprocess-no-ipc/options.json index 0b9df29c588d..2385473c6921 100644 --- a/packages/babel-node/test/fixtures/cli/subprocess-send/options.json +++ b/packages/babel-node/test/fixtures/cli/subprocess-no-ipc/options.json @@ -1,4 +1,4 @@ { "args": ["payload.js"], - "stdout": "sent" + "stdout": "ipc disabled" } diff --git a/packages/babel-node/test/fixtures/misc/child.js b/packages/babel-node/test/fixtures/misc/child.js deleted file mode 100644 index 664224a50993..000000000000 --- a/packages/babel-node/test/fixtures/misc/child.js +++ /dev/null @@ -1 +0,0 @@ -process.send("hello"); diff --git a/packages/babel-node/test/index.js b/packages/babel-node/test/index.js deleted file mode 100644 index 00742f0d003c..000000000000 --- a/packages/babel-node/test/index.js +++ /dev/null @@ -1,27 +0,0 @@ -import { spawn } from "child_process"; -import path from "path"; -import { fileURLToPath } from "url"; - -const dirname = path.dirname(fileURLToPath(import.meta.url)); -const binLoc = path.join(dirname, "../bin/babel-node.js"); -const childLoc = path.join(dirname, "fixtures/misc/child.js"); - -describe("babel-node", () => { - it("ipc works with spawned babel-node process", done => { - expect.assertions(1); - - const child = spawn(process.execPath, [binLoc, childLoc], { - stdio: ["inherit", "inherit", "inherit", "ipc"], - }); - - child.on("message", msg => { - expect(msg).toBe("hello"); - done(); - }); - child.on("error", error => { - console.error(error); - done(); - }); - child.on("exit", () => done()); - }, /* timeout */ 20_000); -});