Replace setImmediate() with .on('listening')

Problem: Unfortunately `setImmediate()` doesn't actually ensure that
`server !== null`, so we can get fun race condition errors that have
been reported on Windows.

Solution: Replace `setImmediate()` hack with event emitter that actually
ensures that the server is listening (and hopefully isn't `null)`.

Fixes: https://github.com/fraction/oasis/issues/401
This commit is contained in:
Christian Bundy 2020-04-13 08:27:14 -07:00
parent e5703770ff
commit 6fe0d1c39c
1 changed files with 1 additions and 2 deletions

View File

@ -117,8 +117,7 @@ module.exports = ({ host, port, middleware, allowHost }) => {
const server = app.listen({ host, port });
// `server.address()` returns null unless you wait until the next tick.
setImmediate(() => {
server.on("listening", () => {
const address = server.address();
if (typeof address === "string") {