@amritk/nish-aarch64-linux 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/INSTALL.md +73 -59
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +50 -4
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/nish.d.ts +4849 -430
  8. package/runtime/nish.h +25 -0
  9. package/runtime/page.d.ts +53 -0
  10. package/runtime/page.mjs +146 -0
  11. package/runtime/runtime-async.c +557 -0
  12. package/runtime/runtime-wasm.c +63 -7
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +252 -15
  16. package/std/README.md +107 -82
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +279 -59
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +128 -52
  28. package/std/crypto/base64url.ts +29 -5
  29. package/std/crypto/bcrypt-batch.ts +101 -34
  30. package/std/crypto/bcrypt.ts +93 -27
  31. package/std/crypto/chacha20poly1305.ts +69 -14
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +66 -16
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +242 -37
  36. package/std/crypto/jwt.ts +204 -72
  37. package/std/crypto/p256.ts +222 -72
  38. package/std/crypto/p384.ts +51 -23
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +99 -30
  41. package/std/crypto/rsa.ts +56 -22
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +163 -24
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +133 -66
  48. package/std/host/ring.ts +124 -0
  49. package/std/json-equal.ts +31 -11
  50. package/std/json-format.ts +112 -13
  51. package/std/json-number.ts +76 -26
  52. package/std/json-pointer.ts +23 -1
  53. package/std/json-tokenizer.ts +126 -51
  54. package/std/json-typed.ts +757 -96
  55. package/std/json-value.ts +438 -157
  56. package/std/json.ts +170 -38
  57. package/std/map.ts +25 -7
  58. package/std/math.ts +36 -4
  59. package/std/net/buffer-pool.ts +42 -11
  60. package/std/net/dns-config.ts +109 -23
  61. package/std/net/dns-message.ts +62 -7
  62. package/std/net/dns-resolver.ts +247 -53
  63. package/std/net/dns.ts +203 -35
  64. package/std/net/fetch.ts +1267 -290
  65. package/std/net/handler.ts +296 -80
  66. package/std/net/hpack.ts +290 -77
  67. package/std/net/http-compress.ts +152 -36
  68. package/std/net/http-fields.ts +85 -9
  69. package/std/net/http1-client-codec.ts +129 -44
  70. package/std/net/http1-codec.ts +279 -114
  71. package/std/net/http1-server.ts +409 -139
  72. package/std/net/http2-frame.ts +289 -51
  73. package/std/net/http2-tls.ts +152 -49
  74. package/std/net/http2.ts +314 -160
  75. package/std/net/http3-frame.ts +165 -27
  76. package/std/net/http3-server.ts +282 -131
  77. package/std/net/http3.ts +377 -173
  78. package/std/net/outbound.ts +37 -3
  79. package/std/net/postgres-carrier.ts +144 -8
  80. package/std/net/postgres.ts +898 -260
  81. package/std/net/prefork.ts +202 -65
  82. package/std/net/qpack.ts +120 -32
  83. package/std/net/quic-conn-ack.ts +60 -12
  84. package/std/net/quic-conn-cid.ts +135 -27
  85. package/std/net/quic-conn-params.ts +109 -22
  86. package/std/net/quic-datagram.ts +50 -3
  87. package/std/net/quic-frame.ts +333 -23
  88. package/std/net/quic-listener.ts +161 -75
  89. package/std/net/quic-packet.ts +373 -73
  90. package/std/net/quic-recovery.ts +287 -65
  91. package/std/net/quic-stream.ts +436 -146
  92. package/std/net/quic.ts +693 -323
  93. package/std/net/router.ts +87 -40
  94. package/std/net/tls/client.ts +167 -51
  95. package/std/net/tls/codec.ts +494 -68
  96. package/std/net/tls/record-client.ts +140 -73
  97. package/std/net/tls/record-server.ts +233 -105
  98. package/std/net/tls/record.ts +142 -36
  99. package/std/net/tls/schedule.ts +235 -26
  100. package/std/net/tls/ticket.ts +161 -40
  101. package/std/net/tls/verify.ts +57 -10
  102. package/std/net/tls-tcp.ts +308 -95
  103. package/std/net/tls.ts +276 -102
  104. package/std/net/upstream.ts +81 -45
  105. package/std/net/url.ts +109 -22
  106. package/std/net/websocket.ts +140 -26
  107. package/std/net/webtransport.ts +243 -106
  108. package/std/pair.ts +10 -2
  109. package/std/random.ts +42 -15
  110. package/std/regex.ts +422 -257
  111. package/std/region.ts +70 -15
  112. package/std/secret.ts +52 -11
  113. package/std/sort.ts +17 -2
  114. package/std/sqlite.ts +256 -35
  115. package/std/sync/ring.ts +229 -56
  116. package/std/sync/shm.ts +203 -23
  117. package/std/term-io.ts +154 -78
  118. package/std/term-pixels.ts +144 -19
  119. package/std/term-probe.ts +77 -35
  120. package/std/term-rate.ts +199 -118
  121. package/std/term.ts +400 -175
  122. package/std/testing.ts +130 -32
  123. package/std/text.ts +84 -17
  124. package/std/threads.ts +248 -56
  125. package/std/yaml-json.ts +111 -97
  126. package/std/yaml-lines.ts +47 -15
  127. package/std/yaml-write.ts +45 -30
  128. package/std/yaml.ts +764 -384
package/std/net/quic.ts CHANGED
@@ -7,11 +7,15 @@
7
7
  * Sans-IO, like the rest of `nish/net`.
8
8
  *
9
9
  * import { QuicConnection, QuicServerConfig } from "nish/net/quic";
10
+ * import { tlsSignEcdsaP256 } from "nish/net/tls";
10
11
  *
11
12
  * const conn = new QuicConnection(config, entropy); // entropy: QUIC_CONN_ENTROPY_SIZE random bytes
12
13
  * conn.receiveWindow(buf, off, len, now); // every datagram the client sends; now in ms
13
14
  * const input: u8[] | null = conn.signatureInput();
14
- * if (input !== null) { conn.sign(tlsSignEcdsaP256(key, input)); }
15
+ * if (input !== null) {
16
+ * const signature: u8[] | null = tlsSignEcdsaP256(key, input); // key: Secret<u8[]>
17
+ * if (signature !== null) { conn.sign(signature); }
18
+ * }
15
19
  * let n: i32 = conn.takeDatagramInto(out, 0, now); // out: at least QUIC_CONN_DATAGRAM_SIZE bytes
16
20
  * while (n > 0) { … send out[0 .. n) to the client … ; n = conn.takeDatagramInto(out, 0, now); }
17
21
  * let id: i64 = conn.nextStreamEvent(); // a stream with news: read it, write it
@@ -30,7 +34,7 @@
30
34
  * client's are parsed and checked (RFC 9000 §7.3, §7.4) before the server's
31
35
  * flight leaves.
32
36
  *
33
- * **Streams** are `nish/net/quic-stream`'s: both kinds, opened by either
37
+ * **Streams.** They are `nish/net/quic-stream`'s: both kinds, opened by either
34
38
  * side, with flow control per stream and for the connection raised as the
35
39
  * application reads, and the stream limits raised as streams finish. The
36
40
  * application sees them through a small surface: `nextStreamEvent` names a
@@ -48,91 +52,100 @@
48
52
  * congestion window, and is never sent again. `readDatagram` hands out what
49
53
  * arrived. Each way is a fixed ring (`nish/net/quic-datagram`).
50
54
  *
51
- * **Loss recovery** (RFC 9002) is `nish/net/quic-recovery`'s: every
55
+ * **Loss recovery** (RFC 9002). It is `nish/net/quic-recovery`'s: every
52
56
  * ack-eliciting packet is recorded in its space's ring with what it carried
53
- * (its CRYPTO range, its STREAM chunks, HANDSHAKE_DONE and the control
54
- * frames), the client's ACKs give the RTT estimate and show what was lost by
55
- * packet or time threshold, and what a lost packet carried is sent again,
56
- * from the CRYPTO bytes a level keeps until it is discarded and the bytes a
57
- * stream keeps until they are acknowledged; a control frame goes again with
58
- * its current value. When nothing is acknowledged for a probe timeout, with
59
- * its backoff, everything in flight is queued again and a probe goes out in
60
- * each space that has packets in flight, a PING if there is nothing to
61
- * resend (§6.2.4) — two datagrams of them during the handshake, each with
62
- * the oldest CRYPTO data not acknowledged. A client that shows it lacks the
63
- * server's handshake data (an Initial repeating CRYPTO data, an
64
- * ack-eliciting Handshake packet) gets it again at once, a datagram each
65
- * time, up to `QUIC_CONN_EARLY_RESENDS` times (§6.2.3). NewReno's window
66
- * holds back everything ack-eliciting but a probe; an ACK always goes.
67
- * PATH_RESPONSE and DATAGRAM are never sent again (RFC 9000 §13.3, RFC 9221
68
- * §5.2). Pacing, and GSO batching, are the carrier's:
69
- * `nish/net/quic-listener`. Once the 1-RTT keys are in, one Application
70
- * Data packet number in every 128 to 1024 is skipped, at a distance drawn
71
- * from the ID seed, and the last `QUIC_CONN_SKIP_RING` are remembered: an
72
- * ACK of one acknowledges a packet never sent, an optimistic ACK meant to
73
- * widen the congestion window, and is PROTOCOL_VIOLATION (RFC 9000 §21.4).
57
+ * (its CRYPTO range, its STREAM chunks, HANDSHAKE_DONE and the control frames),
58
+ * the client's ACKs give the RTT estimate and show what was lost by packet or
59
+ * time threshold, and what a lost packet carried is sent again, from the CRYPTO
60
+ * bytes a level keeps until it is discarded and the bytes a stream keeps until
61
+ * they are acknowledged; a control frame goes again with its current value.
62
+ *
63
+ * When nothing is acknowledged for a probe timeout, with its backoff,
64
+ * everything in flight is queued again and a probe goes out in each space that
65
+ * has packets in flight, a PING if there is nothing to resend (§6.2.4) — two
66
+ * datagrams of them during the handshake, each with the oldest CRYPTO data not
67
+ * acknowledged. A client that shows it lacks the server's handshake data (an
68
+ * Initial repeating CRYPTO data, an ack-eliciting Handshake packet) gets it
69
+ * again at once, a datagram each time, up to `QUIC_CONN_EARLY_RESENDS` times
70
+ * (§6.2.3).
71
+ *
72
+ * NewReno's window holds back everything ack-eliciting but a probe; an ACK
73
+ * always goes. PATH_RESPONSE and DATAGRAM are never sent again (RFC 9000 §13.3,
74
+ * RFC 9221 §5.2). Pacing, and GSO batching, are the carrier's:
75
+ * `nish/net/quic-listener`. Once the 1-RTT keys are in, one Application Data
76
+ * packet number in every 128 to 1024 is skipped, at a distance drawn from the
77
+ * ID seed, and the last `QUIC_CONN_SKIP_RING` are remembered: an ACK of one
78
+ * acknowledges a packet never sent, an optimistic ACK meant to widen the
79
+ * congestion window, and is PROTOCOL_VIOLATION (RFC 9000 §21.4).
74
80
  *
75
81
  * **Nothing allocated per packet** (QUIC-3). A connection is a slot: every
76
- * buffer it uses — the packet record, the CRYPTO reassembly, the
77
- * connection-ID table, the datagram rings — is made by the constructor and
78
- * reused, and `reset` makes the slot a new connection for the next peer. The
79
- * stream buffers are borrowed instead (`QuicStreamPools` in
80
- * `nish/net/quic-stream`): a receive side takes one on its first byte and a
81
- * send side on its first write, each goes back zeroed once the stream is done
82
- * with it, and a server's connections share one set of pools, so idle slots
83
- * hold none. A standalone connection makes private pools as large as all its
84
- * slots at its first packet. The pools are primed before a packet's arena
85
- * block and never touched inside it. A packet is opened (`receiveWindow`) and a datagram built
86
- * and sealed (`takeDatagramInto`) inside a `using a = arena()` block, so the
87
- * AEAD's and the header protection's temporaries go when the block ends, and
88
- * the compiler refuses the block if anything in it could store one. The
89
- * handshake keeps its state in the slot too: one `TlsServer`, made by the
90
- * slot's first handshake and `restart`ed by each after it; every level's
91
- * packet keys, and a key update's, derived in place into `QuicKeysSlot`s
92
- * (`nish/net/quic-packet`) over the slot's HKDF scratch; the Initial
93
- * secrets, the stateless reset tokens and the connection IDs computed in
94
- * arena blocks; the transport parameters encoded and parsed into the
95
- * slot's own objects. After the first connection in a slot, a connection
96
- * keeps nothing at all (QUIC-3 in `docs/security/quic.md`).
82
+ * buffer it uses — the packet record, the CRYPTO reassembly, the connection-ID
83
+ * table, the datagram rings — is made by the constructor and reused, and
84
+ * `reset` makes the slot a new connection for the next peer.
85
+ *
86
+ * The stream buffers are borrowed instead (`QuicStreamPools` in
87
+ * `nish/net/quic-stream`): a receive side takes one on its first byte and a
88
+ * send side on its first write, each goes back zeroed once the stream is done
89
+ * with it, and a server's connections share one set of pools, so idle slots
90
+ * hold none. A standalone connection makes private pools as large as all its
91
+ * slots at its first packet. The pools are primed before a packet's arena block
92
+ * and never touched inside it.
93
+ *
94
+ * A packet is opened (`receiveWindow`) and a datagram built and sealed
95
+ * (`takeDatagramInto`) inside a `using a = arena()` block, so the AEAD's and
96
+ * the header protection's temporaries go when the block ends, and the compiler
97
+ * refuses the block if anything in it could store one.
98
+ *
99
+ * The handshake keeps its state in the slot too: one `TlsServer`, made by the
100
+ * slot's first handshake and `restart`ed by each after it; every level's packet
101
+ * keys, and a key update's, derived in place into `QuicKeysSlot`s
102
+ * (`nish/net/quic-packet`) over the slot's HKDF scratch; the Initial secrets,
103
+ * the stateless reset tokens and the connection IDs computed in arena blocks;
104
+ * the transport parameters encoded and parsed into the slot's own objects.
105
+ * After the first connection in a slot, a connection keeps nothing at all
106
+ * (QUIC-3 in `docs/security/quic.md`).
97
107
  *
98
108
  * **Time.** The connection has no clock: `receive`, `takeDatagram` and
99
109
  * `handleTimer` take the caller's monotonic time in milliseconds, and
100
- * `deadline()` says when `handleTimer` is next due. Five things run on it.
101
- * The idle timeout (RFC 9000 §10.1) is the smaller of the two sides'
110
+ * `deadline()` says when `handleTimer` is next due. Five things run on it. The
111
+ * idle timeout (RFC 9000 §10.1) is the smaller of the two sides'
102
112
  * `max_idle_timeout`, never under `QUIC_CONN_IDLE_FLOOR` or three probe
103
113
  * timeouts; once it passes with nothing received, the connection closes
104
- * silently (`QUIC_STATE_TIMED_OUT`) and wipes its keys. Loss recovery's
105
- * timer declares packets lost by the time threshold or fires the probe
106
- * timeout. After a key update the previous read keys are kept for a
107
- * probe timeout (`recovery.probeTimeout()`), for packets the network
108
- * reordered, before the next generation's are derived (RFC 9001 §6.5). And
109
- * while a new path is validated, its PATH_CHALLENGE goes again a probe
110
- * timeout after the last, and validation gives up max(3 PTO, 6 kInitialRtt)
111
- * after the move (RFC 9000 §8.2.4, §9.3.2). Once
112
- * the connection has closed, by either side, its keys are kept through the
113
- * closing or draining period, three probe timeouts (RFC 9000 §10.2), and
114
- * wiped at its end; `deadline()` is that end, then -1. A close this side
115
- * owes starts the period when `takeDatagram` sends it, not when `close` or
116
- * `fail` is called, so that however long the caller waits, the close still
117
- * goes out.
114
+ * silently (`QUIC_STATE_TIMED_OUT`) and wipes its keys. Loss recovery's timer
115
+ * declares packets lost by the time threshold or fires the probe timeout.
116
+ *
117
+ * After a key update the previous read keys are kept for a probe timeout
118
+ * (`recovery.probeTimeout()`), for packets the network reordered, before the
119
+ * next generation's are derived (RFC 9001 §6.5). And while a new path is
120
+ * validated, its PATH_CHALLENGE goes again a probe timeout after the last, and
121
+ * validation gives up max(3 PTO, 6 kInitialRtt) after the move (RFC 9000
122
+ * §8.2.4, §9.3.2).
123
+ *
124
+ * Once the connection has closed, by either side, its keys are kept through the
125
+ * closing or draining period, three probe timeouts (RFC 9000 §10.2), and wiped
126
+ * at its end; `deadline()` is that end, then -1. A close this side owes starts
127
+ * the period when `takeDatagram` sends it, not when `close` or `fail` is
128
+ * called, so that however long the caller waits, the close still goes out.
118
129
  *
119
- * **Key update** (RFC 9001 §6), both ways. A 1-RTT packet whose Key Phase
120
- * bit differs from the current one is opened with the next generation's read
121
- * keys, which are derived ahead of time so that a forged bit costs no
122
- * derivation and shows no timing difference (§6.3); if it opens, the client
123
- * has updated, and the server's write keys follow before anything is
124
- * acknowledged (§6.2). `updateKeys()` starts an update from the server once
125
- * the client has acknowledged a packet of the current phase (§6.1). The
126
- * generations take turns in two key slots and two secrets a direction, so an
127
- * update keeps nothing in the arena; each still costs two key derivations of
128
- * work, so a connection takes at most `QUIC_CONN_MAX_KEY_UPDATES` updates the
129
- * client starts and closes on the next with KEY_UPDATE_ERROR (QUIC-4).
130
- * The AEAD limits are kept (RFC 9001 §6.6): from three quarters of AES-GCM's
131
- * confidentiality limit, 2^23 packets under one write key, the server starts
132
- * an update itself, and closes with AEAD_LIMIT_REACHED, sealing nothing more,
133
- * if it reaches the limit with none possible; past the integrity limit of
134
- * packets that fail to authenticate over the connection, 2^52 for AES-GCM
135
- * and 2^36 for ChaCha20-Poly1305, it closes with AEAD_LIMIT_REACHED too.
130
+ * **Key update** (RFC 9001 §6), both ways. A 1-RTT packet whose Key Phase bit
131
+ * differs from the current one is opened with the next generation's read keys,
132
+ * which are derived ahead of time so that a forged bit costs no derivation and
133
+ * shows no timing difference (§6.3); if it opens, the client has updated, and
134
+ * the server's write keys follow before anything is acknowledged (§6.2).
135
+ * `updateKeys()` starts an update from the server once the client has
136
+ * acknowledged a packet of the current phase (§6.1).
137
+ *
138
+ * The generations take turns in two key slots and two secrets a direction, so
139
+ * an update keeps nothing in the arena; each still costs two key derivations of
140
+ * work, so a connection takes at most `QUIC_CONN_MAX_KEY_UPDATES` updates the
141
+ * client starts and closes on the next with KEY_UPDATE_ERROR (QUIC-4).
142
+ *
143
+ * The AEAD limits are kept (RFC 9001 §6.6): from three quarters of AES-GCM's
144
+ * confidentiality limit, 2^23 packets under one write key, the server starts an
145
+ * update itself, and closes with AEAD_LIMIT_REACHED, sealing nothing more, if
146
+ * it reaches the limit with none possible; past the integrity limit of packets
147
+ * that fail to authenticate over the connection, 2^52 for AES-GCM and 2^36 for
148
+ * ChaCha20-Poly1305, it closes with AEAD_LIMIT_REACHED too.
136
149
  *
137
150
  * **Sans-IO and deterministic.** No socket, no clock and no random device:
138
151
  * every random choice (the TLS server random and ephemeral key, the server's
@@ -143,61 +156,68 @@
143
156
  *
144
157
  * **Paths** (RFC 9000 §8.2, §9). `receiveWindowFrom` takes the 18-byte
145
158
  * `nish:net` address each datagram came from, and `sendAddress()` says where
146
- * the connection's datagrams go: the first Initial's address, validated by
147
- * the handshake, until the client's address changes. Before the handshake
148
- * is confirmed a datagram from any other address is dropped unread, so no
149
- * forged source moves a handshake or takes its flight. After it, a datagram
150
- * from a new address whose newest non-probing packet authenticates moves
151
- * the connection there (§9.3) — a later one from the old address, with a
152
- * lower packet number, does not move it back — and a PATH_CHALLENGE, eight
153
- * bytes from the ID seed's HMAC, padded to 1,200 bytes, validates the new
154
- * path. Until a PATH_RESPONSE with any of the last three challenges'
155
- * data arrives, on any path, the server sends there at most three times
156
- * what came from it (§9.3.1); a lost challenge is drawn again, fresh, a
157
- * probe timeout later; past max(3 PTO, 6 kInitialRtt) validation has failed
158
- * and the connection goes back to the last validated address, where the
159
- * client — or, for a forged source, the real client's next packet — finds
160
- * it. Validated, a new address starts congestion control and the RTT
161
- * estimate again, a port-only change keeps them (§9.4), and ECN is tested
162
- * again (§13.4.2). A packet that carries only probing frames moves
163
- * nothing: its PATH_CHALLENGE is answered on its own path, alone in a
164
- * datagram `takeProbeInto` writes for `probeAddress`, under that path's own
165
- * 3x limit. The client's connection ID is kept across a move (§9.5): a NAT
166
- * rebinding gives the client no reason to change it, and this server never
167
- * migrates itself. Every address, challenge and counter is a field made by
168
- * the constructor and cleared by `reset`.
159
+ * the connection's datagrams go: the first Initial's address, validated by the
160
+ * handshake, until the client's address changes. Before the handshake is
161
+ * confirmed a datagram from any other address is dropped unread, so no forged
162
+ * source moves a handshake or takes its flight.
163
+ *
164
+ * After it, a datagram from a new address whose newest non-probing packet
165
+ * authenticates moves the connection there (§9.3) — a later one from the old
166
+ * address, with a lower packet number, does not move it back — and a
167
+ * PATH_CHALLENGE, eight bytes from the ID seed's HMAC, padded to 1,200 bytes,
168
+ * validates the new path.
169
+ *
170
+ * Until a PATH_RESPONSE with any of the last three challenges' data arrives, on
171
+ * any path, the server sends there at most three times what came from it
172
+ * (§9.3.1); a lost challenge is drawn again, fresh, a probe timeout later; past
173
+ * max(3 PTO, 6 kInitialRtt) validation has failed and the connection goes back
174
+ * to the last validated address, where the client — or, for a forged source,
175
+ * the real client's next packet — finds it.
176
+ *
177
+ * Validated, a new address starts congestion control and the RTT estimate
178
+ * again, a port-only change keeps them (§9.4), and ECN is tested again
179
+ * (§13.4.2). A packet that carries only probing frames moves nothing: its
180
+ * PATH_CHALLENGE is answered on its own path, alone in a datagram
181
+ * `takeProbeInto` writes for `probeAddress`, under that path's own 3x limit.
182
+ * The client's connection ID is kept across a move (§9.5): a NAT rebinding
183
+ * gives the client no reason to change it, and this server never migrates
184
+ * itself. Every address, challenge and counter is a field made by the
185
+ * constructor and cleared by `reset`.
169
186
  *
170
187
  * **What a peer cannot do.** Nothing it sends makes this module panic. A
171
188
  * datagram that does not parse, a packet that does not authenticate, a
172
189
  * duplicate packet number, a packet for keys already discarded and a 1-RTT
173
- * packet before the handshake is done are dropped and counted in `dropped`.
174
- * A frame that breaks the protocol closes the connection with the transport
175
- * error RFC 9000 names (`QUIC_ERROR_*` in `nish/net/quic-frame`), a TLS alert
176
- * closes it with CRYPTO_ERROR (0x100 + the alert), and the CONNECTION_CLOSE
177
- * is the next datagram out. Before the client's address is validated — by a
178
- * Handshake packet, or by a Retry token `nish/net/quic-listener` checked —
179
- * the server sends at most three times what it received (§8.1). Every buffer
180
- * a peer can fill is fixed: CRYPTO reassembly by `QUIC_CONN_CRYPTO_WINDOW`,
181
- * each stream by the credit advertised (its buffer), the connection by
182
- * `maxData`, the connection-ID table by the limit advertised, the received
183
- * packet numbers by `QUIC_ACK_MAX_RANGES`, and datagrams by their ring.
190
+ * packet before the handshake is done are dropped and counted in `dropped`. A
191
+ * frame that breaks the protocol closes the connection with the transport error
192
+ * RFC 9000 names (`QUIC_ERROR_*` in `nish/net/quic-frame`), a TLS alert closes
193
+ * it with CRYPTO_ERROR (0x100 + the alert), and the CONNECTION_CLOSE is the
194
+ * next datagram out.
195
+ *
196
+ * Before the client's address is validated — by a Handshake packet, or by a
197
+ * Retry token `nish/net/quic-listener` checked — the server sends at most three
198
+ * times what it received (§8.1). Every buffer a peer can fill is fixed: CRYPTO
199
+ * reassembly by `QUIC_CONN_CRYPTO_WINDOW`, each stream by the credit advertised
200
+ * (its buffer), the connection by `maxData`, the connection-ID table by the
201
+ * limit advertised, the received packet numbers by `QUIC_ACK_MAX_RANGES`, and
202
+ * datagrams by their ring.
184
203
  *
185
204
  * **Secrets.** The packet keys of each level live in this connection's key
186
205
  * slots as long as the level does; so do the 1-RTT secrets the next key
187
- * generation is derived from, and the next generation's read keys. The
188
- * traffic secrets and the ephemeral key `TlsServer` holds are `Secret`
189
- * fields its `wipe()` reaches (TLS-1): `afterTls` takes each level's secrets
190
- * out of them once, into this connection's plain slots, and zeroes them
191
- * through `wipeSecret`. The slots are plain bytes, wiped with stores no
192
- * optimiser removes (`secureZero`, and `aesKeyWipe` for an AES schedule):
193
- * each level's key, IV, header-protection key and schedules when the level
194
- * is discarded; each generation's key, IV, schedule and secret when a key
195
- * update replaces it; and the rest — the 1-RTT keys, the key-update secrets,
196
- * whatever `TlsServer` still holds and the connection-ID seed — by
197
- * `release()`, `reset`, the idle timeout, and the end of the closing or
198
- * draining period (RFC 9000 §10.2), three probe timeouts after this side's
199
- * CONNECTION_CLOSE went out or the client's arrived.
200
- * `docs/security/quic.md` records what is wiped where as QUIC-2.
206
+ * generation is derived from, and the next generation's read keys. The traffic
207
+ * secrets and the ephemeral key `TlsServer` holds are `Secret` fields its
208
+ * `wipe()` reaches (TLS-1): `afterTls` takes each level's secrets out of them
209
+ * once, into this connection's plain slots, and zeroes them through
210
+ * `wipeSecret`.
211
+ *
212
+ * The slots are plain bytes, wiped with stores no optimiser removes
213
+ * (`secureZero`, and `aesKeyWipe` for an AES schedule): each level's key, IV,
214
+ * header-protection key and schedules when the level is discarded; each
215
+ * generation's key, IV, schedule and secret when a key update replaces it; and
216
+ * the rest — the 1-RTT keys, the key-update secrets, whatever `TlsServer` still
217
+ * holds and the connection-ID seed — by `release()`, `reset`, the idle timeout,
218
+ * and the end of the closing or draining period (RFC 9000 §10.2), three probe
219
+ * timeouts after this side's CONNECTION_CLOSE went out or the client's arrived.
220
+ * `docs/security/quic.md` records what is wiped where as QUIC-2.
201
221
  *
202
222
  * Written from RFC 9000, RFC 9001 and RFC 9221, in this module's own
203
223
  * structure; nothing here is ported from another implementation. Private
@@ -419,6 +439,11 @@ export const QUIC_CONN_CONFIDENTIALITY_LIMIT: i64 = 8388608
419
439
  * with AEAD_LIMIT_REACHED. AES-GCM's is 2^52, ChaCha20-Poly1305's 2^36.
420
440
  */
421
441
  export const QUIC_CONN_INTEGRITY_LIMIT_GCM: i64 = 4503599627370496
442
+ /**
443
+ * ChaCha20-Poly1305's integrity limit (RFC 9001 §6.6), 2^36 packets that fail
444
+ * to authenticate over a connection, across every key, before it closes with
445
+ * AEAD_LIMIT_REACHED. The connection counts failures against the limit of the AEAD it negotiated.
446
+ */
422
447
  export const QUIC_CONN_INTEGRITY_LIMIT_CHACHA: i64 = 68719476736
423
448
  /**
424
449
  * How many skipped packet numbers the server remembers (RFC 9000 §21.4): an
@@ -428,7 +453,17 @@ export const QUIC_CONN_INTEGRITY_LIMIT_CHACHA: i64 = 68719476736
428
453
  * reaches back at least 7 × 129 = 903 packets.
429
454
  */
430
455
  export const QUIC_CONN_SKIP_RING: i32 = 8
456
+ /**
457
+ * The fewest Application Data packets between two skipped packet numbers
458
+ * (RFC 9000 §21.4). Large enough that the gaps cost the client almost
459
+ * nothing in ACK ranges.
460
+ */
431
461
  export const QUIC_CONN_SKIP_MIN: i64 = 128
462
+ /**
463
+ * How many distances past `QUIC_CONN_SKIP_MIN` a skip may fall at: the next
464
+ * skipped number is `QUIC_CONN_SKIP_MIN` plus 0 to 896 past the number after
465
+ * the last skip, so skips fall 129 to 1025 numbers apart.
466
+ */
432
467
  export const QUIC_CONN_SKIP_SPREAD: i64 = 897
433
468
  /**
434
469
  * The HMAC counter the skip distances are drawn under, past any connection
@@ -600,10 +635,21 @@ export interface QuicServerConfig {
600
635
 
601
636
  /** One run of stream data the client sent, in order, as `readStream` answers it: `fin` when it ends the stream. */
602
637
  export class QuicStreamData {
638
+ /** The RFC 9000 stream ID the bytes arrived on. */
603
639
  streamId: i64 = 0
640
+ /** The bytes of the run, in stream order. */
604
641
  data: u8[]
642
+ /** Whether the run ends the stream: the client sends no bytes after it. */
605
643
  fin: boolean = false
606
644
 
645
+ /**
646
+ * A run as `readStream` hands it out.
647
+ *
648
+ * @param streamId The RFC 9000 stream ID the bytes arrived on.
649
+ * @param data The bytes, in stream order; the run owns this array, so the caller may keep it. Empty
650
+ * for a run that only carries the FIN.
651
+ * @param fin True when the run ends at the stream's FIN, so nothing more follows on `streamId`.
652
+ */
607
653
  constructor(streamId: i64, data: u8[], fin: boolean) {
608
654
  this.streamId = streamId
609
655
  this.data = data
@@ -620,11 +666,22 @@ export class QuicStreamData {
620
666
  * before the Handshake one starts.
621
667
  */
622
668
  class QuicConnReassembly {
669
+ /** @internal Read in quic.ts outside the class. */
623
670
  ring: u8[]
671
+ /**
672
+ * @internal Only its own class reads it; kept public with `ring`, its family, read in quic.ts outside
673
+ * the class.
674
+ */
624
675
  have: u8[]
625
- /** The level whose bytes the ring holds. */
676
+ /**
677
+ * The level whose bytes the ring holds.
678
+ * @internal Read in quic.ts outside the class.
679
+ */
626
680
  level: i32 = 0
627
- /** How many bytes of the level have been handed to TLS. */
681
+ /**
682
+ * How many bytes of the level have been handed to TLS.
683
+ * @internal Read in quic.ts outside the class.
684
+ */
628
685
  delivered: i64 = 0
629
686
 
630
687
  constructor(capacity: i32) {
@@ -632,26 +689,32 @@ class QuicConnReassembly {
632
689
  this.have = new Array<u8>((capacity + 7) >> 3)
633
690
  }
634
691
 
635
- /** Empties the ring for level `level`. */
692
+ /**
693
+ * Empties the ring for level `level`.
694
+ * @internal Read in quic.ts outside the class.
695
+ */
636
696
  reset(level: i32): void {
637
697
  this.level = level
638
698
  this.delivered = 0
639
699
  this.have.fill(toU8(0))
640
700
  }
641
701
 
642
- /** The ring slot of stream offset `offset`. */
702
+ /**
703
+ * The ring slot of stream offset `offset`.
704
+ * @internal Read in quic.ts outside the class.
705
+ */
643
706
  slot(offset: i64): i32 {
644
707
  const capacity: i32 = toI32(this.ring.length)
645
708
  return capacity > 0 ? toI32(offset % toI64(capacity)) : 0
646
709
  }
647
710
 
648
711
  /** Whether the byte in ring slot `s` arrived and is not yet handed out. */
649
- arrived(s: i32): boolean {
712
+ private arrived(s: i32): boolean {
650
713
  return quicStreamBit(this.have, s)
651
714
  }
652
715
 
653
716
  /** Marks ring slot `s` as holding a byte, or not. */
654
- mark(s: i32, on: boolean): void {
717
+ private mark(s: i32, on: boolean): void {
655
718
  quicStreamSetBit(this.have, s, on)
656
719
  }
657
720
 
@@ -659,6 +722,7 @@ class QuicConnReassembly {
659
722
  * Takes `data[from .. from + length)`, which sits at stream offset
660
723
  * `offset`. Answers `false`, taking nothing, when it reaches `capacity` or
661
724
  * more past what was delivered. Bytes already delivered are skipped.
725
+ * @internal Read in quic.ts outside the class.
662
726
  */
663
727
  insert(offset: i64, data: u8[], from: i32, length: i32): boolean {
664
728
  const capacity: i32 = toI32(this.ring.length)
@@ -679,7 +743,10 @@ class QuicConnReassembly {
679
743
  return true
680
744
  }
681
745
 
682
- /** How many bytes from `delivered` have arrived without a gap, up to the end of the ring's array. */
746
+ /**
747
+ * How many bytes from `delivered` have arrived without a gap, up to the end of the ring's array.
748
+ * @internal Read in quic.ts outside the class.
749
+ */
683
750
  run(): i32 {
684
751
  const capacity: i32 = toI32(this.ring.length)
685
752
  const start: i32 = this.slot(this.delivered)
@@ -690,7 +757,10 @@ class QuicConnReassembly {
690
757
  return n
691
758
  }
692
759
 
693
- /** Hands out the `n` bytes `run` counted: their bits cleared, `delivered` moved past them. */
760
+ /**
761
+ * Hands out the `n` bytes `run` counted: their bits cleared, `delivered` moved past them.
762
+ * @internal Read in quic.ts outside the class.
763
+ */
694
764
  consume(n: i32): void {
695
765
  const start: i32 = this.slot(this.delivered)
696
766
  for (let k: i32 = 0; k < n; k += 1) {
@@ -718,6 +788,7 @@ class QuicConnSpace {
718
788
  nextPn: i64 = 0
719
789
  /** The largest packet number the client acknowledged here, or -1. */
720
790
  largestAcked: i64 = -1
791
+ /** @internal Read in quic.ts outside the class. */
721
792
  received: QuicAckRanges
722
793
  /**
723
794
  * Every CRYPTO byte TLS wrote at this level, from stream offset 0, its
@@ -725,8 +796,10 @@ class QuicConnSpace {
725
796
  * a lost range can be sent again; the array is reused by the next
726
797
  * connection in the slot. The bytes from `cryptoOutHead` (offset
727
798
  * `cryptoOutOffset`) are not sent yet.
799
+ * @internal Read in quic.ts outside the class.
728
800
  */
729
801
  cryptoOut: u8[]
802
+ /** @internal Read in quic.ts outside the class. */
730
803
  cryptoOutLength: i32 = 0
731
804
  /**
732
805
  * How many ack-eliciting probe packets this space still owes, which the
@@ -734,25 +807,47 @@ class QuicConnSpace {
734
807
  * §6.2.4), or an early resend (§6.2.3).
735
808
  */
736
809
  probes: i32 = 0
810
+ /** @internal Read in quic.ts outside the class. */
737
811
  cryptoOutOffset: i64 = 0
738
- /** How many CRYPTO bytes the client sent here that were handed to TLS, for a duplicate arriving after the next level started. */
812
+ /**
813
+ * How many CRYPTO bytes the client sent here that were handed to TLS, for a duplicate arriving after the next level started.
814
+ * @internal Read in quic.ts outside the class.
815
+ */
739
816
  cryptoInDelivered: i64 = 0
740
- /** CRYPTO bytes declared lost, to send before new ones: `[cryptoResendLow, cryptoResendHigh)`, or -1. */
817
+ /**
818
+ * CRYPTO bytes declared lost, to send before new ones: `[cryptoResendLow, cryptoResendHigh)`, or -1.
819
+ * @internal Read in quic.ts outside the class.
820
+ */
741
821
  cryptoResendLow: i64 = -1
822
+ /** @internal Read in quic.ts outside the class. */
742
823
  cryptoResendHigh: i64 = -1
743
824
  /** Each packet's CRYPTO frame (offset, and length 0 for none), and its `QUIC_CONN_SENT_*` bits. */
744
- sentCryptoOffset: i64[]
745
- sentCryptoLength: i32[]
746
- sentFlags: u8[]
747
- /** Each packet's STREAM chunks, `QUIC_CONN_PACKET_STREAMS` to a row: how many, then each one's stream, offset, length and FIN. */
825
+ private sentCryptoOffset: i64[]
826
+ private sentCryptoLength: i32[]
827
+ private sentFlags: u8[]
828
+ /**
829
+ * Each packet's STREAM chunks, `QUIC_CONN_PACKET_STREAMS` to a row: how many, then each one's stream, offset, length and FIN.
830
+ * @internal Only its own class reads it; kept public with `sentStreamId`, its family, read in quic.ts
831
+ * outside the class.
832
+ */
748
833
  sentStreamCount: i32[]
834
+ /** @internal Read in quic.ts outside the class. */
749
835
  sentStreamId: i64[]
836
+ /** @internal Read in quic.ts outside the class. */
750
837
  sentStreamOffset: i64[]
838
+ /** @internal Read in quic.ts outside the class. */
751
839
  sentStreamLength: i32[]
840
+ /** @internal Read in quic.ts outside the class. */
752
841
  sentStreamFin: u8[]
753
- /** Each packet's control frames, `QUIC_CONN_PACKET_CONTROL` to a row: their kind and value (a sequence number or a stream). */
842
+ /**
843
+ * Each packet's control frames, `QUIC_CONN_PACKET_CONTROL` to a row: their kind and value (a sequence number or a stream).
844
+ * @internal Only its own class reads it; kept public with `sentControlKind`, its family, read in quic.ts
845
+ * outside the class.
846
+ */
754
847
  sentControlCount: i32[]
848
+ /** @internal Read in quic.ts outside the class. */
755
849
  sentControlKind: u8[]
850
+ /** @internal Read in quic.ts outside the class. */
756
851
  sentControlValue: i64[]
757
852
  /**
758
853
  * Where this space's packet keys live, made once for the slot (QUIC-3):
@@ -760,17 +855,31 @@ class QuicConnSpace {
760
855
  * second of each, which a key update derives the next generation into.
761
856
  */
762
857
  keySlots: QuicKeysSlot[]
763
- /** `TLS_LEVEL_INITIAL`, `_HANDSHAKE` or `_APPLICATION`, which is also the space's index. */
858
+ /**
859
+ * `TLS_LEVEL_INITIAL`, `_HANDSHAKE` or `_APPLICATION`, which is also the space's index.
860
+ * @internal Read in quic.ts outside the class.
861
+ */
764
862
  level: i32 = 0
863
+ /** @internal Read in quic.ts outside the class. */
765
864
  cryptoOutHead: i32 = 0
766
- /** The packet `buildDatagram` is putting together here: where it starts in the datagram, or -1 for none, and its payload's end. */
865
+ /**
866
+ * The packet `buildDatagram` is putting together here: where it starts in the datagram, or -1 for none, and its payload's end.
867
+ * @internal Read in quic.ts outside the class.
868
+ */
767
869
  builtStart: i32 = -1
870
+ /** @internal Read in quic.ts outside the class. */
768
871
  builtEnd: i32 = 0
769
- /** The row the packet being built is described in: one past the last slot. */
872
+ /**
873
+ * The row the packet being built is described in: one past the last slot.
874
+ * @internal Read in quic.ts outside the class.
875
+ */
770
876
  staging: i32 = 0
771
877
  /** Whether the keys were discarded (RFC 9001 §4.9): nothing is sent or received here again. */
772
878
  discarded: boolean = false
773
- /** Whether the packet being built elicits an acknowledgement, and so is recorded. */
879
+ /**
880
+ * Whether the packet being built elicits an acknowledgement, and so is recorded.
881
+ * @internal Read in quic.ts outside the class.
882
+ */
774
883
  stagedEliciting: boolean = false
775
884
 
776
885
  constructor(level: i32) {
@@ -801,7 +910,10 @@ class QuicConnSpace {
801
910
  this.sentControlValue = new Array<i64>(chunkRows * QUIC_CONN_PACKET_CONTROL)
802
911
  }
803
912
 
804
- /** Puts the space back as a new connection has it, keeping its arrays, for a slot reused for another peer. */
913
+ /**
914
+ * Puts the space back as a new connection has it, keeping its arrays, for a slot reused for another peer.
915
+ * @internal Read in quic.ts outside the class.
916
+ */
805
917
  reset(): void {
806
918
  this.readKeys = null
807
919
  this.writeKeys = null
@@ -823,6 +935,7 @@ class QuicConnSpace {
823
935
  * Appends `bytes[0 .. length)` to the CRYPTO stream kept, writing into the
824
936
  * array's old room before growing it: TLS's output buffer is read in
825
937
  * place, so its flight is not copied on the way.
938
+ * @internal Read in quic.ts outside the class.
826
939
  */
827
940
  appendCrypto(bytes: u8[], length: i32): void {
828
941
  for (let k: i32 = 0; k < length && k < toI32(bytes.length); k += 1) {
@@ -840,45 +953,69 @@ class QuicConnSpace {
840
953
  * Key slot `k` of this space: in the Initial and Handshake spaces 0 holds
841
954
  * the read keys and 1 the write keys; in Application Data 0 and 1 hold
842
955
  * the read keys' generations and 2 and 3 the write keys'.
956
+ *
957
+ * @param k The slot index, 0 to 1 in the Initial and Handshake spaces and 0 to 3 in Application Data.
958
+ * @returns The slot itself, borrowed from the space and reused across keys; slot 0 for a `k` out of range.
843
959
  */
844
960
  keySlot(k: i32): QuicKeysSlot {
845
961
  const at: i32 = k >= 0 && k < toI32(this.keySlots.length) ? k : 0
846
962
  return this.keySlots[at]
847
963
  }
848
964
 
849
- /** Zeroes every key this space's slots hold, the AES schedules included. */
965
+ /**
966
+ * Zeroes every key this space's slots hold, the AES schedules included.
967
+ * @internal Read in quic.ts outside the class.
968
+ */
850
969
  wipeKeySlots(): void {
851
970
  for (const slot of this.keySlots) {
852
971
  slot.wipe()
853
972
  }
854
973
  }
855
974
 
856
- /** The number of STREAM chunks row `row` holds, or 0. */
975
+ /**
976
+ * The number of STREAM chunks row `row` holds, or 0.
977
+ * @internal Read in quic.ts outside the class.
978
+ */
857
979
  streamCount(row: i32): i32 {
858
980
  return row >= 0 && row < toI32(this.sentStreamCount.length) ? this.sentStreamCount[row] : 0
859
981
  }
860
982
 
861
- /** The number of control frames row `row` holds, or 0. */
983
+ /**
984
+ * The number of control frames row `row` holds, or 0.
985
+ * @internal Read in quic.ts outside the class.
986
+ */
862
987
  controlCount(row: i32): i32 {
863
988
  return row >= 0 && row < toI32(this.sentControlCount.length) ? this.sentControlCount[row] : 0
864
989
  }
865
990
 
866
- /** The CRYPTO frame's length in row `row`, or 0. */
991
+ /**
992
+ * The CRYPTO frame's length in row `row`, or 0.
993
+ * @internal Read in quic.ts outside the class.
994
+ */
867
995
  cryptoLength(row: i32): i32 {
868
996
  return row >= 0 && row < toI32(this.sentCryptoLength.length) ? this.sentCryptoLength[row] : 0
869
997
  }
870
998
 
871
- /** The CRYPTO frame's offset in row `row`, or 0. */
999
+ /**
1000
+ * The CRYPTO frame's offset in row `row`, or 0.
1001
+ * @internal Read in quic.ts outside the class.
1002
+ */
872
1003
  cryptoOffset(row: i32): i64 {
873
1004
  return row >= 0 && row < toI32(this.sentCryptoOffset.length) ? this.sentCryptoOffset[row] : 0
874
1005
  }
875
1006
 
876
- /** The `QUIC_CONN_SENT_*` bits of row `row`. */
1007
+ /**
1008
+ * The `QUIC_CONN_SENT_*` bits of row `row`.
1009
+ * @internal Read in quic.ts outside the class.
1010
+ */
877
1011
  flags(row: i32): i32 {
878
1012
  return row >= 0 && row < toI32(this.sentFlags.length) ? toI32(this.sentFlags[row]) : 0
879
1013
  }
880
1014
 
881
- /** Empties the staging row, for the next packet. */
1015
+ /**
1016
+ * Empties the staging row, for the next packet.
1017
+ * @internal Read in quic.ts outside the class.
1018
+ */
882
1019
  clearStaged(): void {
883
1020
  this.stagedEliciting = false
884
1021
  this.setCrypto(this.staging, QUIC_CONN_NONE, QUIC_CONN_FROM)
@@ -887,14 +1024,17 @@ class QuicConnSpace {
887
1024
  }
888
1025
 
889
1026
  /** Sets row `row`'s CRYPTO frame. */
890
- setCrypto(row: i32, offset: i64, length: i32): void {
1027
+ private setCrypto(row: i32, offset: i64, length: i32): void {
891
1028
  if (row >= 0 && row < toI32(this.sentCryptoOffset.length) && row < toI32(this.sentCryptoLength.length)) {
892
1029
  this.sentCryptoOffset[row] = offset
893
1030
  this.sentCryptoLength[row] = length
894
1031
  }
895
1032
  }
896
1033
 
897
- /** Sets row `row`'s bits. */
1034
+ /**
1035
+ * Sets row `row`'s bits.
1036
+ * @internal Read in quic.ts outside the class.
1037
+ */
898
1038
  setFlags(row: i32, flags: i32): void {
899
1039
  if (row >= 0 && row < toI32(this.sentFlags.length)) {
900
1040
  this.sentFlags[row] = toU8(flags)
@@ -902,7 +1042,7 @@ class QuicConnSpace {
902
1042
  }
903
1043
 
904
1044
  /** Sets how many STREAM chunks and control frames row `row` holds. */
905
- setCounts(row: i32, streams: i32, control: i32): void {
1045
+ private setCounts(row: i32, streams: i32, control: i32): void {
906
1046
  if (row >= 0 && row < toI32(this.sentStreamCount.length) && row < toI32(this.sentControlCount.length)) {
907
1047
  this.sentStreamCount[row] = streams
908
1048
  this.sentControlCount[row] = control
@@ -910,7 +1050,7 @@ class QuicConnSpace {
910
1050
  }
911
1051
 
912
1052
  /** Sets STREAM chunk `j` of row `row`. */
913
- setChunk(row: i32, j: i32, id: i64, offset: i64, length: i32, fin: boolean): void {
1053
+ private setChunk(row: i32, j: i32, id: i64, offset: i64, length: i32, fin: boolean): void {
914
1054
  const at: i32 = row * QUIC_CONN_PACKET_STREAMS + j
915
1055
  if (
916
1056
  at >= 0 &&
@@ -927,7 +1067,7 @@ class QuicConnSpace {
927
1067
  }
928
1068
 
929
1069
  /** Sets control frame `j` of row `row`. */
930
- setControl(row: i32, j: i32, kind: i32, value: i64): void {
1070
+ private setControl(row: i32, j: i32, kind: i32, value: i64): void {
931
1071
  const at: i32 = row * QUIC_CONN_PACKET_CONTROL + j
932
1072
  if (at >= 0 && at < toI32(this.sentControlKind.length) && at < toI32(this.sentControlValue.length)) {
933
1073
  this.sentControlKind[at] = toU8(kind)
@@ -935,13 +1075,19 @@ class QuicConnSpace {
935
1075
  }
936
1076
  }
937
1077
 
938
- /** Stages a CRYPTO frame at `offset`, `length` bytes, in the packet being built. */
1078
+ /**
1079
+ * Stages a CRYPTO frame at `offset`, `length` bytes, in the packet being built.
1080
+ * @internal Read in quic.ts outside the class.
1081
+ */
939
1082
  stageCrypto(offset: i64, length: i32): void {
940
1083
  this.setCrypto(this.staging, offset, length)
941
1084
  this.stagedEliciting = true
942
1085
  }
943
1086
 
944
- /** Stages a STREAM chunk in the packet being built; the caller keeps under `QUIC_CONN_PACKET_STREAMS`. */
1087
+ /**
1088
+ * Stages a STREAM chunk in the packet being built; the caller keeps under `QUIC_CONN_PACKET_STREAMS`.
1089
+ * @internal Read in quic.ts outside the class.
1090
+ */
945
1091
  stageChunk(id: i64, offset: i64, length: i32, fin: boolean): void {
946
1092
  const streams: i32 = this.streamCount(this.staging)
947
1093
  this.setChunk(this.staging, streams, id, offset, length, fin)
@@ -949,7 +1095,10 @@ class QuicConnSpace {
949
1095
  this.stagedEliciting = true
950
1096
  }
951
1097
 
952
- /** Stages a control frame in the packet being built; the caller keeps under `QUIC_CONN_PACKET_CONTROL`. */
1098
+ /**
1099
+ * Stages a control frame in the packet being built; the caller keeps under `QUIC_CONN_PACKET_CONTROL`.
1100
+ * @internal Read in quic.ts outside the class.
1101
+ */
953
1102
  stageControl(kind: i32, value: i64): void {
954
1103
  const control: i32 = this.controlCount(this.staging)
955
1104
  this.setControl(this.staging, control, kind, value)
@@ -957,7 +1106,10 @@ class QuicConnSpace {
957
1106
  this.stagedEliciting = true
958
1107
  }
959
1108
 
960
- /** Copies the staging row to row `row`, the slot of the packet just sealed. */
1109
+ /**
1110
+ * Copies the staging row to row `row`, the slot of the packet just sealed.
1111
+ * @internal Read in quic.ts outside the class.
1112
+ */
961
1113
  commitStaged(row: i32): void {
962
1114
  const from: i32 = this.staging
963
1115
  this.setCrypto(row, this.cryptoOffset(from), this.cryptoLength(from))
@@ -992,7 +1144,10 @@ class QuicConnSpace {
992
1144
  }
993
1145
  }
994
1146
 
995
- /** Queues CRYPTO bytes `[offset, offset + length)` to be sent again, merged with what is queued already. */
1147
+ /**
1148
+ * Queues CRYPTO bytes `[offset, offset + length)` to be sent again, merged with what is queued already.
1149
+ * @internal Read in quic.ts outside the class.
1150
+ */
996
1151
  resendCrypto(offset: i64, length: i32): void {
997
1152
  if (length <= 0 || this.discarded) {
998
1153
  return
@@ -1011,7 +1166,10 @@ class QuicConnSpace {
1011
1166
  }
1012
1167
  }
1013
1168
 
1014
- /** How many CRYPTO bytes are waiting to be sent. */
1169
+ /**
1170
+ * How many CRYPTO bytes are waiting to be sent.
1171
+ * @internal Read in quic.ts outside the class.
1172
+ */
1015
1173
  cryptoUnsent(): i32 {
1016
1174
  return this.cryptoOutLength - this.cryptoOutHead
1017
1175
  }
@@ -1079,6 +1237,11 @@ const quicConnWipePacketKey = (keys: QuicKeys | null): void => {
1079
1237
  * `nish/net/quic-listener` derives the same token from a packet's ID alone
1080
1238
  * once the connection is gone, which is what lets a server that lost its
1081
1239
  * state end the connection.
1240
+ *
1241
+ * @param key The server's static reset key, `QUIC_CONN_STATIC_KEY_SIZE` bytes
1242
+ * (`QuicServerConfig.statelessResetKey`).
1243
+ * @param cid The connection ID the token is for, all of its bytes.
1244
+ * @returns A fresh 16-byte token (`QUIC_RESET_TOKEN_SIZE`); the full MAC it was cut from is wiped.
1082
1245
  */
1083
1246
  export const quicStatelessResetToken = (key: u8[], cid: u8[]): u8[] => {
1084
1247
  const mac: u8[] = hmacSha256(key, cid)
@@ -1092,6 +1255,14 @@ export const quicStatelessResetToken = (key: u8[], cid: u8[]): u8[] => {
1092
1255
  * `out[outAt ..]`, computed in a connection's own HMAC scratch inside an
1093
1256
  * arena block, so issuing an ID keeps nothing (QUIC-3). The scratch and the
1094
1257
  * full MAC are wiped before it returns.
1258
+ *
1259
+ * @param mac The connection's HMAC-SHA256 scratch, wiped on return.
1260
+ * @param key The server's static reset key, `QUIC_CONN_STATIC_KEY_SIZE` bytes.
1261
+ * @param cid The array holding the connection ID.
1262
+ * @param at Where the ID starts in `cid`.
1263
+ * @param length The ID's length in bytes, at most 20: the caller's to keep, as nothing checks it.
1264
+ * @param out Where the 16-byte token goes; bytes that would fall past its end are not written.
1265
+ * @param outAt Where in `out` the token starts.
1095
1266
  */
1096
1267
  export const quicConnResetTokenInto = (
1097
1268
  mac: HmacSha256Scratch,
@@ -1215,35 +1386,42 @@ const quicConnConfigFits = (config: QuicServerConfig): boolean =>
1215
1386
  * that runs many connections routes datagrams by (`ownsConnectionId`).
1216
1387
  */
1217
1388
  export class QuicConnection {
1218
- config: QuicServerConfig
1389
+ private config: QuicServerConfig
1219
1390
  /** The error code the connection closed with: a transport error, `0x100 + alert`, or the application's code. */
1220
1391
  error: i64 = 0
1221
1392
  /** For a transport error: the type of the frame that caused it, or 0. */
1222
- errorFrameType: i64 = 0
1223
- serverRandom: u8[]
1393
+ private errorFrameType: i64 = 0
1394
+ private serverRandom: u8[]
1224
1395
  /** The x25519 private key `begin` takes from the entropy, until the handshake hands it to `TlsServer`. */
1225
1396
  readonly ephemeral: Secret<u8[]>
1397
+ /** The 32-byte HMAC key, taken from the entropy, that each new local connection ID is derived from; `release` wipes it. */
1226
1398
  cidSeed: u8[]
1227
- /** The DCID of the client's first Initial, which the Initial keys come from: its first `originalDcidLength` bytes. */
1399
+ /**
1400
+ * The DCID of the client's first Initial, which the Initial keys come from: its first `originalDcidLength` bytes.
1401
+ * @internal Read by http3-server.ts.
1402
+ */
1228
1403
  originalDcid: u8[]
1229
1404
  /** The SCID of the client's first Initial: where long-header packets go, and what its transport parameters must name. */
1230
- peerScid: u8[]
1405
+ private peerScid: u8[]
1231
1406
  /** The server's first connection ID, the SCID of every long-header packet it sends. */
1232
- localScid: u8[]
1407
+ private localScid: u8[]
1408
+ /** The `TlsServer` running this connection's handshake: null until the client's first Initial has opened, and again after `reset`. */
1233
1409
  tls: TlsServer | null = null
1234
1410
  /** The slot's `TlsServer`, made by its first handshake and `restart`ed by every one after (QUIC-3). */
1235
1411
  tlsSlot: TlsServer | null = null
1236
1412
  /** Its configuration, made once: the parameters it carries are `localEncoded`, rewritten for each connection. */
1237
- tlsConfig: TlsServerConfig
1413
+ private tlsConfig: TlsServerConfig
1238
1414
  /** The server's transport parameters, set for each connection, and their encoding. */
1239
1415
  localParameters: QuicTransportParameters
1416
+ /** `localParameters` encoded for the TLS extension the server sends. */
1240
1417
  localEncoded: u8[]
1241
1418
  /** The client's transport parameters, parsed into the slot's own object. */
1242
- parsedParameters: QuicTransportParameters
1419
+ private parsedParameters: QuicTransportParameters
1243
1420
  /** Where every key derivation and HMAC of the connection runs. */
1244
- kdf: HkdfScratch
1421
+ private kdf: HkdfScratch
1245
1422
  /** The Initial secrets of RFC 9001 §5.2, the client's and the server's. */
1246
1423
  initialClient: u8[]
1424
+ /** The server's Initial secret (RFC 9001 §5.2); see `initialClient`. */
1247
1425
  initialServer: u8[]
1248
1426
  /**
1249
1427
  * The 1-RTT traffic secrets (RFC 9001 §6.1), one set of SHA-256's length
@@ -1254,40 +1432,51 @@ export class QuicConnection {
1254
1432
  * it moves to them.
1255
1433
  */
1256
1434
  readonly readSecret256: Secret<u8[]>
1435
+ /** The next generation's 1-RTT read secret under SHA-256, 32 bytes; see `readSecret256`. */
1257
1436
  readonly nextReadSecret256: Secret<u8[]>
1437
+ /** The current 1-RTT write secret under SHA-256, 32 bytes; see `readSecret256`. */
1258
1438
  readonly writeSecret256: Secret<u8[]>
1439
+ /** The current 1-RTT read secret under SHA-384, 48 bytes; see `readSecret256`. */
1259
1440
  readonly readSecret384: Secret<u8[]>
1441
+ /** The next generation's 1-RTT read secret under SHA-384, 48 bytes; see `readSecret256`. */
1260
1442
  readonly nextReadSecret384: Secret<u8[]>
1443
+ /** The current 1-RTT write secret under SHA-384, 48 bytes; see `readSecret256`. */
1261
1444
  readonly writeSecret384: Secret<u8[]>
1262
1445
  /** A connection ID being issued and its stateless reset token, before the table copies them. */
1263
- cidScratch: u8[]
1264
- tokenScratch: u8[]
1446
+ private cidScratch: u8[]
1447
+ private tokenScratch: u8[]
1448
+ /** The Initial packet number space: its keys, packet numbers and acknowledgements (RFC 9000 §12.3). */
1265
1449
  initial: QuicConnSpace
1450
+ /** The Handshake packet number space. */
1266
1451
  handshake: QuicConnSpace
1452
+ /** The Application Data packet number space, which holds the 1-RTT keys. */
1267
1453
  application: QuicConnSpace
1454
+ /** The connection IDs of both sides and their stateless reset tokens (`nish/net/quic-conn-cid`). */
1268
1455
  cids: QuicCidTable
1269
1456
  /** Loss detection, the RTT estimate, the congestion window and the pacer (RFC 9002). */
1270
1457
  recovery: QuicRecovery
1271
1458
  /** The client's transport parameters, once the handshake has checked them; until then, every default. */
1272
1459
  peerParameters: QuicTransportParameters
1273
1460
  /** The defaults `peerParameters` starts from, made once and set back by `reset`. */
1274
- defaultParameters: QuicTransportParameters
1461
+ private defaultParameters: QuicTransportParameters
1275
1462
  /** Bytes received and sent, for the anti-amplification limit (RFC 9000 §8.1). */
1276
1463
  bytesReceived: i64 = 0
1464
+ /** Bytes sent, which may not pass three times `bytesReceived` before the address is validated. */
1277
1465
  bytesSent: i64 = 0
1278
1466
  /** The ALPN protocol the handshake chose. */
1279
1467
  alpn: string = ""
1280
- /** The client's `server_name`, or empty. */
1468
+ /** The client's `server_name` the handshake read, or empty when it sent none. */
1281
1469
  serverName: string = ""
1282
1470
  /** The streams, both ways (`nish/net/quic-stream`). */
1283
1471
  streams: QuicStreams
1284
1472
  /** DATAGRAM payloads waiting to be sent, and those the client sent waiting to be read. */
1285
1473
  datagramsOut: QuicDatagramQueue
1474
+ /** DATAGRAM payloads the client sent, waiting to be read; its `dropped` counts those that found it full. */
1286
1475
  datagramsIn: QuicDatagramQueue
1287
1476
  /** The CRYPTO bytes the client sent, put back in order. */
1288
- cryptoIn: QuicConnReassembly
1477
+ private cryptoIn: QuicConnReassembly
1289
1478
  /** PATH_CHALLENGE data to answer: a ring of four, eight bytes each, `pathCount` from `pathHead`. */
1290
- pathData: u8[]
1479
+ private pathData: u8[]
1291
1480
  /**
1292
1481
  * The client's address (RFC 9000 §9), each in `nish:net`'s 18-byte form,
1293
1482
  * for a carrier that passes one to `receiveWindowFrom`: `pathCurrent` is
@@ -1296,9 +1485,13 @@ export class QuicConnection {
1296
1485
  * handshake's, or one a PATH_RESPONSE answered. `pathArrival` is the
1297
1486
  * datagram being read's. Made once and overwritten in place.
1298
1487
  */
1299
- pathCurrent: u8[]
1488
+ private pathCurrent: u8[]
1489
+ /**
1490
+ * The last address the client proved it receives at, in `nish:net`'s
1491
+ * 18-byte form (RFC 9000 §9): the handshake's, or one a PATH_RESPONSE answered.
1492
+ */
1300
1493
  pathValidated: u8[]
1301
- pathArrival: u8[]
1494
+ private pathArrival: u8[]
1302
1495
  /**
1303
1496
  * A PATH_CHALLENGE that came from another address in a datagram that
1304
1497
  * moved nothing (§9.1): answered there by `takeProbeInto`, from
@@ -1308,35 +1501,38 @@ export class QuicConnection {
1308
1501
  * the same way (`sendResponsesBack`).
1309
1502
  */
1310
1503
  probeAddress: u8[]
1311
- probeData: u8[]
1504
+ private probeData: u8[]
1312
1505
  /**
1313
1506
  * The last `QUIC_CONN_CHALLENGES` PATH_CHALLENGE data sent on the path
1314
1507
  * being validated, eight bytes each, `challengeCount` of them, the latest
1315
1508
  * at `(challengesDrawn - 1) % QUIC_CONN_CHALLENGES`: each drawn from the
1316
1509
  * ID seed's HMAC under `QUIC_CONN_CHALLENGE_SEQUENCE`, so unguessable.
1317
1510
  */
1318
- challengeData: u8[]
1511
+ private challengeData: u8[]
1319
1512
  /** Bytes received from and sent to the path being validated, for its own 3x limit (§9.3, §8.1). */
1320
1513
  pathReceived: i64 = 0
1514
+ /** Bytes sent to the path being validated, at most three times `pathReceived`. */
1321
1515
  pathSent: i64 = 0
1322
- probeBudget: i64 = 0
1516
+ private probeBudget: i64 = 0
1323
1517
  /** When the PATH_CHALLENGE goes again with fresh data, and when validation gives up (§8.2.4); -1 when not validating. */
1324
1518
  challengeResendAt: i64 = -1
1519
+ /** When path validation gives up, in milliseconds, or -1 when not validating. */
1325
1520
  challengeUntil: i64 = -1
1521
+ /** How many PATH_CHALLENGE data have been drawn over the connection; see `challengeData`. */
1326
1522
  challengesDrawn: i64 = 0
1327
- frame: QuicFrame
1523
+ private frame: QuicFrame
1328
1524
  /** The header and the packet numbers of the packet being read, reused for every one. */
1329
- header: QuicHeader
1330
- packet: QuicPacket
1525
+ private header: QuicHeader
1526
+ private packet: QuicPacket
1331
1527
  /** For a connection `acceptRetry` set up: the DCID of the client's Initial before the Retry, and the Retry's SCID. */
1332
- retryOriginalDcid: u8[]
1333
- retryScid: u8[]
1528
+ private retryOriginalDcid: u8[]
1529
+ private retryScid: u8[]
1334
1530
  /** An empty array, which a cleared secret or ID is set to. */
1335
- none: u8[]
1531
+ private none: u8[]
1336
1532
  /** The latest time the caller gave, in milliseconds; time never runs backwards here. */
1337
1533
  now: i64 = 0
1338
1534
  /** When the idle timer last restarted (RFC 9000 §10.1), or -1 before the first packet. */
1339
- idleSince: i64 = -1
1535
+ private idleSince: i64 = -1
1340
1536
  /**
1341
1537
  * The other read keys of the Application Data space (RFC 9001 §6.3):
1342
1538
  * the next generation's while `otherIsNext`, and for a probe timeout
@@ -1344,23 +1540,23 @@ export class QuicConnection {
1344
1540
  */
1345
1541
  otherReadKeys: QuicKeys | null = null
1346
1542
  /** The lowest and highest packet numbers opened with the current read keys, or -1. */
1347
- readPhaseLowest: i64 = -1
1348
- readPhaseHighest: i64 = -1
1543
+ private readPhaseLowest: i64 = -1
1544
+ private readPhaseHighest: i64 = -1
1349
1545
  /** The first packet number sent with the current write keys. */
1350
- writePhaseFirst: i64 = 0
1546
+ private writePhaseFirst: i64 = 0
1351
1547
  /** When the previous read keys go and the next are derived, or -1. */
1352
- keyRetainUntil: i64 = -1
1548
+ private keyRetainUntil: i64 = -1
1353
1549
  /**
1354
1550
  * When a closed connection's closing or draining period ends (RFC 9000
1355
1551
  * §10.2), and `runClocks` wipes every key it kept for it, or -1: before
1356
1552
  * the close, while a close this side owes has not gone out, and once the
1357
1553
  * keys are wiped.
1358
1554
  */
1359
- closingUntil: i64 = -1
1555
+ private closingUntil: i64 = -1
1360
1556
  /** The packet number of a client key update read but not applied yet, or -1. */
1361
- phasePending: i64 = -1
1557
+ private phasePending: i64 = -1
1362
1558
  /** The largest DATAGRAM frame the client takes (its `max_datagram_frame_size`), 0 for none. */
1363
- peerMaxDatagramFrame: i64 = 0
1559
+ private peerMaxDatagramFrame: i64 = 0
1364
1560
  /**
1365
1561
  * The AEAD limits this connection keeps (RFC 9001 §6.6): packets one 1-RTT
1366
1562
  * write key seals, and packets that fail to authenticate over the
@@ -1369,9 +1565,15 @@ export class QuicConnection {
1369
1565
  * lower them after the handshake; nothing else should set them.
1370
1566
  */
1371
1567
  confidentialityLimit: i64 = 8388608
1568
+ /**
1569
+ * Packets that may fail to authenticate before the connection closes (RFC
1570
+ * 9001 §6.6). `afterTls` sets it from the suite; a test may lower it after
1571
+ * the handshake.
1572
+ */
1372
1573
  integrityLimit: i64 = 4503599627370496
1373
1574
  /** Packets sealed under the current 1-RTT write key, and packets that failed to authenticate, against those limits. */
1374
1575
  sealedUnderKey: i64 = 0
1576
+ /** Packets that failed to authenticate over the connection, against `integrityLimit`. */
1375
1577
  authFailures: i64 = 0
1376
1578
  /**
1377
1579
  * The next Application Data packet number skipped (RFC 9000 §21.4), or -1
@@ -1380,14 +1582,18 @@ export class QuicConnection {
1380
1582
  * the next goes, and `skipsDrawn` how many distances were drawn.
1381
1583
  */
1382
1584
  nextSkip: i64 = -1
1383
- skipped: i64[]
1585
+ private skipped: i64[]
1586
+ /** How many skip distances have been drawn; see `nextSkip`. */
1384
1587
  skipsDrawn: i64 = 0
1385
1588
  /** How many of `challengeData`'s slots hold a challenge sent on the path being validated. */
1386
- challengeCount: i32 = 0
1589
+ private challengeCount: i32 = 0
1590
+ /** @internal Read by http3-server.ts. */
1387
1591
  originalDcidLength: i32 = 0
1388
- peerScidLength: i32 = 0
1389
- pathHead: i32 = 0
1592
+ private peerScidLength: i32 = 0
1593
+ private pathHead: i32 = 0
1594
+ /** How many PATH_CHALLENGE payloads wait to be answered, 0 to 4. */
1390
1595
  pathCount: i32 = 0
1596
+ /** Where the connection is in its life: one of the `QUIC_STATE_*` constants. */
1391
1597
  state: i32 = 0
1392
1598
  /** Packets dropped without closing the connection: unparseable, unauthenticated, duplicated, or for keys not held. */
1393
1599
  dropped: i32 = 0
@@ -1397,11 +1603,12 @@ export class QuicConnection {
1397
1603
  skipHead: i32 = 0
1398
1604
  /** Which of the Application Data space's two read slots, and two write slots, hold the current keys. */
1399
1605
  appReadSlot: i32 = 0
1606
+ /** Which of the Application Data space's two write slots holds the current keys, 0 or 1. */
1400
1607
  appWriteSlot: i32 = 0
1401
1608
  /** Handshake data sent again early, against `QUIC_CONN_EARLY_RESENDS` (RFC 9002 §6.2.3). */
1402
1609
  earlyResends: i32 = 0
1403
1610
  /** The level at which the packet just read showed the client lacks the server's handshake data, or -1. */
1404
- resendLevel: i32 = -1
1611
+ private resendLevel: i32 = -1
1405
1612
  /**
1406
1613
  * ECN (RFC 9000 §13.4): `ecnMarking` is the carrier's to set when it sends
1407
1614
  * this connection's datagrams with the ECN bits of `datagramEcn`, which
@@ -1412,49 +1619,51 @@ export class QuicConnection {
1412
1619
  * keeps `ecnMarking`, which is the carrier's, and clears the rest.
1413
1620
  */
1414
1621
  datagramEcn: i32 = 0
1622
+ /** Whether the carrier sends with the ECN bits of `datagramEcn`; the carrier sets it, and `reset` keeps it. */
1415
1623
  ecnMarking: boolean = false
1416
1624
  /** Whether `error` is an application code (CONNECTION_CLOSE 0x1d) rather than a transport error. */
1417
1625
  errorIsApplication: boolean = false
1418
1626
  /** Whether the one CONNECTION_CLOSE this side owes has gone out. */
1419
- closeSent: boolean = false
1627
+ private closeSent: boolean = false
1420
1628
  /** Whether the client's address is validated: a Handshake packet from it was processed. */
1421
1629
  addressValidated: boolean = false
1422
1630
  /** Whether the handshake is complete, which for a server is also confirmed (RFC 9001 §4.1.2). */
1423
1631
  handshakeComplete: boolean = false
1424
- handshakeDonePending: boolean = false
1632
+ private handshakeDonePending: boolean = false
1425
1633
  /**
1426
1634
  * Whether `afterTls` has taken the application traffic secrets out of
1427
1635
  * `TlsServer`. It takes them once: `wipeSecret` leaves them known, so a
1428
1636
  * second take would derive the 1-RTT keys from the zeros it left.
1429
1637
  */
1430
- applicationTaken: boolean = false
1638
+ private applicationTaken: boolean = false
1431
1639
  /** Whether the connection was set up by `acceptRetry`. */
1432
- retried: boolean = false
1640
+ private retried: boolean = false
1433
1641
  /** Whether an ack-eliciting packet went out since the last packet was received. */
1434
- elicitingSent: boolean = false
1642
+ private elicitingSent: boolean = false
1435
1643
  /** Whether `otherReadKeys` are the next generation's rather than the previous one's. */
1436
1644
  otherIsNext: boolean = false
1437
1645
  /** The Key Phase bit of the current read keys, and of the current write keys. */
1438
1646
  readPhase: boolean = false
1647
+ /** The Key Phase bit of the current write keys. */
1439
1648
  writePhase: boolean = false
1440
1649
  /** Whether the packet just read brought CRYPTO bytes for TLS, or retired an ID that wants replacing. */
1441
- cryptoArrived: boolean = false
1442
- topUpOwed: boolean = false
1650
+ private cryptoArrived: boolean = false
1651
+ private topUpOwed: boolean = false
1443
1652
  /** Whether `pathCurrent` holds the client's address: the carrier passes addresses, and a first Initial gave one. */
1444
1653
  pathKnown: boolean = false
1445
1654
  /** Whether the datagram being read came from an address other than `pathCurrent`. */
1446
- arrivalOther: boolean = false
1655
+ private arrivalOther: boolean = false
1447
1656
  /** Whether `pathCurrent` is not validated yet: a PATH_CHALLENGE is out on it. */
1448
1657
  pathValidating: boolean = false
1449
1658
  /** Whether a PATH_CHALLENGE is due in the next datagram, and whether the one being built carries a PATH_CHALLENGE or PATH_RESPONSE. */
1450
- challengeOwed: boolean = false
1451
- pathFrameStaged: boolean = false
1659
+ private challengeOwed: boolean = false
1660
+ private pathFrameStaged: boolean = false
1452
1661
  /** Whether a PATH_RESPONSE is owed on `probeAddress`, and whether the datagram being read carried a challenge for it. */
1453
1662
  probeOwed: boolean = false
1454
- probeStaged: boolean = false
1663
+ private probeStaged: boolean = false
1455
1664
  /** Whether the packet being read carries a non-probing frame (§9.1), and whether a packet of the datagram moves the connection (§9.3). */
1456
- nonProbing: boolean = false
1457
- migrateOwed: boolean = false
1665
+ private nonProbing: boolean = false
1666
+ private migrateOwed: boolean = false
1458
1667
 
1459
1668
  /**
1460
1669
  * A connection under `config`, with `entropy` its `QUIC_CONN_ENTROPY_SIZE`
@@ -1463,6 +1672,11 @@ export class QuicConnection {
1463
1672
  * Entropy of another length, or limits outside what `QuicServerConfig`
1464
1673
  * documents, leave the connection closing with INTERNAL_ERROR before it
1465
1674
  * reads anything; it then sends nothing.
1675
+ *
1676
+ * @param config The server's limits and keys. The connection keeps a reference and reads it for
1677
+ * its whole life, `reset`s included, so do not change it under a live connection.
1678
+ * @param entropy `QUIC_CONN_ENTROPY_SIZE` (104) random bytes for this connection's first peer;
1679
+ * zeroed in place once copied.
1466
1680
  */
1467
1681
  constructor(config: QuicServerConfig, entropy: u8[]) {
1468
1682
  this.config = config
@@ -1549,6 +1763,9 @@ export class QuicConnection {
1549
1763
  * the constructor's configuration's. A slot reset this way allocates
1550
1764
  * nothing, and neither does the next handshake (QUIC-3 in
1551
1765
  * `docs/security/quic.md`).
1766
+ *
1767
+ * @param entropy `QUIC_CONN_ENTROPY_SIZE` (104) fresh random bytes for the next peer, never ones
1768
+ * used before; zeroed in place once copied.
1552
1769
  */
1553
1770
  reset(entropy: u8[]): void {
1554
1771
  this.wipeAll()
@@ -1620,7 +1837,7 @@ export class QuicConnection {
1620
1837
  }
1621
1838
 
1622
1839
  /** Takes `entropy` (and wipes it), or closes with INTERNAL_ERROR for entropy or a configuration that does not fit. */
1623
- begin(entropy: u8[]): void {
1840
+ private begin(entropy: u8[]): void {
1624
1841
  quicCidCopy(this.serverRandom, entropy, 0, 32)
1625
1842
  exposeWith(this.ephemeral, entropy, quicConnEphemeralIn)
1626
1843
  quicCidCopy(this.localScid, entropy, 64, QUIC_CONN_CID_LENGTH)
@@ -1636,13 +1853,17 @@ export class QuicConnection {
1636
1853
  }
1637
1854
 
1638
1855
  /** Counts a dropped packet; the count stops at the largest `i32` rather than overflow. */
1639
- drop(): void {
1856
+ private drop(): void {
1640
1857
  if (this.dropped < 2147483647) {
1641
1858
  this.dropped += 1
1642
1859
  }
1643
1860
  }
1644
1861
 
1645
- /** Whether the connection has closed, either way. */
1862
+ /**
1863
+ * Whether the connection has closed, either way.
1864
+ *
1865
+ * @returns True once it is closing, draining or timed out; it stays true until `reset`.
1866
+ */
1646
1867
  closed(): boolean {
1647
1868
  return (
1648
1869
  this.state === QUIC_STATE_CLOSING ||
@@ -1660,6 +1881,10 @@ export class QuicConnection {
1660
1881
  * validated, so the anti-amplification limit does not apply. Call it
1661
1882
  * before the first `receive`; answers whether it took: not once the
1662
1883
  * connection has started, and not for an ID over 20 bytes.
1884
+ *
1885
+ * @param originalDcid The DCID of the client's first Initial, from the checked Retry token; copied.
1886
+ * @param retryScid The SCID the Retry packet carried; copied.
1887
+ * @returns True when the connection took the Retry, false (and nothing changed) otherwise.
1663
1888
  */
1664
1889
  acceptRetry(originalDcid: u8[], retryScid: u8[]): boolean {
1665
1890
  if (
@@ -1675,7 +1900,10 @@ export class QuicConnection {
1675
1900
  return true
1676
1901
  }
1677
1902
 
1678
- /** Closes the connection with transport error `code`, caused by a frame of `frameType` (0 for none). */
1903
+ /**
1904
+ * Closes the connection with transport error `code`, caused by a frame of `frameType` (0 for none).
1905
+ * @internal Read by http3.ts.
1906
+ */
1679
1907
  fail(code: i64, frameType: i64): void {
1680
1908
  if (this.closed()) {
1681
1909
  return
@@ -1691,6 +1919,9 @@ export class QuicConnection {
1691
1919
  * (CONNECTION_CLOSE 0x1d; RFC 9000 §10.2). The next `takeDatagram` answers
1692
1920
  * the close, and nothing after it. Closing twice, or after the client
1693
1921
  * closed, does nothing.
1922
+ *
1923
+ * @param code The application's error code, sent in the CONNECTION_CLOSE; keeping it to 0 to 2^62 - 1 is
1924
+ * the caller's, as nothing checks it.
1694
1925
  */
1695
1926
  close(code: i64): void {
1696
1927
  if (this.closed()) {
@@ -1711,12 +1942,12 @@ export class QuicConnection {
1711
1942
  * `deadline()` names, so that a connection nobody `release()`s does not
1712
1943
  * keep them.
1713
1944
  */
1714
- startClosingPeriod(): void {
1945
+ private startClosingPeriod(): void {
1715
1946
  this.closingUntil = this.now + this.recovery.probeTimeout() * 3
1716
1947
  }
1717
1948
 
1718
1949
  /** The space of packets of `type`, or `null` for a 0-RTT or Retry packet. */
1719
- spaceOf(type: i32): QuicConnSpace | null {
1950
+ private spaceOf(type: i32): QuicConnSpace | null {
1720
1951
  if (type === QUIC_PACKET_INITIAL) {
1721
1952
  return this.initial
1722
1953
  }
@@ -1730,7 +1961,7 @@ export class QuicConnection {
1730
1961
  }
1731
1962
 
1732
1963
  /** The space of TLS level `level`. */
1733
- spaceAt(level: i32): QuicConnSpace {
1964
+ private spaceAt(level: i32): QuicConnSpace {
1734
1965
  if (level === TLS_LEVEL_INITIAL) {
1735
1966
  return this.initial
1736
1967
  }
@@ -1742,12 +1973,22 @@ export class QuicConnection {
1742
1973
  * connection: one of its active connection IDs, or, until the handshake is
1743
1974
  * done, the DCID of the client's first Initial. A closed connection still
1744
1975
  * answers for its IDs; the caller drops it once it is done with it.
1976
+ *
1977
+ * @param dcid The destination connection ID of the datagram's first packet, all of its bytes.
1978
+ * @returns True when the datagram should be handed to this connection.
1745
1979
  */
1746
1980
  ownsConnectionId(dcid: u8[]): boolean {
1747
1981
  return this.ownsConnectionIdAt(dcid, QUIC_CONN_FROM, toI32(dcid.length))
1748
1982
  }
1749
1983
 
1750
- /** `ownsConnectionId` of the ID `buf[at .. at + length)`, read in place, so a carrier routes without a copy. */
1984
+ /**
1985
+ * `ownsConnectionId` of the ID `buf[at .. at + length)`, read in place, so a carrier routes without a copy.
1986
+ *
1987
+ * @param buf The array holding the ID, usually the received datagram.
1988
+ * @param at Where the ID starts in `buf`.
1989
+ * @param length The ID's length in bytes.
1990
+ * @returns True when the datagram should be handed to this connection.
1991
+ */
1751
1992
  ownsConnectionIdAt(buf: u8[], at: i32, length: i32): boolean {
1752
1993
  if (this.cids.ownsLocalAt(buf, at, length)) {
1753
1994
  return true
@@ -1760,7 +2001,13 @@ export class QuicConnection {
1760
2001
  )
1761
2002
  }
1762
2003
 
1763
- /** `receiveWindow` of a whole datagram. */
2004
+ /**
2005
+ * `receiveWindow` of a whole datagram.
2006
+ *
2007
+ * @param datagram The UDP payload, read and never written.
2008
+ * @param now The caller's monotonic time in milliseconds.
2009
+ * @returns 0, or the transport or application error the connection has closed with.
2010
+ */
1764
2011
  receive(datagram: u8[], now: i64): i64 {
1765
2012
  return this.receiveWindow(datagram, QUIC_CONN_FROM, toI32(datagram.length), now)
1766
2013
  }
@@ -1775,6 +2022,12 @@ export class QuicConnection {
1775
2022
  * cannot be used is dropped and counted (see the module header). Once the
1776
2023
  * connection has closed, or its idle timeout has passed by `now`, every
1777
2024
  * datagram is ignored.
2025
+ *
2026
+ * @param buf The array holding the datagram; read, never written, and not kept past the call.
2027
+ * @param off Where the datagram starts in `buf`.
2028
+ * @param len The datagram's length in bytes; a client's first Initial needs at least 1200.
2029
+ * @param now The caller's monotonic time in milliseconds.
2030
+ * @returns 0, or the error the connection has closed with (also once it was already closed).
1778
2031
  */
1779
2032
  receiveWindow(buf: u8[], off: i32, len: i32, now: i64): i64 {
1780
2033
  return this.receiveWindowFrom(buf, off, len, this.none, now)
@@ -1785,6 +2038,13 @@ export class QuicConnection {
1785
2038
  * `nish:net` address: the connection follows the client from one address
1786
2039
  * to another (RFC 9000 §9) — see "Paths" in the module header. An
1787
2040
  * address of any other length is no address, as `receiveWindow` gives.
2041
+ *
2042
+ * @param buf The array holding the datagram; read, never written, and not kept past the call.
2043
+ * @param off Where the datagram starts in `buf`.
2044
+ * @param len The datagram's length in bytes; a client's first Initial needs at least 1200.
2045
+ * @param address The 18-byte `nish:net` address the datagram came from; copied, not kept.
2046
+ * @param now The caller's monotonic time in milliseconds.
2047
+ * @returns 0, or the error the connection has closed with (also once it was already closed).
1788
2048
  */
1789
2049
  receiveWindowFrom(buf: u8[], off: i32, len: i32, address: u8[], now: i64): i64 {
1790
2050
  this.runClocks(now)
@@ -1844,7 +2104,7 @@ export class QuicConnection {
1844
2104
  * `TlsServer` — is made by `establish` once the Initial has opened, so an
1845
2105
  * Initial that does not authenticate costs only its keys.
1846
2106
  */
1847
- start(buf: u8[], header: QuicHeader): boolean {
2107
+ private start(buf: u8[], header: QuicHeader): boolean {
1848
2108
  if (
1849
2109
  header.dcidLength < 8 ||
1850
2110
  header.dcidLength > QUIC_MAX_CID_LENGTH ||
@@ -1890,7 +2150,7 @@ export class QuicConnection {
1890
2150
  * `acceptRetry` set one up, and the first ID's stateless reset token
1891
2151
  * (§18.2).
1892
2152
  */
1893
- establish(): void {
2153
+ private establish(): void {
1894
2154
  const none: u8[] = this.none
1895
2155
  const token: u8[] = this.tokenScratch
1896
2156
  quicConnResetTokenInto(
@@ -1979,7 +2239,7 @@ export class QuicConnection {
1979
2239
  * One packet of a datagram. Answers whether the packet was used: whether it
1980
2240
  * authenticated as this connection's.
1981
2241
  */
1982
- receivePacket(buf: u8[], header: QuicHeader): boolean {
2242
+ private receivePacket(buf: u8[], header: QuicHeader): boolean {
1983
2243
  const type: i32 = header.type
1984
2244
  const first: boolean = this.state === QUIC_STATE_WAIT_INITIAL
1985
2245
  if (first && (type !== QUIC_PACKET_INITIAL || !this.start(buf, header))) {
@@ -1996,7 +2256,7 @@ export class QuicConnection {
1996
2256
  }
1997
2257
 
1998
2258
  /** Undoes `start`, for a first Initial that turned out not to authenticate. */
1999
- unstart(): void {
2259
+ private unstart(): void {
2000
2260
  this.state = QUIC_STATE_WAIT_INITIAL
2001
2261
  this.addressValidated = false
2002
2262
  this.idleSince = -1
@@ -2014,7 +2274,7 @@ export class QuicConnection {
2014
2274
  * the CRYPTO bytes and what it answers — runs after it. Answers whether
2015
2275
  * the packet was used.
2016
2276
  */
2017
- openPacket(buf: u8[], header: QuicHeader, first: boolean): boolean {
2277
+ private openPacket(buf: u8[], header: QuicHeader, first: boolean): boolean {
2018
2278
  const type: i32 = header.type
2019
2279
  const space: QuicConnSpace | null = this.spaceOf(type)
2020
2280
  if (space === null || !this.addressedHere(buf, header)) {
@@ -2085,7 +2345,13 @@ export class QuicConnection {
2085
2345
  * an allocation, which is what lets the block release the packet's
2086
2346
  * temporaries. Answers whether the packet was used rather than dropped.
2087
2347
  */
2088
- openScoped(buf: u8[], header: QuicHeader, space: QuicConnSpace, keys: QuicKeys, first: boolean): boolean {
2348
+ private openScoped(
2349
+ buf: u8[],
2350
+ header: QuicHeader,
2351
+ space: QuicConnSpace,
2352
+ keys: QuicKeys,
2353
+ first: boolean
2354
+ ): boolean {
2089
2355
  const packet: QuicPacket = this.packet
2090
2356
  // Every key generation shares the header-protection key (RFC 9001 §6),
2091
2357
  // so the current keys take it off whatever the Key Phase bit says.
@@ -2157,7 +2423,7 @@ export class QuicConnection {
2157
2423
  * already had — and an update past `QUIC_CONN_MAX_KEY_UPDATES` is too.
2158
2424
  * Answers whether the connection is still open.
2159
2425
  */
2160
- notePhase(pn: i64, other: boolean): boolean {
2426
+ private notePhase(pn: i64, other: boolean): boolean {
2161
2427
  if (other && !this.otherIsNext) {
2162
2428
  if (this.readPhaseLowest >= 0 && pn > this.readPhaseLowest) {
2163
2429
  this.fail(QUIC_ERROR_KEY_UPDATE, QUIC_CONN_NONE)
@@ -2192,7 +2458,7 @@ export class QuicConnection {
2192
2458
  * update, before anything acknowledges the packet; the read keys move on,
2193
2459
  * and the previous ones are kept for a probe timeout.
2194
2460
  */
2195
- applyPhase(pn: i64): void {
2461
+ private applyPhase(pn: i64): void {
2196
2462
  if (this.writePhase === this.readPhase) {
2197
2463
  this.keyUpdates = this.keyUpdates + 1
2198
2464
  if (!this.updateWriteKeys()) {
@@ -2224,7 +2490,7 @@ export class QuicConnection {
2224
2490
  * key, IV and secret wiped. Answers whether it could, which it cannot only
2225
2491
  * for keys `quicKeys` did not make.
2226
2492
  */
2227
- updateWriteKeys(): boolean {
2493
+ private updateWriteKeys(): boolean {
2228
2494
  const keys: QuicKeys | null = this.application.writeKeys
2229
2495
  if (keys === null) {
2230
2496
  return false
@@ -2253,7 +2519,7 @@ export class QuicConnection {
2253
2519
  * before it ends, so the call keeps nothing in the arena. Answers the
2254
2520
  * slot's keys, or `null` for a secret of the wrong length.
2255
2521
  */
2256
- keysFrom(slot: QuicKeysSlot, aead: i32, held: Secret<u8[]>): QuicKeys | null {
2522
+ private keysFrom(slot: QuicKeysSlot, aead: i32, held: Secret<u8[]>): QuicKeys | null {
2257
2523
  let made: boolean = false
2258
2524
  {
2259
2525
  using _scope = arena()
@@ -2273,7 +2539,12 @@ export class QuicConnection {
2273
2539
  * block, which are zeroed before it ends. Answers the slot's keys, or
2274
2540
  * `null`, leaving `next` as it was, for keys `quicKeys` did not make.
2275
2541
  */
2276
- nextKeys(slot: QuicKeysSlot, keys: QuicKeys, current: Secret<u8[]>, next: Secret<u8[]>): QuicKeys | null {
2542
+ private nextKeys(
2543
+ slot: QuicKeysSlot,
2544
+ keys: QuicKeys,
2545
+ current: Secret<u8[]>,
2546
+ next: Secret<u8[]>
2547
+ ): QuicKeys | null {
2277
2548
  let made: boolean = false
2278
2549
  {
2279
2550
  using _scope = arena()
@@ -2298,7 +2569,7 @@ export class QuicConnection {
2298
2569
  * installed and a probe timeout after each update, never while a packet is
2299
2570
  * being opened, so what a packet's Key Phase bit says shows in no timing.
2300
2571
  */
2301
- prepareNextReadKeys(): void {
2572
+ private prepareNextReadKeys(): void {
2302
2573
  const keys: QuicKeys | null = this.application.readKeys
2303
2574
  quicConnWipePacketKey(this.otherReadKeys)
2304
2575
  this.otherReadKeys = null
@@ -2321,7 +2592,7 @@ export class QuicConnection {
2321
2592
  * the original DCID), a short header to an active local ID. Read in place
2322
2593
  * from `buf`.
2323
2594
  */
2324
- addressedHere(buf: u8[], header: QuicHeader): boolean {
2595
+ private addressedHere(buf: u8[], header: QuicHeader): boolean {
2325
2596
  if (header.type === QUIC_PACKET_SHORT) {
2326
2597
  return this.cids.ownsLocalAt(buf, header.dcidStart, header.dcidLength)
2327
2598
  }
@@ -2350,7 +2621,7 @@ export class QuicConnection {
2350
2621
  * does not parse and a frame the packet type may not carry close the
2351
2622
  * connection (RFC 9000 §12.4).
2352
2623
  */
2353
- receiveFrames(
2624
+ private receiveFrames(
2354
2625
  space: QuicConnSpace,
2355
2626
  packetType: i32,
2356
2627
  payload: u8[],
@@ -2387,7 +2658,7 @@ export class QuicConnection {
2387
2658
  }
2388
2659
 
2389
2660
  /** Closes the connection with `error` from a stream frame of `type`, unless it is 0. */
2390
- failOn(error: i64, type: i64): void {
2661
+ private failOn(error: i64, type: i64): void {
2391
2662
  if (error !== QUIC_ERROR_NO_ERROR) {
2392
2663
  this.fail(error, type)
2393
2664
  }
@@ -2397,7 +2668,7 @@ export class QuicConnection {
2397
2668
  * One frame, which the packet type is allowed to carry: it starts at
2398
2669
  * `payload[frameStart]`, and the packet's header is `header` in `buf`.
2399
2670
  */
2400
- receiveFrame(
2671
+ private receiveFrame(
2401
2672
  space: QuicConnSpace,
2402
2673
  frame: QuicFrame,
2403
2674
  payload: u8[],
@@ -2507,7 +2778,7 @@ export class QuicConnection {
2507
2778
  }
2508
2779
 
2509
2780
  /** The client closed the connection: enter the draining state, sending nothing more (RFC 9000 §10.2.2). */
2510
- drain(code: i64, application: boolean): void {
2781
+ private drain(code: i64, application: boolean): void {
2511
2782
  this.state = QUIC_STATE_DRAINING
2512
2783
  this.error = code
2513
2784
  this.errorIsApplication = application
@@ -2521,7 +2792,7 @@ export class QuicConnection {
2521
2792
  * §5, §6.1, §7): each packet it newly acknowledges releases what it
2522
2793
  * carried, and each it shows lost has its frames queued again.
2523
2794
  */
2524
- receiveAck(space: QuicConnSpace, frame: QuicFrame): void {
2795
+ private receiveAck(space: QuicConnSpace, frame: QuicFrame): void {
2525
2796
  if (frame.largest >= space.nextPn || (space.level === TLS_LEVEL_APPLICATION && this.acksSkipped(frame))) {
2526
2797
  this.fail(QUIC_ERROR_PROTOCOL_VIOLATION, toI64(frame.type))
2527
2798
  return
@@ -2565,7 +2836,7 @@ export class QuicConnection {
2565
2836
  * `QUIC_FRAME_MAX_ACK_RANGES` is not, and loss recovery does not act on
2566
2837
  * it either.
2567
2838
  */
2568
- acksSkipped(frame: QuicFrame): boolean {
2839
+ private acksSkipped(frame: QuicFrame): boolean {
2569
2840
  const ranges: i64[] = frame.ackRanges
2570
2841
  for (let k: i32 = 0; k < toI32(this.skipped.length); k += 1) {
2571
2842
  const pn: i64 = this.skipped[k]
@@ -2584,7 +2855,7 @@ export class QuicConnection {
2584
2855
  * to scale is held at 2^40 microseconds first, which is still far past any
2585
2856
  * `max_ack_delay` it is then bounded by (RFC 9002 §5.3).
2586
2857
  */
2587
- ackDelayOf(field: i64): i64 {
2858
+ private ackDelayOf(field: i64): i64 {
2588
2859
  let exponent: i64 = this.peerParameters.ackDelayExponent
2589
2860
  if (exponent < 0 || exponent > 20) {
2590
2861
  exponent = 3
@@ -2595,7 +2866,7 @@ export class QuicConnection {
2595
2866
  }
2596
2867
 
2597
2868
  /** Every packet of `space` that loss recovery's last call declared lost: what each carried is queued again. */
2598
- packetsLost(space: QuicConnSpace, sent: QuicSentPackets): void {
2869
+ private packetsLost(space: QuicConnSpace, sent: QuicSentPackets): void {
2599
2870
  for (let k: i32 = 0; k < sent.lostCount; k += 1) {
2600
2871
  this.requeue(space, sent.lostSlot(k))
2601
2872
  }
@@ -2608,7 +2879,7 @@ export class QuicConnection {
2608
2879
  * one not queued already, and each stream control frame that still says
2609
2880
  * something. A DATAGRAM is never in a record, so it never goes again.
2610
2881
  */
2611
- requeue(space: QuicConnSpace, row: i32): void {
2882
+ private requeue(space: QuicConnSpace, row: i32): void {
2612
2883
  space.resendCrypto(space.cryptoOffset(row), space.cryptoLength(row))
2613
2884
  if ((space.flags(row) & QUIC_CONN_SENT_HANDSHAKE_DONE) !== 0) {
2614
2885
  this.handshakeDonePending = true
@@ -2641,7 +2912,7 @@ export class QuicConnection {
2641
2912
  }
2642
2913
 
2643
2914
  /** A lost control frame of kind `kind` (a sequence number or a stream in `value`), queued again unless it no longer matters. */
2644
- requeueControl(kind: i32, value: i64): void {
2915
+ private requeueControl(kind: i32, value: i64): void {
2645
2916
  if (kind === QUIC_CONN_CONTROL_NEW_CID) {
2646
2917
  for (const entry of this.cids.local) {
2647
2918
  if (entry.used && entry.sequence === value) {
@@ -2666,7 +2937,7 @@ export class QuicConnection {
2666
2937
  * it twice (`QUIC_CONN_HANDSHAKE_PROBES`), so that one more lost datagram
2667
2938
  * does not cost another, doubled, probe timeout.
2668
2939
  */
2669
- probe(): void {
2940
+ private probe(): void {
2670
2941
  for (let level: i32 = 0; level < 3; level += 1) {
2671
2942
  const space: QuicConnSpace = this.spaceAt(level)
2672
2943
  const sent: QuicSentPackets | null = this.recovery.space(level)
@@ -2698,7 +2969,7 @@ export class QuicConnection {
2698
2969
  * one datagram at most. The anti-amplification limit holds it like any
2699
2970
  * datagram (`takeDatagramInto`).
2700
2971
  */
2701
- resendEarly(level: i32): void {
2972
+ private resendEarly(level: i32): void {
2702
2973
  if (
2703
2974
  this.earlyResends >= QUIC_CONN_EARLY_RESENDS ||
2704
2975
  this.initial.probes > 0 ||
@@ -2740,7 +3011,7 @@ export class QuicConnection {
2740
3011
  * its alert unexpected_message (RFC 9001 §4.8). A frame too far ahead is
2741
3012
  * CRYPTO_BUFFER_EXCEEDED.
2742
3013
  */
2743
- receiveCrypto(space: QuicConnSpace, frame: QuicFrame, payload: u8[]): void {
3014
+ private receiveCrypto(space: QuicConnSpace, frame: QuicFrame, payload: u8[]): void {
2744
3015
  const reassembly: QuicConnReassembly = this.cryptoIn
2745
3016
  if (space.level < reassembly.level) {
2746
3017
  if (frame.offset + toI64(frame.dataLength) > space.cryptoInDelivered) {
@@ -2765,7 +3036,7 @@ export class QuicConnection {
2765
3036
  }
2766
3037
 
2767
3038
  /** Hands TLS what now continues the CRYPTO stream, from the buffer itself; an alert closes with CRYPTO_ERROR. */
2768
- feedTls(): void {
3039
+ private feedTls(): void {
2769
3040
  const tls: TlsServer | null = this.tls
2770
3041
  const reassembly: QuicConnReassembly = this.cryptoIn
2771
3042
  if (tls === null) {
@@ -2797,7 +3068,7 @@ export class QuicConnection {
2797
3068
  * PROTOCOL_VIOLATION (§3); otherwise its payload waits for
2798
3069
  * `readDatagram`, or is dropped when the ring is full.
2799
3070
  */
2800
- receiveDatagram(frame: QuicFrame, payload: u8[], frameStart: i32): void {
3071
+ private receiveDatagram(frame: QuicFrame, payload: u8[], frameStart: i32): void {
2801
3072
  const size: i64 = toI64(frame.end - frameStart)
2802
3073
  if (this.config.maxDatagramFrameSize === 0 || size > this.config.maxDatagramFrameSize) {
2803
3074
  // `fin` is how the frame says it was 0x31, the type with a Length.
@@ -2815,12 +3086,12 @@ export class QuicConnection {
2815
3086
  * eight bytes. Only the latest four are kept, so a burst of challenges
2816
3087
  * cannot grow anything.
2817
3088
  */
2818
- receivePathChallenge(frame: QuicFrame, payload: u8[]): void {
3089
+ private receivePathChallenge(frame: QuicFrame, payload: u8[]): void {
2819
3090
  this.queuePathResponse(payload, frame.dataStart)
2820
3091
  }
2821
3092
 
2822
3093
  /** Queues a PATH_RESPONSE of the eight bytes at `data[from]` on the current path. */
2823
- queuePathResponse(data: u8[], from: i32): void {
3094
+ private queuePathResponse(data: u8[], from: i32): void {
2824
3095
  if (this.pathCount >= 4) {
2825
3096
  this.pathHead = 0
2826
3097
  this.pathCount = 0
@@ -2843,7 +3114,7 @@ export class QuicConnection {
2843
3114
  * `probeAddress` is dropped, as a response may be: its data is about to
2844
3115
  * be overwritten.
2845
3116
  */
2846
- stageProbe(frame: QuicFrame, payload: u8[]): void {
3117
+ private stageProbe(frame: QuicFrame, payload: u8[]): void {
2847
3118
  quicConnCopyAt(this.probeData, 0, payload, frame.dataStart, QUIC_PATH_DATA_SIZE)
2848
3119
  this.probeOwed = false
2849
3120
  this.probeStaged = true
@@ -2856,7 +3127,7 @@ export class QuicConnection {
2856
3127
  * ignored; before that, none can be expected, and it is a
2857
3128
  * PROTOCOL_VIOLATION (§19.18).
2858
3129
  */
2859
- receivePathResponse(frame: QuicFrame, payload: u8[]): void {
3130
+ private receivePathResponse(frame: QuicFrame, payload: u8[]): void {
2860
3131
  if (this.challengesDrawn === 0) {
2861
3132
  this.fail(QUIC_ERROR_PROTOCOL_VIOLATION, toI64(QUIC_FRAME_PATH_RESPONSE))
2862
3133
  return
@@ -2991,7 +3262,7 @@ export class QuicConnection {
2991
3262
  * when they do not. The streams take the client's limits from them, and
2992
3263
  * the datagrams its `max_datagram_frame_size`.
2993
3264
  */
2994
- checkPeerParameters(tls: TlsServer): boolean {
3265
+ private checkPeerParameters(tls: TlsServer): boolean {
2995
3266
  const p: QuicTransportParameters = quicParseTransportParametersInto(
2996
3267
  this.parsedParameters,
2997
3268
  tls.clientTransportParameters,
@@ -3023,7 +3294,7 @@ export class QuicConnection {
3023
3294
  }
3024
3295
 
3025
3296
  /** Discards a space's keys and CRYPTO state (RFC 9001 §4.9), wiping the keys. */
3026
- discard(space: QuicConnSpace): void {
3297
+ private discard(space: QuicConnSpace): void {
3027
3298
  if (space.discarded) {
3028
3299
  return
3029
3300
  }
@@ -3049,7 +3320,7 @@ export class QuicConnection {
3049
3320
  * the seed and need no more entropy. It runs in the connection's HMAC
3050
3321
  * scratch inside an arena block, and wipes the MAC and the scratch.
3051
3322
  */
3052
- deriveConnectionId(sequence: i64): void {
3323
+ private deriveConnectionId(sequence: i64): void {
3053
3324
  const mac: HmacSha256Scratch = this.kdf.sha256
3054
3325
  using _scope = arena()
3055
3326
  const counter: u8[] = new Array<u8>(8)
@@ -3080,7 +3351,7 @@ export class QuicConnection {
3080
3351
  * in the slot's scratch and copied into the table, so issuing keeps
3081
3352
  * nothing (QUIC-3).
3082
3353
  */
3083
- topUpConnectionIds(): void {
3354
+ private topUpConnectionIds(): void {
3084
3355
  if (!this.handshakeComplete) {
3085
3356
  return
3086
3357
  }
@@ -3107,7 +3378,13 @@ export class QuicConnection {
3107
3378
  }
3108
3379
  }
3109
3380
 
3110
- /** What CertificateVerify must sign while the handshake waits for it, else `null` (see `TlsServer.signatureInput`). */
3381
+ /**
3382
+ * What CertificateVerify must sign while the handshake waits for it, else `null` (see
3383
+ * `TlsServer.signatureInput`).
3384
+ *
3385
+ * @returns The bytes to sign with the certificate's key, or `null` when no signature is owed or the
3386
+ * connection has closed.
3387
+ */
3111
3388
  signatureInput(): u8[] | null {
3112
3389
  const tls: TlsServer | null = this.tls
3113
3390
  if (tls === null || this.closed()) {
@@ -3121,6 +3398,12 @@ export class QuicConnection {
3121
3398
  * rest of the server's flight. Answers 0, or the CRYPTO_ERROR the
3122
3399
  * connection closed with when TLS refused it (a call at the wrong time, or
3123
3400
  * an empty signature: `internal_error`).
3401
+ *
3402
+ * @param signature The certificate key's signature over `signatureInput()`, in the encoding the
3403
+ * negotiated TLS signature scheme uses.
3404
+ * @returns 0 when TLS took the signature, or the error the connection has closed with (also when it
3405
+ * was already closed). It is also 0, with nothing signed, when the connection has no TLS session
3406
+ * and has not failed.
3124
3407
  */
3125
3408
  sign(signature: u8[]): i64 {
3126
3409
  const tls: TlsServer | null = this.tls
@@ -3143,7 +3426,7 @@ export class QuicConnection {
3143
3426
  * for none. Until the client's transport parameters are read, the server's
3144
3427
  * own alone.
3145
3428
  */
3146
- idleTimeout(): i64 {
3429
+ private idleTimeout(): i64 {
3147
3430
  const local: i64 = this.config.maxIdleTimeout
3148
3431
  const peer: i64 = this.peerParameters.maxIdleTimeout
3149
3432
  let timeout: i64 = local
@@ -3160,7 +3443,11 @@ export class QuicConnection {
3160
3443
  return timeout < floor ? floor : timeout
3161
3444
  }
3162
3445
 
3163
- /** When the idle timeout passes, or -1 when there is none or the timer has not started. */
3446
+ /**
3447
+ * When the idle timeout passes, or -1 when there is none or the timer has not started.
3448
+ *
3449
+ * @returns The time in the caller's milliseconds, or -1.
3450
+ */
3164
3451
  idleDeadline(): i64 {
3165
3452
  const timeout: i64 = this.idleTimeout()
3166
3453
  return timeout >= 0 && this.idleSince >= 0 ? this.idleSince + timeout : -1
@@ -3177,6 +3464,8 @@ export class QuicConnection {
3177
3464
  * gone out (`takeDatagram` sends it), once a closed connection's keys are
3178
3465
  * wiped, and with no idle timeout, nothing in flight, no update pending
3179
3466
  * and no path being validated.
3467
+ *
3468
+ * @returns The time in the caller's milliseconds at which to call `handleTimer`, or -1.
3180
3469
  */
3181
3470
  deadline(): i64 {
3182
3471
  if (this.closed()) {
@@ -3207,6 +3496,8 @@ export class QuicConnection {
3207
3496
  * Whether the anti-amplification limit stops the server sending (RFC 9000
3208
3497
  * §8.1): the client's address is not validated and three times what it
3209
3498
  * sent leaves no room for a whole datagram.
3499
+ *
3500
+ * @returns True while the address (or a new path being validated) has no budget left for a datagram.
3210
3501
  */
3211
3502
  amplificationBlocked(): boolean {
3212
3503
  return (
@@ -3220,23 +3511,26 @@ export class QuicConnection {
3220
3511
  * what came from it since the connection moved there, less what went
3221
3512
  * (RFC 9000 §9.3.1, §8.1). Unlimited, as `QUIC_MAX_VARINT`, on a validated path.
3222
3513
  */
3223
- pathBudget(): i64 {
3514
+ private pathBudget(): i64 {
3224
3515
  return this.pathValidating ? this.pathReceived * 3 - this.pathSent : QUIC_MAX_VARINT
3225
3516
  }
3226
3517
 
3227
3518
  /**
3228
- * Runs whatever is due by `now`, the caller's monotonic time in
3229
- * milliseconds; call it at `deadline()`, or any time. Past the idle timeout
3230
- * the connection closes silently (`QUIC_STATE_TIMED_OUT`, RFC 9000 §10.1):
3231
- * no CONNECTION_CLOSE, every key wiped. Past the closing or draining
3232
- * period of a connection that closed, every key is wiped (§10.2). Past a
3233
- * key update's probe timeout the previous read keys are wiped and the next
3234
- * generation's derived (RFC 9001 §6.5). Past loss recovery's timer, packets lost by the time
3235
- * threshold have their frames queued again, or a probe timeout queues
3236
- * every space's packets in flight and asks for a probe (RFC 9002 §6.1.2,
3237
- * §6.2.4): `takeDatagram` then sends it. `receive` and `takeDatagram` run
3238
- * it themselves. Time never runs backwards here: an earlier `now` than one
3239
- * already given counts as that one.
3519
+ * Runs whatever is due by `now`, the caller's monotonic time in milliseconds;
3520
+ * call it at `deadline()`, or any time. Past the idle timeout the connection
3521
+ * closes silently (`QUIC_STATE_TIMED_OUT`, RFC 9000 §10.1): no
3522
+ * CONNECTION_CLOSE, every key wiped. Past the closing or draining period of a
3523
+ * connection that closed, every key is wiped (§10.2). Past a key update's
3524
+ * probe timeout the previous read keys are wiped and the next generation's
3525
+ * derived (RFC 9001 §6.5).
3526
+ *
3527
+ * Past loss recovery's timer, packets lost by the time threshold have their
3528
+ * frames queued again, or a probe timeout queues every space's packets in
3529
+ * flight and asks for a probe (RFC 9002 §6.1.2, §6.2.4): `takeDatagram` then
3530
+ * sends it. `receive` and `takeDatagram` run it themselves. Time never runs
3531
+ * backwards here: an earlier `now` than one already given counts as that one.
3532
+ *
3533
+ * @param now The caller's monotonic time in milliseconds.
3240
3534
  */
3241
3535
  handleTimer(now: i64): void {
3242
3536
  this.runClocks(now)
@@ -3247,7 +3541,7 @@ export class QuicConnection {
3247
3541
  * `handleTimer`'s idle timeout, key retention and the end of the closing
3248
3542
  * or draining period, which `receive` runs before it reads a datagram.
3249
3543
  */
3250
- runClocks(now: i64): void {
3544
+ private runClocks(now: i64): void {
3251
3545
  if (now > this.now) {
3252
3546
  this.now = now
3253
3547
  }
@@ -3287,7 +3581,7 @@ export class QuicConnection {
3287
3581
  * move, so there is always an address to go back to: the case where
3288
3582
  * there is none, which RFC 9000 §9.3.2 closes silently, cannot arise.
3289
3583
  */
3290
- runPathClock(): void {
3584
+ private runPathClock(): void {
3291
3585
  if (!this.pathValidating) {
3292
3586
  return
3293
3587
  }
@@ -3311,7 +3605,7 @@ export class QuicConnection {
3311
3605
  * from elsewhere cannot move the connection, nor take the server's
3312
3606
  * flight. A carrier that passes no address leaves every path rule off.
3313
3607
  */
3314
- notePath(address: u8[]): boolean {
3608
+ private notePath(address: u8[]): boolean {
3315
3609
  this.arrivalOther = false
3316
3610
  this.migrateOwed = false
3317
3611
  this.probeStaged = false
@@ -3343,7 +3637,7 @@ export class QuicConnection {
3343
3637
  * (`takeProbeInto`). A datagram that authenticated nothing does neither.
3344
3638
  * What came from the path being validated adds to its limit.
3345
3639
  */
3346
- afterPath(counted: boolean, len: i32): void {
3640
+ private afterPath(counted: boolean, len: i32): void {
3347
3641
  if (!counted) {
3348
3642
  return
3349
3643
  }
@@ -3378,7 +3672,7 @@ export class QuicConnection {
3378
3672
  * NAT rebinding never does (§9.5). Answers whether the challenge staged
3379
3673
  * in `probeData` is already queued on the new path (`sendResponsesBack`).
3380
3674
  */
3381
- migrate(address: u8[]): boolean {
3675
+ private migrate(address: u8[]): boolean {
3382
3676
  const swapped: boolean = this.sendResponsesBack()
3383
3677
  quicConnCopyAt(this.pathCurrent, 0, address, 0, QUIC_CONN_ADDRESS_SIZE)
3384
3678
  if (quicConnSameAddress(address, this.pathValidated, false)) {
@@ -3404,7 +3698,7 @@ export class QuicConnection {
3404
3698
  * and the answer is true; otherwise false. A response still owed on the
3405
3699
  * probe route is never overwritten: the ring's are dropped instead.
3406
3700
  */
3407
- sendResponsesBack(): boolean {
3701
+ private sendResponsesBack(): boolean {
3408
3702
  if (this.pathCount <= 0) {
3409
3703
  return false
3410
3704
  }
@@ -3438,7 +3732,7 @@ export class QuicConnection {
3438
3732
  * them with the two before, and owes the challenge; its resend is due a
3439
3733
  * probe timeout later.
3440
3734
  */
3441
- drawChallenge(): void {
3735
+ private drawChallenge(): void {
3442
3736
  this.deriveConnectionId(QUIC_CONN_CHALLENGE_SEQUENCE + this.challengesDrawn)
3443
3737
  const slot: i32 = toI32(this.challengesDrawn % toI64(QUIC_CONN_CHALLENGES))
3444
3738
  quicConnCopyAt(this.challengeData, slot * QUIC_PATH_DATA_SIZE, this.cidScratch, 0, QUIC_PATH_DATA_SIZE)
@@ -3458,7 +3752,7 @@ export class QuicConnection {
3458
3752
  * network path behind a rebinding NAT (§9.4); ECN is tested again either
3459
3753
  * way (§13.4.2).
3460
3754
  */
3461
- pathConfirmed(): void {
3755
+ private pathConfirmed(): void {
3462
3756
  const portOnly: boolean = quicConnSameAddress(this.pathCurrent, this.pathValidated, true)
3463
3757
  quicConnCopyAt(this.pathValidated, 0, this.pathCurrent, 0, QUIC_CONN_ADDRESS_SIZE)
3464
3758
  this.endValidation()
@@ -3469,7 +3763,7 @@ export class QuicConnection {
3469
3763
  }
3470
3764
 
3471
3765
  /** Stops validating a path: no challenge is owed, kept or timed. */
3472
- endValidation(): void {
3766
+ private endValidation(): void {
3473
3767
  this.pathValidating = false
3474
3768
  this.challengeOwed = false
3475
3769
  this.challengeCount = 0
@@ -3480,14 +3774,14 @@ export class QuicConnection {
3480
3774
  }
3481
3775
 
3482
3776
  /** Adds `n` bytes sent to the path being validated's count. */
3483
- notePathSent(n: i32): void {
3777
+ private notePathSent(n: i32): void {
3484
3778
  if (this.pathValidating) {
3485
3779
  this.pathSent = this.pathSent + toI64(n)
3486
3780
  }
3487
3781
  }
3488
3782
 
3489
3783
  /** Clears every path field for a new connection, in the arrays the slot keeps. */
3490
- forgetPath(): void {
3784
+ private forgetPath(): void {
3491
3785
  quicConnZeroAt(this.pathCurrent)
3492
3786
  quicConnZeroAt(this.pathValidated)
3493
3787
  quicConnZeroAt(this.pathArrival)
@@ -3511,6 +3805,9 @@ export class QuicConnection {
3511
3805
  * address `receiveWindowFrom` last moved it to, or the first Initial's.
3512
3806
  * A carrier sends every datagram of `takeDatagramInto` here; it is empty
3513
3807
  * (all zero) for a connection fed by `receiveWindow` alone.
3808
+ *
3809
+ * @returns The connection's own address array, borrowed: it changes in place when the connection
3810
+ * moves, so copy it to keep it.
3514
3811
  */
3515
3812
  sendAddress(): u8[] {
3516
3813
  return this.pathCurrent
@@ -3524,6 +3821,13 @@ export class QuicConnection {
3524
3821
  * path (§8.1), whichever is less. The carrier sends it to
3525
3822
  * `probeAddress`, before `takeDatagramInto`'s datagrams. It is answered
3526
3823
  * once and never sent again (§13.3).
3824
+ *
3825
+ * @param out Where the datagram is written.
3826
+ * @param at Where in `out` it starts; `out` needs `QUIC_CONN_DATAGRAM_SIZE` bytes from here.
3827
+ * @param now The caller's monotonic time in milliseconds; timers due by then run first.
3828
+ * @returns The datagram's length in bytes, or 0 when no PATH_RESPONSE is owed, the connection is closed,
3829
+ * the handshake is not complete, the room is short, or the packet cannot be built within the path's budget (the
3830
+ * owed PATH_RESPONSE is then dropped).
3527
3831
  */
3528
3832
  takeProbeInto(out: u8[], at: i32, now: i64): i32 {
3529
3833
  this.handleTimer(now)
@@ -3551,7 +3855,7 @@ export class QuicConnection {
3551
3855
  }
3552
3856
 
3553
3857
  /** `takeProbeInto`'s packet, of `size` bytes at `out[at]`, built and sealed; 0 when it does not fit. */
3554
- buildProbe(out: u8[], at: i32, size: i32): i32 {
3858
+ private buildProbe(out: u8[], at: i32, size: i32): i32 {
3555
3859
  const space: QuicConnSpace = this.application
3556
3860
  const overhead: i32 = this.overhead(space)
3557
3861
  if (overhead === 0 || size < overhead + 1 + QUIC_PATH_DATA_SIZE) {
@@ -3575,7 +3879,7 @@ export class QuicConnection {
3575
3879
  }
3576
3880
 
3577
3881
  /** Loss recovery's timer, if it is due (RFC 9002 §6.2, A.9). */
3578
- runRecoveryTimer(): void {
3882
+ private runRecoveryTimer(): void {
3579
3883
  if (this.closed() || this.state === QUIC_STATE_WAIT_INITIAL) {
3580
3884
  return
3581
3885
  }
@@ -3600,6 +3904,8 @@ export class QuicConnection {
3600
3904
  * or answered it less than a probe timeout ago, so the next read keys are
3601
3905
  * not ready), and until the client has acknowledged a packet sent with the
3602
3906
  * current keys.
3907
+ *
3908
+ * @returns True when the update started, false when it may not yet.
3603
3909
  */
3604
3910
  updateKeys(): boolean {
3605
3911
  if (
@@ -3620,7 +3926,7 @@ export class QuicConnection {
3620
3926
  * `QUIC_CONN_SKIP_SEQUENCE`, so a client that saw where the last gaps fell
3621
3927
  * cannot tell where the next will.
3622
3928
  */
3623
- scheduleSkip(from: i64): void {
3929
+ private scheduleSkip(from: i64): void {
3624
3930
  this.deriveConnectionId(QUIC_CONN_SKIP_SEQUENCE + this.skipsDrawn)
3625
3931
  this.skipsDrawn = this.skipsDrawn + 1
3626
3932
  let draw: i64 = 0
@@ -3639,6 +3945,8 @@ export class QuicConnection {
3639
3945
  * stopped, or it has room to write again after a short write — or -1
3640
3946
  * when there is none. Each stream comes up once however much happened;
3641
3947
  * read and write it until it has nothing more to say.
3948
+ *
3949
+ * @returns A stream ID, or -1.
3642
3950
  */
3643
3951
  nextStreamEvent(): i64 {
3644
3952
  return this.streams.nextEvent()
@@ -3650,6 +3958,9 @@ export class QuicConnection {
3650
3958
  * `QUIC_STREAM_ERR_STATE` when not connected, or `QUIC_STREAM_ERR_LIMIT`
3651
3959
  * when the client's MAX_STREAMS is reached (STREAMS_BLOCKED then goes out)
3652
3960
  * or `localStreams` of the server's are open.
3961
+ *
3962
+ * @param bidirectional True for a stream both sides send on, false for one only the server sends on.
3963
+ * @returns The new stream's ID, or a negative `QUIC_STREAM_ERR_*`.
3653
3964
  */
3654
3965
  openStream(bidirectional: boolean): i64 {
3655
3966
  if (this.state !== QUIC_STATE_CONNECTED) {
@@ -3667,6 +3978,13 @@ export class QuicConnection {
3667
3978
  * only the client sends on, one already finished or reset, or one the
3668
3979
  * client asked to stop (STOP_SENDING, which this side answered with
3669
3980
  * RESET_STREAM).
3981
+ *
3982
+ * @param id The stream's ID.
3983
+ * @param buf The bytes to send; copied into the stream's buffer, so the caller may reuse it.
3984
+ * @param from Where the bytes start in `buf`.
3985
+ * @param length How many bytes to offer.
3986
+ * @param fin True to finish the sending side once all `length` bytes are taken.
3987
+ * @returns The number of bytes taken, 0 to `length`, or a negative `QUIC_STREAM_ERR_*`.
3670
3988
  */
3671
3989
  streamWrite(id: i64, buf: u8[], from: i32, length: i32, fin: boolean): i32 {
3672
3990
  if (this.state !== QUIC_STATE_CONNECTED) {
@@ -3681,12 +3999,26 @@ export class QuicConnection {
3681
3999
  * once every byte up to the FIN was read, `QUIC_STREAM_ERR_RESET` once the
3682
4000
  * client reset the stream (its code is the stream's `resetCode`), or
3683
4001
  * another `QUIC_STREAM_ERR_*`. Reading is what gives the client more credit.
4002
+ *
4003
+ * @param id The stream's ID.
4004
+ * @param buf Where the bytes go.
4005
+ * @param at Where in `buf` they start.
4006
+ * @param length The most bytes to read.
4007
+ * @returns The number of bytes read, 0, `QUIC_STREAM_END`, or a negative `QUIC_STREAM_ERR_*`.
3684
4008
  */
3685
4009
  streamRead(id: i64, buf: u8[], at: i32, length: i32): i32 {
3686
4010
  return this.streams.read(id, buf, at, length)
3687
4011
  }
3688
4012
 
3689
- /** Abandons stream `id`'s sending side with application error `code` (RESET_STREAM). Answers `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`. */
4013
+ /**
4014
+ * Abandons stream `id`'s sending side with application error `code` (RESET_STREAM). Answers
4015
+ * `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`.
4016
+ *
4017
+ * @param id The stream's ID; it must be one the server sends on.
4018
+ * @param code The application's error code, sent to the client in RESET_STREAM.
4019
+ * @returns `QUIC_STREAM_OK`, or a negative `QUIC_STREAM_ERR_*` (`QUIC_STREAM_ERR_STATE` when not
4020
+ * connected).
4021
+ */
3690
4022
  streamReset(id: i64, code: i64): i32 {
3691
4023
  if (this.state !== QUIC_STATE_CONNECTED) {
3692
4024
  return QUIC_STREAM_ERR_STATE
@@ -3694,7 +4026,15 @@ export class QuicConnection {
3694
4026
  return this.streams.resetStream(id, code)
3695
4027
  }
3696
4028
 
3697
- /** Asks the client to stop sending on stream `id`, with application error `code` (STOP_SENDING). Answers `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`. */
4029
+ /**
4030
+ * Asks the client to stop sending on stream `id`, with application error `code` (STOP_SENDING).
4031
+ * Answers `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`.
4032
+ *
4033
+ * @param id The stream's ID; it must be one the client sends on.
4034
+ * @param code The application's error code, sent to the client in STOP_SENDING.
4035
+ * @returns `QUIC_STREAM_OK`, or a negative `QUIC_STREAM_ERR_*` (`QUIC_STREAM_ERR_STATE` when not
4036
+ * connected).
4037
+ */
3698
4038
  streamStopSending(id: i64, code: i64): i32 {
3699
4039
  if (this.state !== QUIC_STATE_CONNECTED) {
3700
4040
  return QUIC_STREAM_ERR_STATE
@@ -3707,6 +4047,8 @@ export class QuicConnection {
3707
4047
  * none: the all-at-once form of `nextStreamEvent` and `streamRead`, which
3708
4048
  * allocates the run it answers. `fin` marks the last of its stream; a
3709
4049
  * stream the client reset is passed over.
4050
+ *
4051
+ * @returns A new run the caller owns, or `null` when no stream has data or a FIN waiting.
3710
4052
  */
3711
4053
  readStream(): QuicStreamData | null {
3712
4054
  let id: i64 = this.streams.nextEvent()
@@ -3736,6 +4078,12 @@ export class QuicConnection {
3736
4078
  * connected, the stream is unknown, its side is already finished (or the
3737
4079
  * client stopped it), or the data would pass the credit the client gave
3738
4080
  * for the stream or the connection, or the room in the stream's buffer.
4081
+ *
4082
+ * @param id The stream's ID.
4083
+ * @param data The bytes to send; copied, so the caller may reuse it.
4084
+ * @param fin True to finish the sending side after `data`.
4085
+ * @returns `QUIC_STREAM_OK` when every byte was queued, or a negative `QUIC_STREAM_ERR_*` with nothing
4086
+ * queued.
3739
4087
  */
3740
4088
  writeStream(id: i64, data: u8[], fin: boolean): i32 {
3741
4089
  if (this.state !== QUIC_STATE_CONNECTED) {
@@ -3767,6 +4115,8 @@ export class QuicConnection {
3767
4115
  * client's `max_datagram_frame_size` with the frame's type and Length, and
3768
4116
  * a 1-RTT packet of `QUIC_CONN_DATAGRAM_SIZE` with the longest packet
3769
4117
  * number; 0 when the client takes none, or the server sends none.
4118
+ *
4119
+ * @returns The payload size in bytes, 0 up to a little under `QUIC_CONN_DATAGRAM_SIZE`.
3770
4120
  */
3771
4121
  maxDatagramPayload(): i32 {
3772
4122
  const entry: QuicCidEntry | null = this.cids.currentPeerEntry()
@@ -3798,6 +4148,11 @@ export class QuicConnection {
3798
4148
  * none, `QUIC_DATAGRAM_ERR_TOO_BIG` for a frame past the client's limit or
3799
4149
  * a payload past `maxDatagramPayload()`, or `QUIC_DATAGRAM_ERR_FULL` while
3800
4150
  * the ring holds `QUIC_CONN_DATAGRAM_QUEUE` waiting.
4151
+ *
4152
+ * @param buf The array holding the payload; copied into the ring, so the caller may reuse it.
4153
+ * @param from Where the payload starts in `buf`.
4154
+ * @param length The payload's length in bytes.
4155
+ * @returns `QUIC_DATAGRAM_OK`, or one of the `QUIC_DATAGRAM_ERR_*` codes above with nothing queued.
3801
4156
  */
3802
4157
  sendDatagram(buf: u8[], from: i32, length: i32): i32 {
3803
4158
  if (
@@ -3825,6 +4180,11 @@ export class QuicConnection {
3825
4180
  * at most `cap` bytes, and forgets it. Answers its length,
3826
4181
  * `QUIC_DATAGRAM_NONE` when none is waiting, or `QUIC_DATAGRAM_ERR_ROOM`,
3827
4182
  * keeping it, when it does not fit.
4183
+ *
4184
+ * @param buf Where the payload goes.
4185
+ * @param at Where in `buf` it starts.
4186
+ * @param cap The most bytes `buf` has room for from `at`.
4187
+ * @returns The payload's length in bytes, `QUIC_DATAGRAM_NONE`, or `QUIC_DATAGRAM_ERR_ROOM`.
3828
4188
  */
3829
4189
  readDatagram(buf: u8[], at: i32, cap: i32): i32 {
3830
4190
  return this.datagramsIn.pop(buf, at, cap)
@@ -3835,6 +4195,9 @@ export class QuicConnection {
3835
4195
  /**
3836
4196
  * The next datagram to send, or `null` when nothing is due: the
3837
4197
  * allocating form of `takeDatagramInto`, which a test or a recording uses.
4198
+ *
4199
+ * @param now The caller's monotonic time in milliseconds.
4200
+ * @returns A new array the caller owns, at most `QUIC_CONN_DATAGRAM_SIZE` bytes, or `null`.
3838
4201
  */
3839
4202
  takeDatagram(now: i64): u8[] | null {
3840
4203
  const out: u8[] = new Array<u8>(QUIC_CONN_DATAGRAM_SIZE)
@@ -3844,25 +4207,32 @@ export class QuicConnection {
3844
4207
 
3845
4208
  /**
3846
4209
  * Writes the next datagram to send into `out` at `at`, which needs
3847
- * `QUIC_CONN_DATAGRAM_SIZE` bytes of room, and answers its length, or 0
3848
- * when nothing is due (or the room is not there). Call it until it answers
3849
- * 0 after every `receive`, `sign`, write and `close`. It coalesces an
3850
- * Initial, a Handshake and a 1-RTT packet as each has something to carry,
3851
- * pads a datagram with an ack-eliciting Initial to 1200 bytes (RFC 9000
3852
- * §14.1), and before the client's address is validated sends only while
3853
- * three times what was received allows (§8.1). After a close it writes the
3854
- * CONNECTION_CLOSE once, then nothing. While the handshake waits for a
3855
- * signature it writes nothing, so the server's flight leaves whole. Lost
3856
- * data goes before new data. While the congestion window is full (RFC
3857
- * 9002 §7), or a space's record of packets in flight is, a space sends
3858
- * only an ACK, unless a probe timeout asked it for a probe. `now` is the
3859
- * caller's time in milliseconds: a datagram that elicits an
4210
+ * `QUIC_CONN_DATAGRAM_SIZE` bytes of room, and answers its length, or 0 when
4211
+ * nothing is due (or the room is not there). Call it until it answers 0 after
4212
+ * every `receive`, `sign`, write and `close`. It coalesces an Initial, a
4213
+ * Handshake and a 1-RTT packet as each has something to carry, pads a
4214
+ * datagram with an ack-eliciting Initial to 1200 bytes (RFC 9000 §14.1), and
4215
+ * before the client's address is validated sends only while three times what
4216
+ * was received allows (§8.1).
4217
+ *
4218
+ * After a close it writes the CONNECTION_CLOSE once, then nothing. While the
4219
+ * handshake waits for a signature it writes nothing, so the server's flight
4220
+ * leaves whole. Lost data goes before new data. While the congestion window
4221
+ * is full (RFC 9002 §7), or a space's record of packets in flight is, a space
4222
+ * sends only an ACK, unless a probe timeout asked it for a probe.
4223
+ *
4224
+ * `now` is the caller's time in milliseconds: a datagram that elicits an
3860
4225
  * acknowledgement restarts the idle timer when it is the first since the
3861
- * client's last packet (RFC 9000 §10.1), and once the idle timeout has
3862
- * passed nothing goes out. The datagram is built and sealed in place,
3863
- * inside an arena block: nothing it does allocates past the call. This is
3864
- * not paced: a carrier that paces calls `nish/net/quic-listener`'s
4226
+ * client's last packet (RFC 9000 §10.1), and once the idle timeout has passed
4227
+ * nothing goes out. The datagram is built and sealed in place, inside an
4228
+ * arena block: nothing it does allocates past the call. This is not paced: a
4229
+ * carrier that paces calls `nish/net/quic-listener`'s
3865
4230
  * `quicListenerTakeFlight` instead.
4231
+ *
4232
+ * @param out Where the datagram is written.
4233
+ * @param at Where in `out` it starts; `out` needs `QUIC_CONN_DATAGRAM_SIZE` bytes from here.
4234
+ * @param now The caller's monotonic time in milliseconds; timers due by then run first.
4235
+ * @returns The datagram's length in bytes, at most `QUIC_CONN_DATAGRAM_SIZE`, or 0.
3866
4236
  */
3867
4237
  takeDatagramInto(out: u8[], at: i32, now: i64): i32 {
3868
4238
  this.handleTimer(now)
@@ -3908,7 +4278,7 @@ export class QuicConnection {
3908
4278
  * the packet number due to be skipped is (RFC 9000 §21.4): remembered,
3909
4279
  * never sealed nor recorded, and the next skip drawn.
3910
4280
  */
3911
- beforeDatagram(): void {
4281
+ private beforeDatagram(): void {
3912
4282
  const limit: i64 = this.confidentialityLimit
3913
4283
  if (this.sealedUnderKey >= limit - limit / 4) {
3914
4284
  this.updateKeys()
@@ -3931,7 +4301,7 @@ export class QuicConnection {
3931
4301
  * last padded when an Initial elicits, then each sealed where it lies.
3932
4302
  * Answers the datagram's length, or 0 when no space had anything.
3933
4303
  */
3934
- buildDatagram(out: u8[], at: i32): i32 {
4304
+ private buildDatagram(out: u8[], at: i32): i32 {
3935
4305
  let remaining: i32 = QUIC_CONN_DATAGRAM_SIZE
3936
4306
  // A path being validated has its own 3x limit (§9.3.1), which the
3937
4307
  // datagram may not pass; `amplificationBlocked` kept it above the floor.
@@ -4031,7 +4401,7 @@ export class QuicConnection {
4031
4401
  * its packet number, and the AEAD tag. 0 when the space cannot send: no
4032
4402
  * keys, discarded, or no packet number length for its next number.
4033
4403
  */
4034
- overhead(space: QuicConnSpace): i32 {
4404
+ private overhead(space: QuicConnSpace): i32 {
4035
4405
  if (space.writeKeys === null || space.discarded) {
4036
4406
  return 0
4037
4407
  }
@@ -4062,7 +4432,7 @@ export class QuicConnection {
4062
4432
  * written to the space's staging row, and `stagedEliciting` says whether
4063
4433
  * anything elicits an acknowledgement.
4064
4434
  */
4065
- buildPayloadInto(
4435
+ private buildPayloadInto(
4066
4436
  space: QuicConnSpace,
4067
4437
  out: u8[],
4068
4438
  start: i32,
@@ -4101,7 +4471,7 @@ export class QuicConnection {
4101
4471
  * level's kept stream, then those not sent yet. The level's CRYPTO stream
4102
4472
  * is kept whole, so a byte's index in `cryptoOut` is its offset.
4103
4473
  */
4104
- buildCrypto(space: QuicConnSpace, out: u8[], at: i32, end: i32): i32 {
4474
+ private buildCrypto(space: QuicConnSpace, out: u8[], at: i32, end: i32): i32 {
4105
4475
  if (space.cryptoResendLow >= 0) {
4106
4476
  const low: i64 = space.cryptoResendLow
4107
4477
  const want: i32 = toI32(space.cryptoResendHigh - low)
@@ -4157,7 +4527,7 @@ export class QuicConnection {
4157
4527
  * (RFC 9000 §9.3); the path's own 3x limit still bounds them, and a
4158
4528
  * challenge is drawn at most once a probe timeout.
4159
4529
  */
4160
- buildPathFrames(space: QuicConnSpace, out: u8[], start: i32, end: i32): i32 {
4530
+ private buildPathFrames(space: QuicConnSpace, out: u8[], start: i32, end: i32): i32 {
4161
4531
  let at: i32 = start
4162
4532
  while (this.pathCount > 0 && end - at >= 9) {
4163
4533
  const next: i32 = quicPutPathData(
@@ -4205,7 +4575,7 @@ export class QuicConnection {
4205
4575
  * the packet's record: `QUIC_CONN_PACKET_CONTROL` control frames and
4206
4576
  * `QUIC_CONN_PACKET_STREAMS` STREAM frames, and every DATAGRAM that fits.
4207
4577
  */
4208
- buildApplication(space: QuicConnSpace, out: u8[], start: i32, end: i32): i32 {
4578
+ private buildApplication(space: QuicConnSpace, out: u8[], start: i32, end: i32): i32 {
4209
4579
  let at: i32 = start
4210
4580
  if (this.handshakeDonePending && at < end) {
4211
4581
  at = quicPutTypeOnly(out, at, end, QUIC_FRAME_HANDSHAKE_DONE)
@@ -4281,7 +4651,7 @@ export class QuicConnection {
4281
4651
  * the packet's length, or 0 when it could not be sealed, which only keys
4282
4652
  * `quicKeys` did not make cause.
4283
4653
  */
4284
- sealAt(space: QuicConnSpace, out: u8[], start: i32, payloadEnd: i32): i32 {
4654
+ private sealAt(space: QuicConnSpace, out: u8[], start: i32, payloadEnd: i32): i32 {
4285
4655
  // RFC 9001 §6.6: at the confidentiality limit, with no key update made
4286
4656
  // (`beforeDatagram`), the connection closes, and no packet, its
4287
4657
  // CONNECTION_CLOSE included, is sealed under the key again.
@@ -4347,7 +4717,7 @@ export class QuicConnection {
4347
4717
  * what the packet carried is queued again at once, as though lost.
4348
4718
  * Answers the slot recovery recorded it in, or -1.
4349
4719
  */
4350
- recordSent(space: QuicConnSpace, pn: i64, size: i32): i32 {
4720
+ private recordSent(space: QuicConnSpace, pn: i64, size: i32): i32 {
4351
4721
  if (!space.stagedEliciting) {
4352
4722
  return -1
4353
4723
  }
@@ -4371,7 +4741,7 @@ export class QuicConnection {
4371
4741
  }
4372
4742
 
4373
4743
  /** An acknowledged packet in row `row`: each stream chunk and stream control frame it carried is settled. */
4374
- untrack(space: QuicConnSpace, row: i32): void {
4744
+ private untrack(space: QuicConnSpace, row: i32): void {
4375
4745
  const streams: i32 = space.streamCount(row)
4376
4746
  for (let j: i32 = 0; j < streams; j += 1) {
4377
4747
  const at: i32 = row * QUIC_CONN_PACKET_STREAMS + j
@@ -4408,7 +4778,7 @@ export class QuicConnection {
4408
4778
  * 0x1d (§12.4). Before the handshake is complete no 1-RTT packet carries
4409
4779
  * it, because the client cannot yet read one.
4410
4780
  */
4411
- takeCloseInto(out: u8[], at: i32): i32 {
4781
+ private takeCloseInto(out: u8[], at: i32): i32 {
4412
4782
  if (this.closeSent) {
4413
4783
  return 0
4414
4784
  }
@@ -4486,7 +4856,7 @@ export class QuicConnection {
4486
4856
  * reset tokens and `TlsServer`'s expected client Finished and resumption
4487
4857
  * secret.
4488
4858
  */
4489
- wipeAll(): void {
4859
+ private wipeAll(): void {
4490
4860
  this.discard(this.initial)
4491
4861
  this.discard(this.handshake)
4492
4862
  this.discard(this.application)
@@ -4523,7 +4893,7 @@ export class QuicConnection {
4523
4893
  * and so `release()`, `reset`, the idle timeout and the end of the
4524
4894
  * closing or draining period.
4525
4895
  */
4526
- wipe(): void {
4896
+ private wipe(): void {
4527
4897
  wipe(this.ephemeral)
4528
4898
  wipe(this.readSecret256)
4529
4899
  wipe(this.nextReadSecret256)