quicsilver-rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +31 -0
- data/LICENSE +21 -0
- data/README.md +167 -0
- data/app/javascript/quicsilver/action_cable.js +216 -0
- data/app/javascript/quicsilver/cable_boot.js +23 -0
- data/app/javascript/quicsilver/web_transport.js +120 -0
- data/config/importmap.rb +6 -0
- data/docs/action-cable.md +203 -0
- data/docs/local-certificates.md +119 -0
- data/docs/webtransport-endpoints.md +254 -0
- data/lib/generators/quicsilver/install/install_generator.rb +95 -0
- data/lib/generators/quicsilver/install/templates/Procfile.http3.dev.tt +8 -0
- data/lib/generators/quicsilver/install/templates/dev-http3.tt +15 -0
- data/lib/generators/quicsilver/install/templates/quicsilver.tt +5 -0
- data/lib/quicsilver/action_cable/cookie_bridge.rb +70 -0
- data/lib/quicsilver/action_cable/socket.rb +213 -0
- data/lib/quicsilver/action_cable.rb +83 -0
- data/lib/quicsilver/engine.rb +53 -0
- data/lib/quicsilver/middleware/alt_svc.rb +32 -0
- data/lib/quicsilver/rails/cable_credential_helper.rb +27 -0
- data/lib/quicsilver/rails/dev_web_transport_certificate.rb +155 -0
- data/lib/quicsilver/rails/meta_tags_helper.rb +47 -0
- data/lib/quicsilver/rails/port.rb +15 -0
- data/lib/quicsilver/rails/request_protocol.rb +16 -0
- data/lib/quicsilver/rails/server.rb +83 -0
- data/lib/quicsilver/rails/transport_logger.rb +62 -0
- data/lib/quicsilver/rails/version.rb +7 -0
- data/lib/quicsilver/rails/web_transport_certificate_helper.rb +20 -0
- data/lib/quicsilver/rails.rb +3 -0
- data/lib/quicsilver/routes.rb +27 -0
- data/lib/quicsilver/web_transport/channel/dispatcher.rb +76 -0
- data/lib/quicsilver/web_transport/channel.rb +69 -0
- data/lib/quicsilver/web_transport/codec.rb +43 -0
- data/lib/quicsilver/web_transport/datagram_endpoint.rb +40 -0
- data/lib/quicsilver/web_transport/endpoint.rb +137 -0
- data/lib/quicsilver/web_transport/framed_stream.rb +59 -0
- data/lib/quicsilver/web_transport/message_codec.rb +29 -0
- data/lib/quicsilver/web_transport/message_stream_endpoint.rb +54 -0
- data/lib/quicsilver/web_transport/rpc_endpoint.rb +53 -0
- data/lib/quicsilver/web_transport/server_streaming_endpoint.rb +72 -0
- data/lib/quicsilver/web_transport/stream_handler.rb +65 -0
- data/lib/quicsilver/web_transport/thread_runner.rb +18 -0
- data/lib/quicsilver/web_transport.rb +21 -0
- data/lib/quicsilver-rails.rb +17 -0
- data/lib/tasks/quicsilver.rake +16 -0
- metadata +161 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# Action Cable over WebTransport
|
|
2
|
+
|
|
3
|
+
Experimental. Moves Action Cable's transport from a TCP WebSocket onto an HTTP/3 stream, leaving
|
|
4
|
+
channels, subscriptions and the JSON protocol untouched. Turbo Streams ride along unchanged.
|
|
5
|
+
|
|
6
|
+
**Requires Rails 8.0 or newer.** CI runs Rails 8.0, 8.1 and main.
|
|
7
|
+
|
|
8
|
+
## Why WebTransport rather than WebSockets over HTTP/3
|
|
9
|
+
|
|
10
|
+
RFC 9220 defines WebSockets over HTTP/3, and Quicsilver advertises
|
|
11
|
+
`SETTINGS_ENABLE_CONNECT_PROTOCOL`, but **no shipping browser exposes it to JavaScript**. A browser
|
|
12
|
+
asked to open a `wss://` URL uses TCP no matter what `Alt-Svc` advertises. For browser realtime over
|
|
13
|
+
QUIC, WebTransport is the only path.
|
|
14
|
+
|
|
15
|
+
Server-to-server clients are a different story, and may prefer Extended CONNECT.
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
Three pieces in the app, none of them in the connection. The importmap pins for `quicsilver/*` come
|
|
20
|
+
from the engine.
|
|
21
|
+
|
|
22
|
+
### 1. Boot the transport before Turbo
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
// app/javascript/application.js
|
|
26
|
+
import "quicsilver/cable_boot" // must come first
|
|
27
|
+
import "@hotwired/turbo-rails"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Turbo inlines its own copy of Action Cable, and that copy reads the WebSocket class once, when the
|
|
31
|
+
module is evaluated. Evaluating Turbo also upgrades the `turbo-cable-stream-source` elements already
|
|
32
|
+
in the page, which opens the consumer straight away. By the time a later module could call
|
|
33
|
+
`setConsumer`, the cable is already connected over WebSocket.
|
|
34
|
+
|
|
35
|
+
`cable_boot` swaps `globalThis.WebSocket` for the WebTransport socket, lets Turbo's copy capture it,
|
|
36
|
+
and restores the native class immediately so nothing else picks it up by surprise. It only does this
|
|
37
|
+
on pages that arrived over HTTP/3; see [Which transport](#which-transport).
|
|
38
|
+
|
|
39
|
+
### 2. Render the tags
|
|
40
|
+
|
|
41
|
+
One call in the layout's `<head>`:
|
|
42
|
+
|
|
43
|
+
```erb
|
|
44
|
+
<%# layout <head> %>
|
|
45
|
+
<%= quicsilver_meta_tags %>
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
On a page served over HTTP/3 it renders:
|
|
49
|
+
|
|
50
|
+
- the development certificate hash, so the browser accepts a local CA (development only; see
|
|
51
|
+
[`local-certificates.md`](local-certificates.md));
|
|
52
|
+
- the `quicsilver-cable` marker, which tells `cable_boot` to move the cable;
|
|
53
|
+
- the cable's cookies, encrypted (see [Authentication](#authentication));
|
|
54
|
+
- a separate cable credential, if you pass `cable_credential:`.
|
|
55
|
+
|
|
56
|
+
On a page served over TCP it renders nothing. The parts are also available on their own as
|
|
57
|
+
`quicsilver_webtransport_certificate_meta_tag`, `quicsilver_cable_meta_tag`,
|
|
58
|
+
`quicsilver_cable_cookies_meta_tag` and `quicsilver_cable_credential_meta_tag(credential)`.
|
|
59
|
+
|
|
60
|
+
### 3. If your connection reads a custom cookie, name it
|
|
61
|
+
|
|
62
|
+
By default the cable carries the Rails session cookie and `session_id`, which cover Devise,
|
|
63
|
+
`session[:user_id]` and Rails' authentication generator. An app that signs in with its own cookie
|
|
64
|
+
names it instead:
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
# config/application.rb
|
|
68
|
+
config.quicsilver.cable_cookies = %i[session_token]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Authentication
|
|
72
|
+
|
|
73
|
+
Browsers send no cookies on a WebTransport CONNECT (the W3C spec fetches it with credentials mode
|
|
74
|
+
`omit`), so an unchanged `ApplicationCable::Connection` would find nobody. The page that opens the
|
|
75
|
+
cable did arrive with cookies, so the gem gives them back:
|
|
76
|
+
|
|
77
|
+
1. `quicsilver_meta_tags` encrypts the cable's cookies (`config.quicsilver.cable_cookies`, or the
|
|
78
|
+
session cookie and `session_id`) into a token, with a key derived from `secret_key_base`, and
|
|
79
|
+
renders it.
|
|
80
|
+
2. The socket sends the token in its first frame, not the URL, so it stays out of access logs:
|
|
81
|
+
`{"quicsilver_handshake":{"cookies":"…","credential":null}}`.
|
|
82
|
+
3. The adapter decrypts it and restores the request's `Cookie` header before `connect` runs. It drops
|
|
83
|
+
any cookie jar memoized earlier (a log tag that reads cookies builds one when the CONNECT is
|
|
84
|
+
logged), and never replaces cookies the request really carried.
|
|
85
|
+
|
|
86
|
+
Your connection's existing `cookies` lookup then works over both transports.
|
|
87
|
+
|
|
88
|
+
What to know:
|
|
89
|
+
|
|
90
|
+
- **Only those cookies travel**, and only the ones the page has. Set `cable_cookies = []` to send
|
|
91
|
+
none, for an app whose cable is anonymous.
|
|
92
|
+
- **The token expires after a day.** A reconnect after that connects without cookies until the next
|
|
93
|
+
page load, and logs a warning saying so. If WebTransport cables are rejected while WebSocket ones
|
|
94
|
+
aren't, check the cookie your connection reads is among those that travel; QUICSILVER_DEBUG=1 logs
|
|
95
|
+
the ones each cable restored.
|
|
96
|
+
- **It is a snapshot.** A cookie rotated after the page rendered arrives stale until the next load.
|
|
97
|
+
- **Logout revokes the cable only as well as your lookup does.** A database-backed session stops
|
|
98
|
+
working when its row is deleted. A cookie-store session keeps working until the token expires, as a
|
|
99
|
+
copied cookie would.
|
|
100
|
+
- Like any value in the page, a script running in it can read the token. It is only accepted on a
|
|
101
|
+
cable handshake.
|
|
102
|
+
|
|
103
|
+
### A separate credential
|
|
104
|
+
|
|
105
|
+
An app that wants the cable to authenticate differently from the page can pass a credential instead.
|
|
106
|
+
It travels in the same handshake frame, and the connection reads it through
|
|
107
|
+
`quicsilver_cable_credential`, which is `nil` over a WebSocket:
|
|
108
|
+
|
|
109
|
+
```erb
|
|
110
|
+
<%= quicsilver_meta_tags cable_credential: Current.session&.signed_id(purpose: :cable, expires_in: 1.day) %>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
Session.find_signed(quicsilver_cable_credential, purpose: :cable)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Which transport
|
|
118
|
+
|
|
119
|
+
The cable follows the page, with no configuration:
|
|
120
|
+
|
|
121
|
+
| Situation | Cable |
|
|
122
|
+
|---|---|
|
|
123
|
+
| Page arrived over HTTP/3 | WebTransport |
|
|
124
|
+
| Page arrived over TCP (first visit, UDP blocked, plain `bin/dev`) | WebSocket. No cable tags render, so `cable_boot` does nothing |
|
|
125
|
+
| WebTransport session fails to open | WebSocket for the rest of the page, with one console warning |
|
|
126
|
+
| WebTransport session opened, then dropped | Reconnects over WebTransport |
|
|
127
|
+
|
|
128
|
+
`request.http3?` exposes the same fact to controllers and views, for features that should degrade
|
|
129
|
+
over TCP.
|
|
130
|
+
|
|
131
|
+
## Without Turbo
|
|
132
|
+
|
|
133
|
+
An app that builds its own consumer can install the socket on its Action Cable module:
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
import * as ActionCable from "@rails/actioncable"
|
|
137
|
+
import { installWebTransportCable } from "quicsilver/action_cable"
|
|
138
|
+
|
|
139
|
+
installWebTransportCable({ ActionCable })
|
|
140
|
+
const consumer = ActionCable.createConsumer()
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
It returns `false`, and leaves the module alone, where `WebTransport` is undefined.
|
|
144
|
+
|
|
145
|
+
## How it fits together
|
|
146
|
+
|
|
147
|
+
Action Cable reads its socket through a six-method surface (`possible?`, `alive?`, `transmit`,
|
|
148
|
+
`close`, `protocol`, `rack_response`), so no Rails change is needed. The socket is built in one
|
|
149
|
+
constructor, `#initialize(server, env, coder:)`, which Rails 8.0 and 8.1 put in
|
|
150
|
+
`ActionCable::Connection::Base` and later Rails moved into `ActionCable::Server::Socket`. The engine
|
|
151
|
+
prepends into whichever this Rails has, found by asking for the class rather than by version.
|
|
152
|
+
|
|
153
|
+
| Layer | Piece |
|
|
154
|
+
|---|---|
|
|
155
|
+
| Rails admission | `Quicsilver::ActionCable::SocketSelection`, prepended by the engine |
|
|
156
|
+
| Server transport | `Quicsilver::ActionCable::Socket` |
|
|
157
|
+
| Wire framing | `Quicsilver::WebTransport::FramedStream` (4-byte big-endian length prefix) |
|
|
158
|
+
| Browser transport | `WebTransportSocket` in `quicsilver/action_cable` |
|
|
159
|
+
|
|
160
|
+
A WebTransport CONNECT to the cable path is dispatched into the Rack app with the session in
|
|
161
|
+
`env["quicsilver.webtransport"]`, so Action Cable's mounted server handles it like any other request.
|
|
162
|
+
No extra route is needed: don't also mount `webtransport "/cable"`.
|
|
163
|
+
|
|
164
|
+
WebTransport streams are byte streams, so both sides frame messages identically. A mismatch is
|
|
165
|
+
silent corruption rather than an error, which is why the framing has tests on both sides.
|
|
166
|
+
|
|
167
|
+
## Shutdown
|
|
168
|
+
|
|
169
|
+
On a graceful shutdown Quicsilver sends the session `WT_DRAIN_SESSION`, then closes it with a
|
|
170
|
+
`WT_CLOSE_SESSION` capsule. The spec resets a closed session's streams with `WT_SESSION_GONE`; the
|
|
171
|
+
socket treats that as the cable closing, not as an error.
|
|
172
|
+
|
|
173
|
+
## Verifying
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
rake test_js # framing, fallback and the WebSocket surface, under node
|
|
177
|
+
rake test # the Ruby socket adapter
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
In the Rails log, Action Cable labels every cable `[WebSocket]`, because the adapter presents
|
|
181
|
+
WebSocket's interface. The request method tells them apart:
|
|
182
|
+
|
|
183
|
+
```text
|
|
184
|
+
Started CONNECT "/cable" [WebSocket] ... (REQUEST_METHOD: CONNECT, ...) # WebTransport
|
|
185
|
+
Started GET "/cable" [WebSocket] ... (... HTTP_UPGRADE: websocket) # TCP WebSocket
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
In the browser, the network panel shows no WebSocket entry for the cable, and pages show protocol
|
|
189
|
+
`h3`. `ActionCable.logger.enabled = true` in the console prints the usual `welcome` /
|
|
190
|
+
`confirm_subscription` traffic, now arriving over QUIC.
|
|
191
|
+
|
|
192
|
+
## Known limits
|
|
193
|
+
|
|
194
|
+
- One reliable bidirectional stream per session, so Action Cable's own multiplexing still applies.
|
|
195
|
+
Head-of-line blocking is removed between the cable and other requests, not between channels.
|
|
196
|
+
- `Socket#rack_response` accepts the session before the client opens its stream. A client that
|
|
197
|
+
connects and never opens one leaves a session parked until the idle timeout.
|
|
198
|
+
- Subscription adapter latency is unchanged. With `solid_cable`, `polling_interval` still bounds how
|
|
199
|
+
fast a broadcast reaches the socket.
|
|
200
|
+
- Action Cable processes a connection's messages on a thread pool, so a `subscribe` can run before an
|
|
201
|
+
earlier `unsubscribe` of the same stream finishes, raising `AlreadySubscribedError` during Turbo
|
|
202
|
+
navigation. This is Action Cable's behaviour on any transport, but it has shown up more often over
|
|
203
|
+
WebTransport in local testing.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# Local certificates for HTTPS, HTTP/3, and WebTransport
|
|
2
|
+
|
|
3
|
+
Browser HTTP/3 and WebTransport require HTTPS. In development, the cleanest setup is one shared certificate used by both your TCP HTTPS server and Quicsilver's UDP HTTP/3 server.
|
|
4
|
+
|
|
5
|
+
This doc covers local development. In production, use a publicly trusted certificate and do not use `serverCertificateHashes`.
|
|
6
|
+
|
|
7
|
+
## The short version
|
|
8
|
+
|
|
9
|
+
Trust a local mkcert root once per machine, then let the install generator wire both servers to one
|
|
10
|
+
certificate:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
mkcert -install
|
|
14
|
+
bin/rails generate quicsilver:install
|
|
15
|
+
bin/dev-http3
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`bin/dev-http3` runs `bin/rails quicsilver:certificate` before either server starts, which writes:
|
|
19
|
+
|
|
20
|
+
```text
|
|
21
|
+
tmp/quicsilver/webtransport/localhost.pem
|
|
22
|
+
tmp/quicsilver/webtransport/localhost-key.pem
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Puma's HTTPS bind in `Procfile.http3.dev` and Quicsilver's `Server.start` both read those files, and
|
|
26
|
+
`quicsilver_meta_tags` renders the certificate's hash for WebTransport. To cover more hostnames, set
|
|
27
|
+
`QUICSILVER_DEV_HOSTS` and regenerate:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
rm -rf tmp/quicsilver/webtransport
|
|
31
|
+
QUICSILVER_DEV_HOSTS=localhost,quicsilver.test,127.0.0.1,::1 bin/rails quicsilver:certificate
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Why this helper exists
|
|
35
|
+
|
|
36
|
+
Normal HTTPS page loads and WebTransport both require a secure context, but local WebTransport is
|
|
37
|
+
pickier than a normal Rails HTTPS page.
|
|
38
|
+
|
|
39
|
+
`Quicsilver::Rails::DevWebTransportCertificate` generates a local certificate with the shape browsers
|
|
40
|
+
expect for WebTransport:
|
|
41
|
+
|
|
42
|
+
- ECDSA P-256 key
|
|
43
|
+
- valid for 13 days: WebTransport's `serverCertificateHashes` refuses anything over two weeks
|
|
44
|
+
- SANs for local development hosts, with IP addresses (IPv4 and IPv6) as IP entries
|
|
45
|
+
- signed by your local mkcert root when available
|
|
46
|
+
- self-signed when it isn't, which browsers reject; `quicsilver:certificate` warns when that happens
|
|
47
|
+
|
|
48
|
+
When signed by the mkcert root, the same certificate is trusted for normal HTTPS and HTTP/3 pages and
|
|
49
|
+
assets, while also qualifying for WebTransport's certificate hash path. It's replaced automatically a
|
|
50
|
+
day before it expires.
|
|
51
|
+
|
|
52
|
+
## Why it's generated before the servers start
|
|
53
|
+
|
|
54
|
+
Puma parses its `ssl://...?cert=` bind before Rails finishes booting, so the files must exist before
|
|
55
|
+
`bin/rails server` starts. And two processes generating it at once would each write a different
|
|
56
|
+
certificate for the same origin. So `bin/dev-http3` generates it once, then starts foreman.
|
|
57
|
+
|
|
58
|
+
Without the generator, or with another server such as Falcon, do the same: run
|
|
59
|
+
`bin/rails quicsilver:certificate`, then point the server's TLS settings at the two files above.
|
|
60
|
+
|
|
61
|
+
## Alt-Svc
|
|
62
|
+
|
|
63
|
+
The HTTPS responses advertise the UDP HTTP/3 server whenever `QUICSILVER_PORT` is set, on that port.
|
|
64
|
+
Verify it:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
curl -sI https://localhost:3443/ | grep -i alt-svc # alt-svc: h3=":3443"; ma=86400
|
|
68
|
+
curl --http3-only -sI https://localhost:3443/ # HTTP/3 200
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Chrome local HTTP/3 behavior
|
|
72
|
+
|
|
73
|
+
Chrome may not naturally promote local/private-root origins to HTTP/3 through `Alt-Svc`, even when the local root is trusted for TCP HTTPS. This is a browser policy/cache behavior, not a Rails routing issue.
|
|
74
|
+
|
|
75
|
+
For a repeatable local full-H3 browser test, launch Chrome with a fresh profile and force QUIC for the local origin:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
|
|
79
|
+
--user-data-dir=/tmp/quicsilver-chrome-h3 \
|
|
80
|
+
--origin-to-force-quic-on=quicsilver.test:3443 \
|
|
81
|
+
https://quicsilver.test:3443/
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
With that local override, Chrome DevTools should show `h3` for the document and assets, and WebTransport should connect to the same origin.
|
|
85
|
+
|
|
86
|
+
Production with a publicly trusted certificate and normal `Alt-Svc` should not need this flag.
|
|
87
|
+
|
|
88
|
+
## Production notes
|
|
89
|
+
|
|
90
|
+
Use a publicly trusted certificate, usually on the same host and numeric port for TCP and UDP:
|
|
91
|
+
|
|
92
|
+
```text
|
|
93
|
+
Puma/reverse proxy TCP HTTPS: :443
|
|
94
|
+
Quicsilver UDP HTTP/3: :443
|
|
95
|
+
Alt-Svc: h3=":443"; ma=86400
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Production reminders:
|
|
99
|
+
|
|
100
|
+
- UDP must be open end-to-end.
|
|
101
|
+
- Many L7 load balancers do not pass QUIC/UDP through by default.
|
|
102
|
+
- Do not use `serverCertificateHashes` in production.
|
|
103
|
+
- Reload Quicsilver when the TLS certificate renews.
|
|
104
|
+
|
|
105
|
+
## Troubleshooting
|
|
106
|
+
|
|
107
|
+
| Symptom | Likely cause | Fix |
|
|
108
|
+
| --- | --- | --- |
|
|
109
|
+
| TCP HTTPS works but `curl --http3` fails | H3 process is not running or UDP is blocked | Start the app with `bin/dev-http3`; check firewall/listener. |
|
|
110
|
+
| `Alt-Svc` missing | `QUICSILVER_PORT` isn't set for the HTTPS server, or `config.quicsilver.alt_svc = false` | Start with `bin/dev-http3`, which sets it. |
|
|
111
|
+
| WebTransport handshake fails locally | Missing hash meta tag, stale generated cert, or unsupported cert shape | Render `quicsilver_meta_tags` and regenerate with `rm -rf tmp/quicsilver/webtransport`. |
|
|
112
|
+
| Chrome DevTools still shows `http/1.1` locally | Chrome did not naturally promote local Alt-Svc | Use the forced local Chrome profile for full-H3 testing. |
|
|
113
|
+
| `https://localhost:3443/` refuses connection | You connected by TCP to a UDP-only listener | Navigate to the TCP HTTPS origin; Alt-Svc or forced QUIC handles H3. |
|
|
114
|
+
|
|
115
|
+
Inspect the generated cert:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
openssl x509 -in tmp/quicsilver/webtransport/localhost.pem -noout -issuer -subject -dates -text | grep -E "Issuer:|Subject:|Not Before|Not After|Public Key Algorithm"
|
|
119
|
+
```
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# WebTransport endpoints
|
|
2
|
+
|
|
3
|
+
Warning: experimental.
|
|
4
|
+
|
|
5
|
+
WebTransport is now a [Baseline newly available browser API](https://developer.mozilla.org/en-US/docs/Web/API/WebTransport), but Rails integration and deployment patterns are still early.
|
|
6
|
+
|
|
7
|
+
WebTransport support in `quicsilver-rails` is an early-adopter API for browser-native realtime streams over HTTP/3. The normal Rails request/response path still runs through your Rack app over HTTP/3; WebTransport is for long-lived sessions that need streams or datagrams.
|
|
8
|
+
|
|
9
|
+
## Mental model
|
|
10
|
+
|
|
11
|
+
WebTransport is not a controller action and not an HTTP/1.1 WebSocket upgrade. A browser opens an HTTP/3 Extended CONNECT request with `:protocol = webtransport`. After that request is accepted, the browser and server exchange data inside a WebTransport session.
|
|
12
|
+
|
|
13
|
+
In Rails, `quicsilver-rails` maps a route to an endpoint class:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
# config/routes.rb
|
|
17
|
+
Rails.application.routes.draw do
|
|
18
|
+
webtransport "/transports/echo", to: EchoTransport
|
|
19
|
+
end
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Endpoint classes usually live in `app/transports`:
|
|
23
|
+
|
|
24
|
+
```text
|
|
25
|
+
app/transports/echo_transport.rb
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The endpoint owns the WebTransport session lifecycle: accepting streams or datagrams, decoding messages, running application code inside the Rails executor, encoding responses, and closing the session.
|
|
29
|
+
|
|
30
|
+
## Basic reliable message endpoint
|
|
31
|
+
|
|
32
|
+
A reliable bidirectional stream is the simplest first endpoint. It is the closest shape to a WebSocket-like application protocol: both sides can send messages, and messages are reliable and ordered within the stream.
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
# app/transports/echo_transport.rb
|
|
36
|
+
class EchoTransport < Quicsilver::WebTransport::MessageStreamEndpoint
|
|
37
|
+
codec :json
|
|
38
|
+
|
|
39
|
+
def receive(message)
|
|
40
|
+
Rails.logger.info("[echo] #{message.inspect}")
|
|
41
|
+
{ echo: message }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Mount it:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
# config/routes.rb
|
|
50
|
+
Rails.application.routes.draw do
|
|
51
|
+
webtransport "/transports/echo", to: EchoTransport
|
|
52
|
+
end
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Connect from the browser:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { connect, openMessageStream } from "quicsilver/web_transport"
|
|
59
|
+
|
|
60
|
+
const transport = await connect("https://quicsilver.test:3443/transports/echo")
|
|
61
|
+
const stream = await openMessageStream(transport)
|
|
62
|
+
|
|
63
|
+
await stream.send({ hello: "world" })
|
|
64
|
+
|
|
65
|
+
for await (const message of stream.responses()) {
|
|
66
|
+
console.log(message)
|
|
67
|
+
break
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
transport.close()
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The JavaScript helper handles the 4-byte big-endian length prefix used by `Quicsilver::WebTransport::FramedStream`.
|
|
74
|
+
|
|
75
|
+
## Development certificate helper
|
|
76
|
+
|
|
77
|
+
For local WebTransport, render the certificate hash meta tag in your layout:
|
|
78
|
+
|
|
79
|
+
```erb
|
|
80
|
+
<%= quicsilver_webtransport_certificate_meta_tag %>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The browser helper reads this tag and passes `serverCertificateHashes` to `new WebTransport(...)` in development. Production does not render this tag and uses normal public TLS validation.
|
|
84
|
+
|
|
85
|
+
See [`local-certificates.md`](local-certificates.md) for the full local HTTPS, HTTP/3, and WebTransport certificate setup.
|
|
86
|
+
|
|
87
|
+
## Choosing an endpoint shape
|
|
88
|
+
|
|
89
|
+
| Shape | WebTransport primitive | Delivery | Use it when |
|
|
90
|
+
| --- | --- | --- | --- |
|
|
91
|
+
| `MessageStreamEndpoint` | Bidirectional stream | reliable, ordered | both sides exchange messages that must arrive in order |
|
|
92
|
+
| `RpcEndpoint` | Bidirectional stream | reliable request/response | a session command needs a structured result |
|
|
93
|
+
| `ServerStreamingEndpoint` | Unidirectional stream | reliable, ordered server output | the server emits progressive chunks |
|
|
94
|
+
| `DatagramEndpoint` | Datagram | unreliable, unordered | newer state replaces older state |
|
|
95
|
+
|
|
96
|
+
## MessageStreamEndpoint
|
|
97
|
+
|
|
98
|
+
Use `MessageStreamEndpoint` for reliable, ordered, bidirectional application messages.
|
|
99
|
+
|
|
100
|
+
Good fits:
|
|
101
|
+
|
|
102
|
+
- chat-style message exchange
|
|
103
|
+
- notification or subscription protocols
|
|
104
|
+
- collaborative editing operations where order matters
|
|
105
|
+
- WebSocket-like application protocols
|
|
106
|
+
- Action Cable-style channel traffic
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
class ChatTransport < Quicsilver::WebTransport::MessageStreamEndpoint
|
|
110
|
+
codec :json
|
|
111
|
+
|
|
112
|
+
def receive(message)
|
|
113
|
+
case message.fetch("type")
|
|
114
|
+
when "subscribe"
|
|
115
|
+
subscribe_to_room(message.fetch("room_id"))
|
|
116
|
+
{ ok: true, subscribed: message.fetch("room_id") }
|
|
117
|
+
when "message"
|
|
118
|
+
chat_message = ChatMessage.create!(
|
|
119
|
+
room_id: message.fetch("room_id"),
|
|
120
|
+
body: message.fetch("body")
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
{ ok: true, id: chat_message.id }
|
|
124
|
+
else
|
|
125
|
+
{ ok: false, error: "unknown message type" }
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Reliability is not free: delayed data on one stream can delay later data on that same stream. If the state becomes obsolete quickly, use datagrams instead.
|
|
132
|
+
|
|
133
|
+
## RpcEndpoint
|
|
134
|
+
|
|
135
|
+
Use `RpcEndpoint` when the client sends one command and expects one structured result inside an existing WebTransport session.
|
|
136
|
+
|
|
137
|
+
Good fits:
|
|
138
|
+
|
|
139
|
+
- capability negotiation
|
|
140
|
+
- opening or resuming a realtime subscription
|
|
141
|
+
- applying a collaborative operation and returning the committed revision
|
|
142
|
+
- acknowledging delivery or synchronization checkpoints
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
class ApplyOperationTransport < Quicsilver::WebTransport::RpcEndpoint
|
|
146
|
+
codec :json
|
|
147
|
+
|
|
148
|
+
def perform(operation)
|
|
149
|
+
document = Document.find(operation.fetch("document_id"))
|
|
150
|
+
revision = document.apply!(operation.fetch("patch"))
|
|
151
|
+
|
|
152
|
+
{ ok: true, revision: revision }
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Treat RPC commands like normal Rails writes: authenticate the session, authorize the operation, validate input, and use idempotency keys where retries are possible.
|
|
158
|
+
|
|
159
|
+
## ServerStreamingEndpoint
|
|
160
|
+
|
|
161
|
+
Use `ServerStreamingEndpoint` when the client starts or joins work and the server emits a sequence of reliable chunks.
|
|
162
|
+
|
|
163
|
+
Good fits:
|
|
164
|
+
|
|
165
|
+
- token-by-token AI responses
|
|
166
|
+
- import/export progress
|
|
167
|
+
- deploy/build logs
|
|
168
|
+
- tailing logs
|
|
169
|
+
- streaming search/query results
|
|
170
|
+
|
|
171
|
+
```ruby
|
|
172
|
+
class CompletionTransport < Quicsilver::WebTransport::ServerStreamingEndpoint
|
|
173
|
+
codec :json
|
|
174
|
+
|
|
175
|
+
def perform(request)
|
|
176
|
+
AiClient.stream_tokens(request.fetch("prompt")) do |token|
|
|
177
|
+
{ token: token }
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Unlike datagrams, server-streamed messages are not disposable. This shape is for output the client should receive in order.
|
|
184
|
+
|
|
185
|
+
## DatagramEndpoint
|
|
186
|
+
|
|
187
|
+
Use `DatagramEndpoint` only for unreliable, unordered messages where loss is acceptable.
|
|
188
|
+
|
|
189
|
+
Good fits:
|
|
190
|
+
|
|
191
|
+
- collaborative cursor or pointer positions
|
|
192
|
+
- game/simulation state snapshots
|
|
193
|
+
- high-frequency telemetry samples
|
|
194
|
+
- state where the next update replaces the previous one
|
|
195
|
+
|
|
196
|
+
Bad fits:
|
|
197
|
+
|
|
198
|
+
- payments or checkout operations
|
|
199
|
+
- business events
|
|
200
|
+
- chat messages
|
|
201
|
+
- audit trails
|
|
202
|
+
- any durable write that must be observed exactly once
|
|
203
|
+
|
|
204
|
+
```ruby
|
|
205
|
+
class CursorTransport < Quicsilver::WebTransport::DatagramEndpoint
|
|
206
|
+
codec :json
|
|
207
|
+
|
|
208
|
+
def receive(cursor)
|
|
209
|
+
broadcast_cursor(
|
|
210
|
+
user_id: current_user.id,
|
|
211
|
+
x: cursor.fetch("x"),
|
|
212
|
+
y: cursor.fetch("y")
|
|
213
|
+
)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Datagrams may be dropped by the network or by congestion control. Receivers must tolerate loss, duplication, and reordering.
|
|
219
|
+
|
|
220
|
+
## Authentication and authorization
|
|
221
|
+
|
|
222
|
+
A WebTransport session starts as an HTTP/3 request, so cookies and request headers are available at connection time. Do not treat that as enough for the whole session.
|
|
223
|
+
|
|
224
|
+
Recommended rules:
|
|
225
|
+
|
|
226
|
+
- authenticate before accepting or processing the session
|
|
227
|
+
- authorize every command that mutates or reads protected state
|
|
228
|
+
- validate every payload
|
|
229
|
+
- apply rate limits for long-lived sessions
|
|
230
|
+
- use idempotency keys for retryable writes
|
|
231
|
+
- do not use datagrams for durable application operations
|
|
232
|
+
|
|
233
|
+
## Low-level escape hatch
|
|
234
|
+
|
|
235
|
+
The endpoint base classes are the recommended Rails-shaped API. If you need lower-level access, Quicsilver core exposes the raw session through the Rack environment:
|
|
236
|
+
|
|
237
|
+
```ruby
|
|
238
|
+
session = Quicsilver::WebTransport.from_env(request.env)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Use this only when the endpoint base classes are too restrictive. You are then responsible for accepting the session, stream/datagram loops, Rails executor wrapping, framing, error handling, and closing.
|
|
242
|
+
|
|
243
|
+
## Testing
|
|
244
|
+
|
|
245
|
+
See the endpoint tests for executable examples:
|
|
246
|
+
|
|
247
|
+
- `test/quicsilver/web_transport/message_stream_endpoint_test.rb`
|
|
248
|
+
- `test/quicsilver/web_transport/rpc_endpoint_test.rb`
|
|
249
|
+
- `test/quicsilver/web_transport/server_streaming_endpoint_test.rb`
|
|
250
|
+
- `test/quicsilver/web_transport/datagram_endpoint_test.rb`
|
|
251
|
+
|
|
252
|
+
## Demo app
|
|
253
|
+
|
|
254
|
+
See [`quicsilver-playground`](https://github.com/hahmed/quicsilver-playground) for a complete Rails app that serves normal pages/assets over HTTP/3 and includes a browser WebTransport echo demo.
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
require "quicsilver/rails/dev_web_transport_certificate"
|
|
5
|
+
require "quicsilver/rails/port"
|
|
6
|
+
|
|
7
|
+
module Quicsilver
|
|
8
|
+
module Generators
|
|
9
|
+
# Sets an app up to serve HTTP/3 in development, with Action Cable over WebTransport:
|
|
10
|
+
#
|
|
11
|
+
# bin/rails generate quicsilver:install
|
|
12
|
+
# bin/dev-http3
|
|
13
|
+
#
|
|
14
|
+
# Every change is a file the app can read and edit. Running it again changes nothing that's
|
|
15
|
+
# already in place.
|
|
16
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
17
|
+
source_root File.expand_path("templates", __dir__)
|
|
18
|
+
|
|
19
|
+
desc "Serve the app over HTTP/3 in development, with Action Cable over WebTransport."
|
|
20
|
+
|
|
21
|
+
LAYOUT = "app/views/layouts/application.html.erb"
|
|
22
|
+
APPLICATION_JS = "app/javascript/application.js"
|
|
23
|
+
META_TAGS = "<%= quicsilver_meta_tags %>"
|
|
24
|
+
CABLE_BOOT = 'import "quicsilver/cable_boot"'
|
|
25
|
+
|
|
26
|
+
def create_server_script
|
|
27
|
+
template "quicsilver.tt", "bin/quicsilver"
|
|
28
|
+
chmod "bin/quicsilver", 0o755
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def create_dev_script
|
|
32
|
+
template "dev-http3.tt", "bin/dev-http3"
|
|
33
|
+
chmod "bin/dev-http3", 0o755
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def create_procfile
|
|
37
|
+
template "Procfile.http3.dev.tt", "Procfile.http3.dev"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def add_meta_tags_to_layout
|
|
41
|
+
layout = app_file?(LAYOUT) ? File.read(File.join(destination_root, LAYOUT)) : ""
|
|
42
|
+
|
|
43
|
+
if layout.include?(META_TAGS)
|
|
44
|
+
say_status :identical, LAYOUT
|
|
45
|
+
elsif layout.match?(%r{</head>})
|
|
46
|
+
insert_into_file LAYOUT, " #{META_TAGS}\n", before: %r{^\s*</head>}
|
|
47
|
+
elsif app_file?(LAYOUT)
|
|
48
|
+
say_status :skip, "No </head> in #{LAYOUT}: add #{META_TAGS} to the <head> #{LAYOUT} renders", :yellow
|
|
49
|
+
else
|
|
50
|
+
say_status :skip, "No #{LAYOUT}: add #{META_TAGS} to your layout's <head>", :yellow
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The import has to come before Turbo, which captures the WebSocket class as it loads.
|
|
55
|
+
def import_cable_boot
|
|
56
|
+
if app_file?(APPLICATION_JS) && app_file?("config/importmap.rb")
|
|
57
|
+
prepend_to_file APPLICATION_JS, "#{CABLE_BOOT} // must come before Turbo\n"
|
|
58
|
+
else
|
|
59
|
+
say_status :skip, "No importmap: import quicsilver/cable_boot before Turbo in your JavaScript", :yellow
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def show_next_steps
|
|
64
|
+
say <<~MESSAGE
|
|
65
|
+
|
|
66
|
+
Run bin/dev-http3, and open https://localhost:#{Quicsilver::Rails::DEFAULT_PORT}.
|
|
67
|
+
|
|
68
|
+
WebTransport cables carry the Rails session cookie and session_id. If your
|
|
69
|
+
ApplicationCable::Connection reads a different cookie, name it in config/application.rb:
|
|
70
|
+
|
|
71
|
+
config.quicsilver.cable_cookies = %i[your_cookie]
|
|
72
|
+
MESSAGE
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
def app_file?(path)
|
|
77
|
+
File.exist?(File.join(destination_root, path))
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Paths the gem generates the certificate at, so Puma's SSL bind reads the same files.
|
|
81
|
+
def certificate_path(name)
|
|
82
|
+
File.join(Quicsilver::Rails::DevWebTransportCertificate::DIRECTORY,
|
|
83
|
+
Quicsilver::Rails::DevWebTransportCertificate.const_get(name))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The app's other development processes, such as asset watchers, so they keep running.
|
|
87
|
+
def other_dev_processes
|
|
88
|
+
return [] unless app_file?("Procfile.dev")
|
|
89
|
+
|
|
90
|
+
File.readlines(File.join(destination_root, "Procfile.dev"), chomp: true)
|
|
91
|
+
.grep(/\A\s*[\w-]+:/).grep_v(/\A\s*web:/)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Puma serves HTTPS on TCP and Quicsilver serves HTTP/3 on UDP, both on QUICSILVER_PORT. Browsers reach
|
|
2
|
+
# Puma first, and its Alt-Svc header points them at HTTP/3. Started by bin/dev-http3.
|
|
3
|
+
web: bin/rails server -b "ssl://0.0.0.0:${QUICSILVER_PORT}?key=<%= certificate_path(:KEY_FILE_NAME) %>&cert=<%= certificate_path(:CERT_FILE_NAME) %>"
|
|
4
|
+
# foreman stops processes 5s after Ctrl-C, so the graceful shutdown has to fit inside that.
|
|
5
|
+
h3: QUICSILVER_SHUTDOWN_TIMEOUT=3 bin/quicsilver
|
|
6
|
+
<% other_dev_processes.each do |process| -%>
|
|
7
|
+
<%= process %>
|
|
8
|
+
<% end -%>
|