@amritk/nish-x86_64-linux 0.44.0 → 0.45.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 (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
@@ -49,7 +49,7 @@ import {
49
49
  QUIC_ERROR_PROTOCOL_VIOLATION,
50
50
  QUIC_RESET_TOKEN_SIZE,
51
51
  } from "nish/net/quic-frame"
52
- import { QUIC_MAX_CID_LENGTH } from "nish/net/quic-packet"
52
+ import { QUIC_MAX_CID_LENGTH, QUIC_MAX_VARINT } from "nish/net/quic-packet"
53
53
 
54
54
  /** A typed zero: a bare literal is an `f64` under `--number-mode f64`. */
55
55
  const QUIC_CID_ZERO: i32 = 0
@@ -95,7 +95,10 @@ export class QuicCidEntry {
95
95
  */
96
96
  export const quicCidCopy = (to: u8[], from: u8[], at: i32, length: i32): void => {
97
97
  for (let k: i32 = 0; k < toI32(to.length); k += 1) {
98
- to[k] = k < length && at + k >= 0 && at + k < toI32(from.length) ? from[at + k] : toU8(0)
98
+ // Widened, so a sum past 2^31 - 1 lands outside `from` and reads as a missing byte.
99
+ const j: i64 = toI64(at) + toI64(k)
100
+ const i: i32 = j >= 0 && j < 2147483648 ? toI32(j) : -1
101
+ to[k] = k < length && i >= 0 && i < toI32(from.length) ? from[i] : toU8(0)
99
102
  }
100
103
  }
101
104
 
@@ -139,6 +142,19 @@ const quicCidClear = (entry: QuicCidEntry): void => {
139
142
  secureZero(entry.resetToken)
140
143
  }
141
144
 
145
+ /** `count` zeroed sequence numbers, none when `count` is below 1; `count` is below 2^32. */
146
+ const quicCidSequences = (count: i64): i64[] => {
147
+ if (count >= 0 && count <= 65535) {
148
+ return new Array<i64>(toU16(count))
149
+ }
150
+ const out: i64[] = []
151
+ // `count` is twice an i32 at most, below 2^32, so the unsigned counter reaches it before it wraps.
152
+ for (let k: u32 = 0; toI64(k) < count; k += 1) {
153
+ out.push(0)
154
+ }
155
+ return out
156
+ }
157
+
142
158
  /** `count` empty slots. */
143
159
  const quicCidSlots = (count: i32): QuicCidEntry[] => {
144
160
  const out: QuicCidEntry[] = []
@@ -184,7 +200,8 @@ export class QuicCidTable {
184
200
  const peerSlots: i32 = limit < 1 ? 1 : toI32(limit)
185
201
  this.local = quicCidSlots(localCapacity)
186
202
  this.peer = quicCidSlots(peerSlots)
187
- this.retirePending = new Array<i64>(peerSlots * 2)
203
+ // Widened, so twice a large `limit` cannot wrap.
204
+ this.retirePending = quicCidSequences(toI64(peerSlots) * 2)
188
205
  this.limit = limit
189
206
  }
190
207
 
@@ -206,21 +223,23 @@ export class QuicCidTable {
206
223
  /**
207
224
  * Issues `cid`, up to its first 20 bytes, as the next local ID, with its
208
225
  * 16-byte reset token (or none, empty, for sequence 0), and answers its
209
- * sequence number; -1 when every local slot is taken.
226
+ * sequence number; -1 when every local slot is taken or sequence
227
+ * numbers reach 2^62 - 1, the largest a varint carries.
210
228
  *
211
229
  * @param cid The ID's bytes, copied into the slot; past 20 bytes (`QUIC_MAX_CID_LENGTH`) the rest is
212
230
  * dropped.
213
231
  * @param resetToken The 16-byte stateless reset token, copied; any other length, empty included, means
214
232
  * none.
215
- * @returns The ID's sequence number, counting up from 0, or -1 when every local slot is taken.
233
+ * @returns The ID's sequence number, counting up from 0, or -1 when every local slot is taken or the
234
+ * sequence numbers are used up.
216
235
  */
217
236
  addLocal(cid: u8[], resetToken: u8[]): i64 {
218
237
  const k: i32 = quicCidFree(this.local)
219
- if (k < 0) {
238
+ const sequence: i64 = this.nextLocal
239
+ if (k < 0 || k >= toI32(this.local.length) || sequence < 0 || sequence >= QUIC_MAX_VARINT) {
220
240
  return -1
221
241
  }
222
242
  const entry: QuicCidEntry = this.local[k]
223
- const sequence: i64 = this.nextLocal
224
243
  const length: i32 = toI32(cid.length) > QUIC_MAX_CID_LENGTH ? QUIC_MAX_CID_LENGTH : toI32(cid.length)
225
244
  quicCidCopy(entry.cid, cid, 0, length)
226
245
  entry.length = length
@@ -230,7 +249,8 @@ export class QuicCidTable {
230
249
  entry.used = true
231
250
  // Sequence 0 travels in the long header's SCID, never in a frame.
232
251
  entry.announced = sequence === 0
233
- this.nextLocal = sequence + 1
252
+ // Below `QUIC_MAX_VARINT`, so one more cannot wrap.
253
+ this.nextLocal = toI64(toU64(sequence) + 1)
234
254
  return sequence
235
255
  }
236
256
 
@@ -240,13 +260,14 @@ export class QuicCidTable {
240
260
  * @returns The count of issued, not yet retired local IDs, from 0 to `localCapacity`.
241
261
  */
242
262
  activeLocal(): i32 {
243
- let n: i32 = 0
263
+ // At most one per slot, so the unsigned count cannot wrap.
264
+ let n: u32 = 0
244
265
  for (const entry of this.local) {
245
266
  if (entry.used) {
246
267
  n += 1
247
268
  }
248
269
  }
249
- return n
270
+ return toI32(n)
250
271
  }
251
272
 
252
273
  /**
@@ -314,13 +335,14 @@ export class QuicCidTable {
314
335
  * @returns The count of the peer's IDs not yet retired, from 0 to `limit`.
315
336
  */
316
337
  activePeer(): i32 {
317
- let n: i32 = 0
338
+ // At most one per slot, so the unsigned count cannot wrap.
339
+ let n: u32 = 0
318
340
  for (const entry of this.peer) {
319
341
  if (entry.used) {
320
342
  n += 1
321
343
  }
322
344
  }
323
- return n
345
+ return toI32(n)
324
346
  }
325
347
 
326
348
  /**
@@ -399,8 +421,10 @@ export class QuicCidTable {
399
421
  // Count what the frame would leave before changing anything, so a frame
400
422
  // refused for a limit leaves the table as it was.
401
423
  const priorTo: i64 = retirePriorTo > this.peerRetirePriorTo ? retirePriorTo : this.peerRetirePriorTo
402
- let active: i64 = sequence < priorTo ? 0 : 1
403
- let pending: i64 = toI64(this.retireCount) + (sequence < priorTo ? 1 : 0)
424
+ // Each count is at most one per peer slot and queued retirement plus this frame's ID, far below
425
+ // 2^32, so the unsigned sums cannot wrap.
426
+ let active: u32 = sequence < priorTo ? 0 : 1
427
+ let pending: u32 = toU32(this.retireCount) + (sequence < priorTo ? 1 : 0)
404
428
  for (const old of this.peer) {
405
429
  if (!old.used) {
406
430
  continue
@@ -412,9 +436,10 @@ export class QuicCidTable {
412
436
  }
413
437
  }
414
438
  if (
415
- active > this.limit ||
416
- pending > this.limit * 2 ||
417
- pending > toI64(toI32(this.retirePending.length))
439
+ toI64(active) > this.limit ||
440
+ // `pending > limit * 2` for whole numbers, without doubling `limit`.
441
+ toI64((pending + 1) >>> 1) > this.limit ||
442
+ toI64(pending) > toI64(toI32(this.retirePending.length))
418
443
  ) {
419
444
  return QUIC_ERROR_CONNECTION_ID_LIMIT
420
445
  }
@@ -472,7 +497,8 @@ export class QuicCidTable {
472
497
  currentPeer(): u8[] {
473
498
  const entry: QuicCidEntry | null = this.currentPeerEntry()
474
499
  const length: i32 = entry !== null ? entry.length : 0
475
- const out: u8[] = new Array<u8>(length)
500
+ // An entry's length is 0 to 20 (`QUIC_MAX_CID_LENGTH`), so it fits a u16.
501
+ const out: u8[] = new Array<u8>(toU16(length))
476
502
  if (entry !== null) {
477
503
  quicCidCopy(out, entry.cid, 0, length)
478
504
  }
@@ -487,20 +513,33 @@ export class QuicCidTable {
487
513
  */
488
514
  queueRetire(sequence: i64): void {
489
515
  const size: i32 = toI32(this.retirePending.length)
490
- for (let k: i32 = 0; k < this.retireCount; k += 1) {
491
- const at: i32 = (this.retireHead + k) % size
516
+ const count: i32 = this.retireCount
517
+ for (let k: i32 = 0; k < count; k += 1) {
518
+ const at: i32 = this.retireSlot(k)
492
519
  if (at >= 0 && at < size && this.retirePending[at] === sequence) {
493
520
  return
494
521
  }
495
522
  }
496
- if (this.retireCount >= size || size === 0) {
523
+ if (count < 0 || count >= size) {
497
524
  return
498
525
  }
499
- const at: i32 = (this.retireHead + this.retireCount) % size
526
+ const at: i32 = this.retireSlot(count)
500
527
  if (at >= 0 && at < size) {
501
528
  this.retirePending[at] = sequence
502
- this.retireCount = this.retireCount + 1
529
+ this.retireCount = count + 1
530
+ }
531
+ }
532
+
533
+ /**
534
+ * The ring slot `k` places after `retireHead`, or 0 for an empty ring. Unsigned, because the head and
535
+ * `k` are each below 2^31, so the sum cannot wrap.
536
+ */
537
+ private retireSlot(k: i32): i32 {
538
+ const size: u32 = toU32(this.retirePending.length)
539
+ if (size === 0) {
540
+ return 0
503
541
  }
542
+ return toI32((toU32(this.retireHead) + toU32(k)) % size)
504
543
  }
505
544
 
506
545
  /**
@@ -510,13 +549,14 @@ export class QuicCidTable {
510
549
  */
511
550
  takeRetire(): i64 {
512
551
  const size: i32 = toI32(this.retirePending.length)
513
- if (this.retireCount === 0 || size === 0) {
552
+ const count: i32 = this.retireCount
553
+ if (count <= 0 || size === 0) {
514
554
  return -1
515
555
  }
516
556
  const at: i32 = this.retireHead
517
557
  const first: i64 = at >= 0 && at < size ? this.retirePending[at] : -1
518
- this.retireHead = (at + 1) % size
519
- this.retireCount = this.retireCount - 1
558
+ this.retireHead = this.retireSlot(toI32(1))
559
+ this.retireCount = count - 1
520
560
  return first
521
561
  }
522
562
 
@@ -313,8 +313,11 @@ export const quicEncodeTransportParametersInto = (p: QuicTransportParameters, ou
313
313
  const quicParamsCopyInto = (to: u8[], data: u8[], from: i32, length: i32): void => {
314
314
  quicParamsEmpty(to)
315
315
  for (let k: i32 = 0; k < length; k += 1) {
316
- if (from + k >= 0 && from + k < toI32(data.length)) {
317
- to.push(data[from + k])
316
+ // `from` and `k` are both i32, so their sum fits an i64; a byte outside `data` is not copied.
317
+ const j: i64 = toI64(from) + toI64(k)
318
+ const i: i32 = j >= 0 && j < 2147483648 ? toI32(j) : -1
319
+ if (i >= 0 && i < toI32(data.length)) {
320
+ to.push(data[i])
318
321
  }
319
322
  }
320
323
  }
@@ -332,7 +335,8 @@ const quicParamsRefuse = (p: QuicTransportParameters, id: i64): QuicTransportPar
332
335
  * holds, otherwise.
333
336
  */
334
337
  const quicParamsVarintValue = (bytes: u8[], at: i32, length: i32): i64 => {
335
- const value: i64 = quicVarintRead(bytes, at, at + length)
338
+ // The caller's value window lies inside `bytes`, so its end fits an i32.
339
+ const value: i64 = quicVarintRead(bytes, at, toI32(toI64(at) + toI64(length)))
336
340
  if (value < 0 || quicVarintLength(bytes, at) !== length) {
337
341
  return -1
338
342
  }
@@ -504,18 +508,20 @@ export const quicParseTransportParametersInto = (
504
508
  if (id < 0) {
505
509
  return quicParamsRefuse(p, -1)
506
510
  }
507
- at = at + quicVarintLength(bytes, at)
511
+ // The varint just read ends inside `end`, so the advance fits an i32.
512
+ at = toI32(toI64(at) + toI64(quicVarintLength(bytes, at)))
508
513
  const length: i64 = quicVarintRead(bytes, at, end)
509
514
  if (length < 0) {
510
515
  return quicParamsRefuse(p, id)
511
516
  }
512
- at = at + quicVarintLength(bytes, at)
513
- if (length > toI64(end - at)) {
517
+ at = toI32(toI64(at) + toI64(quicVarintLength(bytes, at)))
518
+ if (length > toI64(end) - toI64(at)) {
514
519
  return quicParamsRefuse(p, id)
515
520
  }
516
521
  const valueAt: i32 = at
517
522
  const valueLength: i32 = toI32(length)
518
- at = at + valueLength
523
+ // `length` fits in what is left before `end`, so the advance fits an i32.
524
+ at = toI32(toI64(at) + toI64(valueLength))
519
525
  if (id === toI64(QUIC_TP_MAX_DATAGRAM_FRAME_SIZE)) {
520
526
  const value: i64 = quicParamsVarintValue(bytes, valueAt, valueLength)
521
527
  if (seenDatagram || value < 0) {
@@ -29,6 +29,30 @@
29
29
  import { quicPutDatagram } from "nish/net/quic-frame"
30
30
  import { quicPacketCopy } from "nish/net/quic-packet"
31
31
 
32
+ /** `n` zeroed bytes, allocated at once when `n` fits a u16 and grown otherwise. */
33
+ const quicDatagramBytes = (n: i32): u8[] => {
34
+ if (n >= 0 && n <= 65535) {
35
+ return new Array<u8>(toU16(n))
36
+ }
37
+ const out: u8[] = []
38
+ for (let k: i32 = 0; k < n; k += 1) {
39
+ out.push(0)
40
+ }
41
+ return out
42
+ }
43
+
44
+ /** `n` zeroed lengths, allocated at once when `n` fits a u16 and grown otherwise. */
45
+ const quicDatagramLengths = (n: i32): i32[] => {
46
+ if (n >= 0 && n <= 65535) {
47
+ return new Array<i32>(toU16(n))
48
+ }
49
+ const out: i32[] = []
50
+ for (let k: i32 = 0; k < n; k += 1) {
51
+ out.push(0)
52
+ }
53
+ return out
54
+ }
55
+
32
56
  /** A ring of `entries` datagram payloads of at most `entrySize` bytes each, oldest first. */
33
57
  export class QuicDatagramQueue {
34
58
  /** Entry `k`'s bytes are `data[k * entrySize ..]`, `lengths[k]` of them. */
@@ -49,17 +73,44 @@ export class QuicDatagramQueue {
49
73
 
50
74
  /**
51
75
  * Makes an empty ring, allocating all of its storage now: `entries * entrySize` bytes of
52
- * payload and a 4-byte length per entry.
76
+ * payload and a 4-byte length per entry. A negative size, or a product past 2^31 - 1 bytes, makes a
77
+ * ring of no entries, which refuses every `push`.
53
78
  *
54
79
  * @param entries How many datagrams the ring holds at most before `push` refuses the next.
55
80
  * @param entrySize The largest payload, in bytes, an entry holds; a longer one is dropped, not split.
56
81
  */
57
82
  constructor(entries: i32, entrySize: i32) {
58
- this.data = new Array<u8>(entries * entrySize)
59
- this.lengths = new Array<i32>(entries)
83
+ // Two i32s, so the product fits an i64.
84
+ const bytes: i64 = toI64(entries) * toI64(entrySize)
85
+ const fits: boolean = entries >= 0 && entrySize >= 0 && bytes <= 2147483647
86
+ // `bytes` is at most 2^31 - 1 when it fits.
87
+ this.data = quicDatagramBytes(fits ? toI32(bytes) : 0)
88
+ this.lengths = quicDatagramLengths(fits ? entries : 0)
60
89
  this.entrySize = entrySize
61
90
  }
62
91
 
92
+ /**
93
+ * Where entry `k`'s bytes start in `data`. Every entry lies inside `data`, whose length is an
94
+ * i32, so for an entry the offset fits; any other `k` gives an offset `quicPacketCopy` refuses.
95
+ */
96
+ private offset(k: i32): i32 {
97
+ const at: i64 = toI64(k) * toI64(this.entrySize)
98
+ // Past 2^31 - 1 it is no entry's, and -1 is an offset every copy refuses.
99
+ return at < 0 || at > 2147483647 ? -1 : toI32(at)
100
+ }
101
+
102
+ /**
103
+ * The entry `j` places after `head`, wrapped around the ring; 0 for a ring of no entries.
104
+ * Unsigned, because `head` and `j` are each below 2^31, so the sum cannot wrap.
105
+ */
106
+ private slot(j: i32): i32 {
107
+ const entries: u32 = toU32(this.lengths.length)
108
+ if (entries === 0) {
109
+ return 0
110
+ }
111
+ return toI32((toU32(this.head) + toU32(j)) % entries)
112
+ }
113
+
63
114
  /** Empties the ring, for a connection slot reused for another peer. */
64
115
  reset(): void {
65
116
  this.head = 0
@@ -87,26 +138,24 @@ export class QuicDatagramQueue {
87
138
  * @returns True when the datagram was queued; false when it was dropped instead.
88
139
  */
89
140
  push(buf: u8[], from: i32, length: i32): boolean {
90
- const entries: i32 = this.capacity()
141
+ const entries: i32 = toI32(this.lengths.length)
142
+ const count: i32 = this.count
91
143
  if (
92
- this.count >= entries ||
144
+ count >= entries ||
93
145
  length < 0 ||
94
146
  length > this.entrySize ||
95
147
  from < 0 ||
96
148
  from > toI32(buf.length) - length
97
149
  ) {
98
- if (this.dropped < 2147483647) {
99
- this.dropped = this.dropped + 1
100
- }
150
+ this.countDropped()
101
151
  return false
102
152
  }
103
- const k: i32 = (this.head + this.count) % entries
104
- const base: i32 = k * this.entrySize
105
- quicPacketCopy(this.data, base, buf, from, length)
153
+ const k: i32 = this.slot(count)
154
+ quicPacketCopy(this.data, this.offset(k), buf, from, length)
106
155
  if (k >= 0 && k < entries) {
107
156
  this.lengths[k] = length
108
157
  }
109
- this.count = this.count + 1
158
+ this.count = count + 1
110
159
  return true
111
160
  }
112
161
 
@@ -125,36 +174,50 @@ export class QuicDatagramQueue {
125
174
  * that no packet can carry any more; an empty ring is left as it is.
126
175
  */
127
176
  dropOldest(): void {
128
- if (this.count > 0 && this.dropped < 2147483647) {
129
- this.dropped = this.dropped + 1
177
+ if (this.count > 0) {
178
+ this.countDropped()
130
179
  }
131
180
  this.drop()
132
181
  }
133
182
 
183
+ /** Counts one datagram in `dropped`, which stops at 2^31 - 1 rather than wrap. */
184
+ private countDropped(): void {
185
+ const dropped: i32 = this.dropped
186
+ if (dropped < 2147483647) {
187
+ this.dropped = dropped + 1
188
+ }
189
+ }
190
+
134
191
  /**
135
192
  * Moves the oldest datagram behind the newest, unsent and not counted as
136
193
  * dropped, so the ones queued after it go out first; for one too large
137
194
  * for now that a later packet may carry. A ring of one is left as it is.
138
195
  */
139
196
  deferOldest(): void {
140
- const entries: i32 = this.capacity()
141
- if (this.count < 2) {
197
+ const entries: i32 = toI32(this.lengths.length)
198
+ const count: i32 = this.count
199
+ const head: i32 = this.head
200
+ // `head` is an entry whenever `count` is 1 or more; the test says so for the bounds proof.
201
+ if (count < 2 || head < 0 || head >= entries) {
142
202
  return
143
203
  }
144
- if (this.count < entries) {
145
- const k: i32 = (this.head + this.count) % entries
146
- const length: i32 = this.lengths[this.head]
147
- quicPacketCopy(this.data, k * this.entrySize, this.data, this.head * this.entrySize, length)
148
- this.lengths[k] = length
204
+ if (count < entries) {
205
+ const k: i32 = this.slot(count)
206
+ const length: i32 = this.lengths[head]
207
+ quicPacketCopy(this.data, this.offset(k), this.data, this.offset(head), length)
208
+ if (k >= 0 && k < entries) {
209
+ this.lengths[k] = length
210
+ }
149
211
  }
150
- this.head = (this.head + 1) % entries
212
+ this.head = this.slot(toI32(1))
151
213
  }
152
214
 
153
215
  /** Forgets the oldest datagram. */
154
216
  private drop(): void {
155
- if (this.count > 0) {
156
- this.head = (this.head + 1) % this.capacity()
157
- this.count = this.count - 1
217
+ const count: i32 = this.count
218
+ if (count > 0) {
219
+ this.head = this.slot(toI32(1))
220
+ this.count = count - 1
158
221
  }
159
222
  }
160
223
 
@@ -177,7 +240,7 @@ export class QuicDatagramQueue {
177
240
  if (at < 0 || cap < length || at > toI32(buf.length) - length) {
178
241
  return -2
179
242
  }
180
- quicPacketCopy(buf, at, this.data, this.head * this.entrySize, length)
243
+ quicPacketCopy(buf, at, this.data, this.offset(this.head), length)
181
244
  this.drop()
182
245
  return length
183
246
  }
@@ -197,7 +260,7 @@ export class QuicDatagramQueue {
197
260
  if (length < 0) {
198
261
  return at
199
262
  }
200
- const next: i32 = quicPutDatagram(buf, at, end, this.data, this.head * this.entrySize, length)
263
+ const next: i32 = quicPutDatagram(buf, at, end, this.data, this.offset(this.head), length)
201
264
  if (next < 0) {
202
265
  return at
203
266
  }