@amritk/nish-x86_64-linux 0.42.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 (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  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 +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
@@ -13,7 +13,10 @@
13
13
  *
14
14
  * The module also holds RFC 4648 §4 standard base64 — `+` and `/`, padded with
15
15
  * `=` — as `base64StdPadEncode` and `base64StdPadDecode`, for PEM
16
- * (`nish/crypto/x509`) and SCRAM (`nish/net/postgres`). It is as strict and as
16
+ * (`nish/crypto/x509`) and SCRAM (`nish/net/postgres`), and as
17
+ * `base64StdPadEncodeInto` and `base64StdPadDecodedLength`, which read and
18
+ * write a caller's buffer and allocate nothing, for the WebSocket handshake
19
+ * (`nish/net/websocket`). It is as strict and as
17
20
  * constant-time as the URL alphabet, by the same masks, and it refuses a `=`
18
21
  * anywhere but the last one or two characters of a text whose length is a
19
22
  * multiple of four. They are not `nish/base64`'s `base64Encode`, which encodes
@@ -91,6 +94,19 @@ const base64urlSextetOf = (c: i32): i32 => {
91
94
  return value | ~(upper | lower | digit | dash | underscore)
92
95
  }
93
96
 
97
+ /**
98
+ * Writes `byte` to `out[at]` and answers `at + 1`. Every caller writes inside
99
+ * an array it sized or checked for the text or bytes it puts there; the test
100
+ * is the shape that proves the store, not a check a caller can fail.
101
+ */
102
+ const base64Put = (out: u8[], at: i32, byte: u8): i32 => {
103
+ if (at >= 0 && at < toI32(out.length)) {
104
+ out[at] = byte
105
+ return at + 1
106
+ }
107
+ return at
108
+ }
109
+
94
110
  /**
95
111
  * `data` as unpadded base64url: four characters per three bytes, and two or
96
112
  * three for a short final group.
@@ -161,49 +177,57 @@ export const base64urlDecode = (text: string): u8[] | null => {
161
177
  return null
162
178
  }
163
179
  // Three bytes per full group of four, and one fewer than the characters in a
164
- // short final group; `n * 3 / 4` would overflow for a text over 700 MB.
165
- const out: u8[] = new Array<u8>((n >> 2) * 3 + (tail === 0 ? 0 : tail - 1))
166
- const outLen: i32 = toI32(out.length)
180
+ // short final group; `n * 3 / 4` would overflow for a text over 700 MB. The
181
+ // sum is a `u32`, exact for any `n` here.
182
+ const out: u8[] = new Array<u8>(toU32(n >> 2) * 3 + (tail === 0 ? 0 : toU32(tail) - 1))
167
183
  // All ones once any character is outside the alphabet (a sextet of `-1`
168
184
  // shifted right by 31), and non-zero once the text ends on a bit that
169
185
  // encodes no byte.
170
186
  let bad: i32 = 0
171
187
  // Four characters make one 24-bit word and three bytes, so a full group has
172
188
  // no count of waiting bits to test, and its four sextets are independent.
189
+ // `full` is where the short final group starts, `n` less its tail.
190
+ //
191
+ // Each read is written out here rather than in a helper: a function that
192
+ // hands its `string` parameter to another is taken to keep it, and then a
193
+ // caller decoding a string it built inside `using a = arena()` is refused.
194
+ // The test on each position is the shape that proves the read, and depends
195
+ // on the position alone; positions are `u32`s so that `k + 3` wraps rather
196
+ // than overflows.
197
+ const full: u32 = toU32(n & ~3)
173
198
  let j: i32 = 0
174
- for (let k: i32 = 0; k + 3 < n; k += 4) {
175
- const s0: i32 = base64urlSextetOf(toI32(text.charCodeAt(k)))
176
- const s1: i32 = base64urlSextetOf(toI32(text.charCodeAt(k + 1)))
177
- const s2: i32 = base64urlSextetOf(toI32(text.charCodeAt(k + 2)))
178
- const s3: i32 = base64urlSextetOf(toI32(text.charCodeAt(k + 3)))
199
+ for (let k: u32 = 0; k < full; k += 4) {
200
+ const i0: i32 = toI32(k)
201
+ const i1: i32 = toI32(k + 1)
202
+ const i2: i32 = toI32(k + 2)
203
+ const i3: i32 = toI32(k + 3)
204
+ const s0: i32 = i0 >= 0 && i0 < n ? base64urlSextetOf(toI32(text.charCodeAt(i0))) : -1
205
+ const s1: i32 = i1 >= 0 && i1 < n ? base64urlSextetOf(toI32(text.charCodeAt(i1))) : -1
206
+ const s2: i32 = i2 >= 0 && i2 < n ? base64urlSextetOf(toI32(text.charCodeAt(i2))) : -1
207
+ const s3: i32 = i3 >= 0 && i3 < n ? base64urlSextetOf(toI32(text.charCodeAt(i3))) : -1
179
208
  bad = bad | ((s0 | s1 | s2 | s3) >> 31)
180
209
  const word: i32 = ((s0 & 63) << 18) | ((s1 & 63) << 12) | ((s2 & 63) << 6) | (s3 & 63)
181
- // Always true — `out` was sized from the same length — but written out so
182
- // that the three stores keep no bounds check.
183
- if (j >= 0 && j + 2 < outLen) {
184
- out[j] = toU8(word >> 16)
185
- out[j + 1] = toU8((word >> 8) & 255)
186
- out[j + 2] = toU8(word & 255)
187
- }
188
- j += 3
210
+ j = base64Put(out, j, toU8(word >> 16))
211
+ j = base64Put(out, j, toU8((word >> 8) & 255))
212
+ j = base64Put(out, j, toU8(word & 255))
189
213
  }
190
214
  // A short final group of two or three characters: one or two bytes, and the
191
215
  // four or two bits left over belong to no byte, so they must be zero for `AA`
192
216
  // to be the only spelling of a zero byte and `AB` refused (RFC 4648 §3.5).
193
217
  if (tail !== 0) {
194
- const k: i32 = n - tail
195
- const s0: i32 = base64urlSextetOf(toI32(text.charCodeAt(k)))
196
- const s1: i32 = base64urlSextetOf(toI32(text.charCodeAt(k + 1)))
218
+ const i0: i32 = toI32(full)
219
+ const i1: i32 = toI32(full + 1)
220
+ const i2: i32 = toI32(full + 2)
221
+ const s0: i32 = i0 >= 0 && i0 < n ? base64urlSextetOf(toI32(text.charCodeAt(i0))) : -1
222
+ const s1: i32 = i1 >= 0 && i1 < n ? base64urlSextetOf(toI32(text.charCodeAt(i1))) : -1
197
223
  // A third character only when the tail has one; a two-character tail reads
198
224
  // its place as `A`, the zero sextet, which adds no bits.
199
- const s2: i32 = tail === 3 ? base64urlSextetOf(toI32(text.charCodeAt(k + 2))) : 0
225
+ const s2: i32 = tail === 3 && i2 >= 0 && i2 < n ? base64urlSextetOf(toI32(text.charCodeAt(i2))) : 0
200
226
  bad = bad | ((s0 | s1 | s2) >> 31)
201
227
  const word: i32 = ((s0 & 63) << 18) | ((s1 & 63) << 12) | ((s2 & 63) << 6)
202
- if (j >= 0 && j < outLen) {
203
- out[j] = toU8(word >> 16)
204
- }
205
- if (tail === 3 && j + 1 >= 0 && j + 1 < outLen) {
206
- out[j + 1] = toU8((word >> 8) & 255)
228
+ j = base64Put(out, j, toU8(word >> 16))
229
+ if (tail === 3) {
230
+ base64Put(out, j, toU8((word >> 8) & 255))
207
231
  }
208
232
  // The bits under the last byte written: sixteen of the word when one byte
209
233
  // was, eight when two were.
@@ -283,6 +307,111 @@ export const base64StdPadEncode = (data: u8[]): string => {
283
307
  return parts.join("")
284
308
  }
285
309
 
310
+ /**
311
+ * `base64StdPadEncode` of `data[start .. start + length)`, written into `out`
312
+ * from `at` as the bytes of its characters, allocating nothing: the WebSocket
313
+ * accept key that a server writes for every handshake (RFC 6455 §4.2.2). A
314
+ * range outside `data`, or text that does not fit in `out` from `at`, panics.
315
+ *
316
+ * @param data The buffer holding the bytes to encode; it is read, not changed.
317
+ * @param start Where in `data` the bytes start.
318
+ * @param length How many bytes to encode.
319
+ * @param out The buffer the text is written into.
320
+ * @param at Where in `out` the text starts.
321
+ * @returns The index in `out` just past the text, `at + 4 × ceil(length / 3)`.
322
+ */
323
+ export const base64StdPadEncodeInto = (data: u8[], start: i32, length: i32, out: u8[], at: i32): i32 => {
324
+ if (data.length > 2147483647 || out.length > 2147483647) {
325
+ panic("base64StdPadEncodeInto: a buffer longer than 2^31 - 1 bytes")
326
+ }
327
+ if (start < 0 || length < 0 || start > toI32(data.length) - length || length > 1610612733) {
328
+ panic("base64StdPadEncodeInto: bytes outside their buffer")
329
+ }
330
+ // The text's length, and the room after `at`, as `u32`s: exact, since
331
+ // `length` is at most 1610612733 and `at` at most `out.length`.
332
+ const textLength: u32 = ((toU32(length) + 2) / 3) * 4
333
+ if (at < 0 || at > toI32(out.length) || textLength > toU32(toI32(out.length)) - toU32(at)) {
334
+ panic("base64StdPadEncodeInto: text that does not fit its buffer")
335
+ }
336
+ const end: i32 = toI32(toU32(at) + textLength)
337
+ // `k` runs through `data[start .. start + length)` and `j` through the text;
338
+ // the bounds on them only let the prover see what the tests above checked.
339
+ let j: i32 = at
340
+ let k: i32 = start
341
+ let acc: i32 = 0
342
+ let bits: u32 = 0
343
+ for (let n: i32 = 0; n < length && k >= 0 && k < toI32(data.length); n++) {
344
+ acc = ((acc << 8) | toI32(data[k])) & 0xfff
345
+ k += 1
346
+ bits += 8
347
+ while (bits >= 6) {
348
+ bits -= 6
349
+ j = base64Put(out, j, toU8(base64StdPadCharOf((acc >> toI32(bits)) & 63)))
350
+ }
351
+ }
352
+ if (bits > 0) {
353
+ j = base64Put(out, j, toU8(base64StdPadCharOf((acc << toI32(6 - bits)) & 63)))
354
+ }
355
+ while (j >= 0 && j < end && j < toI32(out.length)) {
356
+ out[j] = 61
357
+ j += 1
358
+ }
359
+ return end
360
+ }
361
+
362
+ /**
363
+ * How many bytes the padded standard base64 in `text[start .. start + length)`
364
+ * decodes to, or -1 unless it is their one canonical spelling, by
365
+ * `base64StdPadDecode`'s rules and its masks; nothing is decoded or allocated.
366
+ * A WebSocket server checks a handshake's `Sec-WebSocket-Key` this way, where
367
+ * it lies in the request (RFC 6455 §4.2.1, item 5). A range outside `text`
368
+ * panics.
369
+ *
370
+ * @param text The buffer holding the text, one character a byte.
371
+ * @param start Where in `text` the base64 starts.
372
+ * @param length How many characters it is.
373
+ * @returns The decoded length, or -1 when the text is not canonical.
374
+ */
375
+ export const base64StdPadDecodedLength = (text: u8[], start: i32, length: i32): i32 => {
376
+ if (text.length > 2147483647) {
377
+ panic("base64StdPadDecodedLength: a buffer longer than 2^31 - 1 bytes")
378
+ }
379
+ if (start < 0 || length < 0 || start > toI32(text.length) - length) {
380
+ panic("base64StdPadDecodedLength: text outside its buffer")
381
+ }
382
+ if ((length & 3) !== 0) {
383
+ return -1
384
+ }
385
+ // The text's last character, a `u32` sum that is exact once the range is
386
+ // inside `text`; a length that is a multiple of four and not 0 is at least 4,
387
+ // so the character before it is in the range too.
388
+ const last: i32 = toI32(toU32(start) + toU32(length) - 1)
389
+ let pad: u32 = 0
390
+ if (length > 0 && last >= 1 && last < toI32(text.length) && toI32(text[last]) === 61) {
391
+ pad = toI32(text[last - 1]) === 61 ? 2 : 1
392
+ }
393
+ const chars: i32 = toI32(toU32(length) - pad)
394
+ let bad: i32 = 0
395
+ let acc: i32 = 0
396
+ let bits: u32 = 0
397
+ let p: i32 = start
398
+ for (let k: i32 = 0; k < chars && p >= 0 && p < toI32(text.length); k++) {
399
+ const v: i32 = base64StdPadSextetOf(toI32(text[p]))
400
+ p += 1
401
+ bad = bad | (v >> 31)
402
+ acc = ((acc << 6) | (v & 63)) & 0xfff
403
+ bits += 6
404
+ if (bits >= 8) {
405
+ bits -= 8
406
+ }
407
+ }
408
+ bad = bad | (acc & toI32((toU32(1) << bits) - 1))
409
+ if (bad !== 0) {
410
+ return -1
411
+ }
412
+ return toI32(toU32(length >> 2) * 3 - pad)
413
+ }
414
+
286
415
  /**
287
416
  * The bytes of padded standard base64 `text`, or `null` unless it is the one
288
417
  * canonical spelling of them: a whole number of four-character groups, `=`
@@ -305,16 +434,16 @@ export const base64StdPadDecode = (text: string): u8[] | null => {
305
434
  if ((n & 3) !== 0) {
306
435
  return null
307
436
  }
308
- let pad: i32 = 0
309
- if (n > 0 && toI32(text.charCodeAt(n - 1)) === 61) {
437
+ // A length that is a multiple of four and not 0 is at least 4.
438
+ let pad: u32 = 0
439
+ if (n >= 4 && toI32(text.charCodeAt(n - 1)) === 61) {
310
440
  pad = toI32(text.charCodeAt(n - 2)) === 61 ? 2 : 1
311
441
  }
312
- const chars: i32 = n - pad
313
- const out: u8[] = new Array<u8>((n >> 2) * 3 - pad)
314
- const outLen: i32 = toI32(out.length)
442
+ const chars: i32 = toI32(toU32(n) - pad)
443
+ const out: u8[] = new Array<u8>(toU32(n >> 2) * 3 - pad)
315
444
  let bad: i32 = 0
316
445
  let acc: i32 = 0
317
- let bits: i32 = 0
446
+ let bits: u32 = 0
318
447
  let j: i32 = 0
319
448
  // The padding is past `chars` and is not data; which positions those are is
320
449
  // a fact about the length. A `=` before them is outside the alphabet.
@@ -325,15 +454,10 @@ export const base64StdPadDecode = (text: string): u8[] | null => {
325
454
  bits += 6
326
455
  if (bits >= 8) {
327
456
  bits -= 8
328
- // Always true — `out` was sized from the same length — and written so
329
- // the store keeps no bounds check.
330
- if (j >= 0 && j < outLen) {
331
- out[j] = toU8((acc >> bits) & 255)
332
- }
333
- j++
457
+ j = base64Put(out, j, toU8((acc >> toI32(bits)) & 255))
334
458
  }
335
459
  }
336
- bad = bad | (acc & ((1 << bits) - 1))
460
+ bad = bad | (acc & toI32((toU32(1) << bits) - 1))
337
461
  if (bad !== 0) {
338
462
  return null
339
463
  }