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,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Quicsilver
|
|
4
|
+
module Rails
|
|
5
|
+
# Everything a page's <head> needs from quicsilver-rails, in one call:
|
|
6
|
+
#
|
|
7
|
+
# <%= quicsilver_meta_tags %>
|
|
8
|
+
#
|
|
9
|
+
# On a page served over HTTP/3 it renders
|
|
10
|
+
#
|
|
11
|
+
# - the development certificate hash, so the browser accepts a local CA for WebTransport;
|
|
12
|
+
# - the quicsilver-cable marker, which tells quicsilver/cable_boot to move the cable onto
|
|
13
|
+
# WebTransport;
|
|
14
|
+
# - the cable's cookies, encrypted, so #connect finds the session as it does over a WebSocket
|
|
15
|
+
# (see Quicsilver::ActionCable::CookieBridge);
|
|
16
|
+
# - a separate cable credential, for apps that pass one.
|
|
17
|
+
#
|
|
18
|
+
# On a page served over TCP it renders nothing, and the cable stays on WebSocket.
|
|
19
|
+
module MetaTagsHelper
|
|
20
|
+
CABLE_META_NAME = "quicsilver-cable"
|
|
21
|
+
COOKIES_META_NAME = "quicsilver-cable-cookies"
|
|
22
|
+
|
|
23
|
+
def quicsilver_meta_tags(cable_credential: nil)
|
|
24
|
+
safe_join [
|
|
25
|
+
quicsilver_webtransport_certificate_meta_tag,
|
|
26
|
+
quicsilver_cable_meta_tag,
|
|
27
|
+
quicsilver_cable_cookies_meta_tag,
|
|
28
|
+
quicsilver_cable_credential_meta_tag(cable_credential)
|
|
29
|
+
].compact, "\n"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Tells quicsilver/cable_boot to move the cable onto WebTransport.
|
|
33
|
+
def quicsilver_cable_meta_tag
|
|
34
|
+
if request.http3?
|
|
35
|
+
tag.meta(name: CABLE_META_NAME, content: "webtransport")
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The allowlisted cookies, encrypted, for the cable to send back in its handshake.
|
|
40
|
+
def quicsilver_cable_cookies_meta_tag
|
|
41
|
+
if request.http3? && (token = Quicsilver::ActionCable.cookie_bridge&.token_for(request.cookies))
|
|
42
|
+
tag.meta(name: COOKIES_META_NAME, content: token)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Quicsilver
|
|
4
|
+
module Rails
|
|
5
|
+
DEFAULT_PORT = 3443
|
|
6
|
+
|
|
7
|
+
# The UDP port the HTTP/3 listener binds, and so the port Alt-Svc advertises: QUICSILVER_PORT,
|
|
8
|
+
# or 3443. One answer for both, because a browser pointed at the wrong port gets no reply and
|
|
9
|
+
# stays on TCP without any error. In production that is usually 443, the UDP port networks let
|
|
10
|
+
# through.
|
|
11
|
+
def self.port
|
|
12
|
+
Integer(ENV.fetch("QUICSILVER_PORT", DEFAULT_PORT))
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Quicsilver
|
|
4
|
+
module Rails
|
|
5
|
+
# Included into ActionDispatch::Request.
|
|
6
|
+
#
|
|
7
|
+
# Whether this request arrived over HTTP/3, which is the only evidence that the client can reach
|
|
8
|
+
# the HTTP/3 listener right now. Configuration says the server offers HTTP/3; a browser on its
|
|
9
|
+
# first visit, or with UDP blocked, is still on TCP.
|
|
10
|
+
module RequestProtocol
|
|
11
|
+
def http3?
|
|
12
|
+
get_header("SERVER_PROTOCOL") == "HTTP/3"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "quicsilver"
|
|
4
|
+
require_relative "port"
|
|
5
|
+
require "quicsilver/rails/dev_web_transport_certificate"
|
|
6
|
+
require "quicsilver/rails/transport_logger"
|
|
7
|
+
|
|
8
|
+
module Quicsilver
|
|
9
|
+
module Rails
|
|
10
|
+
# Boots a Quicsilver HTTP/3 listener for a Rails app, beside the app's TCP server.
|
|
11
|
+
#
|
|
12
|
+
# #!/usr/bin/env ruby
|
|
13
|
+
# require_relative "../config/environment"
|
|
14
|
+
# Quicsilver::Rails::Server.start
|
|
15
|
+
#
|
|
16
|
+
# Port and address come from QUICSILVER_PORT / QUICSILVER_HOST. In development the
|
|
17
|
+
# certificate is the same mkcert one Puma serves on TCP, since a browser switching via
|
|
18
|
+
# Alt-Svc validates the UDP listener against the origin it already trusts.
|
|
19
|
+
module Server
|
|
20
|
+
# Transport settings for browsers, over Quicsilver's defaults. Overridden by keywords to .start.
|
|
21
|
+
TRANSPORT_DEFAULTS = {
|
|
22
|
+
# Ping an idle connection every 20s so NATs keep its UDP mapping. Quicsilver doesn't ping
|
|
23
|
+
# by default.
|
|
24
|
+
keep_alive_interval_ms: 20_000,
|
|
25
|
+
# How long a connection may go silent before it is dropped. Quicsilver's default is 30s.
|
|
26
|
+
idle_timeout_ms: 120_000,
|
|
27
|
+
# Unidirectional streams the browser may have open at once. HTTP/3 itself uses three, and
|
|
28
|
+
# WebTransport apps open more, so Quicsilver's default of 10 leaves little room.
|
|
29
|
+
max_unidirectional_streams: 100
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
def start(app: ::Rails.application, port: Quicsilver::Rails.port,
|
|
35
|
+
address: ENV.fetch("QUICSILVER_HOST", "0.0.0.0"), certificate: DevWebTransportCertificate.fetch,
|
|
36
|
+
shutdown_timeout: shutdown_timeout_from_env, **transport_options)
|
|
37
|
+
log_to_stdout if ::Rails.env.development?
|
|
38
|
+
log_transport_to_rails
|
|
39
|
+
|
|
40
|
+
configuration = Quicsilver::Transport::Configuration.new \
|
|
41
|
+
certificate.cert_file, certificate.key_file, **TRANSPORT_DEFAULTS, **transport_options
|
|
42
|
+
|
|
43
|
+
server = Quicsilver::Server.new Integer(port), address: address, app: app, server_configuration: configuration
|
|
44
|
+
server.shutdown_timeout = shutdown_timeout if shutdown_timeout
|
|
45
|
+
|
|
46
|
+
Quicsilver.logger.info "listening on UDP #{address}:#{port}"
|
|
47
|
+
server.start
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# QUICSILVER_SHUTDOWN_TIMEOUT, in seconds: how long a graceful shutdown on INT or TERM may
|
|
51
|
+
# take. Quicsilver's default is 30s; under foreman, which kills after 5s, it must be shorter.
|
|
52
|
+
def shutdown_timeout_from_env
|
|
53
|
+
value = ENV["QUICSILVER_SHUTDOWN_TIMEOUT"]
|
|
54
|
+
Float(value) if value.present?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# `rails server` is what normally tees the log to the terminal, and this process doesn't go
|
|
58
|
+
# through it. $stdout is synced because foreman reads it through a pipe, which otherwise
|
|
59
|
+
# buffers the whole session.
|
|
60
|
+
def log_to_stdout
|
|
61
|
+
$stdout.sync = true
|
|
62
|
+
|
|
63
|
+
unless ActiveSupport::Logger.logger_outputs_to?(::Rails.logger, $stdout)
|
|
64
|
+
stdout = ActiveSupport::Logger.new($stdout).tap { |logger| logger.formatter = ::Rails.logger.formatter }
|
|
65
|
+
::Rails.logger.broadcast_to(stdout)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Without this the transport writes to its own $stdout logger: absent from log/<env>.log, in a
|
|
70
|
+
# different format, and impossible to line up with the request it belongs to.
|
|
71
|
+
# QUICSILVER_DEBUG=1 turns on the transport's detail.
|
|
72
|
+
#
|
|
73
|
+
# protocol-rack, which serves the Rack app, logs through the console gem to stderr instead, so
|
|
74
|
+
# that goes through the same logger. This process is the HTTP/3 server, so it owns the console
|
|
75
|
+
# gem's output, as `rails server` owns Rails'.
|
|
76
|
+
def log_transport_to_rails
|
|
77
|
+
level = ENV["QUICSILVER_DEBUG"].present? ? Logger::DEBUG : Logger::INFO
|
|
78
|
+
Quicsilver.logger = TransportLogger.new(::Rails.logger, level: level)
|
|
79
|
+
::Console.logger = ::Console::Logger.new(TransportLogger::ConsoleOutput.new(Quicsilver.logger), level: :debug)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "console"
|
|
4
|
+
require "logger"
|
|
5
|
+
|
|
6
|
+
module Quicsilver
|
|
7
|
+
module Rails
|
|
8
|
+
# Quicsilver's transport log (handshakes, WebTransport sessions, stream resets), written through
|
|
9
|
+
# Rails.logger so it lands in log/<env>.log beside the requests it explains, in the app's format.
|
|
10
|
+
#
|
|
11
|
+
# Keeps its own level: QUICSILVER_DEBUG turns on transport detail without turning the whole
|
|
12
|
+
# app's log to DEBUG, and Rails.logger's level still applies on top.
|
|
13
|
+
class TransportLogger < ::Logger
|
|
14
|
+
TAG = "[quicsilver]"
|
|
15
|
+
|
|
16
|
+
def initialize(target, level: ::Logger::INFO)
|
|
17
|
+
super(nil, level: level)
|
|
18
|
+
@target = target
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def add(severity, message = nil, progname = nil)
|
|
22
|
+
severity ||= UNKNOWN
|
|
23
|
+
return true if severity < level
|
|
24
|
+
|
|
25
|
+
message = block_given? ? yield : (message || progname)
|
|
26
|
+
@target.add(severity, "#{TAG} #{message}")
|
|
27
|
+
end
|
|
28
|
+
alias_method :log, :add
|
|
29
|
+
|
|
30
|
+
# An output for the console gem, which protocol-rack logs through instead of Quicsilver.logger.
|
|
31
|
+
# Its error line when a Rack app raises would otherwise go to stderr, in another format and
|
|
32
|
+
# missing from log/<env>.log. Forwarded into a logger, it gets the same tag and level.
|
|
33
|
+
class ConsoleOutput
|
|
34
|
+
SEVERITIES = {
|
|
35
|
+
debug: ::Logger::DEBUG, info: ::Logger::INFO, warn: ::Logger::WARN,
|
|
36
|
+
error: ::Logger::ERROR, fatal: ::Logger::FATAL
|
|
37
|
+
}.freeze
|
|
38
|
+
|
|
39
|
+
def initialize(logger)
|
|
40
|
+
@logger = logger
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Console passes the exception, when there is one, as the last argument.
|
|
44
|
+
def call(subject = nil, *arguments, severity: :info, **, &block)
|
|
45
|
+
@logger.add(SEVERITIES.fetch(severity, ::Logger::UNKNOWN)) { format(subject, arguments, block) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
def format(subject, arguments, block)
|
|
50
|
+
arguments += [ block.call ] if block
|
|
51
|
+
exception = arguments.pop if arguments.last.is_a?(Exception)
|
|
52
|
+
text = [ label(subject), *arguments ].compact.join(" ")
|
|
53
|
+
exception ? "#{text} #{exception.class}: #{exception.message}\n#{Array(exception.backtrace).join("\n")}" : text
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def label(subject)
|
|
57
|
+
subject.is_a?(Module) ? subject.name : subject&.class&.name
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "quicsilver/rails/dev_web_transport_certificate"
|
|
4
|
+
|
|
5
|
+
module Quicsilver
|
|
6
|
+
module Rails
|
|
7
|
+
module WebTransportCertificateHelper
|
|
8
|
+
META_NAME = "quicsilver-webtransport-certificate-sha256"
|
|
9
|
+
|
|
10
|
+
def quicsilver_webtransport_certificate_meta_tag
|
|
11
|
+
# Development-only: the hash pins a local certificate. Only over HTTP/3, where WebTransport
|
|
12
|
+
# can be reached at all.
|
|
13
|
+
return unless ::Rails.env.development? && request.http3?
|
|
14
|
+
|
|
15
|
+
certificate = DevWebTransportCertificate.fetch
|
|
16
|
+
tag.meta(name: META_NAME, content: certificate.sha256)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Quicsilver
|
|
4
|
+
module Routes
|
|
5
|
+
# Mount a Rack-callable Quicsilver WebTransport endpoint.
|
|
6
|
+
#
|
|
7
|
+
# webtransport "/cursors", to: CursorTransport
|
|
8
|
+
#
|
|
9
|
+
# Endpoint classes such as Quicsilver::WebTransport::DatagramEndpoint,
|
|
10
|
+
# RpcEndpoint, and ServerStreamingEndpoint implement .call(env), so they can be mounted
|
|
11
|
+
# directly through Rails routing.
|
|
12
|
+
def webtransport(path, to:, **options)
|
|
13
|
+
match path, to: webtransport_endpoint(to), via: :all, **options
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
# A class named by string is looked up per request, not when routes are drawn, so a reload in
|
|
19
|
+
# development serves the new class rather than the one the routes captured.
|
|
20
|
+
def webtransport_endpoint(to)
|
|
21
|
+
return to if to.respond_to?(:call)
|
|
22
|
+
|
|
23
|
+
name = to.to_s.camelize
|
|
24
|
+
->(env) { name.constantize.call(env) }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "quicsilver/web_transport/framed_stream"
|
|
4
|
+
|
|
5
|
+
module Quicsilver
|
|
6
|
+
module WebTransport
|
|
7
|
+
class Channel
|
|
8
|
+
class Dispatcher
|
|
9
|
+
# The payload could not be decoded: the peer's mistake, not the app's, so it is answered but
|
|
10
|
+
# not reported.
|
|
11
|
+
class MalformedMessage < StandardError; end
|
|
12
|
+
|
|
13
|
+
def initialize(commands:, streams:, codec:, session:)
|
|
14
|
+
@commands = commands
|
|
15
|
+
@streams = streams
|
|
16
|
+
@codec = codec
|
|
17
|
+
@session = session
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def call(raw_stream)
|
|
21
|
+
stream = FramedStream.new(raw_stream)
|
|
22
|
+
message = decode(stream.read_message)
|
|
23
|
+
return stream.close unless message
|
|
24
|
+
|
|
25
|
+
if (handler = commands[message["type"].to_s])
|
|
26
|
+
stream.write_message encode(handler.call(message))
|
|
27
|
+
stream.close
|
|
28
|
+
elsif (handler = streams[message["type"].to_s])
|
|
29
|
+
handler.call(stream: stream, codec: codec, session: session, message: message)
|
|
30
|
+
else
|
|
31
|
+
stream.write_message encode(error("Unknown WebTransport message type: #{message['type']}"))
|
|
32
|
+
stream.close
|
|
33
|
+
end
|
|
34
|
+
rescue Quicsilver::Server::WebTransportStream::ResetError
|
|
35
|
+
# The peer reset the stream: an ordinary disconnect, and nobody is left to reply to.
|
|
36
|
+
nil
|
|
37
|
+
rescue MalformedMessage
|
|
38
|
+
reply_with_error(stream, "Malformed message")
|
|
39
|
+
rescue => exception
|
|
40
|
+
# The peer gets a generic reply: the exception's message can carry SQL, record data or
|
|
41
|
+
# paths. The app's error reporter gets the exception itself.
|
|
42
|
+
::Rails.error.report(exception, handled: true, source: "quicsilver.webtransport")
|
|
43
|
+
reply_with_error(stream, "Internal error")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
attr_reader :commands, :streams, :codec, :session
|
|
48
|
+
|
|
49
|
+
def decode(payload)
|
|
50
|
+
return unless payload
|
|
51
|
+
|
|
52
|
+
codec.decode(payload)
|
|
53
|
+
rescue => error
|
|
54
|
+
raise MalformedMessage, error.message
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Best effort. The stream may already be gone, and a failure here must not replace the
|
|
58
|
+
# error being answered.
|
|
59
|
+
def reply_with_error(stream, message)
|
|
60
|
+
stream&.write_message encode(error(message))
|
|
61
|
+
stream&.close
|
|
62
|
+
rescue IOError, Quicsilver::Server::WebTransportStream::ResetError
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def encode(payload)
|
|
67
|
+
codec.encode(payload)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def error(message)
|
|
71
|
+
{ type: "error", error: message }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "quicsilver/web_transport/endpoint"
|
|
4
|
+
require "quicsilver/web_transport/codec"
|
|
5
|
+
require "quicsilver/web_transport/message_codec"
|
|
6
|
+
|
|
7
|
+
module Quicsilver
|
|
8
|
+
module WebTransport
|
|
9
|
+
class Channel < Endpoint
|
|
10
|
+
include MessageCodec
|
|
11
|
+
|
|
12
|
+
# Wire format for commands, streams and datagrams.
|
|
13
|
+
#
|
|
14
|
+
# class DropChannel < Quicsilver::WebTransport::Channel
|
|
15
|
+
# codec :json
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# Defaults to :json rather than :raw because channels exchange typed
|
|
19
|
+
# messages: the dispatcher routes on message["type"], which needs a
|
|
20
|
+
# structured payload. Subclasses inherit the parent's choice.
|
|
21
|
+
setting :codec, default: :json
|
|
22
|
+
|
|
23
|
+
# Inherited like any setting. Adding to a subclass replaces its copy rather than editing the
|
|
24
|
+
# parent's, and a subclass that adds nothing keeps reading the parent's.
|
|
25
|
+
class_attribute :commands, :streams, instance_accessor: false, default: {}.freeze
|
|
26
|
+
class_attribute :_datagram_handler, instance_accessor: false
|
|
27
|
+
|
|
28
|
+
class << self
|
|
29
|
+
def command(type, to:)
|
|
30
|
+
self.commands = commands.merge(type.to_s => to).freeze
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def stream(type, to:)
|
|
34
|
+
self.streams = streams.merge(type.to_s => to).freeze
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def datagram(&block)
|
|
38
|
+
block ? self._datagram_handler = block : _datagram_handler
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def handle_stream(raw_stream)
|
|
43
|
+
dispatcher.call(raw_stream)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
def run
|
|
48
|
+
each_stream { |raw_stream| handle_stream(raw_stream) }
|
|
49
|
+
each_datagram { |packet| handle_datagram(packet) } if self.class.datagram
|
|
50
|
+
wait_for_session_close
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def handle_datagram(packet)
|
|
54
|
+
instance_exec(packet, &self.class.datagram)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def dispatcher
|
|
58
|
+
@dispatcher ||= Dispatcher.new(
|
|
59
|
+
commands: self.class.commands,
|
|
60
|
+
streams: self.class.streams,
|
|
61
|
+
codec: codec,
|
|
62
|
+
session: session
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
require "quicsilver/web_transport/channel/dispatcher"
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Quicsilver
|
|
6
|
+
module WebTransport
|
|
7
|
+
module Codec
|
|
8
|
+
module Raw
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def decode(payload)
|
|
12
|
+
payload
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def encode(value)
|
|
16
|
+
value.to_s
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
module JSON
|
|
21
|
+
module_function
|
|
22
|
+
|
|
23
|
+
def decode(payload)
|
|
24
|
+
::JSON.parse(payload)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def encode(value)
|
|
28
|
+
::JSON.generate(value)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
def resolve(codec)
|
|
35
|
+
case codec
|
|
36
|
+
when nil, :raw then Raw
|
|
37
|
+
when :json then JSON
|
|
38
|
+
else codec
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "quicsilver/web_transport/endpoint"
|
|
4
|
+
require "quicsilver/web_transport/codec"
|
|
5
|
+
require "quicsilver/web_transport/message_codec"
|
|
6
|
+
|
|
7
|
+
module Quicsilver
|
|
8
|
+
module WebTransport
|
|
9
|
+
# Endpoint for unreliable, unordered WebTransport datagrams.
|
|
10
|
+
#
|
|
11
|
+
# Example:
|
|
12
|
+
# class CursorTransport < Quicsilver::WebTransport::DatagramEndpoint
|
|
13
|
+
# receives CursorUpdate
|
|
14
|
+
#
|
|
15
|
+
# def receive(cursor)
|
|
16
|
+
# broadcast_cursor cursor
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
class DatagramEndpoint < Endpoint
|
|
20
|
+
include MessageCodec
|
|
21
|
+
|
|
22
|
+
setting :receives
|
|
23
|
+
setting :codec, default: :raw
|
|
24
|
+
|
|
25
|
+
def receive(_message)
|
|
26
|
+
raise NotImplementedError, "#{self.class} must implement #receive"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def run
|
|
32
|
+
session.on_datagram do |packet|
|
|
33
|
+
receive decode_with(self.class.receives, packet)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
wait_for_session_close
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails"
|
|
4
|
+
require "action_dispatch"
|
|
5
|
+
require "active_support/core_ext/class/attribute"
|
|
6
|
+
require "quicsilver/web_transport/thread_runner"
|
|
7
|
+
|
|
8
|
+
module Quicsilver
|
|
9
|
+
module WebTransport
|
|
10
|
+
# Base Rack/Rails endpoint for WebTransport sessions.
|
|
11
|
+
#
|
|
12
|
+
# Subclasses own application-level behavior. Quicsilver core owns the raw
|
|
13
|
+
# HTTP/3/WebTransport protocol; this class owns Rails admission, executor
|
|
14
|
+
# wrapping, and long-lived session cleanup.
|
|
15
|
+
class Endpoint
|
|
16
|
+
class << self
|
|
17
|
+
def call(env)
|
|
18
|
+
new(env).call
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
# Declares a class-level macro that subclasses inherit until they set their own:
|
|
23
|
+
#
|
|
24
|
+
# setting :codec, default: :raw # then `codec :json` in a subclass
|
|
25
|
+
#
|
|
26
|
+
# Stored with class_attribute, because an instance variable on the class is not inherited:
|
|
27
|
+
# a subclass of an endpoint configured `codec :json` would read its own unset value.
|
|
28
|
+
def setting(name, default: nil)
|
|
29
|
+
class_attribute :"_#{name}", instance_accessor: false, default: default
|
|
30
|
+
|
|
31
|
+
define_singleton_method(name) do |value = nil|
|
|
32
|
+
value.nil? ? public_send(:"_#{name}") : public_send(:"_#{name}=", value)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
attr_reader :env, :session, :request
|
|
38
|
+
|
|
39
|
+
def initialize(env)
|
|
40
|
+
@env = env
|
|
41
|
+
@session = Quicsilver::WebTransport.from_env(env)
|
|
42
|
+
@request = build_request(env)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def call
|
|
46
|
+
return not_found unless session
|
|
47
|
+
|
|
48
|
+
connect
|
|
49
|
+
session.accept!
|
|
50
|
+
spawn(:session) { run_endpoint }
|
|
51
|
+
|
|
52
|
+
accepted
|
|
53
|
+
rescue => error
|
|
54
|
+
reject(error)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def connect
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def disconnect
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def run
|
|
66
|
+
raise NotImplementedError, "#{self.class} must implement #run"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def run_endpoint
|
|
70
|
+
run
|
|
71
|
+
ensure
|
|
72
|
+
disconnect
|
|
73
|
+
session.close if session.open?
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def wait_for_session_close
|
|
77
|
+
closed = Queue.new
|
|
78
|
+
session.on_close { closed.close }
|
|
79
|
+
closed.pop
|
|
80
|
+
rescue ClosedQueueError
|
|
81
|
+
nil
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def each_stream(&block)
|
|
85
|
+
session.on_stream do |raw_stream|
|
|
86
|
+
spawn(:stream) { block.call(raw_stream) }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def each_datagram(&block)
|
|
91
|
+
session.on_datagram do |packet|
|
|
92
|
+
spawn(:datagram) { block.call(packet) }
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def spawn(kind = :task, &block)
|
|
97
|
+
runner.spawn(kind, executor: executor, source: "quicsilver.webtransport.#{kind}", &block)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def runner
|
|
101
|
+
rails_app ? rails_app.config.quicsilver.webtransport_runner : ThreadRunner.new
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def executor
|
|
105
|
+
rails_app ? rails_app.executor : InlineExecutor
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def build_request(env)
|
|
109
|
+
ActionDispatch::Request.new(rails_app ? rails_app.env_config.merge(env) : env)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# nil outside a booted app, as in this gem's own tests.
|
|
113
|
+
def rails_app
|
|
114
|
+
::Rails.application
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def accepted
|
|
118
|
+
[200, {}, []]
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def not_found
|
|
122
|
+
[404, { "content-type" => "text/plain; charset=utf-8" }, ["WebTransport session not found"]]
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def reject(error)
|
|
126
|
+
session&.reject!(500) unless session&.accepted?
|
|
127
|
+
raise error
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
InlineExecutor = Object.new.tap do |executor|
|
|
131
|
+
def executor.wrap(source: nil)
|
|
132
|
+
yield
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|