@amritk/nish-aarch64-linux 0.44.0 → 0.45.1

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 (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
@@ -5582,11 +5582,7 @@ const p384CurveB = (): u32[] => [
5582
5582
  * The plain 1, `[1, 0, …, 0]`. A Montgomery multiply by it divides by R, which
5583
5583
  * takes an element out of the Montgomery domain: fiat's `from_montgomery`.
5584
5584
  */
5585
- const p384PlainOne = (): u32[] => {
5586
- const out: u32[] = p384Limbs()
5587
- out[0] = 1
5588
- return out
5589
- }
5585
+ const p384PlainOne = (): u32[] => [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
5590
5586
 
5591
5587
  /** `out = a`, limb by limb. */
5592
5588
  const p384Copy = (out: u32[], a: u32[]): void => {
@@ -5682,8 +5678,12 @@ const p384Bit = (e: u32[], i: i32): boolean => {
5682
5678
  * which are a constant.
5683
5679
  */
5684
5680
  const p384FieldInvert = (out: u32[], a: u32[]): void => {
5685
- const e: u32[] = p384Prime()
5686
- e[0] = 0xfffffffd
5681
+ // p - 2: `p384Prime` with its low limb lowered by 2, spelled out so the
5682
+ // array's length is the literal's.
5683
+ const e: u32[] = [
5684
+ 0xfffffffd, 0x0, 0x0, 0xffffffff, 0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
5685
+ 0xffffffff, 0xffffffff,
5686
+ ]
5687
5687
  const acc: u32[] = p384Limbs()
5688
5688
  p384FiatSetOne(acc)
5689
5689
  for (let i: i32 = 383; i >= 0; i--) {
@@ -5697,8 +5697,11 @@ const p384FieldInvert = (out: u32[], a: u32[]): void => {
5697
5697
 
5698
5698
  /** `out = 1 / a` mod n, as `a^(n - 2)` with the scalar Montgomery multiply. */
5699
5699
  const p384ScalarInvert = (out: u32[], a: u32[]): void => {
5700
- const e: u32[] = p384Order()
5701
- e[0] = 0xccc52971
5700
+ // n - 2: `p384Order` with its low limb lowered by 2.
5701
+ const e: u32[] = [
5702
+ 0xccc52971, 0xecec196a, 0x48b0a77a, 0x581a0db2, 0xf4372ddf, 0xc7634d81, 0xffffffff, 0xffffffff,
5703
+ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
5704
+ ]
5702
5705
  const acc: u32[] = p384Limbs()
5703
5706
  p384FiatScalarSetOne(acc)
5704
5707
  for (let i: i32 = 383; i >= 0; i--) {
@@ -5991,10 +5994,14 @@ const p384ScalarInRange = (out: u32[], bytes: u8[], at: i32): boolean => {
5991
5994
  const p384DigestScalar = (out: u32[], digest: u8[]): void => {
5992
5995
  const length: i32 = toI32(digest.length)
5993
5996
  const bytes: u8[] = new Array<u8>(48)
5994
- const take: i32 = length < 48 ? length : 48
5995
- const skip: i32 = 48 - take
5996
- for (let i: i32 = 0; i < take && i < length && skip + i < 48; i++) {
5997
- bytes[skip + i] = digest[i]
5997
+ // The leftmost 48 bytes, big-endian: each one shifts the buffer up a byte
5998
+ // and comes in at the bottom, so a shorter digest ends right-aligned.
5999
+ for (let i: i32 = 0; i < length && i < 48; i++) {
6000
+ for (let j: i32 = 0; j < 47; j++) {
6001
+ const above: i32 = j + 1
6002
+ bytes[j] = bytes[above]
6003
+ }
6004
+ bytes[47] = digest[i]
5998
6005
  }
5999
6006
  const a: u32[] = p384Limbs()
6000
6007
  p384LimbsFromBytes(a, bytes, 0)
@@ -105,19 +105,60 @@ class PkiRootsLoad {
105
105
  }
106
106
  }
107
107
 
108
+ /**
109
+ * Counts one PEM block or file skipped. The count is not capped: past
110
+ * `PKI_ROOTS_MAX_FILES` a trust directory's entries are each counted without
111
+ * a read, so no bound below 2^31 holds for it (PKI-R8).
112
+ */
113
+ const pkiRootsSkip = (load: PkiRootsLoad): void => {
114
+ load.out.skipped = load.out.skipped + 1
115
+ }
116
+
117
+ /**
118
+ * What the line `text[from .. to)` is: 1 for a `-----BEGIN ...-----` line, 2
119
+ * for a line that starts `-----END `, 0 for any other. The line is cut here,
120
+ * so it is released when this returns rather than kept by the caller's loop.
121
+ */
122
+ const pkiRootsLineKind = (text: string, from: i32, to: i32): i32 => {
123
+ const line: string = text.substring(from, to)
124
+ if (line.startsWith("-----BEGIN ") && line.endsWith("-----") && line.length > 16) {
125
+ return 1
126
+ }
127
+ return line.startsWith("-----END ") ? 2 : 0
128
+ }
129
+
130
+ /** The lower-case hex digit of `v`, 0 to 15, as a one-character string. */
131
+ const pkiRootsHexDigit = (v: i32): string => String.fromCharCode(v < 10 ? 48 + v : 87 + v)
132
+
133
+ /**
134
+ * `bytes` in lower-case hex, the key a DER is kept once by. Linear: a for-of reads each octet,
135
+ * with no index left to prove, and pushes its two one-character digits, and
136
+ * one `join` copies the `2n` digits out, so `n` octets cost about `2n`
137
+ * allocations and `O(n)` time. Not `String.fromBytes`, whose range check no
138
+ * proof removes yet.
139
+ */
140
+ const pkiRootsHex = (bytes: u8[]): string => {
141
+ const out: string[] = []
142
+ for (const b of bytes) {
143
+ out.push(pkiRootsHexDigit(toI32(b) >> 4))
144
+ out.push(pkiRootsHexDigit(toI32(b) & 15))
145
+ }
146
+ return out.join("")
147
+ }
148
+
108
149
  /** Keeps `der` as a root when it parses, is new, and the cap has room; counts it skipped otherwise. */
109
150
  const pkiRootsKeep = (load: PkiRootsLoad, der: u8[]): void => {
110
151
  const cert: X509Certificate | null = x509ParseCertificate(der)
111
152
  if (cert === null) {
112
- load.out.skipped = load.out.skipped + 1
153
+ pkiRootsSkip(load)
113
154
  return
114
155
  }
115
- const key: string = String.fromBytes(der, 0, der.length)
156
+ const key: string = pkiRootsHex(der)
116
157
  if (load.seen.has(key)) {
117
158
  return
118
159
  }
119
160
  if (toI32(load.out.roots.length) >= PKI_ROOTS_MAX_ROOTS) {
120
- load.out.skipped = load.out.skipped + 1
161
+ pkiRootsSkip(load)
121
162
  return
122
163
  }
123
164
  load.seen.add(key)
@@ -125,16 +166,23 @@ const pkiRootsKeep = (load: PkiRootsLoad, der: u8[]): void => {
125
166
  }
126
167
 
127
168
  /**
128
- * One PEM block, `text` whole from its BEGIN line through its END line. A block
129
- * labelled other than CERTIFICATE gives `pemToDer` no DER, so it is skipped too.
169
+ * One PEM block, `text[from .. to)` whole from its BEGIN line through its END
170
+ * line. A block labelled other than CERTIFICATE gives `pemToDer` no DER, so it
171
+ * is skipped too.
130
172
  */
131
- const pkiRootsBlock = (load: PkiRootsLoad, text: string): void => {
132
- const ders: u8[][] | null = pemToDer(text, "CERTIFICATE")
133
- if (ders === null || ders.length !== 1) {
134
- load.out.skipped = load.out.skipped + 1
173
+ const pkiRootsBlock = (load: PkiRootsLoad, text: string, from: i32, to: i32): void => {
174
+ const block: string = text.substring(from, to)
175
+ const ders: u8[][] | null = pemToDer(block, "CERTIFICATE")
176
+ if (ders === null) {
177
+ pkiRootsSkip(load)
178
+ return
179
+ }
180
+ const list: u8[][] = ders
181
+ if (toI32(list.length) < 1 || toI32(list.length) > 1) {
182
+ pkiRootsSkip(load)
135
183
  return
136
184
  }
137
- pkiRootsKeep(load, ders[0])
185
+ pkiRootsKeep(load, list[0])
138
186
  }
139
187
 
140
188
  /**
@@ -148,39 +196,43 @@ const pkiRootsSplit = (load: PkiRootsLoad, text: string): void => {
148
196
  let open: boolean = false
149
197
  let blockStart: i32 = 0
150
198
  let lineStart: i32 = 0
151
- while (lineStart < n) {
199
+ while (lineStart >= 0 && lineStart < n) {
152
200
  let lineEnd: i32 = lineStart
153
- while (lineEnd >= 0 && lineEnd < toI32(text.length) && toI32(text.charCodeAt(lineEnd)) !== 10) {
154
- lineEnd = lineEnd + 1
201
+ while (lineEnd >= 0 && lineEnd < toI32(text.length)) {
202
+ if (toI32(text.charCodeAt(lineEnd)) === 10) {
203
+ break
204
+ }
205
+ // Unsigned: lineEnd is below text.length, so it cannot wrap.
206
+ lineEnd = toI32(toU32(lineEnd) + 1)
155
207
  }
156
- const next: i32 = lineEnd < n ? lineEnd + 1 : n
208
+ // Unsigned: lineEnd is below n on that side, so it cannot wrap.
209
+ const next: i32 = lineEnd < n ? toI32(toU32(lineEnd) + 1) : n
157
210
  // A carriage return, space or tab before the line feed is no part of the
158
211
  // line, so a BEGIN with trailing whitespace still opens a block and is
159
212
  // counted when `pemToDer` refuses it, rather than lost unseen.
160
213
  let end: i32 = lineEnd
161
- while (
162
- end > lineStart &&
163
- (toI32(text.charCodeAt(end - 1)) === 13 ||
164
- toI32(text.charCodeAt(end - 1)) === 32 ||
165
- toI32(text.charCodeAt(end - 1)) === 9)
166
- ) {
214
+ while (end > lineStart && end >= 1 && end <= toI32(text.length)) {
215
+ const c: i32 = toI32(text.charCodeAt(end - 1))
216
+ if (c !== 13 && c !== 32 && c !== 9) {
217
+ break
218
+ }
167
219
  end = end - 1
168
220
  }
169
- const line: string = text.slice(lineStart, end)
170
- if (line.startsWith("-----BEGIN ") && line.endsWith("-----") && line.length > 16) {
221
+ const kind: i32 = pkiRootsLineKind(text, lineStart, end)
222
+ if (kind === 1) {
171
223
  if (open) {
172
- load.out.skipped = load.out.skipped + 1
224
+ pkiRootsSkip(load)
173
225
  }
174
226
  open = true
175
227
  blockStart = lineStart
176
- } else if (open && line.startsWith("-----END ")) {
177
- pkiRootsBlock(load, text.slice(blockStart, next))
228
+ } else if (open && kind === 2) {
229
+ pkiRootsBlock(load, text, blockStart, next)
178
230
  open = false
179
231
  }
180
232
  lineStart = next
181
233
  }
182
234
  if (open) {
183
- load.out.skipped = load.out.skipped + 1
235
+ pkiRootsSkip(load)
184
236
  }
185
237
  }
186
238
 
@@ -200,8 +252,9 @@ const pkiRootsRegular = (path: string): string | null => {
200
252
 
201
253
  /** Reads `path` as roots: `false` when it is not a regular file that reads, `true` when it was (or would have been, past a cap). */
202
254
  const pkiRootsFile = (load: PkiRootsLoad, path: string): boolean => {
203
- if (load.files >= PKI_ROOTS_MAX_FILES) {
204
- load.out.skipped = load.out.skipped + 1
255
+ const files: i32 = load.files
256
+ if (files >= PKI_ROOTS_MAX_FILES) {
257
+ pkiRootsSkip(load)
205
258
  return true
206
259
  }
207
260
  const real: string | null = pkiRootsRegular(path)
@@ -209,10 +262,10 @@ const pkiRootsFile = (load: PkiRootsLoad, path: string): boolean => {
209
262
  if (text === null) {
210
263
  return false
211
264
  }
212
- load.files = load.files + 1
265
+ load.files = files + 1
213
266
  load.out.sources.push(path)
214
267
  if (toI32(text.length) > PKI_ROOTS_MAX_FILE_BYTES) {
215
- load.out.skipped = load.out.skipped + 1
268
+ pkiRootsSkip(load)
216
269
  return true
217
270
  }
218
271
  pkiRootsSplit(load, text)
@@ -236,7 +289,7 @@ export const pkiRootsDirEntry = (name: string): boolean => {
236
289
  return name.length > 4
237
290
  }
238
291
  const n: i32 = toI32(name.length)
239
- if (n < 10 || toI32(name.charCodeAt(8)) !== 46) {
292
+ if (n < 10 || toI32(name.length) < 10 || toI32(name.charCodeAt(8)) !== 46) {
240
293
  return false
241
294
  }
242
295
  for (let i: i32 = 0; i < 8 && i < toI32(name.length); i = i + 1) {
@@ -258,9 +311,10 @@ const pkiRootsDir = (load: PkiRootsLoad, dir: string): void => {
258
311
  if (names === null) {
259
312
  return
260
313
  }
314
+ const list: string[] = names
261
315
  const base: string = dir.endsWith("/") ? dir : `${dir}/`
262
- for (let i: i32 = 0; i < toI32(names.length); i = i + 1) {
263
- const name: string = names[i]
316
+ for (let i: i32 = 0; i < toI32(list.length); i = i + 1) {
317
+ const name: string = list[i]
264
318
  if (pkiRootsDirEntry(name)) {
265
319
  pkiRootsFile(load, `${base}${name}`)
266
320
  }
@@ -298,15 +352,18 @@ export const pkiLoadRoots = (file: string | null, dirs: string[], bundles: strin
298
352
  const pkiRootsSplitDirs = (list: string): string[] => {
299
353
  const out: string[] = []
300
354
  let start: i32 = 0
301
- const n: i32 = toI32(list.length)
302
- for (let i: i32 = 0; i <= n; i = i + 1) {
303
- if (i >= n || (i >= 0 && i < toI32(list.length) && toI32(list.charCodeAt(i)) === 58)) {
304
- if (i > start) {
305
- out.push(list.slice(start, i))
355
+ for (let i: i32 = 0; i < toI32(list.length); i = i + 1) {
356
+ if (toI32(list.charCodeAt(i)) === 58) {
357
+ if (i > start && start >= 0) {
358
+ out.push(list.substring(start, i))
306
359
  }
307
360
  start = i + 1
308
361
  }
309
362
  }
363
+ const n: i32 = toI32(list.length)
364
+ if (n > start && start >= 0) {
365
+ out.push(list.substring(start, n))
366
+ }
310
367
  return out
311
368
  }
312
369