@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
package/std/base64.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * export const main = (): i32 => {
8
8
  * const hi: u8[] = [72, 105]; // "Hi"
9
- * const text: string = base64Encode(hi); // "SGk="
9
+ * const text: string | null = base64Encode(hi); // "SGk="
10
10
  * const out: u8[] = new Array<u8>(base64EncodedLength(2) + 1);
11
11
  * const end: i32 = base64EncodeInto(hi, 0, 2, out, 1); // out[1..5) is "SGk=", end is 5
12
12
  * return end;
@@ -21,7 +21,10 @@
21
21
  * - `base64EncodeInto(src, off, len, dst, at)` writes the encoding of
22
22
  * `src[off..off+len)` into `dst` from `at` and allocates nothing, so a frame
23
23
  * can be encoded chunk by chunk straight into its output buffer.
24
- * - `base64Encode(bytes)` is the whole array as a string.
24
+ * - `base64EncodeIfFits(src, off, len, dst, at)` is the same write and the
25
+ * same answer, kept for callers written against it.
26
+ * - `base64Encode(bytes)` is the whole array as a string, or `null` for an
27
+ * array past 1,610,612,733 bytes, whose encoding no string can hold.
25
28
  *
26
29
  * **This module only encodes**: what it is for writes base64 and never reads it
27
30
  * back. It is not the strict decoder of `nish/crypto/base64url`, which is a
@@ -42,20 +45,18 @@ const BASE64_PAD: i32 = 61
42
45
 
43
46
  /**
44
47
  * The characters `n` bytes encode to: four for every three, a short final
45
- * group counted as a whole one. Panics when `n` is negative or the length
46
- * would pass 2^31 - 1, which `n` above 1,610,612,733 does.
48
+ * group counted as a whole one. A negative `n`, or one above 1,610,612,733
49
+ * whose length would pass 2^31 - 1, answers -1.
47
50
  *
48
51
  * @param n How many bytes are to be encoded; zero answers zero.
49
52
  * @returns The number of base64 characters, padding included, that `n` bytes
50
- * encode to: always a multiple of four.
53
+ * encode to: always a multiple of four. -1 for a negative `n` or one whose
54
+ * encoding would be longer than 2^31 - 1 characters.
51
55
  */
52
56
  export const base64EncodedLength = (n: i32): i32 => {
53
- if (n < 0) {
54
- panic("base64EncodedLength: a negative byte count")
55
- }
56
57
  // (2^31 - 1) / 4 * 3 groups' worth of bytes is the most whose encoding fits.
57
- if (n > 1610612733) {
58
- panic("base64EncodedLength: the encoding is longer than 2^31 - 1 characters")
58
+ if (n < 0 || n > 1610612733) {
59
+ return -1
59
60
  }
60
61
  const groups: i32 = toI32((n + 2) / 3)
61
62
  return groups * 4
@@ -64,9 +65,10 @@ export const base64EncodedLength = (n: i32): i32 => {
64
65
  /**
65
66
  * Write the base64 of `src[off..off+len)` into `dst` from index `at`, and
66
67
  * answer the index just past the last character written, which is
67
- * `at + base64EncodedLength(len)`. Nothing is allocated. Panics when the source
68
- * range is not inside `src` or the encoding does not fit in `dst` from `at`;
69
- * both are checked before anything is written.
68
+ * `at + base64EncodedLength(len)`. Nothing is allocated. A source range not
69
+ * inside `src`, an encoding that does not fit in `dst` from `at`, or either
70
+ * array longer than 2^31 - 1 bytes answers -1 with nothing written: all three
71
+ * are checked before anything is written.
70
72
  *
71
73
  * @param src The bytes to encode from; only `src[off..off+len)` is read.
72
74
  * @param off Where in `src` the bytes to encode start.
@@ -75,32 +77,40 @@ export const base64EncodedLength = (n: i32): i32 => {
75
77
  * @param dst The buffer the characters are written into, one ASCII byte per
76
78
  * character; bytes outside the written range are left as they were.
77
79
  * @param at Where in `dst` the first character goes.
78
- * @returns The index in `dst` just past the last character written.
80
+ * @returns The index in `dst` just past the last character written, or -1
81
+ * when the range does not fit.
79
82
  */
80
83
  export const base64EncodeInto = (src: u8[], off: i32, len: i32, dst: u8[], at: i32): i32 => {
81
84
  // Refused before `toI32`, which saturates a longer length under
82
85
  // `--number-mode f64` and would let a range past the true end through.
83
86
  if (src.length > 2147483647 || dst.length > 2147483647) {
84
- panic("base64EncodeInto: an array longer than 2^31 - 1 bytes")
87
+ return -1
85
88
  }
86
89
  const srcLength: i32 = toI32(src.length)
87
90
  const dstLength: i32 = toI32(dst.length)
88
- if (off < 0 || len < 0 || off > srcLength - len) {
89
- panic("base64EncodeInto: the source range is outside the array")
91
+ if (off < 0 || len < 0 || len > srcLength - off) {
92
+ return -1
90
93
  }
91
- const need: i32 = base64EncodedLength(len)
94
+ // Past this many bytes the encoding is longer than 2^31 - 1 characters, as
95
+ // in `base64EncodedLength`.
96
+ if (len > 1610612733) {
97
+ return -1
98
+ }
99
+ const need: i32 = toI32((len + 2) / 3) * 4
92
100
  if (at < 0 || need > dstLength - at) {
93
- panic("base64EncodeInto: the encoding does not fit in the destination")
101
+ return -1
94
102
  }
95
103
  // A local, so that `abc.length >= 64` below is a fact about the string each
96
104
  // character is read from.
97
105
  const abc: string = BASE64_ALPHABET
98
106
  let i: i32 = off
99
107
  let o: i32 = at
100
- let rest: i32 = len
108
+ // At most `srcLength`, by the range check above; summed as `i64` because
109
+ // the overflow proof reads each operand's range alone.
110
+ const end: i32 = toI32(toI64(off) + toI64(len))
101
111
  // The checks above make every term after the first true; they are written out
102
112
  // because they are what proves the loop's accesses in range.
103
- while (rest >= 3 && i >= 0 && i < srcLength - 2 && o >= 0 && o < dstLength - 3 && abc.length >= 64) {
113
+ while (i >= 0 && i < srcLength - 2 && i + 2 < end && o >= 0 && o < dstLength - 3 && abc.length >= 64) {
104
114
  const group: i32 = (toI32(src[i]) << 16) | (toI32(src[i + 1]) << 8) | toI32(src[i + 2])
105
115
  dst[o] = toU8(abc.charCodeAt((group >> 18) & 63))
106
116
  dst[o + 1] = toU8(abc.charCodeAt((group >> 12) & 63))
@@ -108,36 +118,65 @@ export const base64EncodeInto = (src: u8[], off: i32, len: i32, dst: u8[], at: i
108
118
  dst[o + 3] = toU8(abc.charCodeAt(group & 63))
109
119
  i = i + 3
110
120
  o = o + 4
111
- rest = rest - 3
112
121
  }
113
- // A short final group of one or two bytes, padded. As in the loop, the
114
- // checks above make every term after the first false.
115
- if (rest === 0 || i < 0 || i >= srcLength || o < 0 || o >= dstLength - 3 || abc.length < 64) {
122
+ const rest: i64 = toI64(end) - toI64(i)
123
+ // A short final group of one or two bytes, padded; `rest` is 0, 1 or 2, tested
124
+ // by order (`===` 2 on an i64 misreads under runtime/nish.mjs). Every later term is false.
125
+ if (rest < 1 || i < 0 || i >= srcLength || o < 0 || o >= dstLength - 3 || abc.length < 64) {
116
126
  return o
117
127
  }
118
128
  let group: i32 = toI32(src[i]) << 16
119
- if (rest === 2 && i < srcLength - 1) {
129
+ if (rest > 1 && i < srcLength - 1) {
120
130
  group = group | (toI32(src[i + 1]) << 8)
121
131
  }
122
132
  dst[o] = toU8(abc.charCodeAt((group >> 18) & 63))
123
133
  dst[o + 1] = toU8(abc.charCodeAt((group >> 12) & 63))
124
- dst[o + 2] = rest === 2 ? toU8(abc.charCodeAt((group >> 6) & 63)) : toU8(BASE64_PAD)
134
+ dst[o + 2] = rest > 1 ? toU8(abc.charCodeAt((group >> 6) & 63)) : toU8(BASE64_PAD)
125
135
  dst[o + 3] = toU8(BASE64_PAD)
126
136
  return o + 4
127
137
  }
128
138
 
129
139
  /**
130
- * `bytes` as padded base64. Panics past 1,610,612,733 bytes, whose encoding
131
- * would not fit a string.
140
+ * `base64EncodeInto` under its earlier name: the same bytes written to the same
141
+ * place, or -1, with nothing written, when `src[off..off+len)` is not inside
142
+ * `src`, the encoding does not fit in `dst` from `at`, or either array is
143
+ * longer than 2^31 - 1 bytes.
144
+ *
145
+ * @param src The bytes to encode from; only `src[off..off+len)` is read.
146
+ * @param off Where in `src` the bytes to encode start.
147
+ * @param len How many bytes of `src` to encode; zero writes nothing and
148
+ * answers `at` when `at` is inside `dst` or just past it.
149
+ * @param dst The buffer the characters are written into, one ASCII byte per
150
+ * character; bytes outside the written range are left as they were.
151
+ * @param at Where in `dst` the first character goes.
152
+ * @returns The index in `dst` just past the last character written, or -1
153
+ * when the range does not fit.
154
+ */
155
+ export const base64EncodeIfFits = (src: u8[], off: i32, len: i32, dst: u8[], at: i32): i32 =>
156
+ base64EncodeInto(src, off, len, dst, at)
157
+
158
+ /**
159
+ * `bytes` as padded base64, or `null` past 1,610,612,733 bytes, whose encoding
160
+ * would be longer than 2^31 - 1 characters and so would not fit a string.
132
161
  *
133
162
  * @param bytes The bytes to encode, all of them; an empty array answers `""`.
134
- * @returns A fresh string holding the padded encoding.
163
+ * @returns A fresh string holding the padded encoding, or `null` for an array
164
+ * too long to encode.
135
165
  */
136
- export const base64Encode = (bytes: u8[]): string => {
166
+ export const base64Encode = (bytes: u8[]): string | null => {
137
167
  // A longer length saturates to the largest `i32` under `--number-mode f64`,
138
- // which `base64EncodedLength` refuses, as it does any count past 1,610,612,733.
168
+ // which this refuses, as it does any count past 1,610,612,733 (the most
169
+ // bytes whose encoding fits, as in `base64EncodedLength`).
139
170
  const n: i32 = toI32(bytes.length)
140
- const out: u8[] = new Array<u8>(base64EncodedLength(n))
141
- const end: i32 = base64EncodeInto(bytes, 0, n, out, 0)
142
- return String.fromBytes(out, 0, end)
171
+ if (n > 1610612733) {
172
+ return null
173
+ }
174
+ // `n` is at most 1,610,612,733 here, so the length is never -1.
175
+ const length: i32 = base64EncodedLength(n)
176
+ const out: u8[] = length > 0 ? new Array<u8>(length) : []
177
+ // `out` is exactly the encoding's length and the range is all of `bytes`,
178
+ // so this writes all of it and never answers -1.
179
+ base64EncodeInto(bytes, 0, n, out, 0)
180
+ // The encoding fills `out` exactly, so its length is where the string ends.
181
+ return String.fromBytes(out, 0, toI32(out.length))
143
182
  }