nish 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 (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
@@ -9,16 +9,31 @@
9
9
  * connection's stream calls (`openStream`, `streamWrite`, `streamRead`,
10
10
  * `nextStreamEvent` and the rest), not directly.
11
11
  *
12
+ * const pools = new QuicStreamPools(65536, 111, 64) // 64 connections of 100 + 3 + 8 slots
13
+ * const streams = new QuicStreams(100, 3, 8, 65536, 1048576) // client bidi, client uni, own; bytes
14
+ * streams.share(pools) // before any stream has borrowed
15
+ * streams.setPeerLimits(maxData, bidiLocal, bidiRemote, uni, maxStreamsBidi, maxStreamsUni)
16
+ * streams.prime(packetLength) // per packet, before its frames: fills the reserve
17
+ * const error: i64 = streams.onStream(frame.streamId, frame.offset, payload, frame.dataStart,
18
+ * frame.dataLength, frame.fin) // 0, or the transport error to close with
19
+ * const id: i64 = frame.streamId // the stream the frame was for
20
+ * const got: i32 = streams.read(id, into, 0, 4096) // bytes, 0, QUIC_STREAM_END or an error
21
+ * const taken: i32 = streams.write(id, reply, 0, toI32(reply.length), true)
22
+ * streams.beginPacket()
23
+ * const next: i32 = streams.putNextChunk(out, at, end) // `at` when no frame fits
24
+ *
12
25
  * **IDs** (§2.1). The low bit says who opened a stream (0 the client, 1 the
13
- * server) and the next one whether it carries data both ways (0) or only
14
- * from its opener (1). This side is a server: it opens IDs 1 and 3 modulo 4,
15
- * and the client 0 and 2. A frame for a stream of the client's that is past
16
- * the limit this side advertised is STREAM_LIMIT_ERROR; for one of the
17
- * server's that it has not opened, STREAM_STATE_ERROR; and a frame that only
18
- * makes sense on a side the stream does not have (STREAM, RESET_STREAM or
19
- * STREAM_DATA_BLOCKED on the server's unidirectional stream; MAX_STREAM_DATA
20
- * or STOP_SENDING on the client's) is STREAM_STATE_ERROR (§19.4-§19.13). A
21
- * client's stream opens every lower one of its type with it (§3.2).
26
+ * server) and the next one whether it carries data both ways (0) or only from
27
+ * its opener (1).
28
+ *
29
+ * This side is a server: it opens IDs 1 and 3 modulo 4, and the client 0 and 2.
30
+ * A frame for a stream of the client's that is past the limit this side
31
+ * advertised is STREAM_LIMIT_ERROR; for one of the server's that it has not
32
+ * opened, STREAM_STATE_ERROR; and a frame that only makes sense on a side the
33
+ * stream does not have (STREAM, RESET_STREAM or STREAM_DATA_BLOCKED on the
34
+ * server's unidirectional stream; MAX_STREAM_DATA or STOP_SENDING on the
35
+ * client's) is STREAM_STATE_ERROR (§19.4-§19.13). A client's stream opens every
36
+ * lower one of its type with it (§3.2).
22
37
  *
23
38
  * **Fixed slots.** The table is sized once, from the configuration: one slot
24
39
  * per stream that may be open at once — as many client-opened bidirectional
@@ -30,31 +45,34 @@
30
45
  *
31
46
  * **Lent buffers.** A slot holds no buffers of its own. A side borrows a
32
47
  * `bufferSize`-byte buffer, and a bit per byte beside it, from the table's
33
- * `QuicStreamPools` when it first needs one: the receive side on the first
34
- * byte that lands, the send side on the first byte written. Each goes back,
35
- * its used extent zeroed, once it is done with: the receive side once the
36
- * application has read to the final size or the stream is reset, the send
37
- * side once every byte up to the final size is acknowledged or the stream
38
- * is reset, never before, since a lost byte is resent from the send buffer.
39
- * The pools hold two buffers per slot at most, the reserve's included, so a
40
- * borrow never fails, and
41
- * a server shares one pool across all its connections (`share`), so what it
42
- * holds follows the streams moving bytes, not the slots it was sized for.
43
- * Once the pools are warm, nothing a peer sends and nothing the application
44
- * writes allocates.
48
+ * `QuicStreamPools` when it first needs one: the receive side on the first byte
49
+ * that lands, the send side on the first byte written.
50
+ *
51
+ * Each goes back, its used extent zeroed, once it is done with: the receive
52
+ * side once the application has read to the final size or the stream is reset,
53
+ * the send side once every byte up to the final size is acknowledged or the
54
+ * stream is reset, never before, since a lost byte is resent from the send
55
+ * buffer.
56
+ *
57
+ * The pools hold two buffers per slot at most, the reserve's included, so a
58
+ * borrow never fails, and a server shares one pool across all its connections
59
+ * (`share`), so what it holds follows the streams moving bytes, not the slots
60
+ * it was sized for. Once the pools are warm, nothing a peer sends and nothing
61
+ * the application writes allocates.
45
62
  *
46
63
  * **Receiving** (§3.2, §2.2). Data lands in the receive buffer at its offset
47
64
  * modulo the buffer's size, in any order and overlapping, with a bit set per
48
- * byte; what continues the stream from the last byte read is what
49
- * `read` hands out. The credit this side gives a stream (MAX_STREAM_DATA) is
50
- * never more than what the buffer can hold past the last byte read, so a
51
- * byte beyond it is FLOW_CONTROL_ERROR and the buffer cannot overflow; it is
52
- * raised by a whole buffer once the application has read half of one. The
53
- * connection's credit (MAX_DATA) is raised the same way, by `windowData`
54
- * once half of it is read. A FIN or RESET_STREAM fixes the final size, and
55
- * a frame that disagrees with it, or a size below what already arrived, is
56
- * FINAL_SIZE_ERROR (§4.5). A reset stream's bytes count as read for the
57
- * connection's credit.
65
+ * byte; what continues the stream from the last byte read is what `read` hands
66
+ * out. The credit this side gives a stream (MAX_STREAM_DATA) is never more than
67
+ * what the buffer can hold past the last byte read, so a byte beyond it is
68
+ * FLOW_CONTROL_ERROR and the buffer cannot overflow; it is raised by a whole
69
+ * buffer once the application has read half of one.
70
+ *
71
+ * The connection's credit (MAX_DATA) is raised the same way, by `windowData`
72
+ * once half of it is read. A FIN or RESET_STREAM fixes the final size, and a
73
+ * frame that disagrees with it, or a size below what already arrived, is
74
+ * FINAL_SIZE_ERROR (§4.5). A reset stream's bytes count as read for the
75
+ * connection's credit.
58
76
  *
59
77
  * **Sending** (§3.1). The application writes into the send buffer as far as
60
78
  * it has room, a chunk at a time: what `write` takes is its back-pressure,
@@ -185,19 +203,41 @@ const QUIC_STREAM_ZERO: i64 = 0
185
203
  const QUIC_STREAM_ONE: i32 = 1
186
204
  const QUIC_STREAM_NO_BYTES: i32 = 0
187
205
 
188
- /** Whether `id` is a stream this side, the server, opens: its low bit is 1. */
206
+ /**
207
+ * Whether `id` is a stream this side, the server, opens: its low bit is 1.
208
+ *
209
+ * @param id A stream ID (§2.1).
210
+ * @returns True for a server-opened stream, false for a client-opened one.
211
+ */
189
212
  export const quicStreamIsLocal = (id: i64): boolean => (id & 1) !== 0
190
213
 
191
- /** Whether `id` is a unidirectional stream: its second bit is 1. */
214
+ /**
215
+ * Whether `id` is a unidirectional stream: its second bit is 1.
216
+ *
217
+ * @param id A stream ID (§2.1).
218
+ * @returns True when only the opener sends on it, false for a bidirectional stream.
219
+ */
192
220
  export const quicStreamIsUni = (id: i64): boolean => (id & 2) !== 0
193
221
 
194
- /** Whether bit `k` of `bits` is set; false outside the array. */
222
+ /**
223
+ * Whether bit `k` of `bits` is set; false outside the array.
224
+ *
225
+ * @param bits A bitmap, bit `k` in byte `k >> 3` at bit `k & 7`.
226
+ * @param k The bit's index, from 0.
227
+ * @returns True when the bit is set; false when it is clear or `k` is outside the array.
228
+ */
195
229
  export const quicStreamBit = (bits: u8[], k: i32): boolean => {
196
230
  const at: i32 = k >> 3
197
231
  return at >= 0 && at < toI32(bits.length) && (toI32(bits[at]) & (QUIC_STREAM_ONE << (k & 7))) !== 0
198
232
  }
199
233
 
200
- /** Sets or clears bit `k` of `bits`. */
234
+ /**
235
+ * Sets or clears bit `k` of `bits`.
236
+ *
237
+ * @param bits A bitmap, bit `k` in byte `k >> 3` at bit `k & 7`; changed in place.
238
+ * @param k The bit's index, from 0. An index outside the array changes nothing.
239
+ * @param on True to set the bit, false to clear it.
240
+ */
201
241
  export const quicStreamSetBit = (bits: u8[], k: i32, on: boolean): void => {
202
242
  const at: i32 = k >> 3
203
243
  if (at < 0 || at >= toI32(bits.length)) {
@@ -212,50 +252,64 @@ export const quicStreamSetBit = (bits: u8[], k: i32, on: boolean): void => {
212
252
  const quicStreamMin = (a: i64, b: i64): i64 => (a < b ? a : b)
213
253
 
214
254
  /**
215
- * The buffers a connection's streams borrow: `data` lends the receive and
216
- * send buffers of `bufferSize` bytes, `bits` the bitmaps beside them, a bit
217
- * per byte. One table makes a private set for itself; a server makes one set
218
- * for every slot and hands it to each connection's table with
219
- * `QuicStreams.share`, so the memory follows the streams moving bytes rather
220
- * than the slots the server was sized for. Each pool's ceiling is two
221
- * buffers for every stream it serves, one a side, which is what holding
222
- * every buffer at once costs. The reserve below counts against it too, so a
223
- * send side that finds the pools out takes the reserve's last buffer
224
- * (`lendSend`): with that side holding none, not every buffer is lent, and
225
- * the one left over is in the reserve.
255
+ * The buffers a connection's streams borrow: `data` lends the receive and send
256
+ * buffers of `bufferSize` bytes, `bits` the bitmaps beside them, a bit per
257
+ * byte. One table makes a private set for itself; a server makes one set for
258
+ * every slot and hands it to each connection's table with `QuicStreams.share`,
259
+ * so the memory follows the streams moving bytes rather than the slots the
260
+ * server was sized for. Each pool's ceiling is two buffers for every stream it
261
+ * serves, one a side, which is what holding every buffer at once costs.
262
+ *
263
+ * The reserve below counts against it too, so a send side that finds the pools
264
+ * out takes the reserve's last buffer (`lendSend`): with that side holding
265
+ * none, not every buffer is lent, and the one left over is in the reserve.
226
266
  *
227
267
  * Frames are handled inside the arena scope a packet is opened in, where
228
268
  * nothing may allocate, grow a list or push onto one. So the frames lend and
229
- * take back receive buffers through a reserve the pools keep beside their
230
- * free lists, by index alone: `lend` hands one out, `keep` takes one back
231
- * zeroed. Before each scope `prime` trims the reserve to `hold` buffers,
232
- * giving the rest to the pools, and tops it up to as many as the packet
233
- * could start receive sides, taking them. `hold` is one connection's stream
234
- * count, and the reserve three times that long, so a packet that lends
235
- * nothing and takes back every buffer its connection held still fits. Once
236
- * warm, the reserve mostly circulates on its own and `prime` costs a handful
237
- * of comparisons, with no take or give. The pools count the reserve's
238
- * buffers as lent. `prime` and the `drain` before each give-back outside a
239
- * packet trim it to `hold` for the whole server, not each connection, but
240
- * `keep` inside a packet can raise it to three times `hold`, and it stays
241
- * there until the next `prime` or `drain`.
269
+ * take back receive buffers through a reserve the pools keep beside their free
270
+ * lists, by index alone: `lend` hands one out, `keep` takes one back zeroed.
271
+ * Before each scope `prime` trims the reserve to `hold` buffers, giving the
272
+ * rest to the pools, and tops it up to as many as the packet could start
273
+ * receive sides, taking them.
274
+ *
275
+ * `hold` is one connection's stream count, and the reserve three times that
276
+ * long, so a packet that lends nothing and takes back every buffer its
277
+ * connection held still fits. Once warm, the reserve mostly circulates on its
278
+ * own and `prime` costs a handful of comparisons, with no take or give. The
279
+ * pools count the reserve's buffers as lent. `prime` and the `drain` before
280
+ * each give-back outside a packet trim it to `hold` for the whole server, not
281
+ * each connection, but `keep` inside a packet can raise it to three times
282
+ * `hold`, and it stays there until the next `prime` or `drain`.
242
283
  */
243
284
  export class QuicStreamPools {
285
+ /** The pool of stream buffers, `bufferSize` bytes each, that the send and receive sides borrow. */
244
286
  data: BufferPool
287
+ /** The pool of bitmaps beside them, a bit per buffer byte: which bytes are unread or acknowledged. */
245
288
  bits: BufferPool
246
289
  /** The reserve: `ready` buffers and their bitmaps, taken from the pools and not lent to a stream; the rest of each list is `none`. */
247
290
  readyData: u8[][]
291
+ /** The reserve's bitmaps, one beside each `readyData` buffer at the same index. */
248
292
  readyBits: u8[][]
249
293
  /** The empty array a free reserve entry holds. */
250
- none: u8[]
294
+ private none: u8[]
251
295
  /** How many buffers the reserve holds. */
252
296
  ready: i32 = 0
253
- /** The most the reserve keeps from one packet to the next: one connection's stream count. */
297
+ /**
298
+ * The most the reserve keeps from one packet to the next: one connection's stream count.
299
+ * @internal Read in quic-stream.ts outside the class.
300
+ */
254
301
  hold: i32 = 0
255
302
  /** How many buffers each pool's free list has been grown to hold, by `fit`. */
256
- room: i32 = 0
303
+ private room: i32 = 0
257
304
 
258
- /** Pools for `connections` connections of `streams` stream slots each, `bufferSize` bytes each way. */
305
+ /**
306
+ * Pools for `connections` connections of `streams` stream slots each, `bufferSize` bytes each way.
307
+ *
308
+ * @param bufferSize The bytes of each stream buffer; each bitmap is a bit per byte of it.
309
+ * @param streams The stream slots of one connection, its `QuicStreams` count.
310
+ * @param connections How many connections share the pools. Each pool holds at most
311
+ * `streams * 2 * connections` buffers.
312
+ */
259
313
  constructor(bufferSize: i32, streams: i32, connections: i32) {
260
314
  this.data = new BufferPool("QuicStreams.data", bufferSize, streams * 2 * connections)
261
315
  this.bits = new BufferPool("QuicStreams.bits", (bufferSize + 7) >> 3, streams * 2 * connections)
@@ -273,6 +327,7 @@ export class QuicStreamPools {
273
327
  * Readies the reserve for a packet that could start `need` receive sides:
274
328
  * trims it to `hold` buffers, then tops it up to `need`. Takes and gives
275
329
  * only when it is outside that window. Never inside an arena scope.
330
+ * @internal Read in quic-stream.ts outside the class.
276
331
  */
277
332
  prime(need: i32): void {
278
333
  this.drain(this.hold)
@@ -284,7 +339,10 @@ export class QuicStreamPools {
284
339
  this.fit()
285
340
  }
286
341
 
287
- /** Gives the reserve's buffers past the first `most` back to the pools, each already zeroed by `keep`. Never inside an arena scope. */
342
+ /**
343
+ * Gives the reserve's buffers past the first `most` back to the pools, each already zeroed by `keep`. Never inside an arena scope.
344
+ * @internal Read in quic-stream.ts outside the class.
345
+ */
288
346
  drain(most: i32): void {
289
347
  while (this.ready > most) {
290
348
  this.ready = this.ready - 1
@@ -303,7 +361,7 @@ export class QuicStreamPools {
303
361
  * the arena must not move by it (QUIC-3). The room doubles, so the pushes
304
362
  * cost O(1) for each buffer made.
305
363
  */
306
- fit(): void {
364
+ private fit(): void {
307
365
  const made: i32 = this.data.made
308
366
  if (made <= this.room) {
309
367
  return
@@ -320,7 +378,10 @@ export class QuicStreamPools {
320
378
  this.room = target
321
379
  }
322
380
 
323
- /** Lends the reserve's last buffer, into `stream`'s receive side. Panics when the reserve is empty: a packet handled without `prime`. */
381
+ /**
382
+ * Lends the reserve's last buffer, into `stream`'s receive side. Panics when the reserve is empty: a packet handled without `prime`.
383
+ * @internal Read in quic-stream.ts outside the class.
384
+ */
324
385
  lend(stream: QuicStream): void {
325
386
  if (this.ready <= 0) {
326
387
  panic("QuicStreams: a receive buffer was wanted with none primed")
@@ -338,6 +399,7 @@ export class QuicStreamPools {
338
399
  * the same ceiling, so once the pools have made every buffer and lent them
339
400
  * all, a send side wanting one means the reserve holds it. Never inside an
340
401
  * arena scope, where a take would allocate.
402
+ * @internal Read in quic-stream.ts outside the class.
341
403
  */
342
404
  lendSend(stream: QuicStream): void {
343
405
  if (this.data.free.length === 0 && this.data.made >= this.data.ceiling && this.ready > 0) {
@@ -358,6 +420,7 @@ export class QuicStreamPools {
358
420
  * first `used` bytes of the buffer and the bits of them. Safe inside an
359
421
  * arena scope. Panics when the reserve is full: more given back between
360
422
  * two `drain`s than one connection holds, a table's bug.
423
+ * @internal Read in quic-stream.ts outside the class.
361
424
  */
362
425
  keep(buf: u8[], have: u8[], used: i32): void {
363
426
  if (this.ready >= toI32(this.readyData.length)) {
@@ -370,7 +433,12 @@ export class QuicStreamPools {
370
433
  this.ready = this.ready + 1
371
434
  }
372
435
 
373
- /** How many buffers and bitmaps are lent to streams now, the reserve's left out: 0 once every stream that borrowed has given back. */
436
+ /**
437
+ * How many buffers and bitmaps are lent to streams now, the reserve's left out: 0 once every stream
438
+ * that borrowed has given back.
439
+ *
440
+ * @returns The buffers plus the bitmaps out on loan, so a side holding both counts 2.
441
+ */
374
442
  lent(): i32 {
375
443
  return this.data.lent() + this.bits.lent() - this.ready - this.ready
376
444
  }
@@ -380,6 +448,10 @@ export class QuicStreamPools {
380
448
  * Pushes `none` onto `free` until it is `target` long, then pops each one off
381
449
  * again: the capacity stays, so giving back that many buffers to the pool the
382
450
  * list belongs to never grows the arena.
451
+ *
452
+ * @param free A pool's free list; its length is unchanged on return.
453
+ * @param target The capacity, in entries, the list is grown to. No larger than its length does nothing.
454
+ * @param none The placeholder pushed and popped, the table's empty array.
383
455
  */
384
456
  export const quicStreamGrowFree = (free: u8[][], target: i32, none: u8[]): void => {
385
457
  const length: i32 = toI32(free.length)
@@ -397,6 +469,7 @@ export const quicStreamGrowFree = (free: u8[][], target: i32, none: u8[]): void
397
469
  * it asked this side to stop — and written only by `QuicStreams`.
398
470
  */
399
471
  export class QuicStream {
472
+ /** The stream ID (RFC 9000 §2.1), or -1 while the slot is free. */
400
473
  id: i64 = -1
401
474
  /**
402
475
  * The receive buffer: byte `o` of the stream at `o % size`, its bit in
@@ -404,8 +477,12 @@ export class QuicStream {
404
477
  * first byte arrives and after the side is done, and borrowed in between.
405
478
  */
406
479
  recvBuf: u8[]
480
+ /** The bitmap beside `recvBuf`: a bit per buffer byte, set while that byte has arrived and is unread. */
407
481
  recvHave: u8[]
408
- /** How far the application has read. */
482
+ /**
483
+ * How far the application has read.
484
+ * @internal Read by http3.ts, and in quic-stream.ts outside the class.
485
+ */
409
486
  recvRead: i64 = 0
410
487
  /** Every byte below this has arrived. */
411
488
  recvContiguous: i64 = 0
@@ -417,7 +494,10 @@ export class QuicStream {
417
494
  recvLimit: i64 = 0
418
495
  /** The peer's RESET_STREAM error code, or -1. */
419
496
  resetCode: i64 = -1
420
- /** The STOP_SENDING error code this side asked with, or -1. */
497
+ /**
498
+ * The STOP_SENDING error code this side asked with, or -1.
499
+ * @internal Read in quic-stream.ts outside the class.
500
+ */
421
501
  stopSendingCode: i64 = -1
422
502
  /**
423
503
  * The send buffer: byte `o` at `o % size`, its bit in `sendAcked` set once
@@ -426,6 +506,7 @@ export class QuicStream {
426
506
  * packet is sent again from it.
427
507
  */
428
508
  sendBuf: u8[]
509
+ /** The bitmap beside `sendBuf`: a bit per buffer byte, set once acknowledged and not yet released. */
429
510
  sendAcked: u8[]
430
511
  /** Every byte below this was acknowledged, and its room released. */
431
512
  sendBase: i64 = 0
@@ -433,38 +514,78 @@ export class QuicStream {
433
514
  sendNext: i64 = 0
434
515
  /** One past the last byte written. */
435
516
  sendEnd: i64 = 0
436
- /** The credit the peer gave: no byte at or past this offset may be sent. */
517
+ /**
518
+ * The credit the peer gave: no byte at or past this offset may be sent.
519
+ * @internal Read by quic.ts, and in quic-stream.ts outside the class.
520
+ */
437
521
  sendLimit: i64 = 0
438
522
  /** Bytes a lost packet carried, `[resendLow, resendHigh)`, or -1; `resendFin` when the FIN was among them. */
439
523
  resendLow: i64 = -1
524
+ /** One past the last byte to send again, or -1 when nothing is owed; see `resendLow`. */
440
525
  resendHigh: i64 = -1
441
- /** The error code of this side's RESET_STREAM, and its final size. */
526
+ /**
527
+ * The error code of this side's RESET_STREAM, and its final size.
528
+ * @internal Read in quic-stream.ts outside the class.
529
+ */
442
530
  resetSentCode: i64 = 0
531
+ /** @internal Read in quic-stream.ts outside the class. */
443
532
  resetFinal: i64 = 0
444
533
  /** The peer's STOP_SENDING error code, or -1. */
445
534
  stopCode: i64 = -1
446
- /** The limit the last STREAM_DATA_BLOCKED named, or -1. */
535
+ /**
536
+ * The limit the last STREAM_DATA_BLOCKED named, or -1.
537
+ * @internal Read in quic-stream.ts outside the class.
538
+ */
447
539
  blockedAt: i64 = -1
448
- /** The bytes of each buffer once borrowed, which the offsets wrap at whether or not one is. */
540
+ /**
541
+ * The bytes of each buffer once borrowed, which the offsets wrap at whether or not one is.
542
+ * @internal Read in quic-stream.ts outside the class.
543
+ */
449
544
  size: i32 = 0
545
+ /** The send side's state (RFC 9000 §3.1), a `QUIC_SEND_*` constant. */
450
546
  sendState: i32 = 0
547
+ /** The receive side's state (RFC 9000 §3.2), a `QUIC_RECV_*` constant. */
451
548
  recvState: i32 = 0
452
- /** Whether the application wrote the FIN, a frame carried it, and the peer acknowledged it. */
549
+ /**
550
+ * Whether the application wrote the FIN, a frame carried it, and the peer acknowledged it.
551
+ * @internal Read by quic.ts, and in quic-stream.ts outside the class.
552
+ */
453
553
  finQueued: boolean = false
554
+ /** @internal Read in quic-stream.ts outside the class. */
454
555
  finSent: boolean = false
556
+ /** @internal Read in quic-stream.ts outside the class. */
455
557
  finAcked: boolean = false
558
+ /** @internal Read in quic-stream.ts outside the class. */
456
559
  resendFin: boolean = false
457
- /** What this stream owes the peer: MAX_STREAM_DATA, STREAM_DATA_BLOCKED, RESET_STREAM, STOP_SENDING. */
560
+ /**
561
+ * What this stream owes the peer: MAX_STREAM_DATA, STREAM_DATA_BLOCKED, RESET_STREAM, STOP_SENDING.
562
+ * @internal Read in quic-stream.ts outside the class.
563
+ */
458
564
  maxDataOwed: boolean = false
565
+ /** @internal Read in quic-stream.ts outside the class. */
459
566
  blockedOwed: boolean = false
567
+ /** @internal Read in quic-stream.ts outside the class. */
460
568
  resetOwed: boolean = false
569
+ /** @internal Read in quic-stream.ts outside the class. */
461
570
  stopOwed: boolean = false
462
- /** Whether a write was cut short for room, so freed room is an event. */
571
+ /**
572
+ * Whether a write was cut short for room, so freed room is an event.
573
+ * @internal Read in quic-stream.ts outside the class.
574
+ */
463
575
  wantsWrite: boolean = false
464
- /** Whether the slot is in the event ring. */
576
+ /**
577
+ * Whether the slot is in the event ring.
578
+ * @internal Read in quic-stream.ts outside the class.
579
+ */
465
580
  queued: boolean = false
466
581
 
467
- /** A slot for buffers of `bufferSize` bytes, holding none: each side starts as `none`, the table's empty array. */
582
+ /**
583
+ * A slot for buffers of `bufferSize` bytes, holding none: each side starts as `none`, the table's
584
+ * empty array.
585
+ *
586
+ * @param bufferSize The bytes of the buffer each side borrows later.
587
+ * @param none The table's empty array, which every side holds until it borrows; shared, never written.
588
+ */
468
589
  constructor(bufferSize: i32, none: u8[]) {
469
590
  this.size = bufferSize
470
591
  this.recvBuf = none
@@ -473,22 +594,37 @@ export class QuicStream {
473
594
  this.sendAcked = none
474
595
  }
475
596
 
476
- /** The buffer index of stream offset `offset`. */
597
+ /**
598
+ * The buffer index of stream offset `offset`.
599
+ *
600
+ * @param offset A byte offset in the stream, 0 or more.
601
+ * @returns `offset` modulo the buffer size, or 0 when the size is 0.
602
+ */
477
603
  at(offset: i64): i32 {
478
604
  return this.size > 0 ? toI32(offset % toI64(this.size)) : 0
479
605
  }
480
606
 
481
- /** How many bytes can be read now. */
607
+ /**
608
+ * How many bytes can be read now.
609
+ * @internal Read by http3.ts and quic.ts, and in quic-stream.ts outside the class.
610
+ */
482
611
  readable(): i64 {
483
612
  return this.recvContiguous - this.recvRead
484
613
  }
485
614
 
486
- /** How many more bytes the send buffer takes. */
615
+ /**
616
+ * How many more bytes the send buffer takes.
617
+ *
618
+ * @returns The buffer size less the bytes written and not yet acknowledged.
619
+ */
487
620
  room(): i64 {
488
621
  return toI64(this.size) - (this.sendEnd - this.sendBase)
489
622
  }
490
623
 
491
- /** Whether the sending side has a frame to go: lost bytes, new bytes, or a FIN not yet sent. */
624
+ /**
625
+ * Whether the sending side has a frame to go: lost bytes, new bytes, or a FIN not yet sent.
626
+ * @internal Read in quic-stream.ts outside the class.
627
+ */
492
628
  wantsToSend(): boolean {
493
629
  if (this.sendState === QUIC_SEND_NONE || this.sendState >= QUIC_SEND_DATA_RECVD) {
494
630
  return false
@@ -501,7 +637,10 @@ export class QuicStream {
501
637
  )
502
638
  }
503
639
 
504
- /** Whether both sides are done, so the slot can be freed (§3.3). */
640
+ /**
641
+ * Whether both sides are done, so the slot can be freed (§3.3).
642
+ * @internal Read in quic-stream.ts outside the class.
643
+ */
505
644
  finished(): boolean {
506
645
  const sendDone: boolean =
507
646
  this.sendState === QUIC_SEND_NONE ||
@@ -518,6 +657,7 @@ export class QuicStream {
518
657
  * Empties the slot for stream `id`, with `recvLimit` and `sendLimit` its
519
658
  * first credits. It holds no buffers here, and a borrowed bitmap comes
520
659
  * back from its pool already zeroed.
660
+ * @internal Read in quic-stream.ts outside the class.
521
661
  */
522
662
  open(id: i64, recvLimit: i64, sendLimit: i64): void {
523
663
  this.id = id
@@ -554,7 +694,10 @@ export class QuicStream {
554
694
  this.wantsWrite = false
555
695
  }
556
696
 
557
- /** Merges `[offset, offset + length)` into the range to send again, with the FIN when it was lost too. */
697
+ /**
698
+ * Merges `[offset, offset + length)` into the range to send again, with the FIN when it was lost too.
699
+ * @internal Read in quic-stream.ts outside the class.
700
+ */
558
701
  resend(offset: i64, length: i32, fin: boolean): void {
559
702
  if (this.sendState === QUIC_SEND_NONE || this.sendState >= QUIC_SEND_DATA_RECVD) {
560
703
  return
@@ -587,17 +730,18 @@ export class QuicStream {
587
730
  * counters are readable for a test or a log.
588
731
  */
589
732
  export class QuicStreams {
733
+ /** The stream slots, a fixed count set at construction; a free one has `id` -1. */
590
734
  slots: QuicStream[]
591
735
  /** Open addressing by ID: `index[h]` is a slot plus one, 0 for empty. */
592
- index: i32[]
736
+ private index: i32[]
593
737
  /** Slots with news for the application, `eventCount` of them from `eventHead`. */
594
- events: i32[]
738
+ private events: i32[]
595
739
  /**
596
740
  * The free slots, a stack of `freeCount`, so a stream opens without a
597
741
  * scan: a frame naming a high stream ID opens every lower one (§3.2), and
598
742
  * each of those costs one pop, not a pass over the table.
599
743
  */
600
- free: i32[]
744
+ private free: i32[]
601
745
  /**
602
746
  * Where the streams borrow their buffers: the table's own pools, or the
603
747
  * set a server shares across its connections (`share`). `null` when the
@@ -605,78 +749,102 @@ export class QuicStreams {
605
749
  */
606
750
  pools: QuicStreamPools | null = null
607
751
  /** The empty array a side holds while it has no buffer borrowed. */
608
- none: u8[]
752
+ private none: u8[]
609
753
  /** How many receive sides hold a buffer now: the rest are what a packet could start. */
610
- recvLent: i32 = 0
611
- /** The configured limits on the client's streams at once and this side's own. */
754
+ private recvLent: i32 = 0
755
+ /**
756
+ * The configured limits on the client's streams at once and this side's own.
757
+ * @internal Read by http3.ts.
758
+ */
612
759
  maxBidi: i64 = 0
760
+ /** @internal Read by http3.ts. */
613
761
  maxUni: i64 = 0
762
+ /** @internal Read by http3.ts. */
614
763
  localStreams: i64 = 0
615
764
  /** How many of each type the client opened, the limit advertised, and how many of them finished. */
616
765
  peerBidiOpened: i64 = 0
766
+ /** How many unidirectional streams the client opened. */
617
767
  peerUniOpened: i64 = 0
768
+ /** The bidirectional stream limit this side last advertised to the client (MAX_STREAMS). */
618
769
  peerBidiLimit: i64 = 0
770
+ /** The unidirectional stream limit this side last advertised to the client (MAX_STREAMS). */
619
771
  peerUniLimit: i64 = 0
772
+ /** How many of the client's bidirectional streams finished, which is what lets the limit rise. */
620
773
  peerBidiClosed: i64 = 0
774
+ /** How many of the client's unidirectional streams finished, which is what lets the limit rise. */
621
775
  peerUniClosed: i64 = 0
622
776
  /** How many of each type this side opened, the client's limits, and how many of this side's are open. */
623
- localBidiOpened: i64 = 0
624
- localUniOpened: i64 = 0
625
- localBidiLimit: i64 = 0
626
- localUniLimit: i64 = 0
627
- localOpen: i64 = 0
777
+ private localBidiOpened: i64 = 0
778
+ private localUniOpened: i64 = 0
779
+ private localBidiLimit: i64 = 0
780
+ private localUniLimit: i64 = 0
781
+ private localOpen: i64 = 0
628
782
  /** The peer's credit for each stream type as it opens (its transport parameters). */
629
- peerBidiLocalCredit: i64 = 0
630
- peerBidiRemoteCredit: i64 = 0
631
- peerUniCredit: i64 = 0
783
+ private peerBidiLocalCredit: i64 = 0
784
+ private peerBidiRemoteCredit: i64 = 0
785
+ private peerUniCredit: i64 = 0
632
786
  /** The connection's receive credit: what was advertised, its window, what arrived, and what was read. */
633
787
  recvMaxData: i64 = 0
788
+ /** The connection's receive window in bytes: how far past what was read the credit is raised. */
634
789
  windowData: i64 = 0
790
+ /** Bytes the connection received, counted at each stream's highest offset, against `recvMaxData`. */
635
791
  recvTotal: i64 = 0
636
- recvConsumed: i64 = 0
792
+ private recvConsumed: i64 = 0
637
793
  /** The connection's send credit from the peer, what was sent against it, and what is written but not sent. */
638
794
  sendMaxData: i64 = 0
639
- sendTotal: i64 = 0
795
+ private sendTotal: i64 = 0
796
+ /** Bytes the application wrote to every stream, sent or not; a write may not take it past `sendMaxData`. */
640
797
  writtenTotal: i64 = 0
641
798
  /** The transport error `locate` answered -2 for. */
642
- locateError: i64 = 0
799
+ private locateError: i64 = 0
643
800
  /** The limit the last DATA_BLOCKED named, or -1. */
644
- dataBlockedAt: i64 = -1
801
+ private dataBlockedAt: i64 = -1
645
802
  /** The limit each STREAMS_BLOCKED named, or -1. */
646
- streamsBlockedBidiAt: i64 = -1
647
- streamsBlockedUniAt: i64 = -1
803
+ private streamsBlockedBidiAt: i64 = -1
804
+ private streamsBlockedUniAt: i64 = -1
648
805
  /** What the last `putNextChunk` or `putNextControl` wrote, for the packet's record. */
649
806
  lastId: i64 = 0
807
+ /** The stream offset of the last chunk `putNextChunk` wrote. */
650
808
  lastOffset: i64 = 0
809
+ /** The stream the last control frame `putNextControl` wrote names, or 0 for a connection-level one. */
651
810
  lastValue: i64 = 0
652
811
  /** The bytes of each stream's buffer, each way. */
653
812
  bufferSize: i32 = 0
813
+ /** The bytes of the last chunk `putNextChunk` wrote. */
654
814
  lastLength: i32 = 0
815
+ /** Which control frame `putNextControl` last wrote, a `QUIC_STREAM_CONTROL_*` constant. */
655
816
  lastKind: i32 = 0
656
- eventHead: i32 = 0
657
- eventCount: i32 = 0
817
+ private eventHead: i32 = 0
818
+ private eventCount: i32 = 0
658
819
  /** How many slots `free` holds. */
659
820
  freeCount: i32 = 0
660
821
  /** Where the next packet's search for a stream to send starts, and how many it has looked at. */
661
- cursor: i32 = 0
662
- scanned: i32 = 0
822
+ private cursor: i32 = 0
823
+ private scanned: i32 = 0
663
824
  /** Whether the last `putNextChunk` carried the FIN, for the packet's record. */
664
825
  lastFin: boolean = false
665
826
  /** Whether some stream may owe a control frame, so `putNextControl` looks at the slots at all. */
666
- streamOwes: boolean = false
827
+ private streamOwes: boolean = false
667
828
  /** What the connection owes: MAX_DATA, DATA_BLOCKED, MAX_STREAMS and STREAMS_BLOCKED of each type. */
668
- maxDataOwed: boolean = false
669
- dataBlockedOwed: boolean = false
670
- maxStreamsBidiOwed: boolean = false
671
- maxStreamsUniOwed: boolean = false
672
- streamsBlockedBidiOwed: boolean = false
673
- streamsBlockedUniOwed: boolean = false
829
+ private maxDataOwed: boolean = false
830
+ private dataBlockedOwed: boolean = false
831
+ private maxStreamsBidiOwed: boolean = false
832
+ private maxStreamsUniOwed: boolean = false
833
+ private streamsBlockedBidiOwed: boolean = false
834
+ private streamsBlockedUniOwed: boolean = false
674
835
 
675
836
  /**
676
837
  * The table for a connection that lets the client open `maxBidi`
677
838
  * bidirectional and `maxUni` unidirectional streams at once and opens
678
839
  * `localStreams` of its own, each stream with `bufferSize` bytes each way,
679
840
  * and a connection receive window of `windowData` bytes.
841
+ *
842
+ * @param maxBidi The client-opened bidirectional streams that may be open at once.
843
+ * @param maxUni The client-opened unidirectional streams that may be open at once.
844
+ * @param localStreams The streams this side may have open at once, of both types.
845
+ * @param bufferSize The bytes of each stream's buffer, each way: the most a stream's credit runs ahead
846
+ * of the application's reads.
847
+ * @param windowData The connection's receive window in bytes, the MAX_DATA step.
680
848
  */
681
849
  constructor(maxBidi: i64, maxUni: i64, localStreams: i64, bufferSize: i32, windowData: i64) {
682
850
  const count: i32 = toI32(maxBidi + maxUni + localStreams)
@@ -705,6 +873,8 @@ export class QuicStreams {
705
873
  * table's own: how a server's connections share one set. Call it before
706
874
  * any stream has borrowed, or after `reset`. Panics when the pools' buffers
707
875
  * are not `bufferSize` bytes.
876
+ *
877
+ * @param pools The pools to borrow from, kept by reference; usually shared with other connections.
708
878
  */
709
879
  share(pools: QuicStreamPools): void {
710
880
  if (pools.data.size !== this.bufferSize) {
@@ -724,6 +894,7 @@ export class QuicStreams {
724
894
  * table had when each slot owned its buffers: how many it holds turns on
725
895
  * when the peer acknowledges, so a lazily grown private pool would go on
726
896
  * allocating after any warm-up. A server's shared pools grow lazily.
897
+ * @internal Read by quic.ts.
727
898
  */
728
899
  ownPools(): QuicStreamPools | null {
729
900
  const pools: QuicStreamPools | null = this.pools
@@ -755,6 +926,8 @@ export class QuicStreams {
755
926
  * `idle` already is. Once warm the buffers a packet gives back refill the
756
927
  * reserve for the next one, so this costs a handful of comparisons, not a
757
928
  * take per side.
929
+ *
930
+ * @param bytes The packet's length in bytes.
758
931
  */
759
932
  prime(bytes: i32): void {
760
933
  const pools: QuicStreamPools | null = this.ownPools()
@@ -786,7 +959,7 @@ export class QuicStreams {
786
959
  * when the reserve is empty: a packet handled without `prime`, where a
787
960
  * take would allocate inside the scope.
788
961
  */
789
- borrowRecv(stream: QuicStream): void {
962
+ private borrowRecv(stream: QuicStream): void {
790
963
  const pools: QuicStreamPools | null = this.pools
791
964
  if (stream.recvBuf.length > 0 || this.bufferSize <= 0) {
792
965
  return
@@ -803,7 +976,7 @@ export class QuicStreams {
803
976
  * every byte a frame reached: below `recvHighest`, which past a whole
804
977
  * buffer is all of it. Nothing when it holds none.
805
978
  */
806
- giveRecv(stream: QuicStream): void {
979
+ private giveRecv(stream: QuicStream): void {
807
980
  const pools: QuicStreamPools | null = this.pools
808
981
  if (pools !== null && stream.recvBuf.length > 0) {
809
982
  pools.keep(
@@ -818,7 +991,7 @@ export class QuicStreams {
818
991
  }
819
992
 
820
993
  /** Borrows `stream`'s send buffer and bitmap from the pools, unless it holds them already or the streams have none. */
821
- borrowSend(stream: QuicStream): void {
994
+ private borrowSend(stream: QuicStream): void {
822
995
  const pools: QuicStreamPools | null = this.ownPools()
823
996
  if (pools !== null && stream.sendBuf.length === 0) {
824
997
  pools.lendSend(stream)
@@ -832,7 +1005,7 @@ export class QuicStreams {
832
1005
  * byte acknowledged, or the side reset — and before a reset moves
833
1006
  * `sendEnd` back.
834
1007
  */
835
- giveSend(stream: QuicStream): void {
1008
+ private giveSend(stream: QuicStream): void {
836
1009
  const pools: QuicStreamPools | null = this.pools
837
1010
  if (pools !== null && stream.sendBuf.length > 0) {
838
1011
  pools.keep(stream.sendBuf, stream.sendAcked, toI32(quicStreamMin(stream.sendEnd, toI64(stream.size))))
@@ -899,6 +1072,16 @@ export class QuicStreams {
899
1072
  * Takes the peer's transport parameters (RFC 9000 §18.2): its limits on
900
1073
  * the streams this side opens, the credit each kind of stream starts with,
901
1074
  * and the connection's.
1075
+ *
1076
+ * @param maxData `initial_max_data`: the connection's send credit, in bytes.
1077
+ * @param bidiLocal `initial_max_stream_data_bidi_local`: the bytes this side may send on a
1078
+ * client-opened bidirectional stream (local to the peer).
1079
+ * @param bidiRemote `initial_max_stream_data_bidi_remote`: the bytes this side may send on a
1080
+ * bidirectional stream it opens.
1081
+ * @param uni `initial_max_stream_data_uni`: the bytes this side may send on a unidirectional stream it
1082
+ * opens.
1083
+ * @param maxStreamsBidi `initial_max_streams_bidi`: how many bidirectional streams this side may open.
1084
+ * @param maxStreamsUni `initial_max_streams_uni`: how many unidirectional streams this side may open.
902
1085
  */
903
1086
  setPeerLimits(
904
1087
  maxData: i64,
@@ -917,12 +1100,17 @@ export class QuicStreams {
917
1100
  }
918
1101
 
919
1102
  /** The hash bucket `id` starts its probe at. */
920
- bucket(id: i64): i32 {
1103
+ private bucket(id: i64): i32 {
921
1104
  const mixed: i64 = (id >> 2) ^ (id << 3) ^ (id >> 13)
922
1105
  return toI32(mixed & toI64(toI32(this.index.length) - 1))
923
1106
  }
924
1107
 
925
- /** The slot of stream `id`, or -1 when it is not open. */
1108
+ /**
1109
+ * The slot of stream `id`, or -1 when it is not open.
1110
+ *
1111
+ * @param id A stream ID.
1112
+ * @returns The slot's index in the table, or -1 when no open stream has that ID.
1113
+ */
926
1114
  slotOf(id: i64): i32 {
927
1115
  const size: i32 = toI32(this.index.length)
928
1116
  let h: i32 = this.bucket(id)
@@ -942,14 +1130,19 @@ export class QuicStreams {
942
1130
  return -1
943
1131
  }
944
1132
 
945
- /** The stream `id`, or `null` when it is not open. */
1133
+ /**
1134
+ * The stream `id`, or `null` when it is not open.
1135
+ *
1136
+ * @param id A stream ID.
1137
+ * @returns The stream's slot, owned by the table and reused once the stream is done; or `null`.
1138
+ */
946
1139
  find(id: i64): QuicStream | null {
947
1140
  const k: i32 = this.slotOf(id)
948
1141
  return k >= 0 && k < toI32(this.slots.length) ? this.slots[k] : null
949
1142
  }
950
1143
 
951
1144
  /** Puts slot `k` in the index under its stream's ID. */
952
- indexInsert(k: i32, id: i64): void {
1145
+ private indexInsert(k: i32, id: i64): void {
953
1146
  const size: i32 = toI32(this.index.length)
954
1147
  let h: i32 = this.bucket(id)
955
1148
  for (let probe: i32 = 0; probe < size; probe += 1) {
@@ -965,7 +1158,7 @@ export class QuicStreams {
965
1158
  }
966
1159
 
967
1160
  /** Takes slot `k`, holding `id`, out of the index, moving later entries of its probe run back so none is lost. */
968
- indexRemove(k: i32, id: i64): void {
1161
+ private indexRemove(k: i32, id: i64): void {
969
1162
  const size: i32 = toI32(this.index.length)
970
1163
  const mask: i32 = size - 1
971
1164
  let h: i32 = this.bucket(id)
@@ -1010,7 +1203,7 @@ export class QuicStreams {
1010
1203
  }
1011
1204
 
1012
1205
  /** Puts slot `k` on the event ring, unless it is there already. */
1013
- queue(k: i32): void {
1206
+ private queue(k: i32): void {
1014
1207
  const size: i32 = toI32(this.events.length)
1015
1208
  if (k < 0 || k >= toI32(this.slots.length) || this.slots[k].queued || this.eventCount >= size) {
1016
1209
  return
@@ -1029,6 +1222,7 @@ export class QuicStreams {
1029
1222
  * or room to write again after a short write — or -1 when there is none.
1030
1223
  * Each stream comes up once however much happened to it; read and write it
1031
1224
  * until it has nothing more to say.
1225
+ * @internal Read by quic.ts.
1032
1226
  */
1033
1227
  nextEvent(): i64 {
1034
1228
  const size: i32 = toI32(this.events.length)
@@ -1049,7 +1243,7 @@ export class QuicStreams {
1049
1243
  }
1050
1244
 
1051
1245
  /** Puts slot `k` on the free stack. */
1052
- pushFree(k: i32): void {
1246
+ private pushFree(k: i32): void {
1053
1247
  const at: i32 = this.freeCount
1054
1248
  if (at >= 0 && at < toI32(this.free.length)) {
1055
1249
  this.free[at] = k
@@ -1058,7 +1252,7 @@ export class QuicStreams {
1058
1252
  }
1059
1253
 
1060
1254
  /** Takes a free slot off the stack, or -1 when every slot is in use. */
1061
- takeFree(): i32 {
1255
+ private takeFree(): i32 {
1062
1256
  const at: i32 = this.freeCount - 1
1063
1257
  if (at < 0 || at >= toI32(this.free.length)) {
1064
1258
  return -1
@@ -1068,7 +1262,7 @@ export class QuicStreams {
1068
1262
  }
1069
1263
 
1070
1264
  /** Opens `id` in a free slot with its first credits; answers the slot, or -1 when none is free. */
1071
- openSlot(id: i64): i32 {
1265
+ private openSlot(id: i64): i32 {
1072
1266
  const k: i32 = this.takeFree()
1073
1267
  if (k < 0 || k >= toI32(this.slots.length)) {
1074
1268
  return -1
@@ -1093,7 +1287,7 @@ export class QuicStreams {
1093
1287
  * the peer opened counts toward raising its limit: MAX_STREAMS is owed
1094
1288
  * once half the configured number have finished since the last one.
1095
1289
  */
1096
- release(k: i32): void {
1290
+ private release(k: i32): void {
1097
1291
  if (k < 0 || k >= toI32(this.slots.length)) {
1098
1292
  return
1099
1293
  }
@@ -1137,7 +1331,7 @@ export class QuicStreams {
1137
1331
  * STREAM_STATE_ERROR for one of this side's that it has not opened or a
1138
1332
  * unidirectional stream without the half the frame is about (§19.4-§19.13).
1139
1333
  */
1140
- locate(id: i64, receiving: boolean): i32 {
1334
+ private locate(id: i64, receiving: boolean): i32 {
1141
1335
  if (quicStreamIsUni(id) && quicStreamIsLocal(id) === receiving) {
1142
1336
  this.locateError = QUIC_ERROR_STREAM_STATE
1143
1337
  return -2
@@ -1185,7 +1379,7 @@ export class QuicStreams {
1185
1379
  * side gave, for the stream and for the connection (§4.1): past either it
1186
1380
  * is FLOW_CONTROL_ERROR. Answers 0 or that.
1187
1381
  */
1188
- creditReceived(stream: QuicStream, end: i64): i64 {
1382
+ private creditReceived(stream: QuicStream, end: i64): i64 {
1189
1383
  if (end > stream.recvLimit) {
1190
1384
  return QUIC_ERROR_FLOW_CONTROL
1191
1385
  }
@@ -1204,6 +1398,15 @@ export class QuicStreams {
1204
1398
  * `offset`, and the FIN when `fin`. Answers 0, or the transport error that
1205
1399
  * closes the connection: the stream's ID (see `locate`), a stream only this
1206
1400
  * side sends on, the final size, or the credit.
1401
+ *
1402
+ * @param id The frame's stream ID.
1403
+ * @param offset The stream offset of the frame's first byte.
1404
+ * @param buf The packet holding the frame's data; copied from, not kept.
1405
+ * @param from The index in `buf` of the first byte.
1406
+ * @param length How many bytes the frame carries.
1407
+ * @param fin Whether the frame carries the FIN, fixing the final size at `offset + length`.
1408
+ * @returns `QUIC_ERROR_NO_ERROR` (0), or `QUIC_ERROR_STREAM_LIMIT`, `QUIC_ERROR_STREAM_STATE`,
1409
+ * `QUIC_ERROR_FINAL_SIZE` or `QUIC_ERROR_FLOW_CONTROL` to close the connection with.
1207
1410
  */
1208
1411
  onStream(id: i64, offset: i64, buf: u8[], from: i32, length: i32, fin: boolean): i64 {
1209
1412
  const k: i32 = this.locate(id, true)
@@ -1264,7 +1467,16 @@ export class QuicStreams {
1264
1467
  return QUIC_ERROR_NO_ERROR
1265
1468
  }
1266
1469
 
1267
- /** RESET_STREAM (§19.4): the peer abandons the stream at `finalSize` with `code`. Answers 0 or the transport error. */
1470
+ /**
1471
+ * RESET_STREAM (§19.4): the peer abandons the stream at `finalSize` with `code`. Answers 0 or the
1472
+ * transport error.
1473
+ *
1474
+ * @param id The frame's stream ID.
1475
+ * @param code The application's error code, kept for the reader.
1476
+ * @param finalSize The stream's final size in bytes.
1477
+ * @returns `QUIC_ERROR_NO_ERROR` (0), or `QUIC_ERROR_STREAM_LIMIT`, `QUIC_ERROR_STREAM_STATE`,
1478
+ * `QUIC_ERROR_FINAL_SIZE` or `QUIC_ERROR_FLOW_CONTROL` to close the connection with.
1479
+ */
1268
1480
  onReset(id: i64, code: i64, finalSize: i64): i64 {
1269
1481
  const k: i32 = this.locate(id, true)
1270
1482
  if (k === -2) {
@@ -1303,6 +1515,11 @@ export class QuicStreams {
1303
1515
  * STOP_SENDING (§19.5): the peer wants no more of the stream, which this
1304
1516
  * side answers with RESET_STREAM carrying the same code (§3.5). Answers 0
1305
1517
  * or the transport error.
1518
+ *
1519
+ * @param id The frame's stream ID.
1520
+ * @param code The application's error code, sent back in the RESET_STREAM.
1521
+ * @returns `QUIC_ERROR_NO_ERROR` (0), or `QUIC_ERROR_STREAM_LIMIT` or `QUIC_ERROR_STREAM_STATE` to
1522
+ * close the connection with.
1306
1523
  */
1307
1524
  onStopSending(id: i64, code: i64): i64 {
1308
1525
  const k: i32 = this.locate(id, false)
@@ -1321,7 +1538,14 @@ export class QuicStreams {
1321
1538
  return QUIC_ERROR_NO_ERROR
1322
1539
  }
1323
1540
 
1324
- /** MAX_STREAM_DATA (§19.10): more credit for the stream. Answers 0 or the transport error. */
1541
+ /**
1542
+ * MAX_STREAM_DATA (§19.10): more credit for the stream. Answers 0 or the transport error.
1543
+ *
1544
+ * @param id The frame's stream ID.
1545
+ * @param limit The new send limit, a stream offset in bytes; a lower one than held changes nothing.
1546
+ * @returns `QUIC_ERROR_NO_ERROR` (0), or `QUIC_ERROR_STREAM_LIMIT` or `QUIC_ERROR_STREAM_STATE` to
1547
+ * close the connection with.
1548
+ */
1325
1549
  onMaxStreamData(id: i64, limit: i64): i64 {
1326
1550
  const k: i32 = this.locate(id, false)
1327
1551
  if (k === -2) {
@@ -1340,13 +1564,19 @@ export class QuicStreams {
1340
1564
  return QUIC_ERROR_NO_ERROR
1341
1565
  }
1342
1566
 
1343
- /** STREAM_DATA_BLOCKED (§19.13): only the ID is checked; credit follows reading, not asking. */
1567
+ /**
1568
+ * STREAM_DATA_BLOCKED (§19.13): only the ID is checked; credit follows reading, not asking.
1569
+ * @internal Read by quic.ts.
1570
+ */
1344
1571
  onStreamDataBlocked(id: i64): i64 {
1345
1572
  const k: i32 = this.locate(id, true)
1346
1573
  return k === -2 ? this.locateError : QUIC_ERROR_NO_ERROR
1347
1574
  }
1348
1575
 
1349
- /** MAX_DATA (§19.9): more credit for the connection. */
1576
+ /**
1577
+ * MAX_DATA (§19.9): more credit for the connection.
1578
+ * @internal Read by quic.ts.
1579
+ */
1350
1580
  onMaxData(limit: i64): void {
1351
1581
  if (limit > this.sendMaxData) {
1352
1582
  this.sendMaxData = limit
@@ -1354,7 +1584,10 @@ export class QuicStreams {
1354
1584
  }
1355
1585
  }
1356
1586
 
1357
- /** MAX_STREAMS (§19.11): the peer lets this side open more streams of a type. */
1587
+ /**
1588
+ * MAX_STREAMS (§19.11): the peer lets this side open more streams of a type.
1589
+ * @internal Read by quic.ts.
1590
+ */
1358
1591
  onMaxStreams(uni: boolean, limit: i64): void {
1359
1592
  if (uni && limit > this.localUniLimit) {
1360
1593
  this.localUniLimit = limit
@@ -1366,7 +1599,7 @@ export class QuicStreams {
1366
1599
  }
1367
1600
 
1368
1601
  /** Counts `bytes` read (or abandoned by a reset) toward the connection's credit, raising it once half the window is used. */
1369
- consume(bytes: i64): void {
1602
+ private consume(bytes: i64): void {
1370
1603
  this.recvConsumed = this.recvConsumed + bytes
1371
1604
  if (
1372
1605
  this.recvConsumed + this.windowData - this.recvMaxData >= this.windowData / 2 &&
@@ -1382,6 +1615,7 @@ export class QuicStreams {
1382
1615
  * unidirectional. Answers its ID, or `QUIC_STREAM_ERR_LIMIT` when the
1383
1616
  * peer's limit is reached (STREAMS_BLOCKED is then owed) or every slot for
1384
1617
  * this side's streams is taken.
1618
+ * @internal Read by quic.ts.
1385
1619
  */
1386
1620
  open(bidi: boolean): i64 {
1387
1621
  const opened: i64 = bidi ? this.localBidiOpened : this.localUniOpened
@@ -1415,6 +1649,16 @@ export class QuicStreams {
1415
1649
  * FIN when `fin` and every byte fit. Answers how many bytes it took — fewer
1416
1650
  * than `length` when the send buffer is full, and the stream's event fires
1417
1651
  * once acknowledgements free room — or a `QUIC_STREAM_ERR_*`.
1652
+ *
1653
+ * @param id The stream ID.
1654
+ * @param buf The bytes to send; copied, not kept.
1655
+ * @param from The index in `buf` of the first byte.
1656
+ * @param length How many bytes to write, 0 or more; a `from + length` past `buf` answers `QUIC_STREAM_ERR_UNKNOWN`.
1657
+ * @param fin Whether this write ends the stream; queued only when all `length` bytes were taken.
1658
+ * @returns The bytes taken, 0 to `length`; or `QUIC_STREAM_ERR_UNKNOWN` (no such stream, or a window
1659
+ * outside `buf`), `QUIC_STREAM_ERR_DIRECTION` (a stream this side only receives on),
1660
+ * `QUIC_STREAM_ERR_STOPPED` (the peer sent STOP_SENDING) or `QUIC_STREAM_ERR_FINISHED` (the FIN is
1661
+ * queued or the side was reset).
1418
1662
  */
1419
1663
  write(id: i64, buf: u8[], from: i32, length: i32, fin: boolean): i32 {
1420
1664
  const k: i32 = this.slotOf(id)
@@ -1470,6 +1714,14 @@ export class QuicStreams {
1470
1714
  * `QUIC_STREAM_END` or `QUIC_STREAM_ERR_RESET`, so read until one does.
1471
1715
  * After `QuicConnection.release`, a stream that still owed bytes answers
1472
1716
  * `QUIC_STREAM_END` and copies nothing: its buffer went back to the pool.
1717
+ *
1718
+ * @param id The stream ID.
1719
+ * @param buf Where the bytes go.
1720
+ * @param at The index in `buf` of the first byte written.
1721
+ * @param length The most bytes to read, 0 or more; an `at + length` past `buf` answers `QUIC_STREAM_ERR_UNKNOWN`.
1722
+ * @returns The bytes read, 0 or more; `QUIC_STREAM_END`; `QUIC_STREAM_ERR_RESET`;
1723
+ * `QUIC_STREAM_ERR_UNKNOWN` (no such stream, or a window outside `buf`); or
1724
+ * `QUIC_STREAM_ERR_DIRECTION` (a stream this side only sends on).
1473
1725
  */
1474
1726
  read(id: i64, buf: u8[], at: i32, length: i32): i32 {
1475
1727
  const k: i32 = this.slotOf(id)
@@ -1533,7 +1785,7 @@ export class QuicStreams {
1533
1785
  }
1534
1786
 
1535
1787
  /** Abandons the sending side of slot `k` with `code` (§3.1, §19.4): what was not sent is dropped and RESET_STREAM is owed. */
1536
- resetSlot(k: i32, code: i64): void {
1788
+ private resetSlot(k: i32, code: i64): void {
1537
1789
  if (k < 0 || k >= toI32(this.slots.length)) {
1538
1790
  return
1539
1791
  }
@@ -1557,7 +1809,15 @@ export class QuicStreams {
1557
1809
  stream.blockedOwed = false
1558
1810
  }
1559
1811
 
1560
- /** Resets stream `id`'s sending side with `code`. Answers `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`. */
1812
+ /**
1813
+ * Resets stream `id`'s sending side with `code`. Answers `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`.
1814
+ *
1815
+ * @param id The stream ID.
1816
+ * @param code The application's error code for the RESET_STREAM.
1817
+ * @returns `QUIC_STREAM_OK`; `QUIC_STREAM_ERR_UNKNOWN` (no such stream); `QUIC_STREAM_ERR_DIRECTION`
1818
+ * (a stream this side only receives on); or `QUIC_STREAM_ERR_FINISHED` (every byte was already
1819
+ * acknowledged, or the side was already reset).
1820
+ */
1561
1821
  resetStream(id: i64, code: i64): i32 {
1562
1822
  const k: i32 = this.slotOf(id)
1563
1823
  if (k < 0 || k >= toI32(this.slots.length)) {
@@ -1574,7 +1834,16 @@ export class QuicStreams {
1574
1834
  return QUIC_STREAM_OK
1575
1835
  }
1576
1836
 
1577
- /** Asks the peer to stop sending on stream `id`, with `code` (STOP_SENDING, §3.5). Answers `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`. */
1837
+ /**
1838
+ * Asks the peer to stop sending on stream `id`, with `code` (STOP_SENDING, §3.5). Answers
1839
+ * `QUIC_STREAM_OK` or a `QUIC_STREAM_ERR_*`.
1840
+ *
1841
+ * @param id The stream ID.
1842
+ * @param code The application's error code; only the first call's is sent.
1843
+ * @returns `QUIC_STREAM_OK`; `QUIC_STREAM_ERR_UNKNOWN` (no such stream); `QUIC_STREAM_ERR_DIRECTION`
1844
+ * (a stream this side only sends on); or `QUIC_STREAM_ERR_FINISHED` (every byte already arrived,
1845
+ * or the stream was reset).
1846
+ */
1578
1847
  stopSending(id: i64, code: i64): i32 {
1579
1848
  const k: i32 = this.slotOf(id)
1580
1849
  if (k < 0 || k >= toI32(this.slots.length)) {
@@ -1607,6 +1876,11 @@ export class QuicStreams {
1607
1876
  * the offset past it, with `lastId`, `lastOffset`, `lastLength` and
1608
1877
  * `lastFin` describing it for the packet's record; or `at` when no stream
1609
1878
  * has a frame that fits.
1879
+ *
1880
+ * @param buf The packet being built.
1881
+ * @param at The index in `buf` where the frame starts.
1882
+ * @param end The index in `buf` past the room the frame may use.
1883
+ * @returns The index past the frame written, or `at` when none was.
1610
1884
  */
1611
1885
  putNextChunk(buf: u8[], at: i32, end: i32): i32 {
1612
1886
  const count: i32 = toI32(this.slots.length)
@@ -1642,7 +1916,7 @@ export class QuicStreams {
1642
1916
  * (§4.5); or, with no bytes left to send again, a lost FIN alone, as a
1643
1917
  * frame of no bytes at the final size.
1644
1918
  */
1645
- putResend(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
1919
+ private putResend(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
1646
1920
  if (stream.resendLow >= 0) {
1647
1921
  let low: i64 = stream.resendLow < stream.sendBase ? stream.sendBase : stream.resendLow
1648
1922
  while (low < stream.resendHigh && quicStreamBit(stream.sendAcked, stream.at(low))) {
@@ -1699,7 +1973,7 @@ export class QuicStreams {
1699
1973
  }
1700
1974
 
1701
1975
  /** Describes the STREAM frame just written, for the packet's record. */
1702
- noteChunk(id: i64, offset: i64, length: i32, fin: boolean): void {
1976
+ private noteChunk(id: i64, offset: i64, length: i32, fin: boolean): void {
1703
1977
  this.lastId = id
1704
1978
  this.lastOffset = offset
1705
1979
  this.lastLength = length
@@ -1710,7 +1984,7 @@ export class QuicStreams {
1710
1984
  * One STREAM frame of `stream`'s new bytes within both credits. A stream
1711
1985
  * held back by a credit owes the matching BLOCKED frame, once per limit.
1712
1986
  */
1713
- putNew(stream: QuicStream, k: i32, buf: u8[], at: i32, end: i32): i32 {
1987
+ private putNew(stream: QuicStream, k: i32, buf: u8[], at: i32, end: i32): i32 {
1714
1988
  const unsent: i64 = stream.sendEnd - stream.sendNext
1715
1989
  const streamCredit: i64 = stream.sendLimit - stream.sendNext
1716
1990
  const connectionCredit: i64 = this.sendMaxData - this.sendTotal
@@ -1772,6 +2046,7 @@ export class QuicStreams {
1772
2046
  * Answers the offset past it, with `lastKind` (a
1773
2047
  * `QUIC_STREAM_CONTROL_*`) and `lastValue` (the stream it names, or 0)
1774
2048
  * for the packet's record; or `at` when nothing owed fits.
2049
+ * @internal Read by quic.ts.
1775
2050
  */
1776
2051
  putNextControl(buf: u8[], at: i32, end: i32): i32 {
1777
2052
  this.lastValue = 0
@@ -1850,7 +2125,7 @@ export class QuicStreams {
1850
2125
  }
1851
2126
 
1852
2127
  /** The next control frame `stream` owes, if it fits; sets `lastKind`. */
1853
- putStreamControl(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
2128
+ private putStreamControl(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
1854
2129
  if (stream.maxDataOwed) {
1855
2130
  const next: i32 = quicPutStreamValue(
1856
2131
  buf,
@@ -1906,6 +2181,11 @@ export class QuicStreams {
1906
2181
  * and its FIN when `fin`, was acknowledged: the bytes are marked, the
1907
2182
  * buffer's start moves past every acknowledged one, and once all of them
1908
2183
  * and the FIN are, the sending side is done (§3.1 "Data Recvd").
2184
+ *
2185
+ * @param id The stream ID the packet's record names.
2186
+ * @param offset The stream offset of the chunk's first byte.
2187
+ * @param length The chunk's bytes.
2188
+ * @param fin Whether the chunk carried the FIN.
1909
2189
  */
1910
2190
  chunkAcked(id: i64, offset: i64, length: i32, fin: boolean): void {
1911
2191
  const k: i32 = this.slotOf(id)
@@ -1942,7 +2222,15 @@ export class QuicStreams {
1942
2222
  }
1943
2223
  }
1944
2224
 
1945
- /** A packet that carried those bytes was lost: they are queued to go again, unless the side is done or reset. */
2225
+ /**
2226
+ * A packet that carried those bytes was lost: they are queued to go again, unless the side is done
2227
+ * or reset.
2228
+ *
2229
+ * @param id The stream ID the packet's record names; a stream no longer open is ignored.
2230
+ * @param offset The stream offset of the chunk's first byte.
2231
+ * @param length The chunk's bytes.
2232
+ * @param fin Whether the chunk carried the FIN.
2233
+ */
1946
2234
  chunkLost(id: i64, offset: i64, length: i32, fin: boolean): void {
1947
2235
  const stream: QuicStream | null = this.find(id)
1948
2236
  if (stream !== null) {
@@ -1954,6 +2242,7 @@ export class QuicStreams {
1954
2242
  * A packet that carried control frame `kind` (for stream `value`, or 0)
1955
2243
  * was acknowledged: a RESET_STREAM acknowledged ends its sending side
1956
2244
  * (§3.1 "Reset Recvd").
2245
+ * @internal Read by quic.ts.
1957
2246
  */
1958
2247
  controlAcked(kind: i32, value: i64): void {
1959
2248
  if (kind !== QUIC_STREAM_CONTROL_RESET) {
@@ -1970,6 +2259,7 @@ export class QuicStreams {
1970
2259
  * A packet that carried control frame `kind` was lost: it is owed again
1971
2260
  * with the current value, while it still says something (§13.3) — a
1972
2261
  * BLOCKED frame only while the limit it named still holds.
2262
+ * @internal Read by quic.ts.
1973
2263
  */
1974
2264
  controlLost(kind: i32, value: i64): void {
1975
2265
  if (kind === QUIC_STREAM_CONTROL_MAX_DATA) {
@@ -1990,7 +2280,7 @@ export class QuicStreams {
1990
2280
  }
1991
2281
 
1992
2282
  /** A lost control frame of stream `id`. */
1993
- streamControlLost(kind: i32, id: i64): void {
2283
+ private streamControlLost(kind: i32, id: i64): void {
1994
2284
  const stream: QuicStream | null = this.find(id)
1995
2285
  if (stream === null) {
1996
2286
  return