quic: add listEndpoints API · nodejs/node@147150c · GitHub
Skip to content

Commit 147150c

Browse files
jasnelladuh95
authored andcommitted
quic: add listEndpoints API
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #63536 Backport-PR-URL: #64675 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 3b59d12 commit 147150c

5 files changed

Lines changed: 161 additions & 43 deletions

File tree

doc/api/quic.md

Lines changed: 15 additions & 0 deletions

lib/internal/quic/quic.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/* c8 ignore start */
66

77
const {
8+
ArrayFrom,
89
ArrayIsArray,
910
ArrayPrototypePush,
1011
BigInt,
@@ -4737,6 +4738,24 @@ function findSuitableEndpoint(targetAddress) {
47374738
return undefined;
47384739
}
47394740

4741+
/**
4742+
* Returns a list of all active endpoints.
4743+
* @param {object} [options]
4744+
* @param {boolean} [options.active] When true, only return endpoints that are not destroyed, closing, or busy.
4745+
* @returns {QuicEndpoint[]}
4746+
*/
4747+
function listEndpoints(options = kEmptyObject) {
4748+
validateObject(options, 'options');
4749+
const { active = true } = options;
4750+
validateBoolean(active, 'options.active');
4751+
if (!active) {
4752+
return ArrayFrom(endpointRegistry);
4753+
}
4754+
return ArrayFrom(endpointRegistry).filter((endpoint) => {
4755+
return !endpoint.destroyed && !endpoint.closing && !endpoint.busy;
4756+
});
4757+
}
4758+
47404759
/**
47414760
* @param {EndpointOptions|QuicEndpoint|undefined} endpoint
47424761
* @param {boolean} reuseEndpoint
@@ -5340,6 +5359,7 @@ module.exports = {
53405359
getQuicStreamState,
53415360
getQuicSessionState,
53425361
getQuicEndpointState,
5362+
listEndpoints,
53435363
};
53445364

53455365
/* c8 ignore stop */

lib/quic.js

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
'use strict';
22

3-
const {
4-
ObjectCreate,
5-
ObjectSeal,
6-
} = primordials;
7-
83
const {
94
emitExperimentalWarning,
105
} = require('internal/util');
@@ -24,43 +19,32 @@ const {
2419
DEFAULT_GROUPS,
2520
} = require('internal/quic/quic');
2621

27-
function getEnumerableConstant(value) {
28-
return {
29-
__proto__: null,
30-
value,
31-
enumerable: true,
32-
configurable: false,
33-
writable: false,
34-
};
35-
}
36-
37-
const cc = ObjectSeal(ObjectCreate(null, {
38-
__proto__: null,
39-
RENO: getEnumerableConstant(CC_ALGO_RENO),
40-
CUBIC: getEnumerableConstant(CC_ALGO_CUBIC),
41-
BBR: getEnumerableConstant(CC_ALGO_BBR),
42-
}));
22+
const cc = {
23+
get RENO() { return CC_ALGO_RENO; },
24+
get CUBIC() { return CC_ALGO_CUBIC; },
25+
get BBR() { return CC_ALGO_BBR; },
26+
};
4327

44-
const constants = ObjectSeal(ObjectCreate(null, {
45-
__proto__: null,
46-
cc: getEnumerableConstant(cc),
47-
DEFAULT_CIPHERS: getEnumerableConstant(DEFAULT_CIPHERS),
48-
DEFAULT_GROUPS: getEnumerableConstant(DEFAULT_GROUPS),
49-
}));
28+
const constants = {
29+
get cc() { return cc; },
30+
get DEFAULT_CIPHERS() { return DEFAULT_CIPHERS; },
31+
get DEFAULT_GROUPS() { return DEFAULT_GROUPS; },
32+
};
5033

51-
module.exports = ObjectSeal(ObjectCreate(null, {
52-
__proto__: null,
53-
connect: getEnumerableConstant(connect),
54-
listen: getEnumerableConstant(listen),
55-
QuicEndpoint: getEnumerableConstant(QuicEndpoint),
56-
QuicError: getEnumerableConstant(QuicError),
57-
QuicSession: getEnumerableConstant(QuicSession),
58-
QuicStream: getEnumerableConstant(QuicStream),
59-
constants: getEnumerableConstant(constants),
34+
module.exports = {
35+
connect,
36+
listen,
37+
QuicEndpoint,
38+
QuicError,
39+
QuicSession,
40+
QuicStream,
41+
get constants() {
42+
return constants;
43+
},
6044

6145
CC_ALGO_RENO,
6246
CC_ALGO_CUBIC,
6347
CC_ALGO_BBR,
6448
DEFAULT_CIPHERS,
6549
DEFAULT_GROUPS,
66-
}));
50+
};

test/parallel/test-quic-exports.mjs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,5 @@ strictEqual(quic.constants.DEFAULT_GROUPS, 'X25519:P-256:P-384:P-521');
3535
throws(() => { quic.constants.cc.RENO = 'foo'; }, TypeError);
3636
strictEqual(quic.constants.cc.RENO, 'reno');
3737

38-
throws(() => { quic.constants.cc.NEW_CONSTANT = 'bar'; }, TypeError);
39-
strictEqual(quic.constants.cc.NEW_CONSTANT, undefined);
40-
4138
throws(() => { quic.constants.DEFAULT_CIPHERS = 123; }, TypeError);
4239
strictEqual(typeof quic.constants.DEFAULT_CIPHERS, 'string');
43-
44-
throws(() => { quic.constants.NEW_CONSTANT = 456; }, TypeError);
45-
strictEqual(quic.constants.NEW_CONSTANT, undefined);
Lines changed: 105 additions & 0 deletions

0 commit comments

Comments
 (0)