From fbff18a723d0d20a3e658aef1b213625c7ecbf9d Mon Sep 17 00:00:00 2001 From: DidaS Date: Fri, 17 Nov 2023 13:34:05 +0000 Subject: [PATCH] fix(types): Change `Bun.serve` types to allow `void` when using websockets (#7160) * `Response` -> `any` * Revert previous commit & allow void in ws fetch * Added type test to check if you can upgrade connection without returning * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- packages/bun-types/bun.d.ts | 4 ++-- packages/bun-types/tests/serve.test-d.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index a7fe3d578ebd40..d7e33961c9fd8e 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -2014,7 +2014,7 @@ declare module "bun" { this: Server, request: Request, server: Server, - ): Response | undefined | Promise; + ): Response | undefined | void | Promise; } export interface UnixWebSocketServeOptions @@ -2075,7 +2075,7 @@ declare module "bun" { this: Server, request: Request, server: Server, - ): Response | undefined | Promise; + ): Response | undefined | void | Promise; } export interface TLSWebSocketServeOptions diff --git a/packages/bun-types/tests/serve.test-d.ts b/packages/bun-types/tests/serve.test-d.ts index 86d78fd8a07de9..09796506b71a4b 100644 --- a/packages/bun-types/tests/serve.test-d.ts +++ b/packages/bun-types/tests/serve.test-d.ts @@ -79,6 +79,25 @@ Bun.serve({ }, }); +Bun.serve({ + fetch(req, server) { + server.upgrade(req); + }, + + websocket: { + open(ws) { + console.log("WebSocket opened"); + ws.subscribe("test-channel"); + }, + + message(ws, message) { + ws.publish("test-channel", `${message}`); + }, + + perMessageDeflate: true, + }, +}); + Bun.serve({ fetch(req) { throw new Error("woops!");