@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
@@ -177,6 +177,9 @@ export const QUIC_ERROR_CRYPTO: i64 = 0x0100
177
177
  */
178
178
  export const QUIC_FRAME_MAX_ACK_RANGES: i32 = 32
179
179
 
180
+ /** The `ackRanges` slots: a smallest and a largest per range. */
181
+ const QUIC_FRAME_ACK_SLOTS: i32 = QUIC_FRAME_MAX_ACK_RANGES * 2
182
+
180
183
  /** A stream count's ceiling, 2^60 (RFC 9000 §4.6, §19.11). Spelled as a product: an `i64` literal past 2^53 is refused. */
181
184
  const QUIC_FRAME_MAX_STREAMS: i64 = 1073741824 * 1073741824
182
185
 
@@ -275,7 +278,7 @@ export class QuicFrame {
275
278
  */
276
279
  constructor() {
277
280
  // Sized once, so reading an ACK stores nothing new into the frame.
278
- this.ackRanges = new Array<i64>(QUIC_FRAME_MAX_ACK_RANGES * 2)
281
+ this.ackRanges = new Array<i64>(QUIC_FRAME_ACK_SLOTS)
279
282
  }
280
283
  }
281
284
 
@@ -311,7 +314,8 @@ const quicFrameVarint = (r: QuicFrameReader, d: u8[]): i64 => {
311
314
  r.failed = true
312
315
  return 0
313
316
  }
314
- r.at = r.at + quicVarintLength(d, r.at)
317
+ // The varint was read inside the window, so its end fits.
318
+ r.at = toI32(toI64(r.at) + toI64(quicVarintLength(d, r.at)))
315
319
  return value
316
320
  }
317
321
 
@@ -321,21 +325,24 @@ const quicFrameVarint = (r: QuicFrameReader, d: u8[]): i64 => {
321
325
  */
322
326
  const quicFrameSkip = (r: QuicFrameReader, length: i64): i32 => {
323
327
  const at: i32 = r.at
324
- if (r.failed || length < 0 || length > toI64(r.end - at)) {
328
+ if (r.failed || length < 0 || length > toI64(r.end) - toI64(at)) {
325
329
  r.failed = true
326
330
  return at
327
331
  }
328
- r.at = at + toI32(length)
332
+ // `length` is at most `end - at`, so it and the new position fit.
333
+ r.at = toI32(toI64(at) + toI64(toI32(length)))
329
334
  return at
330
335
  }
331
336
 
332
337
  /** Stores the `index`-th ACK range pair; the caller keeps `index` under `QUIC_FRAME_MAX_ACK_RANGES`. */
333
338
  const quicFrameStoreRange = (frame: QuicFrame, index: i32, smallest: i64, largest: i64): void => {
334
- const at: i32 = index * 2
339
+ // `index` is under `QUIC_FRAME_MAX_ACK_RANGES`, so both slots fit an i32.
340
+ const at: i32 = toI32(toI64(index) * 2)
341
+ const next: i32 = toI32(toI64(at) + 1)
335
342
  const ranges: i64[] = frame.ackRanges
336
- if (at >= 0 && at + 1 < toI32(ranges.length)) {
343
+ if (at >= 0 && at < toI32(ranges.length) && next >= 0 && next < toI32(ranges.length)) {
337
344
  ranges[at] = smallest
338
- ranges[at + 1] = largest
345
+ ranges[next] = largest
339
346
  }
340
347
  }
341
348
 
@@ -377,7 +384,9 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
377
384
  if (r.failed) {
378
385
  return QUIC_ERROR_FRAME_ENCODING
379
386
  }
380
- let smallest: i64 = frame.largest - first
387
+ // Varints are below 2^62, so the difference is exact in wrapping u64 and a
388
+ // negative answer means the range reached below zero.
389
+ let smallest: i64 = toI64(toU64(frame.largest) - toU64(first))
381
390
  if (smallest < 0) {
382
391
  return QUIC_ERROR_FRAME_ENCODING
383
392
  }
@@ -385,26 +394,33 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
385
394
  let stored: i32 = 1
386
395
  // Each further range takes at least two bytes, so the loop is bounded by the
387
396
  // payload whatever count the peer claims: a short read ends it.
388
- for (let k: i64 = 0; k < count; k += 1) {
397
+ for (let k: u64 = 0; k < toU64(count); k += 1) {
389
398
  const gap: i64 = quicFrameVarint(r, d)
390
399
  const length: i64 = quicFrameVarint(r, d)
391
400
  if (r.failed) {
392
401
  return QUIC_ERROR_FRAME_ENCODING
393
402
  }
394
403
  // The next range's largest is two below this one's smallest, less the gap.
395
- const top: i64 = smallest - gap - 2
396
- const bottom: i64 = top - length
397
- if (top < 0 || bottom < 0) {
404
+ // `smallest` is 0 or more and the varints below 2^62, so wrapping u64
405
+ // is exact and a negative answer is a range below zero.
406
+ const top: i64 = toI64(toU64(smallest) - toU64(gap) - 2)
407
+ if (top < 0) {
408
+ return QUIC_ERROR_FRAME_ENCODING
409
+ }
410
+ const bottom: i64 = toI64(toU64(top) - toU64(length))
411
+ if (bottom < 0) {
398
412
  return QUIC_ERROR_FRAME_ENCODING
399
413
  }
400
414
  if (stored < QUIC_FRAME_MAX_ACK_RANGES) {
401
415
  quicFrameStoreRange(frame, stored, bottom, top)
402
- stored = stored + 1
416
+ // Under `QUIC_FRAME_MAX_ACK_RANGES`, so the count fits.
417
+ stored = toI32(toI64(stored) + 1)
403
418
  }
404
419
  smallest = bottom
405
420
  }
406
421
  frame.ackRangeCount = stored
407
- frame.ackRangeTotal = count + 1
422
+ // `count` is a varint, below 2^62.
423
+ frame.ackRangeTotal = toI64(toU64(count) + 1)
408
424
  if (ecn) {
409
425
  frame.ect0 = quicFrameVarint(r, d)
410
426
  frame.ect1 = quicFrameVarint(r, d)
@@ -423,8 +439,10 @@ const quicFrameReadData = (r: QuicFrameReader, frame: QuicFrame, length: i64): i
423
439
  if (r.failed) {
424
440
  return QUIC_ERROR_FRAME_ENCODING
425
441
  }
426
- frame.dataLength = toI32(length)
427
- return frame.offset > QUIC_MAX_VARINT - length ? QUIC_ERROR_FRAME_ENCODING : QUIC_ERROR_NO_ERROR
442
+ // The skip held `length` inside the window.
443
+ const dataLength: i32 = toI32(length)
444
+ frame.dataLength = dataLength
445
+ return frame.offset > QUIC_MAX_VARINT - toI64(dataLength) ? QUIC_ERROR_FRAME_ENCODING : QUIC_ERROR_NO_ERROR
428
446
  }
429
447
 
430
448
  /** STREAM (§19.8): the type's three low bits say whether Offset and Length are present and whether FIN is set. */
@@ -434,7 +452,7 @@ const quicFrameReadStream = (r: QuicFrameReader, d: u8[], frame: QuicFrame, bits
434
452
  frame.offset = quicFrameVarint(r, d)
435
453
  }
436
454
  // Without a Length the data runs to the end of the packet.
437
- const length: i64 = (bits & 0x02) !== 0 ? quicFrameVarint(r, d) : toI64(r.end - r.at)
455
+ const length: i64 = (bits & 0x02) !== 0 ? quicFrameVarint(r, d) : toI64(r.end) - toI64(r.at)
438
456
  frame.fin = (bits & 0x01) !== 0
439
457
  if (r.failed) {
440
458
  return QUIC_ERROR_FRAME_ENCODING
@@ -444,7 +462,7 @@ const quicFrameReadStream = (r: QuicFrameReader, d: u8[], frame: QuicFrame, bits
444
462
 
445
463
  /** DATAGRAM (RFC 9221 §4): with a Length, that many bytes; without, the rest of the packet. */
446
464
  const quicFrameReadDatagram = (r: QuicFrameReader, d: u8[], frame: QuicFrame, hasLength: boolean): i64 => {
447
- const length: i64 = hasLength ? quicFrameVarint(r, d) : toI64(r.end - r.at)
465
+ const length: i64 = hasLength ? quicFrameVarint(r, d) : toI64(r.end) - toI64(r.at)
448
466
  frame.fin = hasLength
449
467
  frame.dataStart = quicFrameSkip(r, length)
450
468
  if (r.failed) {
@@ -490,9 +508,11 @@ const quicFrameReadBody = (r: QuicFrameReader, d: u8[], frame: QuicFrame, type:
490
508
  case QUIC_FRAME_PADDING: {
491
509
  // Padding is a run of zero bytes; read it as one frame, so a packet of
492
510
  // a thousand padding bytes is one call rather than a thousand.
493
- while (r.at < r.end && r.at >= 0 && r.at < toI32(d.length) && toI32(d[r.at]) === 0) {
494
- r.at = r.at + 1
511
+ let at: i32 = r.at
512
+ while (at >= 0 && at < r.end && at < toI32(d.length) && toI32(d[at]) === 0) {
513
+ at += 1
495
514
  }
515
+ r.at = at
496
516
  return QUIC_ERROR_NO_ERROR
497
517
  }
498
518
  case QUIC_FRAME_PING:
@@ -693,26 +713,46 @@ export const quicFrameAckEliciting = (type: i32): boolean =>
693
713
  const quicFrameWindowFits = (bytes: u8[], from: i32, length: i32): boolean =>
694
714
  from >= 0 && length >= 0 && from <= toI32(bytes.length) - length
695
715
 
696
- /** Whether `size` bytes from `at` fit before `end` and inside `buf`. */
697
- const quicFrameRoom = (buf: u8[], at: i32, end: i32, size: i32): boolean =>
698
- size > 0 && at >= 0 && end <= toI32(buf.length) && at <= end - size
716
+ /** Whether a frame of `header` bytes and `length` more from `at` fits before `end` and inside `buf`. */
717
+ const quicFrameRoom = (buf: u8[], at: i32, end: i32, header: i32, length: i32): boolean =>
718
+ header > 0 &&
719
+ length >= 0 &&
720
+ at >= 0 &&
721
+ end <= toI32(buf.length) &&
722
+ toI64(header) + toI64(length) <= toI64(end) - toI64(at)
723
+
724
+ /** `at + count` for a cursor inside a frame `quicFrameRoom` fitted before `end`, so the sum fits. */
725
+ const quicFrameAdvance = (at: i32, count: i32): i32 => toI32(toI64(at) + toI64(count))
699
726
 
700
- /** Writes one byte and answers the offset past it; the caller has checked the room. */
727
+ /**
728
+ * Writes one byte and answers the offset past it. Every caller has checked the
729
+ * room with `quicFrameRoom` or made it with `quicFrameGrow`, so the bounds
730
+ * guard never skips the byte.
731
+ */
701
732
  const quicFramePutByte = (buf: u8[], at: i32, value: i32): i32 => {
702
733
  if (at >= 0 && at < toI32(buf.length)) {
703
734
  buf[at] = toU8(value)
704
735
  }
705
- return at + 1
736
+ return quicFrameAdvance(at, 1)
706
737
  }
707
738
 
708
739
  /** Writes a varint in the fewest bytes; the caller has checked the room and the value. */
709
740
  const quicFramePutVarint = (buf: u8[], at: i32, value: i64): i32 =>
710
741
  quicVarintPut(buf, at, value, quicVarintSize(value))
711
742
 
712
- /** Grows `out` by `size` bytes and answers where they start, for a `push` writer to `put` into. */
713
- const quicFrameGrow = (out: u8[], size: i32): i32 => {
743
+ /**
744
+ * Grows `out` by `header` plus `length` bytes and answers where they start, for a `push` writer to
745
+ * `put` into; -1, growing nothing, for a size below one or past what an array holds, which every
746
+ * `put` writer refuses.
747
+ */
748
+ const quicFrameGrow = (out: u8[], header: i32, length: i32): i32 => {
714
749
  const at: i32 = toI32(out.length)
715
- quicPacketGrow(out, size)
750
+ const size: i64 = toI64(header) + toI64(length)
751
+ if (header < 1 || length < 0 || size > toI64(2147483647) - toI64(at)) {
752
+ return -1
753
+ }
754
+ // Checked against the room the array has left just above.
755
+ quicPacketGrow(out, toI32(size))
716
756
  return at
717
757
  }
718
758
 
@@ -741,12 +781,14 @@ export const quicPushPadding = (out: u8[], count: i32): void => {
741
781
  * `at` itself when nothing could be written.
742
782
  */
743
783
  export const quicPutPadding = (buf: u8[], at: i32, count: i32): i32 => {
784
+ let cursor: i32 = at
744
785
  let k: i32 = 0
745
- while (k < count && at + k >= 0 && at + k < toI32(buf.length)) {
746
- buf[at + k] = toU8(0)
786
+ while (k < count && cursor >= 0 && cursor < toI32(buf.length)) {
787
+ buf[cursor] = toU8(0)
788
+ cursor += 1
747
789
  k += 1
748
790
  }
749
- return at + k
791
+ return cursor
750
792
  }
751
793
 
752
794
  /**
@@ -759,7 +801,10 @@ export const quicPutPadding = (buf: u8[], at: i32, count: i32): i32 => {
759
801
  * @returns `at + 1`, or -1 with nothing written.
760
802
  */
761
803
  export const quicPutTypeOnly = (buf: u8[], at: i32, end: i32, type: i32): i32 => {
762
- if ((type !== QUIC_FRAME_PING && type !== QUIC_FRAME_HANDSHAKE_DONE) || !quicFrameRoom(buf, at, end, 1)) {
804
+ if (
805
+ (type !== QUIC_FRAME_PING && type !== QUIC_FRAME_HANDSHAKE_DONE) ||
806
+ !quicFrameRoom(buf, at, end, 1, 0)
807
+ ) {
763
808
  return -1
764
809
  }
765
810
  return quicFramePutByte(buf, at, type)
@@ -776,8 +821,8 @@ export const quicPushTypeOnly = (out: u8[], type: i32): boolean => {
776
821
  if (type !== QUIC_FRAME_PING && type !== QUIC_FRAME_HANDSHAKE_DONE) {
777
822
  return false
778
823
  }
779
- const at: i32 = quicFrameGrow(out, 1)
780
- return quicPutTypeOnly(out, at, at + 1, type) >= 0
824
+ const at: i32 = quicFrameGrow(out, 1, 0)
825
+ return quicPutTypeOnly(out, at, toI32(out.length), type) >= 0
781
826
  }
782
827
 
783
828
  /**
@@ -788,17 +833,23 @@ export const quicPushTypeOnly = (out: u8[], type: i32): boolean => {
788
833
  * @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
789
834
  * @param rangeCount How many pairs to read from the front of `ranges`; the rest of the array is ignored.
790
835
  * @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
791
- * @returns The frame's size in bytes, or 0 when `quicPutAck` would refuse these arguments.
836
+ * @returns The frame's size in bytes, or 0 when `quicPutAck` would refuse these arguments or the
837
+ * frame would pass 2^31 − 33 bytes.
792
838
  */
793
839
  export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32 => {
794
- if (rangeCount < 1 || rangeCount * 2 > toI32(ranges.length) || quicVarintSize(ackDelay) === 0) {
840
+ if (
841
+ rangeCount < 1 ||
842
+ toI64(rangeCount) * 2 > toI64(toI32(ranges.length)) ||
843
+ quicVarintSize(ackDelay) === 0
844
+ ) {
795
845
  return 0
796
846
  }
797
847
  let size: i32 = 1
798
848
  let previousSmallest: i64 = 0
799
849
  for (let k: i32 = 0; k < rangeCount; k += 1) {
800
- const at: i32 = k * 2
801
- const next: i32 = at + 1
850
+ // `k` is under `rangeCount`, which is at most half the array, so both slots fit.
851
+ const at: i32 = toI32(toI64(k) * 2)
852
+ const next: i32 = toI32(toI64(at) + 1)
802
853
  if (at < 0 || at >= toI32(ranges.length) || next < 0 || next >= toI32(ranges.length)) {
803
854
  return 0
804
855
  }
@@ -807,17 +858,29 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
807
858
  if (smallest < 0 || largest < smallest || largest > QUIC_MAX_VARINT) {
808
859
  return 0
809
860
  }
861
+ // A range adds at most 32 bytes, so refusing past 2^31 − 33 keeps the size
862
+ // an i32; no buffer holds a frame that large.
863
+ if (size > 2147483615) {
864
+ return 0
865
+ }
866
+ // `smallest` is 0 or more and at most `largest`, so the difference is exact.
867
+ const span: i64 = toI64(toU64(largest) - toU64(smallest))
810
868
  if (k === 0) {
811
- size = size + quicVarintSize(largest) + quicVarintSize(ackDelay) + quicVarintSize(toI64(rangeCount - 1))
869
+ size =
870
+ size +
871
+ quicVarintSize(largest) +
872
+ quicVarintSize(ackDelay) +
873
+ quicVarintSize(toI64(rangeCount - 1)) +
874
+ quicVarintSize(span)
812
875
  } else {
813
- // Gap: the packets missing between this range and the one above, less one (§19.3.1).
814
- const gap: i64 = previousSmallest - largest - 2
876
+ // Gap: the packets missing between this range and the one above, less one (§19.3.1). Both
877
+ // are 0 to 2^62 − 1, so wrapping u64 is exact and a negative gap is ranges out of order.
878
+ const gap: i64 = toI64(toU64(previousSmallest) - toU64(largest) - 2)
815
879
  if (gap < 0) {
816
880
  return 0
817
881
  }
818
- size = size + quicVarintSize(gap)
882
+ size = size + quicVarintSize(gap) + quicVarintSize(span)
819
883
  }
820
- size = size + quicVarintSize(largest - smallest)
821
884
  previousSmallest = smallest
822
885
  }
823
886
  return size
@@ -848,24 +911,28 @@ export const quicPutAck = (
848
911
  ackDelay: i64
849
912
  ): i32 => {
850
913
  const size: i32 = quicAckSize(ranges, rangeCount, ackDelay)
851
- if (!quicFrameRoom(buf, at, end, size)) {
914
+ if (!quicFrameRoom(buf, at, end, size, 0)) {
852
915
  return -1
853
916
  }
854
917
  let cursor: i32 = quicFramePutByte(buf, at, QUIC_FRAME_ACK)
855
918
  let previousSmallest: i64 = 0
856
919
  for (let k: i32 = 0; k < rangeCount; k += 1) {
857
- const pair: i32 = k * 2
858
- if (pair >= 0 && pair < toI32(ranges.length) && pair + 1 < toI32(ranges.length)) {
920
+ // `quicAckSize` held `rangeCount` to half the array and checked every
921
+ // pair, so the slots fit, the guard below never skips a pair, and the
922
+ // differences are exact in wrapping u64.
923
+ const pair: i32 = toI32(toI64(k) * 2)
924
+ const next: i32 = toI32(toI64(pair) + 1)
925
+ if (pair >= 0 && pair < toI32(ranges.length) && next >= 0 && next < toI32(ranges.length)) {
859
926
  const smallest: i64 = ranges[pair]
860
- const largest: i64 = ranges[pair + 1]
927
+ const largest: i64 = ranges[next]
861
928
  if (k === 0) {
862
929
  cursor = quicFramePutVarint(buf, cursor, largest)
863
930
  cursor = quicFramePutVarint(buf, cursor, ackDelay)
864
- cursor = quicFramePutVarint(buf, cursor, toI64(rangeCount - 1))
931
+ cursor = quicFramePutVarint(buf, cursor, toI64(rangeCount) - 1)
865
932
  } else {
866
- cursor = quicFramePutVarint(buf, cursor, previousSmallest - largest - 2)
933
+ cursor = quicFramePutVarint(buf, cursor, toI64(toU64(previousSmallest) - toU64(largest) - 2))
867
934
  }
868
- cursor = quicFramePutVarint(buf, cursor, largest - smallest)
935
+ cursor = quicFramePutVarint(buf, cursor, toI64(toU64(largest) - toU64(smallest)))
869
936
  previousSmallest = smallest
870
937
  }
871
938
  }
@@ -886,8 +953,8 @@ export const quicPushAck = (out: u8[], ranges: i64[], rangeCount: i32, ackDelay:
886
953
  if (size === 0) {
887
954
  return false
888
955
  }
889
- const at: i32 = quicFrameGrow(out, size)
890
- return quicPutAck(out, at, at + size, ranges, rangeCount, ackDelay) >= 0
956
+ const at: i32 = quicFrameGrow(out, size, 0)
957
+ return quicPutAck(out, at, toI32(out.length), ranges, rangeCount, ackDelay) >= 0
891
958
  }
892
959
 
893
960
  /**
@@ -901,7 +968,8 @@ export const quicPushAck = (out: u8[], ranges: i64[], rangeCount: i32, ackDelay:
901
968
  * @param ect0 The ECT(0) count: packets received in this space marked ECT(0).
902
969
  * @param ect1 The ECT(1) count: packets received in this space marked ECT(1).
903
970
  * @param ce The ECN-CE count: packets received in this space marked CE.
904
- * @returns The frame's size in bytes, or 0 when `quicPutAckEcn` would refuse these arguments.
971
+ * @returns The frame's size in bytes, or 0 when `quicPutAckEcn` would refuse these arguments or the
972
+ * frame would pass 2^31 − 1 bytes.
905
973
  */
906
974
  export const quicAckEcnSize = (
907
975
  ranges: i64[],
@@ -918,7 +986,8 @@ export const quicAckEcnSize = (
918
986
  if (base === 0 || a === 0 || b === 0 || c === 0) {
919
987
  return 0
920
988
  }
921
- return base + a + b + c
989
+ const size: i64 = toI64(base) + toI64(a) + toI64(b) + toI64(c)
990
+ return size > 2147483647 ? 0 : toI32(size)
922
991
  }
923
992
 
924
993
  /**
@@ -950,7 +1019,7 @@ export const quicPutAckEcn = (
950
1019
  ce: i64
951
1020
  ): i32 => {
952
1021
  const size: i32 = quicAckEcnSize(ranges, rangeCount, ackDelay, ect0, ect1, ce)
953
- if (!quicFrameRoom(buf, at, end, size)) {
1022
+ if (!quicFrameRoom(buf, at, end, size, 0)) {
954
1023
  return -1
955
1024
  }
956
1025
  let cursor: i32 = quicPutAck(buf, at, end, ranges, rangeCount, ackDelay)
@@ -988,8 +1057,8 @@ export const quicPushAckEcn = (
988
1057
  if (size === 0) {
989
1058
  return false
990
1059
  }
991
- const at: i32 = quicFrameGrow(out, size)
992
- return quicPutAckEcn(out, at, at + size, ranges, rangeCount, ackDelay, ect0, ect1, ce) >= 0
1060
+ const at: i32 = quicFrameGrow(out, size, 0)
1061
+ return quicPutAckEcn(out, at, toI32(out.length), ranges, rangeCount, ackDelay, ect0, ect1, ce) >= 0
993
1062
  }
994
1063
 
995
1064
  /**
@@ -1031,7 +1100,7 @@ export const quicPutCrypto = (
1031
1100
  !quicFrameWindowFits(data, from, length) ||
1032
1101
  offset < 0 ||
1033
1102
  offset > QUIC_MAX_VARINT - toI64(length) ||
1034
- !quicFrameRoom(buf, at, end, quicCryptoOverhead(offset, length) + length)
1103
+ !quicFrameRoom(buf, at, end, quicCryptoOverhead(offset, length), length)
1035
1104
  ) {
1036
1105
  return -1
1037
1106
  }
@@ -1039,7 +1108,7 @@ export const quicPutCrypto = (
1039
1108
  cursor = quicFramePutVarint(buf, cursor, offset)
1040
1109
  cursor = quicFramePutVarint(buf, cursor, toI64(length))
1041
1110
  quicPacketCopy(buf, cursor, data, from, length)
1042
- return cursor + length
1111
+ return quicFrameAdvance(cursor, length)
1043
1112
  }
1044
1113
 
1045
1114
  /**
@@ -1056,9 +1125,8 @@ export const quicPushCrypto = (out: u8[], offset: i64, data: u8[], from: i32, le
1056
1125
  if (!quicFrameWindowFits(data, from, length) || offset < 0 || offset > QUIC_MAX_VARINT - toI64(length)) {
1057
1126
  return false
1058
1127
  }
1059
- const size: i32 = quicCryptoOverhead(offset, length) + length
1060
- const at: i32 = quicFrameGrow(out, size)
1061
- return quicPutCrypto(out, at, at + size, offset, data, from, length) >= 0
1128
+ const at: i32 = quicFrameGrow(out, quicCryptoOverhead(offset, length), length)
1129
+ return quicPutCrypto(out, at, toI32(out.length), offset, data, from, length) >= 0
1062
1130
  }
1063
1131
 
1064
1132
  /**
@@ -1072,7 +1140,9 @@ export const quicPushCrypto = (out: u8[], offset: i64, data: u8[], from: i32, le
1072
1140
  * @returns The header's size in bytes, not counting the data.
1073
1141
  */
1074
1142
  export const quicStreamOverhead = (streamId: i64, offset: i64, length: i32): i32 =>
1075
- 1 + quicVarintSize(streamId) + (offset === 0 ? 0 : quicVarintSize(offset)) + quicVarintSize(toI64(length))
1143
+ offset === 0
1144
+ ? 1 + quicVarintSize(streamId) + quicVarintSize(toI64(length))
1145
+ : 1 + quicVarintSize(streamId) + quicVarintSize(offset) + quicVarintSize(toI64(length))
1076
1146
 
1077
1147
  /**
1078
1148
  * Writes a STREAM frame (§19.8) carrying `length` bytes of `data` on
@@ -1115,7 +1185,7 @@ export const quicPutStream = (
1115
1185
  quicVarintSize(streamId) === 0 ||
1116
1186
  offset < 0 ||
1117
1187
  offset > QUIC_MAX_VARINT - toI64(length) ||
1118
- !quicFrameRoom(buf, at, end, quicStreamOverhead(streamId, offset, length) + length)
1188
+ !quicFrameRoom(buf, at, end, quicStreamOverhead(streamId, offset, length), length)
1119
1189
  ) {
1120
1190
  return -1
1121
1191
  }
@@ -1127,11 +1197,15 @@ export const quicPutStream = (
1127
1197
  cursor = quicFramePutVarint(buf, cursor, offset)
1128
1198
  }
1129
1199
  cursor = quicFramePutVarint(buf, cursor, toI64(length))
1130
- // Two runs: up to the end of the array, then from its start.
1131
- const first: i32 = length < size - from ? length : size - from
1132
- quicPacketCopy(buf, cursor, data, from, first)
1133
- quicPacketCopy(buf, cursor + first, data, 0, length - first)
1134
- return cursor + length
1200
+ // Two runs: up to the end of the array, then from its start. With no data
1201
+ // `from` may lie past the array, and there is nothing to copy.
1202
+ if (length > 0) {
1203
+ // `from` is inside the array here, so `first` is 1 to `length`.
1204
+ const first: i32 = length < size - from ? length : size - from
1205
+ quicPacketCopy(buf, cursor, data, from, first)
1206
+ quicPacketCopy(buf, quicFrameAdvance(cursor, first), data, 0, toI32(toI64(length) - toI64(first)))
1207
+ }
1208
+ return quicFrameAdvance(cursor, length)
1135
1209
  }
1136
1210
 
1137
1211
  /**
@@ -1166,10 +1240,9 @@ export const quicPushStream = (
1166
1240
  ) {
1167
1241
  return false
1168
1242
  }
1169
- const size: i32 = quicStreamOverhead(streamId, offset, length) + length
1170
- const at: i32 = quicFrameGrow(out, size)
1243
+ const at: i32 = quicFrameGrow(out, quicStreamOverhead(streamId, offset, length), length)
1171
1244
  // A window inside the array is a ring that does not wrap.
1172
- return quicPutStream(out, at, at + size, streamId, offset, data, from, length, fin) >= 0
1245
+ return quicPutStream(out, at, toI32(out.length), streamId, offset, data, from, length, fin) >= 0
1173
1246
  }
1174
1247
 
1175
1248
  /** Whether `type` is one of the frames `quicPutValue` writes, and `value` one it may carry. */
@@ -1202,7 +1275,7 @@ const quicFrameValueFits = (type: i32, value: i64): boolean => {
1202
1275
  * @returns The offset past the frame, or -1 with nothing written.
1203
1276
  */
1204
1277
  export const quicPutValue = (buf: u8[], at: i32, end: i32, type: i32, value: i64): i32 => {
1205
- if (!quicFrameValueFits(type, value) || !quicFrameRoom(buf, at, end, 1 + quicVarintSize(value))) {
1278
+ if (!quicFrameValueFits(type, value) || !quicFrameRoom(buf, at, end, 1 + quicVarintSize(value), 0)) {
1206
1279
  return -1
1207
1280
  }
1208
1281
  return quicFramePutVarint(buf, quicFramePutByte(buf, at, type), value)
@@ -1221,8 +1294,8 @@ export const quicPushValue = (out: u8[], type: i32, value: i64): boolean => {
1221
1294
  return false
1222
1295
  }
1223
1296
  const size: i32 = 1 + quicVarintSize(value)
1224
- const at: i32 = quicFrameGrow(out, size)
1225
- return quicPutValue(out, at, at + size, type, value) >= 0
1297
+ const at: i32 = quicFrameGrow(out, size, 0)
1298
+ return quicPutValue(out, at, toI32(out.length), type, value) >= 0
1226
1299
  }
1227
1300
 
1228
1301
  /**
@@ -1250,7 +1323,7 @@ export const quicPutStreamValue = (
1250
1323
  (type !== QUIC_FRAME_MAX_STREAM_DATA && type !== QUIC_FRAME_STREAM_DATA_BLOCKED) ||
1251
1324
  quicVarintSize(streamId) === 0 ||
1252
1325
  quicVarintSize(value) === 0 ||
1253
- !quicFrameRoom(buf, at, end, 1 + quicVarintSize(streamId) + quicVarintSize(value))
1326
+ !quicFrameRoom(buf, at, end, 1 + quicVarintSize(streamId) + quicVarintSize(value), 0)
1254
1327
  ) {
1255
1328
  return -1
1256
1329
  }
@@ -1276,8 +1349,8 @@ export const quicPushStreamValue = (out: u8[], type: i32, streamId: i64, value:
1276
1349
  return false
1277
1350
  }
1278
1351
  const size: i32 = 1 + quicVarintSize(streamId) + quicVarintSize(value)
1279
- const at: i32 = quicFrameGrow(out, size)
1280
- return quicPutStreamValue(out, at, at + size, type, streamId, value) >= 0
1352
+ const at: i32 = quicFrameGrow(out, size, 0)
1353
+ return quicPutStreamValue(out, at, toI32(out.length), type, streamId, value) >= 0
1281
1354
  }
1282
1355
 
1283
1356
  /** The size of a NEW_CONNECTION_ID frame, or 0 for arguments `quicPutNewConnectionId` refuses. */
@@ -1292,7 +1365,12 @@ const quicFrameNewCidSize = (sequence: i64, retirePriorTo: i64, cidLength: i32,
1292
1365
  ) {
1293
1366
  return 0
1294
1367
  }
1295
- return 1 + quicVarintSize(sequence) + quicVarintSize(retirePriorTo) + 1 + cidLength + QUIC_RESET_TOKEN_SIZE
1368
+ // `cidLength` is 1 to 20 here, so the sum fits.
1369
+ return toI32(
1370
+ toI64(2 + quicVarintSize(sequence) + quicVarintSize(retirePriorTo)) +
1371
+ toI64(cidLength) +
1372
+ toI64(QUIC_RESET_TOKEN_SIZE)
1373
+ )
1296
1374
  }
1297
1375
 
1298
1376
  /**
@@ -1322,7 +1400,7 @@ export const quicPutNewConnectionId = (
1322
1400
  resetToken: u8[]
1323
1401
  ): i32 => {
1324
1402
  const size: i32 = quicFrameNewCidSize(sequence, retirePriorTo, cidLength, toI32(resetToken.length))
1325
- if (size === 0 || cidLength > toI32(connectionId.length) || !quicFrameRoom(buf, at, end, size)) {
1403
+ if (size === 0 || cidLength > toI32(connectionId.length) || !quicFrameRoom(buf, at, end, size, 0)) {
1326
1404
  return -1
1327
1405
  }
1328
1406
  let cursor: i32 = quicFramePutByte(buf, at, QUIC_FRAME_NEW_CONNECTION_ID)
@@ -1330,8 +1408,9 @@ export const quicPutNewConnectionId = (
1330
1408
  cursor = quicFramePutVarint(buf, cursor, retirePriorTo)
1331
1409
  cursor = quicFramePutByte(buf, cursor, cidLength)
1332
1410
  quicPacketCopy(buf, cursor, connectionId, 0, cidLength)
1333
- quicPacketCopy(buf, cursor + cidLength, resetToken, 0, QUIC_RESET_TOKEN_SIZE)
1334
- return cursor + cidLength + QUIC_RESET_TOKEN_SIZE
1411
+ const tokenAt: i32 = quicFrameAdvance(cursor, cidLength)
1412
+ quicPacketCopy(buf, tokenAt, resetToken, 0, QUIC_RESET_TOKEN_SIZE)
1413
+ return quicFrameAdvance(tokenAt, QUIC_RESET_TOKEN_SIZE)
1335
1414
  }
1336
1415
 
1337
1416
  /**
@@ -1358,12 +1437,12 @@ export const quicPushNewConnectionId = (
1358
1437
  if (size === 0) {
1359
1438
  return false
1360
1439
  }
1361
- const at: i32 = quicFrameGrow(out, size)
1440
+ const at: i32 = quicFrameGrow(out, size, 0)
1362
1441
  return (
1363
1442
  quicPutNewConnectionId(
1364
1443
  out,
1365
1444
  at,
1366
- at + size,
1445
+ toI32(out.length),
1367
1446
  sequence,
1368
1447
  retirePriorTo,
1369
1448
  connectionId,
@@ -1390,13 +1469,13 @@ export const quicPutPathData = (buf: u8[], at: i32, end: i32, type: i32, data: u
1390
1469
  if (
1391
1470
  (type !== QUIC_FRAME_PATH_CHALLENGE && type !== QUIC_FRAME_PATH_RESPONSE) ||
1392
1471
  !quicFrameWindowFits(data, from, QUIC_PATH_DATA_SIZE) ||
1393
- !quicFrameRoom(buf, at, end, 1 + QUIC_PATH_DATA_SIZE)
1472
+ !quicFrameRoom(buf, at, end, 1 + QUIC_PATH_DATA_SIZE, 0)
1394
1473
  ) {
1395
1474
  return -1
1396
1475
  }
1397
1476
  const cursor: i32 = quicFramePutByte(buf, at, type)
1398
1477
  quicPacketCopy(buf, cursor, data, from, QUIC_PATH_DATA_SIZE)
1399
- return cursor + QUIC_PATH_DATA_SIZE
1478
+ return quicFrameAdvance(cursor, QUIC_PATH_DATA_SIZE)
1400
1479
  }
1401
1480
 
1402
1481
  /**
@@ -1415,8 +1494,20 @@ export const quicPushPathData = (out: u8[], type: i32, data: u8[], from: i32): b
1415
1494
  ) {
1416
1495
  return false
1417
1496
  }
1418
- const at: i32 = quicFrameGrow(out, 1 + QUIC_PATH_DATA_SIZE)
1419
- return quicPutPathData(out, at, at + 1 + QUIC_PATH_DATA_SIZE, type, data, from) >= 0
1497
+ const at: i32 = quicFrameGrow(out, 1 + QUIC_PATH_DATA_SIZE, 0)
1498
+ return quicPutPathData(out, at, toI32(out.length), type, data, from) >= 0
1499
+ }
1500
+
1501
+ /** The size of a RESET_STREAM or, with `finalSize` at -1, STOP_SENDING frame, or 0 for a value no varint holds. */
1502
+ const quicFrameStreamErrorSize = (streamId: i64, errorCode: i64, finalSize: i64): i32 => {
1503
+ if (finalSize === -1) {
1504
+ return quicFrameVarints3(streamId, errorCode, 0)
1505
+ ? 1 + quicVarintSize(streamId) + quicVarintSize(errorCode)
1506
+ : 0
1507
+ }
1508
+ return quicFrameVarints3(streamId, errorCode, finalSize)
1509
+ ? 1 + quicVarintSize(streamId) + quicVarintSize(errorCode) + quicVarintSize(finalSize)
1510
+ : 0
1420
1511
  }
1421
1512
 
1422
1513
  /**
@@ -1441,12 +1532,8 @@ export const quicPutStreamError = (
1441
1532
  finalSize: i64
1442
1533
  ): i32 => {
1443
1534
  const reset: boolean = finalSize !== -1
1444
- if (!quicFrameVarints3(streamId, errorCode, reset ? finalSize : 0)) {
1445
- return -1
1446
- }
1447
- const size: i32 =
1448
- 1 + quicVarintSize(streamId) + quicVarintSize(errorCode) + (reset ? quicVarintSize(finalSize) : 0)
1449
- if (!quicFrameRoom(buf, at, end, size)) {
1535
+ const size: i32 = quicFrameStreamErrorSize(streamId, errorCode, finalSize)
1536
+ if (!quicFrameRoom(buf, at, end, size, 0)) {
1450
1537
  return -1
1451
1538
  }
1452
1539
  let cursor: i32 = quicFramePutByte(buf, at, reset ? QUIC_FRAME_RESET_STREAM : QUIC_FRAME_STOP_SENDING)
@@ -1468,28 +1555,30 @@ export const quicPutStreamError = (
1468
1555
  * @returns True when the frame was appended.
1469
1556
  */
1470
1557
  export const quicPushStreamError = (out: u8[], streamId: i64, errorCode: i64, finalSize: i64): boolean => {
1471
- const reset: boolean = finalSize !== -1
1472
- if (!quicFrameVarints3(streamId, errorCode, reset ? finalSize : 0)) {
1558
+ const size: i32 = quicFrameStreamErrorSize(streamId, errorCode, finalSize)
1559
+ if (size === 0) {
1473
1560
  return false
1474
1561
  }
1475
- const size: i32 =
1476
- 1 + quicVarintSize(streamId) + quicVarintSize(errorCode) + (reset ? quicVarintSize(finalSize) : 0)
1477
- const at: i32 = quicFrameGrow(out, size)
1478
- return quicPutStreamError(out, at, at + size, streamId, errorCode, finalSize) >= 0
1562
+ const at: i32 = quicFrameGrow(out, size, 0)
1563
+ return quicPutStreamError(out, at, toI32(out.length), streamId, errorCode, finalSize) >= 0
1479
1564
  }
1480
1565
 
1481
- /** The size of a CONNECTION_CLOSE frame, or 0 for a value no varint holds. */
1482
- const quicFrameCloseSize = (application: boolean, errorCode: i64, frameType: i64, reasonLength: i32): i32 => {
1566
+ /**
1567
+ * The size of a CONNECTION_CLOSE frame before its `reasonLength`-byte reason, or 0 for a value no
1568
+ * varint holds.
1569
+ */
1570
+ const quicFrameCloseHeader = (
1571
+ application: boolean,
1572
+ errorCode: i64,
1573
+ frameType: i64,
1574
+ reasonLength: i32
1575
+ ): i32 => {
1483
1576
  if (!quicFrameVarints3(errorCode, application ? 0 : frameType, toI64(reasonLength))) {
1484
1577
  return 0
1485
1578
  }
1486
- return (
1487
- 1 +
1488
- quicVarintSize(errorCode) +
1489
- (application ? 0 : quicVarintSize(frameType)) +
1490
- quicVarintSize(toI64(reasonLength)) +
1491
- reasonLength
1492
- )
1579
+ return application
1580
+ ? 1 + quicVarintSize(errorCode) + quicVarintSize(toI64(reasonLength))
1581
+ : 1 + quicVarintSize(errorCode) + quicVarintSize(frameType) + quicVarintSize(toI64(reasonLength))
1493
1582
  }
1494
1583
 
1495
1584
  /**
@@ -1518,8 +1607,8 @@ export const quicPutConnectionClose = (
1518
1607
  reason: u8[]
1519
1608
  ): i32 => {
1520
1609
  const reasonLength: i32 = toI32(reason.length)
1521
- const size: i32 = quicFrameCloseSize(application, errorCode, frameType, reasonLength)
1522
- if (!quicFrameRoom(buf, at, end, size)) {
1610
+ const header: i32 = quicFrameCloseHeader(application, errorCode, frameType, reasonLength)
1611
+ if (!quicFrameRoom(buf, at, end, header, reasonLength)) {
1523
1612
  return -1
1524
1613
  }
1525
1614
  let cursor: i32 = quicFramePutByte(
@@ -1533,7 +1622,7 @@ export const quicPutConnectionClose = (
1533
1622
  }
1534
1623
  cursor = quicFramePutVarint(buf, cursor, toI64(reasonLength))
1535
1624
  quicPacketCopy(buf, cursor, reason, 0, reasonLength)
1536
- return cursor + reasonLength
1625
+ return quicFrameAdvance(cursor, reasonLength)
1537
1626
  }
1538
1627
 
1539
1628
  /**
@@ -1554,12 +1643,13 @@ export const quicPushConnectionClose = (
1554
1643
  frameType: i64,
1555
1644
  reason: u8[]
1556
1645
  ): boolean => {
1557
- const size: i32 = quicFrameCloseSize(application, errorCode, frameType, toI32(reason.length))
1558
- if (size === 0) {
1646
+ const reasonLength: i32 = toI32(reason.length)
1647
+ const header: i32 = quicFrameCloseHeader(application, errorCode, frameType, reasonLength)
1648
+ if (header === 0) {
1559
1649
  return false
1560
1650
  }
1561
- const at: i32 = quicFrameGrow(out, size)
1562
- return quicPutConnectionClose(out, at, at + size, application, errorCode, frameType, reason) >= 0
1651
+ const at: i32 = quicFrameGrow(out, header, reasonLength)
1652
+ return quicPutConnectionClose(out, at, toI32(out.length), application, errorCode, frameType, reason) >= 0
1563
1653
  }
1564
1654
 
1565
1655
  /**
@@ -1568,15 +1658,19 @@ export const quicPushConnectionClose = (
1568
1658
  *
1569
1659
  * @param out The bytes to append the frame to.
1570
1660
  * @param token The whole array is the token the client is to present later; copied, not kept.
1571
- * @returns True when the frame was appended; false, appending nothing, for an empty token.
1661
+ * @returns True when the frame was appended; false, appending nothing, for an empty token or
1662
+ * one too large to append to `out`.
1572
1663
  */
1573
1664
  export const quicPushNewToken = (out: u8[], token: u8[]): boolean => {
1574
1665
  const length: i32 = toI32(token.length)
1575
1666
  if (length === 0) {
1576
1667
  return false
1577
1668
  }
1578
- const size: i32 = 1 + quicVarintSize(toI64(length)) + length
1579
- let cursor: i32 = quicFramePutByte(out, quicFrameGrow(out, size), QUIC_FRAME_NEW_TOKEN)
1669
+ const at: i32 = quicFrameGrow(out, 1 + quicVarintSize(toI64(length)), length)
1670
+ if (at < 0) {
1671
+ return false
1672
+ }
1673
+ let cursor: i32 = quicFramePutByte(out, at, QUIC_FRAME_NEW_TOKEN)
1580
1674
  cursor = quicFramePutVarint(out, cursor, toI64(length))
1581
1675
  quicPacketCopy(out, cursor, token, 0, length)
1582
1676
  return true
@@ -1587,10 +1681,14 @@ export const quicPushNewToken = (out: u8[], token: u8[]): boolean => {
1587
1681
  * `length` bytes: the type, the Length and the payload. This is the size RFC
1588
1682
  * 9221 §3 holds to `max_datagram_frame_size`.
1589
1683
  *
1590
- * @param length The payload's size in bytes.
1591
- * @returns The whole frame's size in bytes.
1684
+ * @param length The payload's size in bytes, 0 or more. A negative one is not refused: it
1685
+ * answers `1 + length`, which is not a frame's size.
1686
+ * @returns The whole frame's size in bytes, held at 2^31 − 1, a size no buffer has room for.
1592
1687
  */
1593
- export const quicDatagramSize = (length: i32): i32 => 1 + quicVarintSize(toI64(length)) + length
1688
+ export const quicDatagramSize = (length: i32): i32 => {
1689
+ const size: i64 = toI64(1 + quicVarintSize(toI64(length))) + toI64(length)
1690
+ return size > toI64(2147483647) ? 2147483647 : toI32(size)
1691
+ }
1594
1692
 
1595
1693
  /**
1596
1694
  * Writes a DATAGRAM frame with a Length (type 0x31, RFC 9221 §4) carrying
@@ -1605,7 +1703,10 @@ export const quicDatagramSize = (length: i32): i32 => 1 + quicVarintSize(toI64(l
1605
1703
  * @returns The offset past the frame, or -1 with nothing written.
1606
1704
  */
1607
1705
  export const quicPutDatagram = (buf: u8[], at: i32, end: i32, data: u8[], from: i32, length: i32): i32 => {
1608
- if (!quicFrameWindowFits(data, from, length) || !quicFrameRoom(buf, at, end, quicDatagramSize(length))) {
1706
+ if (
1707
+ !quicFrameWindowFits(data, from, length) ||
1708
+ !quicFrameRoom(buf, at, end, 1 + quicVarintSize(toI64(length)), length)
1709
+ ) {
1609
1710
  return -1
1610
1711
  }
1611
1712
  const cursor: i32 = quicFramePutVarint(
@@ -1614,5 +1715,5 @@ export const quicPutDatagram = (buf: u8[], at: i32, end: i32, data: u8[], from:
1614
1715
  toI64(length)
1615
1716
  )
1616
1717
  quicPacketCopy(buf, cursor, data, from, length)
1617
- return cursor + length
1718
+ return quicFrameAdvance(cursor, length)
1618
1719
  }