(port)
| 185 | // ─── HTTPS + WebSocket server ───────────────────────────────────────────────── |
| 186 | |
| 187 | async function createServer(port) { |
| 188 | const tls = getDevCert(); |
| 189 | let server; |
| 190 | |
| 191 | if (tls) { |
| 192 | server = https.createServer(tls, handleRequest); |
| 193 | } else { |
| 194 | log("warn", "No TLS certificate — falling back to HTTP"); |
| 195 | log("warn", "Install openssl to enable HTTPS for your dev server"); |
| 196 | const http = require("node:http"); |
| 197 | server = http.createServer(handleRequest); |
| 198 | } |
| 199 | |
| 200 | const wss = new WebSocketServer({ server }); |
| 201 | |
| 202 | wss.on("connection", (ws) => { |
| 203 | log("info", "App connected via WebSocket"); |
| 204 | ws.on("error", () => {}); |
| 205 | }); |
| 206 | |
| 207 | return { |
| 208 | server, |
| 209 | wss, |
| 210 | broadcast: (msg) => broadcast(wss, msg), |
| 211 | isHttps: !!tls, |
| 212 | protocol: tls ? "https" : "http", |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | function handleRequest(req, res) { |
| 217 | let urlPath = req.url.split("?")[0]; |
no test coverage detected