Skip to content

Commit

Permalink
fix(web): Fix server error handling
Browse files Browse the repository at this point in the history
Exit process if server.listen fails or an error is thrown when trying to
prepare next app.
  • Loading branch information
munshkr committed Mar 28, 2023
1 parent 9a53e62 commit 3e8c0b3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 68 deletions.
123 changes: 63 additions & 60 deletions packages/web/lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Server {
return this.secure ? "https" : "http";
}

start(cb) {
async start(cb) {
if (this.started) return this;

const nextApp = next({
Expand All @@ -94,76 +94,79 @@ class Server {
console.log("[pubsub] Remove client", uuid);
}

nextApp.prepare().then(() => {
const app = express();
await nextApp.prepare();
const app = express();

const wss = new WebSocket.Server({ noServer: true });
const docWss = new WebSocket.Server({ noServer: true });
const pubsubWss = new WebSocket.Server({ noServer: true });
const server = createServer(app, this.secure);
const wss = new WebSocket.Server({ noServer: true });
const docWss = new WebSocket.Server({ noServer: true });
const pubsubWss = new WebSocket.Server({ noServer: true });
const server = createServer(app, this.secure);

server.on("upgrade", (request, socket, head) => {
const { pathname } = url.parse(request.url);
server.on("upgrade", (request, socket, head) => {
const { pathname } = url.parse(request.url);

if (pathname.startsWith("/signal")) {
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit("connection", ws);
});
} else if (pathname.startsWith("/doc")) {
docWss.handleUpgrade(request, socket, head, (ws) => {
docWss.emit("connection", ws);
})
} else if (pathname.startsWith("/pubsub")) {
pubsubWss.handleUpgrade(request, socket, head, (ws) => {
pubsubWss.emit("connection", ws);
});
} else if (pathname.startsWith("/_next/webpack-hmr")) {
nextApp.hotReloader?.onHMR(req, socket, head);
} else {
console.warn("[server] Ignoring request to path:", pathname);
socket.destroy();
}
});
if (pathname.startsWith("/signal")) {
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit("connection", ws);
});
} else if (pathname.startsWith("/doc")) {
docWss.handleUpgrade(request, socket, head, (ws) => {
docWss.emit("connection", ws);
})
} else if (pathname.startsWith("/pubsub")) {
pubsubWss.handleUpgrade(request, socket, head, (ws) => {
pubsubWss.emit("connection", ws);
});
} else if (pathname.startsWith("/_next/webpack-hmr")) {
nextApp.hotReloader?.onHMR(req, socket, head);
} else {
console.warn("[server] Ignoring request to path:", pathname);
socket.destroy();
}
});

wss.on("connection", (conn) => this.onSignalingServerConnection(conn));
docWss.on("connection", setupWSConnection);
wss.on("connection", (conn) => this.onSignalingServerConnection(conn));
docWss.on("connection", setupWSConnection);

// Prepare PubSub WebScoket server (pubsub)
const pubSubServer = new PubSub({
wss: pubsubWss,
onConnection: addClient,
onDisconnection: removeClient,
});
// eslint-disable-next-line no-param-reassign
app.pubsub = pubSubServer;
// Prepare PubSub WebScoket server (pubsub)
const pubSubServer = new PubSub({
wss: pubsubWss,
onConnection: addClient,
onDisconnection: removeClient,
});
// eslint-disable-next-line no-param-reassign
app.pubsub = pubSubServer;

if (process.env.REDIRECT_HTTPS) {
console.log("> Going to redirect http to https");
const sslRedirect = require("./sslRedirect");
app.use(
sslRedirect({
port: process.env.NODE_ENV === "production" ? null : this.port,
})
);
}
if (process.env.REDIRECT_HTTPS) {
console.log("> Going to redirect http to https");
const sslRedirect = require("./sslRedirect");
app.use(
sslRedirect({
port: process.env.NODE_ENV === "production" ? null : this.port,
})
);
}

if (this.staticDir) {
app.use('/static', express.static(this.staticDir))
}
if (this.staticDir) {
app.use('/static', express.static(this.staticDir))
}

// Let Next to handle everything else
app.get("*", (req, res) => {
return handle(req, res);
});
// Let Next to handle everything else
app.get("*", (req, res) => {
return handle(req, res);
});

server.listen(this.port, this.host, (err) => {
if (err) throw err;
console.log(`> Server listening on ${this.host}, port ${this.port}`);
if (cb) cb();
});

server.on('error', (err) => {
console.error(err)
process.exit(2);
});

return this;
server.listen(this.port, this.host, (err) => {
if (err) throw err;
console.log(`> Server listening on ${this.host}, port ${this.port}`);
if (cb) cb();
});
}

onSignalingServerConnection(conn) {
Expand Down
17 changes: 9 additions & 8 deletions packages/web/server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const Server = require("./lib/Server");

module.exports = { Server };

// Enable development mode if NODE_ENV != production
// eslint-disable-next-line global-require
const process = require("process");
const host = process.env.HOST;
const port = process.env.PORT;
const secure = process.env.SECURE === "1";
const isDevelopment = process.env.NODE_ENV !== "production";

const server = new Server({
host,
port,
secure,
isDevelopment,
host,
port,
secure,
isDevelopment,
});
server.start();

server.start().catch(err => {
console.log(err);
process.exit(1);
})

0 comments on commit 3e8c0b3

Please sign in to comment.