From c5d7be21bd836962c88e9ab3aa4f31eca054a09e Mon Sep 17 00:00:00 2001 From: PNHD Date: Fri, 14 Aug 2026 14:43:58 +0700 Subject: [PATCH 1/2] fix: avoid installing global error handler on import --- index.js | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 9d2cf5a..ca38bb3 100755 --- a/index.js +++ b/index.js @@ -95,27 +95,27 @@ if (require.main === module) { app.serve(process.argv.length === 3 ? process.argv[2] : process.cwd()) // redirect http to https (only if https port is the default one) if (!process.env.PORT) app.redirect() -} -/* istanbul ignore next: cannot be tested */ -process.on("uncaughtException", function(err) { - switch (err.errno) { - case "EACCES": - console.error( - "EACCES: run as administrator to use the default ports 443 and 80. " + - "You can also change port with: `PORT=4433 serve ~/myproj`.") - break - case "EADDRINUSE": - console.error("EADDRINUSE: another service on your machine is using " + - "the current port.\nStop it or change port with:" + - "`PORT=4433 serve ~/myproj`.") - break - default: - console.error("Unexpected error " + err.errno + ":\n\n" + err) - break - } - process.exit(1) -}) + /* istanbul ignore next: cannot be tested */ + process.on("uncaughtException", function(err) { + switch (err.errno) { + case "EACCES": + console.error( + "EACCES: run as administrator to use the default ports 443 and 80. " + + "You can also change port with: `PORT=4433 serve ~/myproj`.") + break + case "EADDRINUSE": + console.error("EADDRINUSE: another service on your machine is using " + + "the current port.\nStop it or change port with:" + + "`PORT=4433 serve ~/myproj`.") + break + default: + console.error("Unexpected error " + err.errno + ":\n\n" + err) + break + } + process.exit(1) + }) +} // export as module module.exports = createServer From 2b799a65ef9e5da2fcd0870f7bce7e744c848d6b Mon Sep 17 00:00:00 2001 From: PNHD Date: Fri, 14 Aug 2026 14:44:19 +0700 Subject: [PATCH 2/2] test: cover import-time process side effects --- test/import.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/import.js diff --git a/test/import.js b/test/import.js new file mode 100644 index 0000000..f8d9095 --- /dev/null +++ b/test/import.js @@ -0,0 +1,16 @@ +const assert = require("assert") + +describe("Testing module import", () => { + it("doesn't install an uncaughtException handler when imported", () => { + const modulePath = require.resolve("../index.js") + const listenersBefore = process.listenerCount("uncaughtException") + + delete require.cache[modulePath] + require(modulePath) + + assert.strictEqual( + process.listenerCount("uncaughtException"), + listenersBefore + ) + }) +})