@amritk/nish-x86_64-linux 0.38.0 → 0.40.1

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 (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
package/std/net/http2.ts CHANGED
@@ -175,6 +175,7 @@ import {
175
175
  H2_SETTINGS_MAX_CONCURRENT_STREAMS,
176
176
  H2_SETTINGS_MAX_FRAME_SIZE,
177
177
  H2_SETTINGS_MAX_HEADER_LIST_SIZE,
178
+ H2_SETTINGS_TIMEOUT,
178
179
  H2_STREAM_CLOSED,
179
180
  Http2Frame,
180
181
  http2ParseFrame,
@@ -292,6 +293,16 @@ export class Http2Config {
292
293
  * stream error the peer caused — net of the ones the server finishes.
293
294
  */
294
295
  resetBudget: i32 = 100
296
+ /**
297
+ * Milliseconds the peer has, from `begin`, to acknowledge the server's SETTINGS: past them
298
+ * `expired` answers true and `timeOut` ends the connection with SETTINGS_TIMEOUT (RFC 9113 §6.5.3).
299
+ */
300
+ settingsTimeout: i32 = 10000
301
+ /**
302
+ * Milliseconds without a byte read or written, counted from `lastActive`, after which `expired`
303
+ * answers true and `timeOut` ends the connection with GOAWAY NO_ERROR, streams open or not.
304
+ */
305
+ idleTimeout: i32 = 60000
295
306
  }
296
307
 
297
308
  /**
@@ -509,6 +520,10 @@ export class Http2Connection {
509
520
  data: u8[]
510
521
  /** H2_RESET's, H2_GOAWAY's and H2_ERROR's code. */
511
522
  errorCode: i64 = 0
523
+ /** When the carrier last moved a byte for the connection, in `monotonicNanos`; `begin` starts it. */
524
+ lastActive: i64 = 0
525
+ /** When the server's SETTINGS began to wait for their acknowledgement, in `monotonicNanos`; `begin` sets it. */
526
+ settingsSince: i64 = 0
512
527
  /** Where the bytes fed and not read yet start in `input`. */
513
528
  inputStart: i32 = 0
514
529
  /** Where the bytes fed and not read yet end in `input`. */
@@ -590,6 +605,8 @@ export class Http2Connection {
590
605
  http2CheckCap("headerTableSize", config.headerTableSize, 0, H2_MAX_WINDOW)
591
606
  http2CheckCap("outputSize", config.outputSize, 1024, H2_MAX_WINDOW)
592
607
  http2CheckCap("resetBudget", config.resetBudget, 0, H2_MAX_WINDOW)
608
+ http2CheckCap("settingsTimeout", config.settingsTimeout, 1, H2_MAX_WINDOW)
609
+ http2CheckCap("idleTimeout", config.idleTimeout, 1, H2_MAX_WINDOW)
593
610
  this.config = config
594
611
  this.frame = new Http2Frame()
595
612
  this.empty = []
@@ -688,6 +705,73 @@ export class Http2Connection {
688
705
  this.prefaceOwed = true
689
706
  }
690
707
 
708
+ // ---- The clock ----------------------------------------------------------------
709
+
710
+ /**
711
+ * Starts both of the connection's clocks at `now`: the deadline for the
712
+ * peer's acknowledgement of the server's SETTINGS, and the idle one. The
713
+ * connection reads no clock itself; a carrier calls this when it takes the
714
+ * peer, stamps `lastActive` whenever bytes move, and asks `expired`.
715
+ *
716
+ * @param now the current time in `monotonicNanos`, which both timeouts count from
717
+ */
718
+ begin(now: i64): void {
719
+ this.settingsSince = now
720
+ this.lastActive = now
721
+ }
722
+
723
+ /** Whether the server's SETTINGS have waited longer than `settingsTimeout` at `now` (`monotonicNanos`). */
724
+ private settingsLate(now: i64): boolean {
725
+ return (
726
+ !this.settingsAcked && now - this.settingsSince > toI64(this.config.settingsTimeout) * toI64(1000000)
727
+ )
728
+ }
729
+
730
+ /**
731
+ * Whether the connection has outlived a timeout at `now`: the peer has not
732
+ * acknowledged the server's SETTINGS within `settingsTimeout` of `begin`,
733
+ * or nothing has moved for longer than `idleTimeout`.
734
+ *
735
+ * @param now the current time in `monotonicNanos`
736
+ * @returns true when the carrier should end the connection, after `timeOut` has written its GOAWAY
737
+ */
738
+ expired(now: i64): boolean {
739
+ return this.settingsLate(now) || now - this.lastActive > toI64(this.config.idleTimeout) * toI64(1000000)
740
+ }
741
+
742
+ /**
743
+ * The soonest `expired` can answer true at or after now, however the
744
+ * connection moves from here: `lastActive` only grows, and the SETTINGS
745
+ * deadline goes once they are acknowledged.
746
+ *
747
+ * @param now the current time in `monotonicNanos`
748
+ * @returns the earliest `monotonicNanos` instant at which `expired` could answer true
749
+ */
750
+ expiresNoSooner(now: i64): i64 {
751
+ let soonest: i64 = this.lastActive + toI64(this.config.idleTimeout) * toI64(1000000)
752
+ if (!this.settingsAcked) {
753
+ const settings: i64 = this.settingsSince + toI64(this.config.settingsTimeout) * toI64(1000000)
754
+ soonest = settings < soonest ? settings : soonest
755
+ }
756
+ return soonest > now ? soonest : now
757
+ }
758
+
759
+ /**
760
+ * Ends a connection `expired` at `now`: the connection fails with GOAWAY
761
+ * SETTINGS_TIMEOUT when the peer never acknowledged the server's SETTINGS
762
+ * in time (§6.5.3), and with GOAWAY NO_ERROR when it was idle. The
763
+ * carrier sends what it can of the output, then closes.
764
+ *
765
+ * @param now the current time in `monotonicNanos`, as `expired` was asked at
766
+ */
767
+ timeOut(now: i64): void {
768
+ if (this.state === H2_STATE_FAILED) {
769
+ return
770
+ }
771
+ this.room()
772
+ this.fail(this.settingsLate(now) ? H2_SETTINGS_TIMEOUT : H2_NO_ERROR)
773
+ }
774
+
691
775
  // ---- Borrowed buffers ---------------------------------------------------------
692
776
 
693
777
  /** The pools the connection borrows from, made at ceiling 1 the first time a bare connection asks. */
@@ -1519,13 +1603,23 @@ export class Http2Connection {
1519
1603
  return H2_NEED_MORE
1520
1604
  }
1521
1605
  let opened: boolean = false
1606
+ // INITIAL_WINDOW_SIZE moves every stream's window, so the streams are
1607
+ // walked once, after every entry has been checked: a walk per entry let
1608
+ // one frame of 2,730 of them cost 2,730 walks (H2-5). The windows end at
1609
+ // the frame's last value, as they would entry by entry, and since every
1610
+ // window moves by the same amount, one passes 2^31 - 1 at some entry
1611
+ // (§6.9.2, the entries taken in order, §6.5.3) exactly when it would at
1612
+ // the frame's largest value.
1613
+ let window: i64 = this.peerInitialWindow
1614
+ let highest: i64 = this.peerInitialWindow
1522
1615
  const n: i32 = http2SettingCount(f)
1523
1616
  for (let k: i32 = 0; k < n; k++) {
1524
1617
  const id: i32 = http2SettingId(f, this.input, k)
1525
1618
  const value: i64 = http2SettingValue(f, this.input, k)
1526
1619
  const error: i32 = http2SettingError(id, value)
1527
1620
  if (error !== H2_NO_ERROR) {
1528
- return this.fail(error)
1621
+ // An earlier entry that took a window past 2^31 - 1 was the first error.
1622
+ return this.fail(this.windowPassesMax(highest) ? H2_FLOW_CONTROL_ERROR : error)
1529
1623
  }
1530
1624
  if (id === H2_SETTINGS_HEADER_TABLE_SIZE) {
1531
1625
  const size: i32 = toI32(Math.min(value, toI64(HPACK_DEFAULT_TABLE_SIZE)))
@@ -1533,19 +1627,9 @@ export class Http2Connection {
1533
1627
  this.encoder.setMaxTableSize(size)
1534
1628
  }
1535
1629
  } else if (id === H2_SETTINGS_INITIAL_WINDOW_SIZE) {
1536
- const delta: i64 = value - this.peerInitialWindow
1537
- this.peerInitialWindow = value
1538
- for (const s of this.streams) {
1539
- if (s.id !== 0) {
1540
- s.sendWindow = s.sendWindow + delta
1541
- if (s.sendWindow > toI64(H2_MAX_WINDOW)) {
1542
- return this.fail(H2_FLOW_CONTROL_ERROR)
1543
- }
1544
- if (delta > toI64(0) && s.blocked) {
1545
- s.blocked = false
1546
- opened = true
1547
- }
1548
- }
1630
+ window = value
1631
+ if (value > highest) {
1632
+ highest = value
1549
1633
  }
1550
1634
  } else if (id === H2_SETTINGS_MAX_FRAME_SIZE) {
1551
1635
  this.peerMaxFrameSize = toI32(value)
@@ -1553,6 +1637,22 @@ export class Http2Connection {
1553
1637
  this.peerMaxHeaderList = value
1554
1638
  }
1555
1639
  }
1640
+ if (this.windowPassesMax(highest)) {
1641
+ return this.fail(H2_FLOW_CONTROL_ERROR)
1642
+ }
1643
+ if (window !== this.peerInitialWindow) {
1644
+ const delta: i64 = window - this.peerInitialWindow
1645
+ this.peerInitialWindow = window
1646
+ for (const s of this.streams) {
1647
+ if (s.id !== 0) {
1648
+ s.sendWindow = s.sendWindow + delta
1649
+ if (delta > toI64(0) && s.blocked) {
1650
+ s.blocked = false
1651
+ opened = true
1652
+ }
1653
+ }
1654
+ }
1655
+ }
1556
1656
  this.wrote(http2WriteSettingsAck(this.output, this.outputEnd))
1557
1657
  if (opened) {
1558
1658
  this.stream = 0
@@ -1561,6 +1661,23 @@ export class Http2Connection {
1561
1661
  return H2_NEED_MORE
1562
1662
  }
1563
1663
 
1664
+ /**
1665
+ * Whether SETTINGS_INITIAL_WINDOW_SIZE at `initial` would take an open
1666
+ * stream's send window past 2^31 - 1, the connection error of §6.9.2.
1667
+ */
1668
+ private windowPassesMax(initial: i64): boolean {
1669
+ if (initial <= this.peerInitialWindow) {
1670
+ return false
1671
+ }
1672
+ const delta: i64 = initial - this.peerInitialWindow
1673
+ for (const s of this.streams) {
1674
+ if (s.id !== 0 && s.sendWindow + delta > toI64(H2_MAX_WINDOW)) {
1675
+ return true
1676
+ }
1677
+ }
1678
+ return false
1679
+ }
1680
+
1564
1681
  /** WINDOW_UPDATE (§6.9): a send window grows, and a write it held back may go on. */
1565
1682
  private handleWindowUpdate(f: Http2Frame): i32 {
1566
1683
  const inc: i64 = toI64(f.increment)
@@ -86,9 +86,21 @@
86
86
  * bucket from a bitmap. A deadline past the wheel's horizon is filed at the
87
87
  * horizon and filed again when it comes round. So no call scans the slots.
88
88
  *
89
+ * The HTTP/3 side's clock (`Http3Connection.clock`, H3-5) runs on every
90
+ * flush and files its deadline with the rest: a slot that goes
91
+ * `idleTimeout` without progress while no request is in hand is closed with
92
+ * H3_NO_ERROR, and one whose request head has waited `headTimeout` (a
93
+ * client trickling its HEADERS, the slowloris of HTTP/3) with
94
+ * H3_EXCESSIVE_LOAD, as the program writes no timer of its own. A client
95
+ * that only acknowledges or sends PING keeps its QUIC connection alive but
96
+ * not its slot. The clock times a slot from the first `next` that finds the
97
+ * handshake chose `h3`, whether or not the client has let this side's
98
+ * streams open; a slot the program runs another protocol on, such as
99
+ * `hq-interop`, is left to QUIC's idle timeout.
100
+ *
89
101
  * **Caps, fixed at start-up**: the slots, and inside each the QUIC
90
102
  * configuration's streams and buffers and the `Http3Config`'s field section,
91
- * body chunk and DATA frame.
103
+ * body chunk, DATA frame and two timeouts.
92
104
  *
93
105
  * A slot's connections and address are built on the first Initial accepted into
94
106
  * it and kept, so a pool sized for its peak costs at start-up only its per-slot
@@ -616,13 +628,21 @@ export class Http3Server {
616
628
  * @param config The HTTP/3 configuration, kept by reference; its `bodyChunk` and `maxFieldSectionSize`
617
629
  * size the shared buffer pools.
618
630
  * @param fd The bound UDP socket, kept; the server does not close it.
619
- * @param size How many slots, 1 to 65536; any other panics.
631
+ * @param size How many slots, 1 to 65536; any other panics, as does an `idleTimeout` or `headTimeout`
632
+ * under 1.
620
633
  * @param listenerEntropy `QUIC_LISTENER_ENTROPY_SIZE` random bytes, copied and then zeroed in this array.
621
634
  */
622
635
  constructor(quicConfig: QuicServerConfig, config: Http3Config, fd: i32, size: i32, listenerEntropy: u8[]) {
623
636
  if (size < 1 || size > 65536) {
624
637
  panic(`Http3Server: ${size} slots, outside 1 to 65536`)
625
638
  }
639
+ // Checked here as well as by each slot's connection, which is built only on its first Initial.
640
+ if (config.idleTimeout < 1) {
641
+ panic(`Http3Server: idleTimeout of ${config.idleTimeout}, outside 1 to 2147483647`)
642
+ }
643
+ if (config.headTimeout < 1) {
644
+ panic(`Http3Server: headTimeout of ${config.headTimeout}, outside 1 to 2147483647`)
645
+ }
626
646
  this.quicConfig = quicConfig
627
647
  this.config = config
628
648
  this.fd = fd
@@ -1139,9 +1159,12 @@ export class Http3Server {
1139
1159
  private flushSlot(slot: i32, now: i64): void {
1140
1160
  const quic: QuicConnection = this.quics[slot]
1141
1161
  quic.recovery.micros = this.micros
1142
- if (this.conns[slot].isDone() && !quic.closed()) {
1162
+ const conn: Http3Connection = this.conns[slot]
1163
+ if (conn.isDone() && !quic.closed()) {
1143
1164
  quic.close(H3_NO_ERROR)
1144
1165
  }
1166
+ // An idle client, or one trickling a request's head, loses the slot here (H3-5).
1167
+ const clocked: i64 = conn.clock(now)
1145
1168
  // A PATH_RESPONSE owed on a path the connection did not move to goes
1146
1169
  // there, alone (RFC 9000 §9.1); asking for it runs the connection's
1147
1170
  // timers first, so a validation that ran out has moved it back before
@@ -1199,6 +1222,9 @@ export class Http3Server {
1199
1222
  if (paced > now && (due < 0 || paced < due)) {
1200
1223
  due = paced
1201
1224
  }
1225
+ if (clocked >= 0 && (due < 0 || clocked < due)) {
1226
+ due = clocked
1227
+ }
1202
1228
  this.wheel.file(slot, due, now)
1203
1229
  }
1204
1230
 
package/std/net/http3.ts CHANGED
@@ -94,6 +94,9 @@
94
94
  * - a GOAWAY whose push ID rose, a MAX_PUSH_ID that fell, a CANCEL_PUSH past
95
95
  * the push IDs allowed (H3_ID_ERROR, §5.2, §7.2.7, §7.2.3);
96
96
  * - a control frame past `H3_CONTROL_FRAME_MAX` (H3_EXCESSIVE_LOAD);
97
+ * - a request reset past what `resetBudget` allows — by the client, or by
98
+ * this side for a stream error or a 431 the client caused, net of the
99
+ * requests this side answered (H3_EXCESSIVE_LOAD, §10.5);
97
100
  * - and QPACK's three.
98
101
  *
99
102
  * A stream error resets one request, both ways, and the connection lives: a
@@ -312,11 +315,22 @@ const H3_FLAG_WAITING: i32 = 16
312
315
  const H3_FLAG_WT: i32 = 256
313
316
  /** A request stream past this side's GOAWAY, refused unless its first bytes make it a WebTransport stream. */
314
317
  const H3_FLAG_LATE: i32 = 512
318
+ /** A request already charged to the reset budget: a client's RESET_STREAM and STOP_SENDING on one stream cost one. */
319
+ const H3_FLAG_CHARGED: i32 = 1024
315
320
 
316
321
  /** The connection's states. */
317
322
  const H3_STATE_OPEN: i32 = 0
318
323
  const H3_STATE_FAILED: i32 = 1
319
324
 
325
+ /** The clock's modes. OFF: no `next` has found `h3` chosen yet, so nothing is timed. */
326
+ const H3_CLOCK_OFF: i32 = -1
327
+ /** IDLE: no request in hand; timed by `idleTimeout`. */
328
+ const H3_CLOCK_IDLE: i32 = 0
329
+ /** HEAD: a request short of its head; timed by `headTimeout` and `idleTimeout`. */
330
+ const H3_CLOCK_HEAD: i32 = 1
331
+ /** BUSY: every request in hand past its head; not timed. */
332
+ const H3_CLOCK_BUSY: i32 = 2
333
+
320
334
  /** What `requestFrame` answers when it read a frame header and the stream has more to read. */
321
335
  const H3_CONTINUE: i32 = -1
322
336
 
@@ -364,11 +378,35 @@ export class Http3Config {
364
378
  * DATAGRAM frames; at 0 it must not (RFC 9297 §2.1.1).
365
379
  */
366
380
  webtransportSessions: i32 = 0
381
+ /**
382
+ * The milliseconds a connection may go without progress while no request
383
+ * is in hand before `clock` closes it (H3-5), 1 to 2^31 - 1. Progress is
384
+ * the connection starting, a request's head arriving whole, and the last
385
+ * request in hand finishing. With no request open the close is H3_NO_ERROR;
386
+ * with one still short of its head it is H3_EXCESSIVE_LOAD.
387
+ */
388
+ idleTimeout: i32 = 60000
389
+ /**
390
+ * The milliseconds a request's head may take to arrive whole, from its
391
+ * first byte or the last head before it, before `clock` closes the
392
+ * connection with H3_EXCESSIVE_LOAD (H3-5), 1 to 2^31 - 1. A body that
393
+ * trickles after its head is the program's to time, as a long response is.
394
+ */
395
+ headTimeout: i32 = 30000
367
396
  /**
368
397
  * Whether `:protocol` is accepted (RFC 9220's extended CONNECT), and
369
398
  * SETTINGS_ENABLE_CONNECT_PROTOCOL sent. WebTransport turns it on.
370
399
  */
371
400
  extendedConnect: boolean = false
401
+ /**
402
+ * How many requests may be reset before the connection is closed with
403
+ * H3_EXCESSIVE_LOAD, net of those this side answered: each request the
404
+ * client resets or stops, and each this side resets for a stream error or
405
+ * answers with a 431, costs one; each response this side ends with a FIN
406
+ * earns one back, up to this. Rapid reset (CVE-2023-44487) and MadeYouReset
407
+ * (CVE-2025-8671) over HTTP/3.
408
+ */
409
+ resetBudget: i32 = 100
372
410
  }
373
411
 
374
412
  /** Panics unless `value` lies in `[low, high]`: a cap out of range is the program's mistake. */
@@ -539,6 +577,9 @@ export class Http3Connection {
539
577
  lastStreamId: i64 = 0
540
578
  /** H3_STREAM's session ID. */
541
579
  session: i64 = -1
580
+ /** When `clock` last saw progress, and when it first saw a head waiting since; -1 for not yet. */
581
+ private clockSince: i64 = -1
582
+ private headSince: i64 = -1
542
583
  /**
543
584
  * The QUIC stream slot of the last event's stream: still right when the read that made the event freed it.
544
585
  * @internal Read by `webtransport.ts` to find its own per-slot state; not for callers.
@@ -575,9 +616,18 @@ export class Http3Connection {
575
616
  * with `content-length`, a stream that ends before its HEADERS, a trailer section too large.
576
617
  */
577
618
  streamErrors: i32 = 0
619
+ /** What is left of `config.resetBudget`. */
620
+ private resetCredit: i32 = 0
578
621
  private againHead: i32 = 0
579
622
  private againCount: i32 = 0
580
623
  private waitCount: i32 = 0
624
+ /**
625
+ * The heads delivered, wrapping, and the count and `H3_CLOCK_*` mode `clock` last saw. The mode is
626
+ * OFF (-1) from construction and `restart` until `next` finds `h3` chosen and sets it to IDLE.
627
+ */
628
+ private heads: i32 = 0
629
+ private clockHeads: i32 = 0
630
+ private clockMode: i32 = -1
581
631
  /** How many times `restart` ran: a layer above that keeps state per connection compares it to know when to forget its own. */
582
632
  generation: i32 = 0
583
633
  /** Whether the client's SETTINGS have arrived. */
@@ -620,6 +670,9 @@ export class Http3Connection {
620
670
  http3CheckCap("the QUIC unidirectional stream limit", toI32(quic.streams.maxUni), 3, 1024)
621
671
  http3CheckCap("the QUIC local stream limit", toI32(quic.streams.localStreams), 3, 1024)
622
672
  http3CheckCap("webtransportSessions", config.webtransportSessions, 0, H3_MAX_SESSIONS)
673
+ http3CheckCap("resetBudget", config.resetBudget, 0, 2147483647)
674
+ http3CheckCap("idleTimeout", config.idleTimeout, 1, 2147483647)
675
+ http3CheckCap("headTimeout", config.headTimeout, 1, 2147483647)
623
676
  // RFC 9297 §2.1.1: QUIC datagrams carry HTTP datagrams, which only WebTransport uses here.
624
677
  const datagrams: boolean = quic.datagramsIn.capacity() > 0
625
678
  if (config.webtransportSessions > 0 && !datagrams) {
@@ -737,10 +790,15 @@ export class Http3Connection {
737
790
  this.errorCode = 0
738
791
  this.lastStreamId = 0
739
792
  this.session = -1
793
+ this.clockSince = -1
794
+ this.headSince = -1
740
795
  this.slot = -1
741
796
  this.againHead = 0
742
797
  this.againCount = 0
743
798
  this.waitCount = 0
799
+ this.heads = 0
800
+ this.clockHeads = 0
801
+ this.clockMode = H3_CLOCK_OFF
744
802
  this.generation = this.generation < 2147483647 ? this.generation + 1 : H3_ONE
745
803
  this.dataStart = 0
746
804
  this.dataLength = 0
@@ -750,6 +808,7 @@ export class Http3Connection {
750
808
  this.rejected = 0
751
809
  this.tooLarge = 0
752
810
  this.streamErrors = 0
811
+ this.resetCredit = this.config.resetBudget
753
812
  this.settingsSeen = false
754
813
  this.resetByPeer = false
755
814
  }
@@ -782,6 +841,75 @@ export class Http3Connection {
782
841
  return this.goawayId >= 0 && this.live === 0 && streams.peerBidiClosed >= streams.peerBidiOpened
783
842
  }
784
843
 
844
+ /**
845
+ * Runs the connection's clock (H3-5): closes the QUIC connection once it
846
+ * has gone `idleTimeout` without progress while no request is in hand, or a
847
+ * request's head has waited `headTimeout`, and otherwise answers when to
848
+ * look again. `Http3Server` calls it on every flush of the slot; a program
849
+ * driving a connection itself calls it after each `next` and when the
850
+ * time it answered comes. It allocates nothing. The clock starts on the
851
+ * first `next` after the handshake chose `h3`, whether or not the client's
852
+ * MAX_STREAMS has let this side's own streams open yet: a connection the
853
+ * program runs another protocol on, and never asks for an event, is left
854
+ * to QUIC's own idle timeout.
855
+ *
856
+ * @param now The current millisecond on the caller's monotonic clock.
857
+ * @returns The millisecond the clock next needs a look, or -1 when it needs none: no `next` has
858
+ * found `h3` chosen yet, every request in hand is past its head, or the connection is closed (by this call too).
859
+ */
860
+ clock(now: i64): i64 {
861
+ if (this.clockMode === H3_CLOCK_OFF || this.quic.closed()) {
862
+ return -1
863
+ }
864
+ let mode: i32 = H3_CLOCK_BUSY
865
+ if (this.live === 0) {
866
+ mode = H3_CLOCK_IDLE
867
+ } else if (this.awaitsHead()) {
868
+ mode = H3_CLOCK_HEAD
869
+ }
870
+ const finished: boolean = this.clockMode === H3_CLOCK_BUSY && mode !== H3_CLOCK_BUSY
871
+ if (this.clockSince < 0 || this.heads !== this.clockHeads || finished) {
872
+ this.clockSince = now
873
+ this.clockHeads = this.heads
874
+ this.headSince = -1
875
+ }
876
+ if (mode !== H3_CLOCK_HEAD) {
877
+ this.headSince = -1
878
+ } else if (this.headSince < 0) {
879
+ this.headSince = now
880
+ }
881
+ this.clockMode = mode
882
+ if (mode === H3_CLOCK_BUSY) {
883
+ return -1
884
+ }
885
+ let due: i64 = this.clockSince + toI64(this.config.idleTimeout)
886
+ if (mode === H3_CLOCK_HEAD) {
887
+ due = http3Min(due, this.headSince + toI64(this.config.headTimeout))
888
+ }
889
+ if (now < due) {
890
+ return due
891
+ }
892
+ this.quic.close(mode === H3_CLOCK_HEAD ? H3_EXCESSIVE_LOAD : H3_NO_ERROR)
893
+ return -1
894
+ }
895
+
896
+ /** Whether a request stream the client opened, counted in `live`, has not yet given its head. */
897
+ private awaitsHead(): boolean {
898
+ const skip: i32 = H3_FLAG_DELIVERED | H3_FLAG_WT
899
+ for (let k: i32 = 0; k < toI32(this.slotId.length); k++) {
900
+ const flags: i32 = this.flags[k]
901
+ if (
902
+ this.phase[k] === H3_PHASE_HEADERS &&
903
+ (flags & H3_FLAG_COUNTED) !== 0 &&
904
+ (flags & skip) === 0 &&
905
+ http3IsRequest(this.slotId[k])
906
+ ) {
907
+ return true
908
+ }
909
+ }
910
+ return false
911
+ }
912
+
785
913
  /** Writes `buf[0 .. n)` to this side's new stream `id` whole, as its first bytes; answers whether it took them. */
786
914
  private writeNew(id: i64, buf: u8[], n: i32): boolean {
787
915
  return n > 0 && this.quic.streamWrite(id, buf, H3_ZERO, n, false) === n
@@ -955,6 +1083,12 @@ export class Http3Connection {
955
1083
  this.quic.fail(H3_NO_APPLICATION_PROTOCOL, toI64(0))
956
1084
  return this.fail(H3_NO_APPLICATION_PROTOCOL)
957
1085
  }
1086
+ // The clock runs from here, not from start() finishing: a client
1087
+ // whose MAX_STREAMS keeps this side's unidirectional streams unopened
1088
+ // is still served its requests, so it is still timed.
1089
+ if (this.clockMode === H3_CLOCK_OFF) {
1090
+ this.clockMode = H3_CLOCK_IDLE
1091
+ }
958
1092
  this.start()
959
1093
  }
960
1094
  while (this.state !== H3_STATE_FAILED) {
@@ -1643,6 +1777,10 @@ export class Http3Connection {
1643
1777
  // The client stopped the response (QUIC answered with RESET_STREAM, §3.5 of RFC 9000).
1644
1778
  const told: boolean = (this.flags[k] & H3_FLAG_DELIVERED) !== 0
1645
1779
  this.mark(k, H3_FLAG_SEND_DONE)
1780
+ const charged: i32 = this.chargeReset(k)
1781
+ if (charged !== H3_NEED_MORE) {
1782
+ return charged
1783
+ }
1646
1784
  if (told) {
1647
1785
  if (phase !== H3_PHASE_DONE) {
1648
1786
  this.phase[k] = H3_PHASE_DISCARD
@@ -1863,6 +2001,7 @@ export class Http3Connection {
1863
2001
  this.contentLength[k] = this.fields.contentLength
1864
2002
  this.phase[k] = H3_PHASE_BODY
1865
2003
  this.flags[k] = this.flags[k] | H3_FLAG_DELIVERED
2004
+ this.heads = this.heads < 2147483647 ? this.heads + 1 : H3_ZERO
1866
2005
  this.stream = id
1867
2006
  return H3_REQUEST
1868
2007
  }
@@ -1922,6 +2061,10 @@ export class Http3Connection {
1922
2061
  if (n === QUIC_STREAM_ERR_RESET) {
1923
2062
  // §4.1.1: a response to a request that will not finish is abandoned too.
1924
2063
  this.abandon(k, id, resetCode === H3_REQUEST_CANCELLED ? H3_REQUEST_CANCELLED : H3_REQUEST_INCOMPLETE)
2064
+ const charged: i32 = this.chargeReset(k)
2065
+ if (charged !== H3_NEED_MORE) {
2066
+ return charged
2067
+ }
1925
2068
  return delivered ? this.resetEvent(id, resetCode, true) : this.dropped(id)
1926
2069
  }
1927
2070
  if (n !== QUIC_STREAM_END) {
@@ -1931,13 +2074,15 @@ export class Http3Connection {
1931
2074
  // §4.1.1: the stream ended before a whole request.
1932
2075
  this.abandon(k, id, H3_REQUEST_INCOMPLETE)
1933
2076
  this.streamErrors = h3Count(this.streamErrors)
1934
- return this.dropped(id)
2077
+ const charged: i32 = this.chargeReset(k)
2078
+ return charged !== H3_NEED_MORE ? charged : this.dropped(id)
1935
2079
  }
1936
2080
  if (this.contentLength[k] >= 0 && this.dataTotal[k] !== this.contentLength[k]) {
1937
2081
  // §4.1.2: the body is shorter than its content-length.
1938
2082
  this.abandon(k, id, H3_MESSAGE_ERROR)
1939
2083
  this.streamErrors = h3Count(this.streamErrors)
1940
- return this.resetEvent(id, H3_MESSAGE_ERROR, false)
2084
+ const charged: i32 = this.chargeReset(k)
2085
+ return charged !== H3_NEED_MORE ? charged : this.resetEvent(id, H3_MESSAGE_ERROR, false)
1941
2086
  }
1942
2087
  this.stream = id
1943
2088
  return H3_END
@@ -1957,12 +2102,39 @@ export class Http3Connection {
1957
2102
  this.abandon(k, id, code)
1958
2103
  this.quic.streamStopSending(id, code)
1959
2104
  this.phase[k] = H3_PHASE_DISCARD
2105
+ const charged: i32 = this.chargeReset(k)
2106
+ if (charged !== H3_NEED_MORE) {
2107
+ return charged
2108
+ }
1960
2109
  if ((this.flags[k] & H3_FLAG_DELIVERED) !== 0) {
1961
2110
  return this.resetEvent(id, code, false)
1962
2111
  }
1963
2112
  return this.dropped(id)
1964
2113
  }
1965
2114
 
2115
+ /**
2116
+ * Charges request `k` to the reset budget, once whatever reset it: a client
2117
+ * that cannot reset many requests itself provokes this side into it
2118
+ * (MadeYouReset, CVE-2025-8671). Answers H3_NEED_MORE, or once the budget
2119
+ * is spent the connection error H3_EXCESSIVE_LOAD (§10.5).
2120
+ */
2121
+ private chargeReset(k: i32): i32 {
2122
+ if ((this.flags[k] & H3_FLAG_CHARGED) !== 0) {
2123
+ return H3_NEED_MORE
2124
+ }
2125
+ this.flags[k] = this.flags[k] | H3_FLAG_CHARGED
2126
+ this.resetCredit = this.resetCredit - 1
2127
+ return this.resetCredit < 0 ? this.fail(H3_EXCESSIVE_LOAD) : H3_NEED_MORE
2128
+ }
2129
+
2130
+ /** This side ended request `k`'s response with a FIN: its send side is done, and a request the program answered earns the reset budget one back. */
2131
+ private answered(k: i32): void {
2132
+ this.mark(k, H3_FLAG_SEND_DONE)
2133
+ if ((this.flags[k] & H3_FLAG_DELIVERED) !== 0 && this.resetCredit < this.config.resetBudget) {
2134
+ this.resetCredit = this.resetCredit + 1
2135
+ }
2136
+ }
2137
+
1966
2138
  /** H3_DROPPED for request stream `id` with WebTransport on, which waits on such a stream; H3_NEED_MORE without. */
1967
2139
  private dropped(id: i64): i32 {
1968
2140
  if (!this.webtransport) {
@@ -2000,7 +2172,8 @@ export class Http3Connection {
2000
2172
  }
2001
2173
  this.quic.streamStopSending(id, H3_NO_ERROR)
2002
2174
  this.phase[k] = H3_PHASE_DISCARD
2003
- return this.dropped(id)
2175
+ const charged: i32 = this.chargeReset(k)
2176
+ return charged !== H3_NEED_MORE ? charged : this.dropped(id)
2004
2177
  }
2005
2178
 
2006
2179
  /**
@@ -2191,7 +2364,7 @@ export class Http3Connection {
2191
2364
  this.quic.streamWrite(id, out, H3_ZERO, toI32(length), fin)
2192
2365
  this.flags[k] = this.flags[k] | H3_FLAG_HEADERS_SENT
2193
2366
  if (fin) {
2194
- this.mark(k, H3_FLAG_SEND_DONE)
2367
+ this.answered(k)
2195
2368
  }
2196
2369
  return 0
2197
2370
  }
@@ -2299,7 +2472,7 @@ export class Http3Connection {
2299
2472
  if (len === 0) {
2300
2473
  if (endStream && this.dataLeft[k] === 0) {
2301
2474
  this.quic.streamWrite(id, this.zeros, H3_ZERO, H3_ZERO, true)
2302
- this.mark(k, H3_FLAG_SEND_DONE)
2475
+ this.answered(k)
2303
2476
  }
2304
2477
  return 0
2305
2478
  }
@@ -2336,7 +2509,7 @@ export class Http3Connection {
2336
2509
  break
2337
2510
  }
2338
2511
  if (fin) {
2339
- this.mark(k, H3_FLAG_SEND_DONE)
2512
+ this.answered(k)
2340
2513
  }
2341
2514
  }
2342
2515
  return total > 0 ? total : H3_AGAIN
@@ -2388,7 +2561,7 @@ export class Http3Connection {
2388
2561
  this.quic.streamWrite(id, this.scratch, H3_ZERO, hp, false)
2389
2562
  this.quic.streamWrite(id, buf, off, len, fin)
2390
2563
  if (fin) {
2391
- this.mark(k, H3_FLAG_SEND_DONE)
2564
+ this.answered(k)
2392
2565
  }
2393
2566
  return 0
2394
2567
  }