Skip to content

perf(desktop): serve over in-process memory channel instead of TCP loopback - #35272

Merged
littledivy merged 12 commits into
denoland:mainfrom
divybot:feat/desktop-inproc-transport
Jun 27, 2026
Merged

littledivy merged 12 commits into
denoland:mainfrom
divybot:feat/desktop-inproc-transport

Conversation

@littledivy

Copy link
Copy Markdown
Member

Summary

deno desktop currently serves the user's Deno.serve app on a random TCP loopback port and navigates the embedded webview to http://127.0.0.1:PORT. This replaces that with an in-process byte channel: no port allocation, no localhost exposure, no kernel networking.

Companion laufey PR (adds the app:// scheme handler the webview uses): littledivy/laufey#13.

How it works

  1. The desktop runtime sets DENO_SERVE_ADDRESS=memory:deno-desktop, so Deno.serve binds an in-memory listener (tokio DuplexStream) instead of a socket.
  2. It registers a laufey app:// custom scheme handler. Each browser request is bridged in cli/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, so Deno.serve is unchanged), and stream the response back to the webview.
  3. The webview navigates to app:// once the listener is ready.

Changes

ext/net — new memory transport

  • memory.rs: MemoryStream/MemoryListener over DuplexStream, a process-global name→sender registry, listen_memory/connect_memory/is_listening.
  • Threaded through the network_stream! macro (raw.rs), MemoryStreamResource (io.rs), op_net_listen_memory, request_properties, and listen({ transport: "memory" }).

ext/http — DENO_SERVE_ADDRESS=memory:<name> override (kind 5) in parse_serve_address + 00_serve.ts.

cli/rt_desktop — removes the TCP path entirely (no allocate_random_port, serve_port/serve_host = None); scheme_bridge.rs + app:// navigation.

Verification

  • deno_net / deno_http / deno_runtime / denort_desktop compile; 4 memory unit 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.toml points laufey at a local path; this must become a published laufey version bump before merge (which is why this is a draft — CI can't resolve the path dep).

The ext/net + ext/http memory transport is self-contained and could land independently of the desktop wiring if preferred.

…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).
@littledivy littledivy changed the title feat(desktop): serve over in-process memory channel instead of TCP loopback perf(desktop): serve over in-process memory channel instead of TCP loopback Jun 17, 2026

@divybot divybot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 publishes DENO_SERVE_ADDRESS, then drop it after the env is read but before Deno.serve binds — racy but a way to interfere; or
  • register any name and observe connect_memory traffic 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 Host from the webview (will be Host: desktop). Apps that vhost or build self-URLs from Host will 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 for app://, but please at least log::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 through 00_serve.ts,
  • the request_properties paths added for Memory (host, scheme, port=None),
  • the scheme bridge (no Rust test of bridge() against an httpbin-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.id is asymmetric: server side gets the listener's monotonic counter (1, 2, …), client side from connect_memory always gets 0. Both peers exposing different IDs for the same connection is a minor footgun. Either unify (pass next_id through the mpsc payload 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-tolerant lock() (e.g., .unwrap_or_else(|e| e.into_inner())) is friendlier.
  • memory.rs doc-comment doesn't mention the registry is process-global (so two workers/realms share namespace and race on AddrInUse). Worth a sentence.
  • ext/http/lib.rs:parse_serve_address: empty-name on memory: 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.rs comment around the is_listening check, "(without creating a spurious connection the way connect_memory would)" — connect_memory doesn'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 at listen_memory, accept reports ConnectionAborted on closed listener.
  • Adding break; to the case 4: // Tunnel arm in 00_serve.ts while inserting case 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_listening is a clear win — no spurious connection, no HTTP parse of an arbitrary user route.
  • Using DuplexStream over a global mpsc keeps the hot path zero-syscall and avoids the kernel-network round trip entirely. Good fit for app://.

@littledivy
littledivy marked this pull request as ready for review June 18, 2026 17:57
exe.dev user and others added 8 commits June 18, 2026 19:49
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>
@divybot

divybot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up update from divybot issue denoland/divybot#592:

  • Rebased/merged current origin/main into this PR branch and pushed a273ed6b05.
  • Added f385f2c1e2 to filter app:// scheme bridge hop-by-hop headers, set a stable forwarded Host: desktop, and fix the memory serve override switch fallthrough.
  • Local verification passed:
    • cargo test -p deno_net memory --lib
    • cargo test -p denort_desktop bridge_header_filtering --lib
    • cargo check -p denort_desktop
  • GitHub CI completed 136/136 check-runs with no failures; PR is mergeable and clean.

This session can push to the fork branch but cannot edit the upstream PR body (UpdatePullRequest forbidden). The intended tracking lines are:

Closes #35444
Closes denoland/divybot#592

Co-Authored-By: Divy Srivastava <me@littledivy.com>
@littledivy
littledivy merged commit 4925b0e into denoland:main Jun 27, 2026
136 checks passed
littledivy added a commit that referenced this pull request Jul 1, 2026
…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>
bartlomieju pushed a commit that referenced this pull request Jul 1, 2026
…f TCP loopback (#35272)" (#35670)

Reverts #35272 (`perf(desktop): serve over in-process memory channel
instead of TCP loopback`).
littledivy added a commit that referenced this pull request Jul 1, 2026
…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>
littledivy added a commit that referenced this pull request Jul 1, 2026
…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>
littledivy added a commit that referenced this pull request Jul 11, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants