@amritk/nish-x86_64-linux 0.36.0 → 0.38.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 (103) hide show
  1. package/INSTALL.md +92 -71
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +75 -14
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/core/arrays.d.ts +4 -0
  8. package/runtime/core/arrays.mjs +33 -0
  9. package/runtime/nish.d.ts +1045 -512
  10. package/runtime/nish.h +53 -6
  11. package/runtime/page.d.ts +53 -0
  12. package/runtime/page.mjs +146 -0
  13. package/runtime/runtime-async.c +740 -0
  14. package/runtime/runtime-cross.c +2 -1
  15. package/runtime/runtime-region.c +85 -7
  16. package/runtime/runtime-text.c +1068 -0
  17. package/runtime/runtime-wasm.c +65 -9
  18. package/runtime/runtime.c +50 -961
  19. package/runtime/vite.mjs +48 -11
  20. package/scripts/build.sh +231 -31
  21. package/std/LICENSE-fdlibm +8 -0
  22. package/std/README.md +109 -82
  23. package/std/array.ts +266 -0
  24. package/std/bytes.ts +23 -19
  25. package/std/compress/checksum.ts +1 -1
  26. package/std/crypto/aes.ts +48 -43
  27. package/std/crypto/base64url.ts +7 -5
  28. package/std/crypto/bcrypt-batch.ts +13 -13
  29. package/std/crypto/bcrypt.ts +33 -27
  30. package/std/crypto/chacha20poly1305.ts +16 -14
  31. package/std/crypto/ed25519.ts +19 -16
  32. package/std/crypto/hmac.ts +7 -6
  33. package/std/crypto/jwt.ts +23 -20
  34. package/std/crypto/p256.ts +63 -56
  35. package/std/crypto/p384.ts +23 -20
  36. package/std/crypto/pki.ts +18 -15
  37. package/std/crypto/rsa.ts +23 -19
  38. package/std/crypto/x25519.ts +11 -10
  39. package/std/crypto/x509.ts +69 -62
  40. package/std/host/ring.ts +124 -0
  41. package/std/host/text.ts +86 -0
  42. package/std/json-equal.ts +12 -11
  43. package/std/json-number.ts +12 -12
  44. package/std/json-tokenizer.ts +26 -22
  45. package/std/json-typed.ts +92 -78
  46. package/std/json-value.ts +38 -33
  47. package/std/json.ts +32 -28
  48. package/std/math.ts +743 -41
  49. package/std/net/dns-resolver.ts +46 -6
  50. package/std/net/dns.ts +50 -13
  51. package/std/net/fetch.ts +1744 -295
  52. package/std/net/handler.ts +30 -0
  53. package/std/net/hpack.ts +29 -26
  54. package/std/net/http-compress.ts +42 -28
  55. package/std/net/http-fields.ts +19 -4
  56. package/std/net/http1-client-codec.ts +29 -25
  57. package/std/net/http1-codec.ts +62 -91
  58. package/std/net/http1-server.ts +79 -57
  59. package/std/net/http2-frame.ts +17 -16
  60. package/std/net/http2-tls.ts +17 -15
  61. package/std/net/http2.ts +67 -54
  62. package/std/net/http3-frame.ts +11 -10
  63. package/std/net/http3-server.ts +36 -35
  64. package/std/net/http3.ts +70 -58
  65. package/std/net/postgres-async.ts +1005 -0
  66. package/std/net/postgres.ts +185 -164
  67. package/std/net/prefork.ts +38 -34
  68. package/std/net/qpack.ts +15 -14
  69. package/std/net/quic-conn-ack.ts +1 -0
  70. package/std/net/quic-conn-cid.ts +11 -0
  71. package/std/net/quic-conn-params.ts +41 -14
  72. package/std/net/quic-datagram.ts +7 -0
  73. package/std/net/quic-frame.ts +48 -12
  74. package/std/net/quic-listener.ts +61 -44
  75. package/std/net/quic-packet.ts +71 -52
  76. package/std/net/quic-recovery.ts +59 -21
  77. package/std/net/quic-stream.ts +83 -55
  78. package/std/net/quic.ts +204 -152
  79. package/std/net/serve.ts +763 -0
  80. package/std/net/tls/client.ts +1 -0
  81. package/std/net/tls/codec.ts +81 -12
  82. package/std/net/tls/record-client.ts +30 -18
  83. package/std/net/tls/record-server.ts +74 -46
  84. package/std/net/tls/record.ts +33 -24
  85. package/std/net/tls/ticket.ts +2 -0
  86. package/std/net/tls/verify.ts +8 -7
  87. package/std/net/tls-tcp.ts +58 -50
  88. package/std/net/tls.ts +55 -43
  89. package/std/net/url.ts +12 -9
  90. package/std/net/websocket.ts +23 -19
  91. package/std/net/webtransport.ts +24 -18
  92. package/std/random.ts +8 -6
  93. package/std/regex.ts +70 -59
  94. package/std/sync/ring.ts +24 -21
  95. package/std/sync/shm.ts +9 -7
  96. package/std/term-io.ts +17 -14
  97. package/std/term-probe.ts +11 -10
  98. package/std/term-rate.ts +71 -61
  99. package/std/term.ts +73 -59
  100. package/std/text.ts +242 -15
  101. package/std/threads.ts +19 -15
  102. package/std/yaml-json.ts +17 -15
  103. package/std/yaml-write.ts +21 -14
@@ -765,6 +765,41 @@ export class DnsResolver {
765
765
  this.lookupState[lookup] = DNS_L_FREE
766
766
  }
767
767
 
768
+ /**
769
+ * Ends `lookup` now with the addresses it already has, when one of its
770
+ * current name's two queries has answered with addresses and the other is
771
+ * still asking; the one still asking is owed a `DNS_CLOSE`, as `cancel`
772
+ * owes one. Otherwise the lookup is left as it was. A driver whose own time
773
+ * is up calls this before `cancel`, so a network that never answers AAAA
774
+ * still gives the A addresses it found. Panics when `lookup` is not a
775
+ * lookup in progress, as `result` does.
776
+ *
777
+ * @param lookup The slot `start` answered.
778
+ * @returns Whether the lookup has now finished, its answer for `result`
779
+ * to read; true for one that had finished already.
780
+ */
781
+ settle(lookup: i32): boolean {
782
+ if (lookup < 0 || lookup >= this.maxLookups || this.lookupState[lookup] === DNS_L_FREE) {
783
+ panic(`DnsResolver.settle: ${lookup} is not a lookup in progress`)
784
+ }
785
+ if (this.lookupState[lookup] === DNS_L_DONE) {
786
+ return true
787
+ }
788
+ const a: i32 = lookup * 2
789
+ let open: i32 = -1
790
+ if (this.state[a] === DNS_Q_DONE && this.outcome[a] > 0) {
791
+ open = a + 1
792
+ } else if (this.state[a + 1] === DNS_Q_DONE && this.outcome[a + 1] > 0) {
793
+ open = a
794
+ }
795
+ if (open < 0 || this.state[open] === DNS_Q_DONE) {
796
+ return false
797
+ }
798
+ // The other has addresses, so this finishes the lookup and never reaches a next name, which alone reads the clock.
799
+ dnsResolverDone(this, open, DNS_RESOLVE_NO_ANSWER, 0)
800
+ return true
801
+ }
802
+
768
803
  /**
769
804
  * How many slots are taken.
770
805
  *
@@ -1163,7 +1198,9 @@ const dnsResolverSameName = (a: u8[], b: u8[]): boolean => {
1163
1198
  * an NXDOMAIN, such an error is the lookup's, since the name may well exist.
1164
1199
  * Otherwise the name has no address: NXDOMAIN from both moves the search on,
1165
1200
  * and NODATA ends it, as musl's resolver does, since the name exists and only
1166
- * lacks the type asked.
1201
+ * lacks the type asked. NODATA is `DNS_RESOLVE_NOT_FOUND` even after an
1202
+ * earlier candidate left an error to fall back on: that error stands only
1203
+ * when no later candidate answers.
1167
1204
  */
1168
1205
  const dnsResolverDone = (r: DnsResolver, q: i32, outcome: i32, nowNanos: i64): void => {
1169
1206
  if (r.state[q] === DNS_Q_UDP || r.state[q] === DNS_Q_TCP) {
@@ -1196,14 +1233,17 @@ const dnsResolverDone = (r: DnsResolver, q: i32, outcome: i32, nowNanos: i64): v
1196
1233
  dnsResolverFinish(r, l, error)
1197
1234
  return
1198
1235
  }
1236
+ if (error === 0 && !(r.nxdomain[a] && r.nxdomain[a + 1])) {
1237
+ // NODATA answers for the name, so an earlier candidate's fallback does not stand.
1238
+ dnsResolverFinish(r, l, DNS_RESOLVE_NOT_FOUND)
1239
+ return
1240
+ }
1199
1241
  if (error !== 0 && r.fallback[l] === 0) {
1200
1242
  r.fallback[l] = error
1201
1243
  }
1202
- if (error !== 0 || (r.nxdomain[a] && r.nxdomain[a + 1])) {
1203
- r.candidate[l] = r.candidate[l] + 1
1204
- if (dnsResolverNextCandidate(r, l, nowNanos)) {
1205
- return
1206
- }
1244
+ r.candidate[l] = r.candidate[l] + 1
1245
+ if (dnsResolverNextCandidate(r, l, nowNanos)) {
1246
+ return
1207
1247
  }
1208
1248
  dnsResolverFinish(r, l, r.fallback[l] !== 0 ? r.fallback[l] : DNS_RESOLVE_NOT_FOUND)
1209
1249
  }
package/std/net/dns.ts CHANGED
@@ -279,6 +279,22 @@ export class DnsClient {
279
279
  this.pump(nowNanos)
280
280
  }
281
281
 
282
+ /**
283
+ * `DnsResolver.settle`, closing the socket of the query it stops: ends the
284
+ * lookup with the addresses one family has found when the other is still
285
+ * asking, so a caller whose time is up keeps them.
286
+ *
287
+ * @param lookup An id `start` answered whose answer has not been read.
288
+ * @param nowNanos The caller's monotonic clock now, in nanoseconds.
289
+ * @returns Whether the lookup has finished, so `result` answers it rather than
290
+ * `DNS_RESOLVE_PENDING`.
291
+ */
292
+ settle(lookup: i32, nowNanos: i64): boolean {
293
+ const done: boolean = this.resolver.settle(lookup)
294
+ this.pump(nowNanos)
295
+ return done
296
+ }
297
+
282
298
  /**
283
299
  * The pair `token`, `events` that `pollWait` reported at `nowNanos`, for a
284
300
  * token `owns` answers true for: reads the datagrams (at most
@@ -529,9 +545,9 @@ const dnsClientReadStream = (c: DnsClient, q: i32, fd: i32, hungUp: boolean, now
529
545
  * The addresses of `name`, looked up as `/etc/resolv.conf` and `/etc/hosts`
530
546
  * say (a missing resolv.conf means the defaults, a missing hosts file no
531
547
  * entries), each 18 bytes with `port` into `out` and its TTL in seconds into
532
- * `ttls`. It waits for both the A and the AAAA answer, so a family that
533
- * answered is not returned on its own once `timeoutMs` passes: it is
534
- * `DNS_RESOLVE_TIMEOUT`.
548
+ * `ttls`. It waits for both the A and the AAAA answer until `timeoutMs`
549
+ * passes; then a family that answered with addresses is returned on its own,
550
+ * and only a lookup with none yet is `DNS_RESOLVE_TIMEOUT`.
535
551
  *
536
552
  * **It blocks the thread** until the lookup ends, on a loop of its own, so
537
553
  * it is for a CLI or a program's start-up; a server resolves with a
@@ -548,24 +564,41 @@ const dnsClientReadStream = (c: DnsClient, q: i32, fd: i32, hungUp: boolean, now
548
564
  * `DNS_RESOLVE_MAX_ADDRESSES * 18` bytes.
549
565
  * @param ttls Where each address's TTL goes, in seconds; at least `DNS_RESOLVE_MAX_ADDRESSES`.
550
566
  * @returns The count of addresses written; or a negative code: `DNS_RESOLVE_TIMEOUT` once
551
- * `timeoutMs` has passed, `DNS_RESOLVE_NOT_FOUND`, `DNS_RESOLVE_NO_ANSWER` or
567
+ * `timeoutMs` has passed with no address found, `DNS_RESOLVE_NOT_FOUND`, `DNS_RESOLVE_NO_ANSWER` or
552
568
  * `DNS_RESOLVE_CNAME` from the lookup, the `DNS_ERR_*` code of a name that is not one, or
553
569
  * the negative errno of a poll loop that cannot be made or waited on.
554
570
  */
555
571
  export const resolveHost = (name: string, timeoutMs: i32, port: i32, out: u8[], ttls: i32[]): i32 => {
556
572
  const config: DnsConfig = new DnsConfig()
557
- const conf: u8[] | null = readFileBytesSync("/etc/resolv.conf")
558
- if (conf !== null) {
559
- dnsParseResolvConf(conf, 0, toI32(conf.length), config)
560
- } else {
561
- dnsParseResolvConf(new Array<u8>(0), 0, 0, config)
562
- }
563
573
  const hosts: DnsHosts = new DnsHosts()
574
+ dnsLoadSystem(config, hosts)
575
+ return dnsLookupWith(config, hosts, name, timeoutMs, port, out, ttls)
576
+ }
577
+
578
+ /**
579
+ * Reads the system's resolver settings the way `resolveHost` does:
580
+ * `/etc/resolv.conf` into `config` (a missing file means the defaults) and
581
+ * `/etc/hosts` into `hosts` (a missing file adds no entry). A driver of its
582
+ * own, such as the async `fetch`'s, starts from this so every lookup in a
583
+ * program honours the same files. Both reads are synchronous file reads.
584
+ *
585
+ * @param config A fresh `DnsConfig` to fill, or null to leave
586
+ * `/etc/resolv.conf` unread (a caller with a configuration of its own).
587
+ * @param hosts A fresh `DnsHosts` to fill.
588
+ */
589
+ export const dnsLoadSystem = (config: DnsConfig | null, hosts: DnsHosts): void => {
590
+ if (config !== null) {
591
+ const conf: u8[] | null = readFileBytesSync("/etc/resolv.conf")
592
+ if (conf !== null) {
593
+ dnsParseResolvConf(conf, 0, toI32(conf.length), config)
594
+ } else {
595
+ dnsParseResolvConf(new Array<u8>(0), 0, 0, config)
596
+ }
597
+ }
564
598
  const hostsFile: u8[] | null = readFileBytesSync("/etc/hosts")
565
599
  if (hostsFile !== null) {
566
600
  dnsParseHosts(hostsFile, 0, toI32(hostsFile.length), hosts)
567
601
  }
568
- return dnsLookupWith(config, hosts, name, timeoutMs, port, out, ttls)
569
602
  }
570
603
 
571
604
  /**
@@ -618,8 +651,12 @@ export const dnsLookupWith = (
618
651
  while (status === DNS_RESOLVE_PENDING) {
619
652
  const now: i64 = monotonicNanos()
620
653
  if (now >= end) {
621
- client.cancel(lookup, now)
622
- status = DNS_RESOLVE_TIMEOUT
654
+ if (client.settle(lookup, now)) {
655
+ status = client.result(lookup, out, ttls)
656
+ } else {
657
+ client.cancel(lookup, now)
658
+ status = DNS_RESOLVE_TIMEOUT
659
+ }
623
660
  break
624
661
  }
625
662
  const own: i32 = toI32((end - now + 999999) / 1000000)