Skip to content

Commit 94372b1

Browse files
committed
quic: refactor/improve/document QuicSocket listening event
PR-URL: #34247 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent afc9390 commit 94372b1

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

doc/api/quic.md

+11
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,17 @@ Emitted before the `'close'` event if the `QuicSocket` was destroyed with an
13711371

13721372
The `'error'` event will not be emitted multiple times.
13731373

1374+
#### Event: `'listening'`
1375+
<!-- YAML
1376+
added: REPLACEME
1377+
-->
1378+
1379+
Emitted after `quicsocket.listen()` is called and the `QuicSocket` has started
1380+
listening for incoming `QuicServerSession`s. The callback is invoked with
1381+
no arguments.
1382+
1383+
The `'listening'` event will not be emitted multiple times.
1384+
13741385
#### Event: `'ready'`
13751386
<!-- YAML
13761387
added: REPLACEME

lib/internal/quic/core.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,13 @@ class QuicSocket extends EventEmitter {
11661166
port,
11671167
state.alpn,
11681168
options);
1169-
process.nextTick(emit.bind(this, 'listening'));
1169+
process.nextTick(() => {
1170+
try {
1171+
this.emit('listening');
1172+
} catch (error) {
1173+
this.destroy(error);
1174+
}
1175+
});
11701176
}
11711177

11721178
// When the QuicSocket listen() function is called, the first step is to bind
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Flags: --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasQuic)
6+
common.skip('missing quic');
7+
8+
const assert = require('assert');
9+
const {
10+
key,
11+
cert,
12+
ca,
13+
} = require('../common/quic');
14+
15+
const { createQuicSocket } = require('net');
16+
17+
const options = { key, cert, ca, alpn: 'zzz' };
18+
19+
const server = createQuicSocket({ server: options });
20+
21+
server.on('session', common.mustNotCall());
22+
23+
server.listen();
24+
25+
server.on('error', common.mustCall((error) => {
26+
assert.strictEqual(error.message, 'boom');
27+
}));
28+
29+
server.on('ready', common.mustCall());
30+
31+
server.on('listening', common.mustCall(async () => {
32+
throw new Error('boom');
33+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Flags: --no-warnings
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasQuic)
6+
common.skip('missing quic');
7+
8+
const assert = require('assert');
9+
const {
10+
key,
11+
cert,
12+
ca,
13+
} = require('../common/quic');
14+
15+
const { createQuicSocket } = require('net');
16+
17+
const options = { key, cert, ca, alpn: 'zzz' };
18+
19+
const server = createQuicSocket({ server: options });
20+
21+
server.on('session', common.mustNotCall());
22+
23+
server.listen();
24+
25+
server.on('error', common.mustCall((error) => {
26+
assert.strictEqual(error.message, 'boom');
27+
}));
28+
29+
server.on('ready', common.mustCall());
30+
31+
server.on('listening', common.mustCall(() => {
32+
throw new Error('boom');
33+
}));

0 commit comments

Comments
 (0)