Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Only create @babel/node IPC channel when needed #13295

Merged
merged 3 commits into from May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/babel-node/src/babel-node.js
Expand Up @@ -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 () {
Expand All @@ -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"));
}
});
22 changes: 19 additions & 3 deletions packages/babel-node/test/fixtures.js
Expand Up @@ -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();

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
@@ -1,2 +1,2 @@
process.send({ hello: "world" });
console.log("sent");
console.log("ipc enabled");
@@ -0,0 +1,6 @@
{
"args": ["payload.js"],
"ipcMessage": { "hello": "world" },
"ipc": true,
"stdout": "ipc enabled"
}
@@ -0,0 +1,3 @@
if (process.send == null) {
console.log("ipc disabled");
}
@@ -1,4 +1,4 @@
{
"args": ["payload.js"],
"stdout": "sent"
"stdout": "ipc disabled"
}
1 change: 0 additions & 1 deletion packages/babel-node/test/fixtures/misc/child.js

This file was deleted.

27 changes: 0 additions & 27 deletions packages/babel-node/test/index.js

This file was deleted.