-
-
Notifications
You must be signed in to change notification settings - Fork 36k
quic: add support for HTTP/3 datagrams #64234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pimterry
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
pimterry:h3-datagram-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,6 +276,8 @@ const { | |
|
|
||
| const kNilDatagramId = 0n; | ||
|
|
||
| const kApplicationTypeHttp3 = 2; | ||
|
|
||
| // Module-level registry of all live QuicEndpoint instances. Used by | ||
| // connect() and listen() to find existing endpoints for reuse instead | ||
| // of creating a new one per session. | ||
|
|
@@ -945,6 +947,16 @@ setCallbacks({ | |
| this[kOwner][kBlocked](); | ||
| }, | ||
|
|
||
| /** | ||
| * Called when an HTTP/3 datagram (RFC 9297) is received for this stream. | ||
| * @param {Uint8Array} uint8Array The datagram payload | ||
| * @param {boolean} early True if received as 0-RTT early data. | ||
| */ | ||
| onStreamDatagram(uint8Array, early) { | ||
| debug('stream datagram callback', this[kOwner]); | ||
| this[kOwner][kDatagram](uint8Array, early); | ||
| }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to introduce a new callback? Alternatively, we could introduce a third argument to the existing callback that indicates that the datagram is associated with a specific stream. |
||
|
|
||
| onStreamDrain() { | ||
| // Called when the stream's outbound buffer has capacity for more data. | ||
| debug('stream drain callback', this[kOwner]); | ||
|
|
@@ -1571,6 +1583,7 @@ class QuicStream { | |
| onerror: undefined, | ||
| onblocked: undefined, | ||
| onreset: undefined, | ||
| ondatagram: undefined, | ||
| onheaders: undefined, | ||
| ontrailers: undefined, | ||
| oninfo: undefined, | ||
|
|
@@ -1776,6 +1789,29 @@ class QuicStream { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * The callback invoked when an HTTP/3 datagram (RFC 9297) associated with | ||
| * this stream is received. Read/write. | ||
| * @type {OnStreamDatagramCallback} | ||
| */ | ||
| get ondatagram() { | ||
| assertIsQuicStream(this); | ||
| return this.#inner.ondatagram; | ||
| } | ||
|
|
||
| set ondatagram(fn) { | ||
| assertIsQuicStream(this); | ||
| const inner = this.#inner; | ||
| if (fn === undefined) { | ||
| inner.ondatagram = undefined; | ||
| inner.state.wantsDatagram = false; | ||
| } else { | ||
| validateFunction(fn, 'ondatagram'); | ||
| inner.ondatagram = FunctionPrototypeBind(fn, this); | ||
| inner.state.wantsDatagram = true; | ||
| } | ||
| } | ||
|
|
||
| /** @type {OnHeadersCallback} */ | ||
| get onheaders() { | ||
| assertIsQuicStream(this); | ||
|
|
@@ -2429,6 +2465,52 @@ class QuicStream { | |
| this.#handle.resetStream(BigInt(code)); | ||
| } | ||
|
|
||
| /** | ||
| * Sends an HTTP/3 datagram (RFC 9297) associated with this stream. The | ||
| * payload is framed with the stream's Quarter Stream ID by the C++ layer | ||
| * before being queued for transmission. | ||
| * | ||
| * Datagrams are unreliable: they may be lost, reordered, or dropped if too | ||
| * large or if datagrams were not negotiated. In any such case `0n` is | ||
| * returned and the payload is silently discarded. Otherwise the underlying | ||
| * QUIC datagram id is returned. | ||
| * | ||
| * If the stream is still pending (no id yet) the datagram is buffered and | ||
| * sending attempted once the stream opens. `0n` is still returned if it | ||
| * cannot even be buffered. | ||
| * @param {ArrayBufferView|string} datagram The datagram payload. | ||
| * @param {string} [encoding] The encoding to use if datagram is a string. | ||
| * @returns {bigint} The datagram id, or `0n` if it was not queued. | ||
| */ | ||
| sendDatagram(datagram, encoding = 'utf8') { | ||
| assertIsQuicStream(this); | ||
| if (this.destroyed) return kNilDatagramId; | ||
|
|
||
| // Datagrams are gated by the session's negotiated max datagram size. | ||
| // A value of 0 means datagrams are disabled (transport or, for HTTP/3, | ||
| // the peer's SETTINGS_H3_DATAGRAM). The C++ layer enforces the exact | ||
| // limit including the Quarter Stream ID prefix. | ||
| if (getQuicSessionState(this.#inner.session).maxDatagramSize === 0) { | ||
| return kNilDatagramId; | ||
| } | ||
|
|
||
| if (typeof datagram === 'string') { | ||
| datagram = new Uint8Array(Buffer.from(datagram, encoding)); | ||
| } else if (!isArrayBufferView(datagram)) { | ||
| throw new ERR_INVALID_ARG_TYPE('datagram', | ||
| ['ArrayBufferView', 'string'], | ||
| datagram); | ||
| } | ||
|
|
||
| const length = isDataView(datagram) ? | ||
| DataViewPrototypeGetByteLength(datagram) : | ||
| TypedArrayPrototypeGetByteLength(datagram); | ||
|
|
||
| const id = this.#handle.sendDatagram(datagram); | ||
| debug(`stream datagram ${id} sent with ${length} bytes`); | ||
| return id; | ||
| } | ||
|
|
||
| /** | ||
| * The priority of the stream. If the stream is destroyed or if | ||
| * the session does not support priority, `null` will be | ||
|
|
@@ -2538,6 +2620,7 @@ class QuicStream { | |
| inner.pendingClose.resolve = undefined; | ||
| inner.onblocked = undefined; | ||
| inner.onreset = undefined; | ||
| inner.ondatagram = undefined; | ||
| inner.onheaders = undefined; | ||
| inner.onerror = undefined; | ||
| inner.ontrailers = undefined; | ||
|
|
@@ -2591,6 +2674,14 @@ class QuicStream { | |
| safeCallbackInvoke(inner.onreset, this, error); | ||
| } | ||
|
|
||
| [kDatagram](u8, early) { | ||
| const inner = this.#inner; | ||
| assert(typeof inner.ondatagram === 'function', | ||
| 'Unexpected stream datagram event'); | ||
| if (inner.session === undefined) return; | ||
| safeCallbackInvoke(inner.ondatagram, this, u8, early); | ||
| } | ||
|
|
||
| [kHeaders](headers, kind) { | ||
| const block = parseHeaderPairs(headers); | ||
| const kindName = kHeadersKindName[kind] ?? kind; | ||
|
|
@@ -3359,6 +3450,13 @@ class QuicSession { | |
| throw new ERR_INVALID_STATE('Session is closed'); | ||
| } | ||
|
|
||
| // HTTP/3 sessions must use HTTP/3 datagrams | ||
| if (this.#inner.state.applicationType === kApplicationTypeHttp3) { | ||
| throw new ERR_INVALID_STATE( | ||
| 'Raw datagrams are not supported on HTTP/3 sessions; ' + | ||
| 'use stream.sendDatagram() instead'); | ||
| } | ||
|
|
||
| const maxDatagramSize = this.#inner.state.maxDatagramSize; | ||
|
|
||
| // The peer max datagram size is either unknown or they have explicitly | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: we should expose the type enum constants via the binding so we don't have to worry about keeping anything in sync.