nish 0.44.0 → 0.46.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 (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
package/std/sqlite.ts CHANGED
@@ -719,6 +719,11 @@ export class Statement {
719
719
  }
720
720
  }
721
721
 
722
+ // Cursor.countRange's two calls: the count, then each count read back.
723
+ declare function nishsqlite_cursor_count_range(h: i32, lo: i64, hi: i64, n: u64): i32
724
+ declare function nishsqlite_cursor_count_at(h: i32, i: u64): i64
725
+ declare function nishsqlite_cursor_read_row(h: i32, io: u8[], n: u64, text: u8[], ntext: u64): i32
726
+
722
727
  /**
723
728
  * A B-tree cursor on a table or index, which reads rows without a statement.
724
729
  * Columns count from 0 in the order `openCursor` named them. A call on a
@@ -726,8 +731,12 @@ export class Statement {
726
731
  * `columnI64`).
727
732
  */
728
733
  export class Cursor {
734
+ /** `readRow`'s answers from the adapter, sized by `selectRow`. */
735
+ private rowIo: u8[]
729
736
  /** The adapter's handle. */
730
737
  private handle: i32
738
+ /** The columns `selectRow` chose, -1 before it. */
739
+ private nsel: i32
731
740
 
732
741
  /**
733
742
  * Wraps an adapter handle. A program gets a `Cursor` from
@@ -737,6 +746,8 @@ export class Cursor {
737
746
  */
738
747
  constructor(handle: i32) {
739
748
  this.handle = handle
749
+ this.rowIo = []
750
+ this.nsel = -1
740
751
  }
741
752
 
742
753
  /**
@@ -854,6 +865,46 @@ export class Cursor {
854
865
  return moved(nishsqlite_cursor_seek(this.handle, op))
855
866
  }
856
867
 
868
+ /**
869
+ * Counts an index's entries whose first key column is `k`, for each `k`
870
+ * from `lo` to `hi`, into `out[k - lo]`, in one seek and one walk of the
871
+ * index, for an index whose first column has INTEGER or NUMERIC affinity.
872
+ * On a full index that is what `SELECT count(*) ... WHERE col = k` answers
873
+ * for each `k`; a partial index holds only the rows its WHERE admits, and
874
+ * counts only those. A count that reaches its walk clears the seek key and
875
+ * leaves the cursor on no row, whatever it answers; one refused first leaves
876
+ * both as they were. Copying the counts into `out` takes one call per key,
877
+ * and one more when `out` is longer than the span. `out` past the span, and
878
+ * all of it on an `Err`, is left as it was.
879
+ *
880
+ * @param lo The first key counted.
881
+ * @param hi The last key counted, at least `lo`.
882
+ * @param out Where the counts go; at least `hi - lo + 1` long.
883
+ * @returns The number of entries counted, or `Err` with `SQLITE_MISUSE` for
884
+ * a table cursor, `SQLITE_MISMATCH` for an index whose first column has
885
+ * another affinity, `SQLITE_RANGE` when `lo > hi` or the span is longer
886
+ * than `out`, or the code SQLite fails with, as for `first`.
887
+ */
888
+ countRange(lo: i64, hi: i64, out: i64[]): Result<i32, i32> {
889
+ const n: i32 = nishsqlite_cursor_count_range(this.handle, lo, hi, toU64(out.length))
890
+ if (n < 0) {
891
+ // The adapter answers minus a SQLite result code: `~n + 1` is `-n`,
892
+ // spelled in u32 so no negation of an i32 can overflow.
893
+ return Err(toI32(toU32(~n) + toU32(1)))
894
+ }
895
+ for (let i = 0; i < out.length; i++) {
896
+ const c: i64 = nishsqlite_cursor_count_at(this.handle, toU64(i))
897
+ if (c < 0) {
898
+ break
899
+ }
900
+ // The call above ends what the loop proved of `i`.
901
+ if (i < out.length) {
902
+ out[i] = c
903
+ }
904
+ }
905
+ return Ok(n)
906
+ }
907
+
857
908
  /**
858
909
  * The current row's rowid: a table row's key, an index entry's `rowid`.
859
910
  *
@@ -914,6 +965,126 @@ export class Cursor {
914
965
  return Ok(n)
915
966
  }
916
967
 
968
+ /**
969
+ * Chooses the columns `readRow` reads, in order; a column may repeat. The
970
+ * list holds until the next `selectRow`, so a loop chooses it once and
971
+ * calls `readRow` per row. It allocates one buffer of 11 bytes per column
972
+ * and checks no index: `readRow` answers `SQLITE_RANGE` for one outside the
973
+ * cursor's columns.
974
+ *
975
+ * @param cols The columns' indices, from 0.
976
+ * @returns `Ok()`, or `Err` with `SQLITE_MISUSE` for over 65535 columns,
977
+ * which leaves the earlier list chosen.
978
+ */
979
+ selectRow(cols: readonly i32[]): Result<void, i32> {
980
+ const k: i32 = toI32(cols.length)
981
+ if (k > 0xffff) {
982
+ return Err(SQLITE_MISUSE)
983
+ }
984
+ // Each index goes to the adapter as a little-endian u16; one outside
985
+ // 0..65535 is sent as 65535, which no cursor has, so it is RANGE there.
986
+ // A slot of 9 bytes per column follows, which `readRow` fills.
987
+ const io: u8[] = []
988
+ for (let i: i32 = 0; i < k && i < toI32(cols.length); i++) {
989
+ const c: i32 = cols[i] < 0 || cols[i] > 0xffff ? 0xffff : cols[i]
990
+ io.push(toU8(c & 0xff))
991
+ io.push(toU8(c >> 8))
992
+ }
993
+ for (let i: i32 = 0; i < k; i++) {
994
+ for (let b: i32 = 0; b < 9; b++) {
995
+ io.push(0)
996
+ }
997
+ }
998
+ this.rowIo = io
999
+ this.nsel = k
1000
+ return Ok()
1001
+ }
1002
+
1003
+ /**
1004
+ * Reads the current row's `selectRow` columns in one call. For the `k`th
1005
+ * of them it writes `spans[3k]`, `spans[3k + 1]` and `spans[3k + 2]`: the
1006
+ * type `columnType` answers, then where its bytes start in `text` and how
1007
+ * many there are, which are the bytes `columnInto` copies (0 and 0 for a
1008
+ * number or NULL); and `ints[k]`, the value `columnI64` answers for an
1009
+ * INTEGER or FLOAT column and 0 for TEXT, BLOB and NULL, which it does not
1010
+ * convert. The row's text and blob bytes sit in `text` in column order. A
1011
+ * refusal, or an answer larger than `text.length`, writes no `ints` or
1012
+ * `spans`, and `text` may hold the first columns' bytes: grow `text` and
1013
+ * call again. An empty list answers `Ok(0)` on a row, and `SQLITE_MISUSE`
1014
+ * with no row or a closed cursor, as any list does. It allocates nothing.
1015
+ *
1016
+ * @param ints Receives each column's integer; at least as long as the list.
1017
+ * @param spans Receives each column's type, offset and length; at least
1018
+ * three times as long as the list.
1019
+ * @param text Receives the text and blob bytes.
1020
+ * @returns The text and blob bytes the row holds, or `Err` with the code
1021
+ * `columnType` answers for the first column it refuses (`SQLITE_RANGE`
1022
+ * for an index outside the cursor's columns, `SQLITE_SCHEMA` for a
1023
+ * record too short for it, `SQLITE_MISUSE` with no row or a closed
1024
+ * cursor), or `SQLITE_MISUSE` before `selectRow` or when `ints` or
1025
+ * `spans` is too short, or `SQLITE_TOOBIG` for over 2^31 - 1 bytes.
1026
+ */
1027
+ readRow(ints: i64[], spans: i32[], text: u8[]): Result<i32, i32> {
1028
+ const k: i32 = this.nsel
1029
+ const ni: i32 = toI32(ints.length)
1030
+ const ns: i32 = toI32(spans.length)
1031
+ if (k < 0 || ni < k || ns / 3 < k) {
1032
+ return Err(SQLITE_MISUSE)
1033
+ }
1034
+ const io: u8[] = this.rowIo
1035
+ const n: i32 = nishsqlite_cursor_read_row(this.handle, io, toU64(io.length), text, toU64(text.length))
1036
+ if (n < 0) {
1037
+ // The adapter answers minus a SQLite result code: `~n + 1` is `-n`,
1038
+ // spelled in u32 so no negation of an i32 can overflow.
1039
+ return Err(toI32(toU32(~n) + toU32(1)))
1040
+ }
1041
+ if (n > toI32(text.length)) {
1042
+ return Ok(n)
1043
+ }
1044
+ // The slots follow the 2k index bytes: a column's type, then 8
1045
+ // little-endian bytes, the integer, or a TEXT or BLOB column's length,
1046
+ // under 2^31 since the row's bytes fit `text`. Those bytes sit in `text`
1047
+ // one column after another, so each starts where the last one ended.
1048
+ const nio: i32 = toI32(io.length)
1049
+ let at: i32 = toI32(toI64(k) + toI64(k))
1050
+ let s: i32 = 0
1051
+ let off: i32 = 0
1052
+ for (
1053
+ let i: i32 = 0;
1054
+ i < k && i < toI32(ints.length) && s >= 0 && s <= toI32(spans.length) - 3 && at >= 0 && at <= nio - 9;
1055
+ i++
1056
+ ) {
1057
+ const ty: i32 = toI32(io[at])
1058
+ spans[s] = ty
1059
+ if (ty === SQLITE_TEXT || ty === SQLITE_BLOB) {
1060
+ const len: i32 =
1061
+ toI32(io[at + 1]) | (toI32(io[at + 2]) << 8) | (toI32(io[at + 3]) << 16) | (toI32(io[at + 4]) << 24)
1062
+ ints[i] = 0
1063
+ spans[s + 1] = off
1064
+ spans[s + 2] = len
1065
+ const end: i64 = toI64(off) + toI64(len)
1066
+ off = len >= 0 && end <= toI64(n) ? toI32(end) : n
1067
+ } else {
1068
+ ints[i] =
1069
+ ty === SQLITE_NULL
1070
+ ? 0
1071
+ : toI64(
1072
+ toU64(io[at + 1]) |
1073
+ (toU64(io[at + 2]) << toU64(8)) |
1074
+ ((toU64(io[at + 3]) << toU64(16)) | (toU64(io[at + 4]) << toU64(24))) |
1075
+ ((toU64(io[at + 5]) << toU64(32)) |
1076
+ (toU64(io[at + 6]) << toU64(40)) |
1077
+ ((toU64(io[at + 7]) << toU64(48)) | (toU64(io[at + 8]) << toU64(56))))
1078
+ )
1079
+ spans[s + 1] = 0
1080
+ spans[s + 2] = 0
1081
+ }
1082
+ s += 3
1083
+ at += 9
1084
+ }
1085
+ return Ok(n)
1086
+ }
1087
+
917
1088
  /**
918
1089
  * Closes the cursor; the handle names nothing after.
919
1090
  *