doc/api/quic.md
<!-- source_link=lib/quic.js -->Stability: 1.0 - Early development
The 'node:quic' module provides an implementation of the QUIC protocol.
To access it, start Node.js with the --experimental-quic option and:
import quic from 'node:quic';
const quic = require('node:quic');
The module is only available under the node: scheme.
quic.connect(address[, options])address {string|net.SocketAddress}options {quic.SessionOptions}Initiate a new client-side session.
import { connect } from 'node:quic';
import { Buffer } from 'node:buffer';
const enc = new TextEncoder();
const alpn = 'foo';
const client = await connect('123.123.123.123:8888', { alpn });
await client.createUnidirectionalStream({
body: enc.encode('hello world'),
});
By default, every call to connect(...) will create a new local
QuicEndpoint instance bound to a new random local IP port. To
specify the exact local address to use, or to multiplex multiple
QUIC sessions over a single local port, pass the endpoint option
with either a QuicEndpoint or EndpointOptions as the argument.
import { QuicEndpoint, connect } from 'node:quic';
const endpoint = new QuicEndpoint({
address: '127.0.0.1:1234',
});
const client = await connect('123.123.123.123:8888', { endpoint });
quic.listen(onsession,[options])onsession {quic.OnSessionCallback}options {quic.SessionOptions}Configures the endpoint to listen as a server. When a new session is initiated by
a remote peer, the given onsession callback will be invoked with the created
session.
import { listen } from 'node:quic';
const endpoint = await listen((session) => {
// ... handle the session
});
// Closing the endpoint allows any sessions open when close is called
// to complete naturally while preventing new sessions from being
// initiated. Once all existing sessions have finished, the endpoint
// will be destroyed. The call returns a promise that is resolved once
// the endpoint is destroyed.
await endpoint.close();
By default, every call to listen(...) will create a new local
QuicEndpoint instance bound to a new random local IP port. To
specify the exact local address to use, or to multiplex multiple
QUIC sessions over a single local port, pass the endpoint option
with either a QuicEndpoint or EndpointOptions as the argument.
At most, any single QuicEndpoint can only be configured to listen as
a server once.
QuicEndpointA QuicEndpoint encapsulates the local UDP-port binding for QUIC. It can be
used as both a client and a server.
new QuicEndpoint([options])options {quic.EndpointOptions}endpoint.addressThe local UDP socket address to which the endpoint is bound, if any.
If the endpoint is not currently bound then the value will be undefined. Read only.
endpoint.busyWhen endpoint.busy is set to true, the endpoint will temporarily reject
new sessions from being created. Read/write.
// Mark the endpoint busy. New sessions will be prevented.
endpoint.busy = true;
// Mark the endpoint free. New session will be allowed.
endpoint.busy = false;
The busy property is useful when the endpoint is under heavy load and needs to
temporarily reject new sessions while it catches up.
endpoint.close()Gracefully close the endpoint. The endpoint will close and destroy itself when all currently open sessions close. Once called, new sessions will be rejected.
Returns a promise that is fulfilled when the endpoint is destroyed.
endpoint.closedA promise that is fulfilled when the endpoint is destroyed. This will be the same promise that is
returned by the endpoint.close() function. Read only.
endpoint.closingTrue if endpoint.close() has been called and closing the endpoint has not yet completed.
Read only.
endpoint.destroy([error])error {any}Forcefully closes the endpoint by forcing all open sessions to be immediately closed.
endpoint.destroyedTrue if endpoint.destroy() has been called. Read only.
endpoint.statsThe statistics collected for an active session. Read only.
endpoint[Symbol.asyncDispose]()Calls endpoint.close() and returns a promise that fulfills when the
endpoint has closed.
QuicEndpoint.StatsA view of the collected statistics for an endpoint.
endpointStats.createdAtendpointStats.destroyedAtendpointStats.bytesReceivedendpointStats.bytesSentendpointStats.packetsReceivedendpointStats.packetsSentendpointStats.serverSessionsendpointStats.clientSessionsendpointStats.serverBusyCountendpointStats.retryCountendpointStats.versionNegotiationCountendpointStats.statelessResetCountendpointStats.immediateCloseCountQuicSessionA QuicSession represents the local side of a QUIC connection.
session.close()Initiate a graceful close of the session. Existing streams will be allowed to complete but no new streams will be opened. Once all streams have closed, the session will be destroyed. The returned promise will be fulfilled once the session has been destroyed.
session.closedA promise that is fulfilled once the session is destroyed.
session.destroy([error])error {any}Immediately destroy the session. All streams will be destroys and the session will be closed.
session.destroyedTrue if session.destroy() has been called. Read only.
session.endpointThe endpoint that created this session. Read only.
session.onstreamThe callback to invoke when a new stream is initiated by a remote peer. Read/write.
session.ondatagramThe callback to invoke when a new datagram is received from a remote peer. Read/write.
session.ondatagramstatusThe callback to invoke when the status of a datagram is updated. Read/write.
session.onpathvalidationThe callback to invoke when the path validation is updated. Read/write.
session.onsessionticketThe callback to invoke when a new session ticket is received. Read/write.
session.onversionnegotiationThe callback to invoke when a version negotiation is initiated. Read/write.
session.onhandshakeThe callback to invoke when the TLS handshake is completed. Read/write.
session.createBidirectionalStream([options])options {Object}
body {ArrayBuffer | ArrayBufferView | Blob}sendOrder {number}Open a new bidirectional stream. If the body option is not specified,
the outgoing stream will be half-closed.
session.createUnidirectionalStream([options])options {Object}
body {ArrayBuffer | ArrayBufferView | Blob}sendOrder {number}Open a new unidirectional stream. If the body option is not specified,
the outgoing stream will be closed.
session.pathlocal {net.SocketAddress}remote {net.SocketAddress}The local and remote socket addresses associated with the session. Read only.
session.sendDatagram(datagram)datagram {string|ArrayBufferView}Sends an unreliable datagram to the remote peer, returning the datagram ID.
If the datagram payload is specified as an ArrayBufferView, then ownership of
that view will be transferred to the underlying stream.
session.statsReturn the current statistics for the session. Read only.
session.updateKey()Initiate a key update for the session.
session[Symbol.asyncDispose]()Calls session.close() and returns a promise that fulfills when the
session has closed.
QuicSession.StatssessionStats.createdAtsessionStats.closingAtsessionStats.handshakeCompletedAtsessionStats.handshakeConfirmedAtsessionStats.bytesReceivedsessionStats.bytesSentsessionStats.bidiInStreamCountsessionStats.bidiOutStreamCountsessionStats.uniInStreamCountsessionStats.uniOutStreamCountsessionStats.maxBytesInFlightssessionStats.bytesInFlightsessionStats.blockCountsessionStats.cwndsessionStats.latestRttsessionStats.minRttsessionStats.rttVarsessionStats.smoothedRttsessionStats.ssthreshsessionStats.datagramsReceivedsessionStats.datagramsSentsessionStats.datagramsAcknowledgedsessionStats.datagramsLostQuicStreamstream.closedA promise that is fulfilled when the stream is fully closed.
stream.destroy([error])error {any}Immediately and abruptly destroys the stream.
stream.destroyedTrue if stream.destroy() has been called.
stream.direction'bidi' or 'uni'.The directionality of the stream. Read only.
stream.idThe stream ID. Read only.
stream.onblockedThe callback to invoke when the stream is blocked. Read/write.
stream.onresetThe callback to invoke when the stream is reset. Read/write.
stream.readablestream.sessionThe session that created this stream. Read only.
stream.statsThe current statistics for the stream. Read only.
QuicStream.StatsstreamStats.ackedAtstreamStats.bytesReceivedstreamStats.bytesSentstreamStats.createdAtstreamStats.destroyedAtstreamStats.finalSizestreamStats.isConnectedstreamStats.maxOffsetstreamStats.maxOffsetAcknowledgedstreamStats.maxOffsetReceivedstreamStats.openedAtstreamStats.receivedAtEndpointOptionsThe endpoint configuration options passed when constructing a new QuicEndpoint instance.
endpointOptions.addressIf not specified the endpoint will bind to IPv4 localhost on a random port.
endpointOptions.addressLRUSizeThe endpoint maintains an internal cache of validated socket addresses as a performance optimization. This option sets the maximum number of addresses that are cache. This is an advanced option that users typically won't have need to specify.
endpointOptions.ipv6OnlyWhen true, indicates that the endpoint should bind only to IPv6 addresses.
endpointOptions.maxConnectionsPerHostSpecifies the maximum number of concurrent sessions allowed per remote peer address.
endpointOptions.maxConnectionsTotalSpecifies the maximum total number of concurrent sessions.
endpointOptions.maxRetriesSpecifies the maximum number of QUIC retry attempts allowed per remote peer address.
endpointOptions.maxStatelessResetsPerHostSpecifies the maximum number of stateless resets that are allowed per remote peer address.
endpointOptions.retryTokenExpirationSpecifies the length of time a QUIC retry token is considered valid.
endpointOptions.resetTokenSecretSpecifies the 16-byte secret used to generate QUIC retry tokens.
endpointOptions.tokenExpirationSpecifies the length of time a QUIC token is considered valid.
endpointOptions.tokenSecretSpecifies the 16-byte secret used to generate QUIC tokens.
endpointOptions.udpReceiveBufferSizeendpointOptions.udpSendBufferSizeendpointOptions.udpTTLendpointOptions.validateAddressWhen true, requires that the endpoint validate peer addresses using retry packets
while establishing a new connection.
SessionOptionssessionOptions.alpnThe ALPN protocol identifier.
sessionOptions.caThe CA certificates to use for sessions.
sessionOptions.ccSpecifies the congestion control algorithm that will be used
. Must be set to one of either 'reno', 'cubic', or 'bbr'.
This is an advanced option that users typically won't have need to specify.
sessionOptions.certsThe TLS certificates to use for sessions.
sessionOptions.ciphersThe list of supported TLS 1.3 cipher algorithms.
sessionOptions.crlThe CRL to use for sessions.
sessionOptions.groupsThe list of support TLS 1.3 cipher groups.
sessionOptions.keylogTrue to enable TLS keylogging output.
sessionOptions.keysThe TLS crypto keys to use for sessions.
sessionOptions.maxPayloadSizeSpecifies the maximum UDP packet payload size.
sessionOptions.maxStreamWindowSpecifies the maximum stream flow-control window size.
sessionOptions.maxWindowSpecifies the maximum session flow-control window size.
sessionOptions.minVersionThe minimum QUIC version number to allow. This is an advanced option that users typically won't have need to specify.
sessionOptions.preferredAddressPolicy'use', 'ignore', or 'default'.When the remote peer advertises a preferred address, this option specifies whether to use it or ignore it.
sessionOptions.qlogTrue if qlog output should be enabled.
sessionOptions.sessionTicketsessionOptions.handshakeTimeoutSpecifies the maximum number of milliseconds a TLS handshake is permitted to take to complete before timing out.
sessionOptions.sniThe peer server name to target.
sessionOptions.tlsTraceTrue to enable TLS tracing output.
sessionOptions.transportParamsThe QUIC transport parameters to use for the session.
sessionOptions.unacknowledgedPacketThresholdSpecifies the maximum number of unacknowledged packets a session should allow.
sessionOptions.verifyClientTrue to require verification of TLS client certificate.
sessionOptions.verifyPrivateKeyTrue to require private key verification.
sessionOptions.versionThe QUIC version number to use. This is an advanced option that users typically won't have need to specify.
TransportParamstransportParams.preferredAddressIpv4transportParams.preferredAddressIpv6transportParams.initialMaxStreamDataBidiLocaltransportParams.initialMaxStreamDataBidiRemotetransportParams.initialMaxStreamDataUnitransportParams.initialMaxDatatransportParams.initialMaxStreamsBiditransportParams.initialMaxStreamsUnitransportParams.maxIdleTimeouttransportParams.activeConnectionIDLimittransportParams.ackDelayExponenttransportParams.maxAckDelaytransportParams.maxDatagramFrameSizeOnSessionCallbackthis {quic.QuicEndpoint}session {quic.QuicSession}The callback function that is invoked when a new session is initiated by a remote peer.
OnStreamCallbackthis {quic.QuicSession}stream {quic.QuicStream}OnDatagramCallbackthis {quic.QuicSession}datagram {Uint8Array}early {boolean}OnDatagramStatusCallbackthis {quic.QuicSession}id {bigint}status {string} One of either 'lost' or 'acknowledged'.OnPathValidationCallbackthis {quic.QuicSession}result {string} One of either 'success', 'failure', or 'aborted'.newLocalAddress {net.SocketAddress}newRemoteAddress {net.SocketAddress}oldLocalAddress {net.SocketAddress}oldRemoteAddress {net.SocketAddress}preferredAddress {boolean}OnSessionTicketCallbackthis {quic.QuicSession}ticket {Object}OnVersionNegotiationCallbackthis {quic.QuicSession}version {number}requestedVersions {number[]}supportedVersions {number[]}OnHandshakeCallbackthis {quic.QuicSession}sni {string}alpn {string}cipher {string}cipherVersion {string}validationErrorReason {string}validationErrorCode {number}earlyDataAccepted {boolean}OnBlockedCallbackthis {quic.QuicStream}OnStreamErrorCallbackthis {quic.QuicStream}error {any}quic.endpoint.createdendpoint {quic.QuicEndpoint}config {quic.EndpointOptions}quic.endpoint.listenendpoint {quic.QuicEndpoint}optoins {quic.SessionOptions}quic.endpoint.closingendpoint {quic.QuicEndpoint}hasPendingError {boolean}quic.endpoint.closedendpoint {quic.QuicEndpoint}quic.endpoint.errorendpoint {quic.QuicEndpoint}error {any}quic.endpoint.busy.changeendpoint {quic.QuicEndpoint}busy {boolean}quic.session.created.clientquic.session.created.serverquic.session.open.streamquic.session.received.streamquic.session.send.datagramquic.session.update.keyquic.session.closingquic.session.closedquic.session.receive.datagramquic.session.receive.datagram.statusquic.session.path.validationquic.session.ticketquic.session.version.negotiationquic.session.handshake