Repository navigation
perf(desktop): serve over in-process memory channel instead of TCP loopback - #35272
Conversation
…opback The desktop runtime previously served the user's Deno.serve app on a random TCP loopback port and navigated the embedded webview to http://127.0.0.1:PORT. Replace that with an in-process byte channel: no port allocation, no localhost exposure, no kernel networking. ext/net — new "memory" transport: - memory.rs: MemoryStream/MemoryListener over tokio DuplexStream, a process global name->sender registry, listen_memory/connect_memory/is_listening. - Wired through the network_stream! macro (raw.rs), MemoryStreamResource (io.rs), op_net_listen_memory (ops.rs/lib.rs), request_properties, and listen({ transport: "memory" }) in 01_net.js. ext/http — DENO_SERVE_ADDRESS "memory:<name>" override (kind 5) in parse_serve_address + 00_serve.ts; Deno.serve binds the in-memory listener. cli/rt_desktop — removes the TCP path entirely (no allocate_random_port, no serve_port/host). scheme_bridge.rs registers a laufey app:// scheme handler that, per browser request, opens a connection to the in-memory listener, runs a hyper HTTP/1.1 client over it, and streams the response back to the webview. Navigates to app:// and waits on is_listening for readiness. Requires laufey API v26 (register_scheme_handler) — depended on via a local path while that laufey change is unreleased.
The previous commit ran a non-pinned `deno fmt` that reformatted the entire files. Restore them to upstream and re-apply only the ~39 lines of memory transport changes (case 5 / wantsMemory in 00_serve.ts; memory listen case + op import in 01_net.js).
divybot
left a comment
There was a problem hiding this comment.
Nice direction — replacing the random loopback port with an in-process channel is the right move for deno desktop. A few concerns to flag before this lands; most are about the publicly exposed memory transport rather than the desktop wiring itself.
Concerns
1. op_net_listen_memory skips permissions
ext/net/ops.rs:op_net_listen_memory is the only op_net_listen_* other than tunnel that performs no PermissionsContainer::check_net* call. tunnel gets a pass because it's runtime-singleton, opt-in, and dispatched by the host. Memory listen is freely callable from any user code via Deno.listen({ transport: "memory", name: "anything" }) with no --allow-net (or anything else).
Concretely, in the desktop scenario, an untrusted script in the embedded app can:
- pre-register
name: "deno-desktop"before the runtime publishesDENO_SERVE_ADDRESS, then drop it after the env is read but beforeDeno.servebinds — racy but a way to interfere; or - register any name and observe
connect_memorytraffic if a future caller bridges to it.
At minimum this needs a permission gate (and probably a documented stance — e.g., gated by --allow-net or a new --allow-memory). Worth coordinating with the security folks since this is a brand-new transport surface.
2. No JS-side type declarations
The memory transport is added to op_net_listen_memory, 01_net.js, and the Deno.serve path, but cli/tsc/dts/lib.deno.ns.d.ts and lib.deno_net.d.ts aren't updated. So Deno.listen({ transport: "memory", name: "x" }) is a type error for users. If this is meant to be internal-only (embedder-only), please say so explicitly — and either hide it behind a non-public flag or document the constraint. If it's user-facing, please add the overloads + MemoryAddr + MemoryListenOptions and an @experimental JSDoc tag.
3. Asymmetric API surface
Deno.listen({ transport: "memory" }) is reachable from JS but there's no matching Deno.connect({ transport: "memory" }) — only the Rust connect_memory. If the only intended client is the embedder (the laufey bridge), that's defensible but it should be documented; otherwise it's strange that you can serve over a memory channel that no Deno script can connect to.
4. Request body is buffered in scheme_bridge.rs
scheme_bridge.rs:332 reads the whole webview request body into a Vec before constructing the hyper request, then wraps it in Full::new(...). The response side is properly streamed via body.frame().await. For large form posts / file uploads from the embedded UI this is a real memory hit and asymmetric with how the response is handled. The block comment says "fully buffered by the backend, so these pulls are non-blocking copies" — that's an argument for read efficiency, not for why we shouldn't stream into the hyper client. Consider wiring read_body into a BodyExt stream so the upload path is also constant-memory.
5. Header forwarding
scheme_bridge.rs:bridge:
- Forwards
Hostfrom the webview (will beHost: desktop). Apps that vhost or build self-URLs fromHostwill misbehave. Probably want to rewrite to a stable value. - Doesn't filter hop-by-hop headers (
Connection,Keep-Alive,Transfer-Encoding,Upgrade,Proxy-*,TE,Trailer). They shouldn't leak through a proxy hop. value.to_str()silently drops any header whose value isn't ASCII (e.g., binary-set cookies). Acceptable in practice forapp://, but please at leastlog::debug!the drop.
6. Test coverage is thin
The four memory.rs unit tests cover only the raw registry API. There's nothing for:
Deno.serve({ transport: "memory" })end-to-end through00_serve.ts,- the
request_propertiespaths added forMemory(host, scheme, port=None), - the scheme bridge (no Rust test of
bridge()against anhttpbin-ish in-process server), - backpressure when a slow consumer fills
DUPLEX_BUF_SIZE(64 KiB), - name reuse across worker realms in the same process.
For ~580 added lines including a new transport variant, an integration test that does Deno.serve({ transport: "memory", name }) → connect_memory → roundtrip a request/response would catch a lot.
7. Smaller items
MemoryAddr.idis asymmetric: server side gets the listener's monotonic counter (1, 2, …), client side fromconnect_memoryalways gets0. Both peers exposing different IDs for the same connection is a minor footgun. Either unify (passnext_idthrough thempscpayload so both sides see the same number) or document the asymmetry.registry().lock().unwrap()will panic on poison and cascade into every future memory op in the process. A poison-tolerantlock()(e.g.,.unwrap_or_else(|e| e.into_inner())) is friendlier.memory.rsdoc-comment doesn't mention the registry is process-global (so two workers/realms share namespace and race onAddrInUse). Worth a sentence.ext/http/lib.rs:parse_serve_address: empty-name onmemory:returns(0, …)which silently falls through to "invalid format" handling — the error log just says "empty memory name", but the caller can't distinguish. Probably fine, just noting.- Nit: in
lib.rscomment around theis_listeningcheck, "(without creating a spurious connection the wayconnect_memorywould)" —connect_memorydoesn't create a spurious connection; it creates a real client/server pair that would then need to be drained. The point is fine; the wording is slightly misleading.
Out of scope but worth confirming
The PR is correctly marked draft and the body acknowledges the laufey = { path = "../../../wef/capi" } and LAUFEY_API_VERSION == 26 bump need to wait for an upstream release. The split it suggests (land ext/net + ext/http memory transport independently of the desktop wiring) is the right call — it would let the transport bake with proper tests + permission story before the desktop side flips over.
Looks good
- The lifecycle is solid: name freed on
Drop, dedup atlisten_memory,acceptreportsConnectionAbortedon closed listener. - Adding
break;to thecase 4: // Tunnelarm in00_serve.tswhile insertingcase 5:correctly fixes a pre-existing latent fall-through that was harmless only because Tunnel was the last case. - Removing the TCP loopback handshake-and-poll loop in favor of
is_listeningis a clear win — no spurious connection, no HTTP parse of an arbitrary user route. - Using
DuplexStreamover a global mpsc keeps the hot path zero-syscall and avoids the kernel-network round trip entirely. Good fit forapp://.
The in-process `memory:` channel backs the desktop runtime's
`DENO_SERVE_ADDRESS=memory:<name>` serve path; it is not a general
public transport. Remove it from the user-facing API surface:
- Drop the `case "memory"` from `Deno.listen` and expose an internal
`listenMemory(name)` helper to `ext:deno_http/00_serve` instead.
- Drive the serve path off a module-private symbol set only by the
`DENO_SERVE_ADDRESS` env handler, not a public `Deno.serve({ memory })`
option (the symbol can't be forged by callers).
`Deno.listen({ transport: "memory" })` now rejects like any unknown
transport, and `Deno.serve({ memory })` is no longer special-cased,
while the desktop env-driven path is unchanged.
The previous commit reformatted 00_serve.ts/01_net.js with a stray dprint version, reindenting the IIFE bodies. Restore the canonical formatting and keep only the memory-transport API changes.
Co-Authored-By: Divy Srivastava <me@littledivy.com>
Co-Authored-By: Divy Srivastava <me@littledivy.com>
|
Follow-up update from divybot issue denoland/divybot#592:
This session can push to the fork branch but cannot edit the upstream PR body ( Closes #35444 |
Co-Authored-By: Divy Srivastava <me@littledivy.com>
…nsport `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>
…instead of TCP loopback (#35272)\" (#35670)" This reverses the release-branch revert so the in-memory transport is back in place. The next commit adds the WebSocket loopback proxy that was the missing piece: the reason #35670 was needed at all was that user code doing `new WebSocket("ws://" + window.location.host)` couldn't reach `Deno.serve` when it was bound only to the memory channel. Reintroducing the transport in isolation would reproduce that regression — the follow-up commit is what makes it viable. Refs #35576 Co-Authored-By: Divy Srivastava <me@littledivy.com>
…nsport `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>
Reconcile the stable-serve-port fix with main after #35272 (in-process memory channel) was reverted by #35670, which restored TCP loopback and brought back the ephemeral-port storage-partition problem this PR fixes. Only conflict was the test-module `use super::` list: keep both the stable-port helpers (this branch) and `should_show_native_error_dialog` plus its three tests (main). The port allocation call site now feeds the stable port into the restored TCP-loopback `DENO_SERVE_ADDRESS` setup. Co-Authored-By: Divy Srivastava <me@littledivy.com>
Summary
deno desktopcurrently serves the user'sDeno.serveapp on a random TCP loopback port and navigates the embedded webview tohttp://127.0.0.1:PORT. This replaces that with an in-process byte channel: no port allocation, nolocalhostexposure, no kernel networking.Companion laufey PR (adds the
app://scheme handler the webview uses): littledivy/laufey#13.How it works
DENO_SERVE_ADDRESS=memory:deno-desktop, soDeno.servebinds an in-memory listener (tokioDuplexStream) instead of a socket.app://custom scheme handler. Each browser request is bridged incli/rt_desktop/scheme_bridge.rs: open a connection to the in-memory listener, run a hyper HTTP/1.1 client over it (the server side is hyper too, soDeno.serveis unchanged), and stream the response back to the webview.app://once the listener is ready.Changes
ext/net— newmemorytransportmemory.rs:MemoryStream/MemoryListeneroverDuplexStream, a process-global name→sender registry,listen_memory/connect_memory/is_listening.network_stream!macro (raw.rs),MemoryStreamResource(io.rs),op_net_listen_memory,request_properties, andlisten({ transport: "memory" }).ext/http—DENO_SERVE_ADDRESS=memory:<name>override (kind 5) inparse_serve_address+00_serve.ts.cli/rt_desktop— removes the TCP path entirely (noallocate_random_port,serve_port/serve_host=None);scheme_bridge.rs+app://navigation.Verification
deno_net/deno_http/deno_runtime/denort_desktopcompile; 4memoryunit tests pass (roundtrip, dup-name, missing-listener, name-freed-on-drop).Draft / blocker
Depends on laufey API v26 (
register_scheme_handler), currently unreleased.cli/rt_desktop/Cargo.tomlpointslaufeyat a local path; this must become a publishedlaufeyversion bump before merge (which is why this is a draft — CI can't resolve the path dep).The
ext/net+ext/httpmemory transport is self-contained and could land independently of the desktop wiring if preferred.