Skip to content

Commit 3bee595

Browse files
authored
Avoid installing a global uncaughtException handler on import (#112)
# Pull Request Details ## Related Issue Addresses #77. Importing `https-localhost` currently installs a process-wide `uncaughtException` handler even when the package is only used as a module. That changes application-level error handling and can hide the original stack-trace behavior. This change scopes the existing handler to the CLI path (`require.main === module`) so the CLI keeps its friendly `EACCES` / `EADDRINUSE` messages while library consumers no longer get a global process listener as a side effect of `require("https-localhost")`. A regression test re-imports the module after clearing the require cache and verifies that the `uncaughtException` listener count does not change. ## Types of changes - [ ] Docs change / refactoring / dependency upgrade - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist - [x] My code follows the code style of this project. - [x] I have updated the documentation or my changes dont require it. - [x] I have read the CONTRIBUTING document. - [x] I have added tests to cover my changes. - [ ] All new and existing tests passed.
1 parent 8afb669 commit 3bee595

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,27 @@ if (require.main === module) {
9595
app.serve(process.argv.length === 3 ? process.argv[2] : process.cwd())
9696
// redirect http to https (only if https port is the default one)
9797
if (!process.env.PORT) app.redirect()
98-
}
9998

100-
/* istanbul ignore next: cannot be tested */
101-
process.on("uncaughtException", function(err) {
102-
switch (err.errno) {
103-
case "EACCES":
104-
console.error(
105-
"EACCES: run as administrator to use the default ports 443 and 80. " +
106-
"You can also change port with: `PORT=4433 serve ~/myproj`.")
107-
break
108-
case "EADDRINUSE":
109-
console.error("EADDRINUSE: another service on your machine is using " +
110-
"the current port.\nStop it or change port with:" +
111-
"`PORT=4433 serve ~/myproj`.")
112-
break
113-
default:
114-
console.error("Unexpected error " + err.errno + ":\n\n" + err)
115-
break
116-
}
117-
process.exit(1)
118-
})
99+
/* istanbul ignore next: cannot be tested */
100+
process.on("uncaughtException", function(err) {
101+
switch (err.errno) {
102+
case "EACCES":
103+
console.error(
104+
"EACCES: run as administrator to use the default ports 443 and 80. " +
105+
"You can also change port with: `PORT=4433 serve ~/myproj`.")
106+
break
107+
case "EADDRINUSE":
108+
console.error("EADDRINUSE: another service on your machine is using " +
109+
"the current port.\nStop it or change port with:" +
110+
"`PORT=4433 serve ~/myproj`.")
111+
break
112+
default:
113+
console.error("Unexpected error " + err.errno + ":\n\n" + err)
114+
break
115+
}
116+
process.exit(1)
117+
})
118+
}
119119

120120
// export as module
121121
module.exports = createServer

test/import.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require("assert")
2+
3+
describe("Testing module import", () => {
4+
it("doesn't install an uncaughtException handler when imported", () => {
5+
const modulePath = require.resolve("../index.js")
6+
const listenersBefore = process.listenerCount("uncaughtException")
7+
8+
delete require.cache[modulePath]
9+
require(modulePath)
10+
11+
assert.strictEqual(
12+
process.listenerCount("uncaughtException"),
13+
listenersBefore
14+
)
15+
})
16+
})

0 commit comments

Comments
 (0)