Node.js v25.6.0 文档


QUIC#>

稳定性: 1.0 - 早期开发

源代码: lib/quic.js

'node:quic' 模块提供了 QUIC 协议的实现。要访问它,请使用 --experimental-quic 选项启动 Node.js,并执行以下操作:

🌐 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');

该模块仅在 node: 方案下可用。

🌐 The module is only available under the node: scheme.

quic.connect(address[, options])#>

启动新的客户端会话。

🌐 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'),
}); 

默认情况下,每次调用 connect(...) 都会创建一个新的本地 QuicEndpoint 实例,并绑定到一个随机的本地 IP 端口。要指定使用的确切本地地址,或者在单个本地端口上复用多个 QUIC 会话,请传递 endpoint 选项,其参数可以是 QuicEndpointEndpointOptions

🌐 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
  • 返回:<Promise> 对 quic.QuicEndpoint 的承诺

将端点配置为作为服务器监听。当远程端启动新会话时,将调用所提供的 onsession 回调,并传入创建的会话。

🌐 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(); 

默认情况下,每次调用 listen(...) 都会创建一个新的本地 QuicEndpoint 实例,并绑定到一个随机的本地 IP 端口。要指定使用的确切本地地址,或在单个本地端口上复用多个 QUIC 会话,请传递 endpoint 选项,其参数可以是 QuicEndpointEndpointOptions

🌐 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.

最多,任何单个 QuicEndpoint 只能被配置为以服务器身份监听一次。

🌐 At most, any single QuicEndpoint can only be configured to listen as a server once.

类:QuicEndpoint#>

🌐 Class: QuicEndpoint

QuicEndpoint 封装了 QUIC 的本地 UDP 端口绑定。它可以同时用作客户端和服务器。

🌐 A 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.address#>

端点绑定到的本地 UDP 套接字地址(如果有)。

🌐 The local UDP socket address to which the endpoint is bound, if any.

如果端点当前未绑定,则其值将为 undefined。只读。

🌐 If the endpoint is not currently bound then the value will be undefined. Read only.

endpoint.busy#>

endpoint.busy 设置为 true 时,终端将暂时拒绝创建新的会话。可读/可写。

🌐 When 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; 

busy 属性在终端负载很高且需要在赶上进度时暂时拒绝新会话时非常有用。

🌐 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.

返回一个在端点被销毁时实现的 promise。

🌐 Returns a promise that is fulfilled when the endpoint is destroyed.

endpoint.closed#>

当端点被销毁时会被 fulfillment 的承诺。这将与 endpoint.close() 函数返回的承诺相同。只读。

🌐 A 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.closing#>

如果已调用 endpoint.close() 且端点关闭尚未完成,则为 True。仅可读。

🌐 True if endpoint.close() has been called and closing the endpoint has not yet completed. Read only.

endpoint.destroy([error])#>

通过强制立即关闭所有打开的会话来强制关闭终端。

🌐 Forcefully closes the endpoint by forcing all open sessions to be immediately closed.

endpoint.destroyed#>

如果 endpoint.destroy() 已被调用,则为真。只读。

🌐 True if endpoint.destroy() has been called. Read only.

endpoint.stats#>

  • 类型:quic.QuicEndpoint.Stats

为活动会话收集的统计数据。只读。

🌐 The statistics collected for an active session. Read only.

endpoint[Symbol.asyncDispose]()#>

调用 endpoint.close() 并返回一个在端点关闭后完成的 Promise。

🌐 Calls endpoint.close() and returns a promise that fulfills when the endpoint has closed.

类:QuicEndpoint.Stats#>

🌐 Class: QuicEndpoint.Stats

端点收集的统计信息的视图。

🌐 A view of the collected statistics for an endpoint.

endpointStats.createdAt#>

  • 类型:<bigint> 一个指示端点创建时刻的时间戳。只读。

endpointStats.destroyedAt#>

  • 类型:<bigint> 一个指示端点被销毁时刻的时间戳。仅可读。

endpointStats.bytesReceived#>

  • 类型:<bigint> 此端点接收到的字节总数。只读。

endpointStats.bytesSent#>

  • 类型:<bigint> 此端点发送的字节总数。只读。

endpointStats.packetsReceived#>

  • 类型:<bigint> 此端点成功接收的 QUIC 数据包总数。只读。

endpointStats.packetsSent#>

  • 类型:<bigint> 此端点成功发送的 QUIC 数据包总数。只读。

endpointStats.serverSessions#>

  • 类型:<bigint> 该端点接收的对等端发起会话的总数。仅限读取。

endpointStats.clientSessions#>

  • 类型:<bigint> 此端点启动的会话总数。仅可读取。

endpointStats.serverBusyCount#>

  • 类型:<bigint> 由于端点被标记为忙而导致初始数据包被拒绝的总次数。只读。

endpointStats.retryCount#>

  • 类型:<bigint> 此端点上的 QUIC 重试尝试总数。仅可读。

endpointStats.versionNegotiationCount#>

  • 类型:<bigint> 由于 QUIC 版本不匹配而被拒绝的会话总数。只读。

endpointStats.statelessResetCount#>

  • 类型:<bigint> 此端点处理的无状态重置总数。仅限读取。

endpointStats.immediateCloseCount#>

  • 类型:<bigint> 握手结束前关闭的会话总数。仅限读取。

类:QuicSession#>

🌐 Class: QuicSession

QuicSession 表示 QUIC 连接的本地端。

🌐 A 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.closed#>

会话销毁后实现的 promise。

🌐 A promise that is fulfilled once the session is destroyed.

session.destroy([error])#>

立即销毁会话。所有流将被销毁,且会话将被关闭。

🌐 Immediately destroy the session. All streams will be destroys and the session will be closed.

session.destroyed#>

如果 session.destroy() 已被调用,则为真。只读。

🌐 True if session.destroy() has been called. Read only.

session.endpoint#>

  • 类型:quic.QuicEndpoint

创建此会话的端点。仅限读取。

🌐 The endpoint that created this session. Read only.

session.onstream#>

  • 类型:quic.OnStreamCallback

当远程端启动新流时要调用的回调。可读/写。

🌐 The callback to invoke when a new stream is initiated by a remote peer. Read/write.

session.ondatagram#>

  • 类型:quic.OnDatagramCallback

当从远程节点接收到新的数据报时要调用的回调。可读/可写。

🌐 The callback to invoke when a new datagram is received from a remote peer. Read/write.

session.ondatagramstatus#>

  • 类型:quic.OnDatagramStatusCallback

当数据报的状态更新时要调用的回调。可读写。

🌐 The callback to invoke when the status of a datagram is updated. Read/write.

session.onpathvalidation#>

  • 类型:quic.OnPathValidationCallback

路径验证更新时要调用的回调。可读写。

🌐 The callback to invoke when the path validation is updated. Read/write.

session.onsessionticket#>

  • 类型:quic.OnSessionTicketCallback

在收到新会话票证时调用的回调。可读/可写。

🌐 The callback to invoke when a new session ticket is received. Read/write.

session.onversionnegotiation#>

  • 类型:quic.OnVersionNegotiationCallback

当发起版本协商时要调用的回调函数。可读/可写。

🌐 The callback to invoke when a version negotiation is initiated. Read/write.

session.onhandshake#>

  • 类型:quic.OnHandshakeCallback

TLS 握手完成时调用的回调。可读/可写。

🌐 The callback to invoke when the TLS handshake is completed. Read/write.

session.createBidirectionalStream([options])#>

打开一个新的双向流。如果未指定 body 选项,发送方流将半关闭。

🌐 Open a new bidirectional stream. If the body option is not specified, the outgoing stream will be half-closed.

session.createUnidirectionalStream([options])#>

打开一个新的单向流。如果未指定 body 选项,输出流将被关闭。

🌐 Open a new unidirectional stream. If the body option is not specified, the outgoing stream will be closed.

session.path#>

与会话关联的本地和远程套接字地址。只读。

🌐 The local and remote socket addresses associated with the session. Read only.

session.sendDatagram(datagram)#>

向远程端发送不可靠的数据报,并返回数据报 ID。如果数据报的有效载荷指定为 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 transfered to the underlying stream.

session.stats#>

  • 类型:quic.QuicSession.Stats

返回当前会话的统计数据。仅可读取。

🌐 Return the current statistics for the session. Read only.

session.updateKey()#>

启动会话的密钥更新。

🌐 Initiate a key update for the session.

session[Symbol.asyncDispose]()#>

调用 session.close() 并返回一个在会话关闭后完成的 Promise。

🌐 Calls session.close() and returns a promise that fulfills when the session has closed.

类:QuicSession.Stats#>

🌐 Class: QuicSession.Stats

sessionStats.createdAt#>

sessionStats.closingAt#>

sessionStats.handshakeCompletedAt#>

sessionStats.handshakeConfirmedAt#>

sessionStats.bytesReceived#>

sessionStats.bytesSent#>

sessionStats.bidiInStreamCount#>

sessionStats.bidiOutStreamCount#>

sessionStats.uniInStreamCount#>

sessionStats.uniOutStreamCount#>

sessionStats.maxBytesInFlights#>

sessionStats.bytesInFlight#>

sessionStats.blockCount#>

sessionStats.cwnd#>

sessionStats.latestRtt#>

sessionStats.minRtt#>

sessionStats.rttVar#>

sessionStats.smoothedRtt#>

sessionStats.ssthresh#>

sessionStats.datagramsReceived#>

sessionStats.datagramsSent#>

sessionStats.datagramsAcknowledged#>

sessionStats.datagramsLost#>

类:QuicStream#>

🌐 Class: QuicStream

stream.closed#>

流完全关闭后实现的 promise。

🌐 A promise that is fulfilled when the stream is fully closed.

stream.destroy([error])#>

立即突然销毁流。

🌐 Immediately and abruptly destroys the stream.

stream.destroyed#>

如果已调用 stream.destroy(),则为真。

🌐 True if stream.destroy() has been called.

stream.direction#>

  • 类型:<string> 'bidi''uni' 中的一个。

流向。只读。

🌐 The directionality of the stream. Read only.

stream.id#>

流ID。只读。

🌐 The stream ID. Read only.

stream.onblocked#>

  • 类型:quic.OnBlockedCallback

当流被阻塞时要调用的回调。可读/可写。

🌐 The callback to invoke when the stream is blocked. Read/write.

stream.onreset#>

  • 类型:quic.OnStreamErrorCallback

当流被重置时要调用的回调。可读/可写。

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

stream.readable#>

stream.session#>

  • 类型:quic.QuicSession

创建此流的会话。只读。

🌐 The session that created this stream. Read only.

stream.stats#>

  • 类型:quic.QuicStream.Stats

当前流的数据统计。只读。

🌐 The current statistics for the stream. Read only.

类:QuicStream.Stats#>

🌐 Class: QuicStream.Stats

streamStats.ackedAt#>

streamStats.bytesReceived#>

streamStats.bytesSent#>

streamStats.createdAt#>

streamStats.destroyedAt#>

streamStats.finalSize#>

streamStats.isConnected#>

streamStats.maxOffset#>

streamStats.maxOffsetAcknowledged#>

streamStats.maxOffsetReceived#>

streamStats.openedAt#>

streamStats.receivedAt#>

类型#>

🌐 Types

类型:EndpointOptions#>

🌐 Type: EndpointOptions

在构建新的 QuicEndpoint 实例时传入的端点配置选项。

🌐 The endpoint configuration options passed when constructing a new QuicEndpoint instance.

endpointOptions.address#>

如果未指定,端点将绑定到 IPv4 的 localhost,并使用随机端口。

🌐 If not specified the endpoint will bind to IPv4 localhost on a random port.

endpointOptions.addressLRUSize#>

该端点维护一个经过验证的套接字地址的内部缓存以优化性能。此选项设置缓存的地址的最大数量。这个是高级选项,用户通常不需要指定。

🌐 The 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.ipv6Only#>

当为 true 时,表示该端点应仅绑定到 IPv6 地址。

🌐 When true, indicates that the endpoint should bind only to IPv6 addresses.

endpointOptions.maxConnectionsPerHost#>

指定每个远程对等地址允许的最大并发会话数。

🌐 Specifies the maximum number of concurrent sessions allowed per remote peer address.

endpointOptions.maxConnectionsTotal#>

指定最大并发会话总数。

🌐 Specifies the maximum total number of concurrent sessions.

endpointOptions.maxRetries#>

指定每个远程对等地址允许的最大 QUIC 重试次数。

🌐 Specifies the maximum number of QUIC retry attempts allowed per remote peer address.

endpointOptions.maxStatelessResetsPerHost#>

指定每个远程对等地址允许的最大无状态重置次数。

🌐 Specifies the maximum number of stateless resets that are allowed per remote peer address.

endpointOptions.retryTokenExpiration#>

指定 QUIC 重试令牌被视为有效的时间长度。

🌐 Specifies the length of time a QUIC retry token is considered valid.

endpointOptions.resetTokenSecret#>

指定用于生成 QUIC 重试令牌的 16 字节密钥。

🌐 Specifies the 16-byte secret used to generate QUIC retry tokens.

endpointOptions.tokenExpiration#>

指定 QUIC 令牌被视为有效的时间长度。

🌐 Specifies the length of time a QUIC token is considered valid.

endpointOptions.tokenSecret#>

指定用于生成 QUIC 令牌的 16 字节密钥。

🌐 Specifies the 16-byte secret used to generate QUIC tokens.

endpointOptions.udpReceiveBufferSize#>
endpointOptions.udpSendBufferSize#>
endpointOptions.udpTTL#>
endpointOptions.validateAddress#>

当为true时,要求端点在建立新连接时使用重试数据包来验证对等方地址。

🌐 When true, requires that the endpoint validate peer addresses using retry packets while establishing a new connection.

类型:SessionOptions#>

🌐 Type: SessionOptions

sessionOptions.alpn#>

ALPN 协议标识符。

🌐 The ALPN protocol identifier.

sessionOptions.ca#>

用于会话的 CA 证书。

🌐 The CA certificates to use for sessions.

sessionOptions.cc#>

指定将使用的拥塞控制算法。必须设置为 'reno''cubic''bbr' 中的一个。

🌐 Specifies 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.certs#>

用于会话的 TLS 证书。

🌐 The TLS certificates to use for sessions.

sessionOptions.ciphers#>

支持的 TLS 1.3 密码算法列表。

🌐 The list of supported TLS 1.3 cipher algorithms.

sessionOptions.crl#>

用于会话的 CRL。

🌐 The CRL to use for sessions.

sessionOptions.groups#>

支持 TLS 1.3 密码组的列表。

🌐 The list of support TLS 1.3 cipher groups.

sessionOptions.keylog#>

如果启用 TLS 键盘记录输出,则为 True。

🌐 True to enable TLS keylogging output.

sessionOptions.keys#>

用于会话的 TLS 加密密钥。

🌐 The TLS crypto keys to use for sessions.

sessionOptions.maxPayloadSize#>

指定最大 UDP 数据包有效负载大小。

🌐 Specifies the maximum UDP packet payload size.

sessionOptions.maxStreamWindow#>

指定最大流流量控制窗口大小。

🌐 Specifies the maximum stream flow-control window size.

sessionOptions.maxWindow#>

指定会话流控制窗口的最大大小。

🌐 Specifies the maximum session flow-control window size.

sessionOptions.minVersion#>

允许的最低 QUIC 版本号。这是一个高级选项,普通用户通常不需要指定。

🌐 The minimum QUIC version number to allow. This is an advanced option that users typically won't have need to specify.

sessionOptions.preferredAddressPolicy#>
  • 类型:<string> 'use''ignore''default' 之一。

当远程端广告首选地址时,此选项指定是使用它还是忽略它。

🌐 When the remote peer advertises a preferred address, this option specifies whether to use it or ignore it.

sessionOptions.qlog#>

如果应启用 qlog 输出,则为 True。

🌐 True if qlog output should be enabled.

sessionOptions.sessionTicket#>
sessionOptions.handshakeTimeout#>

指定 TLS 握手允许完成的最长毫秒数,超过此时间将超时。

🌐 Specifies the maximum number of milliseconds a TLS handshake is permitted to take to complete before timing out.

sessionOptions.sni#>

目标对等服务器名称。

🌐 The peer server name to target.

sessionOptions.tlsTrace#>

如果启用 TLS 跟踪输出,则为 True。

🌐 True to enable TLS tracing output.

sessionOptions.transportParams#>
  • 类型:quic.TransportParams

用于会话的 QUIC 传输参数。

🌐 The QUIC transport parameters to use for the session.

sessionOptions.unacknowledgedPacketThreshold#>

指定会话应允许的最大未确认数据包数。

🌐 Specifies the maximum number of unacknowledged packets a session should allow.

sessionOptions.verifyClient#>

如果要求验证 TLS 客户端证书,则为 True。

🌐 True to require verification of TLS client certificate.

sessionOptions.verifyPrivateKey#>

如果要求私钥验证,则为 True。

🌐 True to require private key verification.

sessionOptions.version#>

要使用的 QUIC 版本号。这是一个高级选项,普通用户通常不需要指定。

🌐 The QUIC version number to use. This is an advanced option that users typically won't have need to specify.

类型:TransportParams#>

🌐 Type: TransportParams

transportParams.preferredAddressIpv4#>
transportParams.preferredAddressIpv6#>
transportParams.initialMaxStreamDataBidiLocal#>
transportParams.initialMaxStreamDataBidiRemote#>
transportParams.initialMaxStreamDataUni#>
transportParams.initialMaxData#>
transportParams.initialMaxStreamsBidi#>
transportParams.initialMaxStreamsUni#>
transportParams.maxIdleTimeout#>
transportParams.activeConnectionIDLimit#>
transportParams.ackDelayExponent#>
transportParams.maxAckDelay#>
transportParams.maxDatagramFrameSize#>

回调#>

🌐 Callbacks

回调:OnSessionCallback#>

🌐 Callback: OnSessionCallback

  • this quic.QuicEndpoint
  • session quic.QuicSession

当远程对等方发起新会话时调用的回调函数。

🌐 The callback function that is invoked when a new session is initiated by a remote peer.

回调:OnStreamCallback#>

🌐 Callback: OnStreamCallback

  • this quic.QuicSession
  • stream quic.QuicStream

回调:OnDatagramCallback#>

🌐 Callback: OnDatagramCallback

回调:OnDatagramStatusCallback#>

🌐 Callback: OnDatagramStatusCallback

  • this quic.QuicSession
  • id <bigint>
  • status <string> 'lost''acknowledged' 中的一个。

回调:OnPathValidationCallback#>

🌐 Callback: OnPathValidationCallback

回调:OnSessionTicketCallback#>

🌐 Callback: OnSessionTicketCallback

回调:OnVersionNegotiationCallback#>

🌐 Callback: OnVersionNegotiationCallback

回调:OnHandshakeCallback#>

🌐 Callback: OnHandshakeCallback

回调:OnBlockedCallback#>

🌐 Callback: OnBlockedCallback

  • this quic.QuicStream

回调:OnStreamErrorCallback#>

🌐 Callback: OnStreamErrorCallback

  • this quic.QuicStream
  • error <any>

诊断通道#>

🌐 Diagnostic Channels

通道:quic.endpoint.created#>

🌐 Channel: quic.endpoint.created

  • endpoint quic.QuicEndpoint
  • config quic.EndpointOptions

通道:quic.endpoint.listen#>

🌐 Channel: quic.endpoint.listen

  • endpoint quic.QuicEndpoint
  • optoins quic.SessionOptions

通道:quic.endpoint.closing#>

🌐 Channel: quic.endpoint.closing

  • endpoint quic.QuicEndpoint
  • hasPendingError <boolean>

通道:quic.endpoint.closed#>

🌐 Channel: quic.endpoint.closed

  • endpoint quic.QuicEndpoint

通道:quic.endpoint.error#>

🌐 Channel: quic.endpoint.error

  • endpoint quic.QuicEndpoint
  • error <any>

通道:quic.endpoint.busy.change#>

🌐 Channel: quic.endpoint.busy.change

通道:quic.session.created.client#>

🌐 Channel: quic.session.created.client

通道:quic.session.created.server#>

🌐 Channel: quic.session.created.server

通道:quic.session.open.stream#>

🌐 Channel: quic.session.open.stream

通道:quic.session.received.stream#>

🌐 Channel: quic.session.received.stream

通道:quic.session.send.datagram#>

🌐 Channel: quic.session.send.datagram

通道:quic.session.update.key#>

🌐 Channel: quic.session.update.key

通道:quic.session.closing#>

🌐 Channel: quic.session.closing

通道:quic.session.closed#>

🌐 Channel: quic.session.closed

通道:quic.session.receive.datagram#>

🌐 Channel: quic.session.receive.datagram

通道:quic.session.receive.datagram.status#>

🌐 Channel: quic.session.receive.datagram.status

通道:quic.session.path.validation#>

🌐 Channel: quic.session.path.validation

通道:quic.session.ticket#>

🌐 Channel: quic.session.ticket

通道:quic.session.version.negotiation#>

🌐 Channel: quic.session.version.negotiation

通道:quic.session.handshake#>

🌐 Channel: quic.session.handshake

Node.js 中文网 - 粤ICP备13048890号