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/json-typed.ts CHANGED
@@ -459,6 +459,22 @@ export const JSON_DECODE_TAG: i32 = -26
459
459
  const JSON_TYPED_ERASED: string =
460
460
  "nish/json-typed: jsonDecode, jsonDecodeUnion and jsonEncode are written by the compiler from the class's fields, and under Node the types are erased"
461
461
 
462
+ /**
463
+ * `n` zeroed bytes, `n` at least `0`. A length that fits a `u16`, a number's
464
+ * and nearly every string's, is one allocation whose length needs no check;
465
+ * a longer one is grown a byte at a time.
466
+ */
467
+ const jsonTypedZeroBytes = (n: i32): u8[] => {
468
+ if (n >= 0 && n <= 65535) {
469
+ return new Array<u8>(toU16(n))
470
+ }
471
+ const bytes: u8[] = []
472
+ for (let k: i32 = 0; k < n; k += 1) {
473
+ bytes.push(toU8(0))
474
+ }
475
+ return bytes
476
+ }
477
+
462
478
  /**
463
479
  * A JSON number kept as the text it was written in, so that no digit is lost:
464
480
  * an integer wider than a `u64` and a fraction with more digits than an `f64`
@@ -567,7 +583,7 @@ export class JsonNumber {
567
583
  */
568
584
  setText(text: string): boolean {
569
585
  const n: i32 = toI32(text.length)
570
- const bytes: u8[] = new Array<u8>(n)
586
+ const bytes: u8[] = jsonTypedZeroBytes(n)
571
587
  for (let k: i32 = 0; k < n && k < toI32(bytes.length); k += 1) {
572
588
  let c: i32 = toI32(text.charCodeAt(k))
573
589
  // A number is ASCII; anything wider fails the check below as `~` does.
@@ -803,11 +819,27 @@ export class JsonDecoder {
803
819
  keyByte(at: i32): i32 {
804
820
  const tok: JsonTokenizer = this.tok
805
821
  const n: i32 = tok.stringLength()
806
- if (n !== tok.tokEnd - tok.tokStart - 2 || at < 0 || at >= n) {
822
+ const start: i32 = tok.tokStart
823
+ const src: u8[] = tok.src
824
+ // The width between the quotes is in `i64`, where it cannot wrap. The
825
+ // opening quote is a byte of `src`, so the `+ 1` fits, and a byte `at`
826
+ // past it is in `src` only when `at` is below the bytes left; then the
827
+ // `u32` sum of two non-negative `i32`s is exact and below the length.
828
+ if (
829
+ toI64(n) !== toI64(tok.tokEnd) - toI64(start) - toI64(2) ||
830
+ at < 0 ||
831
+ at >= n ||
832
+ start < 0 ||
833
+ start >= toI32(src.length)
834
+ ) {
807
835
  return -1
808
836
  }
809
- const src: u8[] = tok.src
810
- const i: i32 = tok.tokStart + 1 + at
837
+ const from: i32 = start + 1
838
+ const left: i32 = toI32(src.length) - from
839
+ if (at >= left) {
840
+ return -1
841
+ }
842
+ const i: i32 = toI32(toU32(from) + toU32(at))
811
843
  if (i < 0 || i >= toI32(src.length)) {
812
844
  return -1
813
845
  }
@@ -825,8 +857,9 @@ export class JsonDecoder {
825
857
  * @returns `true` when the key is counted; `false` with `JSON_DECODE_MAP_LIMIT`.
826
858
  */
827
859
  mapRoom(): boolean {
828
- if (this.mapKeys < this.maxMapKeys) {
829
- this.mapKeys += 1
860
+ const keys: i32 = this.mapKeys
861
+ if (keys < this.maxMapKeys) {
862
+ this.mapKeys = keys + 1
830
863
  return true
831
864
  }
832
865
  this.fail(JSON_DECODE_MAP_LIMIT, this.tok.tokStart)
@@ -855,14 +888,19 @@ export class JsonDecoder {
855
888
  if (n < 0) {
856
889
  return ""
857
890
  }
858
- if (n === tok.tokEnd - tok.tokStart - 2) {
891
+ const start: i32 = tok.tokStart
892
+ const end: i32 = tok.tokEnd
893
+ // In `i64`, where the width between the quotes cannot wrap. A string
894
+ // token starts at a byte of the input and spans `n + 2` bytes, so the
895
+ // other tests always hold; they prove both quotes' moves fit.
896
+ if (toI64(n) === toI64(end) - toI64(start) - toI64(2) && start >= 0 && start < end && end > 0) {
859
897
  // No escape, so the bytes between the quotes are the string's, copied
860
898
  // once by `String.fromBytes`.
861
- return String.fromBytes(tok.src, tok.tokStart + 1, tok.tokEnd - 1)
899
+ return String.fromBytes(tok.src, start + 1, end - 1)
862
900
  }
863
901
  // The unescaped bytes are a temporary: a caller that keeps only the string
864
902
  // it answers gives them back (the call-site reclaim, docs/LANGUAGE.md).
865
- const bytes: u8[] = new Array<u8>(n)
903
+ const bytes: u8[] = jsonTypedZeroBytes(n)
866
904
  const at: i32 = 0
867
905
  tok.stringInto(bytes, at)
868
906
  return String.fromBytes(bytes, 0, n)
@@ -966,9 +1004,10 @@ export class JsonDecoder {
966
1004
  }
967
1005
  this.unionStart = -1
968
1006
  // Held at `i32`'s largest value, where raised limits could carry a sum
969
- // past it: nothing an input holds may panic.
970
- const span: i32 = this.tok.tokEnd - start
971
- this.unionReread = span > 2147483647 - this.unionReread ? 2147483647 : this.unionReread + span
1007
+ // past it: nothing an input holds may panic. The sum of three `i32`s
1008
+ // cannot wrap an `i64`, and below the cap it is the `i32` it was.
1009
+ const total: i64 = toI64(this.unionReread) + toI64(this.tok.tokEnd) - toI64(start)
1010
+ this.unionReread = total > toI64(2147483647) ? 2147483647 : toI32(total)
972
1011
  this.tok.rewindObject(start)
973
1012
  return false
974
1013
  }
@@ -1220,7 +1259,11 @@ export class JsonDecoder {
1220
1259
  } else {
1221
1260
  bytes.push(b)
1222
1261
  }
1223
- copied += 1
1262
+ // Byte `copied` now holds `b`, so `copied` is below the length and
1263
+ // this always counts it; the test is what proves the `+ 1` fits.
1264
+ if (copied < toI32(bytes.length)) {
1265
+ copied += 1
1266
+ }
1224
1267
  }
1225
1268
  n.count = copied
1226
1269
  }
@@ -1300,16 +1343,21 @@ export class JsonDecoder {
1300
1343
  return xs
1301
1344
  }
1302
1345
  let n: i32 = 0
1346
+ let grown = false
1303
1347
  while (this.nextElement()) {
1304
1348
  const v: i32 = this.i32()
1305
- if (n < toI32(xs.length)) {
1349
+ if (!grown && n < toI32(xs.length)) {
1350
+ // `n` is below an `i32` length, so the `+ 1` fits.
1306
1351
  xs[n] = v
1352
+ n += 1
1307
1353
  } else {
1354
+ // Every slot `xs` had is written: the rest of the input is pushed.
1308
1355
  xs.push(v)
1356
+ grown = true
1309
1357
  }
1310
- n += 1
1311
1358
  }
1312
- while (toI32(xs.length) > n && xs.length > 0) {
1359
+ // An input shorter than `xs` leaves slots of the old value past `n`.
1360
+ while (!grown && toI32(xs.length) > n && xs.length > 0) {
1313
1361
  xs.pop()
1314
1362
  }
1315
1363
  return xs
@@ -1332,16 +1380,21 @@ export class JsonDecoder {
1332
1380
  return xs
1333
1381
  }
1334
1382
  let n: i32 = 0
1383
+ let grown = false
1335
1384
  while (this.nextElement()) {
1336
1385
  const v: u32 = this.u32()
1337
- if (n < toI32(xs.length)) {
1386
+ if (!grown && n < toI32(xs.length)) {
1387
+ // `n` is below an `i32` length, so the `+ 1` fits.
1338
1388
  xs[n] = v
1389
+ n += 1
1339
1390
  } else {
1391
+ // Every slot `xs` had is written: the rest of the input is pushed.
1340
1392
  xs.push(v)
1393
+ grown = true
1341
1394
  }
1342
- n += 1
1343
1395
  }
1344
- while (toI32(xs.length) > n && xs.length > 0) {
1396
+ // An input shorter than `xs` leaves slots of the old value past `n`.
1397
+ while (!grown && toI32(xs.length) > n && xs.length > 0) {
1345
1398
  xs.pop()
1346
1399
  }
1347
1400
  return xs
@@ -1364,16 +1417,21 @@ export class JsonDecoder {
1364
1417
  return xs
1365
1418
  }
1366
1419
  let n: i32 = 0
1420
+ let grown = false
1367
1421
  while (this.nextElement()) {
1368
1422
  const v: i64 = this.i64()
1369
- if (n < toI32(xs.length)) {
1423
+ if (!grown && n < toI32(xs.length)) {
1424
+ // `n` is below an `i32` length, so the `+ 1` fits.
1370
1425
  xs[n] = v
1426
+ n += 1
1371
1427
  } else {
1428
+ // Every slot `xs` had is written: the rest of the input is pushed.
1372
1429
  xs.push(v)
1430
+ grown = true
1373
1431
  }
1374
- n += 1
1375
1432
  }
1376
- while (toI32(xs.length) > n && xs.length > 0) {
1433
+ // An input shorter than `xs` leaves slots of the old value past `n`.
1434
+ while (!grown && toI32(xs.length) > n && xs.length > 0) {
1377
1435
  xs.pop()
1378
1436
  }
1379
1437
  return xs
@@ -1396,16 +1454,21 @@ export class JsonDecoder {
1396
1454
  return xs
1397
1455
  }
1398
1456
  let n: i32 = 0
1457
+ let grown = false
1399
1458
  while (this.nextElement()) {
1400
1459
  const v: u64 = this.u64()
1401
- if (n < toI32(xs.length)) {
1460
+ if (!grown && n < toI32(xs.length)) {
1461
+ // `n` is below an `i32` length, so the `+ 1` fits.
1402
1462
  xs[n] = v
1463
+ n += 1
1403
1464
  } else {
1465
+ // Every slot `xs` had is written: the rest of the input is pushed.
1404
1466
  xs.push(v)
1467
+ grown = true
1405
1468
  }
1406
- n += 1
1407
1469
  }
1408
- while (toI32(xs.length) > n && xs.length > 0) {
1470
+ // An input shorter than `xs` leaves slots of the old value past `n`.
1471
+ while (!grown && toI32(xs.length) > n && xs.length > 0) {
1409
1472
  xs.pop()
1410
1473
  }
1411
1474
  return xs
@@ -1428,16 +1491,21 @@ export class JsonDecoder {
1428
1491
  return xs
1429
1492
  }
1430
1493
  let n: i32 = 0
1494
+ let grown = false
1431
1495
  while (this.nextElement()) {
1432
1496
  const v: f64 = this.f64()
1433
- if (n < toI32(xs.length)) {
1497
+ if (!grown && n < toI32(xs.length)) {
1498
+ // `n` is below an `i32` length, so the `+ 1` fits.
1434
1499
  xs[n] = v
1500
+ n += 1
1435
1501
  } else {
1502
+ // Every slot `xs` had is written: the rest of the input is pushed.
1436
1503
  xs.push(v)
1504
+ grown = true
1437
1505
  }
1438
- n += 1
1439
1506
  }
1440
- while (toI32(xs.length) > n && xs.length > 0) {
1507
+ // An input shorter than `xs` leaves slots of the old value past `n`.
1508
+ while (!grown && toI32(xs.length) > n && xs.length > 0) {
1441
1509
  xs.pop()
1442
1510
  }
1443
1511
  return xs
@@ -1460,16 +1528,21 @@ export class JsonDecoder {
1460
1528
  return xs
1461
1529
  }
1462
1530
  let n: i32 = 0
1531
+ let grown = false
1463
1532
  while (this.nextElement()) {
1464
1533
  const v: boolean = this.boolean()
1465
- if (n < toI32(xs.length)) {
1534
+ if (!grown && n < toI32(xs.length)) {
1535
+ // `n` is below an `i32` length, so the `+ 1` fits.
1466
1536
  xs[n] = v
1537
+ n += 1
1467
1538
  } else {
1539
+ // Every slot `xs` had is written: the rest of the input is pushed.
1468
1540
  xs.push(v)
1541
+ grown = true
1469
1542
  }
1470
- n += 1
1471
1543
  }
1472
- while (toI32(xs.length) > n && xs.length > 0) {
1544
+ // An input shorter than `xs` leaves slots of the old value past `n`.
1545
+ while (!grown && toI32(xs.length) > n && xs.length > 0) {
1473
1546
  xs.pop()
1474
1547
  }
1475
1548
  return xs
@@ -1493,21 +1566,26 @@ export class JsonDecoder {
1493
1566
  return xs
1494
1567
  }
1495
1568
  let n: i32 = 0
1569
+ let grown = false
1496
1570
  while (this.nextElement()) {
1497
1571
  let cur: string = ""
1498
- if (n < toI32(xs.length)) {
1572
+ if (!grown && n < toI32(xs.length)) {
1499
1573
  cur = xs[n]
1500
1574
  }
1501
1575
  // Read into a local first: the call ends what the guard proved about `n`.
1502
1576
  const v: string = this.string(cur)
1503
- if (n < toI32(xs.length)) {
1577
+ if (!grown && n < toI32(xs.length)) {
1578
+ // `n` is below an `i32` length, so the `+ 1` fits.
1504
1579
  xs[n] = v
1580
+ n += 1
1505
1581
  } else {
1582
+ // Every slot `xs` had is written: the rest of the input is pushed.
1506
1583
  xs.push(v)
1584
+ grown = true
1507
1585
  }
1508
- n += 1
1509
1586
  }
1510
- while (toI32(xs.length) > n && xs.length > 0) {
1587
+ // An input shorter than `xs` leaves slots of the old value past `n`.
1588
+ while (!grown && toI32(xs.length) > n && xs.length > 0) {
1511
1589
  xs.pop()
1512
1590
  }
1513
1591
  return xs
@@ -1742,7 +1820,8 @@ export const jsonEncodeU64 = (w: JsonWriter, v: u64): void => {
1742
1820
  break
1743
1821
  }
1744
1822
  }
1745
- w.raw(digits, first, toI32(digits.length) - first)
1823
+ // `first` is a slot of the 20, so the `i64` difference is 1 to 20.
1824
+ w.raw(digits, first, toI32(toI64(digits.length) - toI64(first)))
1746
1825
  }
1747
1826
 
1748
1827
  /**