Skip to content

Bun.serve types disallow routes and websocket being specified together #17871

Closed
@mayfieldiv

Description

@mayfieldiv

I assume this is unintentional because:

  1. Bun seems to serve the routes and websocket just fine when they're both defined (as well as the fetch for the upgrade and other non-websocket routes)
  2. the static property that routes is replacing works fine alongside websocket

Example of what I'm trying to do:

import index from "./index.html";
import { Elysia } from "elysia";

const app = new Elysia(); // ...

Bun.serve({
  routes: { "/": index }, // ERROR in typescript
  // static: { "/": index }, // typescript is happy with this (though it's deprecated)
  fetch: (req, server) => {
    if (!server.upgrade(req)) {
      return app.handle(req);
    }
  },
  websocket: {
    message: (ws, message) => { /* ... */ },
  },
});

Relevant Bun code:

function serve<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }>(
options: (
| (Omit<ServeOptions, "fetch"> & {
routes: R;
fetch?: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (Omit<ServeOptions, "routes"> & {
routes?: never;
fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| WebSocketServeOptions<T>
) & {
/**
* @deprecated Use `routes` instead in new code. This will continue to work for awhile though.
*/
static?: R;
},
): Server;

reload<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }>(
options: (
| (Omit<ServeOptions, "fetch"> & {
routes: R;
fetch?: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| (Omit<ServeOptions, "routes"> & {
routes?: never;
fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>;
})
| WebSocketServeOptions<T>
) & {
/**
* @deprecated Use `routes` instead in new code. This will continue to work for awhile though.
*/
static?: R;
},
): Server;

Metadata

Metadata

Assignees

Labels

bun:serveBun.serve and HTTP servertypesAn issue with TypeScript types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions