Skip to content

Commit

Permalink
quic: additional API cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Nov 24, 2024
1 parent 036b9e2 commit c5e466e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
39 changes: 31 additions & 8 deletions lib/internal/quic/quic.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ const onEndpointServerSessionChannel = dc.channel('quic.session.created.server')
* @property {bigint|number} [handshakeTimeout] The handshake timeout
* @property {bigint|number} [maxStreamWindow] The maximum stream window
* @property {bigint|number} [maxWindow] The maximum window
* @property {number} [rxDiagnosticLoss] The receive diagnostic loss (range 0.0-1.0)
* @property {number} [txDiagnosticLoss] The transmit diagnostic loss (range 0.0-1.0)
* @property {number} [rxDiagnosticLoss] The receive diagnostic loss probability (range 0.0-1.0)
* @property {number} [txDiagnosticLoss] The transmit diagnostic loss probability (range 0.0-1.0)
* @property {number} [udpReceiveBufferSize] The UDP receive buffer size
* @property {number} [udpSendBufferSize] The UDP send buffer size
* @property {number} [udpTTL] The UDP TTL
Expand Down Expand Up @@ -395,6 +395,31 @@ const onEndpointServerSessionChannel = dc.channel('quic.session.created.server')
* @property {OnStreamErrorCallback} [onreset] The reset callback
* @property {OnHeadersCallback} [onheaders] The headers callback
* @property {OnTrailersCallback} [ontrailers] The trailers callback
* @property {SocketAddress} [address] The local address to bind to
* @property {bigint|number} [retryTokenExpiration] The retry token expiration
* @property {bigint|number} [tokenExpiration] The token expiration
* @property {bigint|number} [maxConnectionsPerHost] The maximum number of connections per host
* @property {bigint|number} [maxConnectionsTotal] The maximum number of total connections
* @property {bigint|number} [maxStatelessResetsPerHost] The maximum number of stateless resets per host
* @property {bigint|number} [addressLRUSize] The size of the address LRU cache
* @property {bigint|number} [maxRetries] The maximum number of retries
* @property {bigint|number} [maxPayloadSize] The maximum payload size
* @property {bigint|number} [unacknowledgedPacketThreshold] The unacknowledged packet threshold
* @property {bigint|number} [handshakeTimeout] The handshake timeout
* @property {bigint|number} [maxStreamWindow] The maximum stream window
* @property {bigint|number} [maxWindow] The maximum window
* @property {number} [rxDiagnosticLoss] The receive diagnostic loss probability (range 0.0-1.0)
* @property {number} [txDiagnosticLoss] The transmit diagnostic loss probability (range 0.0-1.0)
* @property {number} [udpReceiveBufferSize] The UDP receive buffer size
* @property {number} [udpSendBufferSize] The UDP send buffer size
* @property {number} [udpTTL] The UDP TTL
* @property {boolean} [noUdpPayloadSizeShaping] Disable UDP payload size shaping
* @property {boolean} [validateAddress] Validate the address
* @property {boolean} [disableActiveMigration] Disable active migration
* @property {boolean} [ipv6Only] Use IPv6 only
* @property {'reno'|'cubic'|'bbr'|number} [cc] The congestion control algorithm
* @property {ArrayBufferView} [resetTokenSecret] The reset token secret
* @property {ArrayBufferView} [tokenSecret] The token secret
*/

/**
Expand Down Expand Up @@ -1342,7 +1367,7 @@ class QuicEndpoint {
}

/**
* @param {EndpointOptions} options
* @param {EndpointCallbackConfiguration} options
* @returns {EndpointOptions}
*/
#processEndpointOptions(options) {
Expand Down Expand Up @@ -1421,9 +1446,8 @@ class QuicEndpoint {

/**
* @param {EndpointCallbackConfiguration} config
* @param {EndpointOptions} [options]
*/
constructor(config = kEmptyObject, options = kEmptyObject) {
constructor(config = kEmptyObject) {
const {
onsession,
session,
Expand All @@ -1437,15 +1461,15 @@ class QuicEndpoint {
}
this.#sessionConfig = session;

this.#handle = new Endpoint_(this.#processEndpointOptions(options));
this.#handle = new Endpoint_(this.#processEndpointOptions(config));
this.#handle[kOwner] = this;
this.#stats = new QuicEndpointStats(kPrivateConstructor, this.#handle.stats);
this.#state = new QuicEndpointState(kPrivateConstructor, this.#handle.state);

if (onEndpointCreatedChannel.hasSubscribers) {
onEndpointCreatedChannel.publish({
endpoint: this,
options,
config,
});
}
}
Expand Down Expand Up @@ -1618,7 +1642,6 @@ class QuicEndpoint {
throw new ERR_INVALID_ARG_VALUE('options.preferredAddressPolicy', policy);
}


/**
* @param {SessionOptions} options
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('quic internal endpoint listen defaults', { skip: !hasQuic }, async ()
it('are reasonable and work as expected', async () => {
const endpoint = new QuicEndpoint({
onsession() {},
}, {});
});

ok(!endpoint.state.isBound);
ok(!endpoint.state.isReceiving);
Expand Down
22 changes: 8 additions & 14 deletions test/parallel/test-quic-internal-endpoint-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ describe('quic internal endpoint options', { skip: !hasQuic }, async () => {
inspect,
} = require('util');

const callbackConfig = {
onsession() {},
};

it('invalid options', async () => {
['a', null, false, NaN].forEach((i) => {
throws(() => new QuicEndpoint(callbackConfig, i), {
throws(() => new QuicEndpoint(i), {
code: 'ERR_INVALID_ARG_TYPE',
});
});
});

it('valid options', async () => {
// Just Works... using all defaults
new QuicEndpoint(callbackConfig, {});
new QuicEndpoint(callbackConfig);
new QuicEndpoint(callbackConfig, undefined);
new QuicEndpoint();
});

it('various cases', async () => {
Expand Down Expand Up @@ -188,39 +182,39 @@ describe('quic internal endpoint options', { skip: !hasQuic }, async () => {
for (const value of valid) {
const options = {};
options[key] = value;
new QuicEndpoint(callbackConfig, options);
new QuicEndpoint(options);
}

for (const value of invalid) {
const options = {};
options[key] = value;
throws(() => new QuicEndpoint(callbackConfig, options), {
throws(() => new QuicEndpoint(options), {
code: 'ERR_INVALID_ARG_VALUE',
});
}
}
});

it('endpoint can be ref/unrefed without error', async () => {
const endpoint = new QuicEndpoint(callbackConfig, {});
const endpoint = new QuicEndpoint();
endpoint.unref();
endpoint.ref();
endpoint.close();
await endpoint.closed;
});

it('endpoint can be inspected', async () => {
const endpoint = new QuicEndpoint(callbackConfig, {});
const endpoint = new QuicEndpoint({});
strictEqual(typeof inspect(endpoint), 'string');
endpoint.close();
await endpoint.closed;
});

it('endpoint with object address', () => {
new QuicEndpoint(callbackConfig, {
new QuicEndpoint({
address: { host: '127.0.0.1:0' },
});
throws(() => new QuicEndpoint(callbackConfig, { address: '127.0.0.1:0' }), {
throws(() => new QuicEndpoint({ address: '127.0.0.1:0' }), {
code: 'ERR_INVALID_ARG_TYPE',
});
});
Expand Down
24 changes: 6 additions & 18 deletions test/parallel/test-quic-internal-endpoint-stats-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ describe('quic internal endpoint stats and state', { skip: !hasQuic }, () => {
} = require('node:assert');

it('endpoint state', () => {
const endpoint = new QuicEndpoint({
onsession() {},
});
const endpoint = new QuicEndpoint();

strictEqual(endpoint.state.isBound, false);
strictEqual(endpoint.state.isReceiving, false);
Expand Down Expand Up @@ -64,29 +62,23 @@ describe('quic internal endpoint stats and state', { skip: !hasQuic }, () => {
});

it('state is not readable after close', () => {
const endpoint = new QuicEndpoint({
onsession() {},
}, {});
const endpoint = new QuicEndpoint();
endpoint.state[kFinishClose]();
throws(() => endpoint.state.isBound, {
name: 'Error',
});
});

it('state constructor argument is ArrayBuffer', () => {
const endpoint = new QuicEndpoint({
onsession() {},
}, {});
const endpoint = new QuicEndpoint();
const Cons = endpoint.state.constructor;
throws(() => new Cons(kPrivateConstructor, 1), {
code: 'ERR_INVALID_ARG_TYPE'
});
});

it('endpoint stats', () => {
const endpoint = new QuicEndpoint({
onsession() {},
});
const endpoint = new QuicEndpoint();

strictEqual(typeof endpoint.stats.isConnected, 'boolean');
strictEqual(typeof endpoint.stats.createdAt, 'bigint');
Expand Down Expand Up @@ -126,9 +118,7 @@ describe('quic internal endpoint stats and state', { skip: !hasQuic }, () => {
});

it('stats are still readble after close', () => {
const endpoint = new QuicEndpoint({
onsession() {},
}, {});
const endpoint = new QuicEndpoint();
strictEqual(typeof endpoint.stats.toJSON(), 'object');
endpoint.stats[kFinishClose]();
strictEqual(endpoint.stats.isConnected, false);
Expand All @@ -137,9 +127,7 @@ describe('quic internal endpoint stats and state', { skip: !hasQuic }, () => {
});

it('stats constructor argument is ArrayBuffer', () => {
const endpoint = new QuicEndpoint({
onsession() {},
}, {});
const endpoint = new QuicEndpoint();
const Cons = endpoint.stats.constructor;
throws(() => new Cons(kPrivateConstructor, 1), {
code: 'ERR_INVALID_ARG_TYPE',
Expand Down

0 comments on commit c5e466e

Please sign in to comment.