nish 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 (125) hide show
  1. package/README.md +11 -5
  2. package/docs/AI.md +287 -129
  3. package/package.json +9 -9
  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/skills/writing-nish/SKILL.md +12 -4
  21. package/std/README.md +6 -6
  22. package/std/array.ts +10 -2
  23. package/std/bytes.ts +239 -26
  24. package/std/cli-args.ts +1072 -606
  25. package/std/cli-flags.ts +180 -98
  26. package/std/cli.ts +21 -13
  27. package/std/collections.ts +245 -127
  28. package/std/compress/checksum.ts +32 -37
  29. package/std/compress/deflate.ts +1102 -542
  30. package/std/compress/gzip.ts +290 -145
  31. package/std/compress/zlib.ts +57 -21
  32. package/std/crypto/aes.ts +153 -75
  33. package/std/crypto/base64url.ts +163 -39
  34. package/std/crypto/bcrypt-batch.ts +164 -90
  35. package/std/crypto/bcrypt.ts +185 -107
  36. package/std/crypto/chacha20poly1305.ts +84 -68
  37. package/std/crypto/ct.ts +9 -7
  38. package/std/crypto/ed25519.ts +64 -27
  39. package/std/crypto/hkdf.ts +105 -63
  40. package/std/crypto/hmac.ts +95 -89
  41. package/std/crypto/jwt.ts +248 -126
  42. package/std/crypto/p256.ts +758 -444
  43. package/std/crypto/p384.ts +20 -13
  44. package/std/crypto/pki-roots.ts +95 -38
  45. package/std/crypto/pki.ts +164 -60
  46. package/std/crypto/rsa.ts +266 -223
  47. package/std/crypto/scram.ts +141 -0
  48. package/std/crypto/sha1.ts +157 -57
  49. package/std/crypto/sha256.ts +502 -422
  50. package/std/crypto/sha512.ts +204 -135
  51. package/std/crypto/x509-random.ts +9 -3
  52. package/std/crypto/x509.ts +331 -171
  53. package/std/host/ring.ts +1 -1
  54. package/std/host/text.ts +1 -1
  55. package/std/json-equal.ts +35 -19
  56. package/std/json-format.ts +215 -108
  57. package/std/json-number.ts +126 -72
  58. package/std/json-pointer.ts +42 -13
  59. package/std/json-tokenizer.ts +498 -307
  60. package/std/json-typed.ts +115 -36
  61. package/std/json-value.ts +341 -152
  62. package/std/json.ts +300 -158
  63. package/std/net/buffer-pool.ts +17 -7
  64. package/std/net/dns-config.ts +427 -236
  65. package/std/net/dns-message.ts +114 -47
  66. package/std/net/dns-resolver.ts +736 -237
  67. package/std/net/dns.ts +205 -52
  68. package/std/net/fetch.ts +320 -184
  69. package/std/net/grpc.ts +948 -0
  70. package/std/net/handler.ts +1276 -218
  71. package/std/net/hpack.ts +395 -180
  72. package/std/net/http-compress.ts +502 -65
  73. package/std/net/http-fields.ts +85 -49
  74. package/std/net/http-server.ts +1764 -603
  75. package/std/net/http1-client-codec.ts +372 -161
  76. package/std/net/http1-codec.ts +665 -331
  77. package/std/net/http1-server.ts +881 -269
  78. package/std/net/http2-cleartext.ts +165 -95
  79. package/std/net/http2-frame.ts +232 -89
  80. package/std/net/http2-tls.ts +42 -12
  81. package/std/net/http2.ts +327 -101
  82. package/std/net/http3-frame.ts +68 -33
  83. package/std/net/http3-server.ts +672 -206
  84. package/std/net/http3.ts +669 -191
  85. package/std/net/oidc.ts +63 -24
  86. package/std/net/outbound.ts +207 -142
  87. package/std/net/postgres-async.ts +1223 -470
  88. package/std/net/postgres-carrier.ts +575 -372
  89. package/std/net/postgres.ts +1521 -632
  90. package/std/net/prefork.ts +254 -98
  91. package/std/net/protobuf.ts +1017 -0
  92. package/std/net/qpack.ts +143 -92
  93. package/std/net/quic-conn-ack.ts +104 -22
  94. package/std/net/quic-conn-cid.ts +66 -26
  95. package/std/net/quic-conn-params.ts +13 -7
  96. package/std/net/quic-datagram.ts +90 -27
  97. package/std/net/quic-frame.ts +324 -121
  98. package/std/net/quic-listener.ts +206 -91
  99. package/std/net/quic-packet.ts +289 -163
  100. package/std/net/quic-recovery.ts +279 -128
  101. package/std/net/quic-stream.ts +466 -167
  102. package/std/net/quic.ts +584 -258
  103. package/std/net/router.ts +450 -99
  104. package/std/net/serve.ts +661 -240
  105. package/std/net/static.ts +1458 -0
  106. package/std/net/tls/client.ts +140 -45
  107. package/std/net/tls/codec.ts +510 -260
  108. package/std/net/tls/record-client.ts +268 -76
  109. package/std/net/tls/record-server.ts +236 -79
  110. package/std/net/tls/record.ts +173 -47
  111. package/std/net/tls/schedule.ts +58 -39
  112. package/std/net/tls/ticket.ts +295 -103
  113. package/std/net/tls/verify.ts +1 -1
  114. package/std/net/tls-tcp.ts +218 -75
  115. package/std/net/tls.ts +260 -152
  116. package/std/net/upstream.ts +283 -86
  117. package/std/net/url.ts +277 -102
  118. package/std/net/websocket.ts +532 -209
  119. package/std/net/webtransport.ts +755 -436
  120. package/std/regex.ts +1659 -829
  121. package/std/sqlite.ts +325 -0
  122. package/std/sync/ring.ts +320 -156
  123. package/std/sync/shm.ts +53 -6
  124. package/std/text.ts +285 -135
  125. package/std/threads.ts +22 -2
package/std/bytes.ts CHANGED
@@ -522,10 +522,31 @@ export const skipJsonPlain = (b: u8[], from: i32, to: i32): i32 => {
522
522
  * @param count How many bytes to copy; zero copies nothing.
523
523
  */
524
524
  export const copyBytes = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
525
- if (count < 0 || at < 0 || from < 0 || at > toI32(dst.length) - count || from > toI32(src.length) - count) {
525
+ if (!tryCopyBytes(dst, at, src, from, count)) {
526
526
  panic("copyBytes: the range lies outside an array")
527
527
  }
528
+ }
529
+
530
+ /**
531
+ * `copyBytes` that answers `false`, and writes nothing, where `copyBytes`
532
+ * panics: for a negative `count` or a range that does not lie inside its
533
+ * array. A caller a strict module reaches uses this one, so the out-of-range
534
+ * case is a value it handles rather than a panic site.
535
+ *
536
+ * @param dst The array written into; only `dst[at..at+count)` changes.
537
+ * @param at Where in `dst` the first copied byte goes.
538
+ * @param src The array copied from, which may be `dst` itself.
539
+ * @param from Where in `src` the copied bytes start.
540
+ * @param count How many bytes to copy; zero copies nothing.
541
+ * @returns `true` once the bytes are copied, or `false` when either range
542
+ * lies outside its array and nothing was written.
543
+ */
544
+ export const tryCopyBytes = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): boolean => {
545
+ if (count < 0 || at < 0 || from < 0 || at > toI32(dst.length) - count || from > toI32(src.length) - count) {
546
+ return false
547
+ }
528
548
  copyBytesWalk(dst, at, src, from, count)
549
+ return true
529
550
  }
530
551
 
531
552
  /**
@@ -562,11 +583,12 @@ const skipDigitsWalk = (b: u8[], from: i32, to: i32): i32 => {
562
583
  }
563
584
 
564
585
  /**
565
- * The length of the well-formed UTF-8 sequence whose lead byte is `b[i]` and
566
- * that ends at or before `to`, or 0: RFC 3629's table, with the narrowed
567
- * second byte after `E0`, `ED`, `F0` and `F4`.
586
+ * Where the well-formed UTF-8 sequence whose lead byte is `b[i]` ends, when it
587
+ * ends at or before `to`, or `i` when there is none: RFC 3629's table, with the
588
+ * narrowed second byte after `E0`, `ED`, `F0` and `F4`. The end, not the
589
+ * length, so the caller steps to it without a sum.
568
590
  */
569
- const bytesUtf8Length = (b: u8[], i: i32, to: i32): i32 => {
591
+ const bytesUtf8End = (b: u8[], i: i32, to: i32): i32 => {
570
592
  const lead: i32 = toI32(b[i])
571
593
  let low: i32 = BYTES_NON_ASCII
572
594
  let high: i32 = BYTES_CONTINUATION_TOP
@@ -590,24 +612,29 @@ const bytesUtf8Length = (b: u8[], i: i32, to: i32): i32 => {
590
612
  high = 0x8f
591
613
  }
592
614
  } else {
593
- return 0
594
- }
595
- if (to - i < n) {
596
- return 0
615
+ return i
597
616
  }
598
- const second: i32 = toI32(b[i + 1])
599
- if (second < low || second > high) {
600
- return 0
617
+ // Each continuation byte is read only once it is known to lie before both
618
+ // `to` and the array's end; a sequence cut short by either is not one.
619
+ if (i < 0 || i >= toI32(b.length)) {
620
+ return i
601
621
  }
602
- let k: i32 = 2
622
+ let j: i32 = i + 1
623
+ let k: i32 = 1
603
624
  while (k < n) {
604
- const c: i32 = toI32(b[i + k])
605
- if (c < BYTES_NON_ASCII || c > BYTES_CONTINUATION_TOP) {
606
- return 0
625
+ if (j >= to || j >= toI32(b.length)) {
626
+ return i
607
627
  }
628
+ const c: i32 = toI32(b[j])
629
+ const lowest: i32 = k === 1 ? low : BYTES_NON_ASCII
630
+ const highest: i32 = k === 1 ? high : BYTES_CONTINUATION_TOP
631
+ if (c < lowest || c > highest) {
632
+ return i
633
+ }
634
+ j += 1
608
635
  k += 1
609
636
  }
610
- return n
637
+ return j
611
638
  }
612
639
 
613
640
  /** `skipJsonText`'s walk, once its range is clamped. */
@@ -616,11 +643,11 @@ const skipJsonTextWalk = (b: u8[], from: i32, to: i32): i32 => {
616
643
  while (i >= 0 && i < to && i < toI32(b.length)) {
617
644
  const c: i32 = toI32(b[i])
618
645
  if (c >= BYTES_NON_ASCII) {
619
- const n: i32 = bytesUtf8Length(b, i, to)
620
- if (n === 0) {
646
+ const end: i32 = bytesUtf8End(b, i, to)
647
+ if (end === i) {
621
648
  return i
622
649
  }
623
- i += n
650
+ i = end
624
651
  continue
625
652
  }
626
653
  if (c < BYTES_SPACE || c === BYTES_QUOTE || c === BYTES_BACKSLASH) {
@@ -644,19 +671,205 @@ const skipJsonPlainWalk = (b: u8[], from: i32, to: i32): i32 => {
644
671
  return i
645
672
  }
646
673
 
647
- /** `copyBytes`'s walk, once its ranges are checked: forward, or backward when `dst` is `src` and the bytes move up. */
674
+ /**
675
+ * `copyBytes`'s walk, once its ranges are checked: forward, or backward when
676
+ * `dst` is `src` and the bytes move up. Each index is tested against its array
677
+ * as the walk goes, which the checked ranges always pass; natively the walk is
678
+ * one `llvm.memmove` instead.
679
+ */
648
680
  const copyBytesWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
681
+ const dstLength: i32 = toI32(dst.length)
682
+ const srcLength: i32 = toI32(src.length)
649
683
  if (at <= from) {
684
+ let d: i32 = at
685
+ let s: i32 = from
650
686
  let k: i32 = 0
651
- while (k < count) {
652
- dst[at + k] = src[from + k]
687
+ while (k < count && d >= 0 && d < dstLength && s >= 0 && s < srcLength) {
688
+ dst[d] = src[s]
689
+ d += 1
690
+ s += 1
653
691
  k += 1
654
692
  }
655
693
  return
656
694
  }
657
- let k: i32 = count - 1
658
- while (k >= 0) {
659
- dst[at + k] = src[from + k]
695
+ // The last byte of each range, in i64 so the sum cannot overflow; the
696
+ // checked ranges make both fit an i32.
697
+ let d: i32 = toI32(toI64(at) + toI64(count) - toI64(1))
698
+ let s: i32 = toI32(toI64(from) + toI64(count) - toI64(1))
699
+ let k: i32 = count
700
+ while (k > 0 && d >= 0 && d < dstLength && s >= 0 && s < srcLength) {
701
+ dst[d] = src[s]
702
+ d -= 1
703
+ s -= 1
660
704
  k -= 1
661
705
  }
662
706
  }
707
+
708
+ /**
709
+ * `getU32LE` that answers an offset whose four bytes do not all lie in `b`
710
+ * as `Err`, where `getU32LE` panics `index`: the form a strict module reads a
711
+ * record it did not size itself with.
712
+ *
713
+ * @param b The bytes the number is read from; they are not changed.
714
+ * @param off Where in `b` its first byte is; it covers `b[off]` to
715
+ * `b[off + 3]`.
716
+ * @returns The number read, or `Err` naming the offset and the array's length
717
+ * when a byte of the four lies outside `b`.
718
+ */
719
+ export const tryGetU32LE = (b: u8[], off: i32): Result<u32, string> => {
720
+ if (off >= 0 && off < 2147483644 && off + 4 <= toI32(b.length)) {
721
+ // The last byte is read first: once it is in range the checker proves
722
+ // the three below it, where a first read of `b[off]` leaves them unproven.
723
+ const b3: u32 = toU32(b[off + 3])
724
+ const b2: u32 = toU32(b[off + 2])
725
+ const b1: u32 = toU32(b[off + 1])
726
+ const b0: u32 = toU32(b[off])
727
+ return Ok((b0 | (b1 << 8) | (b2 << 16)) + b3 * toU32(0x1000000))
728
+ }
729
+ return Err(bytesRangeError(off, 4, b))
730
+ }
731
+
732
+ /**
733
+ * `getU64LE` that answers an offset whose eight bytes do not all lie in `b`
734
+ * as `Err`, where `getU64LE` panics.
735
+ *
736
+ * @param b The bytes the number is read from; they are not changed.
737
+ * @param off Where in `b` its first byte is; it covers `b[off]` to
738
+ * `b[off + 7]`.
739
+ * @returns The number read, or `Err` naming the offset and the array's length
740
+ * when a byte of the eight lies outside `b`.
741
+ */
742
+ export const tryGetU64LE = (b: u8[], off: i32): Result<u64, string> => {
743
+ if (off >= 0 && off < 2147483640 && off + 8 <= toI32(b.length)) {
744
+ // The last byte first, as in `tryGetU32LE`.
745
+ const b7: u64 = toU64(b[off + 7])
746
+ const b6: u64 = toU64(b[off + 6])
747
+ const b5: u64 = toU64(b[off + 5])
748
+ const b4: u64 = toU64(b[off + 4])
749
+ const b3: u64 = toU64(b[off + 3])
750
+ const b2: u64 = toU64(b[off + 2])
751
+ const b1: u64 = toU64(b[off + 1])
752
+ const b0: u64 = toU64(b[off])
753
+ return Ok(
754
+ b0 |
755
+ (b1 << toU64(8)) |
756
+ ((b2 << toU64(16)) | (b3 << toU64(24))) |
757
+ ((b4 << toU64(32)) | (b5 << toU64(40)) | ((b6 << toU64(48)) | (b7 << toU64(56))))
758
+ )
759
+ }
760
+ return Err(bytesRangeError(off, 8, b))
761
+ }
762
+
763
+ /**
764
+ * `getI32LE` that answers an offset whose four bytes do not all lie in `b`
765
+ * as `Err`, where `getI32LE` panics.
766
+ *
767
+ * @param b The bytes the number is read from; they are not changed.
768
+ * @param off Where in `b` its first byte is; it covers `b[off]` to
769
+ * `b[off + 3]`.
770
+ * @returns The number read, from -2^31 to 2^31 - 1, or `Err` naming the
771
+ * offset and the array's length when a byte of the four lies outside `b`.
772
+ */
773
+ export const tryGetI32LE = (b: u8[], off: i32): Result<i32, string> => {
774
+ const v: Result<u32, string> = tryGetU32LE(b, off)
775
+ if (v.isErr()) {
776
+ return Err(v.error)
777
+ }
778
+ return Ok(toI32(v.value))
779
+ }
780
+
781
+ /**
782
+ * `getI64LE` that answers an offset whose eight bytes do not all lie in `b`
783
+ * as `Err`, where `getI64LE` panics.
784
+ *
785
+ * @param b The bytes the number is read from; they are not changed.
786
+ * @param off Where in `b` its first byte is; it covers `b[off]` to
787
+ * `b[off + 7]`.
788
+ * @returns The number read, from -2^63 to 2^63 - 1, or `Err` naming the
789
+ * offset and the array's length when a byte of the eight lies outside `b`.
790
+ */
791
+ export const tryGetI64LE = (b: u8[], off: i32): Result<i64, string> => {
792
+ const v: Result<u64, string> = tryGetU64LE(b, off)
793
+ if (v.isErr()) {
794
+ return Err(v.error)
795
+ }
796
+ return Ok(toI64(v.value))
797
+ }
798
+
799
+ /**
800
+ * `setU32LE` that answers `false`, and writes nothing, where `setU32LE`
801
+ * panics: for an offset whose four bytes do not all lie in `b`.
802
+ *
803
+ * @param b The bytes written into; only the four at `off` change.
804
+ * @param off Where in `b` the first byte goes; it covers `b[off]` to
805
+ * `b[off + 3]`.
806
+ * @param v The number to store.
807
+ * @returns `true` once the bytes are written, or `false` when a byte of the
808
+ * four lies outside `b` and nothing was written.
809
+ */
810
+ export const trySetU32LE = (b: u8[], off: i32, v: u32): boolean => {
811
+ if (off >= 0 && off < 2147483644 && off + 4 <= toI32(b.length)) {
812
+ b[off] = toU8(v & 255)
813
+ b[off + 1] = toU8((v >> 8) & 255)
814
+ b[off + 2] = toU8((v >> 16) & 255)
815
+ b[off + 3] = toU8((v >> 24) & 255)
816
+ return true
817
+ }
818
+ return false
819
+ }
820
+
821
+ /**
822
+ * `setU64LE` that answers `false`, and writes nothing, where `setU64LE`
823
+ * panics: for an offset whose eight bytes do not all lie in `b`.
824
+ *
825
+ * @param b The bytes written into; only the eight at `off` change.
826
+ * @param off Where in `b` the first byte goes; it covers `b[off]` to
827
+ * `b[off + 7]`.
828
+ * @param v The number to store.
829
+ * @returns `true` once the bytes are written, or `false` when a byte of the
830
+ * eight lies outside `b` and nothing was written.
831
+ */
832
+ export const trySetU64LE = (b: u8[], off: i32, v: u64): boolean => {
833
+ if (off >= 0 && off < 2147483640 && off + 8 <= toI32(b.length)) {
834
+ b[off] = toU8(v & toU64(255))
835
+ b[off + 1] = toU8((v >> toU64(8)) & toU64(255))
836
+ b[off + 2] = toU8((v >> toU64(16)) & toU64(255))
837
+ b[off + 3] = toU8((v >> toU64(24)) & toU64(255))
838
+ b[off + 4] = toU8((v >> toU64(32)) & toU64(255))
839
+ b[off + 5] = toU8((v >> toU64(40)) & toU64(255))
840
+ b[off + 6] = toU8((v >> toU64(48)) & toU64(255))
841
+ b[off + 7] = toU8((v >> toU64(56)) & toU64(255))
842
+ return true
843
+ }
844
+ return false
845
+ }
846
+
847
+ /**
848
+ * `setI32LE` that answers `false`, and writes nothing, where `setI32LE`
849
+ * panics.
850
+ *
851
+ * @param b The bytes written into; only the four at `off` change.
852
+ * @param off Where in `b` the first byte goes; it covers `b[off]` to
853
+ * `b[off + 3]`.
854
+ * @param v The number to store, as its two's-complement bits.
855
+ * @returns `true` once the bytes are written, or `false` when a byte of the
856
+ * four lies outside `b` and nothing was written.
857
+ */
858
+ export const trySetI32LE = (b: u8[], off: i32, v: i32): boolean => trySetU32LE(b, off, toU32(v))
859
+
860
+ /**
861
+ * `setI64LE` that answers `false`, and writes nothing, where `setI64LE`
862
+ * panics.
863
+ *
864
+ * @param b The bytes written into; only the eight at `off` change.
865
+ * @param off Where in `b` the first byte goes; it covers `b[off]` to
866
+ * `b[off + 7]`.
867
+ * @param v The number to store, as its two's-complement bits.
868
+ * @returns `true` once the bytes are written, or `false` when a byte of the
869
+ * eight lies outside `b` and nothing was written.
870
+ */
871
+ export const trySetI64LE = (b: u8[], off: i32, v: i64): boolean => trySetU64LE(b, off, toU64(v))
872
+
873
+ /** The `Err` text of a checked read whose `width` bytes at `off` do not all lie in `b`. */
874
+ const bytesRangeError = (off: i32, width: i32, b: u8[]): string =>
875
+ `bytes ${off} to ${toI64(off) + toI64(width) - toI64(1)} lie outside an array of ${b.length}`