@amritk/nish-x86_64-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
@@ -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
@@ -87,6 +87,7 @@ export const HTTP_CODING_NONE: i32 = -1
87
87
 
88
88
  /** `httpAcceptEncoding`'s `offered` bits: the program can send gzip, deflate. */
89
89
  export const HTTP_OFFER_GZIP: i32 = 1
90
+ /** The `offered` bit for deflate, which is negotiated but never produced here. */
90
91
  export const HTTP_OFFER_DEFLATE: i32 = 2
91
92
 
92
93
  /**
@@ -99,10 +100,18 @@ export const HTTP_OFFER_DEFLATE: i32 = 2
99
100
  * response that already fits one segment, decided without compressing.
100
101
  */
101
102
  export const HTTP_COMPRESS_SENT: i32 = 0
103
+ /** The client did not accept gzip, so the body went as it is; the default before any decision. */
102
104
  export const HTTP_COMPRESS_NOT_ACCEPTED: i32 = 1
105
+ /** The body was shorter than `minLength`, too small to be worth compressing. */
103
106
  export const HTTP_COMPRESS_TOO_SMALL: i32 = 2
107
+ /** The body was longer than `maxLength`, more than `output` was sized for. */
104
108
  export const HTTP_COMPRESS_TOO_LARGE: i32 = 3
109
+ /**
110
+ * Gzip ran but did not save `minSaving` bytes and `minSavingPercent` percent,
111
+ * or the saving asked for was the whole body, so no encoder ran.
112
+ */
105
113
  export const HTTP_COMPRESS_TOO_LITTLE_SAVED: i32 = 4
114
+ /** Under `segmentMode`, compressing would not have saved a TCP segment. */
106
115
  export const HTTP_COMPRESS_NO_FEWER_SEGMENTS: i32 = 5
107
116
 
108
117
  /**
@@ -111,7 +120,13 @@ export const HTTP_COMPRESS_NO_FEWER_SEGMENTS: i32 = 5
111
120
  * encoder always, or the constructor's level always.
112
121
  */
113
122
  export const HTTP_ENCODER_AUTO: i32 = 0
123
+ /**
124
+ * The fixed-Huffman encoder for every body `compress` takes: fastest, at some
125
+ * cost in size on large bodies. `compressAnyway` falls back to the
126
+ * `GzipCompressor` when it fails.
127
+ */
114
128
  export const HTTP_ENCODER_FIXED: i32 = 1
129
+ /** Always the `GzipCompressor` at the constructor's level, whatever the body's length. */
115
130
  export const HTTP_ENCODER_LEVEL: i32 = 2
116
131
 
117
132
  /**
@@ -124,7 +139,15 @@ export const HTTP_DEFAULT_MSS: i32 = 1448
124
139
  /** The bytes `content-encoding: gzip` adds to an HTTP/1.1 head: name, value, ": " and CRLF. */
125
140
  export const HTTP1_GZIP_FIELD_BYTES: i32 = 24
126
141
 
127
- /** The number of TCP segments `bytes` bytes take at `mss` bytes a segment. */
142
+ /**
143
+ * The number of TCP segments `bytes` bytes take at `mss` bytes a segment.
144
+ * A segment size of zero or less panics, since no count would mean anything.
145
+ *
146
+ * @param bytes the length of the whole response, head and body, in bytes
147
+ * @param mss the payload one segment carries, in bytes
148
+ * @returns how many segments the bytes fill, the last one possibly partial;
149
+ * 0 for no bytes
150
+ */
128
151
  export const httpSegments = (bytes: i32, mss: i32): i32 => {
129
152
  if (mss <= 0) {
130
153
  panic("httpSegments: a segment size of zero or less")
@@ -236,6 +259,14 @@ const httpOws = (c: i32): boolean => c === 32 || c === 9
236
259
  * HTTP_OFFER_DEFLATE`): `HTTP_CODING_GZIP`, `HTTP_CODING_DEFLATE`,
237
260
  * `HTTP_CODING_IDENTITY` or `HTTP_CODING_NONE`, by the rules in the module
238
261
  * comment. A window outside `buf` panics.
262
+ *
263
+ * @param buf the buffer holding the field value, such as the request head
264
+ * @param start the index in `buf` of the value's first octet
265
+ * @param end the index just past the value's last octet
266
+ * @param offered which codings the program can produce, `HTTP_OFFER_*` bits
267
+ * or 0 for none
268
+ * @returns the `HTTP_CODING_*` to send the body in, or `HTTP_CODING_NONE`
269
+ * when nothing offered is acceptable and identity is refused too
239
270
  */
240
271
  export const httpAcceptEncoding = (buf: u8[], start: i32, end: i32, offered: i32): i32 => {
241
272
  if (start < 0 || start > end || end > toI32(buf.length)) {
@@ -345,6 +376,11 @@ export const httpAcceptEncoding = (buf: u8[], start: i32, end: i32, offered: i32
345
376
  * body as it is: `identity;q=0`, or `*;q=0` with no `identity` listed. A
346
377
  * server should then send a coding the client takes even where it would not
347
378
  * bother (`HttpCompressor.compressAnyway`).
379
+ *
380
+ * @param buf the buffer holding the field value, such as the request head
381
+ * @param start the index in `buf` of the value's first octet
382
+ * @param end the index just past the value's last octet
383
+ * @returns true when the client said it cannot take the body uncompressed
348
384
  */
349
385
  export const httpIdentityRefused = (buf: u8[], start: i32, end: i32): boolean =>
350
386
  httpAcceptEncoding(buf, start, end, 0) === HTTP_CODING_NONE
@@ -363,21 +399,21 @@ const httpCompressBytes = (text: string): u8[] => {
363
399
  * the response fields that say so. One per thread; allocated once.
364
400
  */
365
401
  export class HttpCompressor {
366
- readonly gzip: GzipCompressor
402
+ private readonly gzip: GzipCompressor
367
403
  /** The small-body encoder: one fixed-Huffman block, nothing kept between calls. */
368
- readonly fixed: GzipFixedCompressor
404
+ private readonly fixed: GzipFixedCompressor
369
405
  /** Where `compress` writes; valid until the next call. */
370
406
  readonly output: u8[]
371
407
  /** The fields `addFields` built: the program's, then `Content-Encoding` and `Vary`. */
372
408
  readonly names: u8[][]
373
409
  readonly values: u8[][]
374
- readonly contentEncodingName: u8[]
375
- readonly gzipValue: u8[]
376
- readonly varyName: u8[]
377
- readonly acceptEncodingValue: u8[]
410
+ private readonly contentEncodingName: u8[]
411
+ private readonly gzipValue: u8[]
412
+ private readonly varyName: u8[]
413
+ private readonly acceptEncodingValue: u8[]
378
414
  /** The shortest body worth compressing, and the longest the output can hold. */
379
- readonly minLength: i32
380
- readonly maxLength: i32
415
+ private readonly minLength: i32
416
+ private readonly maxLength: i32
381
417
  /**
382
418
  * The least a compressed body must save to be sent: `minSaving` bytes and
383
419
  * `minSavingPercent` percent of the body, whichever is more. 1 and 0 by
@@ -417,6 +453,12 @@ export class HttpCompressor {
417
453
  * A compressor at `level` (`DEFLATE_FAST` suits a server) for bodies of
418
454
  * `minLength` to `maxLength` bytes. A level outside 0 to 9, a negative
419
455
  * `minLength` or a `maxLength` below it panics.
456
+ *
457
+ * @param level the deflate level, 0 to 9, for bodies past `fixedLimit`, and
458
+ * for every body when `encoder` is `HTTP_ENCODER_LEVEL`
459
+ * @param minLength the shortest body worth compressing, in bytes
460
+ * @param maxLength the longest body it will compress, in bytes; `output` is
461
+ * sized for it once, here
420
462
  */
421
463
  constructor(level: i32, minLength: i32, maxLength: i32) {
422
464
  if (minLength < 0 || maxLength < minLength) {
@@ -451,6 +493,14 @@ export class HttpCompressor {
451
493
  * length, or -1 to send the body as it is, and sets `decision` either way.
452
494
  * `encoder` picks the encoder; the fixed-Huffman one stops as soon as its
453
495
  * output passes what would be kept. A window outside `body` panics.
496
+ *
497
+ * @param coding the coding negotiated for this request, from
498
+ * `httpAcceptEncoding`
499
+ * @param body the buffer holding the response body
500
+ * @param off the index in `body` where the body starts
501
+ * @param len the body's length in bytes
502
+ * @returns how many bytes of `output` hold the gzip body, or -1 to send the
503
+ * body uncompressed; `decision` says which reason applied
454
504
  */
455
505
  compress(coding: i32, body: u8[], off: i32, len: i32): i32 {
456
506
  if (off < 0 || len < 0 || off > toI32(body.length) - len) {
@@ -493,6 +543,14 @@ export class HttpCompressor {
493
543
  * segments, so long as it is no longer than `maxLength`. Answers the
494
544
  * compressed length, or -1 when `coding` is not gzip or the body is too
495
545
  * long (sent as it is, which RFC 9110 §12.5.3 allows).
546
+ *
547
+ * @param coding the coding negotiated for this request, from
548
+ * `httpAcceptEncoding`
549
+ * @param body the buffer holding the response body
550
+ * @param off the index in `body` where the body starts
551
+ * @param len the body's length in bytes
552
+ * @returns how many bytes of `output` hold the gzip body, or -1 to send the
553
+ * body uncompressed
496
554
  */
497
555
  compressAnyway(coding: i32, body: u8[], off: i32, len: i32): i32 {
498
556
  if (off < 0 || len < 0 || off > toI32(body.length) - len) {
@@ -521,7 +579,13 @@ export class HttpCompressor {
521
579
  return n
522
580
  }
523
581
 
524
- /** The bytes a compressed `len`-byte body must save: `minSaving` and `minSavingPercent`, at least 1. */
582
+ /**
583
+ * The bytes a compressed `len`-byte body must save: `minSaving` and `minSavingPercent`, at least 1.
584
+ *
585
+ * @param len the uncompressed body's length in bytes
586
+ * @returns the fewest bytes gzip must shave off for the compressed body to
587
+ * be sent, never below 1; the percent share rounds up
588
+ */
525
589
  leastSaving(len: i32): i32 {
526
590
  let least: i32 = this.minSaving > 1 ? this.minSaving : 1
527
591
  let pct: i32 = this.minSavingPercent
@@ -544,6 +608,15 @@ export class HttpCompressor {
544
608
  * segments. When it does not, `decision` becomes
545
609
  * `HTTP_COMPRESS_NO_FEWER_SEGMENTS`. `respondEncoded` calls it; a program
546
610
  * driving HTTP/2 or HTTP/3 by hand may with its own head sizes.
611
+ *
612
+ * @param identityBytes the whole response's length sent uncompressed, head
613
+ * included, in bytes
614
+ * @param encodedBytes the whole response's length sent compressed, head
615
+ * included, in bytes
616
+ * @param mss the connection's segment size in bytes, or 0 or less for the
617
+ * compressor's own
618
+ * @returns true when compressing saves at least one segment, so the
619
+ * compressed response is worth sending
547
620
  */
548
621
  fewerSegments(identityBytes: i32, encodedBytes: i32, mss: i32): boolean {
549
622
  const size: i32 = this.segmentSize(mss)
@@ -554,7 +627,12 @@ export class HttpCompressor {
554
627
  return false
555
628
  }
556
629
 
557
- /** The segment `segmentMode` counts in: `mss` when above 0, else `this.mss` when above 0, else `HTTP_DEFAULT_MSS`. */
630
+ /**
631
+ * The segment `segmentMode` counts in: `mss` when above 0, else `this.mss` when above 0, else `HTTP_DEFAULT_MSS`.
632
+ *
633
+ * @internal Called by std/net/http1-server.ts to find the segment size its
634
+ * connection counts a response in before it decides whether to compress.
635
+ */
558
636
  segmentSize(mss: i32): i32 {
559
637
  if (mss > 0) {
560
638
  return mss
@@ -571,6 +649,15 @@ export class HttpCompressor {
571
649
  * `decision` becomes `HTTP_COMPRESS_NO_FEWER_SEGMENTS` and the caller checks
572
650
  * the head: when it does not fit, the caller goes on to `compress`, which
573
651
  * decides again. Anything else answers false and leaves `decision` alone.
652
+ *
653
+ * @param coding the coding negotiated for this request
654
+ * @param len the body's length in bytes
655
+ * @param mss the connection's segment size in bytes, or 0 or less for the
656
+ * compressor's own
657
+ * @returns true when the body fits one segment, so compressing it cannot
658
+ * help and it is sent as it is if its head fits too; false when `coding` is
659
+ * not gzip, `len` is outside `[minLength, maxLength]`, or the body does not
660
+ * fit one segment
574
661
  */
575
662
  oneSegment(coding: i32, len: i32, mss: i32): boolean {
576
663
  if (
@@ -591,6 +678,13 @@ export class HttpCompressor {
591
678
  * `encodedLength` bytes with the fields `addFields` built: less
592
679
  * `Content-Encoding: gzip`, less `Vary` (a body the segment count kept as
593
680
  * it is would go out so to any client), and the other `Content-Length`.
681
+ *
682
+ * @param encodedHead the compressed response's HTTP/1.1 head length in bytes
683
+ * @param encodedLength the compressed body's length in bytes, as its
684
+ * `Content-Length` spells it
685
+ * @param length the uncompressed body's length in bytes
686
+ * @returns the head length, in bytes, the same response would have sent
687
+ * uncompressed
594
688
  */
595
689
  http1IdentityHead(encodedHead: i32, encodedLength: i32, length: i32): i32 {
596
690
  let head: i32 = encodedHead - HTTP1_GZIP_FIELD_BYTES
@@ -605,6 +699,9 @@ export class HttpCompressor {
605
699
  * the response needs `Vary: Accept-Encoding`: a body sent compressed, or
606
700
  * one sent as it is because the client did not take gzip. A body the
607
701
  * lengths or the saving kept as it is would go out the same to any client.
702
+ *
703
+ * @returns true when the response must carry `Vary: Accept-Encoding` so a
704
+ * cache does not hand it to a client that asked differently
608
705
  */
609
706
  varies(): boolean {
610
707
  return this.decision === HTTP_COMPRESS_SENT || this.decision === HTTP_COMPRESS_NOT_ACCEPTED
@@ -615,6 +712,10 @@ export class HttpCompressor {
615
712
  * `Content-Encoding: gzip` and, when `vary`, `Vary: Accept-Encoding`. The
616
713
  * arrays are reused, so after the first few calls this allocates nothing;
617
714
  * they hold until the next call.
715
+ *
716
+ * @param names the program's own response field names, in order
717
+ * @param values the value for each name, at the same index; a value with
718
+ * no name beside it is left out
618
719
  */
619
720
  addFields(names: u8[][], values: u8[][]): void {
620
721
  while (this.names.length > 0) {
@@ -643,7 +744,7 @@ export class HttpCompressor {
643
744
  * Appends `Vary: Accept-Encoding` to the arrays `identityFields` built when
644
745
  * `vary` is set and the last decision `varies`.
645
746
  */
646
- addVary(): void {
747
+ private addVary(): void {
647
748
  if (this.vary && this.varies()) {
648
749
  this.names.push(this.varyName)
649
750
  this.values.push(this.acceptEncodingValue)
@@ -654,6 +755,10 @@ export class HttpCompressor {
654
755
  * Sets `names` / `values` to the program's fields and, when `vary` and the
655
756
  * last decision `varies`, `Vary: Accept-Encoding`: the fields of a body
656
757
  * sent as it is from a resource that is sometimes compressed.
758
+ *
759
+ * @param names the program's own response field names, in order
760
+ * @param values the value for each name, at the same index; a value with
761
+ * no name beside it is left out
657
762
  */
658
763
  identityFields(names: u8[][], values: u8[][]): void {
659
764
  while (this.names.length > 0) {
@@ -674,5 +779,10 @@ export class HttpCompressor {
674
779
  }
675
780
  }
676
781
 
677
- /** A compressor for the usual server: `DEFLATE_FAST`, bodies of 1 KB to 1 MB. */
782
+ /**
783
+ * A compressor for the usual server: `DEFLATE_FAST`, bodies of 1 KB to 1 MB.
784
+ *
785
+ * @returns a new compressor the caller owns, one per thread, with every
786
+ * other setting at its default
787
+ */
678
788
  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 (