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/sqlite.ts CHANGED
@@ -44,6 +44,26 @@
44
44
  * message; every other call answers `Err` with the bare extended code, and
45
45
  * `Database.error` or `Statement.error` turns it into a `SqliteError`.
46
46
  *
47
+ * **A cursor reads a table or index without SQL.** Inside a read transaction
48
+ * (`beginRead` to `endRead`), `openCursor` checks the object's columns against
49
+ * the list the program names, and the `Cursor` then walks SQLite's B-tree
50
+ * directly: `first`, `next`, `seekRowid`, and for an index a seek key built
51
+ * with `keyI64` and `keyText` and moved to with `seek`. Its column reads
52
+ * answer what the row stores and, like a statement's, allocate nothing. Every
53
+ * `exec`, `commit`, `rollback` and `endRead`, and every stepped statement but
54
+ * a plain read (a `pragma_*` SELECT included), closes every cursor first, so
55
+ * a cursor never reads a page that changed; its calls answer `SQLITE_MISUSE`.
56
+ *
57
+ * db.beginRead().expect("begin")
58
+ * const ix = db.openCursor("t_name", "name,rowid").expect("cursor")
59
+ * ix.keyText("ada").expect("key")
60
+ * let on = ix.seek(SEEK_GE).expect("seek")
61
+ * while (on) {
62
+ * // ix.columnInto(0, buf, 0) is the name, ix.rowid() the row
63
+ * on = ix.next().expect("next")
64
+ * }
65
+ * db.endRead().expect("end") // closes `ix`
66
+ *
47
67
  * **Closing is explicit.** `close` finalizes every statement the connection
48
68
  * still has, so a statement never outlives its database; `finalize` frees one
49
69
  * statement early. A connection and its statements belong to the thread that
@@ -92,6 +112,24 @@ declare function nishsqlite_column_f64(h: i32, c: i32): f64
92
112
  declare function nishsqlite_column_bytes(h: i32, c: i32): i32
93
113
  declare function nishsqlite_column_text(h: i32, c: i32): string | null
94
114
  declare function nishsqlite_column_into(h: i32, c: i32, buf: u8[], n: u64, at: i32): i32
115
+ declare function nishsqlite_begin_read(h: i32): i32
116
+ declare function nishsqlite_end_read(h: i32): i32
117
+ declare function nishsqlite_cursor_open(h: i32, name: string, columns: string): i32
118
+ declare function nishsqlite_cursor_close(h: i32): i32
119
+ declare function nishsqlite_cursor_first(h: i32): i32
120
+ declare function nishsqlite_cursor_last(h: i32): i32
121
+ declare function nishsqlite_cursor_next(h: i32): i32
122
+ declare function nishsqlite_cursor_previous(h: i32): i32
123
+ declare function nishsqlite_cursor_seek_rowid(h: i32, rowid: i64): i32
124
+ declare function nishsqlite_key_reset(h: i32): i32
125
+ declare function nishsqlite_key_i64(h: i32, v: i64): i32
126
+ declare function nishsqlite_key_text(h: i32, text: string): i32
127
+ declare function nishsqlite_key_bytes(h: i32, buf: readonly u8[], n: u64, off: i32, len: i32): i32
128
+ declare function nishsqlite_cursor_seek(h: i32, op: i32): i32
129
+ declare function nishsqlite_cursor_column_type(h: i32, c: i32): i32
130
+ declare function nishsqlite_cursor_column_i64(h: i32, c: i32): i64
131
+ declare function nishsqlite_cursor_column_into(h: i32, c: i32, buf: u8[], n: u64, at: i32): i32
132
+ declare function nishsqlite_cursor_rowid(h: i32): i64
95
133
  declare function sqlite3_errstr(code: i32): string
96
134
  declare function sqlite3_libversion(): string
97
135
 
@@ -164,6 +202,15 @@ export const OPEN_MEMORY: i32 = 0x80
164
202
  /** Refuse a path whose last component is a symbolic link. */
165
203
  export const OPEN_NOFOLLOW: i32 = 0x01000000
166
204
 
205
+ /** `Cursor.seek`: the first index entry at or after the key. */
206
+ export const SEEK_GE: i32 = 1
207
+ /** `Cursor.seek`: the first index entry after the key. */
208
+ export const SEEK_GT: i32 = 2
209
+ /** `Cursor.seek`: the last index entry at or before the key. */
210
+ export const SEEK_LE: i32 = 3
211
+ /** `Cursor.seek`: the last index entry before the key. */
212
+ export const SEEK_LT: i32 = 4
213
+
167
214
  /**
168
215
  * The version of the SQLite library linked into this program.
169
216
  *
@@ -216,6 +263,14 @@ const messageFor = (code: i32, connection: string): string => {
216
263
  return connection
217
264
  }
218
265
 
266
+ /** A cursor move's answer: 1 on a row, 0 for none, the negated code. */
267
+ const moved = (r: i32): Result<boolean, i32> => {
268
+ if (r < 0) {
269
+ return Err(-r)
270
+ }
271
+ return Ok(r === 1)
272
+ }
273
+
219
274
  /**
220
275
  * Opens the database at `path`. `flags` holds one of `OPEN_READONLY`,
221
276
  * `OPEN_READWRITE` or `OPEN_READWRITE | OPEN_CREATE`, with any of `OPEN_URI`,
@@ -293,6 +348,59 @@ export class Database {
293
348
  return this.exec("ROLLBACK")
294
349
  }
295
350
 
351
+ /**
352
+ * Starts a read transaction: `BEGIN`, then a read of the schema, which takes
353
+ * the main database's read lock. Its snapshot is what every cursor of this
354
+ * connection reads until `endRead`, and `openCursor` needs one.
355
+ *
356
+ * @returns `Ok()`, or `Err` with `SQLITE_MISUSE` inside a transaction
357
+ * already, or the extended result code, such as `SQLITE_BUSY`.
358
+ */
359
+ beginRead(): Result<void, i32> {
360
+ return done(nishsqlite_begin_read(this.handle))
361
+ }
362
+
363
+ /**
364
+ * Closes every cursor of this connection and commits the transaction.
365
+ *
366
+ * @returns `Ok()`, or `Err` with `SQLITE_MISUSE` outside a transaction, or
367
+ * the extended result code of the `COMMIT`.
368
+ */
369
+ endRead(): Result<void, i32> {
370
+ return done(nishsqlite_end_read(this.handle))
371
+ }
372
+
373
+ /**
374
+ * Opens a cursor on `name`, a table or index of the main database, inside a
375
+ * read transaction (`beginRead`). `columns` is the schema the program reads,
376
+ * checked at open: a table's columns, or an index's key columns, each with
377
+ * ` DESC` where the index has it, and then `rowid`, in order, joined by `,`
378
+ * with no space. A table that is `WITHOUT ROWID`, a view or virtual table,
379
+ * one with a generated column, an index on an expression or with a
380
+ * collation other than `BINARY`, and a database that is not UTF-8 are
381
+ * refused. `close`, `endRead`, closing the connection, any `exec`, and any
382
+ * stepped statement but a plain read (`pragma_*` SELECTs too) close it.
383
+ *
384
+ * @param name The table or index, as the schema names it.
385
+ * @param columns The columns, such as `"id,name,score"` for a table or
386
+ * `"name,score DESC,rowid"` for an index.
387
+ * @returns The cursor, or a `SqliteError`: `SQLITE_MISUSE` outside a read
388
+ * transaction, `SQLITE_SCHEMA` for an object that is not there, is not one
389
+ * a cursor reads, or has columns other than `columns`.
390
+ */
391
+ openCursor(name: string, columns: string): Result<Cursor, SqliteError> {
392
+ const h: i32 = nishsqlite_cursor_open(this.handle, name, columns)
393
+ if (h < 0) {
394
+ if (-h === SQLITE_SCHEMA) {
395
+ return Err(
396
+ new SqliteError(-h, `no table or index ${name} with columns "${columns}" that a cursor reads`)
397
+ )
398
+ }
399
+ return Err(this.error(-h))
400
+ }
401
+ return Ok(new Cursor(h))
402
+ }
403
+
296
404
  /**
297
405
  * Compiles the first statement of `sql`, to be bound, stepped and reset as
298
406
  * often as wanted; text after it is ignored. Text with no statement in it is
@@ -610,3 +718,220 @@ export class Statement {
610
718
  return new SqliteError(code, messageFor(code, nishsqlite_stmt_errmsg(this.handle)))
611
719
  }
612
720
  }
721
+
722
+ /**
723
+ * A B-tree cursor on a table or index, which reads rows without a statement.
724
+ * Columns count from 0 in the order `openCursor` named them. A call on a
725
+ * closed or invalidated cursor answers `SQLITE_MISUSE` (0 from `rowid` and
726
+ * `columnI64`).
727
+ */
728
+ export class Cursor {
729
+ /** The adapter's handle. */
730
+ private handle: i32
731
+
732
+ /**
733
+ * Wraps an adapter handle. A program gets a `Cursor` from
734
+ * `Database.openCursor` rather than building one.
735
+ *
736
+ * @param handle The adapter's cursor handle.
737
+ */
738
+ constructor(handle: i32) {
739
+ this.handle = handle
740
+ }
741
+
742
+ /**
743
+ * Moves to the first row, in the index's order for an index.
744
+ *
745
+ * @returns `true` on a row, `false` for an empty table or index, or `Err`
746
+ * with the code: `SQLITE_MISUSE` for a closed cursor, or SQLite's, such as
747
+ * `SQLITE_CORRUPT`.
748
+ */
749
+ first(): Result<boolean, i32> {
750
+ return moved(nishsqlite_cursor_first(this.handle))
751
+ }
752
+
753
+ /**
754
+ * Moves to the last row.
755
+ *
756
+ * @returns `true` on a row, `false` for an empty table or index, or `Err`
757
+ * with the code, as for `first`.
758
+ */
759
+ last(): Result<boolean, i32> {
760
+ return moved(nishsqlite_cursor_last(this.handle))
761
+ }
762
+
763
+ /**
764
+ * Moves to the next row.
765
+ *
766
+ * @returns `true` on a row, `false` past the last, or `Err` with the code,
767
+ * as for `first`, or `SQLITE_MISUSE` when the cursor has no row to move
768
+ * from.
769
+ */
770
+ next(): Result<boolean, i32> {
771
+ return moved(nishsqlite_cursor_next(this.handle))
772
+ }
773
+
774
+ /**
775
+ * Moves to the row before.
776
+ *
777
+ * @returns `true` on a row, `false` before the first, or `Err` with the
778
+ * code, as for `next`.
779
+ */
780
+ previous(): Result<boolean, i32> {
781
+ return moved(nishsqlite_cursor_previous(this.handle))
782
+ }
783
+
784
+ /**
785
+ * Moves a table cursor to the row whose rowid is `rowid`.
786
+ *
787
+ * @param rowid The row's rowid.
788
+ * @returns `true` when the row is there, `false` when not, or `Err` with
789
+ * the code, as for `first`, or `SQLITE_MISUSE` for an index cursor.
790
+ */
791
+ seekRowid(rowid: i64): Result<boolean, i32> {
792
+ return moved(nishsqlite_cursor_seek_rowid(this.handle, rowid))
793
+ }
794
+
795
+ /**
796
+ * Empties an index cursor's seek key, to build a new one with `keyI64`,
797
+ * `keyText` and `keyTextBytes`.
798
+ *
799
+ * @returns `Ok()`, or `Err` with `SQLITE_MISUSE` for a table cursor or a
800
+ * closed one.
801
+ */
802
+ keyReset(): Result<void, i32> {
803
+ return done(nishsqlite_key_reset(this.handle))
804
+ }
805
+
806
+ /**
807
+ * Appends an integer to the seek key, for the index's next key column.
808
+ *
809
+ * @param v The value.
810
+ * @returns `Ok()`, or `Err` with `SQLITE_RANGE` when the key has a value
811
+ * for every key column already, or `SQLITE_MISUSE` for a table cursor.
812
+ */
813
+ keyI64(v: i64): Result<void, i32> {
814
+ return done(nishsqlite_key_i64(this.handle, v))
815
+ }
816
+
817
+ /**
818
+ * Appends text to the seek key, copied; the text ends at its first NUL.
819
+ *
820
+ * @param text The value.
821
+ * @returns `Ok()`, or `Err` with the code, as for `keyI64`, or
822
+ * `SQLITE_NOMEM`.
823
+ */
824
+ keyText(text: string): Result<void, i32> {
825
+ return done(nishsqlite_key_text(this.handle, text))
826
+ }
827
+
828
+ /**
829
+ * Appends `buf[off, off + len)` to the seek key as text, copied; a window
830
+ * outside `buf` is `SQLITE_MISUSE`.
831
+ *
832
+ * @param buf The bytes to read from.
833
+ * @param off Where the text starts in `buf`, in bytes.
834
+ * @param len The text's length in bytes.
835
+ * @returns `Ok()`, or `Err` with the code, as for `keyText`.
836
+ */
837
+ keyTextBytes(buf: readonly u8[], off: i32, len: i32): Result<void, i32> {
838
+ return done(nishsqlite_key_bytes(this.handle, buf, toU64(buf.length), off, len))
839
+ }
840
+
841
+ /**
842
+ * Moves an index cursor by its seek key, compared with each entry's first
843
+ * key columns, so a key with fewer values than the index is a prefix:
844
+ * `SEEK_GE` and `SEEK_GT` land on the first entry at or after the key,
845
+ * `SEEK_LE` and `SEEK_LT` on the last at or before it, the `E` forms
846
+ * counting an equal entry. The key stays, for the next seek.
847
+ *
848
+ * @param op One of `SEEK_GE`, `SEEK_GT`, `SEEK_LE` and `SEEK_LT`.
849
+ * @returns `true` on an entry, `false` when there is none on that side, or
850
+ * `Err` with the code, as for `first`, or `SQLITE_MISUSE` for a table
851
+ * cursor, an empty key or another `op`.
852
+ */
853
+ seek(op: i32): Result<boolean, i32> {
854
+ return moved(nishsqlite_cursor_seek(this.handle, op))
855
+ }
856
+
857
+ /**
858
+ * The current row's rowid: a table row's key, an index entry's `rowid`.
859
+ *
860
+ * @returns The rowid, or 0 when the cursor has no row.
861
+ */
862
+ rowid(): i64 {
863
+ return nishsqlite_cursor_rowid(this.handle)
864
+ }
865
+
866
+ /**
867
+ * The type the current row stores for column `c`, as written: a REAL
868
+ * column holding a whole number stores an integer, where a query would
869
+ * answer a float.
870
+ *
871
+ * @param c The column's index, from 0, in `openCursor`'s order.
872
+ * @returns One of `SQLITE_INTEGER` to `SQLITE_NULL`, or `Err` with
873
+ * `SQLITE_MISUSE` when the cursor has no row, `SQLITE_RANGE` for a column
874
+ * that is not there, `SQLITE_SCHEMA` for a row written before `ALTER TABLE`
875
+ * added the column, or `SQLITE_ABORT` when the cursor's row has moved.
876
+ */
877
+ columnType(c: i32): Result<i32, i32> {
878
+ const t: i32 = nishsqlite_cursor_column_type(this.handle, c)
879
+ if (t < 0) {
880
+ return Err(-t)
881
+ }
882
+ return Ok(t)
883
+ }
884
+
885
+ /**
886
+ * Column `c` of the current row as an integer, converted as SQLite
887
+ * converts.
888
+ *
889
+ * @param c The column's index, from 0.
890
+ * @returns The value; 0 for NULL and for a column `columnType` refuses.
891
+ */
892
+ columnI64(c: i32): i64 {
893
+ return nishsqlite_cursor_column_i64(this.handle, c)
894
+ }
895
+
896
+ /**
897
+ * Copies a text or blob column's stored bytes into `buf` at `at`, and
898
+ * answers their length, as `Statement.columnInto` does: it copies only when
899
+ * all of them fit, so a length larger than `buf.length - at` means nothing
900
+ * was written. It converts no number to text.
901
+ *
902
+ * @param c The column's index, from 0.
903
+ * @param buf The caller's buffer, written in place.
904
+ * @param at Where in `buf` the copy starts, in bytes.
905
+ * @returns The length in bytes, 0 for NULL, or `Err` with the code, as for
906
+ * `columnType`, or `SQLITE_MISMATCH` for an integer or float, or
907
+ * `SQLITE_MISUSE` for an `at` outside `0..buf.length`.
908
+ */
909
+ columnInto(c: i32, buf: u8[], at: i32): Result<i32, i32> {
910
+ const n: i32 = nishsqlite_cursor_column_into(this.handle, c, buf, toU64(buf.length), at)
911
+ if (n < 0) {
912
+ return Err(-n)
913
+ }
914
+ return Ok(n)
915
+ }
916
+
917
+ /**
918
+ * Closes the cursor; the handle names nothing after.
919
+ *
920
+ * @returns `Ok()`, or `Err` with `SQLITE_MISUSE` for a cursor already
921
+ * closed or invalidated.
922
+ */
923
+ close(): Result<void, i32> {
924
+ return done(nishsqlite_cursor_close(this.handle))
925
+ }
926
+
927
+ /**
928
+ * The error for `code`, which this cursor's last call answered.
929
+ * Allocates the `SqliteError`.
930
+ *
931
+ * @param code The extended result code an `Err` carried.
932
+ * @returns The error, with SQLite's description of the code.
933
+ */
934
+ error(code: i32): SqliteError {
935
+ return new SqliteError(code, sqlite3_errstr(code))
936
+ }
937
+ }