-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathhelpers.mjs
More file actions
68 lines (58 loc) · 2.09 KB
/
helpers.mjs
File metadata and controls
68 lines (58 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Shared QUIC test helpers for session-level setup and teardown.
// Complements test-client.mjs and test-server.mjs (ngtcp2 binary wrappers).
import { hasQuic, skip, mustCall } from '../index.mjs';
import * as fixtures from '../fixtures.mjs';
/**
* Guard check. Skips the test if QUIC is not available.
* Call at the top of every QUIC test after imports.
*/
export function checkQuic() {
if (!hasQuic) {
skip('QUIC is not enabled');
}
}
/**
* Returns TLS credentials from test fixtures.
* @returns {{ keys: KeyObject, certs: Buffer }}
*/
export async function defaultCerts() {
const { createPrivateKey } = await import('node:crypto');
const keys = createPrivateKey(fixtures.readKey('agent1-key.pem'));
const certs = fixtures.readKey('agent1-cert.pem');
return { keys, certs };
}
/**
* Creates a connected client-server QUIC pair.
* Returns the endpoint, both sessions, and a cleanup function.
* @param {object} [options]
* @param {object} [options.serverOptions] - Additional options for listen().
* @param {object} [options.clientOptions] - Additional options for connect().
* @returns {Promise<{
* endpoint: QuicEndpoint,
* serverSession: QuicSession,
* clientSession: QuicSession,
* cleanup: () => Promise<void>
* }>}
*/
export async function createQuicPair(options = {}) {
const { listen, connect } = await import('node:quic');
const { keys, certs } = await defaultCerts();
const serverReady = Promise.withResolvers();
const endpoint = await listen(mustCall((session) => {
serverReady.resolve(session);
}), { keys, certs, ...options.serverOptions });
const clientSession = await connect(endpoint.address, options.clientOptions);
// Wait for both sides to complete the handshake.
const [serverSession] = await Promise.all([
serverReady.promise,
clientSession.opened,
]);
await serverSession.opened;
async function cleanup() {
clientSession.close();
serverSession.close();
await Promise.allSettled([clientSession.closed, serverSession.closed]);
await endpoint.close();
}
return { endpoint, serverSession, clientSession, cleanup };
}