@amritk/nish-aarch64-linux 0.38.0 → 0.39.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 (58) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +348 -37
  3. package/package.json +1 -1
  4. package/runtime/core/host.d.ts +11 -0
  5. package/runtime/core/host.mjs +35 -0
  6. package/runtime/nish.d.ts +301 -18
  7. package/runtime/nish.h +74 -10
  8. package/runtime/nish.mjs +69 -6
  9. package/runtime/node.mjs +117 -0
  10. package/runtime/runtime-aes.c +12 -4
  11. package/runtime/runtime-async.c +43 -10
  12. package/runtime/runtime-checksum.c +207 -15
  13. package/runtime/runtime-net.c +32 -3
  14. package/runtime/runtime-os.c +174 -27
  15. package/runtime/runtime-region.c +94 -10
  16. package/runtime/runtime-sandbox.c +377 -17
  17. package/runtime/runtime-sha256.c +11 -4
  18. package/runtime/runtime-wasm.c +15 -0
  19. package/runtime/runtime.c +5 -0
  20. package/scripts/build.sh +27 -8
  21. package/std/README.md +5 -4
  22. package/std/compress/checksum.ts +13 -0
  23. package/std/compress/deflate.ts +0 -2
  24. package/std/crypto/p256.ts +247 -74
  25. package/std/crypto/p384.ts +3 -1
  26. package/std/crypto/pki.ts +35 -9
  27. package/std/crypto/x25519.ts +153 -64
  28. package/std/crypto/x509.ts +113 -10
  29. package/std/json-tokenizer.ts +20 -0
  30. package/std/json-typed.ts +217 -8
  31. package/std/net/handler.ts +130 -16
  32. package/std/net/http1-codec.ts +499 -150
  33. package/std/net/http1-server.ts +495 -22
  34. package/std/net/http2-cleartext.ts +526 -0
  35. package/std/net/http2-tls.ts +48 -0
  36. package/std/net/http2.ts +84 -0
  37. package/std/net/postgres-async.ts +1304 -86
  38. package/std/net/postgres-carrier.ts +343 -10
  39. package/std/net/postgres.ts +122 -7
  40. package/std/net/prefork.ts +195 -11
  41. package/std/net/quic-datagram.ts +30 -0
  42. package/std/net/quic-listener.ts +38 -12
  43. package/std/net/quic-packet.ts +19 -6
  44. package/std/net/quic-recovery.ts +134 -19
  45. package/std/net/quic-stream.ts +174 -36
  46. package/std/net/quic.ts +418 -18
  47. package/std/net/router.ts +39 -17
  48. package/std/net/serve.ts +162 -58
  49. package/std/net/tls/codec.ts +110 -32
  50. package/std/net/tls/record-server.ts +37 -7
  51. package/std/net/tls-tcp.ts +56 -1
  52. package/std/net/tls.ts +196 -50
  53. package/std/net/websocket.ts +55 -18
  54. package/std/net/webtransport.ts +179 -1
  55. package/std/regex.ts +69 -47
  56. package/std/region.ts +62 -11
  57. package/std/secret.ts +26 -59
  58. package/std/text.ts +233 -0
@@ -111,11 +111,15 @@ export const QUIC_RECOVERY_PACKET_THRESHOLD: i64 = 3
111
111
  export const QUIC_RECOVERY_GRANULARITY: i64 = 1
112
112
  /** kInitialRtt (§6.2.2), in milliseconds: the round trip assumed before the first sample. */
113
113
  export const QUIC_RECOVERY_INITIAL_RTT: i64 = 333
114
- /** max_datagram_size (B.1): the 1200 bytes every datagram this server sends is at most. */
114
+ /**
115
+ * max_datagram_size (B.1) on a path nothing has been learnt of: the 1200
116
+ * bytes every datagram is at most until path MTU discovery validates more
117
+ * (`QuicRecovery.datagramSize`).
118
+ */
115
119
  export const QUIC_RECOVERY_DATAGRAM_SIZE: i64 = 1200
116
- /** kInitialWindow (§7.2): min(10 × 1200, max(14720, 2 × 1200)), so 12000 bytes. */
120
+ /** kInitialWindow (§7.2) at 1200 bytes: min(10 × 1200, max(14720, 2 × 1200)), so 12000 bytes. */
117
121
  export const QUIC_RECOVERY_INITIAL_WINDOW: i64 = 12000
118
- /** kMinimumWindow (§7.2): two datagrams, 2400 bytes. */
122
+ /** kMinimumWindow (§7.2) at 1200 bytes: two datagrams, 2400 bytes. */
119
123
  export const QUIC_RECOVERY_MINIMUM_WINDOW: i64 = 2400
120
124
  /** kPersistentCongestionThreshold (§7.6.1): lost packets spanning three probe timeouts collapse the window. */
121
125
  export const QUIC_RECOVERY_PERSISTENT_THRESHOLD: i64 = 3
@@ -143,6 +147,10 @@ const QUIC_RECOVERY_LOST: i32 = 3
143
147
  const QUIC_RECOVERY_LOST_NOW: i32 = 4
144
148
  /** The bit of a slot's state byte that says its packet was sent marked ECT(0), kept beside the state so a slot costs no second byte. */
145
149
  const QUIC_RECOVERY_MARKED: i32 = 128
150
+ /** The bit of a slot's state byte that says its packet was a path MTU probe, whose loss is not congestion (RFC 9000 §14.4). */
151
+ const QUIC_RECOVERY_PROBE: i32 = 64
152
+ /** The state bits of a slot's state byte, below the two flags. */
153
+ const QUIC_RECOVERY_STATE_BITS: i32 = 63
146
154
 
147
155
  /** The ECT(0) codepoint (RFC 3168), the only one this module asks a caller to mark with. */
148
156
  export const QUIC_RECOVERY_ECT0: i32 = 2
@@ -270,7 +278,7 @@ export class QuicSentPackets {
270
278
  if (s < 0 || s >= toI32(this.state.length)) {
271
279
  return QUIC_RECOVERY_FREE
272
280
  }
273
- return toI32(this.state[s]) & (QUIC_RECOVERY_MARKED - 1)
281
+ return toI32(this.state[s]) & QUIC_RECOVERY_STATE_BITS
274
282
  }
275
283
 
276
284
  /**
@@ -279,7 +287,7 @@ export class QuicSentPackets {
279
287
  */
280
288
  setState(s: i32, value: i32): void {
281
289
  if (s >= 0 && s < toI32(this.state.length)) {
282
- this.state[s] = toU8((toI32(this.state[s]) & QUIC_RECOVERY_MARKED) | value)
290
+ this.state[s] = toU8((toI32(this.state[s]) & (QUIC_RECOVERY_MARKED | QUIC_RECOVERY_PROBE)) | value)
283
291
  }
284
292
  }
285
293
 
@@ -340,7 +348,30 @@ export class QuicSentPackets {
340
348
  */
341
349
  setMarked(s: i32, value: boolean): void {
342
350
  if (s >= 0 && s < toI32(this.state.length)) {
343
- this.state[s] = toU8(this.stateAt(s) | (value ? QUIC_RECOVERY_MARKED : 0))
351
+ const kept: i32 = toI32(this.state[s]) & (QUIC_RECOVERY_STATE_BITS | QUIC_RECOVERY_PROBE)
352
+ this.state[s] = toU8(kept | (value ? QUIC_RECOVERY_MARKED : 0))
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Whether the packet in slot `s` was a path MTU probe (RFC 8899), whose
358
+ * loss says nothing of congestion.
359
+ *
360
+ * @param s A ring slot, as `onPacketSent`, `ackedSlot` or `lostSlot` answers it; any other index is safe.
361
+ * @returns True when `onProbeSent` marked it; false for an index outside the ring.
362
+ */
363
+ probeAt(s: i32): boolean {
364
+ return s >= 0 && s < toI32(this.state.length) && (toI32(this.state[s]) & QUIC_RECOVERY_PROBE) !== 0
365
+ }
366
+
367
+ /**
368
+ * Sets whether the packet in slot `s` was a path MTU probe.
369
+ * @internal Read in quic-recovery.ts outside the class.
370
+ */
371
+ setProbe(s: i32, value: boolean): void {
372
+ if (s >= 0 && s < toI32(this.state.length)) {
373
+ const kept: i32 = toI32(this.state[s]) & (QUIC_RECOVERY_STATE_BITS | QUIC_RECOVERY_MARKED)
374
+ this.state[s] = toU8(kept | (value ? QUIC_RECOVERY_PROBE : 0))
344
375
  }
345
376
  }
346
377
 
@@ -530,6 +561,15 @@ export class QuicRecovery {
530
561
  ptoCount: i32 = 0
531
562
  /** The space the last `onTimeout` acted on, or -1. */
532
563
  timeoutSpace: i32 = -1
564
+ /**
565
+ * max_datagram_size (B.1): the largest datagram the connection sends on
566
+ * this path, 1200 until path MTU discovery validates more. The initial and
567
+ * minimum windows and congestion avoidance's step are counted in it.
568
+ * `resetPath`, which the constructor runs, sets it to
569
+ * `QUIC_RECOVERY_DATAGRAM_SIZE`; a field's initializer
570
+ * must be a literal.
571
+ */
572
+ datagramSize: i64 = 1200
533
573
  /** How many congestion events reduced the window, and how many of them were persistent congestion. */
534
574
  congestionEvents: i32 = 0
535
575
  /**
@@ -599,6 +639,7 @@ export class QuicRecovery {
599
639
  * @internal Read by quic.ts.
600
640
  */
601
641
  resetPath(): void {
642
+ this.datagramSize = QUIC_RECOVERY_DATAGRAM_SIZE
602
643
  this.latestRtt = 0
603
644
  this.smoothedRtt = QUIC_RECOVERY_INITIAL_RTT
604
645
  this.rttVar = QUIC_RECOVERY_INITIAL_RTT / 2
@@ -612,6 +653,50 @@ export class QuicRecovery {
612
653
  this.pacerRtt = QUIC_RECOVERY_INITIAL_RTT * QUIC_RECOVERY_MICROS
613
654
  }
614
655
 
656
+ /**
657
+ * kInitialWindow (§7.2) for the current datagram size: min(10 × size,
658
+ * max(14720, 2 × size)).
659
+ *
660
+ * @returns The initial window in bytes: 12000 at 1200 bytes.
661
+ */
662
+ initialWindow(): i64 {
663
+ const ten: i64 = 10 * this.datagramSize
664
+ const floor: i64 = quicRecoveryMax(14720, 2 * this.datagramSize)
665
+ return ten < floor ? ten : floor
666
+ }
667
+
668
+ /**
669
+ * kMinimumWindow (§7.2) for the current datagram size: two datagrams.
670
+ *
671
+ * @returns The minimum window in bytes: 2400 at 1200 bytes.
672
+ */
673
+ minimumWindow(): i64 {
674
+ return 2 * this.datagramSize
675
+ }
676
+
677
+ /**
678
+ * Sets max_datagram_size to `size` bytes, as path MTU discovery validates
679
+ * a larger path or falls back from one (RFC 9002 §7.2). A window still at
680
+ * the old initial window becomes the new one, so a connection that has
681
+ * learnt nothing of the path yet starts from what the new size allows; a
682
+ * window below the new minimum is raised to it. A smaller size keeps the
683
+ * window as it is, and the pacer's held credit is cut to the new burst.
684
+ *
685
+ * @param size The new datagram size in bytes; anything under 1200 is taken as 1200.
686
+ */
687
+ setDatagramSize(size: i64): void {
688
+ const next: i64 = quicRecoveryMax(size, QUIC_RECOVERY_DATAGRAM_SIZE)
689
+ const atInitial: boolean = this.congestionWindow === this.initialWindow()
690
+ this.datagramSize = next
691
+ if (atInitial) {
692
+ this.congestionWindow = this.initialWindow()
693
+ }
694
+ this.congestionWindow = quicRecoveryMax(this.congestionWindow, this.minimumWindow())
695
+ if (this.pacerBudget > this.initialWindow()) {
696
+ this.pacerBudget = this.initialWindow()
697
+ }
698
+ }
699
+
615
700
  /**
616
701
  * Tests ECN again from the start, as on a new path (RFC 9000 §13.4.2).
617
702
  * Every packet still in flight loses its mark: those sent on the old
@@ -664,10 +749,10 @@ export class QuicRecovery {
664
749
  * Whether the window has room for another full-sized datagram (B.4). The
665
750
  * caller sends nothing ack-eliciting while it has not, except a probe.
666
751
  *
667
- * @returns True when the bytes in flight plus 1200 bytes fit the congestion window.
752
+ * @returns True when the bytes in flight plus `datagramSize` bytes fit the congestion window.
668
753
  */
669
754
  canSend(): boolean {
670
- return this.bytesInFlight() + QUIC_RECOVERY_DATAGRAM_SIZE <= this.congestionWindow
755
+ return this.bytesInFlight() + this.datagramSize <= this.congestionWindow
671
756
  }
672
757
 
673
758
  /**
@@ -703,6 +788,7 @@ export class QuicRecovery {
703
788
  sp.fineSent[s] = this.fineNow(now)
704
789
  sp.size[s] = size
705
790
  sp.setMarked(s, false)
791
+ sp.setProbe(s, false)
706
792
  sp.setState(s, QUIC_RECOVERY_SENT)
707
793
  sp.count = sp.count + 1
708
794
  sp.inFlight = sp.inFlight + 1
@@ -712,6 +798,22 @@ export class QuicRecovery {
712
798
  return s
713
799
  }
714
800
 
801
+ /**
802
+ * Marks the packet in `space`'s slot `slot`, just recorded by
803
+ * `onPacketSent`, as a path MTU probe (RFC 8899): it counts in flight and
804
+ * its acknowledgement grows the window, but its loss is no congestion
805
+ * event and no part of persistent congestion (RFC 9000 §14.4).
806
+ *
807
+ * @param space The space's index, as given to `onPacketSent`.
808
+ * @param slot The slot `onPacketSent` answered; -1 or any other index outside the ring does nothing.
809
+ */
810
+ onProbeSent(space: i32, slot: i32): void {
811
+ const sp: QuicSentPackets | null = this.space(space)
812
+ if (sp !== null) {
813
+ sp.setProbe(slot, true)
814
+ }
815
+ }
816
+
715
817
  /**
716
818
  * The ECN codepoint to mark the next packet with (RFC 9000 A.4): ECT(0)
717
819
  * for the first `QUIC_RECOVERY_ECN_TESTING_PACKETS` and, once the path is
@@ -1068,7 +1170,9 @@ export class QuicRecovery {
1068
1170
  }
1069
1171
  let newest: i64 = -1
1070
1172
  for (let k: i32 = 0; k < sp.lostCount; k += 1) {
1071
- newest = quicRecoveryMax(newest, sp.timeAt(sp.lostSlot(k)))
1173
+ if (!sp.probeAt(sp.lostSlot(k))) {
1174
+ newest = quicRecoveryMax(newest, sp.timeAt(sp.lostSlot(k)))
1175
+ }
1072
1176
  if (sp.markedAt(sp.lostSlot(k)) && this.ecnState !== QUIC_RECOVERY_ECN_CAPABLE) {
1073
1177
  this.ecnTestLost = this.ecnTestLost + 1
1074
1178
  }
@@ -1081,9 +1185,12 @@ export class QuicRecovery {
1081
1185
  ) {
1082
1186
  this.ecnState = QUIC_RECOVERY_ECN_FAILED
1083
1187
  }
1084
- this.congestionEvent(newest, now)
1085
- if (this.persistentCongestion(sp)) {
1086
- this.congestionWindow = QUIC_RECOVERY_MINIMUM_WINDOW
1188
+ // A lost path MTU probe is no sign of congestion (RFC 9000 §14.4): only the others count.
1189
+ if (newest >= 0) {
1190
+ this.congestionEvent(newest, now)
1191
+ }
1192
+ if (newest >= 0 && this.persistentCongestion(sp)) {
1193
+ this.congestionWindow = this.minimumWindow()
1087
1194
  this.recoveryStart = -1
1088
1195
  this.persistentCongestions = this.persistentCongestions + 1
1089
1196
  }
@@ -1109,8 +1216,17 @@ export class QuicRecovery {
1109
1216
  for (let k: i32 = 0; k < sp.count; k += 1) {
1110
1217
  const s: i32 = sp.slot(k)
1111
1218
  const state: i32 = sp.stateAt(s)
1219
+ // An acknowledged probe still breaks the run; only a lost one says nothing (RFC 9000 §14.4).
1220
+ if (state === QUIC_RECOVERY_ACKED) {
1221
+ first = -1
1222
+ fresh = false
1223
+ continue
1224
+ }
1225
+ if (sp.probeAt(s)) {
1226
+ continue
1227
+ }
1112
1228
  const lost: boolean = state === QUIC_RECOVERY_LOST || state === QUIC_RECOVERY_LOST_NOW
1113
- if (state === QUIC_RECOVERY_ACKED || (lost && sp.timeAt(s) < this.firstSampleTime)) {
1229
+ if (lost && sp.timeAt(s) < this.firstSampleTime) {
1114
1230
  first = -1
1115
1231
  fresh = false
1116
1232
  continue
@@ -1141,13 +1257,13 @@ export class QuicRecovery {
1141
1257
  }
1142
1258
  this.recoveryStart = now
1143
1259
  this.ssthresh = this.congestionWindow / 2
1144
- this.congestionWindow = quicRecoveryMax(this.ssthresh, QUIC_RECOVERY_MINIMUM_WINDOW)
1260
+ this.congestionWindow = quicRecoveryMax(this.ssthresh, this.minimumWindow())
1145
1261
  this.congestionEvents = this.congestionEvents + 1
1146
1262
  }
1147
1263
 
1148
1264
  /**
1149
1265
  * OnPacketAcked (B.5) for slot `s`: in slow start the window grows by the
1150
- * packet's size, in congestion avoidance by 1200 × size / window; not at
1266
+ * packet's size, in congestion avoidance by datagramSize × size / window; not at
1151
1267
  * all for a packet sent during recovery, or when `inUse` says the window
1152
1268
  * was not being used (§7.8).
1153
1269
  */
@@ -1159,8 +1275,7 @@ export class QuicRecovery {
1159
1275
  if (this.congestionWindow < this.ssthresh) {
1160
1276
  this.congestionWindow = this.congestionWindow + size
1161
1277
  } else {
1162
- this.congestionWindow =
1163
- this.congestionWindow + (QUIC_RECOVERY_DATAGRAM_SIZE * size) / this.congestionWindow
1278
+ this.congestionWindow = this.congestionWindow + (this.datagramSize * size) / this.congestionWindow
1164
1279
  }
1165
1280
  }
1166
1281
 
@@ -1369,8 +1484,8 @@ export class QuicRecovery {
1369
1484
  const rtt: i64 = quicRecoveryMax(this.pacerRtt, 1)
1370
1485
  const earned: i64 = (elapsed * this.pacerWindow() * 5) / (rtt * 4)
1371
1486
  this.pacerBudget = this.pacerBudget + earned
1372
- if (this.pacerBudget > QUIC_RECOVERY_INITIAL_WINDOW) {
1373
- this.pacerBudget = QUIC_RECOVERY_INITIAL_WINDOW
1487
+ if (this.pacerBudget > this.initialWindow()) {
1488
+ this.pacerBudget = this.initialWindow()
1374
1489
  }
1375
1490
  this.pacerTime = at
1376
1491
  }
@@ -116,6 +116,7 @@ import {
116
116
  quicStreamOverhead,
117
117
  } from "nish/net/quic-frame"
118
118
  import { BufferPool } from "nish/net/buffer-pool"
119
+ import { copyBytes } from "nish/bytes"
119
120
 
120
121
  // ---- What the stream calls answer -----------------------------------------------
121
122
 
@@ -248,6 +249,118 @@ export const quicStreamSetBit = (bits: u8[], k: i32, on: boolean): void => {
248
249
  bits[at] = toU8(on ? old | mask : old & ~mask)
249
250
  }
250
251
 
252
+ /**
253
+ * Sets or clears bits `[from, to)` of `bits`: the ragged ends a bit at a
254
+ * time, the whole bytes between with one `fill`. Bits outside the array are
255
+ * left alone.
256
+ */
257
+ const quicStreamSetBits = (bits: u8[], from: i32, to: i32, on: boolean): void => {
258
+ // Compared a byte at a time, so a bitmap of 2^28 bytes or more cannot wrap `bits.length << 3`
259
+ // negative: the shift runs only when `to` is past the array, so the bit count fits below `to`.
260
+ const bytes: i32 = toI32(bits.length)
261
+ let k: i32 = from < 0 ? 0 : from
262
+ const top: i32 = to >> 3 < bytes ? to : bytes << 3
263
+ while (k < top && (k & 7) !== 0) {
264
+ quicStreamSetBit(bits, k, on)
265
+ k = k + 1
266
+ }
267
+ const whole: i32 = top & ~7
268
+ if (k < whole) {
269
+ bits.fill(on ? 255 : 0, k >> 3, whole >> 3)
270
+ k = whole
271
+ }
272
+ while (k < top) {
273
+ quicStreamSetBit(bits, k, on)
274
+ k = k + 1
275
+ }
276
+ }
277
+
278
+ /**
279
+ * The first bit in `[from, to)` of `bits` that is not `on`, or `to` when
280
+ * they all are; a whole byte of them is passed over at once. A bit outside
281
+ * the array reads as clear.
282
+ */
283
+ const quicStreamScan = (bits: u8[], from: i32, to: i32, on: boolean): i32 => {
284
+ const full: i32 = on ? 255 : 0
285
+ let k: i32 = from
286
+ while (k < to && (k & 7) !== 0) {
287
+ if (quicStreamBit(bits, k) !== on) {
288
+ return k
289
+ }
290
+ k = k + 1
291
+ }
292
+ const bytes: i32 = toI32(bits.length)
293
+ // As in `quicStreamSetBits`, the shift runs only when `to` is past the array, so it cannot wrap.
294
+ const whole: i32 = (to >> 3 < bytes ? to : bytes << 3) & ~7
295
+ let at: i32 = k >> 3
296
+ // `at` is `k >> 3` and `whole` is within the array, so the bounds on `at` hold whenever `k`'s do; they
297
+ // are what the bounds pass reads to drop the check on `bits[at]`.
298
+ while (at >= 0 && at < bytes && k < whole && toI32(bits[at]) === full) {
299
+ k = k + 8
300
+ at = at + 1
301
+ }
302
+ while (k < to) {
303
+ if (quicStreamBit(bits, k) !== on) {
304
+ return k
305
+ }
306
+ k = k + 1
307
+ }
308
+ return to
309
+ }
310
+
311
+ /**
312
+ * The first of `count` bytes of a ring of `size` from index `slot`: they run
313
+ * to the ring's end at most, and the rest wrap to index 0.
314
+ */
315
+ const quicStreamFirstRun = (size: i32, slot: i32, count: i32): i32 =>
316
+ count < size - slot ? count : size - slot
317
+
318
+ /** Sets or clears the bits of `count` ring indexes from `slot`, wrapping at `size`: at most two runs. */
319
+ const quicStreamMarkRing = (bits: u8[], size: i32, slot: i32, count: i32, on: boolean): void => {
320
+ const first: i32 = quicStreamFirstRun(size, slot, count)
321
+ quicStreamSetBits(bits, slot, slot + first, on)
322
+ if (count > first) {
323
+ quicStreamSetBits(bits, 0, count - first, on)
324
+ }
325
+ }
326
+
327
+ /** How many of `count` ring indexes from `slot`, wrapping at `size`, have their bit `on` before the first that does not. */
328
+ const quicStreamRingRun = (bits: u8[], size: i32, slot: i32, count: i32, on: boolean): i32 => {
329
+ const first: i32 = quicStreamFirstRun(size, slot, count)
330
+ const got: i32 = quicStreamScan(bits, slot, slot + first, on) - slot
331
+ if (got < first || count <= first) {
332
+ return got
333
+ }
334
+ return first + quicStreamScan(bits, 0, count - first, on)
335
+ }
336
+
337
+ /**
338
+ * Copies `count` bytes of `src` from `from` into the ring `ring` at index `slot`, wrapping at its end: at most two slices.
339
+ * Every caller passes `slot` from `QuicStream.at`, so below the ring's length, and a `count` of a ring at most:
340
+ * `onStream` keeps a frame's last ring's worth, and `write` clamps to `room()`. So both slices are in range,
341
+ * and `copyBytes` panicking on one that is not marks a broken invariant, not a byte to skip.
342
+ */
343
+ const quicStreamRingIn = (ring: u8[], slot: i32, src: u8[], from: i32, count: i32): void => {
344
+ const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
345
+ copyBytes(ring, slot, src, from, first)
346
+ if (count > first) {
347
+ copyBytes(ring, 0, src, from + first, count - first)
348
+ }
349
+ }
350
+
351
+ /**
352
+ * Copies `count` bytes of the ring `ring` from index `slot`, wrapping at its end, into `dst` at `at`: at most two slices.
353
+ * `read` passes a `count` of at most what is contiguous past `recvRead`, never more than a ring, so, as for
354
+ * `quicStreamRingIn`, both slices are in range.
355
+ */
356
+ const quicStreamRingOut = (dst: u8[], at: i32, ring: u8[], slot: i32, count: i32): void => {
357
+ const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
358
+ copyBytes(dst, at, ring, slot, first)
359
+ if (count > first) {
360
+ copyBytes(dst, at + first, ring, 0, count - first)
361
+ }
362
+ }
363
+
251
364
  /** The smaller of two `i64`s. */
252
365
  const quicStreamMin = (a: i64, b: i64): i64 => (a < b ? a : b)
253
366
 
@@ -1441,22 +1554,35 @@ export class QuicStreams {
1441
1554
  this.borrowRecv(stream)
1442
1555
  }
1443
1556
  const size: i32 = toI32(stream.recvBuf.length)
1444
- for (let k2: i32 = 0; k2 < length; k2 += 1) {
1445
- const position: i64 = offset + toI64(k2)
1446
- const src: i32 = from + k2
1447
- if (position >= stream.recvContiguous && src >= 0 && src < toI32(buf.length)) {
1448
- const slot: i32 = stream.at(position)
1449
- if (slot >= 0 && slot < size) {
1450
- stream.recvBuf[slot] = buf[src]
1451
- quicStreamSetBit(stream.recvHave, slot, true)
1452
- }
1557
+ // A side holds either no buffer (length 0) or one from the pools, whose buffers are the table's
1558
+ // `bufferSize` (`share` refuses pools of another size), so the size test fails only with no buffer.
1559
+ if (size > 0 && size === stream.size) {
1560
+ // The frame's bytes from the first not yet contiguous, inside `buf`; of
1561
+ // more than a buffer, the last buffer's worth, which a byte-wise copy
1562
+ // would have left there. Both clamps below are defensive: the credit
1563
+ // check above caps a frame's end at `recvRead` plus a ring, so no peer
1564
+ // frame reaches them, and they keep the copy in range if that changes.
1565
+ const skipped: i64 = quicStreamMin(stream.recvContiguous - offset, toI64(length))
1566
+ let low: i32 = skipped > 0 ? toI32(skipped) : 0
1567
+ if (low < -from) {
1568
+ low = -from
1569
+ }
1570
+ const high: i32 = length < toI32(buf.length) - from ? length : toI32(buf.length) - from
1571
+ if (high - low > size) {
1572
+ low = high - size
1573
+ }
1574
+ if (low < high) {
1575
+ const slot: i32 = stream.at(offset + toI64(low))
1576
+ quicStreamRingIn(stream.recvBuf, slot, buf, from + low, high - low)
1577
+ quicStreamMarkRing(stream.recvHave, size, slot, high - low, true)
1578
+ }
1579
+ if (stream.recvContiguous < stream.recvHighest) {
1580
+ const pending: i64 = stream.recvHighest - stream.recvContiguous
1581
+ const count: i32 = toI32(quicStreamMin(pending, toI64(size)))
1582
+ stream.recvContiguous =
1583
+ stream.recvContiguous +
1584
+ toI64(quicStreamRingRun(stream.recvHave, size, stream.at(stream.recvContiguous), count, true))
1453
1585
  }
1454
- }
1455
- while (
1456
- stream.recvContiguous < stream.recvHighest &&
1457
- quicStreamBit(stream.recvHave, stream.at(stream.recvContiguous))
1458
- ) {
1459
- stream.recvContiguous = stream.recvContiguous + 1
1460
1586
  }
1461
1587
  if (stream.recvFinal >= 0 && stream.recvContiguous === stream.recvFinal) {
1462
1588
  stream.recvState = QUIC_RECV_DATA_RECVD
@@ -1686,12 +1812,9 @@ export class QuicStreams {
1686
1812
  if (n > 0) {
1687
1813
  this.borrowSend(stream)
1688
1814
  }
1689
- const size: i32 = toI32(stream.sendBuf.length)
1690
- for (let j: i32 = 0; j < n; j += 1) {
1691
- const slot: i32 = stream.at(stream.sendEnd + toI64(j))
1692
- if (slot >= 0 && slot < size && from + j >= 0 && from + j < toI32(buf.length)) {
1693
- stream.sendBuf[slot] = buf[from + j]
1694
- }
1815
+ // No buffer (length 0) is the one way this fails: a pool buffer is always `bufferSize` bytes.
1816
+ if (n > 0 && toI32(stream.sendBuf.length) === stream.size) {
1817
+ quicStreamRingIn(stream.sendBuf, stream.at(stream.sendEnd), buf, from, n)
1695
1818
  }
1696
1819
  stream.sendEnd = stream.sendEnd + toI64(n)
1697
1820
  this.writtenTotal = this.writtenTotal + toI64(n)
@@ -1745,12 +1868,11 @@ export class QuicStreams {
1745
1868
  }
1746
1869
  const n: i32 = toI64(length) < available ? length : toI32(available)
1747
1870
  const size: i32 = toI32(stream.recvBuf.length)
1748
- for (let j: i32 = 0; j < n; j += 1) {
1749
- const slot: i32 = stream.at(stream.recvRead + toI64(j))
1750
- if (slot >= 0 && slot < size && at + j >= 0 && at + j < toI32(buf.length)) {
1751
- buf[at + j] = stream.recvBuf[slot]
1752
- quicStreamSetBit(stream.recvHave, slot, false)
1753
- }
1871
+ // With bytes owed, the buffer is held (the test above), and a held buffer is `bufferSize` bytes.
1872
+ if (n > 0 && size === stream.size) {
1873
+ const slot: i32 = stream.at(stream.recvRead)
1874
+ quicStreamRingOut(buf, at, stream.recvBuf, slot, n)
1875
+ quicStreamMarkRing(stream.recvHave, size, slot, n, false)
1754
1876
  }
1755
1877
  stream.recvRead = stream.recvRead + toI64(n)
1756
1878
  if (n > 0) {
@@ -1919,8 +2041,10 @@ export class QuicStreams {
1919
2041
  private putResend(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
1920
2042
  if (stream.resendLow >= 0) {
1921
2043
  let low: i64 = stream.resendLow < stream.sendBase ? stream.sendBase : stream.resendLow
1922
- while (low < stream.resendHigh && quicStreamBit(stream.sendAcked, stream.at(low))) {
1923
- low = low + 1
2044
+ if (low < stream.resendHigh && stream.size > 0) {
2045
+ const span: i64 = stream.resendHigh - low
2046
+ const count: i32 = toI32(quicStreamMin(span, toI64(stream.size)))
2047
+ low = low + toI64(quicStreamRingRun(stream.sendAcked, stream.size, stream.at(low), count, true))
1924
2048
  }
1925
2049
  const want: i32 = toI32(stream.resendHigh - low)
1926
2050
  if (want <= 0) {
@@ -2197,14 +2321,28 @@ export class QuicStreams {
2197
2321
  // Done or reset: its buffer may be back in the pool, and nothing is owed.
2198
2322
  return
2199
2323
  }
2200
- const top: i64 = offset + toI64(length) < stream.sendEnd ? offset + toI64(length) : stream.sendEnd
2201
- for (let o: i64 = offset < stream.sendBase ? stream.sendBase : offset; o < top; o += 1) {
2202
- quicStreamSetBit(stream.sendAcked, stream.at(o), true)
2203
- }
2204
2324
  const before: i64 = stream.sendBase
2205
- while (stream.sendBase < stream.sendNext && quicStreamBit(stream.sendAcked, stream.at(stream.sendBase))) {
2206
- quicStreamSetBit(stream.sendAcked, stream.at(stream.sendBase), false)
2207
- stream.sendBase = stream.sendBase + 1
2325
+ const size: i32 = stream.size
2326
+ if (size > 0) {
2327
+ const top: i64 = offset + toI64(length) < stream.sendEnd ? offset + toI64(length) : stream.sendEnd
2328
+ const low: i64 = offset < stream.sendBase ? stream.sendBase : offset
2329
+ // The bytes in flight are fewer than a buffer, so a chunk is too.
2330
+ if (low < top) {
2331
+ quicStreamMarkRing(
2332
+ stream.sendAcked,
2333
+ size,
2334
+ stream.at(low),
2335
+ toI32(quicStreamMin(top - low, toI64(size))),
2336
+ true
2337
+ )
2338
+ }
2339
+ if (stream.sendBase < stream.sendNext) {
2340
+ const slot: i32 = stream.at(stream.sendBase)
2341
+ const count: i32 = toI32(quicStreamMin(stream.sendNext - stream.sendBase, toI64(size)))
2342
+ const done: i32 = quicStreamRingRun(stream.sendAcked, size, slot, count, true)
2343
+ quicStreamMarkRing(stream.sendAcked, size, slot, done, false)
2344
+ stream.sendBase = stream.sendBase + toI64(done)
2345
+ }
2208
2346
  }
2209
2347
  if (fin) {
2210
2348
  stream.finAcked = true