Skip to content

Commit 86597db

Browse files
jasnellcjihrig
authored andcommitted
quic: minor reduction in code duplication
PR-URL: #34283 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent a307046 commit 86597db

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

doc/api/quic.md

+6
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ added: REPLACEME
255255
* `options` {Object}
256256
* `client` {Object} A default configuration for QUIC client sessions created
257257
using `quicsocket.connect()`.
258+
* `disableStatelessReset` {boolean} When `true` the `QuicSocket` will not
259+
send stateless resets. **Default**: `false`.
258260
* `endpoint` {Object} An object describing the local address to bind to.
259261
* `address` {string} The local address to bind to. This may be an IPv4 or
260262
IPv6 address or a host name. If a host name is given, it will be resolved
@@ -276,6 +278,10 @@ added: REPLACEME
276278
* `retryTokenTimeout` {number} The maximum number of *seconds* for retry token
277279
validation. Default: `10` seconds.
278280
* `server` {Object} A default configuration for QUIC server sessions.
281+
* `statelessResetSecret` {Buffer|Uint8Array} A 16-byte `Buffer` or
282+
`Uint8Array` providing the secret to use when generating stateless reset
283+
tokens. If not specified, a random secret will be generated for the
284+
`QuicSocket`. **Default**: `undefined`.
279285
* `validateAddress` {boolean} When `true`, the `QuicSocket` will use explicit
280286
address validation using a QUIC `RETRY` frame when listening for new server
281287
sessions. Default: `false`.

lib/internal/quic/core.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ function getStats(obj, idx) {
581581
return stats[idx];
582582
}
583583

584+
function addressOrLocalhost(address, type) {
585+
return address || (type === AF_INET6 ? '::' : '0.0.0.0');
586+
}
587+
588+
function lookupOrDefault(lookup, type) {
589+
return lookup || (type === AF_INET6 ? lookup6 : lookup4);
590+
}
591+
584592
// QuicEndpoint wraps a UDP socket and is owned
585593
// by a QuicSocket. It does not exist independently
586594
// of the QuicSocket.
@@ -610,9 +618,9 @@ class QuicEndpoint {
610618
} = validateQuicEndpointOptions(options);
611619
const state = this[kInternalState];
612620
state.socket = socket;
613-
state.address = address || (type === AF_INET6 ? '::' : '0.0.0.0');
621+
state.address = addressOrLocalhost(address, type);
622+
state.lookup = lookupOrDefault(lookup, type);
614623
state.ipv6Only = ipv6Only;
615-
state.lookup = lookup || (type === AF_INET6 ? lookup6 : lookup4);
616624
state.port = port;
617625
state.reuseAddr = reuseAddr;
618626
state.type = type;
@@ -901,8 +909,8 @@ class QuicSocket extends EventEmitter {
901909
const state = this[kInternalState];
902910

903911
state.client = client;
904-
state.lookup = lookup || (type === AF_INET6 ? lookup6 : lookup4);
905912
state.server = server;
913+
state.lookup = lookupOrDefault(lookup, type);
906914

907915
let socketOptions = 0;
908916
if (validateAddress)
@@ -1286,7 +1294,7 @@ class QuicSocket extends EventEmitter {
12861294
// Notice here that connectAfterLookup is bound to the QuicSession
12871295
// that was created...
12881296
lookup(
1289-
address || (type === AF_INET6 ? '::' : '0.0.0.0'),
1297+
addressOrLocalhost(address, type),
12901298
connectAfterLookup.bind(session, type));
12911299
}
12921300

0 commit comments

Comments
 (0)