diff --git a/docs/guides/websocket/upgrade.md b/docs/guides/websocket/upgrade.md deleted file mode 100644 index cec8c38025..0000000000 --- a/docs/guides/websocket/upgrade.md +++ /dev/null @@ -1,28 +0,0 @@ ---- -name: Upgrade an HTTP request to a WebSocket connection ---- - -Inside `fetch`, use the `server.upgrade()` function to upgrade an incoming `Request` to a WebSocket connection. Bun automatically returns a 101 Switching Protocols response if the upgrade succeeds. - -Refer to the [WebSocket docs](/docs/api/websockets) for more information on building WebSocket servers. - -```ts -const server = Bun.serve<{ authToken: string }>({ - fetch(req, server) { - const success = server.upgrade(req); - if (success) { - // Bun automatically returns a 101 Switching Protocols - // if the upgrade succeeds - return undefined; - } - - // handle HTTP request normally - return new Response("Hello world!"); - }, - websocket: { - // define websocket handlers - }, -}); - -console.log(`Listening on localhost:\${server.port}`); -``` diff --git a/src/bun.js/api/BunObject.zig b/src/bun.js/api/BunObject.zig index f6a74a318c..d89cfb53ab 100644 --- a/src/bun.js/api/BunObject.zig +++ b/src/bun.js/api/BunObject.zig @@ -3022,6 +3022,10 @@ pub fn serve( return .undefined; } + if (globalObject.hasException()) { + return .zero; + } + break :brk config_; }; diff --git a/test/js/bun/http/bun-server.test.ts b/test/js/bun/http/bun-server.test.ts index 8e673224fc..dc1aa60ecd 100644 --- a/test/js/bun/http/bun-server.test.ts +++ b/test/js/bun/http/bun-server.test.ts @@ -432,3 +432,49 @@ test("Bun.serve().unref() works", async () => { test("unref keeps process alive for ongoing connections", async () => { expect([path.join(import.meta.dir, "unref-fixture-2.ts")]).toRun(); }); + +test("Bun does not crash when given invalid config", async () => { + const server1 = Bun.serve({ + fetch(request, server) { + // + throw new Error("Should not be called"); + }, + port: 0, + }); + + const cases = [ + { + fetch() {}, + port: server1.port, + websocket: {}, + }, + { + port: server1.port, + get websocket() { + throw new Error(); + }, + }, + { + fetch() {}, + port: server1.port, + get websocket() { + throw new Error(); + }, + }, + { + fetch() {}, + port: server1.port, + get ssl() { + throw new Error(); + }, + }, + ]; + + for (const options of cases) { + expect(() => { + Bun.serve(options as any); + }).toThrow(); + } + + server1.stop(); +});