@amritk/nish-aarch64-linux 0.36.0 → 0.38.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 (103) hide show
  1. package/INSTALL.md +92 -71
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +75 -14
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/core/arrays.d.ts +4 -0
  8. package/runtime/core/arrays.mjs +33 -0
  9. package/runtime/nish.d.ts +1045 -512
  10. package/runtime/nish.h +53 -6
  11. package/runtime/page.d.ts +53 -0
  12. package/runtime/page.mjs +146 -0
  13. package/runtime/runtime-async.c +740 -0
  14. package/runtime/runtime-cross.c +2 -1
  15. package/runtime/runtime-region.c +85 -7
  16. package/runtime/runtime-text.c +1068 -0
  17. package/runtime/runtime-wasm.c +65 -9
  18. package/runtime/runtime.c +50 -961
  19. package/runtime/vite.mjs +48 -11
  20. package/scripts/build.sh +231 -31
  21. package/std/LICENSE-fdlibm +8 -0
  22. package/std/README.md +109 -82
  23. package/std/array.ts +266 -0
  24. package/std/bytes.ts +23 -19
  25. package/std/compress/checksum.ts +1 -1
  26. package/std/crypto/aes.ts +48 -43
  27. package/std/crypto/base64url.ts +7 -5
  28. package/std/crypto/bcrypt-batch.ts +13 -13
  29. package/std/crypto/bcrypt.ts +33 -27
  30. package/std/crypto/chacha20poly1305.ts +16 -14
  31. package/std/crypto/ed25519.ts +19 -16
  32. package/std/crypto/hmac.ts +7 -6
  33. package/std/crypto/jwt.ts +23 -20
  34. package/std/crypto/p256.ts +63 -56
  35. package/std/crypto/p384.ts +23 -20
  36. package/std/crypto/pki.ts +18 -15
  37. package/std/crypto/rsa.ts +23 -19
  38. package/std/crypto/x25519.ts +11 -10
  39. package/std/crypto/x509.ts +69 -62
  40. package/std/host/ring.ts +124 -0
  41. package/std/host/text.ts +86 -0
  42. package/std/json-equal.ts +12 -11
  43. package/std/json-number.ts +12 -12
  44. package/std/json-tokenizer.ts +26 -22
  45. package/std/json-typed.ts +92 -78
  46. package/std/json-value.ts +38 -33
  47. package/std/json.ts +32 -28
  48. package/std/math.ts +743 -41
  49. package/std/net/dns-resolver.ts +46 -6
  50. package/std/net/dns.ts +50 -13
  51. package/std/net/fetch.ts +1744 -295
  52. package/std/net/handler.ts +30 -0
  53. package/std/net/hpack.ts +29 -26
  54. package/std/net/http-compress.ts +42 -28
  55. package/std/net/http-fields.ts +19 -4
  56. package/std/net/http1-client-codec.ts +29 -25
  57. package/std/net/http1-codec.ts +62 -91
  58. package/std/net/http1-server.ts +79 -57
  59. package/std/net/http2-frame.ts +17 -16
  60. package/std/net/http2-tls.ts +17 -15
  61. package/std/net/http2.ts +67 -54
  62. package/std/net/http3-frame.ts +11 -10
  63. package/std/net/http3-server.ts +36 -35
  64. package/std/net/http3.ts +70 -58
  65. package/std/net/postgres-async.ts +1005 -0
  66. package/std/net/postgres.ts +185 -164
  67. package/std/net/prefork.ts +38 -34
  68. package/std/net/qpack.ts +15 -14
  69. package/std/net/quic-conn-ack.ts +1 -0
  70. package/std/net/quic-conn-cid.ts +11 -0
  71. package/std/net/quic-conn-params.ts +41 -14
  72. package/std/net/quic-datagram.ts +7 -0
  73. package/std/net/quic-frame.ts +48 -12
  74. package/std/net/quic-listener.ts +61 -44
  75. package/std/net/quic-packet.ts +71 -52
  76. package/std/net/quic-recovery.ts +59 -21
  77. package/std/net/quic-stream.ts +83 -55
  78. package/std/net/quic.ts +204 -152
  79. package/std/net/serve.ts +763 -0
  80. package/std/net/tls/client.ts +1 -0
  81. package/std/net/tls/codec.ts +81 -12
  82. package/std/net/tls/record-client.ts +30 -18
  83. package/std/net/tls/record-server.ts +74 -46
  84. package/std/net/tls/record.ts +33 -24
  85. package/std/net/tls/ticket.ts +2 -0
  86. package/std/net/tls/verify.ts +8 -7
  87. package/std/net/tls-tcp.ts +58 -50
  88. package/std/net/tls.ts +55 -43
  89. package/std/net/url.ts +12 -9
  90. package/std/net/websocket.ts +23 -19
  91. package/std/net/webtransport.ts +24 -18
  92. package/std/random.ts +8 -6
  93. package/std/regex.ts +70 -59
  94. package/std/sync/ring.ts +24 -21
  95. package/std/sync/shm.ts +9 -7
  96. package/std/term-io.ts +17 -14
  97. package/std/term-probe.ts +11 -10
  98. package/std/term-rate.ts +71 -61
  99. package/std/term.ts +73 -59
  100. package/std/text.ts +242 -15
  101. package/std/threads.ts +19 -15
  102. package/std/yaml-json.ts +17 -15
  103. package/std/yaml-write.ts +21 -14
@@ -60,12 +60,17 @@ const QUIC_CID_ZERO: i32 = 0
60
60
  * says whether the slot holds an ID at all.
61
61
  */
62
62
  export class QuicCidEntry {
63
+ /** The ID's sequence number, as issued in NEW_CONNECTION_ID; 0 for the ID of the handshake. */
63
64
  sequence: i64 = 0
65
+ /** @internal Read by `net/quic.ts` and `net/http3-server.ts`. */
64
66
  cid: u8[]
65
67
  /** The stateless reset token issued with it (RFC 9000 §10.3); `hasToken` is false for a peer's sequence 0. */
66
68
  resetToken: u8[]
69
+ /** How many bytes of `cid` the ID takes, 0 to 20. */
67
70
  length: i32 = 0
71
+ /** @internal Read by `net/quic.ts` and `net/http3-server.ts`. */
68
72
  used: boolean = false
73
+ /** @internal Read only by this module's table. */
69
74
  hasToken: boolean = false
70
75
  /** For a local ID: whether a NEW_CONNECTION_ID frame announced it yet. Sequence 0 needs none. */
71
76
  announced: boolean = false
@@ -148,7 +153,12 @@ const quicCidSlots = (count: i32): QuicCidEntry[] => {
148
153
  * `active_connection_id_limit` this side advertised, which bounds the peer's.
149
154
  */
150
155
  export class QuicCidTable {
156
+ /** The slots for the IDs this side issued, so a caller can walk them, used or not. */
151
157
  local: QuicCidEntry[]
158
+ /**
159
+ * The slots for the IDs the peer issued, used or not, as `limit` bounds them; a caller walks
160
+ * them to see each ID and its reset token.
161
+ */
152
162
  peer: QuicCidEntry[]
153
163
  /** Peer sequence numbers this side must send RETIRE_CONNECTION_ID for, oldest first, as a ring of `retireCount` from `retireHead`. */
154
164
  private retirePending: i64[]
@@ -160,6 +170,7 @@ export class QuicCidTable {
160
170
  /** The index in `peer` of the ID this side sends to, or -1 before the peer gave one. */
161
171
  private current: i32 = -1
162
172
  private retireHead: i32 = 0
173
+ /** How many RETIRE_CONNECTION_ID frames this side still owes the peer, at most twice `limit`. */
163
174
  retireCount: i32 = 0
164
175
 
165
176
  /**
@@ -20,20 +20,23 @@
20
20
  * says it then has. The encoder writes a parameter only when it differs from
21
21
  * that default, plus the connection IDs, which have none.
22
22
  *
23
- * **What a parse refuses.** Each of these is a TRANSPORT_PARAMETER_ERROR
24
- * (§7.4, §18.2): an ID or a length that does not fit, a parameter running past the bytes, a
25
- * known parameter sent twice, a varint-valued parameter whose value is not
26
- * exactly one varint, `disable_active_migration` with a value, a server-only
27
- * parameter from a client (`original_destination_connection_id`,
28
- * `preferred_address`, `retry_source_connection_id`,
29
- * `stateless_reset_token`), a stateless reset token that is not 16 bytes, a
30
- * connection ID over 20 bytes, `max_udp_payload_size` below 1200,
31
- * `ack_delay_exponent` above 20, `max_ack_delay` at 2^14 or above,
32
- * `active_connection_id_limit` below 2, and a stream count above 2^60.
33
- * Unknown parameters, the reserved ones (31 × N + 27) among them, are
34
- * skipped, as §18.1 asks; a second copy of an unknown one is not noticed.
35
- * Whether `initial_source_connection_id` is present and right is the
36
- * connection's check, because it needs the packet the ID came in.
23
+ * **What a parse refuses.**
24
+ *
25
+ * Each of these is a TRANSPORT_PARAMETER_ERROR (§7.4, §18.2): an ID or a length
26
+ * that does not fit, a parameter running past the bytes, a known parameter sent
27
+ * twice, a varint-valued parameter whose value is not exactly one varint,
28
+ * `disable_active_migration` with a value, a server-only parameter from a
29
+ * client (`original_destination_connection_id`, `preferred_address`,
30
+ * `retry_source_connection_id`, `stateless_reset_token`), a stateless reset
31
+ * token that is not 16 bytes, a connection ID over 20 bytes,
32
+ * `max_udp_payload_size` below 1200, `ack_delay_exponent` above 20,
33
+ * `max_ack_delay` at 2^14 or above, `active_connection_id_limit` below 2, and a
34
+ * stream count above 2^60.
35
+ *
36
+ * Unknown parameters, the reserved ones (31 × N + 27) among them, are skipped,
37
+ * as §18.1 asks; a second copy of an unknown one is not noticed. Whether
38
+ * `initial_source_connection_id` is present and right is the connection's
39
+ * check, because it needs the packet the ID came in.
37
40
  *
38
41
  * **Provenance.** Written from RFC 9000 §18, in this module's own structure; nothing here is
39
42
  * ported from another implementation. Private names carry the
@@ -131,31 +134,55 @@ const QUIC_PARAMS_MAX_STREAMS: i64 = 1073741824 * 1073741824
131
134
  * frame).
132
135
  */
133
136
  export class QuicTransportParameters {
137
+ /** What the last parse answered: `QUIC_ERROR_NO_ERROR`, or `QUIC_ERROR_TRANSPORT_PARAMETER` with `errorParameter` set. */
134
138
  error: i64 = 0
139
+ /** `max_idle_timeout`, in milliseconds; 0, the default, for no idle timeout. */
135
140
  maxIdleTimeout: i64 = 0
141
+ /** `max_udp_payload_size`: the largest UDP payload the sender takes, at least 1200 bytes; 65527 by default. */
136
142
  maxUdpPayloadSize: i64 = 65527
143
+ /** `initial_max_data`: the connection's first receive credit, in bytes, across every stream. */
137
144
  initialMaxData: i64 = 0
145
+ /** `initial_max_stream_data_bidi_local`: the first credit, in bytes, on a bidirectional stream the sender opens. */
138
146
  initialMaxStreamDataBidiLocal: i64 = 0
147
+ /** `initial_max_stream_data_bidi_remote`: the first credit, in bytes, on a bidirectional stream its peer opens. */
139
148
  initialMaxStreamDataBidiRemote: i64 = 0
149
+ /** `initial_max_stream_data_uni`: the first credit, in bytes, on a unidirectional stream its peer opens. */
140
150
  initialMaxStreamDataUni: i64 = 0
151
+ /** `initial_max_streams_bidi`: how many bidirectional streams the peer may open at first, at most 2^60. */
141
152
  initialMaxStreamsBidi: i64 = 0
153
+ /** `initial_max_streams_uni`: how many unidirectional streams the peer may open at first, at most 2^60. */
142
154
  initialMaxStreamsUni: i64 = 0
155
+ /** `ack_delay_exponent`: the power of two an ACK frame's delay is scaled by, at most 20; 3 by default. */
143
156
  ackDelayExponent: i64 = 3
157
+ /** `max_ack_delay`, in milliseconds: how long the sender may hold an acknowledgement, below 2^14; 25 by default. */
144
158
  maxAckDelay: i64 = 25
159
+ /** `active_connection_id_limit`: how many of its peer's connection IDs the sender keeps, at least 2. */
145
160
  activeConnectionIdLimit: i64 = 2
146
161
  /** RFC 9221's `max_datagram_frame_size`: 0, the default, for no DATAGRAM frames. */
147
162
  maxDatagramFrameSize: i64 = 0
163
+ /** The ID of the parameter a failed parse is about, or -1 when the bytes did not frame; read beside `error`. */
148
164
  errorParameter: i64 = 0
165
+ /** `original_destination_connection_id`: the client's first Destination Connection ID; server only. */
149
166
  originalDcid: u8[]
167
+ /** `stateless_reset_token`: 16 bytes that end a stateless reset for the server's connection ID; server only. */
150
168
  statelessResetToken: u8[]
169
+ /** `preferred_address`, kept as the bytes the server sent, since this side never acts on it; server only. */
151
170
  preferredAddress: u8[]
171
+ /** `initial_source_connection_id`: the Source Connection ID of the sender's first Initial packet. */
152
172
  initialScid: u8[]
173
+ /** `retry_source_connection_id`: the Source Connection ID of the server's Retry; server only. */
153
174
  retryScid: u8[]
175
+ /** Whether `originalDcid` was set or read; an empty ID is a value, so its length cannot say. */
154
176
  hasOriginalDcid: boolean = false
177
+ /** Whether `statelessResetToken` was set or read. */
155
178
  hasStatelessResetToken: boolean = false
179
+ /** Whether `preferredAddress` was set or read. */
156
180
  hasPreferredAddress: boolean = false
181
+ /** Whether `initialScid` was set or read; an empty ID is a value, so its length cannot say. */
157
182
  hasInitialScid: boolean = false
183
+ /** Whether `retryScid` was set or read; an empty ID is a value, so its length cannot say. */
158
184
  hasRetryScid: boolean = false
185
+ /** `disable_active_migration`: the sender does not support active connection migration (RFC 9000 §9) from the address the handshake used. */
159
186
  disableActiveMigration: boolean = false
160
187
 
161
188
  /**
@@ -33,9 +33,16 @@ import { quicPacketCopy } from "nish/net/quic-packet"
33
33
  export class QuicDatagramQueue {
34
34
  /** Entry `k`'s bytes are `data[k * entrySize ..]`, `lengths[k]` of them. */
35
35
  data: u8[]
36
+ /** Each entry's payload length in bytes, 0 to `entrySize`; entry `k`'s is `lengths[k]`. */
36
37
  lengths: i32[]
38
+ /**
39
+ * The largest payload an entry holds, in bytes, fixed when the ring is made; it is also the
40
+ * stride of `data`. A longer datagram is refused and counted in `dropped`.
41
+ */
37
42
  entrySize: i32 = 0
43
+ /** The index of the oldest entry; the `j`-th oldest is entry `(head + j) % capacity()`. */
38
44
  head: i32 = 0
45
+ /** How many datagrams wait in the ring, from 0 to its entry count. */
39
46
  count: i32 = 0
40
47
  /** Datagrams refused for want of room or for their size, which the peer's side counts as dropped. */
41
48
  dropped: i32 = 0
@@ -14,18 +14,21 @@
14
14
  * at = frame.end;
15
15
  * }
16
16
  *
17
- * **Coverage.** Every frame type of RFC 9000 is read, 0x00 to 0x1e, and RFC 9221's
18
- * DATAGRAM, 0x30 and 0x31, so a type outside those is the one parse error a
19
- * well-formed packet cannot produce. What
20
- * a frame *means* is `nish/net/quic`'s: this module checks only what §19 says
21
- * of the encoding itself, and answers the transport error the RFC names for
22
- * each break — `QUIC_ERROR_FRAME_ENCODING` for a truncated field, a length
23
- * past the packet, an ACK range below packet number zero, an offset past
24
- * 2^62 − 1, a stream count past 2^60, an empty NEW_TOKEN, a connection ID
25
- * outside 1 to 20 bytes or a Retire Prior To above its own sequence number;
26
- * `QUIC_ERROR_PROTOCOL_VIOLATION` for a frame type spelled in more bytes than
27
- * it needs (§12.4). Every read is bounds-checked against the window the caller
28
- * gives, so nothing a peer sends can make this module panic.
17
+ * **Coverage.** Every frame type of RFC 9000 is read, 0x00 to 0x1e, and RFC
18
+ * 9221's DATAGRAM, 0x30 and 0x31, so a type outside those is the one parse
19
+ * error a well-formed packet cannot produce.
20
+ *
21
+ * What a frame *means* is `nish/net/quic`'s: this module checks only what §19
22
+ * says of the encoding itself, and answers the transport error the RFC names
23
+ * for each break — `QUIC_ERROR_FRAME_ENCODING` for a truncated field, a length
24
+ * past the packet, an ACK range below packet number zero, an offset past 2^62 −
25
+ * 1, a stream count past 2^60, an empty NEW_TOKEN, a connection ID outside 1 to
26
+ * 20 bytes or a Retire Prior To above its own sequence number;
27
+ * `QUIC_ERROR_PROTOCOL_VIOLATION` for a frame type spelled in more bytes than
28
+ * it needs (§12.4).
29
+ *
30
+ * Every read is bounds-checked against the window the caller gives, so nothing
31
+ * a peer sends can make this module panic.
29
32
  *
30
33
  * **No copy of the bulk.** A CRYPTO, STREAM or DATAGRAM frame's data, a
31
34
  * NEW_TOKEN's token, a PATH_CHALLENGE's eight bytes and a CONNECTION_CLOSE's
@@ -208,29 +211,62 @@ export const QUIC_PATH_DATA_SIZE: i32 = 8
208
211
  * phrase as the data.
209
212
  */
210
213
  export class QuicFrame {
214
+ /** The stream a STREAM, RESET_STREAM, STOP_SENDING, MAX_STREAM_DATA or STREAM_DATA_BLOCKED frame names. */
211
215
  streamId: i64 = 0
216
+ /** The byte offset in the stream (STREAM) or the handshake stream (CRYPTO) where the data starts. */
212
217
  offset: i64 = 0
218
+ /** The frame's one number: a limit, a final size or a sequence number, as the list above gives per type. */
213
219
  value: i64 = 0
220
+ /** The application or transport error code of a RESET_STREAM, STOP_SENDING or CONNECTION_CLOSE. */
214
221
  errorCode: i64 = 0
222
+ /** The frame type a transport CONNECTION_CLOSE (0x1c) blames; 0 for the application form. */
215
223
  frameType: i64 = 0
224
+ /** A NEW_CONNECTION_ID's Retire Prior To: every peer ID with a lower sequence number is to be retired. */
216
225
  retirePriorTo: i64 = 0
226
+ /** The largest packet number an ACK acknowledges. */
217
227
  largest: i64 = 0
228
+ /** An ACK's delay as sent, not yet scaled by the peer's `ack_delay_exponent`. */
218
229
  ackDelay: i64 = 0
230
+ /**
231
+ * An ACK's ranges as `[smallest, largest]` packet-number pairs, highest
232
+ * first. Only the first `ackRangeCount` pairs are meaningful; the array
233
+ * keeps its `QUIC_FRAME_MAX_ACK_RANGES * 2` length.
234
+ */
219
235
  ackRanges: i64[]
236
+ /**
237
+ * How many ranges the ACK carried, which can exceed `ackRangeCount` when ranges past the stored cap
238
+ * were read and dropped.
239
+ */
220
240
  ackRangeTotal: i64 = 0
241
+ /** An ACK_ECN's cumulative count of packets received marked ECT(0). */
221
242
  ect0: i64 = 0
243
+ /** An ACK_ECN's cumulative count of packets received marked ECT(1). */
222
244
  ect1: i64 = 0
245
+ /** An ACK_ECN's cumulative count of packets received marked ECN-CE (congestion experienced). */
223
246
  ce: i64 = 0
224
247
  /** The frame type, with every STREAM type answered as `QUIC_FRAME_STREAM`. */
225
248
  type: i32 = 0
226
249
  /** One past the frame's last byte, where the next frame starts. */
227
250
  end: i32 = 0
251
+ /**
252
+ * Where the frame's data (stream or CRYPTO bytes, token, path data, datagram or reason phrase)
253
+ * starts in the payload.
254
+ */
228
255
  dataStart: i32 = 0
256
+ /** How many bytes of data start at `dataStart`. */
229
257
  dataLength: i32 = 0
258
+ /**
259
+ * How many `[smallest, largest]` pairs of `ackRanges` hold an ACK range, at most
260
+ * `QUIC_FRAME_MAX_ACK_RANGES`.
261
+ */
230
262
  ackRangeCount: i32 = 0
263
+ /** Where a NEW_CONNECTION_ID's connection ID starts in the payload. */
231
264
  connectionIdStart: i32 = 0
265
+ /** A NEW_CONNECTION_ID's connection ID length in bytes, 1 to 20. */
232
266
  connectionIdLength: i32 = 0
267
+ /** Where a NEW_CONNECTION_ID's 16-byte stateless reset token starts in the payload. */
233
268
  resetTokenStart: i32 = 0
269
+ /** A STREAM frame's FIN bit (the stream ends with this data), or a DATAGRAM that carried a Length. */
234
270
  fin: boolean = false
235
271
 
236
272
  /**
@@ -35,30 +35,34 @@
35
35
  * listener a packet for a connection that is still alive would give that
36
36
  * token, which ends the connection, to whoever sent it (RFC 9000 §21.11).
37
37
  *
38
- * **Retry tokens** (§8.1.4). They are this server's own format: a marker byte, the
39
- * time the token was issued, and the client's original Destination
38
+ * **Retry tokens** (§8.1.4). They are this server's own format: a marker byte,
39
+ * the time the token was issued, and the client's original Destination
40
40
  * Connection ID, then 16 bytes of HMAC-SHA256 under the configuration's
41
- * `retryTokenKey` over all of that, the Retry's Source Connection ID (which
42
- * the client's next Initial must be sent to) and the client's address as the
43
- * caller gives it, port included. So a token is good only from the address
44
- * it was sent to, only for the connection it was issued for, and only for
45
- * `retryTokenLifetime` milliseconds. A token that carries the marker and fails
46
- * any of that is an invalid Retry token, which closes the attempt with
47
- * INVALID_TOKEN (§8.1.2); a token without the marker, which some other
48
- * server issued, is treated as no token at all (§8.1.3).
41
+ * `retryTokenKey` over all of that, the Retry's Source Connection ID (which the
42
+ * client's next Initial must be sent to) and the client's address as the caller
43
+ * gives it, port included. So a token is good only from the address it was sent
44
+ * to, only for the connection it was issued for, and only for
45
+ * `retryTokenLifetime` milliseconds.
46
+ *
47
+ * A token that carries the marker and fails any of that is an invalid Retry
48
+ * token, which closes the attempt with INVALID_TOKEN (§8.1.2); a token without
49
+ * the marker, which some other server issued, is treated as no token at all
50
+ * (§8.1.3).
49
51
  *
50
52
  * **Stateless resets** (§10.3). They end in the token `quicStatelessResetToken`
51
- * derives (computed in place, by `quicConnResetTokenInto`) from the configuration's static key and the packet's connection
52
- * ID, so a server that restarted with the same key can end the connections
53
- * it lost. RFC 9000 §10.3 and §10.3.3 bound them, and each bound is held
54
- * here. A reset is sent only for a short header, and is always shorter than
55
- * the datagram that caused it, so two endpoints resetting each other shrink
56
- * to nothing; one byte shorter for a datagram of 43 bytes or fewer, and
57
- * never under 21 bytes, so a datagram of 21 or fewer gets none. That also
58
- * bounds amplification at less than one to one. They are rate limited:
59
- * `QUIC_LISTENER_RESET_BURST` at once, then one per
60
- * `QUIC_LISTENER_RESET_INTERVAL` milliseconds. Each reset's other bytes are
61
- * unpredictable, from the listener's own generator.
53
+ * derives (computed in place, by `quicConnResetTokenInto`) from the
54
+ * configuration's static key and the packet's connection ID, so a server that
55
+ * restarted with the same key can end the connections it lost. RFC 9000 §10.3
56
+ * and §10.3.3 bound them, and each bound is held here.
57
+ *
58
+ * A reset is sent only for a short header, and is always shorter than the
59
+ * datagram that caused it, so two endpoints resetting each other shrink to
60
+ * nothing; one byte shorter for a datagram of 43 bytes or fewer, and never
61
+ * under 21 bytes, so a datagram of 21 or fewer gets none. That also bounds
62
+ * amplification at less than one to one. They are rate limited:
63
+ * `QUIC_LISTENER_RESET_BURST` at once, then one per
64
+ * `QUIC_LISTENER_RESET_INTERVAL` milliseconds. Each reset's other bytes are
65
+ * unpredictable, from the listener's own generator.
62
66
  *
63
67
  * **Pacing** (RFC 9002 §7.7). It is the carrier's send loop, so it is here
64
68
  * beside the rest of what the carrier calls: `quicListenerTakePaced` takes a
@@ -78,18 +82,19 @@
78
82
  *
79
83
  * **GSO and GRO** (`nish:net`'s `UDP_SEGMENT` and `UDP_GRO`). A carrier that
80
84
  * sends with segmentation offload takes a whole flight at once:
81
- * `quicListenerTakeFlight` writes as many datagrams as the pacer has credit
82
- * for back to back into one buffer, every one `QUIC_CONN_DATAGRAM_SIZE`
83
- * bytes but the last, which is what one `udpSendTo` with that segment size
84
- * cuts back into datagrams; each is written in place, so a flight allocates
85
- * nothing. The flight is paced as a whole: it is as long as the pacer's
86
- * credit, and charged to it datagram by datagram, so a burst is never more
87
- * than the initial window however the carrier sends it. It starts only on a
88
- * millisecond's earning, up to four datagrams, so a fast connection's pacer,
89
- * which earns by the microsecond, does not let it out a datagram per send;
90
- * `quicListenerFlightTime` says when. On the receiving
91
- * side, `quicListenerReceiveSegments` hands a connection the datagrams of
92
- * one GRO receive, segment by segment, in place.
85
+ * `quicListenerTakeFlight` writes as many datagrams as the pacer has credit for
86
+ * back to back into one buffer, every one `QUIC_CONN_DATAGRAM_SIZE` bytes but
87
+ * the last, which is what one `udpSendTo` with that segment size cuts back into
88
+ * datagrams; each is written in place, so a flight allocates nothing.
89
+ *
90
+ * The flight is paced as a whole: it is as long as the pacer's credit, and
91
+ * charged to it datagram by datagram, so a burst is never more than the initial
92
+ * window however the carrier sends it. It starts only on a millisecond's
93
+ * earning, up to four datagrams, so a fast connection's pacer, which earns by
94
+ * the microsecond, does not let it out a datagram per send;
95
+ * `quicListenerFlightTime` says when. On the receiving side,
96
+ * `quicListenerReceiveSegments` hands a connection the datagrams of one GRO
97
+ * receive, segment by segment, in place.
93
98
  *
94
99
  * **ECN** (RFC 9000 §13.4). A carrier whose platform sends the ECN bits it
95
100
  * is given sets the connection's `ecnMarking` and sends each datagram with
@@ -223,10 +228,15 @@ export const QUIC_LISTENER_ANSWER_SIZE: i32 = QUIC_CONN_DATAGRAM_SIZE
223
228
  * with exactly the room its reply takes.
224
229
  */
225
230
  export class QuicListenerAnswer {
231
+ /** The datagram to send back to the peer's address; empty when the answer sends nothing. */
226
232
  reply: u8[]
233
+ /** After a Retry, the DCID of the client's first Initial, which `QuicConnection.acceptRetry` checks. */
227
234
  originalDcid: u8[]
235
+ /** After a Retry, the SCID the Retry chose, which `QuicConnection.acceptRetry` checks. */
228
236
  retryScid: u8[]
237
+ /** What to do with the datagram: a `QUIC_LISTEN_*`, `QUIC_LISTEN_DROP` after `wipe`. */
229
238
  kind: i32 = 0
239
+ /** Whether an accepted Initial carried a valid Retry token, so `originalDcid` and `retryScid` are set. */
230
240
  retried: boolean = false
231
241
  private room: i32 = 0
232
242
 
@@ -342,6 +352,7 @@ const quicListenerWindowFits = (length: i32, at: i32, len: i32): boolean =>
342
352
  */
343
353
  export class QuicListener {
344
354
  private config: QuicServerConfig
355
+ /** The 32 bytes of entropy every unpredictable byte is made from; `release` wipes it. */
345
356
  seed: u8[]
346
357
  /** Unpredictable bytes not yet used, from `poolHead`, and the counter the next block is made from. */
347
358
  pool: u8[]
@@ -357,8 +368,11 @@ export class QuicListener {
357
368
  private lengths: u8[]
358
369
  /** The Initial secrets of an INVALID_TOKEN close, and its keys, both directions. */
359
370
  initialClient: u8[]
371
+ /** The server's Initial secret of an INVALID_TOKEN close; `initialClient` is the client's. */
360
372
  initialServer: u8[]
373
+ /** The INVALID_TOKEN close's keys for reading the client's Initial. */
361
374
  readSlot: QuicKeysSlot
375
+ /** The INVALID_TOKEN close's keys for sealing the server's Initial. */
362
376
  writeSlot: QuicKeysSlot
363
377
  private packet: QuicPacket
364
378
  /** The client's SCID and the DCID it sent to, copied out for `quicPutLongHeader`, which reads IDs from 0. */
@@ -376,9 +390,11 @@ export class QuicListener {
376
390
  /** When the reset budget was last topped up (-1: never yet), and the stateless resets that may go now. */
377
391
  private resetRefilled: i64 = -1
378
392
  private resetBudget: i32 = 0
393
+ /** Where the unused bytes of `pool` start: `QUIC_LISTENER_POOL` when it is spent. */
379
394
  poolHead: i32 = 0
380
395
  /** Stateless resets sent, and ones the rate limit held back. */
381
396
  resetsSent: i32 = 0
397
+ /** Stateless resets the rate limit held back, which a log or test reads to see the limit working. */
382
398
  resetsLimited: i32 = 0
383
399
  /** Whether the configuration and entropy were usable. */
384
400
  private usable: boolean = false
@@ -992,17 +1008,18 @@ export class QuicFlight {
992
1008
 
993
1009
  /**
994
1010
  * Writes the next flight `conn` sends at `now` into `buf[at .. at + cap)`,
995
- * datagram after datagram, for one GSO send, and answers its length in
996
- * bytes (0 for none); `flight` says how many datagrams it holds and the
997
- * segment size. A flight starts only once the pacer has the credit
998
- * `quicListenerFlightTime` waits for, a millisecond's earning of one to four
999
- * datagrams; then each datagram goes only when the pacer has credit for a
1000
- * full one (RFC 9002 §7.7) and is charged at its real size, as
1001
- * `quicListenerTakePaced` does, so the flight is the pacer's whole credit
1002
- * and no more. It stops at the first datagram shorter than
1003
- * `QUIC_CONN_DATAGRAM_SIZE`, which can only be the last of a segmented send,
1004
- * at `QUIC_LISTENER_FLIGHT_MAX` datagrams, when `cap` has no room for
1005
- * another, and before a datagram whose ECN codepoint would not be the first's.
1011
+ * datagram after datagram, for one GSO send, and answers its length in bytes (0
1012
+ * for none); `flight` says how many datagrams it holds and the segment size.
1013
+ *
1014
+ * A flight starts only once the pacer has the credit `quicListenerFlightTime`
1015
+ * waits for, a millisecond's earning of one to four datagrams; then each
1016
+ * datagram goes only when the pacer has credit for a full one (RFC 9002 §7.7)
1017
+ * and is charged at its real size, as `quicListenerTakePaced` does, so the
1018
+ * flight is the pacer's whole credit and no more. It stops at the first
1019
+ * datagram shorter than `QUIC_CONN_DATAGRAM_SIZE`, which can only be the last
1020
+ * of a segmented send, at `QUIC_LISTENER_FLIGHT_MAX` datagrams, when `cap` has
1021
+ * no room for another, and before a datagram whose ECN codepoint would not be
1022
+ * the first's.
1006
1023
  *
1007
1024
  * @param conn The connection to send for.
1008
1025
  * @param now The caller's time in milliseconds.
@@ -41,18 +41,19 @@
41
41
  * which is what a server needs to send Version Negotiation, and reported as
42
42
  * `QUIC_ERR_VERSION`.
43
43
  *
44
- * **What a parse refuses.** Every read is bounds-checked against the
45
- * datagram, so nothing a peer sends can make this module panic. A header
46
- * that runs past the datagram is `QUIC_ERR_TRUNCATED`; a clear fixed bit
47
- * `QUIC_ERR_FIXED_BIT`; a connection ID over 20 bytes in a version 1 header
48
- * `QUIC_ERR_CID_LENGTH`; a Length field reaching past the datagram
49
- * `QUIC_ERR_LENGTH`; a packet too short to sample for header protection
50
- * `QUIC_ERR_SAMPLE`; a payload that does not authenticate `QUIC_ERR_DECRYPT`;
51
- * and nonzero reserved bits once both protections are off
52
- * `QUIC_ERR_RESERVED_BITS`, which RFC 9000 §17.2 makes a connection error
53
- * rather than a drop, so the payload is still handed back with it. The
54
- * builders, whose inputs come from this side rather than from a peer, answer
55
- * `null` for an argument out of range.
44
+ * **What a parse refuses.** Every read is bounds-checked against the datagram,
45
+ * so nothing a peer sends can make this module panic.
46
+ *
47
+ * A header that runs past the datagram is `QUIC_ERR_TRUNCATED`; a clear fixed
48
+ * bit `QUIC_ERR_FIXED_BIT`; a connection ID over 20 bytes in a version 1 header
49
+ * `QUIC_ERR_CID_LENGTH`; a Length field reaching past the datagram
50
+ * `QUIC_ERR_LENGTH`; a packet too short to sample for header protection
51
+ * `QUIC_ERR_SAMPLE`; a payload that does not authenticate `QUIC_ERR_DECRYPT`;
52
+ * and nonzero reserved bits once both protections are off
53
+ * `QUIC_ERR_RESERVED_BITS`, which RFC 9000 §17.2 makes a connection error
54
+ * rather than a drop, so the payload is still handed back with it. The
55
+ * builders, whose inputs come from this side rather than from a peer, answer
56
+ * `null` for an argument out of range.
56
57
  *
57
58
  * **In place.** For a connection that allocates nothing per packet,
58
59
  * `quicParseHeaderInto` reads a header as windows into the datagram, the
@@ -68,24 +69,24 @@
68
69
  * datagram starts. A short header has none, so it runs to the datagram's end.
69
70
  *
70
71
  * **Secrets.** `QuicKeys` holds a packet key, an IV and a header-protection
71
- * key, pointing into a `QuicKeysSlot`'s arrays. `quicKeys` and
72
- * `quicKeysUpdate` take a traffic secret as a `Secret` from `nish:secret`
73
- * and a slot the caller owns, and derive into the slot; a slot the caller
74
- * owns is the caller's to wipe, and `QuicKeysSlot.wipe` zeroes it with
75
- * stores no optimiser removes, AES schedules included (QUIC-1).
76
- * `quicKeyUpdateSecret` answers the next secret as a `Secret`, and every
77
- * derivation wipes its HKDF intermediates before it returns. `quicKeysWipe`
78
- * zeroes the arrays one `QuicKeys` points at. The Initial keys are not
79
- * secret, because anyone who reads the client's first DCID can derive them
80
- * (RFC 9001 §5.2), so `quicInitialSecrets` answers plain arrays, and
81
- * `quicKeysInto`, `quicKeysUpdateInto`, `quicKeyUpdateSecretInto` and
82
- * `quicInitialSecretsInto` derive from plain secrets over a caller's
83
- * `HkdfScratch`; the Handshake and 1-RTT keys are secret. Each derivation's
84
- * own temporaries go with its arena block. A slot whose traffic secret is
85
- * held as a `Secret` may also derive with `quicKeysHeldInto`, and ratchet
86
- * the secret in place with `quicKeyUpdateSecretHeldInto`. Sealing and
87
- * opening a packet wipe the nonce, which gives the IV away, and the
88
- * header-protection mask once each is used (QUIC-2).
72
+ * key, pointing into a `QuicKeysSlot`'s arrays. `quicKeys` and `quicKeysUpdate`
73
+ * take a traffic secret as a `Secret` from `nish:secret` and a slot the caller
74
+ * owns, and derive into the slot; a slot the caller owns is the caller's to
75
+ * wipe, and `QuicKeysSlot.wipe` zeroes it with stores no optimiser removes, AES
76
+ * schedules included (QUIC-1). `quicKeyUpdateSecret` answers the next secret as
77
+ * a `Secret`, and every derivation wipes its HKDF intermediates before it
78
+ * returns. `quicKeysWipe` zeroes the arrays one `QuicKeys` points at.
79
+ *
80
+ * The Initial keys are not secret, because anyone who reads the client's first
81
+ * DCID can derive them (RFC 9001 §5.2), so `quicInitialSecrets` answers plain
82
+ * arrays, and `quicKeysInto`, `quicKeysUpdateInto`, `quicKeyUpdateSecretInto`
83
+ * and `quicInitialSecretsInto` derive from plain secrets over a caller's
84
+ * `HkdfScratch`; the Handshake and 1-RTT keys are secret. Each derivation's own
85
+ * temporaries go with its arena block. A slot whose traffic secret is held as a
86
+ * `Secret` may also derive with `quicKeysHeldInto`, and ratchet the secret in
87
+ * place with `quicKeyUpdateSecretHeldInto`. Sealing and opening a packet wipe
88
+ * the nonce, which gives the IV away, and the header-protection mask once each
89
+ * is used (QUIC-2).
89
90
  *
90
91
  * **Constant time.** The AEADs and the header-protection masks are
91
92
  * `nish/crypto/aes` and `nish/crypto/chacha20poly1305`, with their guarantees.
@@ -250,10 +251,18 @@ export class QuicHeader {
250
251
  * windows out into the arrays above.
251
252
  */
252
253
  dcidStart: i32 = 0
254
+ /**
255
+ * The Destination Connection ID's length in bytes, 0 to 20; a short header's is the length the
256
+ * reader passed.
257
+ */
253
258
  dcidLength: i32 = 0
259
+ /** Where the Source Connection ID starts in the datagram; 0 for a short header. */
254
260
  scidStart: i32 = 0
261
+ /** The Source Connection ID's length in bytes, 0 to 20; 0 for a short header. */
255
262
  scidLength: i32 = 0
263
+ /** Where an Initial's or a Retry's token starts in the datagram; 0 for every other type. */
256
264
  tokenStart: i32 = 0
265
+ /** An Initial's or a Retry's token length in bytes; 0 for every other type. */
257
266
  tokenLength: i32 = 0
258
267
 
259
268
  /**
@@ -1199,21 +1208,22 @@ const quicPacketKeysUsable = (keys: QuicKeys): boolean => {
1199
1208
  }
1200
1209
 
1201
1210
  /**
1202
- * The packet-protection keys of RFC 9001 §5.1 from a traffic secret, held as
1203
- * a `Secret` the caller still owns and wipes, derived into `slot`, which the
1211
+ * The packet-protection keys of RFC 9001 §5.1 from a traffic secret, held as a
1212
+ * `Secret` the caller still owns and wipes, derived into `slot`, which the
1204
1213
  * caller owns too: the AEAD key (`quic key`), the IV (`quic iv`) and the
1205
1214
  * header-protection key (`quic hp`), each HKDF-Expand-Label with an empty
1206
- * context under the hash `aead` pairs with, written into the slot's arrays
1207
- * and the AES schedules expanded there. The secret is read through a plain
1208
- * copy inside an arena block, which is zeroed, and the derivation's
1209
- * temporaries go with the block, so the call keeps in the arena only the
1210
- * `HkdfScratch` it makes for the derivation, which wipes itself before the
1211
- * derivation returns.
1212
- * Answers the slot's keys, which the next derivation into it overwrites and
1213
- * `QuicKeysSlot.wipe` zeroes, or `null`, deriving nothing, for an `aead`
1214
- * this module does not know or a secret that is not that hash's length. The
1215
- * Initial secrets are public (RFC 9001 §5.2), so a caller deriving Initial
1216
- * keys wraps a copy of one in `secret`, or uses `quicKeysInto`.
1215
+ * context under the hash `aead` pairs with, written into the slot's arrays and
1216
+ * the AES schedules expanded there.
1217
+ *
1218
+ * The secret is read through a plain copy inside an arena block, which is
1219
+ * zeroed, and the derivation's temporaries go with the block, so the call keeps
1220
+ * in the arena only the `HkdfScratch` it makes for the derivation, which wipes
1221
+ * itself before the derivation returns. Answers the slot's keys, which the next
1222
+ * derivation into it overwrites and `QuicKeysSlot.wipe` zeroes, or `null`,
1223
+ * deriving nothing, for an `aead` this module does not know or a secret that is
1224
+ * not that hash's length. The Initial secrets are public (RFC 9001 §5.2), so a
1225
+ * caller deriving Initial keys wraps a copy of one in `secret`, or uses
1226
+ * `quicKeysInto`.
1217
1227
  *
1218
1228
  * @param slot The caller's slot to derive into; its earlier keys are overwritten.
1219
1229
  * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
@@ -1255,15 +1265,16 @@ export const quicKeyUpdateSecret = (aead: i32, trafficSecret: Secret<u8[]>): Sec
1255
1265
 
1256
1266
  /**
1257
1267
  * The next generation's keys in a key update (RFC 9001 §6), derived into
1258
- * `slot`, which the caller owns and which must not be the slot `keys` live
1259
- * in: the packet key and IV from `nextSecret`, which `quicKeyUpdateSecret`
1260
- * made from the current secret and the caller still owns, read through a
1261
- * plain copy inside an arena block that is zeroed, and `keys`'
1262
- * header-protection key kept, since header protection never changes with
1263
- * the phase. The new generation holds its own copy of that key and its own
1264
- * schedule, in the slot, so wiping one generation leaves the other usable.
1265
- * Answers the slot's keys, or `null`, deriving nothing, for keys `quicKeys`
1266
- * did not make, keys that live in `slot`, or a secret of the wrong length.
1268
+ * `slot`, which the caller owns and which must not be the slot `keys` live in:
1269
+ * the packet key and IV from `nextSecret`, which `quicKeyUpdateSecret` made
1270
+ * from the current secret and the caller still owns, read through a plain copy
1271
+ * inside an arena block that is zeroed, and `keys`' header-protection key kept,
1272
+ * since header protection never changes with the phase.
1273
+ *
1274
+ * The new generation holds its own copy of that key and its own schedule, in
1275
+ * the slot, so wiping one generation leaves the other usable. Answers the
1276
+ * slot's keys, or `null`, deriving nothing, for keys `quicKeys` did not make,
1277
+ * keys that live in `slot`, or a secret of the wrong length.
1267
1278
  *
1268
1279
  * @param slot The caller's slot for the next generation; a different slot from the one `keys` live in.
1269
1280
  * @param keys The current generation's keys, whose header-protection key is copied; left unchanged.
@@ -1335,13 +1346,21 @@ export const quicKeysWipe = (keys: QuicKeys): void => {
1335
1346
  export class QuicKeysSlot {
1336
1347
  /** The keys, pointing into the arrays below; `aead` is 0 until something is derived. */
1337
1348
  keys: QuicKeys
1349
+ /** The 16-byte packet key an AES-128-GCM suite uses. */
1338
1350
  key16: u8[]
1351
+ /** The 32-byte packet key an AES-256-GCM or ChaCha20-Poly1305 suite uses. */
1339
1352
  key32: u8[]
1353
+ /** The 16-byte header-protection key an AES-128-GCM suite uses. */
1340
1354
  hp16: u8[]
1355
+ /** The 32-byte header-protection key an AES-256-GCM or ChaCha20-Poly1305 suite uses. */
1341
1356
  hp32: u8[]
1357
+ /** The AES-128 key schedule for packet protection, expanded from `key16`. */
1342
1358
  packet128: AesKey
1359
+ /** The AES-256 key schedule for packet protection, expanded from `key32`. */
1343
1360
  packet256: AesKey
1361
+ /** The AES-128 key schedule for header protection, expanded from `hp16`. */
1344
1362
  hp128: AesKey
1363
+ /** The AES-256 key schedule for header protection, expanded from `hp32`. */
1345
1364
  hp256: AesKey
1346
1365
 
1347
1366
  /**