Skip to content

feat(qmux): add server-side Session.accept() (JS)#324

Merged
kixelated merged 2 commits into
mainfrom
claude/web-transport-314-83aa2f
Jul 16, 2026
Merged

feat(qmux): add server-side Session.accept() (JS)#324
kixelated merged 2 commits into
mainfrom
claude/web-transport-314-83aa2f

Conversation

@kixelated

Copy link
Copy Markdown
Collaborator

Closes #314.

js/qmux's Session could only ever be a client. The protocol logic was already role-aware, but #isServer was never assigned and the socket was not injectable, so there was no way to build a server session from an accepted WebSocket — a JS accept server had no equivalent of the ws:// fallback moq-relay serves alongside WebTransport.

API

import Session, { selectSubprotocol } from "@moq/qmux"

Deno.serve((req) => {
	const protocol = selectSubprotocol(req.headers.get("sec-websocket-protocol"), {
		protocols: ["moq-lite-04"],
		versions: { "moq-lite-04": null },
	})
	if (!protocol) return new Response("no supported protocol", { status: 400 })

	const { socket, response } = Deno.upgradeWebSocket(req, { protocol })
	const session = Session.accept(socket)  // a WebTransport, server-side
	handle(session)
	return response
})

Session.accept mirrors the Rust crate's qmux::ws::Server::accept. new Session(url) keeps the WebTransport constructor signature, so install() is unaffected.

Changes

  • Session.accept(socket, options) takes a plain WebSocket, a WebSocketStream, or anything WebSocketStreamLike — so Deno.upgradeWebSocket, ws, and Bun.serve all work. The constructor now accepts a socket in place of a URL, which is what makes the role injectable.
  • WebSocketStream.adopt() (@moq/web-socket-stream) wraps a socket that already exists. An accepted socket is typically already OPEN, so onopen has already fired and waiting for it would hang forever; opened now settles from readyState.
  • selectSubprotocol() is the accept-side mirror of resolveSubprotocols. The host runs the handshake, so it — not the library — picks the subprotocol; this expands the same protocols + versions pair and returns the first wire form the client offered. Preference is ours, not the client's, matching qmux::ws::Server. It returns undefined on no match so the caller fails the upgrade: accepting with no subprotocol silently negotiates the legacy webtransport wire format and misreads every QMux frame.
  • AcceptOptions.protocol stands in for hosts that don't report the negotiated subprotocol on the socket (Bun is one).

Verification

New interop-client Rust example, the mirror of the existing interop-server: a real Rust client drives a JS Session.accept server across qmux-00/01/02 — 1.2 MB in each direction with flow-control replenishment, bidi streams, datagrams, and a clean close. It runs in CI via just test.

Reverting #isServer = true fails that test immediately with Invalid stream ID direction, along with four unit tests, so the coverage is anchored to the fix rather than passing incidentally. just check and just test are green.

Notes

🤖 Generated with Claude Code

js/qmux's Session could only ever be a client: the protocol logic was already
role-aware, but `#isServer` was never assigned and the socket was not injectable,
so there was no way to build a server session from an accepted WebSocket. A JS
accept server had no equivalent of the ws:// fallback moq-relay serves alongside
WebTransport.

Add `Session.accept(socket, options)`, mirroring the Rust crate's
`qmux::ws::Server::accept`. It takes a plain WebSocket, a WebSocketStream, or
anything WebSocketStreamLike, so Deno.upgradeWebSocket, `ws`, and Bun.serve all
work. `new Session(url)` keeps the WebTransport constructor signature, so
install() is unaffected.

Supporting changes:

- `WebSocketStream.adopt()` wraps a socket that already exists. An accepted
  socket is typically already OPEN, so `onopen` has already fired and waiting for
  it would hang; `opened` now settles from readyState.
- `selectSubprotocol()` is the accept-side mirror of resolveSubprotocols. The
  host runs the handshake, so it picks the subprotocol; this expands the same
  protocols+versions pair and returns the first wire form the client offered.
  Preference is ours, not the client's, matching qmux::ws::Server. Returns
  undefined on no match so the caller fails the upgrade — accepting with no
  subprotocol silently negotiates the legacy `webtransport` wire format and
  misreads every QMux frame.
- `AcceptOptions.protocol` stands in for hosts that don't report the negotiated
  subprotocol on the socket (Bun is one).

Verified by a new interop-client Rust example, the mirror of the existing
interop-server: a real Rust client drives a JS Session.accept server across
qmux-00/01/02 — 1.2 MB each way with flow-control replenishment, bidi streams,
datagrams, and a clean close. It runs in CI via `just test`, and fails with
"Invalid stream ID direction" without the role assignment.

Closes #314

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry @kixelated, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@kixelated, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 44 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 38fbaaff-19c1-435b-86a7-ac2c14f4568d

📥 Commits

Reviewing files that changed from the base of the PR and between f0c64c1 and ab531f6.

📒 Files selected for processing (10)
  • js/qmux/README.md
  • js/qmux/src/index.ts
  • js/qmux/src/session.test.ts
  • js/qmux/src/session.ts
  • js/qmux/src/subprotocol.test.ts
  • js/qmux/tests/interop.test.ts
  • js/web-socket-stream/README.md
  • js/web-socket-stream/src/index.test.ts
  • js/web-socket-stream/src/index.ts
  • rs/qmux/examples/interop-client.rs
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch claude/web-transport-314-83aa2f

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

`openWebSocketStream` normalizes the native WebSocketStream's ArrayBuffer
chunks into Uint8Array via a TransformStream, so the dial path always reaches
the decoder with the right type. A transport passed straight to the
constructor or to Session.accept never goes through it — and Chromium's
native WebSocketStream yields ArrayBuffer — so the varint decoder threw
"Expected ArrayBuffer for the first argument" and the session never opened.

Normalize in the read loop instead: one branch per message, no extra stream
hop on the hot path, and it defends every inbound transport rather than only
the one openWebSocketStream built.

Also drop the "for browsers" framing from the qmux README now that the
package serves both roles.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@kixelated
kixelated enabled auto-merge (squash) July 16, 2026 16:43
@kixelated
kixelated merged commit 2909f30 into main Jul 16, 2026
2 of 3 checks passed
@kixelated
kixelated deleted the claude/web-transport-314-83aa2f branch July 16, 2026 17:02
This was referenced Jul 16, 2026
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.

js/qmux: no server-side Session — can't accept a WebSocket (#isServer hardcoded, socket not injectable)

1 participant