@amritk/nish-aarch64-linux 0.35.0 → 0.36.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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -5,9 +5,13 @@
5
5
  *
6
6
  * import { QuicAckRanges } from "nish/net/quic-conn-ack";
7
7
  *
8
- * const received = new QuicAckRanges();
9
- * if (!received.record(packetNumber, ackEliciting)) { … a duplicate: drop it … }
10
- * if (received.ackPending) { received.pushAck(payload, 0); }
8
+ * const received: QuicAckRanges = new QuicAckRanges()
9
+ * const fresh: boolean = received.record(toI64(7), true) // true: 7 is new and asks for an ACK
10
+ * const again: boolean = received.record(toI64(7), true) // false: a duplicate, so drop the packet
11
+ * const payload: u8[] = []
12
+ * if (received.ackPending) {
13
+ * received.pushAck(payload, toI64(0)) // appends ACK [7, 7], clears ackPending
14
+ * }
11
15
  *
12
16
  * **Bounded.** At most `QUIC_ACK_MAX_RANGES` ranges are kept, highest first.
13
17
  * A new range past that drops the lowest, and everything at or below what it
@@ -17,12 +21,12 @@
17
21
  * peer that sends packet numbers full of holes costs a fixed amount of memory
18
22
  * and loses only very late packets.
19
23
  *
20
- * **When to acknowledge** is the connection's choice; this module records
24
+ * **When to acknowledge.** That is the connection's choice; this module records
21
25
  * whether something ack-eliciting arrived since the last ACK it wrote
22
26
  * (`ackPending`). ACK frames carry an ACK Delay the caller supplies, and no
23
27
  * ECN counts.
24
28
  *
25
- * Written from RFC 9000 §12.3, §13.2 and §19.3, not ported from another
29
+ * **Provenance.** Written from RFC 9000 §12.3, §13.2 and §19.3, not ported from another
26
30
  * implementation. Private names carry the `quicAck` prefix
27
31
  * (`docs/wp26-stdlib.md` §3e).
28
32
  */
@@ -42,16 +46,25 @@ export class QuicAckRanges {
42
46
  largest: i64 = -1
43
47
  /** Every packet number at or below this is answered as already received; -1 until a range is dropped. */
44
48
  floor: i64 = -1
45
- ranges: i64[]
49
+ private ranges: i64[]
46
50
  count: i32 = 0
47
51
  /** Whether an ack-eliciting packet arrived since the last `pushAck`. */
48
52
  ackPending: boolean = false
49
53
 
54
+ /**
55
+ * Makes an empty set, nothing received yet, with room for
56
+ * `QUIC_ACK_MAX_RANGES` ranges.
57
+ */
50
58
  constructor() {
51
59
  this.ranges = new Array<i64>(QUIC_ACK_MAX_RANGES * 2)
52
60
  }
53
61
 
54
- /** The smallest packet number of range `k`, which the caller keeps below `count`. */
62
+ /**
63
+ * The smallest packet number of range `k`, which the caller keeps below `count`.
64
+ *
65
+ * @param k The range's index, 0 for the highest range.
66
+ * @returns The range's smallest packet number, or -1 for an index outside the storage.
67
+ */
55
68
  low(k: i32): i64 {
56
69
  const at: i32 = k * 2
57
70
  if (at < 0 || at >= toI32(this.ranges.length)) {
@@ -60,7 +73,12 @@ export class QuicAckRanges {
60
73
  return this.ranges[at]
61
74
  }
62
75
 
63
- /** The largest packet number of range `k`. */
76
+ /**
77
+ * The largest packet number of range `k`.
78
+ *
79
+ * @param k The range's index, 0 for the highest range; the caller keeps it below `count`.
80
+ * @returns The range's largest packet number, or -1 for an index outside the storage.
81
+ */
64
82
  high(k: i32): i64 {
65
83
  const at: i32 = k * 2 + 1
66
84
  if (at < 0 || at >= toI32(this.ranges.length)) {
@@ -69,7 +87,14 @@ export class QuicAckRanges {
69
87
  return this.ranges[at]
70
88
  }
71
89
 
72
- /** Sets range `k` to `[low, high]`. */
90
+ /**
91
+ * Sets range `k` to `[low, high]`. An index outside the storage changes nothing, and `count` is
92
+ * not moved: the caller keeps the ranges ordered and disjoint.
93
+ *
94
+ * @param k The range's index, 0 for the highest range.
95
+ * @param low The range's smallest packet number.
96
+ * @param high The range's largest packet number, at least `low`.
97
+ */
73
98
  set(k: i32, low: i64, high: i64): void {
74
99
  const at: i32 = k * 2
75
100
  const next: i32 = at + 1
@@ -80,7 +105,12 @@ export class QuicAckRanges {
80
105
  this.ranges[next] = high
81
106
  }
82
107
 
83
- /** Whether `pn` was received, or is at or below the floor and so cannot be told apart from one that was. */
108
+ /**
109
+ * Whether `pn` was received, or is at or below the floor and so cannot be told apart from one that was.
110
+ *
111
+ * @param pn The full (decoded) packet number to look up.
112
+ * @returns True when `pn` must be treated as already processed.
113
+ */
84
114
  contains(pn: i64): boolean {
85
115
  if (pn <= this.floor) {
86
116
  return true
@@ -101,6 +131,11 @@ export class QuicAckRanges {
101
131
  * an acknowledgement. Answers `false`, changing nothing, for a packet number
102
132
  * already received (or at or below the floor), or one that is not a packet
103
133
  * number at all.
134
+ *
135
+ * @param pn The full (decoded) packet number, which must be in 0..2^62-1 to count.
136
+ * @param ackEliciting Whether the packet held a frame other than ACK, PADDING or
137
+ * CONNECTION_CLOSE, so that it sets `ackPending`.
138
+ * @returns True when `pn` was new and is now recorded; false for a duplicate or an out-of-range number.
104
139
  */
105
140
  record(pn: i64, ackEliciting: boolean): boolean {
106
141
  if (pn < 0 || pn > QUIC_MAX_VARINT || this.contains(pn)) {
@@ -138,7 +173,7 @@ export class QuicAckRanges {
138
173
  }
139
174
 
140
175
  /** Removes range `k`, moving the lower ones up. */
141
- remove(k: i32): void {
176
+ private remove(k: i32): void {
142
177
  for (let j: i32 = k; j + 1 < this.count; j += 1) {
143
178
  this.set(j, this.low(j + 1), this.high(j + 1))
144
179
  }
@@ -150,7 +185,7 @@ export class QuicAckRanges {
150
185
  * is taken the lowest range is dropped first and the floor raised over it;
151
186
  * if `pn` itself would be that lowest range, it is the one dropped.
152
187
  */
153
- insert(k: i32, pn: i64): void {
188
+ private insert(k: i32, pn: i64): void {
154
189
  if (this.count === QUIC_ACK_MAX_RANGES) {
155
190
  if (k === this.count) {
156
191
  this.floor = pn
@@ -170,6 +205,11 @@ export class QuicAckRanges {
170
205
  * Appends an ACK frame for every range kept, with `ackDelay` (already
171
206
  * scaled by this endpoint's ACK delay exponent), and clears `ackPending`.
172
207
  * Answers `false`, appending nothing, when nothing has been received.
208
+ *
209
+ * @param out The packet payload being built; the frame is appended to its end.
210
+ * @param ackDelay The ACK Delay field's value: the time since the largest packet arrived, in
211
+ * microseconds divided by 2^ack_delay_exponent.
212
+ * @returns True when the frame was appended; false, leaving `out` and `ackPending` alone, otherwise.
173
213
  */
174
214
  pushAck(out: u8[], ackDelay: i64): boolean {
175
215
  if (this.count === 0 || !quicPushAck(out, this.ranges, this.count, ackDelay)) {
@@ -184,6 +224,13 @@ export class QuicAckRanges {
184
224
  * and clears `ackPending`. Answers the offset past it, or -1, writing
185
225
  * nothing and leaving `ackPending` as it was, when nothing has been
186
226
  * received or the frame does not fit.
227
+ *
228
+ * @param buf The packet buffer to write into.
229
+ * @param at The byte offset in `buf` where the frame starts.
230
+ * @param end The byte offset the frame must end at or before.
231
+ * @param ackDelay The ACK Delay field's value: the time since the largest packet arrived, in
232
+ * microseconds divided by 2^ack_delay_exponent.
233
+ * @returns The byte offset one past the frame, or -1 when nothing was written.
187
234
  */
188
235
  putAck(buf: u8[], at: i32, end: i32, ackDelay: i64): i32 {
189
236
  if (this.count === 0) {
@@ -5,25 +5,28 @@
5
5
  *
6
6
  * import { QuicCidTable } from "nish/net/quic-conn-cid";
7
7
  *
8
- * const ids = new QuicCidTable(4, 4); // the active_connection_id_limit advertised; local slots
9
- * ids.addLocal(serverScid, token); // sequence 0
10
- * ids.addPeer(0, 0, clientScid, none); // the client's first SCID, sequence 0
11
- * const error: i64 = ids.retireLocal(frame.value, packetDcid);
8
+ * const ids = new QuicCidTable(4, 4) // the limit advertised; local slots
9
+ * ids.addLocal(serverScid, []) // sequence 0, sent in the SCID, so no token
10
+ * ids.addLocal(nextCid, resetToken) // sequence 1, for a NEW_CONNECTION_ID frame
11
+ * ids.addPeer(0, 0, clientScid, []) // the client's first SCID, sequence 0
12
+ * const dcid: u8[] = ids.currentPeer() // the ID this side sends to
13
+ * const error: i64 = ids.retireLocal(sequence, packet, dcidAt, dcidLength)
14
+ * // error is QUIC_ERROR_NO_ERROR, or QUIC_ERROR_PROTOCOL_VIOLATION to close with
12
15
  *
13
- * **Local IDs** are the ones this side handed out: sequence 0 is the Source
16
+ * **Local IDs.** They are the ones this side handed out: sequence 0 is the Source
14
17
  * Connection ID of its first packet, and each later one goes out in a
15
18
  * NEW_CONNECTION_ID frame (`nextUnannounced` answers the next one still to). The peer
16
19
  * retires one with RETIRE_CONNECTION_ID: a sequence number never issued, or
17
20
  * the one the retiring packet was itself sent to, is a PROTOCOL_VIOLATION
18
21
  * (§19.16).
19
22
  *
20
- * **Peer IDs** come from the peer's first packet and its NEW_CONNECTION_ID
23
+ * **Peer IDs.** They come from the peer's first packet and its NEW_CONNECTION_ID
21
24
  * frames (§19.15). A sequence number seen before must carry the same ID and
22
25
  * token, or it is a PROTOCOL_VIOLATION; an ID already given another sequence
23
26
  * number is one too. More active IDs than the limit this side advertised is
24
27
  * a CONNECTION_ID_LIMIT_ERROR (§5.1.1). Retire Prior To retires every older ID
25
- * at once: each is queued in `retirePending`, for this side to send
26
- * RETIRE_CONNECTION_ID for, and `current` moves to the oldest still active.
28
+ * at once: each is queued for this side to send RETIRE_CONNECTION_ID for,
29
+ * and the ID this side sends to moves to the oldest still active.
27
30
  * Queued retirements past twice the limit are a CONNECTION_ID_LIMIT_ERROR too
28
31
  * (§5.1.2), which bounds what a peer can make this side owe it.
29
32
  *
@@ -67,13 +70,24 @@ export class QuicCidEntry {
67
70
  /** For a local ID: whether a NEW_CONNECTION_ID frame announced it yet. Sequence 0 needs none. */
68
71
  announced: boolean = false
69
72
 
73
+ /**
74
+ * Makes an unused slot, with room for the longest connection ID and its
75
+ * reset token.
76
+ */
70
77
  constructor() {
71
78
  this.cid = new Array<u8>(QUIC_MAX_CID_LENGTH)
72
79
  this.resetToken = new Array<u8>(QUIC_RESET_TOKEN_SIZE)
73
80
  }
74
81
  }
75
82
 
76
- /** Copies `length` bytes of `from` at `at` into the start of `to`, and zeroes the rest of `to`. */
83
+ /**
84
+ * Copies `length` bytes of `from` at `at` into the start of `to`, and zeroes the rest of `to`.
85
+ *
86
+ * @param to The destination; every one of its bytes is written, so it is fully overwritten.
87
+ * @param from The source; bytes it does not have (an index before 0 or past its end) are copied as zero.
88
+ * @param at The offset in `from` of the first byte to copy.
89
+ * @param length How many bytes to copy; past `to.length` the extra bytes are dropped.
90
+ */
77
91
  export const quicCidCopy = (to: u8[], from: u8[], at: i32, length: i32): void => {
78
92
  for (let k: i32 = 0; k < toI32(to.length); k += 1) {
79
93
  to[k] = k < length && at + k >= 0 && at + k < toI32(from.length) ? from[at + k] : toU8(0)
@@ -137,18 +151,24 @@ export class QuicCidTable {
137
151
  local: QuicCidEntry[]
138
152
  peer: QuicCidEntry[]
139
153
  /** Peer sequence numbers this side must send RETIRE_CONNECTION_ID for, oldest first, as a ring of `retireCount` from `retireHead`. */
140
- retirePending: i64[]
154
+ private retirePending: i64[]
141
155
  /** The next local sequence number to issue. */
142
156
  nextLocal: i64 = 0
143
157
  /** The peer's highest Retire Prior To so far. */
144
- peerRetirePriorTo: i64 = 0
145
- limit: i64 = 2
158
+ private peerRetirePriorTo: i64 = 0
159
+ private limit: i64 = 2
146
160
  /** The index in `peer` of the ID this side sends to, or -1 before the peer gave one. */
147
- current: i32 = -1
148
- retireHead: i32 = 0
161
+ private current: i32 = -1
162
+ private retireHead: i32 = 0
149
163
  retireCount: i32 = 0
150
164
 
151
- /** A table for an advertised `limit` (2 to 8 in practice) with `localCapacity` slots for this side's IDs. */
165
+ /**
166
+ * A table for an advertised `limit` (2 to 8 in practice) with `localCapacity` slots for this side's IDs.
167
+ *
168
+ * @param limit The `active_connection_id_limit` this side advertised: how many peer IDs may be active at
169
+ * once, and half the size of the retirement ring. Below 1 still gets one peer slot.
170
+ * @param localCapacity How many of this side's IDs can be active at once; `addLocal` answers -1 past it.
171
+ */
152
172
  constructor(limit: i64, localCapacity: i32) {
153
173
  const peerSlots: i32 = limit < 1 ? 1 : toI32(limit)
154
174
  this.local = quicCidSlots(localCapacity)
@@ -173,9 +193,15 @@ export class QuicCidTable {
173
193
  }
174
194
 
175
195
  /**
176
- * Issues the first `length` bytes of `cid` as the next local ID, with its
196
+ * Issues `cid`, up to its first 20 bytes, as the next local ID, with its
177
197
  * 16-byte reset token (or none, empty, for sequence 0), and answers its
178
198
  * sequence number; -1 when every local slot is taken.
199
+ *
200
+ * @param cid The ID's bytes, copied into the slot; past 20 bytes (`QUIC_MAX_CID_LENGTH`) the rest is
201
+ * dropped.
202
+ * @param resetToken The 16-byte stateless reset token, copied; any other length, empty included, means
203
+ * none.
204
+ * @returns The ID's sequence number, counting up from 0, or -1 when every local slot is taken.
179
205
  */
180
206
  addLocal(cid: u8[], resetToken: u8[]): i64 {
181
207
  const k: i32 = quicCidFree(this.local)
@@ -197,7 +223,11 @@ export class QuicCidTable {
197
223
  return sequence
198
224
  }
199
225
 
200
- /** How many local IDs are active: a retired one leaves its slot. */
226
+ /**
227
+ * How many local IDs are active: a retired one leaves its slot.
228
+ *
229
+ * @returns The count of issued, not yet retired local IDs, from 0 to `localCapacity`.
230
+ */
201
231
  activeLocal(): i32 {
202
232
  let n: i32 = 0
203
233
  for (const entry of this.local) {
@@ -208,12 +238,26 @@ export class QuicCidTable {
208
238
  return n
209
239
  }
210
240
 
211
- /** Whether `dcid` is an active local ID: whether a packet sent to it is this connection's. */
241
+ /**
242
+ * Whether `dcid` is an active local ID: whether a packet sent to it is this connection's.
243
+ *
244
+ * @param dcid A packet's whole Destination Connection ID.
245
+ * @returns True when it matches one of this side's active IDs byte for byte.
246
+ */
212
247
  ownsLocal(dcid: u8[]): boolean {
213
248
  return this.ownsLocalAt(dcid, QUIC_CID_ZERO, toI32(dcid.length))
214
249
  }
215
250
 
216
- /** Whether `buf[at .. at + length)` is an active local ID. Every slot is compared, each in constant time. */
251
+ /**
252
+ * Whether `buf[at .. at + length)` is an active local ID. Each active slot
253
+ * of that length is compared in constant time, and the loop does not stop
254
+ * at a match; unused slots and slots of another length are skipped.
255
+ *
256
+ * @param buf The bytes holding the ID, such as a received datagram; only read.
257
+ * @param at The offset of the ID's first byte in `buf`.
258
+ * @param length The ID's length in bytes.
259
+ * @returns True when the bytes match one of this side's active IDs of that length.
260
+ */
217
261
  ownsLocalAt(buf: u8[], at: i32, length: i32): boolean {
218
262
  let owned: boolean = false
219
263
  for (const entry of this.local) {
@@ -231,6 +275,12 @@ export class QuicCidTable {
231
275
  * ID the packet itself was sent to (§19.16). Retiring one twice is fine. A
232
276
  * retired ID leaves its slot, so a peer that retires one ID after another
233
277
  * cannot grow the table.
278
+ *
279
+ * @param sequence The Sequence Number field of the RETIRE_CONNECTION_ID frame.
280
+ * @param buf The bytes holding the Destination Connection ID of the packet the frame came in.
281
+ * @param at The offset of that ID's first byte in `buf`.
282
+ * @param length That ID's length in bytes.
283
+ * @returns `QUIC_ERROR_NO_ERROR`, or `QUIC_ERROR_PROTOCOL_VIOLATION` to close the connection with.
234
284
  */
235
285
  retireLocal(sequence: i64, buf: u8[], at: i32, length: i32): i64 {
236
286
  if (sequence < 0 || sequence >= this.nextLocal) {
@@ -247,7 +297,11 @@ export class QuicCidTable {
247
297
  return QUIC_ERROR_NO_ERROR
248
298
  }
249
299
 
250
- /** How many peer IDs are active: retired ones leave their slots. */
300
+ /**
301
+ * How many peer IDs are active: retired ones leave their slots.
302
+ *
303
+ * @returns The count of the peer's IDs not yet retired, from 0 to `limit`.
304
+ */
251
305
  activePeer(): i32 {
252
306
  let n: i32 = 0
253
307
  for (const entry of this.peer) {
@@ -258,7 +312,15 @@ export class QuicCidTable {
258
312
  return n
259
313
  }
260
314
 
261
- /** `addPeerAt` of whole arrays: `resetToken` empty for none. */
315
+ /**
316
+ * `addPeerAt` of whole arrays: `resetToken` empty for none.
317
+ *
318
+ * @param sequence The ID's sequence number: 0 for the SCID of the peer's first packet.
319
+ * @param retirePriorTo The frame's Retire Prior To field, 0 for the first packet's SCID.
320
+ * @param cid The whole ID, copied into a slot.
321
+ * @param resetToken The 16-byte stateless reset token; any other length, empty included, means none.
322
+ * @returns `QUIC_ERROR_NO_ERROR`, or the error to close the connection with, as `addPeerAt` answers.
323
+ */
262
324
  addPeer(sequence: i64, retirePriorTo: i64, cid: u8[], resetToken: u8[]): i64 {
263
325
  return this.addPeerAt(
264
326
  sequence,
@@ -282,6 +344,17 @@ export class QuicCidTable {
282
344
  * or the queued retirements would pass what §5.1 allows, in which case the
283
345
  * table is left as it was. The tokens are compared in constant time, since
284
346
  * a token is what a stateless reset is checked against.
347
+ *
348
+ * @param sequence The ID's sequence number from the NEW_CONNECTION_ID frame, or 0 for the first packet's
349
+ * SCID.
350
+ * @param retirePriorTo The frame's Retire Prior To field; a value below an earlier one changes nothing.
351
+ * @param cid The bytes holding the ID, such as the received packet; only read, and the ID is copied out.
352
+ * @param cidAt The offset of the ID's first byte in `cid`.
353
+ * @param cidLength The ID's length in bytes, 0 to 20; longer is a PROTOCOL_VIOLATION.
354
+ * @param token The bytes holding the reset token; read only when `hasToken`.
355
+ * @param tokenAt The offset of the token's first byte in `token`.
356
+ * @param hasToken Whether a 16-byte token comes with the ID: false only for the first packet's SCID.
357
+ * @returns `QUIC_ERROR_NO_ERROR`, `QUIC_ERROR_PROTOCOL_VIOLATION` or `QUIC_ERROR_CONNECTION_ID_LIMIT`.
285
358
  */
286
359
  addPeerAt(
287
360
  sequence: i64,
@@ -363,18 +436,28 @@ export class QuicCidTable {
363
436
  return QUIC_ERROR_NO_ERROR
364
437
  }
365
438
 
366
- /** Points `current` at the oldest active peer ID, or -1 when none is. */
439
+ /** Makes the oldest active peer ID the current one, or none when no peer ID is active. */
367
440
  prune(): void {
368
441
  this.current = quicCidOldest(this.peer, -1, false)
369
442
  }
370
443
 
371
- /** The peer ID this side sends to: its slot, or `null` before the peer gave any. */
444
+ /**
445
+ * The peer ID this side sends to: its slot, or `null` before the peer gave any.
446
+ *
447
+ * @returns The table's own entry, not a copy, so it changes when the peer's IDs do; `null` when no
448
+ * peer ID is active.
449
+ */
372
450
  currentPeerEntry(): QuicCidEntry | null {
373
451
  const at: i32 = this.current
374
452
  return at >= 0 && at < toI32(this.peer.length) ? this.peer[at] : null
375
453
  }
376
454
 
377
- /** A copy of the peer ID this side sends to, or empty before the peer gave any. */
455
+ /**
456
+ * A copy of the peer ID this side sends to, or empty before the peer gave any.
457
+ *
458
+ * @returns A new array of the ID's bytes, the caller's to keep; empty when no peer ID is active, and
459
+ * also when the active one is zero-length (use `currentPeerEntry` to tell the two apart).
460
+ */
378
461
  currentPeer(): u8[] {
379
462
  const entry: QuicCidEntry | null = this.currentPeerEntry()
380
463
  const length: i32 = entry !== null ? entry.length : 0
@@ -385,7 +468,12 @@ export class QuicCidTable {
385
468
  return out
386
469
  }
387
470
 
388
- /** Queues a RETIRE_CONNECTION_ID for peer sequence `sequence`, unless it is queued already or the ring is full. */
471
+ /**
472
+ * Queues a RETIRE_CONNECTION_ID for peer sequence `sequence`, unless it is queued already or the
473
+ * ring is full.
474
+ *
475
+ * @param sequence The sequence number of the peer ID to retire; `takeRetire` hands it back.
476
+ */
389
477
  queueRetire(sequence: i64): void {
390
478
  const size: i32 = toI32(this.retirePending.length)
391
479
  for (let k: i32 = 0; k < this.retireCount; k += 1) {
@@ -404,7 +492,11 @@ export class QuicCidTable {
404
492
  }
405
493
  }
406
494
 
407
- /** The oldest queued retirement, or -1 when none is queued. Taking it removes it. */
495
+ /**
496
+ * The oldest queued retirement, or -1 when none is queued. Taking it removes it.
497
+ *
498
+ * @returns The peer sequence number to send RETIRE_CONNECTION_ID for, or -1 when none is queued.
499
+ */
408
500
  takeRetire(): i64 {
409
501
  const size: i32 = toI32(this.retirePending.length)
410
502
  if (this.retireCount === 0 || size === 0) {
@@ -417,7 +509,12 @@ export class QuicCidTable {
417
509
  return first
418
510
  }
419
511
 
420
- /** The first local ID still to be announced in a NEW_CONNECTION_ID frame, oldest first, or `null`. */
512
+ /**
513
+ * The first local ID still to be announced in a NEW_CONNECTION_ID frame, oldest first, or `null`.
514
+ *
515
+ * @returns The table's own entry, whose `announced` the caller sets once the frame is sent; `null` when
516
+ * every active local ID was announced.
517
+ */
421
518
  nextUnannounced(): QuicCidEntry | null {
422
519
  const k: i32 = quicCidOldest(this.local, -1, true)
423
520
  return k >= 0 && k < toI32(this.local.length) ? this.local[k] : null
@@ -6,19 +6,22 @@
6
6
  * import { QuicTransportParameters, quicEncodeTransportParameters,
7
7
  * quicParseTransportParameters } from "nish/net/quic-conn-params";
8
8
  *
9
- * const mine = new QuicTransportParameters();
10
- * mine.initialMaxData = 65536;
11
- * const bytes: u8[] = quicEncodeTransportParameters(mine); // for TlsServerConfig
12
- * const theirs = quicParseTransportParameters(clientBytes, false);
13
- * if (theirs.error !== 0) { … close with theirs.error … }
9
+ * const mine: QuicTransportParameters = new QuicTransportParameters()
10
+ * mine.initialMaxData = 65536
11
+ * const bytes: u8[] = quicEncodeTransportParameters(mine) // for TlsServerConfig
12
+ * // bytes is [0x04, 0x04, 0x80, 0x01, 0x00, 0x00]: initial_max_data, length 4, 65536
13
+ * const theirs: QuicTransportParameters = quicParseTransportParameters(bytes, false)
14
+ * if (theirs.error !== 0) {
15
+ * // close the connection with theirs.error; theirs.errorParameter names the parameter
16
+ * }
14
17
  *
15
- * A `QuicTransportParameters` starts at every parameter's default (§18.2),
16
- * so a parse leaves a parameter the peer did not send at the value the RFC
18
+ * **Defaults.** A `QuicTransportParameters` starts at every parameter's
19
+ * default (§18.2), so a parse leaves a parameter the peer did not send at the value the RFC
17
20
  * says it then has. The encoder writes a parameter only when it differs from
18
21
  * that default, plus the connection IDs, which have none.
19
22
  *
20
- * **What a parse refuses**, each a TRANSPORT_PARAMETER_ERROR (§7.4, §18.2):
21
- * an ID or a length that does not fit, a parameter running past the bytes, a
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
22
25
  * known parameter sent twice, a varint-valued parameter whose value is not
23
26
  * exactly one varint, `disable_active_migration` with a value, a server-only
24
27
  * parameter from a client (`original_destination_connection_id`,
@@ -32,7 +35,7 @@
32
35
  * Whether `initial_source_connection_id` is present and right is the
33
36
  * connection's check, because it needs the packet the ID came in.
34
37
  *
35
- * Written from RFC 9000 §18, in this module's own structure; nothing here is
38
+ * **Provenance.** Written from RFC 9000 §18, in this module's own structure; nothing here is
36
39
  * ported from another implementation. Private names carry the
37
40
  * `quicParams` prefix (`docs/wp26-stdlib.md` §3e).
38
41
  */
@@ -51,22 +54,60 @@ import {
51
54
 
52
55
  // ---- Parameter IDs (RFC 9000 §18.2) ---------------------------------------------
53
56
 
57
+ /**
58
+ * `original_destination_connection_id`: the Destination Connection ID of the client's first Initial; server
59
+ * only.
60
+ */
54
61
  export const QUIC_TP_ORIGINAL_DCID: i32 = 0x00
62
+ /** `max_idle_timeout`, in milliseconds; 0 or absent for no idle timeout. */
55
63
  export const QUIC_TP_MAX_IDLE_TIMEOUT: i32 = 0x01
64
+ /** `stateless_reset_token`: the 16-byte token for the server's first connection ID; server only. */
56
65
  export const QUIC_TP_STATELESS_RESET_TOKEN: i32 = 0x02
66
+ /**
67
+ * `max_udp_payload_size`: the largest UDP payload, in bytes, the sender takes; at least 1200, default 65527.
68
+ */
57
69
  export const QUIC_TP_MAX_UDP_PAYLOAD_SIZE: i32 = 0x03
70
+ /** `initial_max_data`: the connection-wide flow-control limit, in bytes. */
58
71
  export const QUIC_TP_INITIAL_MAX_DATA: i32 = 0x04
72
+ /**
73
+ * `initial_max_stream_data_bidi_local`: the flow-control limit, in bytes, for bidirectional streams the
74
+ * sender opens.
75
+ */
59
76
  export const QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL: i32 = 0x05
77
+ /**
78
+ * `initial_max_stream_data_bidi_remote`: the flow-control limit, in bytes, for bidirectional streams the peer
79
+ * opens.
80
+ */
60
81
  export const QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE: i32 = 0x06
82
+ /**
83
+ * `initial_max_stream_data_uni`: the flow-control limit, in bytes, for unidirectional streams the peer opens.
84
+ */
61
85
  export const QUIC_TP_INITIAL_MAX_STREAM_DATA_UNI: i32 = 0x07
86
+ /** `initial_max_streams_bidi`: how many bidirectional streams the peer may open; at most 2^60. */
62
87
  export const QUIC_TP_INITIAL_MAX_STREAMS_BIDI: i32 = 0x08
88
+ /** `initial_max_streams_uni`: how many unidirectional streams the peer may open; at most 2^60. */
63
89
  export const QUIC_TP_INITIAL_MAX_STREAMS_UNI: i32 = 0x09
90
+ /** `ack_delay_exponent`: an ACK Delay field counts units of 2^this microseconds; at most 20, default 3. */
64
91
  export const QUIC_TP_ACK_DELAY_EXPONENT: i32 = 0x0a
92
+ /**
93
+ * `max_ack_delay`: the longest the sender delays an acknowledgement, in milliseconds; below 2^14, default 25.
94
+ */
65
95
  export const QUIC_TP_MAX_ACK_DELAY: i32 = 0x0b
96
+ /** `disable_active_migration`: present, with no value, when the sender takes no connection migration. */
66
97
  export const QUIC_TP_DISABLE_ACTIVE_MIGRATION: i32 = 0x0c
98
+ /** `preferred_address`: an address the server would have the client move to; server only. */
67
99
  export const QUIC_TP_PREFERRED_ADDRESS: i32 = 0x0d
100
+ /**
101
+ * `active_connection_id_limit`: how many of the peer's connection IDs the sender keeps; at least 2, the
102
+ * default.
103
+ */
68
104
  export const QUIC_TP_ACTIVE_CONNECTION_ID_LIMIT: i32 = 0x0e
105
+ /** `initial_source_connection_id`: the Source Connection ID of the sender's first Initial. */
69
106
  export const QUIC_TP_INITIAL_SCID: i32 = 0x0f
107
+ /**
108
+ * `retry_source_connection_id`: the Source Connection ID of the server's Retry, when it sent one; server
109
+ * only.
110
+ */
70
111
  export const QUIC_TP_RETRY_SCID: i32 = 0x10
71
112
  /**
72
113
  * `max_datagram_frame_size` (RFC 9221 §3): the largest DATAGRAM frame, type
@@ -117,6 +158,10 @@ export class QuicTransportParameters {
117
158
  hasRetryScid: boolean = false
118
159
  disableActiveMigration: boolean = false
119
160
 
161
+ /**
162
+ * Makes the parameters an endpoint has before it sets or reads any: §18.2's
163
+ * defaults, every connection ID and token empty.
164
+ */
120
165
  constructor() {
121
166
  this.originalDcid = []
122
167
  this.statelessResetToken = []
@@ -191,6 +236,9 @@ const quicParamsPushBytes = (out: u8[], id: i32, bytes: u8[], present: boolean):
191
236
  * default, every byte-valued one only when its `has*` flag is set, and
192
237
  * `disable_active_migration` only when true. Nothing is checked: these are
193
238
  * this endpoint's own values, and a value no varint holds is left out.
239
+ *
240
+ * @param p This endpoint's parameters, only read.
241
+ * @returns A new array holding the extension body; empty when every parameter is at its default.
194
242
  */
195
243
  export const quicEncodeTransportParameters = (p: QuicTransportParameters): u8[] => {
196
244
  const out: u8[] = []
@@ -203,6 +251,9 @@ export const quicEncodeTransportParameters = (p: QuicTransportParameters): u8[]
203
251
  * encoding is pushed into the room the array already has, so a connection
204
252
  * slot that keeps one array for its parameters grows it only when they
205
253
  * outgrow every earlier connection's.
254
+ *
255
+ * @param p This endpoint's parameters, only read.
256
+ * @param out The array the extension body replaces the contents of.
206
257
  */
207
258
  export const quicEncodeTransportParametersInto = (p: QuicTransportParameters, out: u8[]): void => {
208
259
  quicParamsEmpty(out)
@@ -387,6 +438,10 @@ const quicParamsIsVarint = (id: i32): boolean =>
387
438
  * `QUIC_ERROR_TRANSPORT_PARAMETER` for any of the breaks the module header
388
439
  * lists, with `errorParameter` naming the parameter; on an error the other
389
440
  * fields hold what was read so far and are not to be used.
441
+ *
442
+ * @param bytes The extension body as the peer sent it, only read.
443
+ * @param fromServer True when the peer is the server, so that the server-only parameters are allowed.
444
+ * @returns A new `QuicTransportParameters`; read its `error` before any other field.
390
445
  */
391
446
  export const quicParseTransportParameters = (bytes: u8[], fromServer: boolean): QuicTransportParameters => {
392
447
  const p: QuicTransportParameters = new QuicTransportParameters()
@@ -399,6 +454,11 @@ export const quicParseTransportParameters = (bytes: u8[], fromServer: boolean):
399
454
  * default first, its byte-valued fields emptied and refilled in the room
400
455
  * their arrays have: a connection slot parses each client's parameters into
401
456
  * one object of its own. Answers `p`.
457
+ *
458
+ * @param p The object to overwrite with the parsed parameters and the verdict in `error`.
459
+ * @param bytes The extension body as the peer sent it, only read.
460
+ * @param fromServer True when the peer is the server, so that the server-only parameters are allowed.
461
+ * @returns `p` itself; read its `error` before any other field.
402
462
  */
403
463
  export const quicParseTransportParametersInto = (
404
464
  p: QuicTransportParameters,