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/net/dns.ts CHANGED
@@ -78,6 +78,7 @@ import {
78
78
  DNS_RESOLVE_MAX_ADDRESSES,
79
79
  DNS_RESOLVE_PENDING,
80
80
  DNS_RESOLVE_TIMEOUT,
81
+ DNS_RESOLVER_MAX_LOOKUPS,
81
82
  DNS_SEND_TCP,
82
83
  DnsResolver,
83
84
  } from "nish/net/dns-resolver"
@@ -174,7 +175,8 @@ export class DnsClient {
174
175
  /**
175
176
  * A client for `resolver` whose sockets join the loop `loop` under tokens
176
177
  * from `tokenBase`. Every buffer the client uses is made here, one TCP
177
- * output buffer per query among them. Panics when `tokenBase` is negative.
178
+ * output buffer per query among them. Panics when `tokenBase` is negative,
179
+ * or when `resolver.maxLookups` was written outside 1 to `DNS_RESOLVER_MAX_LOOKUPS`.
178
180
  *
179
181
  * @param resolver The core the client drives; the client owns it from now on, so go through
180
182
  * the client's `start`, `result` and `cancel` rather than the resolver's.
@@ -186,21 +188,29 @@ export class DnsClient {
186
188
  if (tokenBase < 0) {
187
189
  panic(`DnsClient: a token base of ${tokenBase} is negative`)
188
190
  }
189
- const n: i32 = resolver.maxLookups * 2
191
+ // `DnsResolver`'s constructor holds `maxLookups` to this range; only a
192
+ // write to the field leaves it, and the resolver's own arrays are then the
193
+ // wrong size too. Inside it the `u16` count of slots, at most 128, is exact.
194
+ const lookups: i32 = resolver.maxLookups
195
+ if (lookups < 1 || lookups > DNS_RESOLVER_MAX_LOOKUPS) {
196
+ panic(`DnsClient: ${lookups} lookups is outside 1 to ${DNS_RESOLVER_MAX_LOOKUPS}`)
197
+ }
198
+ const slots: u16 = toU16(lookups) * 2
199
+ const n: i32 = toI32(slots)
190
200
  this.resolver = resolver
191
201
  this.loop = loop
192
202
  this.tokenBase = tokenBase
193
- this.fds = new Array<i32>(n)
203
+ this.fds = new Array<i32>(slots)
194
204
  this.fds.fill(-1)
195
- this.fdState = new Array<i32>(n)
205
+ this.fdState = new Array<i32>(slots)
196
206
  this.outputs = []
197
207
  let q: i32 = 0
198
208
  while (q < n) {
199
209
  this.outputs.push(new Array<u8>(DNS_OUTGOING_MAX_BYTES))
200
210
  q += 1
201
211
  }
202
- this.outputLength = new Array<i32>(n)
203
- this.outputSent = new Array<i32>(n)
212
+ this.outputLength = new Array<i32>(slots)
213
+ this.outputSent = new Array<i32>(slots)
204
214
  this.out = new Array<u8>(DNS_OUTGOING_MAX_BYTES)
205
215
  this.to = new Array<u8>(DNS_ADDRESS_BYTES)
206
216
  this.meta = new Array<i32>(2)
@@ -217,7 +227,9 @@ export class DnsClient {
217
227
  * @returns The width of the client's token range.
218
228
  */
219
229
  tokens(): i32 {
220
- return this.resolver.maxLookups * 2
230
+ // The constructor made one socket slot per query, so this is
231
+ // `maxLookups * 2` without the multiply.
232
+ return toI32(this.fds.length)
221
233
  }
222
234
 
223
235
  /**
@@ -228,7 +240,9 @@ export class DnsClient {
228
240
  * @returns True when `token` lies in `tokenBase` to `tokenBase + tokens() - 1`.
229
241
  */
230
242
  owns(token: i32): boolean {
231
- return token >= this.tokenBase && token - this.tokenBase < this.tokens()
243
+ // In i64, where the difference of two i32s cannot overflow.
244
+ const q: i64 = toI64(token) - toI64(this.tokenBase)
245
+ return q >= 0 && q < toI64(this.tokens())
232
246
  }
233
247
 
234
248
  /**
@@ -308,15 +322,20 @@ export class DnsClient {
308
322
  * @param nowNanos The caller's monotonic clock now, in nanoseconds.
309
323
  */
310
324
  ready(token: i32, events: i32, nowNanos: i64): void {
311
- if (!this.owns(token)) {
325
+ // `owns`, inline, so the slot it finds is the one indexed below. The
326
+ // per-query arrays are all made `tokens()` long.
327
+ const fds: i32[] = this.fds
328
+ const fdState: i32[] = this.fdState
329
+ const slot: i64 = toI64(token) - toI64(this.tokenBase)
330
+ const q: i32 = slot >= 0 && slot < toI64(fds.length) ? toI32(slot) : -1
331
+ if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
312
332
  panic(`DnsClient.ready: the token ${token} is not the client's`)
313
333
  }
314
- const q: i32 = token - this.tokenBase
315
- const fd: i32 = this.fds[q]
334
+ const fd: i32 = fds[q]
316
335
  if (fd < 0) {
317
336
  return
318
337
  }
319
- const state: i32 = this.fdState[q]
338
+ const state: i32 = fdState[q]
320
339
  if (state === DNS_FD_UDP) {
321
340
  dnsClientReadDatagrams(this, q, fd, nowNanos)
322
341
  } else if (state === DNS_FD_TCP_WRITE) {
@@ -356,7 +375,10 @@ export class DnsClient {
356
375
  }
357
376
  let wait: i64 = 0
358
377
  if (deadline > nowNanos) {
359
- wait = (deadline - nowNanos + 999999) / 1000000
378
+ // The gap is above 0 and below 2^64, so `u64` holds it exactly where an
379
+ // `i64` difference could overflow; rounded up, it is below 2^45 ms.
380
+ const gap: u64 = toU64(deadline) - toU64(nowNanos)
381
+ wait = toI64((gap - 1) / 1000000 + 1)
360
382
  }
361
383
  if (ownMs >= 0 && toI64(ownMs) < wait) {
362
384
  return ownMs
@@ -372,11 +394,16 @@ export class DnsClient {
372
394
  let q: i32 = this.nextOutgoing()
373
395
  while (q >= 0 && q < toI32(this.fds.length)) {
374
396
  dnsClientClose(this, q)
375
- const kind: i32 = this.meta[0]
376
- if (kind === DNS_SEND_TCP) {
377
- dnsClientOpenTcp(this, q, nowNanos)
378
- } else if (kind !== DNS_CLOSE) {
379
- dnsClientSendUdp(this, q, nowNanos)
397
+ // `meta` is made two long and the core has just written it; a shorter
398
+ // one, never seen, would leave only the close done.
399
+ const meta: i32[] = this.meta
400
+ if (meta.length > 0) {
401
+ const kind: i32 = meta[0]
402
+ if (kind === DNS_SEND_TCP) {
403
+ dnsClientOpenTcp(this, q, nowNanos)
404
+ } else if (kind !== DNS_CLOSE) {
405
+ dnsClientSendUdp(this, q, nowNanos)
406
+ }
380
407
  }
381
408
  q = this.nextOutgoing()
382
409
  }
@@ -392,7 +419,11 @@ export class DnsClient {
392
419
  crypto.getRandomValues(this.idBytes)
393
420
  this.idFresh = true
394
421
  }
395
- const id: i32 = (toI32(this.idBytes[0]) << 8) | toI32(this.idBytes[1])
422
+ const idBytes: u8[] = this.idBytes
423
+ if (idBytes.length < 2) {
424
+ return -1 // never taken: the constructor makes `idBytes` two long and nothing replaces it
425
+ }
426
+ const id: i32 = (toI32(idBytes[0]) << 8) | toI32(idBytes[1])
396
427
  const q: i32 = this.resolver.nextOutgoing(this.out, this.to, this.meta, id)
397
428
  if (q >= 0 && this.meta.length > 0 && this.meta[0] !== DNS_CLOSE) {
398
429
  this.idFresh = false
@@ -401,13 +432,22 @@ export class DnsClient {
401
432
  }
402
433
  }
403
434
 
404
- /** Closes query `q`'s socket, if it has one. */
435
+ /**
436
+ * Closes query `q`'s socket, if it has one. Every `q` here is one `pump`
437
+ * bounded by `fds`, and the per-query arrays are all one length, so the
438
+ * range test is never taken.
439
+ */
405
440
  const dnsClientClose = (c: DnsClient, q: i32): void => {
406
- const fd: i32 = c.fds[q]
441
+ const fds: i32[] = c.fds
442
+ const fdState: i32[] = c.fdState
443
+ if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
444
+ return
445
+ }
446
+ const fd: i32 = fds[q]
407
447
  if (fd >= 0) {
408
448
  netClose(fd)
409
- c.fds[q] = -1
410
- c.fdState[q] = DNS_FD_NONE
449
+ fds[q] = -1
450
+ fdState[q] = DNS_FD_NONE
411
451
  }
412
452
  }
413
453
 
@@ -433,6 +473,37 @@ const dnsClientIsV4 = (to: u8[]): boolean => {
433
473
  /** `n` plus one, held at the i32 maximum, so a counter never overflows. */
434
474
  const dnsClientCount = (n: i32): i32 => (n < 2147483647 ? n + 1 : n)
435
475
 
476
+ /**
477
+ * Records `fd` as query `q`'s socket in `state`, or answers false for a `q`
478
+ * outside the per-query arrays, which `pump` never passes.
479
+ */
480
+ const dnsClientTake = (c: DnsClient, q: i32, fd: i32, state: i32): boolean => {
481
+ const fds: i32[] = c.fds
482
+ const fdState: i32[] = c.fdState
483
+ if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
484
+ return false
485
+ }
486
+ fds[q] = fd
487
+ fdState[q] = state
488
+ return true
489
+ }
490
+
491
+ /**
492
+ * Watches `fd` on the client's loop for `events` under query `q`'s token,
493
+ * adding it or, with `modify`, changing its events. False when that fails,
494
+ * as for a token past the i32 maximum, which a `tokenBase` too near it gives.
495
+ */
496
+ const dnsClientWatch = (c: DnsClient, q: i32, fd: i32, events: i32, modify: boolean): boolean => {
497
+ const token: i64 = toI64(c.tokenBase) + toI64(q)
498
+ if (token > 2147483647) {
499
+ return false
500
+ }
501
+ if (modify) {
502
+ return pollModify(c.loop, fd, events, toI32(token)) === 0
503
+ }
504
+ return pollAdd(c.loop, fd, events, toI32(token)) === 0
505
+ }
506
+
436
507
  /** Sends `c.out` to `c.to` from a new socket on a port the kernel picks, watched for reading. */
437
508
  const dnsClientSendUdp = (c: DnsClient, q: i32, nowNanos: i64): void => {
438
509
  const fd: i32 = udpBind(dnsClientIsV4(c.to) ? "0.0.0.0" : "::", 0, 0)
@@ -441,12 +512,20 @@ const dnsClientSendUdp = (c: DnsClient, q: i32, nowNanos: i64): void => {
441
512
  return
442
513
  }
443
514
  c.socketsOpened = dnsClientCount(c.socketsOpened)
444
- c.fds[q] = fd
445
- c.fdState[q] = DNS_FD_UDP
446
- const length: i32 = c.meta[1]
515
+ if (!dnsClientTake(c, q, fd, DNS_FD_UDP)) {
516
+ netClose(fd)
517
+ return
518
+ }
519
+ // The core writes `meta[1]` with every send; a `meta` too short for it, or
520
+ // a length outside `out`, never seen, fails the try as a short send would.
521
+ const meta: i32[] = c.meta
522
+ const out: u8[] = c.out
523
+ const length: i32 = meta.length > 1 ? meta[1] : -1
447
524
  if (
448
- udpSendTo(fd, c.out, 0, length, c.to, 0, 0) !== length ||
449
- pollAdd(c.loop, fd, 1, c.tokenBase + q) !== 0
525
+ length < 0 ||
526
+ length > toI32(out.length) ||
527
+ udpSendTo(fd, out, 0, length, c.to, 0, 0) !== length ||
528
+ !dnsClientWatch(c, q, fd, 1, false)
450
529
  ) {
451
530
  dnsClientFail(c, q, nowNanos)
452
531
  }
@@ -460,18 +539,33 @@ const dnsClientOpenTcp = (c: DnsClient, q: i32, nowNanos: i64): void => {
460
539
  return
461
540
  }
462
541
  c.socketsOpened = dnsClientCount(c.socketsOpened)
463
- c.fds[q] = fd
464
- c.fdState[q] = DNS_FD_TCP_WRITE
465
- const length: i32 = c.meta[1]
466
- const output: u8[] = c.outputs[q]
542
+ if (!dnsClientTake(c, q, fd, DNS_FD_TCP_WRITE)) {
543
+ netClose(fd)
544
+ return
545
+ }
546
+ const outputs: u8[][] = c.outputs
547
+ const outputLength: i32[] = c.outputLength
548
+ const outputSent: i32[] = c.outputSent
549
+ if (q >= toI32(outputs.length) || q >= toI32(outputLength.length) || q >= toI32(outputSent.length)) {
550
+ dnsClientFail(c, q, nowNanos) // never taken: every per-query array is `tokens()` long
551
+ return
552
+ }
553
+ const meta: i32[] = c.meta
554
+ if (meta.length < 2) {
555
+ dnsClientFail(c, q, nowNanos) // never taken, as in `dnsClientSendUdp`
556
+ return
557
+ }
558
+ const length: i32 = meta[1]
559
+ const out: u8[] = c.out
560
+ const output: u8[] = outputs[q]
467
561
  let i: i32 = 0
468
- while (i < length && i < toI32(output.length) && i < toI32(c.out.length)) {
469
- output[i] = c.out[i]
562
+ while (i < length && i < toI32(output.length) && i < toI32(out.length)) {
563
+ output[i] = out[i]
470
564
  i += 1
471
565
  }
472
- c.outputLength[q] = length
473
- c.outputSent[q] = 0
474
- if (pollAdd(c.loop, fd, 2, c.tokenBase + q) !== 0) {
566
+ outputLength[q] = length
567
+ outputSent[q] = 0
568
+ if (!dnsClientWatch(c, q, fd, 2, false)) {
475
569
  dnsClientFail(c, q, nowNanos)
476
570
  }
477
571
  }
@@ -482,7 +576,12 @@ const dnsClientOpenTcp = (c: DnsClient, q: i32, nowNanos: i64): void => {
482
576
  */
483
577
  const dnsClientReadDatagrams = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): void => {
484
578
  let read: i32 = 0
485
- while (c.fds[q] === fd && read < DNS_CLIENT_MAX_DATAGRAMS) {
579
+ while (read < DNS_CLIENT_MAX_DATAGRAMS) {
580
+ // Read again each time: the core may end the try, or another open its socket.
581
+ const fds: i32[] = c.fds
582
+ if (q < 0 || q >= toI32(fds.length) || fds[q] !== fd) {
583
+ return
584
+ }
486
585
  const n: i32 = udpRecvFrom(fd, c.buf, 0, DNS_UDP_MAX_REPLY, c.from, c.recvMeta)
487
586
  if (n < 0) {
488
587
  return
@@ -499,23 +598,55 @@ const dnsClientReadDatagrams = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): v
499
598
  * is written, the socket watched for reading instead.
500
599
  */
501
600
  const dnsClientWrite = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): void => {
502
- if (c.outputSent[q] === 0 && connectResult(fd) !== 0) {
601
+ const first: i32[] = c.outputSent
602
+ if (q < 0 || q >= toI32(first.length)) {
603
+ return // never taken: `ready` bounds `q` by the per-query arrays
604
+ }
605
+ if (first[q] === 0 && connectResult(fd) !== 0) {
503
606
  dnsClientFail(c, q, nowNanos)
504
607
  return
505
608
  }
506
- while (c.outputSent[q] < c.outputLength[q]) {
507
- const n: i32 = netWrite(fd, c.outputs[q], c.outputSent[q], c.outputLength[q] - c.outputSent[q])
609
+ while (true) {
610
+ // Each call below may change what the fields hold, so each turn reads
611
+ // them again.
612
+ const outputs: u8[][] = c.outputs
613
+ const outputLength: i32[] = c.outputLength
614
+ const outputSent: i32[] = c.outputSent
615
+ if (q >= toI32(outputs.length) || q >= toI32(outputLength.length) || q >= toI32(outputSent.length)) {
616
+ return // never taken, as above
617
+ }
618
+ const output: u8[] = outputs[q]
619
+ const length: i32 = outputLength[q]
620
+ const sent: i32 = outputSent[q]
621
+ if (sent >= length) {
622
+ break
623
+ }
624
+ // `dnsClientOpenTcp` keeps a length the core wrote, at most
625
+ // `DNS_OUTGOING_MAX_BYTES`, the size of `output`; one outside it, never
626
+ // seen, fails the try as a failed write does.
627
+ if (sent < 0 || length < 0 || length > toI32(output.length)) {
628
+ dnsClientFail(c, q, nowNanos)
629
+ return
630
+ }
631
+ const n: i32 = netWrite(fd, output, sent, length - sent)
508
632
  if (n === -11) {
509
633
  return
510
634
  }
511
- if (n <= 0) {
635
+ // A write never takes more than it is given; in i64 the sum cannot overflow.
636
+ const next: i64 = toI64(sent) + toI64(n)
637
+ if (n <= 0 || next > toI64(length)) {
512
638
  dnsClientFail(c, q, nowNanos)
513
639
  return
514
640
  }
515
- c.outputSent[q] = c.outputSent[q] + n
641
+ if (q < toI32(outputSent.length)) {
642
+ outputSent[q] = toI32(next)
643
+ }
516
644
  }
517
- c.fdState[q] = DNS_FD_TCP_READ
518
- if (pollModify(c.loop, fd, 1, c.tokenBase + q) !== 0) {
645
+ const fdState: i32[] = c.fdState
646
+ if (q < toI32(fdState.length)) {
647
+ fdState[q] = DNS_FD_TCP_READ
648
+ }
649
+ if (!dnsClientWatch(c, q, fd, 1, true)) {
519
650
  dnsClientFail(c, q, nowNanos)
520
651
  }
521
652
  }
@@ -527,7 +658,15 @@ const dnsClientWrite = (c: DnsClient, q: i32, fd: i32, nowNanos: i64): void => {
527
658
  * the reading, though the next one may open its socket on the same fd.
528
659
  */
529
660
  const dnsClientReadStream = (c: DnsClient, q: i32, fd: i32, hungUp: boolean, nowNanos: i64): void => {
530
- while (c.fds[q] === fd && c.fdState[q] === DNS_FD_TCP_READ) {
661
+ while (true) {
662
+ const fds: i32[] = c.fds
663
+ const fdState: i32[] = c.fdState
664
+ if (q < 0 || q >= toI32(fds.length) || q >= toI32(fdState.length)) {
665
+ return // never taken: `ready` bounds `q` by the per-query arrays
666
+ }
667
+ if (fds[q] !== fd || fdState[q] !== DNS_FD_TCP_READ) {
668
+ return
669
+ }
531
670
  const n: i32 = netRead(fd, c.buf, 0, DNS_UDP_MAX_REPLY)
532
671
  if (n === -11 && !hungUp) {
533
672
  return
@@ -640,7 +779,10 @@ export const dnsLookupWith = (
640
779
  }
641
780
  const client: DnsClient = new DnsClient(new DnsResolver(config, hosts, 1), loop, 0)
642
781
  const begun: i64 = monotonicNanos()
643
- const end: i64 = begun + toI64(timeoutMs) * 1000000
782
+ // The budget and the time spent are kept apart, where `begun` plus the
783
+ // budget could overflow: `now >= end` is `spent >= budget`. The clock only
784
+ // moves on, so `spent` is its true value, which `u64` holds exactly.
785
+ const budget: i64 = toI64(timeoutMs) * 1000000
644
786
  const lookup: i32 = client.start(name, port, begun)
645
787
  if (lookup < 0) {
646
788
  netClose(loop)
@@ -650,7 +792,8 @@ export const dnsLookupWith = (
650
792
  let status: i32 = client.result(lookup, out, ttls)
651
793
  while (status === DNS_RESOLVE_PENDING) {
652
794
  const now: i64 = monotonicNanos()
653
- if (now >= end) {
795
+ const spent: i64 = toI64(toU64(now) - toU64(begun))
796
+ if (spent >= budget) {
654
797
  if (client.settle(lookup, now)) {
655
798
  status = client.result(lookup, out, ttls)
656
799
  } else {
@@ -659,7 +802,9 @@ export const dnsLookupWith = (
659
802
  }
660
803
  break
661
804
  }
662
- const own: i32 = toI32((end - now + 999999) / 1000000)
805
+ // `end - now`, above 0 here, in milliseconds rounded up.
806
+ const left: u64 = toU64(budget) - toU64(spent)
807
+ const own: i32 = toI32(toI64((left - 1) / 1000000 + 1))
663
808
  const count: i32 = pollWait(loop, ready, client.timeoutMs(now, own))
664
809
  if (count < 0) {
665
810
  // A signal answers 0, so a negative count is the loop failing: waiting again would spin.
@@ -668,10 +813,18 @@ export const dnsLookupWith = (
668
813
  break
669
814
  }
670
815
  const after: i64 = monotonicNanos()
816
+ // `ready` holds four pairs, so `k < 4` is the old `2 * k + 1 < ready.length`.
671
817
  let k: i32 = 0
672
- while (k < count && 2 * k + 1 < toI32(ready.length)) {
673
- if (client.owns(ready[2 * k])) {
674
- client.ready(ready[2 * k], ready[2 * k + 1], after)
818
+ while (k < count && k < 4) {
819
+ const at: i32 = 2 * k
820
+ const next: i32 = at + 1
821
+ if (at >= toI32(ready.length) || next >= toI32(ready.length)) {
822
+ break
823
+ }
824
+ const token: i32 = ready[at]
825
+ const events: i32 = ready[next]
826
+ if (client.owns(token)) {
827
+ client.ready(token, events, after)
675
828
  }
676
829
  k += 1
677
830
  }