Skip to content

Commit fad3544

Browse files
committed
quic: add QuicEndpoint.listening & QuicStream.destroy() and tests
Starting to explore and cover the existing implementation, this covers the basic endpoint & stream lifecycle and the exposed properties. Added endpoint.listening to match net.Server and round out endpoint properties, and stream.destroy() which is already called by quicSession.destroy() and documented, but didn't actually exist. Signed-off-by: Tim Perry <pimterry@gmail.com>
1 parent f8b79a1 commit fad3544

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

doc/api/quic.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ added: v23.8.0
204204

205205
True if `endpoint.destroy()` has been called. Read only.
206206

207+
### `endpoint.listening`
208+
209+
* Type: {boolean}
210+
211+
True if the endpoint is actively listening for incoming connections. Read only.
212+
207213
### `endpoint.setSNIContexts(entries[, options])`
208214

209215
<!-- YAML

lib/internal/quic/quic.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,20 @@ class QuicStream {
808808
return this.#pendingClose.promise;
809809
}
810810

811+
/**
812+
* Immediately destroys the stream. Any queued data is discarded. If an
813+
* error is given, the closed promise will be rejected with that error.
814+
* If no error is given, the closed promise will be resolved.
815+
* @param {any} error
816+
*/
817+
destroy(error) {
818+
QuicStream.#assertIsQuicStream(this);
819+
if (this.destroyed) return;
820+
const handle = this.#handle;
821+
this[kFinishClose](error);
822+
handle.destroy();
823+
}
824+
811825
/**
812826
* Sets the outbound data source for the stream. This can only be called
813827
* once and must be called before any data will be sent. The body can be
@@ -1930,6 +1944,12 @@ class QuicEndpoint {
19301944
return this.#isPendingClose;
19311945
}
19321946

1947+
/** @type {boolean} */
1948+
get listening() {
1949+
QuicEndpoint.#assertIsQuicEndpoint(this);
1950+
return this.#listening;
1951+
}
1952+
19331953
/** @type {boolean} */
19341954
get destroyed() {
19351955
QuicEndpoint.#assertIsQuicEndpoint(this);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Flags: --experimental-quic --no-warnings
2+
3+
import { hasQuic, skip, mustCall } from '../common/index.mjs';
4+
import assert from 'node:assert';
5+
import * as fixtures from '../common/fixtures.mjs';
6+
7+
if (!hasQuic) {
8+
skip('QUIC is not enabled');
9+
}
10+
11+
// Import after the hasQuic check
12+
const quic = await import('node:quic');
13+
const { createPrivateKey } = await import('node:crypto');
14+
15+
const keys = createPrivateKey(fixtures.readKey('agent1-key.pem'));
16+
const certs = fixtures.readKey('agent1-cert.pem');
17+
18+
const serverDone = Promise.withResolvers();
19+
const clientDone = Promise.withResolvers();
20+
21+
// Create a server endpoint
22+
const serverEndpoint = await quic.listen(mustCall((serverSession) => {
23+
serverSession.opened.then((info) => {
24+
assert.ok(serverSession.endpoint !== null);
25+
assert.strictEqual(serverSession.destroyed, false);
26+
27+
const stats = serverSession.stats;
28+
assert.strictEqual(stats.isConnected, true);
29+
assert.ok(stats.handshakeCompletedAt > 0n);
30+
assert.ok(stats.handshakeConfirmedAt > 0n);
31+
assert.strictEqual(stats.closingAt, 0n);
32+
33+
serverDone.resolve();
34+
serverSession.close();
35+
}).then(mustCall());
36+
}), { sni: { '*': { keys, certs } } });
37+
38+
assert.strictEqual(serverEndpoint.busy, false);
39+
assert.strictEqual(serverEndpoint.closing, false);
40+
assert.strictEqual(serverEndpoint.destroyed, false);
41+
assert.strictEqual(serverEndpoint.listening, true);
42+
43+
assert.ok(serverEndpoint.address !== undefined);
44+
assert.strictEqual(serverEndpoint.address.family, 'ipv4');
45+
assert.strictEqual(serverEndpoint.address.address, '127.0.0.1');
46+
assert.ok(typeof serverEndpoint.address.port === 'number');
47+
assert.ok(serverEndpoint.address.port > 0);
48+
49+
const epStats = serverEndpoint.stats;
50+
assert.strictEqual(epStats.isConnected, true);
51+
assert.ok(epStats.createdAt > 0n);
52+
53+
// Connect with a client
54+
const clientSession = await quic.connect(serverEndpoint.address);
55+
56+
assert.strictEqual(clientSession.destroyed, false);
57+
assert.ok(clientSession.endpoint !== null);
58+
assert.strictEqual(clientSession.stats.isConnected, true);
59+
60+
clientSession.opened.then((clientInfo) => {
61+
assert.strictEqual(clientInfo.servername, 'localhost');
62+
assert.strictEqual(clientInfo.protocol, 'h3');
63+
assert.strictEqual(clientInfo.cipherVersion, 'TLSv1.3');
64+
assert.ok(clientInfo.local !== undefined);
65+
assert.ok(clientInfo.remote !== undefined);
66+
67+
const cStats = clientSession.stats;
68+
assert.strictEqual(cStats.isConnected, true);
69+
assert.ok(cStats.handshakeCompletedAt > 0n);
70+
assert.ok(cStats.bytesSent > 0n, 'Expected bytesSent > 0 after handshake');
71+
72+
clientDone.resolve();
73+
}).then(mustCall());
74+
75+
await Promise.all([serverDone.promise, clientDone.promise]);
76+
77+
// Open a bidirectional stream.
78+
const stream = await clientSession.createBidirectionalStream();
79+
80+
assert.strictEqual(stream.destroyed, false);
81+
assert.strictEqual(stream.direction, 'bidi');
82+
assert.strictEqual(stream.session, clientSession);
83+
assert.ok(stream.id !== null, 'Non-pending stream should have an id');
84+
assert.strictEqual(typeof stream.id, 'bigint');
85+
assert.strictEqual(stream.pending, false);
86+
assert.strictEqual(stream.stats.isConnected, true);
87+
assert.ok(stream.readable instanceof ReadableStream);
88+
89+
// Destroying the session should destroy it and the stream, and clear its properties.
90+
clientSession.destroy();
91+
assert.strictEqual(clientSession.destroyed, true);
92+
assert.strictEqual(clientSession.endpoint, null);
93+
assert.strictEqual(clientSession.stats.isConnected, false);
94+
95+
assert.strictEqual(stream.destroyed, true);
96+
assert.strictEqual(stream.session, null);
97+
assert.strictEqual(stream.id, null);
98+
assert.strictEqual(stream.direction, null);

0 commit comments

Comments
 (0)