Skip to content

Commit 2f56cd2

Browse files
committed
quic: add applicationOptions to session
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: nodejs#63267 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 4aea61d commit 2f56cd2

11 files changed

Lines changed: 320 additions & 71 deletions

File tree

doc/api/quic.md

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,18 @@ added: v23.8.0
744744

745745
A `QuicSession` represents the local side of a QUIC connection.
746746

747+
### `session.applicationOptions`
748+
749+
<!-- YAML
750+
added: REPLACEME
751+
-->
752+
753+
* Type: {quic.ApplicationOptions}
754+
755+
The current application-level options for this session. These include settings
756+
that are specific to the negotiated application protocol (e.g. HTTP/3) and may
757+
be negotiated separately from the transport parameters. Read only.
758+
747759
### `session.close([options])`
748760

749761
<!-- YAML
@@ -2238,6 +2250,70 @@ added: v23.8.0
22382250

22392251
## Types
22402252

2253+
### type: `ApplicationOptions`
2254+
2255+
<!-- YAML
2256+
added: REPLACEME
2257+
-->
2258+
2259+
* Type: {Object}
2260+
2261+
The application specific options.
2262+
2263+
#### `applicationOptions.maxHeaderPairs`
2264+
2265+
* Type: {bigint|number}
2266+
2267+
Maximum number of header name-value pairs accepted per header block.
2268+
Headers beyond this limit are silently dropped. **Default:** `128`
2269+
2270+
#### `applicationOptions.maxHeaderLength`
2271+
2272+
* Type: {bigint|number}
2273+
2274+
Maximum total byte length of all header names and values combined per header
2275+
block. Headers that would push the total over this limit are silently
2276+
dropped. **Default:** `8192`
2277+
2278+
#### `applicationOptions.maxFieldSectionSize`
2279+
2280+
* Type: {bigint|number}
2281+
2282+
Maximum size of a compressed header field section (QPACK). `0` means
2283+
unlimited. **Default:** `0`
2284+
2285+
#### `applicationOptions.qpackMaxDTableCapacity`
2286+
2287+
* Type: {bigint|number}
2288+
2289+
QPACK dynamic table capacity in bytes. Set to `0` to disable the dynamic
2290+
table. **Default:** `4096`
2291+
2292+
#### `applicationOptions.qpackEncoderMaxDTableCapacity`
2293+
2294+
* Type: {bigint|number}
2295+
2296+
QPACK encoder maximum dynamic table capacity. **Default:** `4096`
2297+
2298+
#### `applicationOptions.qpackBlockedStreams`
2299+
2300+
* Type: {bigint|number}
2301+
2302+
Maximum number of streams that can e blocked waiting for QPACK dynamic table
2303+
updates. **Default:** `100`
2304+
2305+
#### `applicationOptions.enableConnectProtocol`
2306+
2307+
* Type: {boolean}
2308+
2309+
Enable the extended CONNECT protocol (RFC 9220). **Default:** `false`
2310+
2311+
#### `applicationOptions.enableDatagrams`
2312+
2313+
* Type: {boolean}
2314+
2315+
Enable HTTP/3 datagrams (RFC 9297). **Default:** `false`
2316+
22412317
### Type: `EndpointOptions`
22422318

22432319
<!-- YAML
@@ -2496,30 +2572,9 @@ Default: `'h3'`
24962572
added: v26.2.0
24972573
-->
24982574

2499-
* Type: {Object}
2575+
* Type: {quic.ApplicationOptions}
25002576

2501-
HTTP/3 application-specific options. These only apply when the negotiated
2502-
ALPN selects the HTTP/3 application (`'h3'`).
2503-
2504-
* `maxHeaderPairs` {number} Maximum number of header name-value pairs
2505-
accepted per header block. Headers beyond this limit are silently
2506-
dropped. **Default:** `128`
2507-
* `maxHeaderLength` {number} Maximum total byte length of all header
2508-
names and values combined per header block. Headers that would push
2509-
the total over this limit are silently dropped. **Default:** `8192`
2510-
* `maxFieldSectionSize` {number} Maximum size of a compressed header
2511-
field section (QPACK). `0` means unlimited. **Default:** `0`
2512-
* `qpackMaxDTableCapacity` {number} QPACK dynamic table capacity in
2513-
bytes. Set to `0` to disable the dynamic table. **Default:** `4096`
2514-
* `qpackEncoderMaxDTableCapacity` {number} QPACK encoder maximum
2515-
dynamic table capacity. **Default:** `4096`
2516-
* `qpackBlockedStreams` {number} Maximum number of streams that can
2517-
be blocked waiting for QPACK dynamic table updates.
2518-
**Default:** `100`
2519-
* `enableConnectProtocol` {boolean} Enable the extended CONNECT
2520-
protocol (RFC 9220). **Default:** `false`
2521-
* `enableDatagrams` {boolean} Enable HTTP/3 datagrams (RFC 9297).
2522-
**Default:** `false`
2577+
Application-specific options.
25232578

25242579
```mjs
25252580
const { listen } = await import('node:quic');

lib/internal/quic/quic.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,14 @@ class QuicSession {
26782678
debug('session created');
26792679
}
26802680

2681+
get applicationOptions() {
2682+
// We don't cache application options because they may be updated by the
2683+
// C++ layer after session creation depending on the behavior of the
2684+
// application.
2685+
if (this.destroyed) return null;
2686+
return this.#handle.applicationOptions();
2687+
}
2688+
26812689
get localTransportParams() {
26822690
if (this.#inner.localTransportParams !== undefined) {
26832691
return this.#inner.localTransportParams;

src/quic/application.cc

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919

2020
namespace node {
2121

22+
using v8::BigInt;
23+
using v8::Boolean;
24+
using v8::DictionaryTemplate;
2225
using v8::Just;
2326
using v8::Local;
2427
using v8::Maybe;
28+
using v8::MaybeLocal;
2529
using v8::Nothing;
2630
using v8::Object;
2731
using v8::Value;
@@ -114,6 +118,44 @@ Maybe<Session::Application_Options> Session::Application_Options::From(
114118
return Just<Application_Options>(options);
115119
}
116120

121+
MaybeLocal<Object> Session::Application_Options::ToObject(
122+
Environment* env) const {
123+
auto& binding_data = BindingData::Get(env);
124+
auto tmpl = binding_data.application_options_template();
125+
static constexpr std::string_view names[] = {
126+
"maxHeaderPairs",
127+
"maxHeaderLength",
128+
"maxFieldSectionSize",
129+
"qpackMaxDtableCapacity",
130+
"qpackEncoderMaxDtableCapacity",
131+
"qpackBlockedStreams",
132+
"enableConnectProtocol",
133+
"enableDatagrams",
134+
};
135+
if (tmpl.IsEmpty()) {
136+
tmpl = DictionaryTemplate::New(env->isolate(), names);
137+
binding_data.set_application_options_template(tmpl);
138+
}
139+
MaybeLocal<Value> values[] = {
140+
BigInt::NewFromUnsigned(env->isolate(), max_header_pairs),
141+
BigInt::NewFromUnsigned(env->isolate(), max_header_length),
142+
BigInt::NewFromUnsigned(env->isolate(), max_field_section_size),
143+
BigInt::NewFromUnsigned(env->isolate(), qpack_max_dtable_capacity),
144+
BigInt::NewFromUnsigned(env->isolate(),
145+
qpack_encoder_max_dtable_capacity),
146+
BigInt::NewFromUnsigned(env->isolate(), qpack_blocked_streams),
147+
Boolean::New(env->isolate(), enable_connect_protocol),
148+
Boolean::New(env->isolate(), enable_datagrams),
149+
};
150+
static_assert(std::size(values) == std::size(names));
151+
152+
auto obj = tmpl->NewInstance(env->context(), values);
153+
if (obj->SetPrototypeV2(env->context(), Null(env->isolate())).IsNothing()) {
154+
return {};
155+
}
156+
return obj;
157+
}
158+
117159
// ============================================================================
118160

119161
std::string Session::Application::StreamData::ToString() const {
@@ -655,7 +697,10 @@ class DefaultApplication final : public Session::Application {
655697
// Marked NOLINT because the cpp linter gets confused about this using
656698
// statement not being sorted with the using v8 statements at the top
657699
// of the namespace.
658-
using Application::Application; // NOLINT
700+
DefaultApplication(Session* session, const Options& options)
701+
: Session::Application(session, options), options_(options) {}
702+
703+
const Options& options() const override { return options_; }
659704

660705
Session::Application::Type type() const override {
661706
return Session::Application::Type::DEFAULT;
@@ -827,6 +872,8 @@ class DefaultApplication final : public Session::Application {
827872
}
828873
}
829874

875+
Options options_;
876+
830877
Stream::Queue stream_queue_;
831878
};
832879

src/quic/application.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Session::Application : public MemoryRetainer {
3838
Application(Session* session, const Options& options);
3939
DISALLOW_COPY_AND_MOVE(Application)
4040

41+
// Get the active options for this application. These may differ from the
42+
// options passed at construction time since some options can be negotiated.
43+
virtual const Options& options() const = 0;
44+
4145
// The type of Application, exposed via the session state so JS
4246
// can observe which Application was selected after ALPN negotiation.
4347
// This is used primarily for testing/debugging.

src/quic/bindingdata.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,17 @@ void BindingData::set_transport_params_template(
385385

386386
Local<DictionaryTemplate> BindingData::transport_params_template() const {
387387
return PersistentToLocal::Default(env()->isolate(),
388-
transport_params_template_);
388+
transport_params_template_);
389+
}
390+
391+
void BindingData::set_application_options_template(
392+
Local<DictionaryTemplate> tmpl) {
393+
application_options_template_.Reset(env()->isolate(), tmpl);
394+
}
395+
396+
Local<DictionaryTemplate> BindingData::application_options_template() const {
397+
return PersistentToLocal::Default(env()->isolate(),
398+
application_options_template_);
389399
}
390400

391401
#define V(name, _) \

src/quic/bindingdata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ class BindingData final
306306
void set_transport_params_template(v8::Local<v8::DictionaryTemplate> tmpl);
307307
v8::Local<v8::DictionaryTemplate> transport_params_template() const;
308308

309+
void set_application_options_template(v8::Local<v8::DictionaryTemplate> tmpl);
310+
v8::Local<v8::DictionaryTemplate> application_options_template() const;
311+
309312
#define V(name, _) \
310313
void set_##name##_callback(v8::Local<v8::Function> fn); \
311314
v8::Local<v8::Function> name##_callback() const;
@@ -325,6 +328,7 @@ class BindingData final
325328
#undef V
326329

327330
v8::Global<v8::DictionaryTemplate> transport_params_template_;
331+
v8::Global<v8::DictionaryTemplate> application_options_template_;
328332

329333
#define V(name, _) v8::Global<v8::Function> name##_callback_;
330334
QUIC_JS_CALLBACKS(V)

src/quic/http3.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class Http3ApplicationImpl final : public Session::Application {
157157
session->set_priority_supported();
158158
}
159159

160+
const Options& options() const override { return options_; }
161+
160162
Session::Application::Type type() const override {
161163
return Session::Application::Type::HTTP3;
162164
}

src/quic/session.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ uint64_t MaxDatagramPayload(uint64_t max_frame_size) {
193193
V(SendDatagram, sendDatagram, SIDE_EFFECT) \
194194
V(LocalTransportParams, localTransportParams, NO_SIDE_EFFECT) \
195195
V(RemoteTransportParams, remoteTransportParams, NO_SIDE_EFFECT) \
196+
V(ApplicationOptions, applicationOptions, NO_SIDE_EFFECT)
196197

197198
struct Session::State final {
198199
#define V(_, name, type) type name;
@@ -1195,6 +1196,24 @@ struct Session::Impl final : public MemoryRetainer {
11951196
args.GetReturnValue().Set(obj);
11961197
}
11971198
}
1199+
1200+
JS_METHOD(ApplicationOptions) {
1201+
auto env = Environment::GetCurrent(args);
1202+
Session* session;
1203+
ASSIGN_OR_RETURN_UNWRAP(&session, args.This());
1204+
1205+
Local<Object> obj;
1206+
if (!session->has_application()) {
1207+
// The application has not yet been selected (ALPN negotiation is not
1208+
// yet complete on the server) or the session has been destroyed. In
1209+
// either case, the application options are not available.
1210+
return args.GetReturnValue().SetUndefined();
1211+
}
1212+
auto& options = session->application().options();
1213+
if (options.ToObject(env).ToLocal(&obj)) {
1214+
args.GetReturnValue().Set(obj);
1215+
}
1216+
}
11981217
// Internal ngtcp2 callbacks
11991218

12001219
static int on_acknowledge_stream_data_offset(ngtcp2_conn* conn,
@@ -2074,6 +2093,10 @@ TLSSession& Session::tls_session() const {
20742093
return *tls_session_;
20752094
}
20762095

2096+
bool Session::has_application() const {
2097+
return !is_destroyed() && impl_->application_ != nullptr;
2098+
}
2099+
20772100
Session::Application& Session::application() const {
20782101
DCHECK(!is_destroyed());
20792102
DCHECK(impl_->application_);

src/quic/session.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
9191

9292
std::string ToString() const;
9393

94+
v8::MaybeLocal<v8::Object> ToObject(Environment* env) const;
95+
9496
static const Application_Options kDefault;
9597
};
9698

@@ -324,6 +326,7 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
324326
uint32_t version() const;
325327
Endpoint& endpoint() const;
326328
TLSSession& tls_session() const;
329+
bool has_application() const;
327330
Application& application() const;
328331
const Config& config() const;
329332
const Options& options() const;

0 commit comments

Comments
 (0)