@amritk/nish-aarch64-linux 0.49.1 → 0.51.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 (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
@@ -59,6 +59,18 @@
59
59
  * `Date` field takes. It reads no clock, so a wasm32 build can import it; the
60
60
  * caller supplies the second.
61
61
  *
62
+ * **Media types.** `mediaTypeParse` reads a `Content-Type` value (RFC 9110
63
+ * §8.3.1): a `type/subtype` pair of tokens, lowercased, then `;`-separated
64
+ * parameters whose names are lowercased tokens and whose values are tokens or
65
+ * quoted strings, a backslash in one escaping the octet after it. A
66
+ * parameter named twice is refused, since two readers could believe
67
+ * different ones, and so is a 17th, which keeps that check linear in the
68
+ * value. `formDataDispositionParse` reads the `Content-Disposition`
69
+ * of a `multipart/form-data` part the same way, except that a backslash in a
70
+ * quoted value is a backslash: the HTML form submission algorithm writes `"`
71
+ * as `%22` and leaves `\` alone, so `filename="C:\x.txt"` names the file a
72
+ * browser sent. `mediaTypeParam` looks a parameter up.
73
+ *
62
74
  * Names and values are octets (`u8[]`), as HPACK and QPACK answer them. A
63
75
  * section that passes keeps references to the caller's arrays, not copies, so
64
76
  * it is valid for as long as they are.
@@ -66,8 +78,8 @@
66
78
  * Private helpers share the importing program's flat symbol namespace
67
79
  * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
68
80
  *
69
- * Written from RFC 9110, RFC 9113, RFC 9114 and RFC 8441, not ported from
70
- * another implementation.
81
+ * Written from RFC 9110, RFC 9113, RFC 9114, RFC 8441 and the HTML form
82
+ * submission algorithm, not ported from another implementation.
71
83
  */
72
84
 
73
85
  /** A field section that has the shape asked for. */
@@ -355,8 +367,12 @@ const httpFieldsStatusOf = (value: u8[]): i32 => {
355
367
  if (digit < 0 || digit > 9) {
356
368
  return -1
357
369
  }
358
- // Three digits are at most 999, so the unsigned sum cannot leave an `i32`.
359
- code = toI32(toU32(code) * 10 + toU32(digit))
370
+ // The length test above allows three digits, so `code` is at most 99
371
+ // before the last one; the test is what proves the sum cannot overflow.
372
+ if (code < 0 || code > 99) {
373
+ return -1
374
+ }
375
+ code = code * 10 + digit
360
376
  }
361
377
  return code >= 100 && code <= 599 ? code : -1
362
378
  }
@@ -367,17 +383,17 @@ const httpFieldsLengthOf = (value: u8[]): i64 => {
367
383
  if (n === 0 || n > HTTP_FIELDS_MAX_LENGTH_DIGITS) {
368
384
  return toI64(-1)
369
385
  }
370
- let length: i64 = 0
386
+ // The value stays `u64` throughout: at most eighteen digits stay below
387
+ // 10^18, so the sum never wraps and its value fits an `i64` exactly.
388
+ let length: u64 = 0
371
389
  for (const b of value) {
372
390
  const digit: i32 = toI32(b) - 48
373
391
  if (digit < 0 || digit > 9) {
374
392
  return toI64(-1)
375
393
  }
376
- // At most eighteen digits stay below 10^18, so the unsigned sum cannot
377
- // leave an `i64`.
378
- length = toI64(toU64(length) * toU64(10) + toU64(digit))
394
+ length = length * 10 + toU64(digit)
379
395
  }
380
- return length
396
+ return toI64(length)
381
397
  }
382
398
 
383
399
  /** Whether `authority` holds an `@`, which only userinfo would put there (RFC 9113 §8.3.1). */
@@ -714,16 +730,20 @@ const HTTP_FIELDS_DATE_NAMES: string = "SunMonTueWedThuFriSatJanFebMarAprMayJunJ
714
730
  * Writes `b` at `out[at + k]`. Its one caller, `httpDateWrite`, has checked
715
731
  * `0 <= at` and `at + HTTP_DATE_SIZE <= out.length` before the first write,
716
732
  * and every `k` it passes is a constant from 0 to 28, under `HTTP_DATE_SIZE`:
717
- * so `at + k` is inside `out`, the u32 sum does not wrap and the test always
718
- * holds. The test is what proves the index; nothing is allocated, and a byte
719
- * it skipped would show in the `httpDateWrite` round trips `tests/run.js`
720
- * pins byte for byte (`tests/link/net_http_date`). The skip is unreachable by
721
- * that room check in `httpDateWrite`, which the prover does not carry across
722
- * the call, so the test stays.
733
+ * so `at + k` is inside `out` and the tests always hold. The first test,
734
+ * whose literals are `HTTP_DATE_SIZE - 1` and `2^31 - 1` less that, proves
735
+ * the sum cannot overflow, and the second proves the index; nothing is
736
+ * allocated, and a byte it skipped would show in the `httpDateWrite` round
737
+ * trips `tests/run.js` pins byte for byte (`tests/link/net_http_date`). The
738
+ * skip is unreachable by that room check in `httpDateWrite`, which the prover
739
+ * does not carry across the call, so the tests stay.
723
740
  */
724
741
  const httpFieldsPut = (out: u8[], at: i32, k: i32, b: u32): void => {
725
- const pos: i32 = toI32(toU32(at) + toU32(k))
726
- if (pos >= 0 && pos < toI32(out.length)) {
742
+ if (at < 0 || at > 2147483619 || k < 0 || k > 28) {
743
+ return
744
+ }
745
+ const pos: i32 = at + k
746
+ if (pos < toI32(out.length)) {
727
747
  out[pos] = toU8(b)
728
748
  }
729
749
  }
@@ -733,22 +753,36 @@ const httpFieldsPut = (out: u8[], at: i32, k: i32, b: u32): void => {
733
753
  * `out[at + k ..)`. `httpDateWrite` passes a weekday, `(days + 4) % 7`, so 0
734
754
  * to 6, or `6 + month` with `month` 1 to 12 by the 153-day rule, so 7 to 18:
735
755
  * `3 * index + 2` is at most 56, under the table's 57 letters, and the test
736
- * always holds. It is what proves the index.
756
+ * always holds. It is what proves the index. Every `k` it is passed is 0 or
757
+ * 8, so the test on `k`, whose 26 is `HTTP_DATE_SIZE - 3`, always holds too;
758
+ * it is what proves `k + d` cannot overflow.
737
759
  */
738
760
  const httpFieldsPutName = (out: u8[], at: i32, k: i32, index: u32): void => {
761
+ if (k < 0 || k > 26) {
762
+ return
763
+ }
739
764
  const names: string = HTTP_FIELDS_DATE_NAMES
740
- for (let d: u32 = 0; d < 3; d++) {
741
- const pos: i32 = toI32(index * 3 + d)
765
+ for (let d: i32 = 0; d < 3; d++) {
766
+ // The letter's place is worked in `u32`, as `index` is: at most 56.
767
+ const step: u32 = toU32(d)
768
+ const pos: i32 = toI32(index * 3 + step)
742
769
  if (pos >= 0 && pos < toI32(names.length)) {
743
- httpFieldsPut(out, at, toI32(toU32(k) + d), toU32(names.charCodeAt(pos)))
770
+ httpFieldsPut(out, at, k + d, toU32(names.charCodeAt(pos)))
744
771
  }
745
772
  }
746
773
  }
747
774
 
748
- /** Writes `n`, 0 to 99, as two digits at `out[at + k ..)`. */
775
+ /**
776
+ * Writes `n`, 0 to 99, as two digits at `out[at + k ..)`. Every `k` it is
777
+ * passed is at most 23, so the test, whose 27 is `HTTP_DATE_SIZE - 2`, always
778
+ * holds; it is what proves `k + 1` cannot overflow.
779
+ */
749
780
  const httpFieldsPutTwo = (out: u8[], at: i32, k: i32, n: u32): void => {
781
+ if (k < 0 || k > 27) {
782
+ return
783
+ }
750
784
  httpFieldsPut(out, at, k, toU32(48) + n / 10)
751
- httpFieldsPut(out, at, toI32(toU32(k) + toU32(1)), toU32(48) + (n % 10))
785
+ httpFieldsPut(out, at, k + 1, toU32(48) + (n % 10))
752
786
  }
753
787
 
754
788
  /**
@@ -774,12 +808,7 @@ const httpFieldsPutTwo = (out: u8[], at: i32, k: i32, n: u32): void => {
774
808
  * or -1 when nothing was written
775
809
  */
776
810
  export const httpDateWrite = (out: u8[], at: i32, seconds: i64): i32 => {
777
- if (
778
- seconds < 0 ||
779
- seconds > HTTP_DATE_LAST_SECOND ||
780
- at < 0 ||
781
- toI64(at) + toI64(HTTP_DATE_SIZE) > toI64(out.length)
782
- ) {
811
+ if (seconds < 0 || seconds > HTTP_DATE_LAST_SECOND || at < 0 || at > toI32(out.length) - 29) {
783
812
  return -1
784
813
  }
785
814
  // The test above keeps `seconds` far below 2^63, so every step is `u64`
@@ -819,6 +848,273 @@ export const httpDateWrite = (out: u8[], at: i32, seconds: i64): i32 => {
819
848
  httpFieldsPut(out, at, 26, 71)
820
849
  httpFieldsPut(out, at, 27, 77)
821
850
  httpFieldsPut(out, at, 28, 84)
822
- // The date fits in `out` from `at`, so the end is at most its length.
823
- return toI32(toU32(at) + toU32(HTTP_DATE_SIZE))
851
+ // The room test above, whose 29 is `HTTP_DATE_SIZE`, keeps the end at most
852
+ // `out.length`; it is what proves the sum cannot overflow.
853
+ return at + 29
854
+ }
855
+
856
+ /**
857
+ * A media type or a disposition as `mediaTypeParse` and
858
+ * `formDataDispositionParse` read it: the type and subtype in lowercase, and
859
+ * the parameters in the order they came, each name in lowercase beside its
860
+ * value with any quoting removed.
861
+ */
862
+ export class MediaType {
863
+ /** The type, `text` in `text/html`, or a disposition's type, in lowercase. */
864
+ type: string = ""
865
+ /** The subtype, `html` in `text/html`, in lowercase; empty for a disposition. */
866
+ subtype: string = ""
867
+ /** The parameter names, in lowercase, in the order they came. */
868
+ names: string[] = []
869
+ /** The parameter values, `values[k]` beside `names[k]`, quotes removed. */
870
+ values: string[] = []
871
+ }
872
+
873
+ /** The most parameters a media type or disposition may carry; one more is refused. */
874
+ const HTTP_FIELDS_MAX_PARAMS: i32 = 16
875
+
876
+ /** Whether `c` is a `tchar` (RFC 9110 §5.6.2), in either case. */
877
+ const httpFieldsTchar = (c: i32): boolean => httpFieldsLowerTchar(c) || (c >= 65 && c <= 90)
878
+
879
+ /** Whether `c` is optional whitespace (RFC 9110 §5.6.3): a space or a tab. */
880
+ const httpFieldsOws = (c: i32): boolean => c === 32 || c === 9
881
+
882
+ /**
883
+ * Whether `c` may stand inside a quoted string as itself (RFC 9110 §5.6.4's
884
+ * `qdtext`): a tab, a space, any visible character but `"` and `\`, or
885
+ * obs-text.
886
+ */
887
+ const httpFieldsQdtext = (c: i32): boolean =>
888
+ c === 9 || c === 32 || c === 33 || (c >= 35 && c <= 91) || (c >= 93 && c <= 126) || (c >= 128 && c <= 255)
889
+
890
+ /**
891
+ * The octets in `bytes[from .. to)` as a string, letters in lowercase when
892
+ * `lower`; empty when the window is not inside `bytes`. It goes a byte at a
893
+ * time because `String.fromBytes` keeps a range check no proof removes; each
894
+ * `String.fromCharCode` is the one octet, so the string's bytes are the
895
+ * window's.
896
+ */
897
+ const httpFieldsString = (bytes: readonly u8[], from: i32, to: i32, lower: boolean): string => {
898
+ const parts: string[] = []
899
+ let i: i32 = from
900
+ while (i >= 0 && i < to && i < toI32(bytes.length)) {
901
+ const c: i32 = toI32(bytes[i])
902
+ parts.push(String.fromCharCode(lower && c >= 65 && c <= 90 ? c | 32 : c))
903
+ i = i + 1
904
+ }
905
+ return parts.join("")
906
+ }
907
+
908
+ /** The end of the run of `tchar` that starts at `from`, at most `to`. */
909
+ const httpFieldsTokenEnd = (bytes: readonly u8[], from: i32, to: i32): i32 => {
910
+ const n: i32 = toI32(bytes.length)
911
+ let i: i32 = from
912
+ while (i >= 0 && i < to && i < n && httpFieldsTchar(toI32(bytes[i]))) {
913
+ i = i + 1
914
+ }
915
+ return i
916
+ }
917
+
918
+ /** The first index from `from` that is not optional whitespace, at most `to`. */
919
+ const httpFieldsOwsEnd = (bytes: readonly u8[], from: i32, to: i32): i32 => {
920
+ const n: i32 = toI32(bytes.length)
921
+ let i: i32 = from
922
+ while (i >= 0 && i < to && i < n && httpFieldsOws(toI32(bytes[i]))) {
923
+ i = i + 1
924
+ }
925
+ return i
926
+ }
927
+
928
+ /**
929
+ * Reads the parameters in `bytes[from .. to)` into `into`: each is optional
930
+ * whitespace, `;`, optional whitespace and then, unless the list ends or
931
+ * another `;` follows, a token name, `=` and a value that is a token or a
932
+ * quoted string (RFC 9110 §5.6.6). With `escapes`, a backslash inside the
933
+ * quotes takes the next octet as itself (§5.6.4); without, it is an octet
934
+ * like any other, as HTML form submission writes a name (it escapes `"` as
935
+ * `%22` instead). Answers false for any other shape, for a name given
936
+ * twice, letters compared without case, and for a 17th parameter.
937
+ */
938
+ const httpFieldsParams = (
939
+ bytes: readonly u8[],
940
+ from: i32,
941
+ to: i32,
942
+ escapes: boolean,
943
+ into: MediaType
944
+ ): boolean => {
945
+ let i: i32 = from
946
+ while (i >= 0 && i < to && i < toI32(bytes.length)) {
947
+ i = httpFieldsOwsEnd(bytes, i, to)
948
+ if (i === to) {
949
+ return true
950
+ }
951
+ if (i < 0 || i >= toI32(bytes.length) || toI32(bytes[i]) !== 59) {
952
+ return false
953
+ }
954
+ i = httpFieldsOwsEnd(bytes, i + 1, to)
955
+ if (i === to) {
956
+ return true
957
+ }
958
+ if (i < 0 || i >= toI32(bytes.length)) {
959
+ return false
960
+ }
961
+ if (toI32(bytes[i]) === 59) {
962
+ continue
963
+ }
964
+ const nameEnd: i32 = httpFieldsTokenEnd(bytes, i, to)
965
+ if (
966
+ nameEnd === i ||
967
+ nameEnd < 0 ||
968
+ nameEnd >= to ||
969
+ nameEnd >= toI32(bytes.length) ||
970
+ toI32(bytes[nameEnd]) !== 61
971
+ ) {
972
+ return false
973
+ }
974
+ // A cap on the count keeps the repeat check below from going quadratic.
975
+ if (toI32(into.names.length) >= HTTP_FIELDS_MAX_PARAMS) {
976
+ return false
977
+ }
978
+ const name: string = httpFieldsString(bytes, i, nameEnd, true)
979
+ for (const seen of into.names) {
980
+ if (seen === name) {
981
+ return false
982
+ }
983
+ }
984
+ i = nameEnd + 1
985
+ if (i < to && i < toI32(bytes.length) && toI32(bytes[i]) === 34) {
986
+ const out: string[] = []
987
+ i = i + 1
988
+ let closed: boolean = false
989
+ while (!closed && i >= 0 && i < to && i < toI32(bytes.length)) {
990
+ const c: i32 = toI32(bytes[i])
991
+ if (c === 34) {
992
+ closed = true
993
+ } else if (c === 92 && escapes) {
994
+ // A quoted pair: the octet after the backslash is a tab, a space,
995
+ // a visible character or obs-text, and stands for itself.
996
+ i = i + 1
997
+ if (i >= to || i >= toI32(bytes.length)) {
998
+ return false
999
+ }
1000
+ const e: i32 = toI32(bytes[i])
1001
+ if (!(e === 9 || (e >= 32 && e <= 126) || (e >= 128 && e <= 255))) {
1002
+ return false
1003
+ }
1004
+ out.push(String.fromCharCode(e))
1005
+ } else if (httpFieldsQdtext(c) || c === 92) {
1006
+ out.push(String.fromCharCode(c))
1007
+ } else {
1008
+ return false
1009
+ }
1010
+ i = i + 1
1011
+ }
1012
+ if (!closed) {
1013
+ return false
1014
+ }
1015
+ into.names.push(name)
1016
+ into.values.push(out.join(""))
1017
+ } else {
1018
+ const valueEnd: i32 = httpFieldsTokenEnd(bytes, i, to)
1019
+ if (valueEnd === i) {
1020
+ return false
1021
+ }
1022
+ into.names.push(name)
1023
+ into.values.push(httpFieldsString(bytes, i, valueEnd, false))
1024
+ i = valueEnd
1025
+ }
1026
+ }
1027
+ return i === to
1028
+ }
1029
+
1030
+ /**
1031
+ * Reads a `Content-Type` value (RFC 9110 §8.3.1): a `type/subtype` pair of
1032
+ * tokens, then parameters, each `; name=value` with optional whitespace
1033
+ * around the `;`, the value a token or a quoted string whose backslash takes
1034
+ * the next octet as itself. Whitespace may also open and close the value.
1035
+ * Type, subtype and parameter names are answered in lowercase; values as
1036
+ * given, less their quoting.
1037
+ *
1038
+ * const m: MediaType | null = mediaTypeParse(value, 0, toI32(value.length));
1039
+ * // m.type === "multipart", mediaTypeParam(m, "boundary") === "xyz"
1040
+ *
1041
+ * @param value the octets that hold the field value
1042
+ * @param from where the value starts in `value`
1043
+ * @param to where it ends, one past its last octet
1044
+ * @returns the media type, or null when the window is not inside `value`, a
1045
+ * token is empty or holds a non-`tchar`, a quoted string is not closed or
1046
+ * holds a control character, a parameter has no `=`, a parameter name
1047
+ * comes twice, or more than 16 parameters come
1048
+ */
1049
+ export const mediaTypeParse = (value: readonly u8[], from: i32, to: i32): MediaType | null => {
1050
+ const n: i32 = toI32(value.length)
1051
+ if (from < 0 || to < from || to > n) {
1052
+ return null
1053
+ }
1054
+ const start: i32 = httpFieldsOwsEnd(value, from, to)
1055
+ const typeEnd: i32 = httpFieldsTokenEnd(value, start, to)
1056
+ if (typeEnd === start || typeEnd < 0 || typeEnd >= to || typeEnd >= n || toI32(value[typeEnd]) !== 47) {
1057
+ return null
1058
+ }
1059
+ const subStart: i32 = typeEnd + 1
1060
+ const subEnd: i32 = httpFieldsTokenEnd(value, subStart, to)
1061
+ if (subEnd === subStart) {
1062
+ return null
1063
+ }
1064
+ const m: MediaType = new MediaType()
1065
+ m.type = httpFieldsString(value, start, typeEnd, true)
1066
+ m.subtype = httpFieldsString(value, subStart, subEnd, true)
1067
+ return httpFieldsParams(value, subEnd, to, true, m) ? m : null
1068
+ }
1069
+
1070
+ /**
1071
+ * Reads a part's `Content-Disposition` value the way a browser writes one in
1072
+ * a `multipart/form-data` body (RFC 7578 §4.2, and HTML's form submission):
1073
+ * a disposition type token, then parameters as `mediaTypeParse` reads them,
1074
+ * except that a backslash inside quotes is an octet like any other. A sender
1075
+ * writes a `"`, CR or LF in a name as `%22`, `%0D` or `%0A`, which are kept
1076
+ * as given; Windows paths keep their backslashes.
1077
+ *
1078
+ * @param value the octets that hold the field value
1079
+ * @param from where the value starts in `value`
1080
+ * @param to where it ends, one past its last octet
1081
+ * @returns the disposition, its type in `type` and `subtype` empty, or null
1082
+ * on the shapes `mediaTypeParse` refuses past the type
1083
+ */
1084
+ export const formDataDispositionParse = (value: readonly u8[], from: i32, to: i32): MediaType | null => {
1085
+ const n: i32 = toI32(value.length)
1086
+ if (from < 0 || to < from || to > n) {
1087
+ return null
1088
+ }
1089
+ const start: i32 = httpFieldsOwsEnd(value, from, to)
1090
+ const typeEnd: i32 = httpFieldsTokenEnd(value, start, to)
1091
+ if (typeEnd === start) {
1092
+ return null
1093
+ }
1094
+ const m: MediaType = new MediaType()
1095
+ m.type = httpFieldsString(value, start, typeEnd, true)
1096
+ return httpFieldsParams(value, typeEnd, to, false, m) ? m : null
1097
+ }
1098
+
1099
+ /**
1100
+ * The value of parameter `name` of `m`, its letters compared without case.
1101
+ *
1102
+ * @param m a media type or disposition `mediaTypeParse` or
1103
+ * `formDataDispositionParse` answered
1104
+ * @param name the parameter's name, in any case
1105
+ * @returns its value, quotes removed, or null when `m` has no such parameter
1106
+ */
1107
+ export const mediaTypeParam = (m: MediaType, name: string): string | null => {
1108
+ const raw: u8[] = httpFieldBytes(name)
1109
+ const key: string = httpFieldsString(raw, 0, toI32(raw.length), true)
1110
+ const count: i32 =
1111
+ toI32(m.names.length) < toI32(m.values.length) ? toI32(m.names.length) : toI32(m.values.length)
1112
+ let k: i32 = 0
1113
+ while (k >= 0 && k < count && k < toI32(m.names.length) && k < toI32(m.values.length)) {
1114
+ if (m.names[k] === key) {
1115
+ return m.values[k]
1116
+ }
1117
+ k = k + 1
1118
+ }
1119
+ return null
824
1120
  }