nish 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/README.md +27 -21
  2. package/docs/AI.md +237 -160
  3. package/docs/INSTALL.md +1 -1
  4. package/package.json +12 -12
  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
package/std/sqlite.ts CHANGED
@@ -787,6 +787,20 @@ declare function nishsqlite_cursor_count_at(h: i32, i: u64): i64
787
787
  declare function nishsqlite_cursor_read_row(h: i32, io: u8[], n: u64, text: u8[], ntext: u64): i32
788
788
  declare function nishsqlite_cursor_record(h: i32, buf: u8[], n: u64): i32
789
789
 
790
+ /**
791
+ * The SQLite result code the adapter answered as `n`, minus that code: `-n`,
792
+ * spelled `~n + 1`. The adapter only answers small codes; a stray
793
+ * `-2147483648`, whose negation is past an `i32`, answers `2147483647` rather
794
+ * than overflow, and the test with the literal is what proves the sum.
795
+ */
796
+ const sqliteNegated = (n: i32): i32 => {
797
+ const m: i32 = ~n
798
+ if (m < 0 || m > 2147483646) {
799
+ return 2147483647
800
+ }
801
+ return m + 1
802
+ }
803
+
790
804
  /**
791
805
  * A B-tree cursor on a table or index, which reads rows without a statement.
792
806
  * Columns count from 0 in the order `openCursor` named them. A call on a
@@ -963,9 +977,8 @@ export class Cursor {
963
977
  countRange(lo: i64, hi: i64, out: i64[]): Result<i32, i32> {
964
978
  const n: i32 = nishsqlite_cursor_count_range(this.handle, lo, hi, toU64(out.length))
965
979
  if (n < 0) {
966
- // The adapter answers minus a SQLite result code: `~n + 1` is `-n`,
967
- // spelled in u32 so no negation of an i32 can overflow.
968
- return Err(toI32(toU32(~n) + toU32(1)))
980
+ // The adapter answers minus a SQLite result code.
981
+ return Err(sqliteNegated(n))
969
982
  }
970
983
  for (let i = 0; i < out.length; i++) {
971
984
  const c: i64 = nishsqlite_cursor_count_at(this.handle, toU64(i))
@@ -1000,9 +1013,8 @@ export class Cursor {
1000
1013
  record(buf: u8[]): Result<i32, i32> {
1001
1014
  const n: i32 = nishsqlite_cursor_record(this.handle, buf, toU64(buf.length))
1002
1015
  if (n < 0) {
1003
- // The adapter answers minus a SQLite result code: `~n + 1` is `-n`,
1004
- // spelled in u32 so no negation of an i32 can overflow.
1005
- return Err(toI32(toU32(~n) + toU32(1)))
1016
+ // The adapter answers minus a SQLite result code.
1017
+ return Err(sqliteNegated(n))
1006
1018
  }
1007
1019
  return Ok(n)
1008
1020
  }
@@ -1136,9 +1148,8 @@ export class Cursor {
1136
1148
  const io: u8[] = this.rowIo
1137
1149
  const n: i32 = nishsqlite_cursor_read_row(this.handle, io, toU64(io.length), text, toU64(text.length))
1138
1150
  if (n < 0) {
1139
- // The adapter answers minus a SQLite result code: `~n + 1` is `-n`,
1140
- // spelled in u32 so no negation of an i32 can overflow.
1141
- return Err(toI32(toU32(~n) + toU32(1)))
1151
+ // The adapter answers minus a SQLite result code.
1152
+ return Err(sqliteNegated(n))
1142
1153
  }
1143
1154
  if (n > toI32(text.length)) {
1144
1155
  return Ok(n)
@@ -1341,21 +1352,27 @@ export class RowRecord {
1341
1352
  if (t < toI64(0)) {
1342
1353
  return false
1343
1354
  }
1344
- // A varint is at most 5 bytes, so the mask keeps its length.
1345
- // In u32: idx is below hdr, so the sum is at most 2^31 + 4, and one
1346
- // that wraps negative ends the walk at the next varint, which refuses it.
1347
- idx = toI32(toU32(idx) + toU32(this.vlen & 7))
1355
+ // A varint is at most 5 bytes, so the mask keeps its length. `idx` is
1356
+ // below `hdr`, so only a header within 7 bytes of 2^31 could pass the
1357
+ // literal, which no record buffer reaches; the test is what proves the
1358
+ // sum, and a header that did would be refused: the walk answers false.
1359
+ if (idx > 2147483640) {
1360
+ return false
1361
+ }
1362
+ idx = idx + (this.vlen & 7)
1348
1363
  const ty: i32 = toI32(t)
1349
1364
  types[k] = ty
1350
- offs[k] = toI32(d)
1365
+ // At most the record's length, checked above, so it is exact in an i32.
1366
+ const at: i32 = toI32(d)
1367
+ offs[k] = at
1351
1368
  let sz: i32 = 0
1352
1369
  if (ty >= 12) {
1353
1370
  sz = (ty - 12) >> 1
1354
1371
  } else if (ty >= 0 && ty < toI32(sizes.length)) {
1355
1372
  sz = sizes[ty] & 15
1356
1373
  }
1357
- // In u64, which cannot wrap: d is at most the record's length here.
1358
- d = toI64(toU64(d) + toU64(sz))
1374
+ // Two i32s summed in i64, which cannot overflow.
1375
+ d = toI64(at) + toI64(sz)
1359
1376
  u = k + 1
1360
1377
  }
1361
1378
  if (d > toI64(this.length) && u > 0 && u <= toI32(types.length)) {
@@ -1462,9 +1479,10 @@ export class RowRecord {
1462
1479
  }
1463
1480
  const first: i32 = toI32(b[at])
1464
1481
  let v: i64 = signed && first >= 128 ? toI64(first - 256) : toI64(first)
1465
- // In u32, which cannot wrap: the window was checked to lie inside `buf`.
1466
- const stop: i32 = toI32(toU32(at) + toU32(n))
1467
- for (let i: i32 = at + 1; i >= 0 && i < stop && i < toI32(b.length); i += 1) {
1482
+ // Two i32s summed in i64, which cannot overflow; the window was checked
1483
+ // to lie inside `buf`.
1484
+ const stop: i64 = toI64(at) + toI64(n)
1485
+ for (let i: i32 = at + 1; i >= 0 && toI64(i) < stop && i < toI32(b.length); i += 1) {
1468
1486
  v = (v << toI64(8)) | toI64(b[i])
1469
1487
  }
1470
1488
  return v