Skip to content

Commit f3327ea

Browse files
committed
quic: add stream headers support
1 parent 630c355 commit f3327ea

4 files changed

Lines changed: 400 additions & 44 deletions

File tree

doc/api/quic.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,17 +558,26 @@ added: v23.8.0
558558

559559
* `options` {Object}
560560
* `body` {ArrayBuffer | ArrayBufferView | Blob}
561+
* `headers` {Object} Initial request or response headers to send. Only
562+
used when the session supports headers (e.g. HTTP/3). If `body` is not
563+
specified and `headers` is provided, the stream is treated as
564+
headers-only (terminal).
561565
* `priority` {string} The priority level of the stream. One of `'high'`,
562566
`'default'`, or `'low'`. **Default:** `'default'`.
563567
* `incremental` {boolean} When `true`, data from this stream may be
564568
interleaved with data from other streams of the same priority level.
565569
When `false`, the stream should be completed before same-priority peers.
566570
**Default:** `false`.
571+
* `onheaders` {quic.OnHeadersCallback} Callback for received headers.
572+
* `ontrailers` {quic.OnTrailersCallback} Callback for received trailers.
573+
* `onwanttrailers` {Function} Callback when trailers should be sent.
567574
* Returns: {Promise} for a {quic.QuicStream}
568575

569576
Open a new bidirectional stream. If the `body` option is not specified,
570577
the outgoing stream will be half-closed. The `priority` and `incremental`
571578
options are only used when the session supports priority (e.g. HTTP/3).
579+
The `headers`, `onheaders`, `ontrailers`, and `onwanttrailers` options
580+
are only used when the session supports headers (e.g. HTTP/3).
572581

573582
### `session.createUnidirectionalStream([options])`
574583

@@ -578,12 +587,16 @@ added: v23.8.0
578587

579588
* `options` {Object}
580589
* `body` {ArrayBuffer | ArrayBufferView | Blob}
590+
* `headers` {Object} Initial request headers to send.
581591
* `priority` {string} The priority level of the stream. One of `'high'`,
582592
`'default'`, or `'low'`. **Default:** `'default'`.
583593
* `incremental` {boolean} When `true`, data from this stream may be
584594
interleaved with data from other streams of the same priority level.
585595
When `false`, the stream should be completed before same-priority peers.
586596
**Default:** `false`.
597+
* `onheaders` {quic.OnHeadersCallback} Callback for received headers.
598+
* `ontrailers` {quic.OnTrailersCallback} Callback for received trailers.
599+
* `onwanttrailers` {Function} Callback when trailers should be sent.
587600
* Returns: {Promise} for a {quic.QuicStream}
588601

589602
Open a new unidirectional stream. If the `body` option is not specified,
@@ -982,6 +995,124 @@ added: v23.8.0
982995

983996
The callback to invoke when the stream is reset. Read/write.
984997

998+
### `stream.headers`
999+
1000+
<!-- YAML
1001+
added: REPLACEME
1002+
-->
1003+
1004+
* Type: {Object|undefined}
1005+
1006+
The buffered initial headers received on this stream, or `undefined` if the
1007+
application does not support headers or no headers have been received yet.
1008+
For server-side streams, this contains the request headers (e.g., `:method`,
1009+
`:path`, `:scheme`). For client-side streams, this contains the response
1010+
headers (e.g., `:status`).
1011+
1012+
Header names are lowercase strings. Multi-value headers are represented as
1013+
arrays. The object has `__proto__: null`.
1014+
1015+
### `stream.onheaders`
1016+
1017+
<!-- YAML
1018+
added: REPLACEME
1019+
-->
1020+
1021+
* Type: {quic.OnHeadersCallback}
1022+
1023+
The callback to invoke when headers are received on the stream. The callback
1024+
receives `(headers, kind)` where `headers` is an object (same format as
1025+
`stream.headers`) and `kind` is one of `'initial'` or `'informational'`
1026+
(for 1xx responses). Throws `ERR_INVALID_STATE` if set on a session that
1027+
does not support headers. Read/write.
1028+
1029+
### `stream.ontrailers`
1030+
1031+
<!-- YAML
1032+
added: REPLACEME
1033+
-->
1034+
1035+
* Type: {quic.OnTrailersCallback}
1036+
1037+
The callback to invoke when trailing headers are received from the peer.
1038+
The callback receives `(trailers)` where `trailers` is an object in the
1039+
same format as `stream.headers`. Throws `ERR_INVALID_STATE` if set on a
1040+
session that does not support headers. Read/write.
1041+
1042+
### `stream.onwanttrailers`
1043+
1044+
<!-- YAML
1045+
added: REPLACEME
1046+
-->
1047+
1048+
* Type: {Function}
1049+
1050+
The callback to invoke when the application is ready for trailing headers
1051+
to be sent. This is called synchronously — the user must call
1052+
[`stream.sendTrailers()`][] within this callback. Throws
1053+
`ERR_INVALID_STATE` if set on a session that does not support headers.
1054+
Read/write.
1055+
1056+
### `stream.pendingTrailers`
1057+
1058+
<!-- YAML
1059+
added: REPLACEME
1060+
-->
1061+
1062+
* Type: {Object|undefined}
1063+
1064+
Set trailing headers to be sent automatically when the application requests
1065+
them. This is an alternative to the [`stream.onwanttrailers`][] callback
1066+
for cases where the trailers are known before the body completes. Throws
1067+
`ERR_INVALID_STATE` if set on a session that does not support headers.
1068+
Read/write.
1069+
1070+
### `stream.sendHeaders(headers[, options])`
1071+
1072+
<!-- YAML
1073+
added: REPLACEME
1074+
-->
1075+
1076+
* `headers` {Object} Header object with string keys and string or
1077+
string-array values. Pseudo-headers (`:method`, `:path`, etc.) must
1078+
appear before regular headers.
1079+
* `options` {Object}
1080+
* `terminal` {boolean} If `true`, the stream is closed for sending
1081+
after the headers (no body will follow). **Default:** `false`.
1082+
* Returns: {boolean}
1083+
1084+
Sends initial or response headers on the stream. For client-side streams,
1085+
this sends request headers. For server-side streams, this sends response
1086+
headers. Throws `ERR_INVALID_STATE` if the session does not support headers.
1087+
1088+
### `stream.sendInformationalHeaders(headers)`
1089+
1090+
<!-- YAML
1091+
added: REPLACEME
1092+
-->
1093+
1094+
* `headers` {Object} Header object. Must include `:status` with a 1xx
1095+
value (e.g., `{ ':status': '103', 'link': '</style.css>; rel=preload' }`).
1096+
* Returns: {boolean}
1097+
1098+
Sends informational (1xx) response headers. Server only. Throws
1099+
`ERR_INVALID_STATE` if the session does not support headers.
1100+
1101+
### `stream.sendTrailers(headers)`
1102+
1103+
<!-- YAML
1104+
added: REPLACEME
1105+
-->
1106+
1107+
* `headers` {Object} Trailing header object. Pseudo-headers must not be
1108+
included in trailers.
1109+
* Returns: {boolean}
1110+
1111+
Sends trailing headers on the stream. Must be called synchronously during
1112+
the [`stream.onwanttrailers`][] callback, or set ahead of time via
1113+
[`stream.pendingTrailers`][]. Throws `ERR_INVALID_STATE` if the session
1114+
does not support headers.
1115+
9851116
### `stream.priority`
9861117

9871118
<!-- YAML
@@ -1930,6 +2061,26 @@ added: v23.8.0
19302061
* `this` {quic.QuicStream}
19312062
* `error` {any}
19322063

2064+
### Callback: `OnHeadersCallback`
2065+
2066+
<!-- YAML
2067+
added: REPLACEME
2068+
-->
2069+
2070+
* `this` {quic.QuicStream}
2071+
* `headers` {Object} Header object with lowercase string keys and
2072+
string or string-array values.
2073+
* `kind` {string} One of `'initial'` or `'informational'`.
2074+
2075+
### Callback: `OnTrailersCallback`
2076+
2077+
<!-- YAML
2078+
added: REPLACEME
2079+
-->
2080+
2081+
* `this` {quic.QuicStream}
2082+
* `trailers` {Object} Trailing header object.
2083+
19332084
## Diagnostic Channels
19342085

19352086
### Channel: `quic.endpoint.created`
@@ -2081,4 +2232,7 @@ added: v23.8.0
20812232

20822233
[`session.onnewtoken`]: #sessiononnewtoken
20832234
[`sessionOptions.sni`]: #sessionoptionssni-server-only
2235+
[`stream.onwanttrailers`]: #streamonwanttrailers
2236+
[`stream.pendingTrailers`]: #streampendingtrailers
2237+
[`stream.sendTrailers()`]: #streamsendtrailersheaders
20842238
[`stream.setPriority()`]: #streamsetpriorityoptions

0 commit comments

Comments
 (0)