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 });
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.
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;
The busy property is useful when the endpoint is under heavy load and needs to
temporarily reject new sessions while it catches up.
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.
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.
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.
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.
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.