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:
@@ -27,6 +27,30 @@ integrationTest("vite dev - launches", async () => {
|
||||
expect(text).toContain("it works");
|
||||
});
|
||||
|
||||
integrationTest("vite dev - upgrades WebSocket connections", async () => {
|
||||
const address = demoServer.address();
|
||||
const wsUrl = address.replace(/^http/, "ws") + "/tests/ws_echo";
|
||||
|
||||
const ws = new WebSocket(wsUrl);
|
||||
const reply = await new Promise<string>((resolve, reject) => {
|
||||
const timer = setTimeout(
|
||||
() => reject(new Error("timed out waiting for echo")),
|
||||
5000,
|
||||
);
|
||||
ws.onopen = () => ws.send("hello");
|
||||
ws.onmessage = (e) => {
|
||||
clearTimeout(timer);
|
||||
resolve(typeof e.data === "string" ? e.data : "");
|
||||
};
|
||||
ws.onerror = () => {
|
||||
clearTimeout(timer);
|
||||
reject(new Error("websocket errored"));
|
||||
};
|
||||
});
|
||||
ws.close();
|
||||
expect(reply).toBe("echo: hello");
|
||||
});
|
||||
|
||||
integrationTest("vite dev - serves static files", async () => {
|
||||
const res = await fetch(`${demoServer.address()}/test_static/foo.txt`);
|
||||
const text = await res.text();
|
||||
|
||||
Reference in New Issue
Block a user