
Bun runtime for Vercel Functions now accepts Bun.serve as an entrypoint
The Bun runtime for Vercel Functions now supports Bun.serve() as a function entrypoint, including WebSocket handlers. The server you run locally with Bun deploys as-is, without being wrapped in a framework.
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
The Bun runtime for Vercel Functions now supports Bun.serve() as a function entrypoint, including WebSocket handlers. The server you run locally with Bun deploys as-is, without being wrapped in a framework.
Enable the runtime by setting "bunVersion": "1.x" in vercel.json.
Copy link to headingDeploy a routes-based server
Create a server with a routes map in server.ts at the project root.
1Bun.serve({
2 routes: {
3 "/api/boolean": () => Response.json({ success: true }),
4 "/api/users/:id": (request) => Response.json({ user: request.params.id }),
5 "/*": () => new Response("api catch-all"),
6 },
7});
A Bun.serve server deployed to Vercel with static, dynamic, and wildcard routes
Copy link to headingAccept WebSocket connections
Add a websocket handler and call server.upgrade(request) in fetch to upgrade matching requests. The rest of the server stays the same.
1Bun.serve({
2 routes: {
3 "/health": Response.json({ status: "ok" }),
4 },
5 fetch(request, server) {
6 const { pathname } = new URL(request.url);
7 if (pathname === "/ws" && server.upgrade(request)) {
8 return;
9 }
10 return new Response("Not found", { status: 404 });
11 },
12 websocket: {
13 message(socket, message) {
14 socket.send(message);
15 },
16 },
17});
A websocket handler echoes messages back, and fetch upgrades requests to /ws and falls back to a 404
WebSocket connections run on Fluid compute with Active CPU pricing, so you pay only for time spent processing messages, not idle connection time. A connection is pinned to one function instance for its lifetime, and a single instance can handle multiple concurrent connections. Use an external data store to coordinate messages across instances.
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。