summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Hagemeister <marvin@deno.com>2024-05-20 15:24:13 +0200
committerGitHub <noreply@github.com>2024-05-20 15:24:13 +0200
commitfb3f82b9eafef7da732c0af7e8a86c3b87075e3c (patch)
treecffcb5edd9e5c8b3c7128a685ab26c5cf30af371
parentd7709daaa09ebd40b5756a2ce6d3e1c0c3bf7984 (diff)
fix(node): track `SIG*` listeners in `process.listeners` (#23890)
Some npm libraries like `signal-exit` rely on the length of the listener array returned by `process.listeners("SIGNT")` to be correct to function. We weren't tracking `SIG*` events there, which broke those npm libraries. Fixes https://github.com/denoland/deno/issues/22892
-rw-r--r--ext/node/polyfills/process.ts3
-rw-r--r--tests/unit_node/process_test.ts16
2 files changed, 19 insertions, 0 deletions
diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts
index 9c73f5a51..f742e6634 100644
--- a/ext/node/polyfills/process.ts
+++ b/ext/node/polyfills/process.ts
@@ -469,6 +469,7 @@ Process.prototype.on = function (
} else if (event === "SIGTERM" && Deno.build.os === "windows") {
// Ignores SIGTERM on windows.
} else {
+ EventEmitter.prototype.on.call(this, event, listener);
Deno.addSignalListener(event as Deno.Signal, listener);
}
} else {
@@ -494,6 +495,7 @@ Process.prototype.off = function (
} else if (event === "SIGTERM" && Deno.build.os === "windows") {
// Ignores SIGTERM on windows.
} else {
+ EventEmitter.prototype.off.call(this, event, listener);
Deno.removeSignalListener(event as Deno.Signal, listener);
}
} else {
@@ -537,6 +539,7 @@ Process.prototype.prependListener = function (
if (event === "SIGBREAK" && Deno.build.os !== "windows") {
// Ignores SIGBREAK if the platform is not windows.
} else {
+ EventEmitter.prototype.prependListener.call(this, event, listener);
Deno.addSignalListener(event as Deno.Signal, listener);
}
} else {
diff --git a/tests/unit_node/process_test.ts b/tests/unit_node/process_test.ts
index 0eadb0a16..8f56c92a0 100644
--- a/tests/unit_node/process_test.ts
+++ b/tests/unit_node/process_test.ts
@@ -1108,3 +1108,19 @@ Deno.test({
process.constructor.call({});
},
});
+
+// Test for https://github.com/denoland/deno/issues/22892
+Deno.test("process.listeners - include SIG* events", () => {
+ const listener = () => console.log("SIGINT");
+ process.on("SIGINT", listener);
+ assertEquals(process.listeners("SIGINT").length, 1);
+
+ const listener2 = () => console.log("SIGINT");
+ process.prependListener("SIGINT", listener2);
+ assertEquals(process.listeners("SIGINT").length, 2);
+
+ process.off("SIGINT", listener);
+ assertEquals(process.listeners("SIGINT").length, 1);
+ process.off("SIGINT", listener2);
+ assertEquals(process.listeners("SIGINT").length, 0);
+});