nish 0.35.0 → 0.37.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 (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
@@ -4,17 +4,48 @@
4
4
  * and answers each lookup's addresses, while the program's own loop owns the
5
5
  * sockets and the clock.
6
6
  *
7
- * const resolver = new DnsResolver(config, hosts, 4);
8
- * const lookup = resolver.start("example.com", 443, now);
9
- * // in the loop: send what nextOutgoing gives, feed replies to receive,
10
- * // call tick(now) when nextDeadlineNanos() passes, then read result.
7
+ * import {
8
+ * DNS_OUTGOING_MAX_BYTES, DNS_RESOLVE_MAX_ADDRESSES, DnsResolver,
9
+ * } from "nish/net/dns-resolver"
10
+ * import { DNS_ADDRESS_BYTES } from "nish/net/dns-message"
11
+ * import { DnsConfig, dnsParseResolvConf } from "nish/net/dns-config"
12
+ * import { readFileBytesSync } from "nish:fs"
13
+ * import { monotonicNanos } from "nish:process"
11
14
  *
12
- * `nish/net/dns` drives it over `nish:net` sockets and adds a blocking
13
- * `resolveHost`; this module never imports `nish:net`, never reads a clock
14
- * and never reads a file, so it compiles for `wasm32` and a test drives it
15
- * with scripted packets.
15
+ * const config = new DnsConfig()
16
+ * const resolv: u8[] = readFileBytesSync("/etc/resolv.conf") ?? new Array<u8>(0)
17
+ * dnsParseResolvConf(resolv, 0, toI32(resolv.length), config)
18
+ * const resolver = new DnsResolver(config, null, 4)
19
+ * const lookup: i32 = resolver.start("example.com", 443, monotonicNanos())
20
+ * // lookup < 0: DNS_RESOLVE_BUSY, or the DNS_ERR_* code of a name that is not one
21
+ * const out: u8[] = new Array<u8>(DNS_OUTGOING_MAX_BYTES)
22
+ * const to: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
23
+ * const meta: i32[] = new Array<i32>(2)
24
+ * const idBytes: u8[] = new Array<u8>(2)
25
+ * crypto.getRandomValues(idBytes) // a fresh, random 16-bit ID per send
26
+ * let q: i32 = resolver.nextOutgoing(out, to, meta, (toI32(idBytes[0]) << 8) | toI32(idBytes[1]))
27
+ * while (q >= 0) {
28
+ * // meta[0] DNS_SEND_UDP or DNS_SEND_TCP: close q's old socket, send out[0, meta[1]) to `to`
29
+ * // meta[0] DNS_CLOSE: close q's socket
30
+ * crypto.getRandomValues(idBytes)
31
+ * q = resolver.nextOutgoing(out, to, meta, (toI32(idBytes[0]) << 8) | toI32(idBytes[1]))
32
+ * }
33
+ * // a datagram for q: resolver.receive(q, buf, len, from, now)
34
+ * // TCP bytes for q: resolver.receiveStream(q, buf, len, now)
35
+ * // q's socket failed: resolver.fail(q, now)
36
+ * // now >= resolver.nextDeadlineNanos() (when it is not -1): resolver.tick(now)
37
+ * const addresses: u8[] = new Array<u8>(DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES)
38
+ * const ttls: i32[] = new Array<i32>(DNS_RESOLVE_MAX_ADDRESSES)
39
+ * const status: i32 = resolver.result(lookup, addresses, ttls)
40
+ * // DNS_RESOLVE_PENDING, an address count, or a negative DNS_RESOLVE_* code
16
41
  *
17
- * A lookup asks A and AAAA at once, one query each. Each query tries the
42
+ * **It never touches the outside world.** `nish/net/dns` drives it over
43
+ * `nish:net` sockets and adds a blocking `resolveHost`; this module never
44
+ * imports `nish:net`, never reads a clock and never reads a file, so it
45
+ * compiles for `wasm32` and a test drives it with scripted packets. Every time
46
+ * it takes is `nowNanos` on the caller's monotonic clock, in nanoseconds.
47
+ *
48
+ * **A lookup asks A and AAAA at once**, one query each. Each query tries the
18
49
  * servers of its `DnsConfig` in turn, `attempts` rounds of `timeout` seconds
19
50
  * a try, follows a CNAME the reply leaves unanswered with a query of its own,
20
51
  * at most `DNS_RESOLVER_MAX_CNAME_HOPS` of them, and asks again over TCP when
@@ -22,20 +53,27 @@
22
53
  * resolv.conf(5) describes. `localhost`, an address literal and a hosts entry
23
54
  * answer at once with no packet.
24
55
  *
25
- * A reply from any address or port but the server's, or with another ID or
26
- * question, is dropped and counted, and the query keeps waiting for the true
27
- * one: a spoofer can lose a race, never end a lookup. The ID of each send is
28
- * the caller's, given to `nextOutgoing`: `nish/net/dns` draws it from
56
+ * **A spoofer can lose a race, never end a lookup.** A reply from any address
57
+ * or port but the server's, or with another ID or question, is dropped and
58
+ * counted, and the query keeps waiting for the true one. The ID of each send
59
+ * is the caller's, given to `nextOutgoing`: `nish/net/dns` draws it from
29
60
  * `crypto.getRandomValues`, which a `wasm32` build cannot reach, so this
30
61
  * module asks for none and a test can script its IDs.
31
62
  *
32
- * Every buffer is made in the constructor; a lookup allocates only the
63
+ * **Every buffer is made in the constructor**; a lookup allocates only the
33
64
  * sixteen transient bytes `dnsParseAddress` takes to test a name for an
34
- * address literal.
65
+ * address literal. The resolver keeps the `DnsConfig` and `DnsHosts` it was
66
+ * given, not a copy, and reads them while lookups run: parse new bytes into
67
+ * them between lookups, not during one.
68
+ *
69
+ * **Misuse panics; a server's bytes do not.** A query or lookup number the
70
+ * resolver did not give out, a buffer too short, a port or ID that is not 16
71
+ * bits all panic, while anything a server sends is dropped, counted or fails
72
+ * that server. Every counter stops at the i32 maximum rather than wrapping.
73
+ *
35
74
  * Private helpers share the importing program's flat symbol namespace
36
75
  * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
37
76
  */
38
-
39
77
  import {
40
78
  DNS_ADDRESS_BYTES,
41
79
  DNS_ERR_ID,
@@ -58,7 +96,12 @@ import { DnsConfig, DnsHosts, dnsHostsFind, dnsParseAddress } from "nish/net/dns
58
96
 
59
97
  /** `result` for a lookup that has not finished. */
60
98
  export const DNS_RESOLVE_PENDING: i32 = -40
61
- /** The name does not exist, or has no address: NXDOMAIN or NODATA from every candidate. */
99
+ /**
100
+ * The name has no address: NXDOMAIN for every candidate of the search list,
101
+ * or NODATA for one (which ends the search, as the name exists). Also the
102
+ * answer, with no packet sent, when `start` finds no usable candidate (every
103
+ * suffixed name too long or not a name).
104
+ */
62
105
  export const DNS_RESOLVE_NOT_FOUND: i32 = -41
63
106
  /** No server gave an answer: each timed out, failed or was refused, `attempts` rounds over. */
64
107
  export const DNS_RESOLVE_NO_ANSWER: i32 = -42
@@ -75,7 +118,10 @@ export const DNS_RESOLVE_TIMEOUT: i32 = -45
75
118
 
76
119
  /** The most queries a CNAME chain may add to one query before it is `DNS_RESOLVE_CNAME`. */
77
120
  export const DNS_RESOLVER_MAX_CNAME_HOPS: i32 = 8
78
- /** The longest reply read over TCP; a longer length prefix fails the server. */
121
+ /**
122
+ * The longest reply read over TCP, in bytes; a length prefix over it, or under
123
+ * the 12-byte header, fails the server.
124
+ */
79
125
  export const DNS_TCP_MAX_REPLY: i32 = 16384
80
126
  /** The most addresses one lookup answers: `DNS_MAX_ANSWERS` A and as many AAAA. */
81
127
  export const DNS_RESOLVE_MAX_ADDRESSES: i32 = 2 * DNS_MAX_ANSWERS
@@ -87,7 +133,7 @@ export const DNS_SEND_UDP: i32 = 1
87
133
  export const DNS_SEND_TCP: i32 = 2
88
134
  /** `nextOutgoing`'s `meta[0]`: close the socket the query held, if any. */
89
135
  export const DNS_CLOSE: i32 = 3
90
- /** The room `nextOutgoing` needs in `out`: a query and TCP's two-byte length. */
136
+ /** The room `nextOutgoing` needs in `out`, in bytes: a query and TCP's two-byte length. */
91
137
  export const DNS_OUTGOING_MAX_BYTES: i32 = DNS_QUERY_MAX_BYTES + 2
92
138
 
93
139
  // A query's state. A lookup's: free, running, or finished with its result held.
@@ -105,56 +151,111 @@ const DNS_L_DONE: i32 = 2
105
151
  * `q / 2`, and asks A when `q` is even and AAAA when it is odd.
106
152
  */
107
153
  export class DnsResolver {
154
+ /** @internal Used by `dnsResolverNextCandidate`, `dnsResolverCandidate`, `dnsResolverSend` and `dnsResolverServerFailed` in this module. */
108
155
  config: DnsConfig
109
- hosts: DnsHosts | null
110
- /** Each query's bytes, from offset 0: the header, the name and the question's type and class. */
156
+ private hosts: DnsHosts | null
157
+ /**
158
+ * Each query's bytes, from offset 0: the header, the name and the question's type and class.
159
+ * @internal Used by `dnsResolverAsk` in this module.
160
+ */
111
161
  queries: u8[][]
112
- /** Each query's name in wire form, labels and root, as asked; and that name lowercased, the start of its chain. */
162
+ /**
163
+ * Each query's name in wire form, labels and root, as asked; and that name lowercased, the start of its chain.
164
+ * @internal Used by `dnsResolverNextCandidate`, `dnsResolverBegin`, `dnsResolverAsk` and `dnsResolverAnswer` in this module.
165
+ */
113
166
  names: u8[][]
167
+ /** @internal Used by `dnsResolverBegin` and `dnsResolverAnswer` in this module. */
114
168
  starts: u8[][]
115
169
  /** Each query's TCP reply as it arrives, without its two-byte length. */
116
- streams: u8[][]
117
- /** Each query's addresses and their TTLs, once it has its answer. */
170
+ private streams: u8[][]
171
+ /**
172
+ * Each query's addresses and their TTLs, once it has its answer.
173
+ * @internal Used by `dnsResolverGather` and `dnsResolverAnswer` in this module.
174
+ */
118
175
  answers: u8[][]
176
+ /** @internal Used by `dnsResolverGather` and `dnsResolverAnswer` in this module. */
119
177
  answerTtls: i32[][]
120
- /** Each lookup's name in wire form, without its root byte, as `start` gave it. */
178
+ /**
179
+ * Each lookup's name in wire form, without its root byte, as `start` gave it.
180
+ * @internal Used by `dnsResolverCandidate` in this module.
181
+ */
121
182
  bases: u8[][]
122
- /** Each lookup's answer, A first. */
183
+ /**
184
+ * Each lookup's answer, A first.
185
+ * @internal Used by `dnsResolverGather` in this module.
186
+ */
123
187
  lookupAddresses: u8[][]
188
+ /** @internal Used by `dnsResolverGather` in this module. */
124
189
  lookupTtls: i32[][]
190
+ /** @internal Used by `dnsResolverAnswer` in this module. */
125
191
  reply: DnsReply
126
- scratch: u8[]
192
+ private scratch: u8[]
193
+ /** @internal Used by `dnsResolverAsk` in this module. */
127
194
  queryLength: i32[]
195
+ /** @internal Used by `dnsResolverBegin`, `dnsResolverAsk` and `dnsResolverAnswer` in this module. */
128
196
  nameLength: i32[]
197
+ /** @internal Used by `dnsResolverStop`, `dnsResolverSend` and `dnsResolverDone` in this module. */
129
198
  state: i32[]
130
- /** The outcome of a finished query: its address count, or `DNS_RESOLVE_*`. */
199
+ /**
200
+ * The outcome of a finished query: its address count, or `DNS_RESOLVE_*`.
201
+ * @internal Used by `dnsResolverDone` in this module.
202
+ */
131
203
  outcome: i32[]
132
- /** The action `nextOutgoing` owes the driver for a query: 0 or a `DNS_SEND_*`/`DNS_CLOSE`. */
204
+ /**
205
+ * The action `nextOutgoing` owes the driver for a query: 0 or a `DNS_SEND_*`/`DNS_CLOSE`.
206
+ * @internal Used by `dnsResolverStop`, `dnsResolverSend` and `dnsResolverDone` in this module.
207
+ */
133
208
  pending: i32[]
209
+ /** @internal Used by `dnsResolverAsk` and `dnsResolverServerFailed` in this module. */
134
210
  server: i32[]
211
+ /** @internal Used by `dnsResolverAsk` and `dnsResolverServerFailed` in this module. */
135
212
  round: i32[]
213
+ /** @internal Used by `dnsResolverBegin` and `dnsResolverAnswer` in this module. */
136
214
  hops: i32[]
215
+ /** @internal Used by `dnsResolverSend` in this module. */
137
216
  deadline: i64[]
138
- /** Bytes of the TCP reply read so far, its two-byte length included, and that length once read. */
217
+ /**
218
+ * Bytes of the TCP reply read so far, its two-byte length included, and that length once read.
219
+ * @internal Used by `dnsResolverSend` in this module.
220
+ */
139
221
  streamHave: i32[]
222
+ /** @internal Used by `dnsResolverSend` in this module. */
140
223
  streamWant: i32[]
224
+ /** @internal Used by `dnsResolverCandidate` in this module. */
141
225
  baseLength: i32[]
142
- /** Whether a finished query's name was NXDOMAIN, which moves the search on, rather than NODATA, which ends it. */
226
+ /**
227
+ * Whether a finished query's name was NXDOMAIN, which moves the search on, rather than NODATA, which ends it.
228
+ * @internal Used by `dnsResolverAsk`, `dnsResolverAnswer` and `dnsResolverDone` in this module.
229
+ */
143
230
  nxdomain: boolean[]
144
- /** Whether a lookup's name ended in a dot, so no search suffix applies. */
231
+ /**
232
+ * Whether a lookup's name ended in a dot, so no search suffix applies.
233
+ * @internal Used by `dnsResolverNextCandidate` in this module.
234
+ */
145
235
  absolute: boolean[]
146
- /** The candidate a lookup is asking, by `dnsResolverCandidate`'s order, and how many it has. */
236
+ /**
237
+ * The candidate a lookup is asking, by `dnsResolverCandidate`'s order, and how many it has.
238
+ * @internal Used by `dnsResolverNextCandidate` and `dnsResolverDone` in this module.
239
+ */
147
240
  candidate: i32[]
241
+ /** @internal Used by `dnsResolverNextCandidate` in this module. */
148
242
  candidates: i32[]
149
- /** The error a lookup ends with if no later candidate answers: a family that reached no server while the other was NXDOMAIN, or 0. */
243
+ /**
244
+ * The error a lookup ends with if no later candidate answers: a family that reached no server while the other was NXDOMAIN, or 0.
245
+ * @internal Used by `dnsResolverDone` in this module.
246
+ */
150
247
  fallback: i32[]
248
+ /** @internal Used by `dnsResolverNextCandidate` in this module. */
151
249
  dots: i32[]
250
+ /** @internal Used by `dnsResolverFinishNow` and `dnsResolverFinish` in this module. */
152
251
  lookupState: i32[]
252
+ /** @internal Used by `dnsResolverFinishNow` and `dnsResolverFinish` in this module. */
153
253
  lookupResult: i32[]
154
- lookupPort: i32[]
254
+ private lookupPort: i32[]
255
+ /** @internal Used by `dnsResolverCheckQuery` in this module; DnsClient's constructor and `DnsClient.tokens` in std/net/dns.ts. */
155
256
  maxLookups: i32 = 0
156
257
  /** Where `nextOutgoing` looks first, so no query waits behind another forever. */
157
- cursor: i32 = 0
258
+ private cursor: i32 = 0
158
259
  /** Datagrams dropped for coming from another address or port than the server's. */
159
260
  droppedSource: i32 = 0
160
261
  /** Replies dropped for another ID. */
@@ -165,7 +266,10 @@ export class DnsResolver {
165
266
  droppedMalformed: i32 = 0
166
267
  /** Tries that timed out. */
167
268
  timeouts: i32 = 0
168
- /** Tries that ended in SERVFAIL, REFUSED, a bad TCP reply or a socket failure. */
269
+ /**
270
+ * Tries that failed, for any reason: a timeout (also counted in `timeouts`),
271
+ * SERVFAIL, REFUSED, FORMERR, NOTIMP, a bad TCP reply or a socket failure.
272
+ */
169
273
  serverFailures: i32 = 0
170
274
  /** Truncated replies asked again over TCP. */
171
275
  tcpRetries: i32 = 0
@@ -177,8 +281,18 @@ export class DnsResolver {
177
281
  /**
178
282
  * A resolver with room for `maxLookups` lookups at once, every buffer made
179
283
  * now. Panics unless `maxLookups` is 1 to `DNS_RESOLVER_MAX_LOOKUPS`.
180
- * `config` and `hosts` are read at each `start`, so a program may parse
181
- * them again between lookups.
284
+ * `config` and `hosts` are kept, not copied: `start` reads both, and a
285
+ * running query reads `config`'s servers, search list, `searchCount`,
286
+ * `ndots`, `timeout` and `attempts`, so a program may parse them again
287
+ * between lookups but not while one runs.
288
+ *
289
+ * @param config The servers, search list, `ndots`, `timeout` (seconds) and
290
+ * `attempts`, as `dnsParseResolvConf` read them.
291
+ * @param hosts The hosts table answered before any packet is sent, or `null`
292
+ * for none.
293
+ * @param maxLookups How many lookups may run at once, 1 to
294
+ * `DNS_RESOLVER_MAX_LOOKUPS` (64). Each holds the buffers of two queries,
295
+ * among them a `DNS_TCP_MAX_REPLY` (16 KiB) TCP reply buffer apiece.
182
296
  */
183
297
  constructor(config: DnsConfig, hosts: DnsHosts | null, maxLookups: i32) {
184
298
  if (maxLookups < 1 || maxLookups > DNS_RESOLVER_MAX_LOOKUPS) {
@@ -247,8 +361,18 @@ export class DnsResolver {
247
361
  *
248
362
  * `localhost` and any name under it answer 127.0.0.1 and ::1 (RFC 6761
249
363
  * §6.3), an address literal answers itself, and a name in `hosts` answers
250
- * its entries, each at once and with no packet: `result` has them now.
251
- * Panics when `port` is not 16 bits.
364
+ * its entries, each at once and with no packet: `result` has them now,
365
+ * with TTL 0. Panics when `port` is not 16 bits.
366
+ *
367
+ * @param name The name to look up: a host name, relative or with a trailing
368
+ * dot, or an IPv4 or IPv6 literal.
369
+ * @param port The port, 0 to 0xFFFF, written into every address `result`
370
+ * gives.
371
+ * @param nowNanos The caller's monotonic clock now, in nanoseconds; the first
372
+ * tries' deadlines are `timeout` seconds after it.
373
+ * @returns The lookup's slot, 0 to `maxLookups - 1`, to pass to `result` or
374
+ * `cancel`; or `DNS_RESOLVE_BUSY`, or a negative `DNS_ERR_*` code from
375
+ * `dnsBuildQuery`, when no lookup was started.
252
376
  */
253
377
  start(name: string, port: i32, nowNanos: i64): i32 {
254
378
  if (port < 0 || port > 0xffff) {
@@ -331,6 +455,22 @@ export class DnsResolver {
331
455
  * socket the query held, so the driver closes that one first. Panics when
332
456
  * `out` is shorter than `DNS_OUTGOING_MAX_BYTES`, `to` shorter than 18
333
457
  * bytes or `meta` than 2, or when `id` is not 16 bits.
458
+ *
459
+ * Queries take turns, so a query that always has work does not starve the
460
+ * rest. Call it until it answers -1 after every `start`, `receive`,
461
+ * `receiveStream`, `fail`, `tick` and `cancel`.
462
+ *
463
+ * @param out Where a send's bytes go, from offset 0; at least
464
+ * `DNS_OUTGOING_MAX_BYTES` long.
465
+ * @param to Where the server's 18-byte address (IPv6 form and port) goes for
466
+ * a send; left as it was for a close.
467
+ * @param meta Two slots: `meta[0]` gets the action, `meta[1]` the number of
468
+ * bytes of `out` to send (0 for `DNS_CLOSE`); not written when the call
469
+ * answers -1.
470
+ * @param id The ID for this send, 0 to 0xFFFF: fresh and random each call.
471
+ * A close or no action does not use it.
472
+ * @returns The query the action is for, 0 to `2 * maxLookups - 1` (its
473
+ * lookup is the query divided by 2), or -1 when nothing is owed.
334
474
  */
335
475
  nextOutgoing(out: u8[], to: u8[], meta: i32[], id: i32): i32 {
336
476
  if (id < 0 || id > 0xffff) {
@@ -388,8 +528,18 @@ export class DnsResolver {
388
528
  * `q`'s socket at `nowNanos`. One from another address or port than the
389
529
  * server's, with another ID or question, or malformed, is dropped and
390
530
  * counted, and the query keeps waiting. One that answers the query with an
391
- * RCODE the resolver does not read, FORMERR or NOTIMP, fails the server. Panics when `q` is not a query or
392
- * `len` is outside `buf`.
531
+ * RCODE the resolver does not read, FORMERR or NOTIMP, or with SERVFAIL or
532
+ * REFUSED, fails the server. A truncated reply asks again over TCP. Panics
533
+ * when `q` is not a query, `len` is outside `buf` or `from` is shorter than
534
+ * 18 bytes. `buf` is read only during the call.
535
+ *
536
+ * @param q The query whose socket the datagram arrived on, as `nextOutgoing`
537
+ * answered it.
538
+ * @param buf The datagram, from offset 0.
539
+ * @param len How many bytes of `buf` the datagram is.
540
+ * @param from The sender's 18-byte address, IPv6 form and port, compared with
541
+ * the server's.
542
+ * @param nowNanos The caller's monotonic clock now, in nanoseconds.
393
543
  */
394
544
  receive(q: i32, buf: u8[], len: i32, from: u8[], nowNanos: i64): void {
395
545
  dnsResolverCheckQuery(this, q, "receive")
@@ -437,8 +587,16 @@ export class DnsResolver {
437
587
  * `len` bytes from `buf[0]` read from query `q`'s TCP socket at
438
588
  * `nowNanos`. A length over `DNS_TCP_MAX_REPLY`, a reply that is malformed
439
589
  * or answers another question, or one still truncated fails the server,
440
- * since a stream cannot carry a second, true reply after it. Panics as
441
- * `receive` does.
590
+ * since a stream cannot carry a second, true reply after it. So does a
591
+ * length under the 12-byte header. Bytes for a query not waiting on TCP, or
592
+ * past the reply's length, are ignored. Panics when `q` is not a query or
593
+ * `len` is outside `buf`. The bytes are copied, so `buf` may be reused.
594
+ *
595
+ * @param q The query whose TCP socket the bytes came from.
596
+ * @param buf The bytes read, from offset 0, in any slicing of the stream:
597
+ * the two-byte length and the reply may arrive a byte at a time.
598
+ * @param len How many bytes of `buf` were read.
599
+ * @param nowNanos The caller's monotonic clock now, in nanoseconds.
442
600
  */
443
601
  receiveStream(q: i32, buf: u8[], len: i32, nowNanos: i64): void {
444
602
  dnsResolverCheckQuery(this, q, "receiveStream")
@@ -486,8 +644,13 @@ export class DnsResolver {
486
644
  /**
487
645
  * The driver's word that query `q`'s socket failed at `nowNanos`: it could
488
646
  * not be opened or written, the connection was refused, or the stream
489
- * ended before the reply did. The query moves to the next server. Panics
490
- * when `q` is not a query.
647
+ * ended before the reply did. The query moves to the next server, or after
648
+ * `attempts` rounds this query ends as `DNS_RESOLVE_NO_ANSWER` (its lookup
649
+ * moves to the next candidate when its other query was NXDOMAIN). A query
650
+ * not waiting on a socket ignores it. Panics when `q` is not a query.
651
+ *
652
+ * @param q The query whose socket failed.
653
+ * @param nowNanos The caller's monotonic clock now, in nanoseconds.
491
654
  */
492
655
  fail(q: i32, nowNanos: i64): void {
493
656
  dnsResolverCheckQuery(this, q, "fail")
@@ -496,7 +659,13 @@ export class DnsResolver {
496
659
  }
497
660
  }
498
661
 
499
- /** Moves every query whose try has run past its deadline at `nowNanos` to the next server. */
662
+ /**
663
+ * Moves every query whose try has reached its deadline at `nowNanos` to the
664
+ * next server, counting each in `timeouts`; a query past its last round
665
+ * ends as `DNS_RESOLVE_NO_ANSWER`. Calling it early does no harm.
666
+ *
667
+ * @param nowNanos The caller's monotonic clock now, in nanoseconds.
668
+ */
500
669
  tick(nowNanos: i64): void {
501
670
  const n: i32 = this.maxLookups * 2
502
671
  let q: i32 = 0
@@ -509,7 +678,13 @@ export class DnsResolver {
509
678
  }
510
679
  }
511
680
 
512
- /** The earliest deadline of any try in flight, on the caller's clock, or -1 when none is. */
681
+ /**
682
+ * The earliest deadline of any try in flight, so the driver knows how long
683
+ * it may wait before calling `tick`.
684
+ *
685
+ * @returns That deadline in nanoseconds on the caller's monotonic clock, or
686
+ * -1 when no try is in flight.
687
+ */
513
688
  nextDeadlineNanos(): i64 {
514
689
  const n: i32 = this.maxLookups * 2
515
690
  let soonest: i64 = -1
@@ -532,8 +707,18 @@ export class DnsResolver {
532
707
  * (A first, then AAAA, 18 bytes each, with the port `start` was given) and
533
708
  * their TTLs in seconds to `ttls`, or a negative `DNS_RESOLVE_*` code. A
534
709
  * finished lookup's slot is free again once this has answered it. Panics
535
- * when `lookup` is not a slot that `start` gave out, or when `out` or
536
- * `ttls` has no room for `DNS_RESOLVE_MAX_ADDRESSES`.
710
+ * when `lookup` is not a lookup in progress (never started, cancelled, or
711
+ * its answer already read), or when `out` or `ttls` has no room for
712
+ * `DNS_RESOLVE_MAX_ADDRESSES`.
713
+ *
714
+ * @param lookup The slot `start` answered.
715
+ * @param out Where the addresses go, from offset 0: at least
716
+ * `DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES` bytes.
717
+ * @param ttls Where each address's TTL goes, in seconds, 0 for one that
718
+ * needed no packet: at least `DNS_RESOLVE_MAX_ADDRESSES` long.
719
+ * @returns `DNS_RESOLVE_PENDING` while the lookup runs; then the address
720
+ * count, 1 to `DNS_RESOLVE_MAX_ADDRESSES`, or `DNS_RESOLVE_NOT_FOUND`,
721
+ * `DNS_RESOLVE_NO_ANSWER` or `DNS_RESOLVE_CNAME`.
537
722
  */
538
723
  result(lookup: i32, out: u8[], ttls: i32[]): i32 {
539
724
  if (lookup < 0 || lookup >= this.maxLookups || this.lookupState[lookup] === DNS_L_FREE) {
@@ -565,7 +750,11 @@ export class DnsResolver {
565
750
 
566
751
  /**
567
752
  * Ends `lookup` now, whether or not it has finished, and frees its slot;
568
- * the sockets of its queries are owed a `DNS_CLOSE`. Panics as `result`.
753
+ * each of its queries that may hold a socket is owed a `DNS_CLOSE`, which
754
+ * `nextOutgoing` hands out in its turn. Panics when `lookup` is not a lookup in
755
+ * progress, as `result` does.
756
+ *
757
+ * @param lookup The slot `start` answered.
569
758
  */
570
759
  cancel(lookup: i32): void {
571
760
  if (lookup < 0 || lookup >= this.maxLookups || this.lookupState[lookup] === DNS_L_FREE) {
@@ -576,7 +765,12 @@ export class DnsResolver {
576
765
  this.lookupState[lookup] = DNS_L_FREE
577
766
  }
578
767
 
579
- /** How many lookups are in progress or hold an answer not yet read. */
768
+ /**
769
+ * How many slots are taken.
770
+ *
771
+ * @returns How many lookups are in progress or hold an answer not yet read,
772
+ * 0 to `maxLookups`.
773
+ */
580
774
  active(): i32 {
581
775
  let count: i32 = 0
582
776
  let l: i32 = 0