@amritk/nish-aarch64-linux 0.35.0 → 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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -15,7 +15,7 @@
15
15
  * windows, `src[off .. off + len)` in and `out[at ..)` out, and answer where
16
16
  * the output ends. Everything a call needs — the match finder's hash chains,
17
17
  * the block's symbols, the Huffman scratch, the decoder's tables — is
18
- * allocated by the constructor, about 330 KB for a `Deflater` and 4 KB for an
18
+ * allocated by the constructor, about 330 KB for a `Deflater` and 28 KB for an
19
19
  * `Inflater`, so build one per thread and reuse it: a call allocates nothing.
20
20
  * The free functions `deflateCompress` and `deflateDecompress` build one for
21
21
  * a single call, for a program that compresses once.
@@ -174,6 +174,10 @@ const INF_S_DONE: i32 = 3
174
174
  * level: the input stored, five bytes of framing for every 65,535 and for
175
175
  * every block, and slack for the last partial byte. `n` is at most
176
176
  * 2^31 - 1 less the framing; a negative `n` answers 0.
177
+ *
178
+ * @param n The length of the input, in bytes.
179
+ * @returns A size in bytes, not a position: an `out` with this many bytes
180
+ * from `at` always holds the stream.
177
181
  */
178
182
  export const deflateBound = (n: i32): i32 => {
179
183
  if (n < 0) {
@@ -186,6 +190,10 @@ export const deflateBound = (n: i32): i32 => {
186
190
  * The most bytes a compressed body of `n` input bytes takes in any of this
187
191
  * directory's formats: `deflateBound(n)` with room for gzip's 18 bytes of
188
192
  * header and trailer, which covers zlib's 6.
193
+ *
194
+ * @param n The length of the input, in bytes.
195
+ * @returns A size in bytes, not a position: an `out` with this many bytes
196
+ * from `at` always holds the raw, zlib or gzip stream.
189
197
  */
190
198
  export const compressBound = (n: i32): i32 => deflateBound(n) + 18
191
199
 
@@ -451,16 +459,17 @@ const deflateCanonical = (lens: i32[], n: i32, codes: i32[], count: i32[], next:
451
459
  * literal/length symbols, the largest alphabet.
452
460
  */
453
461
  export class DeflateHuffmanScratch {
454
- /** Symbols sorted by frequency, then by symbol. */
462
+ /** @internal Used by `deflateLengths`. Symbols sorted by frequency, then by symbol. */
455
463
  readonly syms: i32[]
456
- /** Their frequencies, then (in place) the tree, then their lengths. */
464
+ /** @internal Used by `deflateLengths`. Their frequencies, then (in place) the tree, then their lengths. */
457
465
  readonly keys: i32[]
458
- /** The other half of each radix pass. */
466
+ /** @internal Used by `deflateLengths`. The other half of each radix pass. */
459
467
  readonly tmpSyms: i32[]
468
+ /** @internal Used by `deflateLengths`. */
460
469
  readonly tmpKeys: i32[]
461
- /** The radix pass's 256 buckets. */
470
+ /** @internal Used by `deflateLengths`. The radix pass's 256 buckets. */
462
471
  readonly buckets: i32[]
463
- /** How many codes have each length, 0 to 32. */
472
+ /** @internal Used by `deflateLengths`. How many codes have each length, 0 to 32. */
464
473
  readonly perLength: i32[]
465
474
 
466
475
  /** Allocates the scratch. */
@@ -666,64 +675,70 @@ const deflateLengths = (freq: i32[], n: i32, limit: i32, lens: i32[], w: Deflate
666
675
  */
667
676
  export class Deflater {
668
677
  /** `head[h]`: the most recent position whose hash is `h`, plus `delta`; 0 for none. */
669
- readonly head: i32[]
678
+ private readonly head: i32[]
670
679
  /** `prev[p & 32767]`: the position before `p` with `p`'s hash. */
671
- readonly prev: i32[]
680
+ private readonly prev: i32[]
672
681
  /** The block's symbols: a literal byte, or `dist << 9 | (256 + len - 3)`. */
673
- readonly tokens: i32[]
682
+ private readonly tokens: i32[]
674
683
  /** The block's literal/length and distance frequencies. */
675
- readonly litFreq: i32[]
676
- readonly distFreq: i32[]
684
+ private readonly litFreq: i32[]
685
+ private readonly distFreq: i32[]
677
686
  /** The dynamic code for the block. */
678
- readonly litLens: i32[]
679
- readonly litCodes: i32[]
680
- readonly distLens: i32[]
681
- readonly distCodes: i32[]
687
+ private readonly litLens: i32[]
688
+ private readonly litCodes: i32[]
689
+ private readonly distLens: i32[]
690
+ private readonly distCodes: i32[]
682
691
  /** The code-length code, and the run-length coded lengths it codes. */
683
- readonly clFreq: i32[]
684
- readonly clLens: i32[]
685
- readonly clCodes: i32[]
686
- readonly rle: i32[]
692
+ private readonly clFreq: i32[]
693
+ private readonly clLens: i32[]
694
+ private readonly clCodes: i32[]
695
+ private readonly rle: i32[]
687
696
  /** The fixed code (§3.2.6). */
688
- readonly fixedLitLens: i32[]
689
- readonly fixedLitCodes: i32[]
690
- readonly fixedDistLens: i32[]
691
- readonly fixedDistCodes: i32[]
697
+ private readonly fixedLitLens: i32[]
698
+ private readonly fixedLitCodes: i32[]
699
+ private readonly fixedDistLens: i32[]
700
+ private readonly fixedDistCodes: i32[]
692
701
  /** Length - 3 to length symbol - 257; distance to distance symbol, as `deflateDistSymbol` reads it. */
693
- readonly lengthSymbol: i32[]
694
- readonly distSymbol: i32[]
702
+ private readonly lengthSymbol: i32[]
703
+ private readonly distSymbol: i32[]
695
704
  /** Base and extra bits of each length and distance symbol. */
696
- readonly lengthBase: i32[]
697
- readonly lengthExtra: i32[]
698
- readonly distBase: i32[]
699
- readonly distExtra: i32[]
705
+ private readonly lengthBase: i32[]
706
+ private readonly lengthExtra: i32[]
707
+ private readonly distBase: i32[]
708
+ private readonly distExtra: i32[]
700
709
  /** Canonical-code and length-building scratch. */
701
- readonly countScratch: i32[]
702
- readonly nextScratch: i32[]
703
- readonly scratch: DeflateHuffmanScratch
710
+ private readonly countScratch: i32[]
711
+ private readonly nextScratch: i32[]
712
+ private readonly scratch: DeflateHuffmanScratch
704
713
  /** Bits written but not yet stored, low bit first, and how many. */
705
- bitBuf: u64
706
- bitCount: i32
707
- /** The level, 0 to 9. */
714
+ private bitBuf: u64
715
+ private bitCount: i32
716
+ /** @internal Used by `GzipCompressor`, `GzipWriter` (std/compress/gzip.ts) and `ZlibCompressor` (std/compress/zlib.ts). The level, 0 to 9. */
708
717
  readonly level: i32
709
718
  /** Candidates tried per position. */
710
- readonly maxChain: i32
719
+ private readonly maxChain: i32
711
720
  /** A match this long is taken without looking further. */
712
- readonly niceLength: i32
721
+ private readonly niceLength: i32
713
722
  /** A match shorter than this is checked against the next position's (levels 4 to 9); 0 for greedy. */
714
- readonly lazyLength: i32
723
+ private readonly lazyLength: i32
715
724
  /** The shortest match taken: 4 at levels 1 to 3, whose hash covers four bytes, else 3. */
716
- readonly minMatch: i32
725
+ private readonly minMatch: i32
717
726
  /** A table position is an index of the current input plus this. */
718
- delta: i32
727
+ private delta: i32
719
728
  /** The next call's `delta` floor: past every position the tables hold, by a whole window. */
720
- base: i32
729
+ private base: i32
721
730
  /** How many of `tokens` the block holds. */
722
- count: i32
731
+ private count: i32
723
732
  /** How many entries of `rle` the block uses. */
724
- rleCount: i32
733
+ private rleCount: i32
725
734
 
726
- /** A compressor at `level`, 0 to 9; another level panics. */
735
+ /**
736
+ * A compressor at `level`, 0 to 9; another level panics.
737
+ *
738
+ * @param level How hard to compress: `DEFLATE_STORE` (0) stores,
739
+ * `DEFLATE_FAST` (1) to 3 match greedily, 4 to `DEFLATE_BEST` (9) match
740
+ * lazily on longer chains, `DEFLATE_DEFAULT` being 6.
741
+ */
727
742
  constructor(level: i32) {
728
743
  if (level < 0 || level > 9) {
729
744
  panic(`Deflater: level ${level} is outside 0 to 9`)
@@ -861,6 +876,7 @@ export class Deflater {
861
876
  }
862
877
 
863
878
  /**
879
+ * @internal Called by `GzipWriter` (std/compress/gzip.ts).
864
880
  * Forgets the history: the next call starts a stream from nothing. Only a
865
881
  * streaming writer needs it; `compress` starts a new stream on every call.
866
882
  */
@@ -874,6 +890,16 @@ export class Deflater {
874
890
  * DEFLATE stream, and answers where it ends, or `DEFLATE_NO_ROOM` when
875
891
  * `out` cannot hold it: `deflateBound(len)` bytes always can. A window
876
892
  * outside its array panics. Allocates nothing.
893
+ *
894
+ * @param src The array that holds the data to compress.
895
+ * @param off Where in `src` the data starts.
896
+ * @param len How many bytes of data to compress.
897
+ * @param out The caller's array the stream is written into.
898
+ * @param at Where in `out` the stream starts.
899
+ * @returns The index in `out` one past the stream's last byte, so the
900
+ * stream is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
901
+ * bytes of `out` from `at` may have been overwritten and mean nothing.
902
+ * Each call is a whole stream: nothing carries over to the next.
877
903
  */
878
904
  compress(src: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
879
905
  deflateWindow("Deflater.compress", src, off, len)
@@ -890,6 +916,7 @@ export class Deflater {
890
916
  }
891
917
 
892
918
  /**
919
+ * @internal Called by `GzipWriter` (std/compress/gzip.ts).
893
920
  * Starts a stream whose history begins at index `start` of the next input:
894
921
  * every position the tables hold is pushed a window behind it.
895
922
  */
@@ -903,6 +930,7 @@ export class Deflater {
903
930
  }
904
931
 
905
932
  /**
933
+ * @internal Called by `GzipWriter` (std/compress/gzip.ts).
906
934
  * Ends a stream whose input ended at index `end`: the next stream's
907
935
  * positions start a window past every position the tables now hold.
908
936
  */
@@ -911,6 +939,7 @@ export class Deflater {
911
939
  }
912
940
 
913
941
  /**
942
+ * @internal Called by `GzipWriter` (std/compress/gzip.ts).
914
943
  * Moves the tables' positions down by `shift`, as the input they index
915
944
  * moves down by `shift` bytes; a position that falls below 0 is dropped.
916
945
  * The streaming writer calls it when it slides its window.
@@ -920,6 +949,7 @@ export class Deflater {
920
949
  }
921
950
 
922
951
  /**
952
+ * @internal Called by `GzipWriter` (std/compress/gzip.ts).
923
953
  * Writes the whole bytes of the bit buffer, and the last partial byte
924
954
  * padded with zeros, into `out` at `op`; answers the end, or
925
955
  * `DEFLATE_NO_ROOM`.
@@ -946,7 +976,7 @@ export class Deflater {
946
976
  * storing whole words into `out` at `op`; answers the new `op`. The caller
947
977
  * has checked the room.
948
978
  */
949
- putBits(out: u8[], op: i32, v: i32, n: i32): i32 {
979
+ private putBits(out: u8[], op: i32, v: i32, n: i32): i32 {
950
980
  let bb: u64 = this.bitBuf | (toU64(toU32(v)) << toU64(this.bitCount))
951
981
  let bc: i32 = this.bitCount + n
952
982
  let p: i32 = op
@@ -970,7 +1000,7 @@ export class Deflater {
970
1000
  * partial byte's padding: the byte alignment a stored block's `LEN`
971
1001
  * starts at. Answers the new `op`; the caller has checked the room.
972
1002
  */
973
- alignBits(out: u8[], op: i32): i32 {
1003
+ private alignBits(out: u8[], op: i32): i32 {
974
1004
  let p: i32 = op
975
1005
  let bb: u64 = this.bitBuf
976
1006
  let bc: i32 = this.bitCount
@@ -987,6 +1017,7 @@ export class Deflater {
987
1017
  }
988
1018
 
989
1019
  /**
1020
+ * @internal Called by `GzipWriter` (std/compress/gzip.ts).
990
1021
  * Compresses `src[start .. end)` as blocks into `out[op .. oe)`, matching
991
1022
  * back as far as `histStart`; `final` marks the last block BFINAL. Answers
992
1023
  * the new output position, with up to 31 bits still in the bit buffer, or
@@ -1046,7 +1077,7 @@ export class Deflater {
1046
1077
  * window above the floor again; a position more than a window behind `p`
1047
1078
  * becomes 0, which is never a candidate.
1048
1079
  */
1049
- rebase(p: i32): void {
1080
+ private rebase(p: i32): void {
1050
1081
  const shift: i32 = p + this.delta - (DEFLATE_WINDOW + 1)
1051
1082
  const head: i32[] = this.head
1052
1083
  const prev: i32[] = this.prev
@@ -1067,7 +1098,7 @@ export class Deflater {
1067
1098
  * `len << 16 | dist`, or 0 for none at least `minMatch` long. `p + 4 <=
1068
1099
  * end`, which the caller checks.
1069
1100
  */
1070
- longest(src: u8[], p: i32, end: i32, histStart: i32): i32 {
1101
+ private longest(src: u8[], p: i32, end: i32, histStart: i32): i32 {
1071
1102
  const head: i32[] = this.head
1072
1103
  const prev: i32[] = this.prev
1073
1104
  if (
@@ -1143,7 +1174,7 @@ export class Deflater {
1143
1174
  }
1144
1175
 
1145
1176
  /** Records `p` in the chains without searching; `p + 4 <= src.length`. */
1146
- insert(src: u8[], p: i32): void {
1177
+ private insert(src: u8[], p: i32): void {
1147
1178
  const head: i32[] = this.head
1148
1179
  const prev: i32[] = this.prev
1149
1180
  if (toI32(head.length) !== DEFLATE_HASH_SIZE || toI32(prev.length) !== DEFLATE_WINDOW) {
@@ -1160,14 +1191,14 @@ export class Deflater {
1160
1191
  }
1161
1192
 
1162
1193
  /** Clears the block's symbols and frequencies. */
1163
- startBlock(): void {
1194
+ private startBlock(): void {
1164
1195
  this.litFreq.fill(0)
1165
1196
  this.distFreq.fill(0)
1166
1197
  this.count = 0
1167
1198
  }
1168
1199
 
1169
1200
  /** Records the match `len`, `dist` as the block's next symbol. */
1170
- addMatch(len: i32, dist: i32): void {
1201
+ private addMatch(len: i32, dist: i32): void {
1171
1202
  const tokens: i32[] = this.tokens
1172
1203
  const n: i32 = this.count
1173
1204
  if (n < 0 || n >= toI32(tokens.length) || len < 3 || len > 258 || dist < 1 || dist > DEFLATE_WINDOW) {
@@ -1187,7 +1218,7 @@ export class Deflater {
1187
1218
  }
1188
1219
 
1189
1220
  /** Records the literal `b` as the block's next symbol. */
1190
- addLiteral(b: i32): void {
1221
+ private addLiteral(b: i32): void {
1191
1222
  const tokens: i32[] = this.tokens
1192
1223
  const n: i32 = this.count
1193
1224
  if (n < 0 || n >= toI32(tokens.length) || b < 0 || b > 255) {
@@ -1205,7 +1236,7 @@ export class Deflater {
1205
1236
  * finder, the chains and the symbol bookkeeping are written out in this one
1206
1237
  * loop over locals, because this is the fast level's whole cost.
1207
1238
  */
1208
- collectGreedy(src: u8[], start: i32, end: i32, histStart: i32): i32 {
1239
+ private collectGreedy(src: u8[], start: i32, end: i32, histStart: i32): i32 {
1209
1240
  this.startBlock()
1210
1241
  const head: i32[] = this.head
1211
1242
  const prev: i32[] = this.prev
@@ -1313,7 +1344,7 @@ export class Deflater {
1313
1344
  * Fills the block's symbols from `p`, deferring each match by a position
1314
1345
  * while the next one finds a longer one; answers where the block ends.
1315
1346
  */
1316
- collectLazy(src: u8[], start: i32, end: i32, histStart: i32): i32 {
1347
+ private collectLazy(src: u8[], start: i32, end: i32, histStart: i32): i32 {
1317
1348
  this.startBlock()
1318
1349
  let p: i32 = start
1319
1350
  const room: i32 = DEFLATE_BLOCK_TOKENS - 2
@@ -1358,7 +1389,7 @@ export class Deflater {
1358
1389
  * `pending` bits: each piece of up to 65,535 bytes has three header bits,
1359
1390
  * the padding to a byte and four bytes of `LEN` / `NLEN`.
1360
1391
  */
1361
- storedBits(n: i32, pending: i32): i32 {
1392
+ private storedBits(n: i32, pending: i32): i32 {
1362
1393
  let bits: i32 = 0
1363
1394
  let left: i32 = n
1364
1395
  let bc: i32 = pending
@@ -1375,6 +1406,7 @@ export class Deflater {
1375
1406
  }
1376
1407
 
1377
1408
  /**
1409
+ * @internal Called by `GzipWriter` (std/compress/gzip.ts).
1378
1410
  * Writes `src[start .. end)` as stored blocks, the last BFINAL when
1379
1411
  * `final`; answers the new position, or -1 without writing when it does
1380
1412
  * not fit in `out[op .. oe)`.
@@ -1411,7 +1443,7 @@ export class Deflater {
1411
1443
  * `hlit` literal/length and `hdist` distance lengths, and their
1412
1444
  * frequencies in `clFreq`.
1413
1445
  */
1414
- buildRle(hlit: i32, hdist: i32): void {
1446
+ private buildRle(hlit: i32, hdist: i32): void {
1415
1447
  const rle: i32[] = this.rle
1416
1448
  const freq: i32[] = this.clFreq
1417
1449
  const ll: i32[] = this.litLens
@@ -1479,7 +1511,7 @@ export class Deflater {
1479
1511
  * dynamic, a fixed and a stored block; answers the new position, or -1
1480
1512
  * without writing when that does not fit in `out[op .. oe)`.
1481
1513
  */
1482
- writeBlock(src: u8[], start: i32, end: i32, final: boolean, out: u8[], op: i32, oe: i32): i32 {
1514
+ private writeBlock(src: u8[], start: i32, end: i32, final: boolean, out: u8[], op: i32, oe: i32): i32 {
1483
1515
  const lf: i32[] = this.litFreq
1484
1516
  const df: i32[] = this.distFreq
1485
1517
  lf[256] = 1
@@ -1585,7 +1617,7 @@ export class Deflater {
1585
1617
  * code into `out` at `op`; answers the new position. The room was checked
1586
1618
  * by the block's exact size.
1587
1619
  */
1588
- writeSymbols(lc: i32[], ll: i32[], dc: i32[], dl: i32[], out: u8[], op: i32): i32 {
1620
+ private writeSymbols(lc: i32[], ll: i32[], dc: i32[], dl: i32[], out: u8[], op: i32): i32 {
1589
1621
  const tokens: i32[] = this.tokens
1590
1622
  const n: i32 = this.count
1591
1623
  const lsym: i32[] = this.lengthSymbol
@@ -1664,6 +1696,10 @@ export class Deflater {
1664
1696
  * bits a byte, the block's header and end code, and the eight bytes of slack
1665
1697
  * its word-wide bit writer stores past the last whole byte. A negative `n`
1666
1698
  * answers the bound of 0.
1699
+ *
1700
+ * @param n The length of the input, in bytes.
1701
+ * @returns A size in bytes, not a position: a `room` of this many always
1702
+ * holds the block.
1667
1703
  */
1668
1704
  export const fixedDeflateBound = (n: i32): i32 => {
1669
1705
  if (n < 0) {
@@ -1682,17 +1718,17 @@ export const fixedDeflateBound = (n: i32): i32 => {
1682
1718
  */
1683
1719
  export class FixedDeflater {
1684
1720
  /** `head[h]`: one past the last position, from the input's start, whose four bytes hash to `h`; 0 for none. */
1685
- readonly head: i32[]
1721
+ private readonly head: i32[]
1686
1722
  /** Per byte: its fixed code, bit-reversed, below bit 16, and the code's length above. */
1687
- readonly literal: i32[]
1723
+ private readonly literal: i32[]
1688
1724
  /** Per length - 3: the length symbol's code and then its extra bits, below bit 16, and how many bits above. */
1689
- readonly length: i32[]
1725
+ private readonly length: i32[]
1690
1726
  /** Distance to distance symbol, as `Deflater.distSymbol` maps it. */
1691
- readonly distSymbol: i32[]
1727
+ private readonly distSymbol: i32[]
1692
1728
  /** Per distance symbol: its five-bit code, bit-reversed; its base distance; its extra bits. */
1693
- readonly distCode: i32[]
1694
- readonly distBase: i32[]
1695
- readonly distExtra: i32[]
1729
+ private readonly distCode: i32[]
1730
+ private readonly distBase: i32[]
1731
+ private readonly distExtra: i32[]
1696
1732
 
1697
1733
  /** The tables, once. */
1698
1734
  constructor() {
@@ -1753,6 +1789,18 @@ export class FixedDeflater {
1753
1789
  * the output when it is smaller passes the most it will take, and an input
1754
1790
  * that does not compress is abandoned part way. `fixedDeflateBound(len)`
1755
1791
  * always fits. A window outside its array panics. Allocates nothing.
1792
+ *
1793
+ * @param src The array that holds the data to compress.
1794
+ * @param off Where in `src` the data starts.
1795
+ * @param len How many bytes of data to compress.
1796
+ * @param out The caller's array the block is written into.
1797
+ * @param at Where in `out` the block starts.
1798
+ * @param room How many bytes of `out` from `at` the block may use; the
1799
+ * window `out[at .. at + room)` must lie inside `out`.
1800
+ * @returns The index in `out` one past the block's last byte, so the
1801
+ * stream is `out[at .. result)`; or `DEFLATE_NO_ROOM`, after which the
1802
+ * bytes of `out[at .. at + room)` may have been overwritten and mean
1803
+ * nothing.
1756
1804
  */
1757
1805
  compress(src: u8[], off: i32, len: i32, out: u8[], at: i32, room: i32): i32 {
1758
1806
  deflateWindow("FixedDeflater.compress", src, off, len)
@@ -1884,21 +1932,29 @@ export class FixedDeflater {
1884
1932
  */
1885
1933
  export class InflateTable {
1886
1934
  /**
1935
+ * @internal Used by `Inflater`.
1887
1936
  * `entries[peek]`: value << 16 | extra << 11 | kind << 8 | length. The low
1888
1937
  * byte is the length alone, so a bit buffer shifts by an entry as it is.
1889
1938
  */
1890
1939
  readonly entries: i32[]
1891
- /** The prefix the table indexes. */
1940
+ /** @internal Used by `Inflater`. The prefix the table indexes. */
1892
1941
  readonly bits: i32
1893
- /** How many codes have each length 0 to 15. */
1942
+ /** @internal Used by `Inflater`. How many codes have each length 0 to 15. */
1894
1943
  readonly count: i32[]
1895
- /** The symbols, ordered by code. */
1944
+ /** @internal Used by `Inflater`. The symbols, ordered by code. */
1896
1945
  readonly symbols: i32[]
1897
1946
 
1898
1947
  /**
1899
1948
  * A table indexing `bits` bits, for an alphabet of up to `symbols`
1900
1949
  * symbols, with room for `capacity` entries: the main table and its
1901
- * subtables.
1950
+ * subtables. The arrays are allocated here and filled by `Inflater`.
1951
+ *
1952
+ * @param bits How many bits of the next code the main table indexes at
1953
+ * once; a longer code goes through a subtable.
1954
+ * @param symbols The size of the alphabet the table decodes, such as 288
1955
+ * for literal/length codes or 32 for distances.
1956
+ * @param capacity How many entries to allocate for the main table and
1957
+ * all its subtables; `Inflater` passes zlib's `enough` figures.
1902
1958
  */
1903
1959
  constructor(bits: i32, symbols: i32, capacity: i32) {
1904
1960
  this.bits = bits
@@ -1936,38 +1992,42 @@ const INF_TABLE_CODELEN: i32 = 2
1936
1992
  * Not shared between threads.
1937
1993
  */
1938
1994
  export class Inflater {
1939
- readonly lit: InflateTable
1940
- readonly dist: InflateTable
1941
- readonly codeLengths: InflateTable
1942
- readonly fixedLit: InflateTable
1943
- readonly fixedDist: InflateTable
1995
+ private readonly lit: InflateTable
1996
+ private readonly dist: InflateTable
1997
+ private readonly codeLengths: InflateTable
1998
+ private readonly fixedLit: InflateTable
1999
+ private readonly fixedDist: InflateTable
1944
2000
  /** The lengths a dynamic header sends, literal/length then distance. */
1945
- readonly lens: i32[]
2001
+ private readonly lens: i32[]
1946
2002
  /** The order-indexed code-length code lengths. */
1947
- readonly clLens: i32[]
2003
+ private readonly clLens: i32[]
1948
2004
  /** Scratch for building a table. */
1949
- readonly offsets: i32[]
2005
+ private readonly offsets: i32[]
1950
2006
  /** Each symbol's table entry with a length of 0, per kind of table: `entryOf(kind, s, 0)`. */
1951
- readonly litEntries: i32[]
1952
- readonly distEntries: i32[]
1953
- readonly clEntries: i32[]
1954
- readonly lengthBase: i32[]
1955
- readonly lengthExtra: i32[]
1956
- readonly distBase: i32[]
1957
- readonly distExtra: i32[]
2007
+ private readonly litEntries: i32[]
2008
+ private readonly distEntries: i32[]
2009
+ private readonly clEntries: i32[]
2010
+ private readonly lengthBase: i32[]
2011
+ private readonly lengthExtra: i32[]
2012
+ private readonly distBase: i32[]
2013
+ private readonly distExtra: i32[]
1958
2014
  /** Where `inflate` resumes: the block header, a stored block, a Huffman block, or done. */
1959
- state: i32
2015
+ private state: i32
1960
2016
  /** Whether the block being read is the final one. */
1961
- last: boolean
2017
+ private last: boolean
1962
2018
  /** Whether the Huffman block uses the fixed code. */
1963
- fixed: boolean
2019
+ private fixed: boolean
1964
2020
  /** Bytes of the current stored block still to copy. */
1965
- storedLeft: i32
2021
+ private storedLeft: i32
1966
2022
  /** Bits read but not consumed, low bit first, and how many. */
1967
- bitBuf: u64
1968
- bitCount: i32
1969
- /** Where the last `inflate` stopped in its input and its output. */
2023
+ private bitBuf: u64
2024
+ private bitCount: i32
2025
+ /**
2026
+ * Where the last `inflate` stopped in its input: the first byte it did not
2027
+ * use, or after `INFLATE_DONE` the first byte after the final block.
2028
+ */
1970
2029
  inPos: i32
2030
+ /** Where the last `inflate` stopped in its output: one past the last byte it wrote. */
1971
2031
  outPos: i32
1972
2032
  /** The input bytes the last `decompress` read, its final block included. */
1973
2033
  consumed: i32
@@ -2030,7 +2090,12 @@ export class Inflater {
2030
2090
  }
2031
2091
  }
2032
2092
 
2033
- /** Starts a new stream: the next `inflate` reads a first block header. */
2093
+ /**
2094
+ * Starts a new stream: the next `inflate` reads a first block header and
2095
+ * whatever the last stream left in the bit buffer is dropped. A new
2096
+ * `Inflater` is already at the start, and `decompress` resets itself;
2097
+ * call this before reusing one for another stream through `inflate`.
2098
+ */
2034
2099
  reset(): void {
2035
2100
  this.state = INF_S_BLOCK
2036
2101
  this.last = false
@@ -2043,7 +2108,7 @@ export class Inflater {
2043
2108
  /**
2044
2109
  * The table entry for symbol `s` of a table of `kind`.
2045
2110
  */
2046
- entryOf(kind: i32, s: i32, len: i32): i32 {
2111
+ private entryOf(kind: i32, s: i32, len: i32): i32 {
2047
2112
  if (kind === INF_TABLE_CODELEN) {
2048
2113
  return (s << 16) | len
2049
2114
  }
@@ -2071,7 +2136,7 @@ export class Inflater {
2071
2136
  * (an incomplete set of a single one-bit code is allowed, as RFC 1951
2072
2137
  * §3.2.7 allows one distance code).
2073
2138
  */
2074
- build(t: InflateTable, lens: i32[], from: i32, n: i32, kind: i32): i32 {
2139
+ private build(t: InflateTable, lens: i32[], from: i32, n: i32, kind: i32): i32 {
2075
2140
  const count: i32[] = t.count
2076
2141
  const syms: i32[] = t.symbols
2077
2142
  const entries: i32[] = t.entries
@@ -2276,7 +2341,7 @@ export class Inflater {
2276
2341
  * symbol with its true code length, or -1 when the bits run out before a
2277
2342
  * code ends (`bc` too small), or -2 when no code matches.
2278
2343
  */
2279
- slowDecode(t: InflateTable, kind: i32, bb: u64, bc: i32): i32 {
2344
+ private slowDecode(t: InflateTable, kind: i32, bb: u64, bc: i32): i32 {
2280
2345
  const count: i32[] = t.count
2281
2346
  const syms: i32[] = t.symbols
2282
2347
  let code: i32 = 0
@@ -2305,7 +2370,7 @@ export class Inflater {
2305
2370
  }
2306
2371
 
2307
2372
  /** Reads bytes of `src` into the bit buffer from `inPos` while it has room and `end` allows. */
2308
- fill(src: u8[], end: i32): void {
2373
+ private fill(src: u8[], end: i32): void {
2309
2374
  let bb: u64 = this.bitBuf
2310
2375
  let bc: i32 = this.bitCount
2311
2376
  let ip: i32 = this.inPos
@@ -2331,7 +2396,7 @@ export class Inflater {
2331
2396
  }
2332
2397
 
2333
2398
  /** The low `n` bits of the bit buffer, consumed; the caller has checked that `n` are there. */
2334
- take(n: i32): i32 {
2399
+ private take(n: i32): i32 {
2335
2400
  const v: i32 = toI32(this.bitBuf & ((toU64(1) << toU64(n)) - toU64(1)))
2336
2401
  this.bitBuf = this.bitBuf >> toU64(n)
2337
2402
  this.bitCount = this.bitCount - n
@@ -2343,7 +2408,7 @@ export class Inflater {
2343
2408
  * the bits to the next byte boundary: where a stored block's `LEN`, or the
2344
2409
  * framing after the final block, begins.
2345
2410
  */
2346
- unread(): void {
2411
+ private unread(): void {
2347
2412
  const drop: i32 = this.bitCount & 7
2348
2413
  this.bitBuf = this.bitBuf >> toU64(drop)
2349
2414
  this.bitCount = this.bitCount - drop
@@ -2357,7 +2422,7 @@ export class Inflater {
2357
2422
  * and builds its tables. Answers 0, `INFLATE_NEED_INPUT` when the input ran
2358
2423
  * out first (nothing is consumed then: the caller rewinds), or an error.
2359
2424
  */
2360
- readDynamic(src: u8[], end: i32): i32 {
2425
+ private readDynamic(src: u8[], end: i32): i32 {
2361
2426
  // The bit buffer in locals: the lengths are stored into an `i32[]`,
2362
2427
  // which the fields could alias, and every symbol would reload them.
2363
2428
  let bb: u64 = this.bitBuf
@@ -2512,14 +2577,29 @@ export class Inflater {
2512
2577
 
2513
2578
  /**
2514
2579
  * Decodes from `src[ip .. inEnd)` into `out[op .. outEnd)`, resuming where
2515
- * the last call stopped; a match may reach back to `histStart`. `more`
2516
- * says whether input may follow `inEnd`: with it, running out is
2517
- * `INFLATE_NEED_INPUT` (every byte up to `inPos` is used, and the bit
2518
- * buffer keeps what it read ahead), without it `DEFLATE_ERR_TRUNCATED`.
2519
- * Answers `INFLATE_DONE` once the final block has ended, with `inPos` the
2520
- * first byte after it; `INFLATE_NEED_OUTPUT` when the next symbol does not
2521
- * fit; or a negative `DEFLATE_ERR_*`. `inPos` and `outPos` say where it
2522
- * stopped. Windows outside their arrays panic.
2580
+ * the last call stopped: the resumable step under `decompress`, for a
2581
+ * stream that arrives in pieces or an output buffer that is drained
2582
+ * between calls. `GzipDecompressor`, `GzipReader` and `ZlibDecompressor`
2583
+ * are built on it. `inPos` and `outPos` say where it stopped. Windows
2584
+ * outside their arrays panic. Allocates nothing.
2585
+ *
2586
+ * @param src The array that holds the compressed input.
2587
+ * @param ip Where in `src` this call's input starts; the bytes before it
2588
+ * that an earlier call read ahead are in the bit buffer, not re-read.
2589
+ * @param inEnd One past this call's last input byte.
2590
+ * @param more Whether input may follow `inEnd`: with it, running out is
2591
+ * `INFLATE_NEED_INPUT` (every byte up to `inPos` is used, and the bit
2592
+ * buffer keeps what it read ahead), without it `DEFLATE_ERR_TRUNCATED`.
2593
+ * @param out The array the decoded bytes are written into.
2594
+ * @param op Where in `out` this call writes first.
2595
+ * @param outEnd One past the last byte of `out` this call may write.
2596
+ * @param histStart The earliest byte of `out` a match may copy from; the
2597
+ * bytes from it to `op` must be the stream's earlier output, up to the
2598
+ * 32 KB window.
2599
+ * @returns `INFLATE_DONE` once the final block has ended, with `inPos` the
2600
+ * first byte after it; `INFLATE_NEED_INPUT` when the input ran out with
2601
+ * `more`; `INFLATE_NEED_OUTPUT` when the next symbol does not fit before
2602
+ * `outEnd`; or a negative `DEFLATE_ERR_*` for a malformed stream.
2523
2603
  */
2524
2604
  inflate(
2525
2605
  src: u8[],
@@ -2550,7 +2630,7 @@ export class Inflater {
2550
2630
  }
2551
2631
 
2552
2632
  /** `inflate`'s state machine, once its windows are checked. */
2553
- step(src: u8[], inEnd: i32, more: boolean, out: u8[], outEnd: i32, histStart: i32): i32 {
2633
+ private step(src: u8[], inEnd: i32, more: boolean, out: u8[], outEnd: i32, histStart: i32): i32 {
2554
2634
  while (true) {
2555
2635
  const s: i32 = this.state
2556
2636
  if (s === INF_S_DONE) {
@@ -2648,7 +2728,7 @@ export class Inflater {
2648
2728
  * (answers 0, the state back at a block header), or until the input or
2649
2729
  * the output runs short or the stream is malformed (answers that code).
2650
2730
  */
2651
- huffman(src: u8[], inEnd: i32, more: boolean, out: u8[], outEnd: i32, histStart: i32): i32 {
2731
+ private huffman(src: u8[], inEnd: i32, more: boolean, out: u8[], outEnd: i32, histStart: i32): i32 {
2652
2732
  const lt: InflateTable = this.fixed ? this.fixedLit : this.lit
2653
2733
  const dt: InflateTable = this.fixed ? this.fixedDist : this.dist
2654
2734
  const le: i32[] = lt.entries
@@ -2954,7 +3034,7 @@ export class Inflater {
2954
3034
  * says: -1 when the bits run out before the code ends, -2 when no code
2955
3035
  * matches. A code is decoded only once all its bits are here.
2956
3036
  */
2957
- lookup(t: InflateTable, kind: i32, entries: i32[], bb: u64, bc: i32): i32 {
3037
+ private lookup(t: InflateTable, kind: i32, entries: i32[], bb: u64, bc: i32): i32 {
2958
3038
  const mask: u64 = toU64((1 << t.bits) - 1)
2959
3039
  let e: i32 = entries[toI32(bb & mask)]
2960
3040
  if (((e >> 8) & 7) === INF_SUB) {
@@ -2976,6 +3056,19 @@ export class Inflater {
2976
3056
  * block, or another negative `DEFLATE_ERR_*` for a malformed stream. A
2977
3057
  * window outside its array, or a negative `limit`, panics. Allocates
2978
3058
  * nothing.
3059
+ *
3060
+ * @param src The array that holds the raw DEFLATE stream.
3061
+ * @param off Where in `src` the stream starts.
3062
+ * @param len How many bytes the stream is; every one must belong to it,
3063
+ * since a byte after the final block is `DEFLATE_ERR_TRAILING`.
3064
+ * @param out The caller's array the data is written into.
3065
+ * @param at Where in `out` the data starts.
3066
+ * @param limit The most bytes of data to accept; a longer stream is
3067
+ * `DEFLATE_ERR_OUTPUT_LIMIT`, as is one that reaches the end of `out`.
3068
+ * @returns The index in `out` one past the data's last byte, so the data
3069
+ * is `out[at .. result)`; or a negative `DEFLATE_ERR_*` code. After an
3070
+ * error the bytes of `out` from `at` may have been overwritten and mean
3071
+ * nothing. Either way `consumed` says how far into `src` it read.
2979
3072
  */
2980
3073
  decompress(src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 {
2981
3074
  deflateWindow("Inflater.decompress", src, off, len)
@@ -3005,10 +3098,30 @@ export class Inflater {
3005
3098
  * `Deflater.compress` at `level`, building the compressor for this call
3006
3099
  * alone: 330 KB allocated and filled each time, so keep a `Deflater` for
3007
3100
  * anything repeated.
3101
+ *
3102
+ * @param src The array that holds the data to compress.
3103
+ * @param off Where in `src` the data starts.
3104
+ * @param len How many bytes of data to compress.
3105
+ * @param out The caller's array the stream is written into.
3106
+ * @param at Where in `out` the stream starts.
3107
+ * @param level How hard to compress, 0 to 9; another level panics.
3108
+ * @returns The index in `out` one past the stream's last byte, or
3109
+ * `DEFLATE_NO_ROOM`, as `Deflater.compress` answers.
3008
3110
  */
3009
3111
  export const deflateCompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, level: i32): i32 =>
3010
3112
  new Deflater(level).compress(src, off, len, out, at)
3011
3113
 
3012
- /** `Inflater.decompress`, building the decompressor for this call alone. */
3114
+ /**
3115
+ * `Inflater.decompress`, building the decompressor for this call alone.
3116
+ *
3117
+ * @param src The array that holds the raw DEFLATE stream.
3118
+ * @param off Where in `src` the stream starts.
3119
+ * @param len How many bytes the stream is.
3120
+ * @param out The caller's array the data is written into.
3121
+ * @param at Where in `out` the data starts.
3122
+ * @param limit The most bytes of data to accept.
3123
+ * @returns The index in `out` one past the data's last byte, or a negative
3124
+ * `DEFLATE_ERR_*` code, as `Inflater.decompress` answers.
3125
+ */
3013
3126
  export const deflateDecompress = (src: u8[], off: i32, len: i32, out: u8[], at: i32, limit: i32): i32 =>
3014
3127
  new Inflater().decompress(src, off, len, out, at, limit)