nish 0.34.1 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/README.md +7 -6
  2. package/docs/AI.md +86 -16
  3. package/docs/INSTALL.md +22 -13
  4. package/llms.txt +5 -2
  5. package/package.json +15 -5
  6. package/runtime/async.d.mts +49 -0
  7. package/runtime/async.mjs +27 -0
  8. package/runtime/nish.d.ts +4681 -427
  9. package/runtime/nish.h +6 -2
  10. package/runtime/nish.mjs +4 -0
  11. package/runtime/runtime-net.c +22 -0
  12. package/runtime/runtime-shm.c +93 -2
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +108 -13
  16. package/std/README.md +131 -131
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +258 -42
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +80 -9
  28. package/std/crypto/base64url.ts +22 -0
  29. package/std/crypto/bcrypt-batch.ts +88 -21
  30. package/std/crypto/bcrypt.ts +60 -0
  31. package/std/crypto/chacha20poly1305.ts +53 -0
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +47 -0
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +235 -31
  36. package/std/crypto/jwt.ts +192 -63
  37. package/std/crypto/p256.ts +159 -16
  38. package/std/crypto/p384.ts +28 -3
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +81 -15
  41. package/std/crypto/rsa.ts +33 -3
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +152 -14
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +67 -7
  48. package/std/json-equal.ts +19 -0
  49. package/std/json-format.ts +112 -13
  50. package/std/json-number.ts +64 -14
  51. package/std/json-pointer.ts +23 -1
  52. package/std/json-tokenizer.ts +100 -29
  53. package/std/json-typed.ts +713 -66
  54. package/std/json-value.ts +400 -124
  55. package/std/json.ts +138 -10
  56. package/std/map.ts +25 -7
  57. package/std/math.ts +36 -4
  58. package/std/net/buffer-pool.ts +42 -11
  59. package/std/net/dns-config.ts +109 -23
  60. package/std/net/dns-message.ts +62 -7
  61. package/std/net/dns-resolver.ts +247 -53
  62. package/std/net/dns.ts +203 -35
  63. package/std/net/fetch.ts +151 -61
  64. package/std/net/handler.ts +278 -80
  65. package/std/net/hpack.ts +262 -52
  66. package/std/net/http-compress.ts +125 -15
  67. package/std/net/http-fields.ts +85 -9
  68. package/std/net/http1-client-codec.ts +100 -19
  69. package/std/net/http1-codec.ts +297 -77
  70. package/std/net/http1-server.ts +430 -94
  71. package/std/net/http2-frame.ts +272 -35
  72. package/std/net/http2-tls.ts +135 -34
  73. package/std/net/http2.ts +247 -106
  74. package/std/net/http3-frame.ts +154 -17
  75. package/std/net/http3-server.ts +275 -101
  76. package/std/net/http3.ts +307 -115
  77. package/std/net/outbound.ts +37 -3
  78. package/std/net/postgres-carrier.ts +144 -8
  79. package/std/net/postgres.ts +801 -112
  80. package/std/net/prefork.ts +164 -31
  81. package/std/net/qpack.ts +105 -18
  82. package/std/net/quic-conn-ack.ts +59 -12
  83. package/std/net/quic-conn-cid.ts +124 -27
  84. package/std/net/quic-conn-params.ts +70 -10
  85. package/std/net/quic-datagram.ts +43 -3
  86. package/std/net/quic-frame.ts +286 -12
  87. package/std/net/quic-listener.ts +101 -32
  88. package/std/net/quic-packet.ts +302 -21
  89. package/std/net/quic-recovery.ts +264 -52
  90. package/std/net/quic-stream.ts +353 -91
  91. package/std/net/quic.ts +1124 -201
  92. package/std/net/router.ts +87 -40
  93. package/std/net/tls/client.ts +166 -51
  94. package/std/net/tls/codec.ts +413 -56
  95. package/std/net/tls/record-client.ts +110 -55
  96. package/std/net/tls/record-server.ts +159 -59
  97. package/std/net/tls/record.ts +109 -12
  98. package/std/net/tls/schedule.ts +235 -26
  99. package/std/net/tls/ticket.ts +159 -40
  100. package/std/net/tls/verify.ts +49 -3
  101. package/std/net/tls-tcp.ts +250 -45
  102. package/std/net/tls.ts +221 -59
  103. package/std/net/upstream.ts +81 -45
  104. package/std/net/url.ts +97 -13
  105. package/std/net/websocket.ts +122 -12
  106. package/std/net/webtransport.ts +220 -89
  107. package/std/pair.ts +10 -2
  108. package/std/random.ts +34 -9
  109. package/std/regex.ts +352 -198
  110. package/std/region.ts +70 -15
  111. package/std/secret.ts +52 -11
  112. package/std/sort.ts +17 -2
  113. package/std/sqlite.ts +256 -35
  114. package/std/sync/ring.ts +430 -40
  115. package/std/sync/shm.ts +222 -15
  116. package/std/term-io.ts +137 -64
  117. package/std/term-pixels.ts +144 -19
  118. package/std/term-probe.ts +66 -25
  119. package/std/term-rate.ts +128 -57
  120. package/std/term.ts +332 -121
  121. package/std/testing.ts +130 -32
  122. package/std/text.ts +73 -7
  123. package/std/threads.ts +229 -41
  124. package/std/yaml-json.ts +94 -82
  125. package/std/yaml-lines.ts +47 -15
  126. package/std/yaml-write.ts +24 -16
  127. package/std/yaml.ts +764 -384
package/std/net/hpack.ts CHANGED
@@ -12,6 +12,9 @@
12
12
  *
13
13
  * import { HpackDecoder, HpackEncoder, HPACK_INDEX_INCREMENTAL, HPACK_OK } from "nish/net/hpack";
14
14
  *
15
+ * const name: u8[] = [58, 112, 97, 116, 104]; // ":path"
16
+ * const value: u8[] = [47]; // "/"
17
+ *
15
18
  * const enc = new HpackEncoder(4096);
16
19
  * const block: u8[] = [];
17
20
  * enc.encodeField(block, name, value, HPACK_INDEX_INCREMENTAL, true);
@@ -407,24 +410,44 @@ const hpackCheckWindow = (what: string, buf: u8[], off: i32, len: i32): void =>
407
410
  *
408
411
  * Canonical means that the codes of one length are consecutive integers in
409
412
  * symbol order, and that the first code of a length is one past the last code
410
- * of the length before, shifted left by the difference. So `firstCode[L]` and
411
- * `countAt[L]` say which `L`-bit integers are codes, and `symbols` lists the
412
- * symbols in code order, `baseRank[L]` being where length `L` starts in it.
413
+ * of the length before, shifted left by the difference. So the first code of
414
+ * each length and how many codes it has say which `L`-bit integers are codes,
415
+ * and a list of the symbols in code order, with where each length starts in
416
+ * it, turns such a code into its symbol: decoding needs no tree.
413
417
  */
414
418
  export class HpackHuffman {
415
- /** The code of each symbol, aligned on its least significant bit. */
419
+ /**
420
+ * The code of each symbol, aligned on its least significant bit.
421
+ * @internal Read by `hpackHuffmanEncode`; not for callers.
422
+ */
416
423
  codes: i32[]
417
- /** The length of each symbol's code, in bits. */
424
+ /**
425
+ * The length of each symbol's code, in bits.
426
+ * @internal Read by `hpackHuffmanLength` and `hpackHuffmanEncode`; not for callers.
427
+ */
418
428
  lengths: i32[]
419
- /** The symbols in code order. */
429
+ /**
430
+ * The symbols in code order.
431
+ * @internal Read by `hpackHuffmanDecode`; not for callers.
432
+ */
420
433
  symbols: i32[]
421
- /** The first code of each length, by length (0 to 30). */
434
+ /**
435
+ * The first code of each length, by length (0 to 30).
436
+ * @internal Read by `hpackHuffmanDecode` and by `qpack.ts`'s Huffman decoder; not for callers.
437
+ */
422
438
  firstCode: i32[]
423
- /** How many codes each length has, by length. */
439
+ /**
440
+ * How many codes each length has, by length.
441
+ * @internal Read by `hpackHuffmanDecode` and by `qpack.ts`'s Huffman decoder; not for callers.
442
+ */
424
443
  countAt: i32[]
425
- /** Where each length's symbols start in `symbols`, by length. */
444
+ /**
445
+ * Where each length's symbols start in `symbols`, by length.
446
+ * @internal Read by `hpackHuffmanDecode`; not for callers.
447
+ */
426
448
  baseRank: i32[]
427
449
 
450
+ /** Rebuilds the code from Appendix B's 257 code lengths. Build one and reuse it: each build allocates. */
428
451
  constructor() {
429
452
  this.codes = new Array<i32>(257)
430
453
  this.lengths = new Array<i32>(257)
@@ -454,7 +477,15 @@ export class HpackHuffman {
454
477
  }
455
478
  }
456
479
 
457
- /** The length of `src[off, off + len)` once Huffman-coded, in bytes. */
480
+ /**
481
+ * The length of `src[off, off + len)` once Huffman-coded, in bytes, padding included. A window outside
482
+ * `src` panics, as does a coded length past 2^31 - 1 bytes.
483
+ * @param h The code to measure with.
484
+ * @param src The octets to measure; only read.
485
+ * @param off Where the window starts, an index into `src`.
486
+ * @param len How many octets from `off` on to measure.
487
+ * @returns The number of bytes `hpackHuffmanEncode` would append for the same window, 0 for an empty one.
488
+ */
458
489
  export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i32): i32 => {
459
490
  hpackCheckWindow("hpackHuffmanLength", src, off, len)
460
491
  let bits: i64 = 0
@@ -478,6 +509,12 @@ export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i3
478
509
  *
479
510
  * The accumulator holds fewer than eight waiting bits between symbols, and a
480
511
  * code is at most 30, so it never needs more than 37 bits.
512
+ * A window outside `src` panics.
513
+ * @param h The code to encode with.
514
+ * @param src The octets to encode; only read.
515
+ * @param off Where the window starts, an index into `src`.
516
+ * @param len How many octets from `off` on to encode.
517
+ * @param out Where the coded bytes go: appended after what it holds, `hpackHuffmanLength` of them.
481
518
  */
482
519
  export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): void => {
483
520
  hpackCheckWindow("hpackHuffmanEncode", src, off, len)
@@ -510,6 +547,16 @@ export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i3
510
547
  * thirty bits always are. What is left when the input ends is padding, which
511
548
  * §5.2 requires to be a prefix of EOS — all ones — and shorter than a byte.
512
549
  * `out` keeps what was decoded before an error; a caller discards it.
550
+ * A window outside `src` panics.
551
+ * @param h The code to decode with.
552
+ * @param src The coded bytes, the peer's; only read.
553
+ * @param off Where the window starts, an index into `src`.
554
+ * @param len How many bytes from `off` on to decode; every one is read unless an EOS comes first.
555
+ * @param out Where the decoded octets go, appended after what it holds: at most `len * 8 / 5` of them,
556
+ * since every code is at least five bits long.
557
+ * @returns `HPACK_OK` when the window decoded whole and ended in valid padding; `HPACK_ERR_HUFFMAN_EOS` when
558
+ * a complete code was the EOS symbol, at the point it was read; `HPACK_ERR_HUFFMAN_PADDING` when the bits
559
+ * left after the last symbol are more than seven or not all ones.
513
560
  */
514
561
  export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): i32 => {
515
562
  hpackCheckWindow("hpackHuffmanDecode", src, off, len)
@@ -546,11 +593,26 @@ export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i3
546
593
  * and where the block ends. The primitives below advance `pos`.
547
594
  */
548
595
  export class HpackCursor {
596
+ /** @internal The block being read; read by this module's decoders, not for callers. */
549
597
  buf: u8[]
598
+ /**
599
+ * Where the next read starts, as an index into the cursor's buffer. `hpackDecodeInteger` and
600
+ * `hpackDecodeString` move it past what they read; after a negative answer it is wherever the read
601
+ * stopped, which a caller should not rely on.
602
+ */
550
603
  pos: i32
604
+ /**
605
+ * One past the last byte the cursor may read.
606
+ * @internal Read by this module's decoders; not for callers.
607
+ */
551
608
  end: i32
552
609
 
553
- /** A cursor over `buf[off, off + len)`, which must lie inside `buf`. */
610
+ /**
611
+ * A cursor over `buf[off, off + len)`, which must lie inside `buf`: a window outside it panics.
612
+ * @param buf The block to read; kept by reference, not copied, so it must not change while it is read.
613
+ * @param off Where the first read starts, an index into `buf`.
614
+ * @param len How many bytes from `off` on the cursor may read.
615
+ */
554
616
  constructor(buf: u8[], off: i32, len: i32) {
555
617
  hpackCheckWindow("HpackCursor", buf, off, len)
556
618
  this.buf = buf
@@ -558,7 +620,10 @@ export class HpackCursor {
558
620
  this.end = off + len
559
621
  }
560
622
 
561
- /** Points the cursor at `buf[off, off + len)` instead, which must lie inside `buf`. */
623
+ /**
624
+ * Points the cursor at `buf[off, off + len)` instead, which must lie inside `buf`.
625
+ * @internal Called by `HpackDecoder.decode` for each block; not for callers.
626
+ */
562
627
  reset(buf: u8[], off: i32, len: i32): void {
563
628
  hpackCheckWindow("HpackCursor", buf, off, len)
564
629
  this.buf = buf
@@ -572,6 +637,12 @@ export class HpackCursor {
572
637
  * first byte's high bits being `flags` (the representation's pattern).
573
638
  * A negative value, or a prefix outside 1 to 8 bits, panics: both are the
574
639
  * program's choice.
640
+ * @param out Where the integer's bytes go, appended after what it holds: one byte when `value` fits the
641
+ * prefix, else the prefix byte and one to five continuation bytes.
642
+ * @param flags The first byte's bits above the prefix; it must leave the low `prefixBits` bits clear, since
643
+ * they are or-ed with it.
644
+ * @param prefixBits How many low bits of the first byte the integer starts in, 1 to 8.
645
+ * @param value The integer, 0 to 2^31 - 1.
575
646
  */
576
647
  export const hpackEncodeInteger = (out: u8[], flags: i32, prefixBits: i32, value: i32): void => {
577
648
  if (prefixBits < 1 || prefixBits > 8 || value < 0) {
@@ -600,6 +671,11 @@ export const hpackEncodeInteger = (out: u8[], flags: i32, prefixBits: i32, value
600
671
  * An `i32` needs at most five continuation bytes after a full prefix, so a
601
672
  * sixth is refused even when its bits are zero; otherwise a run of `0x80`
602
673
  * bytes would be read for as long as the peer cared to send it.
674
+ * @param c Where to read; its position moves past every byte read, also when the answer is an error.
675
+ * @param prefixBits How many low bits of the first byte hold the prefix, 1 to 8; the bits above are not read.
676
+ * @returns The integer, 0 to 2^31 - 1; `HPACK_ERR_TRUNCATED` when the cursor has no byte left before the
677
+ * integer ends, at the first byte or at a continuation byte; `HPACK_ERR_INTEGER_OVERFLOW` when the value
678
+ * passes 2^31 - 1, or when the fifth continuation byte still has its continuation bit set.
603
679
  */
604
680
  export const hpackDecodeInteger = (c: HpackCursor, prefixBits: i32): i32 => {
605
681
  if (prefixBits < 1 || prefixBits > 8) {
@@ -636,6 +712,11 @@ export const hpackDecodeInteger = (c: HpackCursor, prefixBits: i32): i32 => {
636
712
  * Appends `data` as a §5.2 string literal: Huffman-coded when `huffman` is
637
713
  * true, else raw. A caller who wants the shorter of the two compares
638
714
  * `hpackHuffmanLength` with the raw length.
715
+ * @param h The code to encode with when `huffman` is true.
716
+ * @param out Where the literal goes, appended after what it holds: its length with a 7-bit prefix and the
717
+ * H bit, then the octets.
718
+ * @param data The octets to write, all of them; only read.
719
+ * @param huffman Whether to Huffman-code them, even when that is longer than raw.
639
720
  */
640
721
  export const hpackEncodeString = (h: HpackHuffman, out: u8[], data: u8[], huffman: boolean): void => {
641
722
  const n: i32 = toI32(data.length)
@@ -654,6 +735,16 @@ export const hpackEncodeString = (h: HpackHuffman, out: u8[], data: u8[], huffma
654
735
  * Reads a §5.2 string literal at the cursor and appends its octets to `out`,
655
736
  * answering `HPACK_OK` or a negative `HPACK_ERR_*` code. The length is checked
656
737
  * against what the block has left before anything is read.
738
+ * @param h The code to decode a Huffman-coded literal with.
739
+ * @param c Where to read; on `HPACK_OK` its position is just past the literal. On an error it is wherever
740
+ * the read stopped.
741
+ * @param out Where the string's octets go, appended after what it holds; on an error it keeps whatever was
742
+ * appended before the error, which a caller discards. The literal's length is bounded only by what the
743
+ * block has left, and a Huffman-coded one decodes to at most 8/5 of its coded length.
744
+ * @returns `HPACK_OK` when the literal was read whole; `HPACK_ERR_TRUNCATED` when the cursor has no byte
745
+ * left, the length integer ends early, or the length is more than the block has left after it;
746
+ * `HPACK_ERR_INTEGER_OVERFLOW` when the length passes 2^31 - 1 or is padded past five continuation bytes;
747
+ * `HPACK_ERR_HUFFMAN_EOS` or `HPACK_ERR_HUFFMAN_PADDING` from `hpackHuffmanDecode` for a Huffman-coded one.
657
748
  */
658
749
  export const hpackDecodeString = (h: HpackHuffman, c: HpackCursor, out: u8[]): i32 => {
659
750
  if (c.pos >= c.end) {
@@ -683,11 +774,11 @@ export const hpackDecodeString = (h: HpackHuffman, c: HpackCursor, out: u8[]): i
683
774
  * whose size — every entry's name and value plus 32 — never passes `maxSize`.
684
775
  *
685
776
  * Every entry's octets live in one store, `bytes`: its name, then its value,
686
- * oldest entry first. `entries` says where each entry sits, two numbers to an
777
+ * oldest entry first. An index says where each entry sits, two numbers to an
687
778
  * entry — where it starts in `bytes` and how long its name is; its value runs
688
779
  * to where the next one starts — oldest first from entry `first` on, so that
689
780
  * adding one is two `push`es and evicting one moves `first`. The evicted
690
- * prefix of `entries`, and of `bytes`, is shifted out in place once it is half
781
+ * prefix of the index, and of `bytes`, is shifted out in place once it is half
691
782
  * of either, which keeps both amortised constant time without a ring's index
692
783
  * arithmetic. Shifting and popping keep an array's storage, and `bytes` never
693
784
  * holds more than three times `maxSize`, so a table the peer fills and empties
@@ -696,12 +787,28 @@ export const hpackDecodeString = (h: HpackHuffman, c: HpackCursor, out: u8[]): i
696
787
  * last.
697
788
  */
698
789
  export class HpackTable {
790
+ /**
791
+ * Every entry's octets, its name then its value, oldest first, after any evicted entries' octets not yet
792
+ * shifted out. Any change to the table may move what it holds.
793
+ */
699
794
  bytes: u8[]
700
- entries: i32[]
795
+ private entries: i32[]
796
+ /**
797
+ * How many evicted entries the table still records before its oldest live one: eviction moves it up,
798
+ * and shifting the evicted ones out returns it to 0.
799
+ */
701
800
  first: i32
801
+ /**
802
+ * The table's size as §4.1 counts it, in octets: every entry's name and value plus 32; at most `maxSize`.
803
+ */
702
804
  size: i32
805
+ /** The most `size` may be, in octets (§4.2); set by the constructor and `resize`. */
703
806
  maxSize: i32
704
807
 
808
+ /**
809
+ * An empty table.
810
+ * @param maxSize The most its size may be, in octets as §4.1 counts them; a negative one panics.
811
+ */
705
812
  constructor(maxSize: i32) {
706
813
  if (maxSize < 0) {
707
814
  panic(`HpackTable: a maximum size of ${maxSize}`)
@@ -714,75 +821,105 @@ export class HpackTable {
714
821
  }
715
822
 
716
823
  /** How many entries `entries` describes, evicted ones included. */
717
- slots(): i32 {
824
+ private slots(): i32 {
718
825
  return toI32(this.entries.length) >> 1
719
826
  }
720
827
 
721
- /** How many entries the table holds. */
828
+ /**
829
+ * How many entries the table holds.
830
+ * @returns The live entry count: dynamic indexes 1 to it are in range; 0 when the table is empty.
831
+ */
722
832
  count(): i32 {
723
833
  return this.slots() - this.first
724
834
  }
725
835
 
726
836
  /** Where dynamic entry `i`, 1 being the newest, sits in `entries`; `i` must be in range. */
727
- slot(i: i32): i32 {
837
+ private slot(i: i32): i32 {
728
838
  return this.slots() - i
729
839
  }
730
840
 
731
841
  /** Where entry `k` of `entries` starts in `bytes`. */
732
- startOf(k: i32): i32 {
842
+ private startOf(k: i32): i32 {
733
843
  return this.entries[k * 2]
734
844
  }
735
845
 
736
846
  /** Where entry `k` of `entries` ends in `bytes`: where the next starts, or the end of `bytes`. */
737
- endOf(k: i32): i32 {
847
+ private endOf(k: i32): i32 {
738
848
  return k + 1 < this.slots() ? this.entries[k * 2 + 2] : toI32(this.bytes.length)
739
849
  }
740
850
 
741
851
  /** What entry `k` of `entries` costs (§4.1); it fits an `i32` because the table holds it. */
742
- costOf(k: i32): i32 {
852
+ private costOf(k: i32): i32 {
743
853
  return this.endOf(k) - this.startOf(k) + HPACK_ENTRY_OVERHEAD
744
854
  }
745
855
 
746
- /** Where the name of dynamic entry `i` starts in `bytes`. */
856
+ /**
857
+ * Where the name of dynamic entry `i` starts in `bytes`.
858
+ * @param i The dynamic index, 1 being the newest; it must be from 1 to `count()` and is not checked.
859
+ * @returns The offset in `bytes`, valid until the table next changes.
860
+ */
747
861
  nameStart(i: i32): i32 {
748
862
  return this.startOf(this.slot(i))
749
863
  }
750
864
 
751
- /** How many octets the name of dynamic entry `i` has. */
865
+ /**
866
+ * How many octets the name of dynamic entry `i` has.
867
+ * @internal Read by this module's decoder and encoder; not for callers.
868
+ */
752
869
  nameLength(i: i32): i32 {
753
870
  return this.entries[this.slot(i) * 2 + 1]
754
871
  }
755
872
 
756
- /** Where the value of dynamic entry `i` starts in `bytes`, straight after its name. */
873
+ /**
874
+ * Where the value of dynamic entry `i` starts in `bytes`, straight after its name.
875
+ * @internal Read by this module's decoder and encoder; not for callers.
876
+ */
757
877
  valueStart(i: i32): i32 {
758
878
  return this.nameStart(i) + this.nameLength(i)
759
879
  }
760
880
 
761
- /** How many octets the value of dynamic entry `i` has. */
881
+ /**
882
+ * How many octets the value of dynamic entry `i` has.
883
+ * @internal Read by this module's decoder and encoder; not for callers.
884
+ */
762
885
  valueLength(i: i32): i32 {
763
886
  return this.endOf(this.slot(i)) - this.valueStart(i)
764
887
  }
765
888
 
766
- /** What dynamic entry `i` costs (§4.1). */
889
+ /**
890
+ * What dynamic entry `i` costs (§4.1).
891
+ * @internal Read by `HpackDecoder`; not for callers.
892
+ */
767
893
  entrySize(i: i32): i32 {
768
894
  return this.costOf(this.slot(i))
769
895
  }
770
896
 
771
- /** A fresh copy of the name of dynamic entry `i`. It allocates; the codec reads the window instead. */
897
+ /**
898
+ * A fresh copy of the name of dynamic entry `i`. It allocates; the codec reads the window instead.
899
+ * @param i The dynamic index, 1 being the newest; it must be from 1 to `count()` and is not checked.
900
+ * @returns A new array, the caller's to keep.
901
+ */
772
902
  name(i: i32): u8[] {
773
903
  const out: u8[] = []
774
904
  hpackAppend(out, this.bytes, this.nameStart(i), this.nameLength(i))
775
905
  return out
776
906
  }
777
907
 
778
- /** A fresh copy of the value of dynamic entry `i`, as `name` makes the name. */
908
+ /**
909
+ * A fresh copy of the value of dynamic entry `i`, as `name` makes the name.
910
+ * @param i The dynamic index, 1 being the newest; it must be from 1 to `count()` and is not checked.
911
+ * @returns A new array, the caller's to keep.
912
+ */
779
913
  value(i: i32): u8[] {
780
914
  const out: u8[] = []
781
915
  hpackAppend(out, this.bytes, this.valueStart(i), this.valueLength(i))
782
916
  return out
783
917
  }
784
918
 
785
- /** Evicts the oldest entries until the size is at most `limit` (§4.4). */
919
+ /**
920
+ * Evicts the oldest entries until the size is at most `limit` (§4.4).
921
+ * @internal Called by `add`, `resize` and `HpackDecoder`; not for callers.
922
+ */
786
923
  evictTo(limit: i32): void {
787
924
  while (toI64(this.size) > toI64(limit) && this.count() > 0) {
788
925
  this.size -= this.costOf(this.first)
@@ -792,7 +929,7 @@ export class HpackTable {
792
929
  }
793
930
 
794
931
  /** Shifts the evicted prefix out of `entries`, and out of `bytes`, once it is half of either. */
795
- compact(): void {
932
+ private compact(): void {
796
933
  const end: i32 = toI32(this.bytes.length)
797
934
  const start: i32 = this.count() > 0 ? this.startOf(this.first) : end
798
935
  const n: i32 = this.slots()
@@ -826,6 +963,11 @@ export class HpackTable {
826
963
  * added (§4.4): that is not an error. Neither array may be `bytes` itself,
827
964
  * since eviction may move what it holds. Returns the octets it pushed into
828
965
  * `bytes`: none for an entry it did not add.
966
+ * @param name The entry's name; copied in, not kept.
967
+ * @param value The entry's value; copied in, not kept.
968
+ * @returns How many octets it pushed into `bytes`, the name's and the value's together: 0 when the entry
969
+ * was larger than `maxSize` and the table was emptied instead, and also for an added entry whose name
970
+ * and value are both empty.
829
971
  */
830
972
  add(name: u8[], value: u8[]): i32 {
831
973
  const cost: i64 = hpackEntrySize(name, value)
@@ -850,7 +992,10 @@ export class HpackTable {
850
992
  return pushed
851
993
  }
852
994
 
853
- /** Sets the maximum size, evicting what no longer fits (§4.3). */
995
+ /**
996
+ * Sets the maximum size, evicting what no longer fits (§4.3).
997
+ * @param maxSize The new maximum, in octets as §4.1 counts them; a negative one panics.
998
+ */
854
999
  resize(maxSize: i32): void {
855
1000
  if (maxSize < 0) {
856
1001
  panic(`HpackTable.resize: a maximum size of ${maxSize}`)
@@ -898,11 +1043,15 @@ const HPACK_FIELD_CLASSES: i32 = 24
898
1043
  * class, each class made when first needed.
899
1044
  */
900
1045
  export class HpackWork {
1046
+ /** @internal The read position, reset for each block by `HpackDecoder.decode`; not for callers. */
901
1047
  cursor: HpackCursor
1048
+ /** @internal A literal's new name as it is read, by `HpackDecoder`; not for callers. */
902
1049
  nameScratch: u8[]
1050
+ /** @internal A literal's value as it is read, by `HpackDecoder`; not for callers. */
903
1051
  valueScratch: u8[]
904
- pools: u8[][][]
1052
+ private pools: u8[][][]
905
1053
 
1054
+ /** Empty scratch arrays, a cursor over nothing, and no pools until a field needs one. */
906
1055
  constructor() {
907
1056
  this.nameScratch = []
908
1057
  this.valueScratch = []
@@ -911,7 +1060,10 @@ export class HpackWork {
911
1060
  this.pools = []
912
1061
  }
913
1062
 
914
- /** The smallest size class with room for `length` octets: the first `j` with 2^j past it, at most the last. */
1063
+ /**
1064
+ * The smallest size class with room for `length` octets: the first `j` with 2^j past it, at most the last.
1065
+ * @internal Called by `HpackDecoder` as it gives fields back; not for callers.
1066
+ */
915
1067
  classOf(length: i32): i32 {
916
1068
  let j: i32 = 0
917
1069
  while (j < HPACK_FIELD_CLASSES - 1 && 1 << j <= length) {
@@ -920,7 +1072,10 @@ export class HpackWork {
920
1072
  return j
921
1073
  }
922
1074
 
923
- /** The pool of class `j`, made the first time a field needs it. */
1075
+ /**
1076
+ * The pool of class `j`, made the first time a field needs it.
1077
+ * @internal Called by `HpackDecoder` as it gives fields back; not for callers.
1078
+ */
924
1079
  pool(j: i32): u8[][] {
925
1080
  while (toI32(this.pools.length) <= j) {
926
1081
  const fresh: u8[][] = []
@@ -933,6 +1088,7 @@ export class HpackWork {
933
1088
  * An empty array with room for `length` octets: one an earlier block gave
934
1089
  * back, emptied, to its class's pool, or, once the pool is empty, a new one made with
935
1090
  * the class's room.
1091
+ * @internal Called by `HpackDecoder` for each field it answers; not for callers.
936
1092
  */
937
1093
  take(length: i32): u8[] {
938
1094
  const j: i32 = this.classOf(length)
@@ -970,29 +1126,58 @@ export class HpackWork {
970
1126
  * as SETTINGS_MAX_HEADER_LIST_SIZE does (RFC 9113 §6.5.2).
971
1127
  */
972
1128
  export class HpackDecoder {
1129
+ /** The dynamic table, as the blocks decoded so far have left it. */
973
1130
  table: HpackTable
974
- huffman: HpackHuffman
1131
+ private huffman: HpackHuffman
1132
+ /**
1133
+ * The SETTINGS_HEADER_TABLE_SIZE in force, in octets: a size update above it is refused. Set by the
1134
+ * constructor and `setSettingsLimit`, and put back to 4096 by `http2.ts`'s `restart`.
1135
+ */
975
1136
  settingsLimit: i32
1137
+ /**
1138
+ * The header-list limit, in octets counted as RFC 9113 §6.5.2 counts them: the field that takes the
1139
+ * block's running total past it, and every one after, is dropped and `decode` answers
1140
+ * `HPACK_LIST_TOO_LARGE`.
1141
+ */
976
1142
  maxHeaderListSize: i32
977
- /** The smallest limit set since the last size update, or -1 when none is owed. */
1143
+ /**
1144
+ * The smallest limit set since the last size update, or -1 when none is owed.
1145
+ * @internal Cleared by `http2.ts` when a connection restarts; not for callers.
1146
+ */
978
1147
  owedUpdate: i32
979
- /** The fatal error a decode met, which every later decode answers; 0 until then. */
1148
+ /**
1149
+ * The fatal error a decode met, which every later decode answers; 0 until then.
1150
+ * @internal Cleared by `http2.ts` when a connection restarts; not for callers.
1151
+ */
980
1152
  failed: i32
1153
+ /**
1154
+ * The names of the last block's fields, in order: the fields before the list passed its limit, and none
1155
+ * after a negative answer. Each array is the decoder's, valid until the next `decode`.
1156
+ */
981
1157
  names: u8[][]
1158
+ /** The values of the last block's fields, by the same position as `names`, and valid as long. */
982
1159
  values: u8[][]
1160
+ /** Whether each field of the last block came never indexed (§6.2.3), by the same position as `names`. */
983
1161
  neverIndexed: boolean[]
984
1162
  /** The storage a block is decoded with, made by the first `decode`, so an idle connection holds none. */
985
- work: HpackWork | null = null
1163
+ private work: HpackWork | null = null
986
1164
  /**
987
1165
  * The octets the decoder has moved through its dynamic table, counted where
988
- * they move: each name or value `appendNameAt` and `appendValueAt` copy out
989
- * of the table, and each octet `table.add` pushes in. The budget
1166
+ * they move: each name or value it copies out of the dynamic table (a static
1167
+ * entry's do not count), and each octet `table.add` pushes in. The budget
990
1168
  * (`HPACK_COPY_FACTOR`) is charged before a literal's copies, from the
991
1169
  * sizes it will copy; this counts what was copied, so that tests can hold
992
1170
  * the two equal. It is public so that tests can read it.
993
1171
  */
994
1172
  tableCopied: i64 = 0
995
1173
 
1174
+ /**
1175
+ * A decoder with an empty table, holding no working storage until the first `decode`.
1176
+ * @param settingsLimit The SETTINGS_HEADER_TABLE_SIZE in force, in octets; the table starts at it, and a
1177
+ * negative one panics.
1178
+ * @param maxHeaderListSize The header-list limit, in octets as RFC 9113 §6.5.2 counts them; a negative one
1179
+ * panics.
1180
+ */
996
1181
  constructor(settingsLimit: i32, maxHeaderListSize: i32) {
997
1182
  if (maxHeaderListSize < 0) {
998
1183
  panic(`HpackDecoder: a header-list limit of ${maxHeaderListSize}`)
@@ -1009,7 +1194,7 @@ export class HpackDecoder {
1009
1194
  }
1010
1195
 
1011
1196
  /** The decoder's working storage, made the first time it is asked for. */
1012
- working(): HpackWork {
1197
+ private working(): HpackWork {
1013
1198
  const work: HpackWork | null = this.work
1014
1199
  if (work !== null) {
1015
1200
  return work
@@ -1024,6 +1209,8 @@ export class HpackDecoder {
1024
1209
  * A limit below the table's current maximum obliges the peer's encoder to
1025
1210
  * begin its next block with an update to at most the smallest limit set
1026
1211
  * since its last one (§4.2); the decoder holds it to that.
1212
+ * @param limit The acknowledged limit, in octets; a negative one panics. The table's own maximum does not
1213
+ * change until the peer's size update.
1027
1214
  */
1028
1215
  setSettingsLimit(limit: i32): void {
1029
1216
  if (limit < 0) {
@@ -1036,7 +1223,7 @@ export class HpackDecoder {
1036
1223
  }
1037
1224
 
1038
1225
  /** Appends the name at static or dynamic index `index`, which must be in range, to `out`. */
1039
- appendNameAt(index: i32, out: u8[]): void {
1226
+ private appendNameAt(index: i32, out: u8[]): void {
1040
1227
  if (index <= HPACK_STATIC_LENGTH) {
1041
1228
  hpackAppendText(out, hpackStaticName(index))
1042
1229
  return
@@ -1047,7 +1234,7 @@ export class HpackDecoder {
1047
1234
  }
1048
1235
 
1049
1236
  /** Appends the value at `index` to `out`, as `appendNameAt` does the name. */
1050
- appendValueAt(index: i32, out: u8[]): void {
1237
+ private appendValueAt(index: i32, out: u8[]): void {
1051
1238
  if (index <= HPACK_STATIC_LENGTH) {
1052
1239
  hpackAppendText(out, hpackStaticValue(index))
1053
1240
  return
@@ -1058,7 +1245,7 @@ export class HpackDecoder {
1058
1245
  }
1059
1246
 
1060
1247
  /** How many octets the name at `index` has. */
1061
- nameLengthAt(index: i32): i32 {
1248
+ private nameLengthAt(index: i32): i32 {
1062
1249
  if (index <= HPACK_STATIC_LENGTH) {
1063
1250
  return toI32(hpackStaticName(index).length)
1064
1251
  }
@@ -1066,7 +1253,7 @@ export class HpackDecoder {
1066
1253
  }
1067
1254
 
1068
1255
  /** What the entry at `index` counts towards a header list, without building it. */
1069
- sizeAt(index: i32): i64 {
1256
+ private sizeAt(index: i32): i64 {
1070
1257
  if (index <= HPACK_STATIC_LENGTH) {
1071
1258
  return (
1072
1259
  toI64(hpackStaticName(index).length) +
@@ -1078,12 +1265,12 @@ export class HpackDecoder {
1078
1265
  }
1079
1266
 
1080
1267
  /** Whether `index` names an entry of either table. */
1081
- hasIndex(index: i32): boolean {
1268
+ private hasIndex(index: i32): boolean {
1082
1269
  return index >= 1 && index <= HPACK_STATIC_LENGTH + this.table.count()
1083
1270
  }
1084
1271
 
1085
1272
  /** Empties the fields and gives each one's array back, emptied, to its class's pool, for the next block. */
1086
- clear(): void {
1273
+ private clear(): void {
1087
1274
  while (toI32(this.names.length) > 0) {
1088
1275
  const work: HpackWork = this.working()
1089
1276
  const last: i32 = toI32(this.names.length) - 1
@@ -1105,6 +1292,13 @@ export class HpackDecoder {
1105
1292
  * `HPACK_OK`, `HPACK_LIST_TOO_LARGE` or a negative `HPACK_ERR_*` code; the
1106
1293
  * module comment lists what each means. After a negative answer the fields
1107
1294
  * are empty and the decoder is spent.
1295
+ * @param block The bytes holding the block; only read during the call, since every field is copied out.
1296
+ * @param off Where the block starts in `block`.
1297
+ * @param len How many bytes the block has. A window outside `block` panics, unless the decoder has failed.
1298
+ * @returns `HPACK_OK`; `HPACK_LIST_TOO_LARGE` when the list passed `maxHeaderListSize`; or one of
1299
+ * `HPACK_ERR_TRUNCATED`, `HPACK_ERR_INTEGER_OVERFLOW`, `HPACK_ERR_INDEX`, `HPACK_ERR_HUFFMAN_PADDING`,
1300
+ * `HPACK_ERR_HUFFMAN_EOS`, `HPACK_ERR_TABLE_SIZE`, `HPACK_ERR_SIZE_UPDATE_POSITION`,
1301
+ * `HPACK_ERR_SIZE_UPDATE_MISSING` or `HPACK_ERR_COPY_BUDGET`, which every later call answers again.
1108
1302
  */
1109
1303
  decode(block: u8[], off: i32, len: i32): i32 {
1110
1304
  this.clear()
@@ -1141,7 +1335,7 @@ export class HpackDecoder {
1141
1335
  * copied into the pools, which the limit bounds; nothing is allocated once
1142
1336
  * those have grown to the largest block and list seen.
1143
1337
  */
1144
- decodeFields(c: HpackCursor): i32 {
1338
+ private decodeFields(c: HpackCursor): i32 {
1145
1339
  let listSize: i64 = 0
1146
1340
  let tooLarge: boolean = false
1147
1341
  let sawField: boolean = false
@@ -1270,7 +1464,7 @@ export class HpackDecoder {
1270
1464
  }
1271
1465
 
1272
1466
  /** A pooled copy of `bytes`, for the caller. */
1273
- copyOf(bytes: u8[]): u8[] {
1467
+ private copyOf(bytes: u8[]): u8[] {
1274
1468
  const out: u8[] = this.working().take(toI32(bytes.length))
1275
1469
  for (const b of bytes) {
1276
1470
  out.push(b)
@@ -1287,11 +1481,21 @@ export class HpackDecoder {
1287
1481
  * HTTP/2's 4096 until SETTINGS say otherwise — so no update is sent for it.
1288
1482
  */
1289
1483
  export class HpackEncoder {
1484
+ /** The dynamic table, which `encodeField` adds to and `setMaxTableSize` resizes. */
1290
1485
  table: HpackTable
1486
+ /** @internal Read by `http2.ts` to size a field's strings with `hpackHuffmanLength`; not for callers. */
1291
1487
  huffman: HpackHuffman
1292
- /** The smallest size set since the last block, or -1 when no update is owed. */
1488
+ /**
1489
+ * The smallest size set since the last block, or -1 when no update is owed.
1490
+ * @internal Cleared by `http2.ts` when a connection restarts; not for callers.
1491
+ */
1293
1492
  owedMinimum: i32
1294
1493
 
1494
+ /**
1495
+ * An encoder with an empty table, owing no size update.
1496
+ * @param maxSize The table's maximum size, in octets, which both ends already agree on; a negative one
1497
+ * panics.
1498
+ */
1295
1499
  constructor(maxSize: i32) {
1296
1500
  this.table = new HpackTable(maxSize)
1297
1501
  this.huffman = new HpackHuffman()
@@ -1304,6 +1508,7 @@ export class HpackEncoder {
1304
1508
  * the caller's to keep. Call it between blocks: the next `encodeField`
1305
1509
  * begins with the update, or with two when the size went down and then up
1306
1510
  * again, the smallest first (§4.2).
1511
+ * @param maxSize The new maximum, in octets; a negative one panics.
1307
1512
  */
1308
1513
  setMaxTableSize(maxSize: i32): void {
1309
1514
  if (this.owedMinimum < 0 || maxSize < this.owedMinimum) {
@@ -1317,7 +1522,7 @@ export class HpackEncoder {
1317
1522
  * table has one. The static table is searched first, then the dynamic one
1318
1523
  * from the newest entry.
1319
1524
  */
1320
- findField(name: u8[], value: u8[]): i32 {
1525
+ private findField(name: u8[], value: u8[]): i32 {
1321
1526
  for (let i: i32 = 1; i <= HPACK_STATIC_LENGTH; i += 1) {
1322
1527
  if (hpackSameAsText(name, hpackStaticName(i)) && hpackSameAsText(value, hpackStaticValue(i))) {
1323
1528
  return i
@@ -1337,7 +1542,7 @@ export class HpackEncoder {
1337
1542
  }
1338
1543
 
1339
1544
  /** The lowest index whose entry has `name`, or 0 when neither table has one. */
1340
- findName(name: u8[]): i32 {
1545
+ private findName(name: u8[]): i32 {
1341
1546
  for (let i: i32 = 1; i <= HPACK_STATIC_LENGTH; i += 1) {
1342
1547
  if (hpackSameAsText(name, hpackStaticName(i))) {
1343
1548
  return i
@@ -1357,6 +1562,11 @@ export class HpackEncoder {
1357
1562
  * and `indexing` is not `HPACK_INDEX_NEVER`, else as a literal of the kind
1358
1563
  * `indexing` names, its name by index when a table has it and its strings
1359
1564
  * Huffman-coded when `huffman` is true. Any other `indexing` panics.
1565
+ * @param out The block being built; the field, after any size update owed, is appended to it.
1566
+ * @param name The field's name; read, and copied into the table under incremental indexing, not kept.
1567
+ * @param value The field's value, treated as `name` is.
1568
+ * @param indexing `HPACK_INDEX_INCREMENTAL`, `HPACK_INDEX_WITHOUT` or `HPACK_INDEX_NEVER`.
1569
+ * @param huffman Whether a literal's strings are Huffman-coded; a field sent by index ignores it.
1360
1570
  */
1361
1571
  encodeField(out: u8[], name: u8[], value: u8[], indexing: i32, huffman: boolean): void {
1362
1572
  if (indexing < HPACK_INDEX_INCREMENTAL || indexing > HPACK_INDEX_NEVER) {