nish 0.44.0 → 0.46.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 (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -342,13 +342,14 @@ export class TlsTcpServer {
342
342
  * @returns The number of slots with an open descriptor, lingering ones included; 0 to `size()`.
343
343
  */
344
344
  busy(): i32 {
345
- let n: i32 = 0
345
+ // Counted unsigned: the count is at most `fds.length`, so it cannot wrap.
346
+ let n: u32 = 0
346
347
  for (const fd of this.fds) {
347
348
  if (fd >= 0) {
348
- n = n + 1
349
+ n = n + toU32(1)
349
350
  }
350
351
  }
351
- return n
352
+ return toI32(n)
352
353
  }
353
354
 
354
355
  /**
@@ -369,7 +370,73 @@ export class TlsTcpServer {
369
370
  * holds none.
370
371
  */
371
372
  fd(slot: i32): i32 {
372
- return this.holds(slot) ? this.fds[slot] : -1
373
+ const fds: i32[] = this.fds
374
+ return slot >= 0 && slot < toI32(fds.length) && fds[slot] >= 0 ? fds[slot] : -1
375
+ }
376
+
377
+ /**
378
+ * The connection of `slot` while it holds one, or `null` for a free or
379
+ * unknown slot. Every slot below the high-water mark has a connection, so
380
+ * for a held slot this is never `null`; the guard is here, beside the
381
+ * index, so the bounds proof sees it.
382
+ */
383
+ private live(slot: i32): TlsRecordServer | null {
384
+ const fds: i32[] = this.fds
385
+ const conns: TlsRecordServer[] = this.connections
386
+ if (slot < 0 || slot >= toI32(fds.length) || slot >= toI32(conns.length) || fds[slot] < 0) {
387
+ return null
388
+ }
389
+ return conns[slot]
390
+ }
391
+
392
+ /**
393
+ * `slot`'s linger (`lingered`). `lingered` grows with `fds`, so a slot
394
+ * past it was never used; it reads as `TLS_TCP_LINGERED`, done, which is
395
+ * what every caller answers for a slot it does not hold.
396
+ */
397
+ private lingerOf(slot: i32): i32 {
398
+ const lingered: i32[] = this.lingered
399
+ return slot >= 0 && slot < toI32(lingered.length) ? lingered[slot] : TLS_TCP_LINGERED
400
+ }
401
+
402
+ /** Sets `slot`'s linger; a slot past `lingered`, never used, has none to set. */
403
+ private setLinger(slot: i32, linger: i32): void {
404
+ const lingered: i32[] = this.lingered
405
+ if (slot >= 0 && slot < toI32(lingered.length)) {
406
+ lingered[slot] = linger
407
+ }
408
+ }
409
+
410
+ /** Records activity on `slot` at `now`; a slot past `lastActive`, never used, has no clock. */
411
+ private touch(slot: i32, now: i64): void {
412
+ const lastActive: i64[] = this.lastActive
413
+ if (slot >= 0 && slot < toI32(lastActive.length)) {
414
+ lastActive[slot] = now
415
+ }
416
+ }
417
+
418
+ /**
419
+ * Sets `slot`'s descriptor, a live linger and its last activity together,
420
+ * for `accept` and `close`. Answers false, changing nothing, for a slot
421
+ * past the per-slot arrays, which grow together in `accept` and so cover
422
+ * every slot it answers.
423
+ */
424
+ private mark(slot: i32, fd: i32, now: i64): boolean {
425
+ const fds: i32[] = this.fds
426
+ const lingered: i32[] = this.lingered
427
+ const lastActive: i64[] = this.lastActive
428
+ if (
429
+ slot < 0 ||
430
+ slot >= toI32(fds.length) ||
431
+ slot >= toI32(lingered.length) ||
432
+ slot >= toI32(lastActive.length)
433
+ ) {
434
+ return false
435
+ }
436
+ fds[slot] = fd
437
+ lingered[slot] = TLS_TCP_LIVE
438
+ lastActive[slot] = now
439
+ return true
373
440
  }
374
441
 
375
442
  /**
@@ -429,9 +496,11 @@ export class TlsTcpServer {
429
496
  // first accepts take slots 0, 1, 2 ... and a pool that never fills never
430
497
  // builds its last slots.
431
498
  let slot: i32 = TLS_TCP_POOL_FULL
432
- if (this.free.length > 0) {
433
- slot = this.free[this.free.length - 1]
434
- this.free.pop()
499
+ const free: i32[] = this.free
500
+ const freeCount: i32 = toI32(free.length)
501
+ if (freeCount > 0 && free.length > 0) {
502
+ slot = free[freeCount - 1]
503
+ free.pop()
435
504
  } else if (toI32(this.fds.length) < this.slots) {
436
505
  slot = toI32(this.fds.length)
437
506
  this.fds.push(TLS_TCP_FREE)
@@ -444,10 +513,18 @@ export class TlsTcpServer {
444
513
  }
445
514
  const conn: TlsRecordServer =
446
515
  slot < toI32(this.connections.length) ? this.connections[slot] : this.build()
447
- this.fds[slot] = fd
448
- this.lingered[slot] = TLS_TCP_LIVE
449
- this.lastActive[slot] = monotonicNanos()
450
- const random: u8[] = this.randoms[slot]
516
+ // `build` pushed this slot's random if it was new, so the guard never
517
+ // fails; it sits here, after the call, so the bounds proof sees it.
518
+ const randoms: u8[][] = this.randoms
519
+ if (slot >= toI32(randoms.length)) {
520
+ netClose(fd)
521
+ return TLS_TCP_POOL_FULL
522
+ }
523
+ const random: u8[] = randoms[slot]
524
+ if (!this.mark(slot, fd, monotonicNanos())) {
525
+ netClose(fd)
526
+ return TLS_TCP_POOL_FULL
527
+ }
451
528
  for (
452
529
  let k: i32 = 0;
453
530
  k < TLS_TCP_KEY_SIZE && k < toI32(random.length) && k < toI32(serverRandom.length);
@@ -472,15 +549,15 @@ export class TlsTcpServer {
472
549
  * @returns The slot's interest: `TLS_RECORD_*` bits saying what to wait for next.
473
550
  */
474
551
  readable(slot: i32): i32 {
475
- if (!this.holds(slot)) {
552
+ const conn: TlsRecordServer | null = this.live(slot)
553
+ if (conn === null) {
476
554
  return TLS_RECORD_DONE
477
555
  }
478
- if (this.lingered[slot] !== TLS_TCP_LIVE) {
556
+ if (this.lingerOf(slot) !== TLS_TCP_LIVE) {
479
557
  return this.drain(slot)
480
558
  }
481
- this.lastActive[slot] = monotonicNanos()
482
- const fd: i32 = this.fds[slot]
483
- const conn: TlsRecordServer = this.connections[slot]
559
+ this.touch(slot, monotonicNanos())
560
+ const fd: i32 = this.fd(slot)
484
561
  while (true) {
485
562
  const room: i32 = conn.space()
486
563
  const want: i32 = room < toI32(this.scratch.length) ? room : toI32(this.scratch.length)
@@ -513,8 +590,8 @@ export class TlsTcpServer {
513
590
  }
514
591
  // What a lingering slot drains is not activity, so `idleTimeout` bounds
515
592
  // how long a client that never hangs up can hold it.
516
- if (this.lingered[slot] === TLS_TCP_LIVE) {
517
- this.lastActive[slot] = monotonicNanos()
593
+ if (this.lingerOf(slot) === TLS_TCP_LIVE) {
594
+ this.touch(slot, monotonicNanos())
518
595
  }
519
596
  return this.flush(slot)
520
597
  }
@@ -533,7 +610,7 @@ export class TlsTcpServer {
533
610
  if (!this.holds(slot)) {
534
611
  return TLS_RECORD_DONE
535
612
  }
536
- if (this.lingered[slot] !== TLS_TCP_LIVE) {
613
+ if (this.lingerOf(slot) !== TLS_TCP_LIVE) {
537
614
  return this.lingerInterest(slot)
538
615
  }
539
616
  const wants: i32 = this.send(slot)
@@ -544,10 +621,18 @@ export class TlsTcpServer {
544
621
 
545
622
  /** Writes `slot`'s output until the socket would block or it is all sent; answers the connection's `interest`. */
546
623
  private send(slot: i32): i32 {
547
- const fd: i32 = this.fds[slot]
548
- const conn: TlsRecordServer = this.connections[slot]
624
+ const conn: TlsRecordServer | null = this.live(slot)
625
+ if (conn === null) {
626
+ return TLS_RECORD_DONE
627
+ }
628
+ const fd: i32 = this.fd(slot)
549
629
  while (conn.outputEnd > conn.outputStart) {
550
- const n: i32 = netWrite(fd, conn.output, conn.outputStart, conn.outputEnd - conn.outputStart)
630
+ const n: i32 = netWrite(
631
+ fd,
632
+ conn.output,
633
+ conn.outputStart,
634
+ tlsTcpPending(conn.outputStart, conn.outputEnd)
635
+ )
551
636
  if (n <= 0) {
552
637
  if (n !== TLS_RECORD_AGAIN) {
553
638
  conn.abort()
@@ -567,17 +652,18 @@ export class TlsTcpServer {
567
652
  * (`drain`). Any other is done.
568
653
  */
569
654
  private linger(slot: i32): i32 {
570
- const conn: TlsRecordServer = this.connections[slot]
655
+ const conn: TlsRecordServer | null = this.live(slot)
571
656
  if (
657
+ conn === null ||
572
658
  conn.state !== TLS_RECORD_STATE_CLOSED ||
573
659
  conn.peerClosed ||
574
660
  conn.ended ||
575
- netShutdown(this.fds[slot], TLS_TCP_SHUT_WRITE) < 0
661
+ netShutdown(this.fd(slot), TLS_TCP_SHUT_WRITE) < 0
576
662
  ) {
577
- this.lingered[slot] = TLS_TCP_LINGERED
663
+ this.setLinger(slot, TLS_TCP_LINGERED)
578
664
  return TLS_RECORD_DONE
579
665
  }
580
- this.lingered[slot] = 0
666
+ this.setLinger(slot, toI32(0))
581
667
  conn.wipeKeys()
582
668
  conn.release()
583
669
  return this.drain(slot)
@@ -590,14 +676,20 @@ export class TlsTcpServer {
590
676
  * (`TLS_RECORD_DONE`).
591
677
  */
592
678
  private drain(slot: i32): i32 {
593
- while (this.lingered[slot] >= 0) {
594
- const n: i32 = netRead(this.fds[slot], this.scratch, TLS_TCP_FROM, toI32(this.scratch.length))
679
+ const fd: i32 = this.fd(slot)
680
+ let seen: i32 = this.lingerOf(slot)
681
+ while (seen >= 0) {
682
+ const n: i32 = netRead(fd, this.scratch, TLS_TCP_FROM, toI32(this.scratch.length))
595
683
  if (n === TLS_RECORD_AGAIN) {
684
+ this.setLinger(slot, seen)
596
685
  return TLS_RECORD_WANT_READ
597
686
  }
598
- this.lingered[slot] =
599
- n > 0 && this.lingered[slot] + n <= TLS_TCP_LINGER_LIMIT ? this.lingered[slot] + n : TLS_TCP_LINGERED
687
+ // Summed unsigned: `seen` is not negative in the loop and `n` is a
688
+ // positive count, so the sum is below 2^32 and cannot wrap.
689
+ const sum: u32 = n > 0 ? toU32(seen) + toU32(n) : toU32(0)
690
+ seen = n > 0 && sum <= toU32(TLS_TCP_LINGER_LIMIT) ? toI32(sum) : TLS_TCP_LINGERED
600
691
  }
692
+ this.setLinger(slot, seen)
601
693
  return TLS_RECORD_DONE
602
694
  }
603
695
 
@@ -610,12 +702,12 @@ export class TlsTcpServer {
610
702
  * @returns True while the slot holds a connection that is draining its client before `close`.
611
703
  */
612
704
  lingering(slot: i32): boolean {
613
- return this.holds(slot) && this.lingered[slot] >= 0
705
+ return this.holds(slot) && this.lingerOf(slot) >= 0
614
706
  }
615
707
 
616
708
  /** A lingering `slot`'s interest: to be read, or `TLS_RECORD_DONE` once its linger is over. */
617
709
  private lingerInterest(slot: i32): i32 {
618
- return this.lingered[slot] >= 0 ? TLS_RECORD_WANT_READ : TLS_RECORD_DONE
710
+ return this.lingerOf(slot) >= 0 ? TLS_RECORD_WANT_READ : TLS_RECORD_DONE
619
711
  }
620
712
 
621
713
  /**
@@ -625,12 +717,11 @@ export class TlsTcpServer {
625
717
  * @returns The slot's `TLS_RECORD_*` interest bits; the low two are the events for `pollModify`.
626
718
  */
627
719
  interest(slot: i32): i32 {
628
- if (!this.holds(slot)) {
720
+ const conn: TlsRecordServer | null = this.live(slot)
721
+ if (conn === null) {
629
722
  return TLS_RECORD_DONE
630
723
  }
631
- return this.lingered[slot] !== TLS_TCP_LIVE
632
- ? this.lingerInterest(slot)
633
- : this.connections[slot].interest()
724
+ return this.lingerOf(slot) !== TLS_TCP_LIVE ? this.lingerInterest(slot) : conn.interest()
634
725
  }
635
726
 
636
727
  /**
@@ -641,7 +732,8 @@ export class TlsTcpServer {
641
732
  * waiting for a signature.
642
733
  */
643
734
  signatureInput(slot: i32): u8[] | null {
644
- return this.holds(slot) ? this.connections[slot].signatureInput() : null
735
+ const conn: TlsRecordServer | null = this.live(slot)
736
+ return conn !== null ? conn.signatureInput() : null
645
737
  }
646
738
 
647
739
  /**
@@ -653,10 +745,11 @@ export class TlsTcpServer {
653
745
  * @returns The slot's interest after sending; `TLS_RECORD_DONE` for a free slot.
654
746
  */
655
747
  sign(slot: i32, signature: u8[]): i32 {
656
- if (!this.holds(slot)) {
748
+ const conn: TlsRecordServer | null = this.live(slot)
749
+ if (conn === null) {
657
750
  return TLS_RECORD_DONE
658
751
  }
659
- this.connections[slot].sign(signature)
752
+ conn.sign(signature)
660
753
  return this.flush(slot)
661
754
  }
662
755
 
@@ -677,8 +770,12 @@ export class TlsTcpServer {
677
770
  return this.interest(slot)
678
771
  }
679
772
  const signature: u8[] | null = this.signer.sign(key, input)
773
+ const conn: TlsRecordServer | null = this.live(slot)
774
+ if (conn === null) {
775
+ return TLS_RECORD_DONE
776
+ }
680
777
  if (signature === null) {
681
- this.connections[slot].fail(TLS_ALERT_INTERNAL_ERROR)
778
+ conn.fail(TLS_ALERT_INTERNAL_ERROR)
682
779
  return this.flush(slot)
683
780
  }
684
781
  return this.sign(slot, signature)
@@ -723,10 +820,11 @@ export class TlsTcpServer {
723
820
  strikes: TlsTicketStrikes,
724
821
  now: i64
725
822
  ): i32 {
726
- if (!this.holds(slot)) {
823
+ const conn: TlsRecordServer | null = this.live(slot)
824
+ if (conn === null) {
727
825
  return TLS_RECORD_DONE
728
826
  }
729
- this.connections[slot].resume(current, currentInfo, previous, previousInfo, strikes, now)
827
+ conn.resume(current, currentInfo, previous, previousInfo, strikes, now)
730
828
  return this.flush(slot)
731
829
  }
732
830
 
@@ -742,10 +840,11 @@ export class TlsTcpServer {
742
840
  * @returns The slot's interest after sending; `TLS_RECORD_DONE` for a free slot.
743
841
  */
744
842
  issue(slot: i32, key: Secret<u8[]>, info: TlsTicketKeyInfo, now: i64): i32 {
745
- if (!this.holds(slot)) {
843
+ const conn: TlsRecordServer | null = this.live(slot)
844
+ if (conn === null) {
746
845
  return TLS_RECORD_DONE
747
846
  }
748
- this.connections[slot].issue(key, info, now)
847
+ conn.issue(key, info, now)
749
848
  return this.flush(slot)
750
849
  }
751
850
 
@@ -764,10 +863,11 @@ export class TlsTcpServer {
764
863
  * @returns The bytes copied, or 0, -11 or -104 as above; `TLS_RECORD_INVALID` (-22) for a free slot.
765
864
  */
766
865
  read(slot: i32, buf: u8[], off: i32, len: i32): i32 {
767
- if (!this.holds(slot)) {
866
+ const conn: TlsRecordServer | null = this.live(slot)
867
+ if (conn === null) {
768
868
  return TLS_RECORD_INVALID
769
869
  }
770
- const n: i32 = this.connections[slot].read(buf, off, len)
870
+ const n: i32 = conn.read(buf, off, len)
771
871
  this.flush(slot)
772
872
  return n
773
873
  }
@@ -786,10 +886,11 @@ export class TlsTcpServer {
786
886
  * `TLS_RECORD_INVALID` (-22) for a free slot.
787
887
  */
788
888
  write(slot: i32, data: u8[], off: i32, len: i32): i32 {
789
- if (!this.holds(slot)) {
889
+ const conn: TlsRecordServer | null = this.live(slot)
890
+ if (conn === null) {
790
891
  return TLS_RECORD_INVALID
791
892
  }
792
- const n: i32 = this.connections[slot].write(data, off, len)
893
+ const n: i32 = conn.write(data, off, len)
793
894
  this.flush(slot)
794
895
  return n
795
896
  }
@@ -804,10 +905,11 @@ export class TlsTcpServer {
804
905
  * `TlsRecordServer.keyUpdate` says; `TLS_RECORD_INVALID` (-22) for a free slot.
805
906
  */
806
907
  keyUpdate(slot: i32, requestPeer: boolean): i32 {
807
- if (!this.holds(slot)) {
908
+ const conn: TlsRecordServer | null = this.live(slot)
909
+ if (conn === null) {
808
910
  return TLS_RECORD_INVALID
809
911
  }
810
- const r: i32 = this.connections[slot].keyUpdate(requestPeer)
912
+ const r: i32 = conn.keyUpdate(requestPeer)
811
913
  this.flush(slot)
812
914
  return r
813
915
  }
@@ -825,10 +927,11 @@ export class TlsTcpServer {
825
927
  * wait for.
826
928
  */
827
929
  shutdown(slot: i32): i32 {
828
- if (!this.holds(slot)) {
930
+ const conn: TlsRecordServer | null = this.live(slot)
931
+ if (conn === null) {
829
932
  return TLS_RECORD_DONE
830
933
  }
831
- this.connections[slot].close()
934
+ conn.close()
832
935
  return this.flush(slot)
833
936
  }
834
937
 
@@ -843,19 +946,17 @@ export class TlsTcpServer {
843
946
  * @param slot The slot to free; a free or unknown slot is left alone.
844
947
  */
845
948
  close(slot: i32): void {
846
- if (!this.holds(slot)) {
949
+ const conn: TlsRecordServer | null = this.live(slot)
950
+ if (conn === null) {
847
951
  return
848
952
  }
849
- const conn: TlsRecordServer = this.connections[slot]
850
- if (this.lingered[slot] === TLS_TCP_LIVE) {
953
+ if (this.lingerOf(slot) === TLS_TCP_LIVE) {
851
954
  conn.close()
852
955
  this.send(slot)
853
956
  }
854
- netClose(this.fds[slot])
855
- this.fds[slot] = TLS_TCP_FREE
856
- this.lingered[slot] = TLS_TCP_LIVE
957
+ netClose(this.fd(slot))
958
+ this.mark(slot, TLS_TCP_FREE, toI64(0))
857
959
  this.free.push(slot)
858
- this.lastActive[slot] = toI64(0)
859
960
  conn.wipeKeys()
860
961
  // Whatever the last flush could not send is dropped with its buffer.
861
962
  conn.release()
@@ -871,7 +972,8 @@ export class TlsTcpServer {
871
972
  * already taken for a slot this closes would otherwise be read as the next
872
973
  * connection's, once `accept` reuses the slot. It is an idle limit, not a
873
974
  * handshake deadline: a client that sends a byte inside every
874
- * `idleTimeout` keeps its slot.
975
+ * `idleTimeout` keeps its slot. A `now` before a slot's last activity
976
+ * does not idle it.
875
977
  *
876
978
  * @param now The time now, in `monotonicNanos()`, which each slot's last activity is judged against.
877
979
  * @returns How many slots this call closed; 0 while `idleTimeout` is 0 or less.
@@ -881,14 +983,31 @@ export class TlsTcpServer {
881
983
  return 0
882
984
  }
883
985
  const limit: i64 = toI64(this.idleTimeout) * TLS_TCP_NANOS_PER_MS
884
- let n: i32 = 0
986
+ // Counted unsigned: at most one per slot, so it cannot wrap.
987
+ let n: u32 = 0
885
988
  for (let k: i32 = 0; k < toI32(this.fds.length); k++) {
886
- if (this.holds(k) && now - this.lastActive[k] > limit) {
989
+ if (this.idle(k, now, limit)) {
887
990
  this.close(k)
888
- n = n + 1
991
+ n = n + toU32(1)
889
992
  }
890
993
  }
891
- return n
994
+ return toI32(n)
995
+ }
996
+
997
+ /**
998
+ * Whether `slot` holds a connection whose last activity is more than
999
+ * `limit` nanoseconds before `now`. The gap is taken unsigned once `now`
1000
+ * is past the last activity, where it is exact, so a `now` from far
1001
+ * outside the clock's range cannot overflow it.
1002
+ */
1003
+ private idle(slot: i32, now: i64, limit: i64): boolean {
1004
+ const fds: i32[] = this.fds
1005
+ const lastActive: i64[] = this.lastActive
1006
+ if (slot < 0 || slot >= toI32(fds.length) || slot >= toI32(lastActive.length) || fds[slot] < 0) {
1007
+ return false
1008
+ }
1009
+ const last: i64 = lastActive[slot]
1010
+ return now > last && toU64(now) - toU64(last) > toU64(limit)
892
1011
  }
893
1012
  }
894
1013
 
@@ -1108,7 +1227,12 @@ export class TlsTcpClient {
1108
1227
  }
1109
1228
  const conn: TlsRecordClient = this.conn
1110
1229
  while (conn.outputEnd > conn.outputStart) {
1111
- const n: i32 = netWrite(this.socket, conn.output, conn.outputStart, conn.outputEnd - conn.outputStart)
1230
+ const n: i32 = netWrite(
1231
+ this.socket,
1232
+ conn.output,
1233
+ conn.outputStart,
1234
+ tlsTcpPending(conn.outputStart, conn.outputEnd)
1235
+ )
1112
1236
  if (n <= 0) {
1113
1237
  if (n !== TLS_RECORD_AGAIN) {
1114
1238
  conn.abort()
@@ -1214,9 +1338,12 @@ export class TlsTcpClient {
1214
1338
  let result: i32 = 0
1215
1339
  while (true) {
1216
1340
  if (done < len) {
1217
- const n: i32 = this.write(data, off + done, len - done)
1341
+ // Wrapping sums, exact while they fit: `done` runs from 0 up to `len`,
1342
+ // so the rest is exact, and an `off + done` past `i32` comes out
1343
+ // negative, a window `write` refuses with -22 rather than a panic.
1344
+ const n: i32 = this.write(data, toI32(toU32(off) + toU32(done)), toI32(toU32(len) - toU32(done)))
1218
1345
  if (n >= 0) {
1219
- done = done + n
1346
+ done = toI32(toU32(done) + toU32(n))
1220
1347
  } else if (n !== TLS_RECORD_AGAIN) {
1221
1348
  result = n
1222
1349
  break
@@ -1289,15 +1416,19 @@ export class TlsTcpClient {
1289
1416
  }
1290
1417
  pollModify(loop, this.socket, wants & TLS_TCP_POLL_EVENTS, TLS_TCP_TOKEN)
1291
1418
  const ready: i32[] = new Array<i32>(2)
1292
- const count: i32 = pollWait(
1293
- loop,
1294
- ready,
1295
- toI32((end - now + TLS_TCP_NANOS_PER_MS - 1) / TLS_TCP_NANOS_PER_MS)
1296
- )
1419
+ // The wait rounded up to whole milliseconds, taken unsigned: `end` is
1420
+ // past `now`, so the gap is exact, and dividing first cannot overflow.
1421
+ // The divisor is the literal of `TLS_TCP_NANOS_PER_MS`, which the
1422
+ // divide proof needs to see.
1423
+ const wait: u64 = toU64(end) - toU64(now)
1424
+ const ms: u64 = wait / toU64(1000000) + (wait % toU64(1000000) !== toU64(0) ? toU64(1) : toU64(0))
1425
+ const count: i32 = pollWait(loop, ready, toI32(ms))
1297
1426
  if (count < 0) {
1298
1427
  return count
1299
1428
  }
1300
- if (count > 0) {
1429
+ // `ready` is the two entries made above; the length is checked again
1430
+ // because `pollWait` wrote it.
1431
+ if (count > 0 && toI32(ready.length) > 1) {
1301
1432
  if ((ready[1] & TLS_TCP_READ_EVENTS) !== 0) {
1302
1433
  this.readable()
1303
1434
  } else {
@@ -1373,8 +1504,20 @@ export class TlsTcpClient {
1373
1504
  }
1374
1505
  }
1375
1506
 
1376
- /** The monotonic clock `timeoutMs` milliseconds from now. */
1377
- const tlsTcpDeadline = (timeoutMs: i32): i64 => monotonicNanos() + toI64(timeoutMs) * TLS_TCP_NANOS_PER_MS
1507
+ /**
1508
+ * The monotonic clock `timeoutMs` milliseconds from now. Added wrapping:
1509
+ * the clock counts up from boot, far below 2^62, and an `i32` of
1510
+ * milliseconds is below 2^52 nanoseconds either way, so the sum is exact.
1511
+ */
1512
+ const tlsTcpDeadline = (timeoutMs: i32): i64 =>
1513
+ toI64(toU64(monotonicNanos()) + toU64(toI64(timeoutMs) * TLS_TCP_NANOS_PER_MS))
1514
+
1515
+ /**
1516
+ * How many output bytes wait between `start` and `end`, for a loop that runs
1517
+ * while `end > start`. Taken unsigned: a record layer's output start is never
1518
+ * negative, so the gap is exact and cannot overflow.
1519
+ */
1520
+ const tlsTcpPending = (start: i32, end: i32): i32 => toI32(toU32(end) - toU32(start))
1378
1521
 
1379
1522
  /**
1380
1523
  * Opens a TLS 1.3 connection to the server at `addr` (`nish:net`'s 18-byte