@amritk/nish-x86_64-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
@@ -297,8 +297,9 @@ export class Upstream {
297
297
  if (connections < 1 || slots < 1 || limits.maxHead < 1 || limits.timeoutMs < 1) {
298
298
  panic("Upstream: connections, calls, maxHead and timeoutMs must each be at least 1")
299
299
  }
300
- // Both are in 0 .. 2^31, so their unsigned sum cannot wrap.
301
- if (firstId < 0 || toU32(firstId) + toU32(connections) > toU32(HANDLER_MAX_WATCHED)) {
300
+ // The test's 16 is HANDLER_MAX_WATCHED: past it the sum is past it too,
301
+ // and short of it `firstId + connections` is at most 32.
302
+ if (firstId < 0 || firstId > 16 || connections > 16 || firstId + connections > HANDLER_MAX_WATCHED) {
302
303
  panic(
303
304
  `Upstream: ${connections} watch ids from ${firstId} are not all inside 0 to ${HANDLER_MAX_WATCHED - 1}`
304
305
  )
@@ -311,15 +312,15 @@ export class Upstream {
311
312
  this.connections = []
312
313
  for (let k: i32 = 0; k < connections; k += 1) {
313
314
  // `firstId + k` is below `firstId + connections`, which the check above holds to HANDLER_MAX_WATCHED.
314
- const id: i32 = toI32(toU32(firstId) + toU32(k))
315
+ const id: i32 = firstId + k
315
316
  this.connections.push(new UpstreamConnection(id, upstreamParser(limits)))
316
317
  }
317
318
  const placeholder: HttpRequest = new HttpRequest(-1)
318
319
  this.calls = []
319
320
  const free: i32[] = upstreamSlots(slots)
320
321
  for (let k: i32 = 0; k < slots && k < toI32(free.length); k += 1) {
321
- // `k` is below `slots`, so `slots - 1 - k` is in 0 .. slots - 1 and the unsigned difference cannot wrap.
322
- free[k] = toI32(toU32(slots) - 1 - toU32(k))
322
+ // `k` is below `slots`, so `slots - 1 - k` is in 0 .. slots - 1.
323
+ free[k] = slots - 1 - k
323
324
  }
324
325
  for (let k: i32 = 0; k < slots; k += 1) {
325
326
  this.calls.push(new UpstreamResponse(k, limits.maxHead, placeholder))
@@ -425,9 +426,9 @@ export class Upstream {
425
426
  r.length = 0
426
427
  r.retried = false
427
428
  // The clock counts nanoseconds since boot and the span is under 2^52 ns, so the sum is far inside
428
- // i64; added as u64 it is the same two's-complement sum without an overflow check.
429
+ // i64 and `upstreamLater` never saturates it.
429
430
  const span: i64 = toI64(this.limits.timeoutMs) * UPSTREAM_NS_PER_MS
430
- r.deadline = toI64(toU64(monotonicNanos()) + toU64(span))
431
+ r.deadline = upstreamLater(monotonicNanos(), span)
431
432
  if (this.error !== UPSTREAM_E_NONE) {
432
433
  this.end(r, this.error)
433
434
  return UPSTREAM_E_NONE
@@ -504,8 +505,13 @@ export class Upstream {
504
505
  * @returns The count of slots in use.
505
506
  */
506
507
  pending(): i32 {
507
- // `freeCount` stays in 0 .. free.length, so the unsigned difference cannot wrap.
508
- return toI32(toU32(toI32(this.free.length)) - toU32(this.freeCount))
508
+ // `freeCount` stays in 0 .. free.length, so the test never fires.
509
+ const size: i32 = toI32(this.free.length)
510
+ const free: i32 = this.freeCount
511
+ if (free < 0 || size < free) {
512
+ return 0
513
+ }
514
+ return size - free
509
515
  }
510
516
 
511
517
  /**
@@ -661,14 +667,19 @@ export class Upstream {
661
667
  const head: u8[] = r.head
662
668
  const end: i32 = r.headLength
663
669
  // `get` wrote `end` bytes into `head` and `sent` only grows to `end`, so this never fires.
664
- if (c.sent < 0 || end > toI32(head.length)) {
670
+ if (c.sent < 0 || end < 0 || end > toI32(head.length)) {
665
671
  this.finish(c, UPSTREAM_E_REQUEST)
666
672
  return
667
673
  }
668
674
  let sent: i32 = c.sent
669
675
  while (sent < end) {
670
- // Unsigned, as `0 <= sent < end` makes the difference fit and the prover cannot relate the two.
671
- const left: i32 = toI32(toU32(end) - toU32(sent))
676
+ // `sent` starts at least 0 and only grows, so this never fires; it is
677
+ // what proves `end - sent` fits.
678
+ if (sent < 0) {
679
+ this.finish(c, UPSTREAM_E_REQUEST)
680
+ return
681
+ }
682
+ const left: i32 = end - sent
672
683
  const n: i32 = netWrite(c.fd, head, sent, left)
673
684
  if (n === UPSTREAM_WOULD_BLOCK) {
674
685
  this.interest(c, UPSTREAM_WRITE)
@@ -678,8 +689,17 @@ export class Upstream {
678
689
  this.finish(c, UPSTREAM_E_CLOSED)
679
690
  return
680
691
  }
681
- // Unsigned, as `n <= left` keeps the sum at most `end`.
682
- sent = toI32(toU32(sent) + toU32(n))
692
+ // `netWrite` writes at most the `left` bytes it was asked for, so `gap`
693
+ // is never negative and `sent` becomes `sent + n`, at most `end`. A
694
+ // larger answer would count the head as all sent.
695
+ let next: i32 = end
696
+ if (left >= 0) {
697
+ const gap: i32 = left - n
698
+ if (gap >= 0) {
699
+ next = end - gap
700
+ }
701
+ }
702
+ sent = next
683
703
  c.sent = sent
684
704
  }
685
705
  this.interest(c, UPSTREAM_READ)
@@ -839,33 +859,51 @@ const upstreamParser = (limits: UpstreamLimits): Http1ResponseParser => {
839
859
  return p
840
860
  }
841
861
 
842
- /**
843
- * `n` zero bytes, none when `n` is below 1. Filled by `push`, so no `i32`
844
- * length is computed for `new Array`.
845
- */
862
+ /** `n` zero bytes, none when `n` is below 1: one zeroed allocation, not a `push` per byte. */
846
863
  const upstreamBytes = (n: i32): u8[] => {
847
- const a: u8[] = []
848
- for (let i: i32 = 0; i < n; i += 1) {
849
- a.push(0)
864
+ if (n <= 0) {
865
+ return []
850
866
  }
851
- return a
867
+ return new Array<u8>(n)
852
868
  }
853
869
 
854
- /** Room for `n` slot indexes, filled the way `upstreamBytes` is. */
870
+ /** Room for `n` zeroed slot indexes, none when `n` is below 1, allocated the way `upstreamBytes` is. */
855
871
  const upstreamSlots = (n: i32): i32[] => {
856
- const a: i32[] = []
857
- for (let i: i32 = 0; i < n; i += 1) {
858
- a.push(0)
872
+ if (n <= 0) {
873
+ return []
859
874
  }
860
- return a
875
+ return new Array<i32>(n)
861
876
  }
862
877
 
863
878
  /**
864
879
  * The poll token the handler's loop reports for watch id `id`: `-2 - id`.
865
- * Watch ids are in 0 .. HANDLER_MAX_WATCHED, so the unsigned difference is
866
- * that same small negative number and does not wrap past it.
880
+ * Watch ids are in 0 .. HANDLER_MAX_WATCHED, which the constructor checks,
881
+ * so the difference is a small negative number and the test, whose
882
+ * 2147483646 is the largest id `-2 - id` holds, never fires; past it the
883
+ * answer is the one the unsigned difference gave.
884
+ */
885
+ const upstreamToken = (id: i32): i32 => {
886
+ if (id > 2147483646) {
887
+ return 2147483647
888
+ }
889
+ return -2 - id
890
+ }
891
+
892
+ /**
893
+ * The `monotonicNanos` instant `span` nanoseconds after `now`, or the last
894
+ * `i64` when the sum would pass it, which no clock reading does. Both are at
895
+ * least 0, so they are summed in `u64`, where two values below 2^63 cannot
896
+ * wrap, and the sum is turned back only once it is known to fit. A negative
897
+ * `now` or `span`, which no caller passes, answers `now`.
867
898
  */
868
- const upstreamToken = (id: i32): i32 => toI32(toU32(-2) - toU32(id))
899
+ const upstreamLater = (now: i64, span: i64): i64 => {
900
+ if (now < 0 || span < 0) {
901
+ return now
902
+ }
903
+ const sum: u64 = toU64(now) + toU64(span)
904
+ const top: u64 = (toU64(1) << toU64(63)) - toU64(1)
905
+ return sum > top ? toI64(top) : toI64(sum)
906
+ }
869
907
 
870
908
  /**
871
909
  * A ring of call slot indexes, one place per slot. Each slot sits in at most
@@ -889,9 +927,12 @@ class UpstreamQueue {
889
927
  if (this.start < 0 || this.start >= n || offset < 0 || offset >= n) {
890
928
  return -1
891
929
  }
892
- // Both are below `n`, under 2^31, so the unsigned sum cannot wrap, and one lap brings it back under `n`.
930
+ // Both are below `n`, under 2^31, so the unsigned sum cannot wrap, and one lap brings it back under `n`:
931
+ // worked in `u32` throughout, the place is at most `n - 1` and turns back exactly.
932
+ const size: u32 = toU32(n)
893
933
  const sum: u32 = toU32(this.start) + toU32(offset)
894
- return toI32(sum < toU32(n) ? sum : sum - toU32(n))
934
+ const place: u32 = sum < size ? sum : sum - size
935
+ return toI32(place)
895
936
  }
896
937
 
897
938
  /** Puts `index` last. */
@@ -974,13 +1015,17 @@ const upstreamAppend = (r: UpstreamResponse, data: u8[], off: i32, len: i32): bo
974
1015
  if (length > toI32(old.length)) {
975
1016
  return false
976
1017
  }
977
- // Built by `push`, so no `i32` length is computed for `new Array`.
978
- const grown: u8[] = []
979
- for (let i: i32 = 0; i < length && i < toI32(old.length); i += 1) {
980
- grown.push(old[i])
1018
+ // One zeroed allocation and one bulk copy; the guard proves the length.
1019
+ if (size > toU32(2147483647)) {
1020
+ return false
1021
+ }
1022
+ const room: i32 = toI32(size)
1023
+ if (room <= 0) {
1024
+ return false
981
1025
  }
982
- while (toU32(grown.length) < size) {
983
- grown.push(0)
1026
+ const grown: u8[] = new Array<u8>(room)
1027
+ if (!tryCopyBytes(grown, 0, old, 0, length)) {
1028
+ return false
984
1029
  }
985
1030
  r.body = grown
986
1031
  }