nish 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 (118) hide show
  1. package/docs/AI.md +9 -2
  2. package/docs/INSTALL.md +6 -1
  3. package/package.json +15 -5
  4. package/runtime/nish.d.ts +4612 -430
  5. package/runtime/vite.d.ts +30 -0
  6. package/runtime/vite.mjs +150 -0
  7. package/scripts/build.sh +108 -13
  8. package/std/base64.ts +34 -5
  9. package/std/bytes.ts +256 -40
  10. package/std/cli-args.ts +296 -70
  11. package/std/cli-flags.ts +184 -19
  12. package/std/cli.ts +475 -54
  13. package/std/collections.ts +121 -23
  14. package/std/compress/checksum.ts +44 -14
  15. package/std/compress/deflate.ts +224 -111
  16. package/std/compress/gzip.ts +217 -53
  17. package/std/compress/zlib.ts +63 -7
  18. package/std/crypto/aes.ts +80 -9
  19. package/std/crypto/base64url.ts +22 -0
  20. package/std/crypto/bcrypt-batch.ts +88 -21
  21. package/std/crypto/bcrypt.ts +60 -0
  22. package/std/crypto/chacha20poly1305.ts +53 -0
  23. package/std/crypto/ct.ts +32 -13
  24. package/std/crypto/ed25519.ts +47 -0
  25. package/std/crypto/hkdf.ts +198 -13
  26. package/std/crypto/hmac.ts +235 -31
  27. package/std/crypto/jwt.ts +192 -63
  28. package/std/crypto/p256.ts +159 -16
  29. package/std/crypto/p384.ts +28 -3
  30. package/std/crypto/pki-roots.ts +32 -2
  31. package/std/crypto/pki.ts +81 -15
  32. package/std/crypto/rsa.ts +33 -3
  33. package/std/crypto/sha1.ts +3 -0
  34. package/std/crypto/sha256.ts +24 -1
  35. package/std/crypto/sha512.ts +99 -14
  36. package/std/crypto/x25519.ts +152 -14
  37. package/std/crypto/x509-random.ts +10 -0
  38. package/std/crypto/x509.ts +67 -7
  39. package/std/json-equal.ts +19 -0
  40. package/std/json-format.ts +112 -13
  41. package/std/json-number.ts +64 -14
  42. package/std/json-pointer.ts +23 -1
  43. package/std/json-tokenizer.ts +100 -29
  44. package/std/json-typed.ts +713 -66
  45. package/std/json-value.ts +400 -124
  46. package/std/json.ts +138 -10
  47. package/std/map.ts +25 -7
  48. package/std/math.ts +36 -4
  49. package/std/net/buffer-pool.ts +42 -11
  50. package/std/net/dns-config.ts +109 -23
  51. package/std/net/dns-message.ts +62 -7
  52. package/std/net/dns-resolver.ts +247 -53
  53. package/std/net/dns.ts +203 -35
  54. package/std/net/fetch.ts +151 -61
  55. package/std/net/handler.ts +278 -80
  56. package/std/net/hpack.ts +262 -52
  57. package/std/net/http-compress.ts +125 -15
  58. package/std/net/http-fields.ts +85 -9
  59. package/std/net/http1-client-codec.ts +100 -19
  60. package/std/net/http1-codec.ts +235 -27
  61. package/std/net/http1-server.ts +343 -85
  62. package/std/net/http2-frame.ts +272 -35
  63. package/std/net/http2-tls.ts +135 -34
  64. package/std/net/http2.ts +247 -106
  65. package/std/net/http3-frame.ts +154 -17
  66. package/std/net/http3-server.ts +246 -96
  67. package/std/net/http3.ts +307 -115
  68. package/std/net/outbound.ts +37 -3
  69. package/std/net/postgres-carrier.ts +144 -8
  70. package/std/net/postgres.ts +723 -103
  71. package/std/net/prefork.ts +164 -31
  72. package/std/net/qpack.ts +105 -18
  73. package/std/net/quic-conn-ack.ts +59 -12
  74. package/std/net/quic-conn-cid.ts +124 -27
  75. package/std/net/quic-conn-params.ts +70 -10
  76. package/std/net/quic-datagram.ts +43 -3
  77. package/std/net/quic-frame.ts +286 -12
  78. package/std/net/quic-listener.ts +101 -32
  79. package/std/net/quic-packet.ts +302 -21
  80. package/std/net/quic-recovery.ts +228 -44
  81. package/std/net/quic-stream.ts +353 -91
  82. package/std/net/quic.ts +489 -171
  83. package/std/net/router.ts +87 -40
  84. package/std/net/tls/client.ts +166 -51
  85. package/std/net/tls/codec.ts +413 -56
  86. package/std/net/tls/record-client.ts +110 -55
  87. package/std/net/tls/record-server.ts +159 -59
  88. package/std/net/tls/record.ts +109 -12
  89. package/std/net/tls/schedule.ts +235 -26
  90. package/std/net/tls/ticket.ts +159 -40
  91. package/std/net/tls/verify.ts +49 -3
  92. package/std/net/tls-tcp.ts +250 -45
  93. package/std/net/tls.ts +221 -59
  94. package/std/net/upstream.ts +81 -45
  95. package/std/net/url.ts +97 -13
  96. package/std/net/websocket.ts +122 -12
  97. package/std/net/webtransport.ts +220 -89
  98. package/std/pair.ts +10 -2
  99. package/std/random.ts +34 -9
  100. package/std/regex.ts +352 -198
  101. package/std/region.ts +70 -15
  102. package/std/secret.ts +52 -11
  103. package/std/sort.ts +17 -2
  104. package/std/sqlite.ts +256 -35
  105. package/std/sync/ring.ts +205 -35
  106. package/std/sync/shm.ts +194 -16
  107. package/std/term-io.ts +137 -64
  108. package/std/term-pixels.ts +144 -19
  109. package/std/term-probe.ts +66 -25
  110. package/std/term-rate.ts +128 -57
  111. package/std/term.ts +332 -121
  112. package/std/testing.ts +130 -32
  113. package/std/text.ts +73 -7
  114. package/std/threads.ts +229 -41
  115. package/std/yaml-json.ts +94 -82
  116. package/std/yaml-lines.ts +47 -15
  117. package/std/yaml-write.ts +24 -16
  118. package/std/yaml.ts +764 -384
@@ -146,8 +146,16 @@ export const QUIC_RECOVERY_ECT0: i32 = 2
146
146
  export const QUIC_RECOVERY_ECN_TESTING_PACKETS: i32 = 10
147
147
  /** ECN's states (RFC 9000 A.4): testing, marking the first packets; unknown, those sent and none validated yet; capable; and failed, for the connection's life. */
148
148
  export const QUIC_RECOVERY_ECN_TESTING: i32 = 0
149
+ /**
150
+ * ECN is unknown: the testing packets are sent, none is validated yet, and nothing more is marked
151
+ * until one is.
152
+ */
149
153
  export const QUIC_RECOVERY_ECN_UNKNOWN: i32 = 1
154
+ /** The path is ECN-capable: a marked packet's counts validated, and every packet is marked from here on. */
150
155
  export const QUIC_RECOVERY_ECN_CAPABLE: i32 = 2
156
+ /**
157
+ * Validation failed: nothing is marked again for the connection's life, unless `restartEcn` tests a new path.
158
+ */
151
159
  export const QUIC_RECOVERY_ECN_FAILED: i32 = 3
152
160
 
153
161
  /** Microseconds in a millisecond: the pacer's clock against the caller's `now`. */
@@ -193,8 +201,8 @@ export class QuicSentPackets {
193
201
  lastAckEliciting: i64 = -1
194
202
  /** The bytes of this space's packets in flight. */
195
203
  bytesInFlight: i64 = 0
196
- capacity: i32 = 0
197
- head: i32 = 0
204
+ private capacity: i32 = 0
205
+ private head: i32 = 0
198
206
  count: i32 = 0
199
207
  /** How many recorded packets are still in flight: neither acknowledged nor lost. */
200
208
  inFlight: i32 = 0
@@ -207,6 +215,13 @@ export class QuicSentPackets {
207
215
  ect1: i64 = 0
208
216
  ce: i64 = 0
209
217
 
218
+ /**
219
+ * An empty ring; `QuicRecovery` makes one for each space.
220
+ *
221
+ * @param capacity How many sent packets the ring records at once, fixed for its life: in flight and
222
+ * settled alike, until `compact` drops the settled ones. `QUIC_RECOVERY_HANDSHAKE_CAPACITY` or
223
+ * `QUIC_RECOVERY_APPLICATION_CAPACITY`.
224
+ */
210
225
  constructor(capacity: i32) {
211
226
  this.capacity = capacity
212
227
  this.pn = new Array<i64>(capacity)
@@ -218,12 +233,21 @@ export class QuicSentPackets {
218
233
  this.lost = new Array<i32>(capacity)
219
234
  }
220
235
 
221
- /** The ring slot of the `k`-th packet kept, counting from the oldest. */
236
+ /**
237
+ * The ring slot of the `k`-th packet kept, counting from the oldest.
238
+ * @internal Read by quic.ts, and in quic-recovery.ts outside the class.
239
+ */
222
240
  slot(k: i32): i32 {
223
241
  return (this.head + k) % this.capacity
224
242
  }
225
243
 
226
- /** The state of slot `s`, or free for a slot outside the ring. */
244
+ /**
245
+ * The state of slot `s`, or free for a slot outside the ring.
246
+ *
247
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
248
+ * @returns The slot's state without its ECT(0) mark: free, in flight, acknowledged, lost, or lost now
249
+ * (`markLost`'s mark for a packet the current call declared lost).
250
+ */
227
251
  stateAt(s: i32): i32 {
228
252
  if (s < 0 || s >= toI32(this.state.length)) {
229
253
  return QUIC_RECOVERY_FREE
@@ -231,67 +255,128 @@ export class QuicSentPackets {
231
255
  return toI32(this.state[s]) & (QUIC_RECOVERY_MARKED - 1)
232
256
  }
233
257
 
234
- /** Sets slot `s`'s state. */
258
+ /**
259
+ * Sets slot `s`'s state.
260
+ * @internal Read in quic-recovery.ts outside the class.
261
+ */
235
262
  setState(s: i32, value: i32): void {
236
263
  if (s >= 0 && s < toI32(this.state.length)) {
237
264
  this.state[s] = toU8((toI32(this.state[s]) & QUIC_RECOVERY_MARKED) | value)
238
265
  }
239
266
  }
240
267
 
241
- /** The packet number in slot `s`, or -1. */
268
+ /**
269
+ * The packet number in slot `s`, or -1.
270
+ *
271
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
272
+ * @returns The packet number recorded there, or -1 for an index outside the ring.
273
+ */
242
274
  pnAt(s: i32): i64 {
243
275
  return s >= 0 && s < toI32(this.pn.length) ? this.pn[s] : -1
244
276
  }
245
277
 
246
- /** When the packet in slot `s` was sent, or -1. */
278
+ /**
279
+ * When the packet in slot `s` was sent, or -1.
280
+ *
281
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
282
+ * @returns The `now` it was recorded at, in the caller's milliseconds, or -1 for an index outside the ring.
283
+ */
247
284
  timeAt(s: i32): i64 {
248
285
  return s >= 0 && s < toI32(this.timeSent.length) ? this.timeSent[s] : -1
249
286
  }
250
287
 
251
- /** When the packet in slot `s` was sent on the pacer's clock, in microseconds, or -1. */
288
+ /**
289
+ * When the packet in slot `s` was sent on the pacer's clock, in microseconds, or -1.
290
+ *
291
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
292
+ * @returns The send time in microseconds (`now × 1000` when `micros` was not current), or -1 for an
293
+ * index outside the ring.
294
+ */
252
295
  fineAt(s: i32): i64 {
253
296
  return s >= 0 && s < toI32(this.fineSent.length) ? this.fineSent[s] : -1
254
297
  }
255
298
 
256
- /** The size of the packet in slot `s`, or 0. */
299
+ /**
300
+ * The size of the packet in slot `s`, or 0.
301
+ *
302
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
303
+ * @returns Its size on the wire in bytes, or 0 for an index outside the ring.
304
+ */
257
305
  sizeAt(s: i32): i32 {
258
306
  return s >= 0 && s < toI32(this.size.length) ? this.size[s] : 0
259
307
  }
260
308
 
261
- /** Whether the packet in slot `s` was sent marked ECT(0). */
309
+ /**
310
+ * Whether the packet in slot `s` was sent marked ECT(0).
311
+ *
312
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
313
+ * @returns True when `onEctSent` marked it; false for an index outside the ring.
314
+ */
262
315
  markedAt(s: i32): boolean {
263
316
  return s >= 0 && s < toI32(this.state.length) && (toI32(this.state[s]) & QUIC_RECOVERY_MARKED) !== 0
264
317
  }
265
318
 
266
- /** Sets whether the packet in slot `s` was sent marked ECT(0). */
319
+ /**
320
+ * Sets whether the packet in slot `s` was sent marked ECT(0).
321
+ * @internal Read in quic-recovery.ts outside the class.
322
+ */
267
323
  setMarked(s: i32, value: boolean): void {
268
324
  if (s >= 0 && s < toI32(this.state.length)) {
269
325
  this.state[s] = toU8(this.stateAt(s) | (value ? QUIC_RECOVERY_MARKED : 0))
270
326
  }
271
327
  }
272
328
 
273
- /** The `k`-th slot the last call acknowledged, or -1. */
329
+ /**
330
+ * The `k`-th slot the last call acknowledged, or -1.
331
+ *
332
+ * @param k The position in `acked`, from 0 to `ackedCount - 1`, oldest first.
333
+ * @returns The slot, or -1 for `k` out of range. It names that packet only until the next call that
334
+ * records, settles or frees a packet in this space, any of which can free or reuse the slot:
335
+ * `onPacketSent`, `onAck`, `onAckEcn`, `onTimeout`, `evictOldest`, `discardSpace` and `reset` on the
336
+ * recovery, and this ring's own `full` and `compact`.
337
+ */
274
338
  ackedSlot(k: i32): i32 {
275
339
  return k >= 0 && k < this.ackedCount && k < toI32(this.acked.length) ? this.acked[k] : -1
276
340
  }
277
341
 
278
- /** The `k`-th slot the last call declared lost, or -1. */
342
+ /**
343
+ * The `k`-th slot the last call declared lost, or -1.
344
+ *
345
+ * @param k The position in `lost`, from 0 to `lostCount - 1`, oldest first.
346
+ * @returns The slot, or -1 for `k` out of range. It names that packet only until the next call that
347
+ * records, settles or frees a packet in this space, any of which can free or reuse the slot:
348
+ * `onPacketSent`, `onAck`, `onAckEcn`, `onTimeout`, `evictOldest`, `discardSpace` and `reset` on the
349
+ * recovery, and this ring's own `full` and `compact`.
350
+ */
279
351
  lostSlot(k: i32): i32 {
280
352
  return k >= 0 && k < this.lostCount && k < toI32(this.lost.length) ? this.lost[k] : -1
281
353
  }
282
354
 
283
- /** Whether slot `s` holds a packet still in flight. */
355
+ /**
356
+ * Whether slot `s` holds a packet still in flight.
357
+ *
358
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
359
+ * @returns True for a packet neither acknowledged nor lost; false for an index outside the ring.
360
+ */
284
361
  inFlightAt(s: i32): boolean {
285
362
  return this.stateAt(s) === QUIC_RECOVERY_SENT
286
363
  }
287
364
 
288
- /** Whether every slot is taken, so nothing more can be recorded until a packet is settled. */
365
+ /**
366
+ * Whether every slot is taken, so nothing more can be recorded until a packet is settled.
367
+ *
368
+ * @returns True when `onPacketSent` would refuse a packet for want of room. Settled slots at the oldest
369
+ * end are freed first, so asking can change `count`.
370
+ */
289
371
  full(): boolean {
290
372
  this.compact()
291
373
  return this.count >= this.capacity
292
374
  }
293
375
 
294
- /** Frees the oldest slots that are settled, acknowledged or lost. */
376
+ /**
377
+ * Frees the oldest slots that are settled, acknowledged or lost.
378
+ * @internal Read by quic.ts, and in quic-recovery.ts outside the class.
379
+ */
295
380
  compact(): void {
296
381
  while (this.count > 0 && this.stateAt(this.head) !== QUIC_RECOVERY_SENT) {
297
382
  this.setState(this.head, QUIC_RECOVERY_FREE)
@@ -303,20 +388,29 @@ export class QuicSentPackets {
303
388
  }
304
389
  }
305
390
 
306
- /** Forgets the results of the last call. */
391
+ /**
392
+ * Forgets the results of the last call.
393
+ * @internal Read in quic-recovery.ts outside the class.
394
+ */
307
395
  clearResults(): void {
308
396
  this.ackedCount = 0
309
397
  this.lostCount = 0
310
398
  }
311
399
 
312
- /** Takes packet `s` out of flight. */
400
+ /**
401
+ * Takes packet `s` out of flight.
402
+ * @internal Read in quic-recovery.ts outside the class.
403
+ */
313
404
  settle(s: i32, state: i32): void {
314
405
  this.setState(s, state)
315
406
  this.inFlight = this.inFlight - 1
316
407
  this.bytesInFlight = this.bytesInFlight - toI64(this.sizeAt(s))
317
408
  }
318
409
 
319
- /** Settles packet `s` as lost and lists it in `lost`. */
410
+ /**
411
+ * Settles packet `s` as lost and lists it in `lost`.
412
+ * @internal Read in quic-recovery.ts outside the class.
413
+ */
320
414
  markLost(s: i32): void {
321
415
  this.settle(s, QUIC_RECOVERY_LOST_NOW)
322
416
  if (this.lostCount < toI32(this.lost.length)) {
@@ -325,7 +419,10 @@ export class QuicSentPackets {
325
419
  }
326
420
  }
327
421
 
328
- /** Drops every packet, as when the space's keys are discarded (A.11). */
422
+ /**
423
+ * Drops every packet, as when the space's keys are discarded (A.11).
424
+ * @internal Read in quic-recovery.ts outside the class.
425
+ */
329
426
  clear(): void {
330
427
  for (let s: i32 = 0; s < toI32(this.state.length); s += 1) {
331
428
  this.state[s] = toU8(QUIC_RECOVERY_FREE)
@@ -339,7 +436,10 @@ export class QuicSentPackets {
339
436
  this.clearResults()
340
437
  }
341
438
 
342
- /** Empties the ring and forgets every number, for a connection slot reused for another peer. */
439
+ /**
440
+ * Empties the ring and forgets every number, for a connection slot reused for another peer.
441
+ * @internal Read in quic-recovery.ts outside the class.
442
+ */
343
443
  reset(): void {
344
444
  this.clear()
345
445
  this.largestAcked = -1
@@ -418,6 +518,10 @@ export class QuicRecovery {
418
518
  /** Whether the handshake is confirmed (RFC 9001 §4.1.2), which the caller sets: the peer's max_ack_delay then bounds its ACK delays, and the Application Data space has a probe timeout. */
419
519
  handshakeConfirmed: boolean = false
420
520
 
521
+ /**
522
+ * Makes the state a new connection has, as `reset` leaves it: every space
523
+ * empty, the RTT estimate and the window at their initial values.
524
+ */
421
525
  constructor() {
422
526
  this.spaces = [
423
527
  new QuicSentPackets(QUIC_RECOVERY_HANDSHAKE_CAPACITY),
@@ -454,6 +558,7 @@ export class QuicRecovery {
454
558
  * address it has proved it holds (RFC 9000 §9.4): what was learnt of the
455
559
  * old path says nothing about the new one. The packets in flight, the
456
560
  * peer's `max_ack_delay` and the counters are kept.
561
+ * @internal Read by quic.ts.
457
562
  */
458
563
  resetPath(): void {
459
564
  this.latestRtt = 0
@@ -476,6 +581,7 @@ export class QuicRecovery {
476
581
  * went out under the old test. Their loss, or an ACK of them, says
477
582
  * nothing of the new test, and only packets marked from here on count
478
583
  * toward it.
584
+ * @internal Read by quic.ts.
479
585
  */
480
586
  restartEcn(): void {
481
587
  for (const sp of this.spaces) {
@@ -489,7 +595,13 @@ export class QuicRecovery {
489
595
  this.ecnTestLost = 0
490
596
  }
491
597
 
492
- /** Space `space`'s packets, or `null` for an index that names no space. */
598
+ /**
599
+ * Space `space`'s packets, or `null` for an index that names no space.
600
+ *
601
+ * @param space A space's index: `QUIC_RECOVERY_INITIAL`, `QUIC_RECOVERY_HANDSHAKE` or
602
+ * `QUIC_RECOVERY_APPLICATION`.
603
+ * @returns The space's own ring, not a copy, or `null` for any other index.
604
+ */
493
605
  space(space: i32): QuicSentPackets | null {
494
606
  if (space < 0 || space >= toI32(this.spaces.length)) {
495
607
  return null
@@ -497,7 +609,11 @@ export class QuicRecovery {
497
609
  return this.spaces[space]
498
610
  }
499
611
 
500
- /** bytes_in_flight (B.2): every space's packets in flight. */
612
+ /**
613
+ * bytes_in_flight (B.2): every space's packets in flight.
614
+ *
615
+ * @returns The bytes of every packet in flight, summed over the three spaces.
616
+ */
501
617
  bytesInFlight(): i64 {
502
618
  let total: i64 = 0
503
619
  for (const sp of this.spaces) {
@@ -509,6 +625,8 @@ export class QuicRecovery {
509
625
  /**
510
626
  * Whether the window has room for another full-sized datagram (B.4). The
511
627
  * caller sends nothing ack-eliciting while it has not, except a probe.
628
+ *
629
+ * @returns True when the bytes in flight plus 1200 bytes fit the congestion window.
512
630
  */
513
631
  canSend(): boolean {
514
632
  return this.bytesInFlight() + QUIC_RECOVERY_DATAGRAM_SIZE <= this.congestionWindow
@@ -520,6 +638,12 @@ export class QuicRecovery {
520
638
  * the caller keeps what it carried, or -1, recording nothing, for a space
521
639
  * that does not exist, a size that is not positive, a packet number that is
522
640
  * not above the last one recorded, or a ring that is full.
641
+ *
642
+ * @param space The space's index, `QUIC_RECOVERY_INITIAL` to `QUIC_RECOVERY_APPLICATION`.
643
+ * @param pn The packet's number: above the last one recorded and at most 2^62 − 1.
644
+ * @param size The packet's size on the wire in bytes; must be positive.
645
+ * @param now The send time, in the caller's milliseconds.
646
+ * @returns The ring slot the packet is recorded in, or -1 when nothing was recorded.
523
647
  */
524
648
  onPacketSent(space: i32, pn: i64, size: i32, now: i64): i32 {
525
649
  const sp: QuicSentPackets | null = this.space(space)
@@ -555,6 +679,8 @@ export class QuicRecovery {
555
679
  * for the first `QUIC_RECOVERY_ECN_TESTING_PACKETS` and, once the path is
556
680
  * capable, for every one; 0, Not-ECT, while the testing packets wait to be
557
681
  * validated and once validation failed.
682
+ *
683
+ * @returns `QUIC_RECOVERY_ECT0` (2) to mark with, or 0 for Not-ECT.
558
684
  */
559
685
  ecnCodepoint(): i32 {
560
686
  const testing: boolean =
@@ -567,6 +693,9 @@ export class QuicRecovery {
567
693
  * `onPacketSent` answered for it, or -1 for a packet that elicits no
568
694
  * acknowledgement and so was not recorded. Every packet of a datagram
569
695
  * marked counts, whatever `ecnCodepoint()` answers by the last of them.
696
+ *
697
+ * @param space The space's index, `QUIC_RECOVERY_INITIAL` to `QUIC_RECOVERY_APPLICATION`.
698
+ * @param slot The slot `onPacketSent` answered for the packet, or -1 when it was not recorded.
570
699
  */
571
700
  onEctSent(space: i32, slot: i32): void {
572
701
  const sp: QuicSentPackets | null = this.space(space)
@@ -601,6 +730,14 @@ export class QuicRecovery {
601
730
  * exist or there is no range. A packet number the space never sent is the
602
731
  * caller's to refuse (RFC 9000 §13.1): only it knows the packets that
603
732
  * elicit no acknowledgement, which are not recorded here.
733
+ *
734
+ * @param space The space's index, `QUIC_RECOVERY_INITIAL` to `QUIC_RECOVERY_APPLICATION`.
735
+ * @param ranges The acknowledged packet numbers, as `[smallest, largest]` pairs, highest first; only read.
736
+ * @param count How many pairs of `ranges` to read; past the array's length the rest are ignored.
737
+ * @param ackDelay The peer's ACK Delay, already scaled, in milliseconds.
738
+ * @param now The time the ACK arrived, in the caller's milliseconds.
739
+ * @returns How many recorded packets were newly acknowledged, or -1 when the space or the ranges are
740
+ * missing.
604
741
  */
605
742
  onAck(space: i32, ranges: i64[], count: i32, ackDelay: i64, now: i64): i32 {
606
743
  const none: i64 = -1
@@ -613,6 +750,17 @@ export class QuicRecovery {
613
750
  * carries none. The counts are validated (RFC 9000 §13.4.2.1) and a rise
614
751
  * in ECN-CE is a congestion event (§7.1, B.7) before loss is detected, as
615
752
  * A.7 orders it.
753
+ *
754
+ * @param space The space's index, `QUIC_RECOVERY_INITIAL` to `QUIC_RECOVERY_APPLICATION`.
755
+ * @param ranges The acknowledged packet numbers, as `[smallest, largest]` pairs, highest first; only read.
756
+ * @param count How many pairs of `ranges` to read; past the array's length the rest are ignored.
757
+ * @param ackDelay The peer's ACK Delay, already scaled, in milliseconds.
758
+ * @param now The time the ACK arrived, in the caller's milliseconds.
759
+ * @param ect0 The frame's ECT(0) count, or -1 for an ACK without counts.
760
+ * @param ect1 The frame's ECT(1) count, or -1 for an ACK without counts.
761
+ * @param ce The frame's ECN-CE count, or -1 for an ACK without counts.
762
+ * @returns How many recorded packets were newly acknowledged, or -1 when the space or the ranges are
763
+ * missing.
616
764
  */
617
765
  onAckEcn(
618
766
  space: i32,
@@ -699,7 +847,7 @@ export class QuicRecovery {
699
847
  * acknowledged it is timed from `later`, a send time no earlier than the
700
848
  * largest acknowledged packet's, which recovery did not record.
701
849
  */
702
- processEcn(
850
+ private processEcn(
703
851
  sp: QuicSentPackets,
704
852
  raised: boolean,
705
853
  ect0: i64,
@@ -756,7 +904,7 @@ export class QuicRecovery {
756
904
  }
757
905
 
758
906
  /** UpdateRtt (§5.3, A.7) with a sample in `latestRtt`, `ackDelay` the peer's delay in milliseconds. */
759
- updateRtt(ackDelay: i64, now: i64): void {
907
+ private updateRtt(ackDelay: i64, now: i64): void {
760
908
  const latest: i64 = this.latestRtt
761
909
  if (this.firstSampleTime < 0) {
762
910
  this.minRtt = latest
@@ -780,13 +928,16 @@ export class QuicRecovery {
780
928
  }
781
929
 
782
930
  /** The peer's ACK delay in milliseconds as §5.3 counts it: at most max_ack_delay once the handshake is confirmed. */
783
- boundAckDelay(ackDelay: i64): i64 {
931
+ private boundAckDelay(ackDelay: i64): i64 {
784
932
  return this.handshakeConfirmed && ackDelay > this.maxAckDelay ? this.maxAckDelay : ackDelay
785
933
  }
786
934
 
787
935
  /**
788
936
  * `now` on the pacer's clock: `micros` when it falls inside the
789
937
  * millisecond `now` names, and `now × 1000` otherwise.
938
+ *
939
+ * @param now The caller's time in milliseconds.
940
+ * @returns The same instant in microseconds.
790
941
  */
791
942
  fineNow(now: i64): i64 {
792
943
  const coarse: i64 = now * QUIC_RECOVERY_MICROS
@@ -802,7 +953,7 @@ export class QuicRecovery {
802
953
  * runs long by up to a millisecond: only a carrier that calls `clock` on
803
954
  * some sends and not others sees that.
804
955
  */
805
- fineSample(now: i64, sent: i64): i64 {
956
+ private fineSample(now: i64, sent: i64): i64 {
806
957
  const at: i64 = this.fineNow(now)
807
958
  const floor: i64 = at === this.micros ? 1 : QUIC_RECOVERY_GRANULARITY * QUIC_RECOVERY_MICROS
808
959
  return quicRecoveryMax(at - sent, floor)
@@ -815,7 +966,7 @@ export class QuicRecovery {
815
966
  * true minimum, so a delay is taken off a little more readily than
816
967
  * `updateRtt` takes it.
817
968
  */
818
- updatePacerRtt(sample: i64, ackDelay: i64, first: boolean): void {
969
+ private updatePacerRtt(sample: i64, ackDelay: i64, first: boolean): void {
819
970
  if (first) {
820
971
  this.pacerRtt = sample
821
972
  return
@@ -828,7 +979,11 @@ export class QuicRecovery {
828
979
  this.pacerRtt = (7 * this.pacerRtt + adjusted) / 8
829
980
  }
830
981
 
831
- /** The loss delay (§6.1.2): 9/8 of the larger of the latest and the smoothed RTT, at least the granularity. */
982
+ /**
983
+ * The loss delay (§6.1.2): 9/8 of the larger of the latest and the smoothed RTT, at least the granularity.
984
+ *
985
+ * @returns How long after a packet was sent it is lost by the time threshold, in milliseconds, at least 1.
986
+ */
832
987
  lossDelay(): i64 {
833
988
  const delay: i64 = (9 * quicRecoveryMax(this.latestRtt, this.smoothedRtt)) / 8
834
989
  return quicRecoveryMax(delay, QUIC_RECOVERY_GRANULARITY)
@@ -840,7 +995,7 @@ export class QuicRecovery {
840
995
  * three numbers below the largest acknowledged; the first that is neither
841
996
  * sets `lossTime`.
842
997
  */
843
- detectLoss(sp: QuicSentPackets, now: i64): void {
998
+ private detectLoss(sp: QuicSentPackets, now: i64): void {
844
999
  sp.lossTime = -1
845
1000
  const delay: i64 = this.lossDelay()
846
1001
  const lostSendTime: i64 = now - delay
@@ -869,7 +1024,7 @@ export class QuicRecovery {
869
1024
  * congestion event for the newest of them, and persistent congestion
870
1025
  * when they span long enough.
871
1026
  */
872
- packetsLost(sp: QuicSentPackets, now: i64): void {
1027
+ private packetsLost(sp: QuicSentPackets, now: i64): void {
873
1028
  if (sp.lostCount === 0) {
874
1029
  return
875
1030
  }
@@ -906,7 +1061,7 @@ export class QuicRecovery {
906
1061
  * spanning more than three probe timeouts with max_ack_delay, and holding
907
1062
  * at least one packet this call declared lost.
908
1063
  */
909
- persistentCongestion(sp: QuicSentPackets): boolean {
1064
+ private persistentCongestion(sp: QuicSentPackets): boolean {
910
1065
  if (this.firstSampleTime < 0) {
911
1066
  return false
912
1067
  }
@@ -937,12 +1092,12 @@ export class QuicRecovery {
937
1092
  }
938
1093
 
939
1094
  /** Whether a packet sent at `sentTime` was sent during the current recovery period (B.4). */
940
- inRecovery(sentTime: i64): boolean {
1095
+ private inRecovery(sentTime: i64): boolean {
941
1096
  return this.recoveryStart >= 0 && sentTime <= this.recoveryStart
942
1097
  }
943
1098
 
944
1099
  /** OnCongestionEvent (B.6): halve the window, once per recovery period. */
945
- congestionEvent(sentTime: i64, now: i64): void {
1100
+ private congestionEvent(sentTime: i64, now: i64): void {
946
1101
  if (this.inRecovery(sentTime)) {
947
1102
  return
948
1103
  }
@@ -958,7 +1113,7 @@ export class QuicRecovery {
958
1113
  * all for a packet sent during recovery, or when `inUse` says the window
959
1114
  * was not being used (§7.8).
960
1115
  */
961
- packetAcked(sp: QuicSentPackets, s: i32, inUse: boolean): void {
1116
+ private packetAcked(sp: QuicSentPackets, s: i32, inUse: boolean): void {
962
1117
  if (!inUse || this.inRecovery(sp.timeAt(s))) {
963
1118
  return
964
1119
  }
@@ -976,6 +1131,8 @@ export class QuicRecovery {
976
1131
  * max(4 × rttvar, kGranularity), plus the peer's max_ack_delay once the
977
1132
  * handshake is confirmed. It is also what a key update's old keys are kept
978
1133
  * for and what the idle timeout is at least three of.
1134
+ *
1135
+ * @returns The period in milliseconds, at least 1.
979
1136
  */
980
1137
  probeTimeout(): i64 {
981
1138
  const base: i64 = this.ptoBase()
@@ -983,7 +1140,7 @@ export class QuicRecovery {
983
1140
  }
984
1141
 
985
1142
  /** smoothed_rtt + max(4 × rttvar, kGranularity): every probe timeout's period before max_ack_delay and the backoff. */
986
- ptoBase(): i64 {
1143
+ private ptoBase(): i64 {
987
1144
  return this.smoothedRtt + quicRecoveryMax(4 * this.rttVar, QUIC_RECOVERY_GRANULARITY)
988
1145
  }
989
1146
 
@@ -991,8 +1148,9 @@ export class QuicRecovery {
991
1148
  * GetPtoTimeAndSpace (A.8): when the probe timeout fires, the earliest
992
1149
  * over the spaces with an ack-eliciting packet in flight, with the backoff
993
1150
  * applied; the Application Data space counts only once the handshake is
994
- * confirmed. Answers -1 with nothing in flight. `ptoSpace()` names the
995
- * space it chose.
1151
+ * confirmed. Answers -1 with nothing in flight.
1152
+ *
1153
+ * @returns The time the probe timeout fires, in the caller's milliseconds, or -1 when no space has one.
996
1154
  */
997
1155
  ptoTime(): i64 {
998
1156
  const space: i32 = this.ptoSpace()
@@ -1003,7 +1161,7 @@ export class QuicRecovery {
1003
1161
  }
1004
1162
 
1005
1163
  /** The probe timeout of space `space`, from its last ack-eliciting packet, with the backoff. */
1006
- ptoTimeOf(space: i32): i64 {
1164
+ private ptoTimeOf(space: i32): i64 {
1007
1165
  const sp: QuicSentPackets | null = this.space(space)
1008
1166
  if (sp === null || sp.lastAckEliciting < 0) {
1009
1167
  return -1
@@ -1017,7 +1175,7 @@ export class QuicRecovery {
1017
1175
  }
1018
1176
 
1019
1177
  /** The space whose probe timeout fires first, or -1 when no space may have one. */
1020
- ptoSpace(): i32 {
1178
+ private ptoSpace(): i32 {
1021
1179
  let best: i32 = -1
1022
1180
  let bestTime: i64 = -1
1023
1181
  for (let space: i32 = 0; space < toI32(this.spaces.length); space += 1) {
@@ -1035,7 +1193,7 @@ export class QuicRecovery {
1035
1193
  }
1036
1194
 
1037
1195
  /** The space with the earliest `lossTime`, or -1 when none is set. */
1038
- lossSpace(): i32 {
1196
+ private lossSpace(): i32 {
1039
1197
  let best: i32 = -1
1040
1198
  let bestTime: i64 = -1
1041
1199
  for (let space: i32 = 0; space < toI32(this.spaces.length); space += 1) {
@@ -1053,6 +1211,9 @@ export class QuicRecovery {
1053
1211
  * time-threshold loss, else the probe timeout — or -1 for none. With
1054
1212
  * `amplificationBlocked`, when the server may send nothing until the client
1055
1213
  * does (RFC 9000 §8.1), there is no probe timeout (§6.2.2.1).
1214
+ *
1215
+ * @param amplificationBlocked Whether the anti-amplification limit stops the server sending anything now.
1216
+ * @returns The time `onTimeout` is next due, in the caller's milliseconds, or -1 when no timer is set.
1056
1217
  */
1057
1218
  deadline(amplificationBlocked: boolean): i64 {
1058
1219
  const loss: i32 = this.lossSpace()
@@ -1072,6 +1233,11 @@ export class QuicRecovery {
1072
1233
  * timeout is due, the backoff doubles and the caller is to send a probe in
1073
1234
  * `timeoutSpace`: `QUIC_RECOVERY_TIMEOUT_PTO`. Before either is due, or
1074
1235
  * when nothing is timed, `QUIC_RECOVERY_TIMEOUT_NONE`.
1236
+ *
1237
+ * @param now The caller's time in milliseconds.
1238
+ * @param amplificationBlocked Whether the anti-amplification limit stops the server sending anything now.
1239
+ * @returns One of `QUIC_RECOVERY_TIMEOUT_NONE`, `QUIC_RECOVERY_TIMEOUT_LOSS` or
1240
+ * `QUIC_RECOVERY_TIMEOUT_PTO`.
1075
1241
  */
1076
1242
  onTimeout(now: i64, amplificationBlocked: boolean): i32 {
1077
1243
  for (const sp of this.spaces) {
@@ -1104,6 +1270,9 @@ export class QuicRecovery {
1104
1270
  * when the ring is full. The caller has already queued what the space had
1105
1271
  * in flight again, which is what a probe sends. Answers the slot, or -1
1106
1272
  * when nothing is in flight there.
1273
+ *
1274
+ * @param space The space's index, `QUIC_RECOVERY_INITIAL` to `QUIC_RECOVERY_APPLICATION`.
1275
+ * @returns The slot of the packet given up, now settled as lost, or -1 when nothing is in flight there.
1107
1276
  */
1108
1277
  evictOldest(space: i32): i32 {
1109
1278
  const sp: QuicSentPackets | null = this.space(space)
@@ -1124,6 +1293,8 @@ export class QuicRecovery {
1124
1293
  /**
1125
1294
  * OnPacketNumberSpaceDiscarded (A.11): the space's keys are gone, so its
1126
1295
  * packets leave flight uncounted, its timers stop, and the backoff resets.
1296
+ *
1297
+ * @param space The space's index, `QUIC_RECOVERY_INITIAL` to `QUIC_RECOVERY_APPLICATION`.
1127
1298
  */
1128
1299
  discardSpace(space: i32): void {
1129
1300
  const sp: QuicSentPackets | null = this.space(space)
@@ -1135,7 +1306,7 @@ export class QuicRecovery {
1135
1306
  }
1136
1307
 
1137
1308
  /** The congestion window the pacer earns by: at most `QUIC_RECOVERY_PACER_WINDOW`. */
1138
- pacerWindow(): i64 {
1309
+ private pacerWindow(): i64 {
1139
1310
  return this.congestionWindow < QUIC_RECOVERY_PACER_WINDOW
1140
1311
  ? this.congestionWindow
1141
1312
  : QUIC_RECOVERY_PACER_WINDOW
@@ -1146,7 +1317,7 @@ export class QuicRecovery {
1146
1317
  * window per `pacerRtt`, and holds at most the initial window, which is the
1147
1318
  * largest burst it allows.
1148
1319
  */
1149
- pacerRefill(now: i64): void {
1320
+ private pacerRefill(now: i64): void {
1150
1321
  const at: i64 = this.fineNow(now)
1151
1322
  if (this.pacerTime < 0 || at < this.pacerTime) {
1152
1323
  this.pacerTime = at
@@ -1171,6 +1342,10 @@ export class QuicRecovery {
1171
1342
  * bytes go: 0 when it may go now, else its wait in microseconds rounded
1172
1343
  * up to whole milliseconds, so at least 1. The rate is 5/4 of the window per `pacerRtt`
1173
1344
  * (§7.7's N of 1.25).
1345
+ *
1346
+ * @param now The caller's time in milliseconds; the pacer reads `micros` too when it is current.
1347
+ * @param size The datagram's size in bytes.
1348
+ * @returns The wait in whole milliseconds: 0 to send now, else at least 1.
1174
1349
  */
1175
1350
  pacerDelay(now: i64, size: i32): i64 {
1176
1351
  this.pacerRefill(now)
@@ -1190,6 +1365,10 @@ export class QuicRecovery {
1190
1365
  * pacer that earns by the microsecond would otherwise let a fast
1191
1366
  * connection out a datagram at a time, one send each; one that earns
1192
1367
  * slowly still lets a datagram go as soon as it has the credit for it.
1368
+ *
1369
+ * @param least The smallest quantum, in bytes, such as one full datagram.
1370
+ * @param most The largest quantum, in bytes, such as `QUIC_LISTENER_FLIGHT_QUANTUM`.
1371
+ * @returns The bytes the pacer earns in a millisecond, held to `least` and `most`.
1193
1372
  */
1194
1373
  pacerQuantum(least: i32, most: i32): i32 {
1195
1374
  const rtt: i64 = quicRecoveryMax(this.pacerRtt, 1)
@@ -1200,7 +1379,12 @@ export class QuicRecovery {
1200
1379
  return milli > toI64(most) ? most : toI32(milli)
1201
1380
  }
1202
1381
 
1203
- /** Charges the pacer for a datagram of `size` bytes sent at `now`. */
1382
+ /**
1383
+ * Charges the pacer for a datagram of `size` bytes sent at `now`.
1384
+ *
1385
+ * @param now The send time in the caller's milliseconds; the pacer is topped up to it first.
1386
+ * @param size The datagram's size in bytes, which may take the credit below 0.
1387
+ */
1204
1388
  onPaced(now: i64, size: i32): void {
1205
1389
  this.pacerRefill(now)
1206
1390
  this.pacerBudget = this.pacerBudget - toI64(size)