fix: support WebSocket upgrades in dev server (#3823)

Vite's Connect-based dev server dispatches WebSocket upgrades as
'upgrade' events on the underlying http.Server rather than routing them
through middleware, so `Deno.upgradeWebSocket()` was never reached and
the client hung in CONNECTING. The plugin now attaches an upgrade
listener that hands the raw node socket off to Fresh's existing
`ctx.upgrade()` path.

The plumbing leans on denoland/deno#33342, which taught
`Deno.upgradeWebSocket` to accept a `{ socket, head }` option for
requests that didn't come in through `Deno.serve`. A
`WeakMap<Request, { socket, head }>` kept on `globalThis` acts as the
side channel: the plugin's upgrade listener populates it,
`ctx.upgrade()` reads and clears it before calling Deno. The map lives
on `globalThis` because Vite's SSR runner evaluates `context.ts`
separately from Deno's own load of the plugin, so each side would
otherwise hold its own module-local instance.

Fixes #3350
This commit is contained in:
Bartek Iwańczuk
2026-05-27 07:51:19 +02:00
committed by GitHub
parent acd04e13f8
commit 1fcf2d9ad5
5 changed files with 138 additions and 2 deletions
+70 -1
View File
@@ -1,7 +1,7 @@
import type { DevEnvironment, Plugin } from "vite";
import * as path from "@std/path";
import { contentType as getStdContentType } from "@std/media-types/content-type";
import { ASSET_CACHE_BUST_KEY } from "fresh/internal";
import { ASSET_CACHE_BUST_KEY, upgradeSourceMap } from "fresh/internal";
import { createRequest, sendResponse } from "@remix-run/node-fetch-server";
import { hashCode } from "../shared.ts";
import type { ResolvedFreshViteConfig } from "../utils.ts";
@@ -28,6 +28,75 @@ export function devServer(freshConfig: ResolvedFreshViteConfig): Plugin[] {
`^(${base})?/(@(vite|fs|id)|\\.vite)/`,
);
// WebSocket upgrade requests don't flow through Connect middleware —
// node:http dispatches them as `upgrade` events on the http.Server.
// Vite's own HMR server identifies its connections via the
// `sec-websocket-protocol` header (`vite-hmr`/`vite-ping`), so we
// skip those and let Vite handle them. Everything else we route into
// the Fresh handler with the raw socket + head stashed in
// `upgradeSourceMap` so `ctx.upgrade()` can finish the handshake via
// `Deno.upgradeWebSocket(req, { socket, head })`.
server.httpServer?.on("upgrade", (nodeReq, nodeSocket, head) => {
const protocolHeader = nodeReq.headers["sec-websocket-protocol"];
if (
typeof protocolHeader === "string" &&
/\b(vite-hmr|vite-ping)\b/.test(protocolHeader)
) {
return;
}
(async () => {
try {
const serverCfg = server.config.server;
const protocol = serverCfg.https ? "https" : "http";
const host = nodeReq.headers.host ?? "localhost";
const url = new URL(
nodeReq.url ?? "/",
`${protocol}://${host}`,
);
const headers = new Headers();
for (const [key, value] of Object.entries(nodeReq.headers)) {
if (value === undefined || value === null) continue;
if (Array.isArray(value)) {
for (const v of value) {
if (typeof v === "string") headers.append(key, v);
}
} else if (typeof value === "string") {
headers.set(key, value);
}
}
const req = new Request(url, {
method: nodeReq.method ?? "GET",
headers,
});
upgradeSourceMap.set(req, { socket: nodeSocket, head });
const mod = await server.ssrLoadModule("fresh:server_entry");
mod.setErrorInterceptor((err: unknown) => {
if (err instanceof Error) server.ssrFixStacktrace(err);
});
await mod.default.fetch(req);
// If the handler called ctx.upgrade(), the 101 has already been
// written to nodeSocket and the WebSocket is live. If it didn't,
// we have no usable channel left to report an error on — close
// the socket so the client doesn't hang.
if (!nodeSocket.destroyed && upgradeSourceMap.has(req)) {
nodeSocket.destroy();
}
upgradeSourceMap.delete(req);
} catch (err) {
if (err instanceof Error) server.ssrFixStacktrace(err);
// deno-lint-ignore no-console
console.error("[fresh] WebSocket upgrade failed:", err);
if (!nodeSocket.destroyed) nodeSocket.destroy();
}
})();
});
server.middlewares.use(async (nodeReq, nodeRes, next) => {
const serverCfg = server.config.server;