deno
5fd6780d - fix(desktop): route WebSocket over TCP loopback proxy into memory transport

Commit
141 days ago
fix(desktop): route WebSocket over TCP loopback proxy into memory transport `deno desktop` (#35272) serves `Deno.serve` on an in-process memory channel and navigates the webview to `app://`. That kept native HTTP working through a laufey scheme handler bridge but broke every desktop app whose frontend does `new WebSocket("ws://" + window.location.host)` (#35576). Two things conspired: 1. `window.location.host` under `app://desktop/` is `desktop`. The URL becomes `ws://desktop` and the browser fails DNS with `ERR_NAME_NOT_RESOLVED` — modern webviews route `ws://` through their own network stack, not through custom scheme handlers. 2. Even if the browser did hand `ws://desktop` to the scheme handler, `laufey::SchemeExchange` is HTTP one-shot (`read_body` → `begin` → `write` → `finish`) with no upgrade/duplex primitive, so a WebSocket handshake could never complete over it. This preserves the memory-transport perf win from #35272 while restoring WebSocket support without a laufey API change or a full revert: - `scheme_bridge::spawn_ws_loopback_proxy` binds `127.0.0.1:0` (kernel picks the port). Each accepted TCP connection is peeked for a `Upgrade: websocket` header; anything else is rejected with 400 and closed, so the loopback is not a second entry point for plain HTTP. Matching handshakes open a `connect_memory("deno-desktop")` client, replay the buffered head, and shuttle bytes in both directions. - The webview navigates to `app://127.0.0.1:<PORT>/` instead of a cosmetic authority. `window.location.host` is now the loopback address, so user code that concatenates it into a `ws://` URL lands on the proxy and reaches `Deno.serve` unchanged. HTTP still flows through the `app://` scheme handler → memory transport path exactly as before; the proxy exists only for WebSocket. Response streaming (#35590) is unaffected because that path is unchanged and still needs the same scheme-bridge streaming work. New tests in `scheme_bridge.rs`: - `app_url_format` — URL encodes the loopback port - `find_end_of_head_handles_split` — request-head boundary detection - `recognises_websocket_upgrade` — accepts mixed-case + multi-token `Upgrade` values - `rejects_non_upgrade_requests` — plain HTTP + `Upgrade: h2c` bounce Refs #35576 Refs #35590 Closes denoland/divybot#745 Co-Authored-By: Divy Srivastava <me@littledivy.com>
Author
Committer
Parents
Loading