nish 0.35.0 → 0.37.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 (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
@@ -4,7 +4,7 @@
4
4
  * body with `Content-Encoding: gzip`, for any of the three servers.
5
5
  *
6
6
  * import { DEFLATE_FAST } from "nish/compress/deflate";
7
- * import { HTTP_CODING_GZIP, HttpCompressor } from "nish/net/http-compress";
7
+ * import { HTTP_OFFER_GZIP, HttpCompressor, httpAcceptEncoding } from "nish/net/http-compress";
8
8
  *
9
9
  * const enc = new HttpCompressor(DEFLATE_FAST, 1024, 65536); // once per thread
10
10
  * // HTTP/1.1, in one call:
@@ -15,7 +15,7 @@
15
15
  * if (m >= 0) {
16
16
  * enc.addFields(names, values);
17
17
  * h2.respond(id, 200, enc.names, enc.values, false);
18
- * h2.write(id, enc.output, 0, m);
18
+ * h2.writeData(id, enc.output, 0, m, true);
19
19
  * }
20
20
  *
21
21
  * **Opt-in.** Nothing here runs unless the program asks: it decides which
@@ -23,18 +23,19 @@
23
23
  * its `minLength`, longer than its `maxLength`, or that gzip would not make
24
24
  * smaller, answering -1 so the program sends the bytes as they are.
25
25
  *
26
- * **Compression that pays for itself.** A body is sent compressed only when
27
- * it saves at least `minSaving` bytes and `minSavingPercent` percent of the
28
- * body; otherwise the compressed bytes are dropped and the body goes as it
29
- * is. With `segmentMode` set, `respondEncoded` also sends it compressed only
30
- * when the whole response, head included, takes fewer TCP segments of `mss`
31
- * bytes (the connection's `mss` if set, else the compressor's, 1448 by
32
- * default): a 1300-byte body that gzips to 700 still fits in one segment
33
- * either way. After each call `decision` says why (`HTTP_COMPRESS_SENT`,
34
- * `_NOT_ACCEPTED`, `_TOO_SMALL`, `_TOO_LARGE`, `_TOO_LITTLE_SAVED`,
35
- * `_NO_FEWER_SEGMENTS`), for a program to count. A response whose identity
36
- * form already fits one segment is `_NO_FEWER_SEGMENTS` without running gzip
37
- * at all, as no compression could take fewer.
26
+ * **Compression that pays for itself.** A body is sent compressed only when it
27
+ * saves at least `minSaving` bytes and `minSavingPercent` percent of the body;
28
+ * otherwise the compressed bytes are dropped and the body goes as it is. With
29
+ * `segmentMode` set, `respondEncoded` also sends it compressed only when the
30
+ * whole response, head included, takes fewer TCP segments of `mss` bytes (the
31
+ * connection's `mss` if set, else the compressor's, 1448 by default): a
32
+ * 1300-byte body that gzips to 700 still fits in one segment either way.
33
+ *
34
+ * After each call `decision` says why (`HTTP_COMPRESS_SENT`, `_NOT_ACCEPTED`,
35
+ * `_TOO_SMALL`, `_TOO_LARGE`, `_TOO_LITTLE_SAVED`, `_NO_FEWER_SEGMENTS`), for a
36
+ * program to count. A response whose identity form already fits one segment is
37
+ * `_NO_FEWER_SEGMENTS` without running gzip at all, as no compression could
38
+ * take fewer.
38
39
  *
39
40
  * **Two encoders.** Bodies up to `fixedLimit` (16 KB) go through
40
41
  * `GzipFixedCompressor`: one fixed-Huffman block, a 16 KB window, no state
@@ -51,15 +52,17 @@
51
52
  *
52
53
  * **Negotiation** (`httpAcceptEncoding`) follows §12.5.3: each listed coding
53
54
  * with its q-value, `x-gzip` as `gzip`, `*` for every coding not listed, and
54
- * `identity` acceptable unless it is given q=0, directly or through `*`. Of
55
- * the codings the program offers, the most preferred with q above 0 wins, the
56
- * program's order (gzip, then deflate) breaking ties; it beats `identity` at
57
- * an equal q, and beats an `identity` the field does not mention at any q. With no field at all the answer is
58
- * `HTTP_CODING_IDENTITY` (the RFC allows any coding then, but a client that
59
- * says nothing is safest sent nothing it may not read); an empty field asks
60
- * for identity too. `HTTP_CODING_NONE` means nothing offered is acceptable
61
- * and identity is refused: a server may answer 406 or send identity anyway.
62
- * A malformed element is skipped. The parser allocates nothing.
55
+ * `identity` acceptable unless it is given q=0, directly or through `*`.
56
+ *
57
+ * Of the codings the program offers, the most preferred with q above 0 wins,
58
+ * the program's order (gzip, then deflate) breaking ties; it beats `identity`
59
+ * at an equal q, and beats an `identity` the field does not mention at any q.
60
+ * With no field at all the answer is `HTTP_CODING_IDENTITY` (the RFC allows any
61
+ * coding then, but a client that says nothing is safest sent nothing it may not
62
+ * read); an empty field asks for identity too. `HTTP_CODING_NONE` means nothing
63
+ * offered is acceptable and identity is refused: a server may answer 406 or
64
+ * send identity anyway. A malformed element is skipped. The parser allocates
65
+ * nothing.
63
66
  *
64
67
  * **Compressing allocates nothing** and caches nothing between responses:
65
68
  * `HttpCompressor` holds a `GzipCompressor`, a `GzipFixedCompressor`, an
@@ -87,6 +90,7 @@ export const HTTP_CODING_NONE: i32 = -1
87
90
 
88
91
  /** `httpAcceptEncoding`'s `offered` bits: the program can send gzip, deflate. */
89
92
  export const HTTP_OFFER_GZIP: i32 = 1
93
+ /** The `offered` bit for deflate, which is negotiated but never produced here. */
90
94
  export const HTTP_OFFER_DEFLATE: i32 = 2
91
95
 
92
96
  /**
@@ -99,10 +103,18 @@ export const HTTP_OFFER_DEFLATE: i32 = 2
99
103
  * response that already fits one segment, decided without compressing.
100
104
  */
101
105
  export const HTTP_COMPRESS_SENT: i32 = 0
106
+ /** The client did not accept gzip, so the body went as it is; the default before any decision. */
102
107
  export const HTTP_COMPRESS_NOT_ACCEPTED: i32 = 1
108
+ /** The body was shorter than `minLength`, too small to be worth compressing. */
103
109
  export const HTTP_COMPRESS_TOO_SMALL: i32 = 2
110
+ /** The body was longer than `maxLength`, more than `output` was sized for. */
104
111
  export const HTTP_COMPRESS_TOO_LARGE: i32 = 3
112
+ /**
113
+ * Gzip ran but did not save `minSaving` bytes and `minSavingPercent` percent,
114
+ * or the saving asked for was the whole body, so no encoder ran.
115
+ */
105
116
  export const HTTP_COMPRESS_TOO_LITTLE_SAVED: i32 = 4
117
+ /** Under `segmentMode`, compressing would not have saved a TCP segment. */
106
118
  export const HTTP_COMPRESS_NO_FEWER_SEGMENTS: i32 = 5
107
119
 
108
120
  /**
@@ -111,7 +123,13 @@ export const HTTP_COMPRESS_NO_FEWER_SEGMENTS: i32 = 5
111
123
  * encoder always, or the constructor's level always.
112
124
  */
113
125
  export const HTTP_ENCODER_AUTO: i32 = 0
126
+ /**
127
+ * The fixed-Huffman encoder for every body `compress` takes: fastest, at some
128
+ * cost in size on large bodies. `compressAnyway` falls back to the
129
+ * `GzipCompressor` when it fails.
130
+ */
114
131
  export const HTTP_ENCODER_FIXED: i32 = 1
132
+ /** Always the `GzipCompressor` at the constructor's level, whatever the body's length. */
115
133
  export const HTTP_ENCODER_LEVEL: i32 = 2
116
134
 
117
135
  /**
@@ -124,7 +142,15 @@ export const HTTP_DEFAULT_MSS: i32 = 1448
124
142
  /** The bytes `content-encoding: gzip` adds to an HTTP/1.1 head: name, value, ": " and CRLF. */
125
143
  export const HTTP1_GZIP_FIELD_BYTES: i32 = 24
126
144
 
127
- /** The number of TCP segments `bytes` bytes take at `mss` bytes a segment. */
145
+ /**
146
+ * The number of TCP segments `bytes` bytes take at `mss` bytes a segment.
147
+ * A segment size of zero or less panics, since no count would mean anything.
148
+ *
149
+ * @param bytes the length of the whole response, head and body, in bytes
150
+ * @param mss the payload one segment carries, in bytes
151
+ * @returns how many segments the bytes fill, the last one possibly partial;
152
+ * 0 for no bytes
153
+ */
128
154
  export const httpSegments = (bytes: i32, mss: i32): i32 => {
129
155
  if (mss <= 0) {
130
156
  panic("httpSegments: a segment size of zero or less")
@@ -236,6 +262,14 @@ const httpOws = (c: i32): boolean => c === 32 || c === 9
236
262
  * HTTP_OFFER_DEFLATE`): `HTTP_CODING_GZIP`, `HTTP_CODING_DEFLATE`,
237
263
  * `HTTP_CODING_IDENTITY` or `HTTP_CODING_NONE`, by the rules in the module
238
264
  * comment. A window outside `buf` panics.
265
+ *
266
+ * @param buf the buffer holding the field value, such as the request head
267
+ * @param start the index in `buf` of the value's first octet
268
+ * @param end the index just past the value's last octet
269
+ * @param offered which codings the program can produce, `HTTP_OFFER_*` bits
270
+ * or 0 for none
271
+ * @returns the `HTTP_CODING_*` to send the body in, or `HTTP_CODING_NONE`
272
+ * when nothing offered is acceptable and identity is refused too
239
273
  */
240
274
  export const httpAcceptEncoding = (buf: u8[], start: i32, end: i32, offered: i32): i32 => {
241
275
  if (start < 0 || start > end || end > toI32(buf.length)) {
@@ -345,6 +379,11 @@ export const httpAcceptEncoding = (buf: u8[], start: i32, end: i32, offered: i32
345
379
  * body as it is: `identity;q=0`, or `*;q=0` with no `identity` listed. A
346
380
  * server should then send a coding the client takes even where it would not
347
381
  * bother (`HttpCompressor.compressAnyway`).
382
+ *
383
+ * @param buf the buffer holding the field value, such as the request head
384
+ * @param start the index in `buf` of the value's first octet
385
+ * @param end the index just past the value's last octet
386
+ * @returns true when the client said it cannot take the body uncompressed
348
387
  */
349
388
  export const httpIdentityRefused = (buf: u8[], start: i32, end: i32): boolean =>
350
389
  httpAcceptEncoding(buf, start, end, 0) === HTTP_CODING_NONE
@@ -363,27 +402,29 @@ const httpCompressBytes = (text: string): u8[] => {
363
402
  * the response fields that say so. One per thread; allocated once.
364
403
  */
365
404
  export class HttpCompressor {
366
- readonly gzip: GzipCompressor
405
+ private readonly gzip: GzipCompressor
367
406
  /** The small-body encoder: one fixed-Huffman block, nothing kept between calls. */
368
- readonly fixed: GzipFixedCompressor
407
+ private readonly fixed: GzipFixedCompressor
369
408
  /** Where `compress` writes; valid until the next call. */
370
409
  readonly output: u8[]
371
410
  /** The fields `addFields` built: the program's, then `Content-Encoding` and `Vary`. */
372
411
  readonly names: u8[][]
412
+ /** The value of each field in `names`, at the same index; pass both to `respond`. */
373
413
  readonly values: u8[][]
374
- readonly contentEncodingName: u8[]
375
- readonly gzipValue: u8[]
376
- readonly varyName: u8[]
377
- readonly acceptEncodingValue: u8[]
414
+ private readonly contentEncodingName: u8[]
415
+ private readonly gzipValue: u8[]
416
+ private readonly varyName: u8[]
417
+ private readonly acceptEncodingValue: u8[]
378
418
  /** The shortest body worth compressing, and the longest the output can hold. */
379
- readonly minLength: i32
380
- readonly maxLength: i32
419
+ private readonly minLength: i32
420
+ private readonly maxLength: i32
381
421
  /**
382
422
  * The least a compressed body must save to be sent: `minSaving` bytes and
383
423
  * `minSavingPercent` percent of the body, whichever is more. 1 and 0 by
384
424
  * default (any saving at all); a value below those counts as them.
385
425
  */
386
426
  minSaving: i32
427
+ /** The percent of the body a compressed body must save, as well as `minSaving` bytes: 0 by default, 100 or more sends nothing compressed. */
387
428
  minSavingPercent: i32
388
429
  /**
389
430
  * Whether `addFields` adds `Vary: Accept-Encoding` (RFC 9110 §12.5.5), which
@@ -398,6 +439,7 @@ export class HttpCompressor {
398
439
  * sets it, or a connection's own `mss` overrides it.
399
440
  */
400
441
  segmentMode: boolean
442
+ /** The TCP segment size `segmentMode` counts in, in bytes: `HTTP_DEFAULT_MSS` by default, and 0 or less means that default. */
401
443
  mss: i32
402
444
  /** `HTTP_ENCODER_AUTO` (the default), `HTTP_ENCODER_FIXED` or `HTTP_ENCODER_LEVEL`. */
403
445
  encoder: i32
@@ -417,6 +459,12 @@ export class HttpCompressor {
417
459
  * A compressor at `level` (`DEFLATE_FAST` suits a server) for bodies of
418
460
  * `minLength` to `maxLength` bytes. A level outside 0 to 9, a negative
419
461
  * `minLength` or a `maxLength` below it panics.
462
+ *
463
+ * @param level the deflate level, 0 to 9, for bodies past `fixedLimit`, and
464
+ * for every body when `encoder` is `HTTP_ENCODER_LEVEL`
465
+ * @param minLength the shortest body worth compressing, in bytes
466
+ * @param maxLength the longest body it will compress, in bytes; `output` is
467
+ * sized for it once, here
420
468
  */
421
469
  constructor(level: i32, minLength: i32, maxLength: i32) {
422
470
  if (minLength < 0 || maxLength < minLength) {
@@ -451,6 +499,14 @@ export class HttpCompressor {
451
499
  * length, or -1 to send the body as it is, and sets `decision` either way.
452
500
  * `encoder` picks the encoder; the fixed-Huffman one stops as soon as its
453
501
  * output passes what would be kept. A window outside `body` panics.
502
+ *
503
+ * @param coding the coding negotiated for this request, from
504
+ * `httpAcceptEncoding`
505
+ * @param body the buffer holding the response body
506
+ * @param off the index in `body` where the body starts
507
+ * @param len the body's length in bytes
508
+ * @returns how many bytes of `output` hold the gzip body, or -1 to send the
509
+ * body uncompressed; `decision` says which reason applied
454
510
  */
455
511
  compress(coding: i32, body: u8[], off: i32, len: i32): i32 {
456
512
  if (off < 0 || len < 0 || off > toI32(body.length) - len) {
@@ -493,6 +549,14 @@ export class HttpCompressor {
493
549
  * segments, so long as it is no longer than `maxLength`. Answers the
494
550
  * compressed length, or -1 when `coding` is not gzip or the body is too
495
551
  * long (sent as it is, which RFC 9110 §12.5.3 allows).
552
+ *
553
+ * @param coding the coding negotiated for this request, from
554
+ * `httpAcceptEncoding`
555
+ * @param body the buffer holding the response body
556
+ * @param off the index in `body` where the body starts
557
+ * @param len the body's length in bytes
558
+ * @returns how many bytes of `output` hold the gzip body, or -1 to send the
559
+ * body uncompressed
496
560
  */
497
561
  compressAnyway(coding: i32, body: u8[], off: i32, len: i32): i32 {
498
562
  if (off < 0 || len < 0 || off > toI32(body.length) - len) {
@@ -521,7 +585,13 @@ export class HttpCompressor {
521
585
  return n
522
586
  }
523
587
 
524
- /** The bytes a compressed `len`-byte body must save: `minSaving` and `minSavingPercent`, at least 1. */
588
+ /**
589
+ * The bytes a compressed `len`-byte body must save: `minSaving` and `minSavingPercent`, at least 1.
590
+ *
591
+ * @param len the uncompressed body's length in bytes
592
+ * @returns the fewest bytes gzip must shave off for the compressed body to
593
+ * be sent, never below 1; the percent share rounds up
594
+ */
525
595
  leastSaving(len: i32): i32 {
526
596
  let least: i32 = this.minSaving > 1 ? this.minSaving : 1
527
597
  let pct: i32 = this.minSavingPercent
@@ -544,6 +614,15 @@ export class HttpCompressor {
544
614
  * segments. When it does not, `decision` becomes
545
615
  * `HTTP_COMPRESS_NO_FEWER_SEGMENTS`. `respondEncoded` calls it; a program
546
616
  * driving HTTP/2 or HTTP/3 by hand may with its own head sizes.
617
+ *
618
+ * @param identityBytes the whole response's length sent uncompressed, head
619
+ * included, in bytes
620
+ * @param encodedBytes the whole response's length sent compressed, head
621
+ * included, in bytes
622
+ * @param mss the connection's segment size in bytes, or 0 or less for the
623
+ * compressor's own
624
+ * @returns true when compressing saves at least one segment, so the
625
+ * compressed response is worth sending
547
626
  */
548
627
  fewerSegments(identityBytes: i32, encodedBytes: i32, mss: i32): boolean {
549
628
  const size: i32 = this.segmentSize(mss)
@@ -554,7 +633,12 @@ export class HttpCompressor {
554
633
  return false
555
634
  }
556
635
 
557
- /** The segment `segmentMode` counts in: `mss` when above 0, else `this.mss` when above 0, else `HTTP_DEFAULT_MSS`. */
636
+ /**
637
+ * The segment `segmentMode` counts in: `mss` when above 0, else `this.mss` when above 0, else `HTTP_DEFAULT_MSS`.
638
+ *
639
+ * @internal Called by std/net/http1-server.ts to find the segment size its
640
+ * connection counts a response in before it decides whether to compress.
641
+ */
558
642
  segmentSize(mss: i32): i32 {
559
643
  if (mss > 0) {
560
644
  return mss
@@ -571,6 +655,15 @@ export class HttpCompressor {
571
655
  * `decision` becomes `HTTP_COMPRESS_NO_FEWER_SEGMENTS` and the caller checks
572
656
  * the head: when it does not fit, the caller goes on to `compress`, which
573
657
  * decides again. Anything else answers false and leaves `decision` alone.
658
+ *
659
+ * @param coding the coding negotiated for this request
660
+ * @param len the body's length in bytes
661
+ * @param mss the connection's segment size in bytes, or 0 or less for the
662
+ * compressor's own
663
+ * @returns true when the body fits one segment, so compressing it cannot
664
+ * help and it is sent as it is if its head fits too; false when `coding` is
665
+ * not gzip, `len` is outside `[minLength, maxLength]`, or the body does not
666
+ * fit one segment
574
667
  */
575
668
  oneSegment(coding: i32, len: i32, mss: i32): boolean {
576
669
  if (
@@ -591,6 +684,13 @@ export class HttpCompressor {
591
684
  * `encodedLength` bytes with the fields `addFields` built: less
592
685
  * `Content-Encoding: gzip`, less `Vary` (a body the segment count kept as
593
686
  * it is would go out so to any client), and the other `Content-Length`.
687
+ *
688
+ * @param encodedHead the compressed response's HTTP/1.1 head length in bytes
689
+ * @param encodedLength the compressed body's length in bytes, as its
690
+ * `Content-Length` spells it
691
+ * @param length the uncompressed body's length in bytes
692
+ * @returns the head length, in bytes, the same response would have sent
693
+ * uncompressed
594
694
  */
595
695
  http1IdentityHead(encodedHead: i32, encodedLength: i32, length: i32): i32 {
596
696
  let head: i32 = encodedHead - HTTP1_GZIP_FIELD_BYTES
@@ -605,6 +705,9 @@ export class HttpCompressor {
605
705
  * the response needs `Vary: Accept-Encoding`: a body sent compressed, or
606
706
  * one sent as it is because the client did not take gzip. A body the
607
707
  * lengths or the saving kept as it is would go out the same to any client.
708
+ *
709
+ * @returns true when the response must carry `Vary: Accept-Encoding` so a
710
+ * cache does not hand it to a client that asked differently
608
711
  */
609
712
  varies(): boolean {
610
713
  return this.decision === HTTP_COMPRESS_SENT || this.decision === HTTP_COMPRESS_NOT_ACCEPTED
@@ -615,6 +718,10 @@ export class HttpCompressor {
615
718
  * `Content-Encoding: gzip` and, when `vary`, `Vary: Accept-Encoding`. The
616
719
  * arrays are reused, so after the first few calls this allocates nothing;
617
720
  * they hold until the next call.
721
+ *
722
+ * @param names the program's own response field names, in order
723
+ * @param values the value for each name, at the same index; a value with
724
+ * no name beside it is left out
618
725
  */
619
726
  addFields(names: u8[][], values: u8[][]): void {
620
727
  while (this.names.length > 0) {
@@ -643,7 +750,7 @@ export class HttpCompressor {
643
750
  * Appends `Vary: Accept-Encoding` to the arrays `identityFields` built when
644
751
  * `vary` is set and the last decision `varies`.
645
752
  */
646
- addVary(): void {
753
+ private addVary(): void {
647
754
  if (this.vary && this.varies()) {
648
755
  this.names.push(this.varyName)
649
756
  this.values.push(this.acceptEncodingValue)
@@ -654,6 +761,10 @@ export class HttpCompressor {
654
761
  * Sets `names` / `values` to the program's fields and, when `vary` and the
655
762
  * last decision `varies`, `Vary: Accept-Encoding`: the fields of a body
656
763
  * sent as it is from a resource that is sometimes compressed.
764
+ *
765
+ * @param names the program's own response field names, in order
766
+ * @param values the value for each name, at the same index; a value with
767
+ * no name beside it is left out
657
768
  */
658
769
  identityFields(names: u8[][], values: u8[][]): void {
659
770
  while (this.names.length > 0) {
@@ -674,5 +785,10 @@ export class HttpCompressor {
674
785
  }
675
786
  }
676
787
 
677
- /** A compressor for the usual server: `DEFLATE_FAST`, bodies of 1 KB to 1 MB. */
788
+ /**
789
+ * A compressor for the usual server: `DEFLATE_FAST`, bodies of 1 KB to 1 MB.
790
+ *
791
+ * @returns a new compressor the caller owns, one per thread, with every
792
+ * other setting at its default
793
+ */
678
794
  export const httpCompressorDefault = (): HttpCompressor => new HttpCompressor(DEFLATE_FAST, 1024, 1048576)
@@ -164,6 +164,10 @@ const httpFieldsAllTchar = (bytes: u8[], upper: boolean): boolean => {
164
164
  * Whether `name` may be sent as a regular field name in HTTP/2 or HTTP/3: a
165
165
  * non-empty token in lowercase (RFC 9110 §5.1, RFC 9113 §8.2.1, RFC 9114
166
166
  * §4.2). A pseudo-header's name starts with a colon and is not one.
167
+ *
168
+ * @param name the field name's octets, as the program would send them
169
+ * @returns true when the name may go on the wire as it is; false for an empty
170
+ * name, a non-token character or an uppercase letter
167
171
  */
168
172
  export const httpFieldNameValid = (name: u8[]): boolean => httpFieldsAllTchar(name, false)
169
173
 
@@ -171,6 +175,10 @@ export const httpFieldNameValid = (name: u8[]): boolean => httpFieldsAllTchar(na
171
175
  * Whether `value` may be sent as a field value: no NUL, CR or LF, and no
172
176
  * space or tab as its first or last octet (RFC 9113 §8.2.1, RFC 9114 §4.2).
173
177
  * Other octets, obs-text included, pass as they are.
178
+ *
179
+ * @param value the field value's octets, as the program would send them
180
+ * @returns true when the value cannot end a line or smuggle whitespace at its
181
+ * edges, so it may be sent as it is
174
182
  */
175
183
  export const httpFieldValueValid = (value: u8[]): boolean => {
176
184
  const n: i32 = toI32(value.length)
@@ -188,7 +196,13 @@ export const httpFieldValueValid = (value: u8[]): boolean => {
188
196
  return first !== 32 && first !== 9 && last !== 32 && last !== 9
189
197
  }
190
198
 
191
- /** Whether the octets of `bytes` spell `text` exactly. */
199
+ /**
200
+ * Whether the octets of `bytes` spell `text` exactly.
201
+ *
202
+ * @param bytes the octets to compare, such as a decoded field name
203
+ * @param text the expected spelling, one character an octet; case matters
204
+ * @returns true only when both have the same length and every octet matches
205
+ */
192
206
  export const httpFieldIs = (bytes: u8[], text: string): boolean => {
193
207
  const n: i32 = toI32(bytes.length)
194
208
  if (n !== toI32(text.length)) {
@@ -202,7 +216,13 @@ export const httpFieldIs = (bytes: u8[], text: string): boolean => {
202
216
  return true
203
217
  }
204
218
 
205
- /** The octets of `text`, one a character: what a field name or value is sent as. */
219
+ /**
220
+ * The octets of `text`, one a character: what a field name or value is sent as.
221
+ *
222
+ * @param text the name or value to turn into octets; each character's code is
223
+ * kept in one octet, so it should be Latin-1
224
+ * @returns a fresh array the caller owns, one octet per character of `text`
225
+ */
206
226
  export const httpFieldBytes = (text: string): u8[] => {
207
227
  const out: u8[] = []
208
228
  const n: i32 = toI32(text.length)
@@ -218,6 +238,11 @@ export const httpFieldBytes = (text: string): u8[] => {
218
238
  * the fields RFC 9110 §7.6.1 names, `keep-alive`, `proxy-connection`,
219
239
  * `transfer-encoding` and `upgrade`, and `te` with any value but
220
240
  * `trailers`.
241
+ *
242
+ * @param name the field name's octets, in lowercase
243
+ * @param value the field's value, which matters only for `te`
244
+ * @returns true when the field belongs to one HTTP/1.1 hop and must not be
245
+ * sent or accepted in an HTTP/2 or HTTP/3 message
221
246
  */
222
247
  export const httpFieldConnectionSpecific = (name: u8[], value: u8[]): boolean => {
223
248
  if (httpFieldIs(name, "te")) {
@@ -239,6 +264,9 @@ export const httpFieldConnectionSpecific = (name: u8[], value: u8[]): boolean =>
239
264
  * §7.1.3). A table entry can be probed by a peer that controls part of what
240
265
  * is compressed beside it, so a secret sent indexed can leak through the
241
266
  * compressed length (CRIME's shape).
267
+ *
268
+ * @param name the field name's octets, in lowercase
269
+ * @returns true when the encoder should send this field never-indexed
242
270
  */
243
271
  export const httpFieldSensitive = (name: u8[]): boolean =>
244
272
  httpFieldIs(name, "authorization") ||
@@ -355,6 +383,12 @@ const httpFieldsHasUserinfo = (authority: u8[]): boolean => {
355
383
  * connection-specific, and as many values as names. Answers `HTTP_FIELDS_OK`
356
384
  * or the first refusal. A version's writer refuses what this refuses, so
357
385
  * that a program's string can never reach the wire as a field it was not.
386
+ *
387
+ * @param names the regular field names about to be sent, in order
388
+ * @param values the value for each name, at the same index
389
+ * @returns `HTTP_FIELDS_OK` when the whole section may be sent, or the
390
+ * `HTTP_FIELDS_*` code of the first field that may not (`HTTP_FIELDS_BAD_VALUE`
391
+ * too when the two lists differ in length)
358
392
  */
359
393
  export const httpFieldsCheckOutgoing = (names: u8[][], values: u8[][]): i32 => {
360
394
  const n: i32 = toI32(names.length)
@@ -406,8 +440,9 @@ export class HttpFields {
406
440
  /** The `content-length`, or `HTTP_FIELDS_NO_LENGTH`. */
407
441
  contentLength: i64 = -1
408
442
  /** The empty value an absent pseudo-header reads as, made once. */
409
- none: u8[]
443
+ private none: u8[]
410
444
 
445
+ /** Makes an empty section, ready for its first read; no field is set yet. */
411
446
  constructor() {
412
447
  this.method = []
413
448
  this.scheme = []
@@ -436,12 +471,23 @@ export class HttpFields {
436
471
  this.contentLength = HTTP_FIELDS_NO_LENGTH
437
472
  }
438
473
 
439
- /** Whether the method is CONNECT, plain (RFC 9113 §8.5) or extended (RFC 8441). */
474
+ /**
475
+ * Whether the method is CONNECT, plain (RFC 9113 §8.5) or extended (RFC 8441).
476
+ *
477
+ * @returns true when the last read section's `:method` is exactly `CONNECT`
478
+ */
440
479
  isConnect(): boolean {
441
480
  return httpFieldIs(this.method, "CONNECT")
442
481
  }
443
482
 
444
- /** The value of the first regular field named `name` (lowercase), or `null`. */
483
+ /**
484
+ * The value of the first regular field named `name` (lowercase), or `null`.
485
+ *
486
+ * @param name the field name to look for, in lowercase as HTTP/2 and HTTP/3
487
+ * send it
488
+ * @returns the caller's own value array for that field, not a copy, or `null`
489
+ * when the section has no such field
490
+ */
445
491
  get(name: string): u8[] | null {
446
492
  const n: i32 = toI32(this.names.length)
447
493
  for (let k: i32 = 0; k < n && k < toI32(this.names.length) && k < toI32(this.values.length); k++) {
@@ -458,7 +504,7 @@ export class HttpFields {
458
504
  * rules — which pseudo-headers are required — are the caller's. Answers
459
505
  * the mask of pseudo-headers seen, or a negative refusal.
460
506
  */
461
- readSection(names: u8[][], values: u8[][], allowed: i32): i32 {
507
+ private readSection(names: u8[][], values: u8[][], allowed: i32): i32 {
462
508
  this.clear()
463
509
  const n: i32 = toI32(names.length)
464
510
  if (n !== toI32(values.length)) {
@@ -514,7 +560,7 @@ export class HttpFields {
514
560
  }
515
561
 
516
562
  /** Records the pseudo-header `pseudo` as `value`, checking its syntax. */
517
- takePseudo(pseudo: i32, value: u8[]): i32 {
563
+ private takePseudo(pseudo: i32, value: u8[]): i32 {
518
564
  switch (pseudo) {
519
565
  case HTTP_FIELDS_METHOD:
520
566
  this.method = value
@@ -546,6 +592,14 @@ export class HttpFields {
546
592
  * all four of the others (RFC 8441 §4). Any other request has `:scheme`
547
593
  * and `:path`. A `:path` that does not start with `/` is OPTIONS's `*` or
548
594
  * refused, and a `host` field must name `:authority` when both are there.
595
+ *
596
+ * @param names the decoded field names, pseudo-headers first, in wire order
597
+ * @param values the decoded value for each name, at the same index
598
+ * @param extendedConnect whether this connection sent
599
+ * SETTINGS_ENABLE_CONNECT_PROTOCOL, so a `:protocol` is allowed
600
+ * @returns `HTTP_FIELDS_OK` when the section is a well-formed request and
601
+ * this object now describes it, or the `HTTP_FIELDS_*` code of the first
602
+ * rule it breaks
549
603
  */
550
604
  readRequest(names: u8[][], values: u8[][], extendedConnect: boolean): i32 {
551
605
  const all: i32 =
@@ -592,7 +646,15 @@ export class HttpFields {
592
646
  return HTTP_FIELDS_OK
593
647
  }
594
648
 
595
- /** Reads a response's field section: exactly one `:status`, and no other pseudo-header (RFC 9113 §8.3.2). */
649
+ /**
650
+ * Reads a response's field section: exactly one `:status`, and no other pseudo-header (RFC 9113 §8.3.2).
651
+ *
652
+ * @param names the decoded field names, `:status` first, in wire order
653
+ * @param values the decoded value for each name, at the same index
654
+ * @returns `HTTP_FIELDS_OK` when the section is a well-formed response and
655
+ * `status` holds its code, or the `HTTP_FIELDS_*` code of the first rule
656
+ * it breaks
657
+ */
596
658
  readResponse(names: u8[][], values: u8[][]): i32 {
597
659
  const seen: i32 = this.readSection(names, values, HTTP_FIELDS_STATUS)
598
660
  if (seen < 0) {
@@ -601,7 +663,15 @@ export class HttpFields {
601
663
  return (seen & HTTP_FIELDS_STATUS) !== 0 ? HTTP_FIELDS_OK : HTTP_FIELDS_PSEUDO_MISSING
602
664
  }
603
665
 
604
- /** Reads a trailer section: regular fields only (RFC 9113 §8.1). */
666
+ /**
667
+ * Reads a trailer section: regular fields only (RFC 9113 §8.1).
668
+ *
669
+ * @param names the decoded trailer field names, in wire order
670
+ * @param values the decoded value for each name, at the same index
671
+ * @returns `HTTP_FIELDS_OK` when every field is a valid regular field, or the
672
+ * `HTTP_FIELDS_*` code of the first that is not (a known pseudo-header
673
+ * here is `HTTP_FIELDS_PSEUDO_FORBIDDEN`)
674
+ */
605
675
  readTrailers(names: u8[][], values: u8[][]): i32 {
606
676
  const seen: i32 = this.readSection(names, values, HTTP_FIELDS_ZERO)
607
677
  return seen < 0 ? seen : HTTP_FIELDS_OK
@@ -649,6 +719,12 @@ const httpFieldsPutTwo = (out: u8[], at: i32, n: i32): void => {
649
719
  * 146,097 days long. Every step
650
720
  * is `i64` arithmetic on numbers that are not negative, so `--number-mode
651
721
  * f64` writes the same bytes.
722
+ *
723
+ * @param out the buffer to write into; its length is not changed
724
+ * @param at the index in `out` where the date's first letter goes
725
+ * @param seconds the instant to name, in whole seconds since 1970 UTC
726
+ * @returns the index just past the date's last octet (`at + HTTP_DATE_SIZE`),
727
+ * or -1 when nothing was written
652
728
  */
653
729
  export const httpDateWrite = (out: u8[], at: i32, seconds: i64): i32 => {
654
730
  if (