@amritk/nish-x86_64-linux 0.34.1 → 0.36.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 (126) hide show
  1. package/INSTALL.md +22 -13
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +86 -16
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +49 -0
  6. package/runtime/async.mjs +27 -0
  7. package/runtime/nish.d.ts +4681 -427
  8. package/runtime/nish.h +6 -2
  9. package/runtime/nish.mjs +4 -0
  10. package/runtime/runtime-net.c +22 -0
  11. package/runtime/runtime-shm.c +93 -2
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +108 -13
  15. package/std/README.md +131 -131
  16. package/std/base64.ts +34 -5
  17. package/std/bytes.ts +258 -42
  18. package/std/cli-args.ts +296 -70
  19. package/std/cli-flags.ts +184 -19
  20. package/std/cli.ts +475 -54
  21. package/std/collections.ts +121 -23
  22. package/std/compress/checksum.ts +44 -14
  23. package/std/compress/deflate.ts +224 -111
  24. package/std/compress/gzip.ts +217 -53
  25. package/std/compress/zlib.ts +63 -7
  26. package/std/crypto/aes.ts +80 -9
  27. package/std/crypto/base64url.ts +22 -0
  28. package/std/crypto/bcrypt-batch.ts +88 -21
  29. package/std/crypto/bcrypt.ts +60 -0
  30. package/std/crypto/chacha20poly1305.ts +53 -0
  31. package/std/crypto/ct.ts +32 -13
  32. package/std/crypto/ed25519.ts +47 -0
  33. package/std/crypto/hkdf.ts +198 -13
  34. package/std/crypto/hmac.ts +235 -31
  35. package/std/crypto/jwt.ts +192 -63
  36. package/std/crypto/p256.ts +159 -16
  37. package/std/crypto/p384.ts +28 -3
  38. package/std/crypto/pki-roots.ts +32 -2
  39. package/std/crypto/pki.ts +81 -15
  40. package/std/crypto/rsa.ts +33 -3
  41. package/std/crypto/sha1.ts +3 -0
  42. package/std/crypto/sha256.ts +24 -1
  43. package/std/crypto/sha512.ts +99 -14
  44. package/std/crypto/x25519.ts +152 -14
  45. package/std/crypto/x509-random.ts +10 -0
  46. package/std/crypto/x509.ts +67 -7
  47. package/std/json-equal.ts +19 -0
  48. package/std/json-format.ts +112 -13
  49. package/std/json-number.ts +64 -14
  50. package/std/json-pointer.ts +23 -1
  51. package/std/json-tokenizer.ts +100 -29
  52. package/std/json-typed.ts +713 -66
  53. package/std/json-value.ts +400 -124
  54. package/std/json.ts +138 -10
  55. package/std/map.ts +25 -7
  56. package/std/math.ts +36 -4
  57. package/std/net/buffer-pool.ts +42 -11
  58. package/std/net/dns-config.ts +109 -23
  59. package/std/net/dns-message.ts +62 -7
  60. package/std/net/dns-resolver.ts +247 -53
  61. package/std/net/dns.ts +203 -35
  62. package/std/net/fetch.ts +151 -61
  63. package/std/net/handler.ts +278 -80
  64. package/std/net/hpack.ts +262 -52
  65. package/std/net/http-compress.ts +125 -15
  66. package/std/net/http-fields.ts +85 -9
  67. package/std/net/http1-client-codec.ts +100 -19
  68. package/std/net/http1-codec.ts +297 -77
  69. package/std/net/http1-server.ts +430 -94
  70. package/std/net/http2-frame.ts +272 -35
  71. package/std/net/http2-tls.ts +135 -34
  72. package/std/net/http2.ts +247 -106
  73. package/std/net/http3-frame.ts +154 -17
  74. package/std/net/http3-server.ts +275 -101
  75. package/std/net/http3.ts +307 -115
  76. package/std/net/outbound.ts +37 -3
  77. package/std/net/postgres-carrier.ts +144 -8
  78. package/std/net/postgres.ts +801 -112
  79. package/std/net/prefork.ts +164 -31
  80. package/std/net/qpack.ts +105 -18
  81. package/std/net/quic-conn-ack.ts +59 -12
  82. package/std/net/quic-conn-cid.ts +124 -27
  83. package/std/net/quic-conn-params.ts +70 -10
  84. package/std/net/quic-datagram.ts +43 -3
  85. package/std/net/quic-frame.ts +286 -12
  86. package/std/net/quic-listener.ts +101 -32
  87. package/std/net/quic-packet.ts +302 -21
  88. package/std/net/quic-recovery.ts +264 -52
  89. package/std/net/quic-stream.ts +353 -91
  90. package/std/net/quic.ts +1124 -201
  91. package/std/net/router.ts +87 -40
  92. package/std/net/tls/client.ts +166 -51
  93. package/std/net/tls/codec.ts +413 -56
  94. package/std/net/tls/record-client.ts +110 -55
  95. package/std/net/tls/record-server.ts +159 -59
  96. package/std/net/tls/record.ts +109 -12
  97. package/std/net/tls/schedule.ts +235 -26
  98. package/std/net/tls/ticket.ts +159 -40
  99. package/std/net/tls/verify.ts +49 -3
  100. package/std/net/tls-tcp.ts +250 -45
  101. package/std/net/tls.ts +221 -59
  102. package/std/net/upstream.ts +81 -45
  103. package/std/net/url.ts +97 -13
  104. package/std/net/websocket.ts +122 -12
  105. package/std/net/webtransport.ts +220 -89
  106. package/std/pair.ts +10 -2
  107. package/std/random.ts +34 -9
  108. package/std/regex.ts +352 -198
  109. package/std/region.ts +70 -15
  110. package/std/secret.ts +52 -11
  111. package/std/sort.ts +17 -2
  112. package/std/sqlite.ts +256 -35
  113. package/std/sync/ring.ts +430 -40
  114. package/std/sync/shm.ts +222 -15
  115. package/std/term-io.ts +137 -64
  116. package/std/term-pixels.ts +144 -19
  117. package/std/term-probe.ts +66 -25
  118. package/std/term-rate.ts +128 -57
  119. package/std/term.ts +332 -121
  120. package/std/testing.ts +130 -32
  121. package/std/text.ts +73 -7
  122. package/std/threads.ts +229 -41
  123. package/std/yaml-json.ts +94 -82
  124. package/std/yaml-lines.ts +47 -15
  125. package/std/yaml-write.ts +24 -16
  126. package/std/yaml.ts +764 -384
@@ -183,11 +183,11 @@ const TLS_TCP_NANOS_PER_MS: i64 = 1000000
183
183
  * `fd(slot)`.
184
184
  */
185
185
  export class TlsTcpServer {
186
- config: TlsServerConfig
186
+ private config: TlsServerConfig
187
187
  /**
188
188
  * The connections built so far, slot `k`'s at `k`: one per slot ever
189
189
  * accepted into. `connection` never adds to it; a slot never built reads
190
- * `vacant` instead.
190
+ * one shared vacant connection instead.
191
191
  */
192
192
  connections: TlsRecordServer[]
193
193
  /**
@@ -201,7 +201,7 @@ export class TlsTcpServer {
201
201
  * over, then how many bytes it has thrown away while it waits for the
202
202
  * client's end of stream, then `TLS_TCP_LINGERED` once it is done.
203
203
  */
204
- lingered: i32[]
204
+ private lingered: i32[]
205
205
  /**
206
206
  * The slots closed since they were used, a stack whose top `accept` takes
207
207
  * before it builds a slot past the high-water mark, so finding one costs
@@ -209,11 +209,11 @@ export class TlsTcpServer {
209
209
  */
210
210
  free: i32[]
211
211
  /** Where the socket is read into before a connection takes the bytes: the largest record. */
212
- scratch: u8[]
212
+ private scratch: u8[]
213
213
  /** The last accepted peer's address, in `nish:net`'s 18-byte form. */
214
214
  peer: u8[]
215
215
  /** Signs every slot's CertificateVerify in `signP256`, one for the pool, keeping nothing per signature. */
216
- signer: TlsP256Signer
216
+ private signer: TlsP256Signer
217
217
  /**
218
218
  * Each slot's own copy of its connection's server random: `TlsServer`
219
219
  * keeps the array it is given and writes it into the ServerHello only once
@@ -224,9 +224,9 @@ export class TlsTcpServer {
224
224
  */
225
225
  randoms: u8[][]
226
226
  /** How many slots the pool may hold, which `size` answers. */
227
- slots: i32 = 1
227
+ private slots: i32 = 1
228
228
  /** Whether `enableTickets` has run, so a slot built after it resumes too. */
229
- tickets: boolean = false
229
+ private tickets: boolean = false
230
230
  /**
231
231
  * What `connection` answers for every slot never built: one failed
232
232
  * connection no slot holds and every such slot shares, so the slot reads
@@ -234,7 +234,7 @@ export class TlsTcpServer {
234
234
  * session. Its first read allocates it, once per server; it has no
235
235
  * tickets, and a caller must not change it.
236
236
  */
237
- vacant: TlsRecordServer | null = null
237
+ private vacant: TlsRecordServer | null = null
238
238
  /**
239
239
  * The pools every slot borrows its record buffers from, each with a
240
240
  * ceiling of one per slot: `lent()` is 0 once every connection has ended,
@@ -242,7 +242,7 @@ export class TlsTcpServer {
242
242
  */
243
243
  pools: TlsRecordPools
244
244
  /** The listening socket, which the program made with `tcpListen` and still owns. */
245
- listener: i32 = -1
245
+ private listener: i32 = -1
246
246
 
247
247
  /**
248
248
  * A pool of `size` slots, at least one, serving `config` on `listener`, a
@@ -251,6 +251,10 @@ export class TlsTcpServer {
251
251
  * kept, and its record buffers are borrowed from `pools` while a
252
252
  * connection uses them, so a pool sized for a peak costs at start-up only
253
253
  * the read scratch, the signer and the empty pools.
254
+ *
255
+ * @param config What every slot serves: certificate chain, ALPN and the rest; every slot shares it.
256
+ * @param listener The listening socket from `tcpListen`; the program still owns it and closes it.
257
+ * @param size How many connections may be open at once; anything below 1 is taken as 1.
254
258
  */
255
259
  constructor(config: TlsServerConfig, listener: i32, size: i32) {
256
260
  this.config = config
@@ -273,6 +277,8 @@ export class TlsTcpServer {
273
277
  * entry must sit at index `k` of `connections` and `randoms`. Its handshake
274
278
  * is never given a ClientHello: `accept` restarts it for a connection with
275
279
  * real randomness, and every later connection reuses its state.
280
+ *
281
+ * @returns The new slot's connection, failed until `accept` restarts it, already pushed onto `connections`.
276
282
  */
277
283
  build(): TlsRecordServer {
278
284
  const random: u8[] = new Array<u8>(TLS_TCP_KEY_SIZE)
@@ -290,7 +296,7 @@ export class TlsTcpServer {
290
296
  * alert, so a stray call on it fails closed, and that borrows no buffer
291
297
  * until a connection moves bytes through it.
292
298
  */
293
- make(random: u8[]): TlsRecordServer {
299
+ private make(random: u8[]): TlsRecordServer {
294
300
  // A placeholder key for a first handshake, which never runs.
295
301
  const zeros: u8[] = new Array<u8>(TLS_TCP_KEY_SIZE)
296
302
  const conn: TlsRecordServer = new TlsRecordServer(new TlsServer(this.config, random, zeros))
@@ -299,12 +305,20 @@ export class TlsTcpServer {
299
305
  return conn
300
306
  }
301
307
 
302
- /** How many slots the pool has, built or not. */
308
+ /**
309
+ * How many slots the pool has, built or not.
310
+ *
311
+ * @returns The slot count the constructor settled on, at least 1.
312
+ */
303
313
  size(): i32 {
304
314
  return this.slots
305
315
  }
306
316
 
307
- /** How many slots hold a connection. */
317
+ /**
318
+ * How many slots hold a connection.
319
+ *
320
+ * @returns The number of slots with an open descriptor, lingering ones included; 0 to `size()`.
321
+ */
308
322
  busy(): i32 {
309
323
  let n: i32 = 0
310
324
  for (const fd of this.fds) {
@@ -315,12 +329,23 @@ export class TlsTcpServer {
315
329
  return n
316
330
  }
317
331
 
318
- /** Whether `slot` is a slot of this pool that holds a connection. */
332
+ /**
333
+ * Whether `slot` is a slot of this pool that holds a connection.
334
+ *
335
+ * @param slot A slot number, as `accept` answered it; any value is safe to ask about.
336
+ * @returns True when `slot` is in range and holds a descriptor, lingering or live.
337
+ */
319
338
  holds(slot: i32): boolean {
320
339
  return slot >= 0 && slot < toI32(this.fds.length) && this.fds[slot] >= 0
321
340
  }
322
341
 
323
- /** The descriptor of `slot`, for `pollAdd`; -1 for a free or unknown slot. */
342
+ /**
343
+ * The descriptor of `slot`, for `pollAdd`; -1 for a free or unknown slot.
344
+ *
345
+ * @param slot A slot number, as `accept` answered it.
346
+ * @returns The socket descriptor, which the pool still owns and closes in `close`; -1 when `slot`
347
+ * holds none.
348
+ */
324
349
  fd(slot: i32): i32 {
325
350
  return this.holds(slot) ? this.fds[slot] : -1
326
351
  }
@@ -328,9 +353,13 @@ export class TlsTcpServer {
328
353
  /**
329
354
  * The connection in `slot`: its state, its handshake (`tls`: suite, ALPN,
330
355
  * server name) and the alerts sent and received. A free slot answers the
331
- * last connection it held. A slot never accepted into answers `vacant`,
332
- * failed, as a free slot does, and builds no slot; a slot out of range
356
+ * last connection it held. A slot never accepted into answers one shared
357
+ * vacant connection, failed, as a free slot does, and builds no slot; a slot out of range
333
358
  * names slot 0's.
359
+ *
360
+ * @param slot A slot number, as `accept` answered it.
361
+ * @returns The slot's connection, for reading; the pool keeps it and reuses it for the slot's next
362
+ * connection.
334
363
  */
335
364
  connection(slot: i32): TlsRecordServer {
336
365
  const index: i32 = slot >= 0 && slot < this.slots ? slot : 0
@@ -357,6 +386,11 @@ export class TlsTcpServer {
357
386
  * closed), or `tcpAccept`'s own failure; and `TLS_RECORD_INVALID` (-22),
358
387
  * before any connection is taken, for a random or key that is not 32
359
388
  * bytes. On any answer but a slot both arrays are left as they were.
389
+ *
390
+ * @param serverRandom 32 fresh random bytes for the ServerHello's random; copied, not kept.
391
+ * @param ephemeralPrivate 32 fresh random bytes for this handshake's key share; when a slot is answered it
392
+ * has been copied into the slot's own `Secret` and this array wiped.
393
+ * @returns The slot, 0 or more, that now holds the connection; or one of the negative failures above.
360
394
  */
361
395
  accept(serverRandom: u8[], ephemeralPrivate: u8[]): i32 {
362
396
  if (
@@ -407,7 +441,11 @@ export class TlsTcpServer {
407
441
  * The socket of `slot` is readable: reads it until it would block or the
408
442
  * connection has no room, handing each read to the connection, then sends
409
443
  * what that produced. Answers the connection's `interest`, or, while the
410
- * slot lingers, what `drain` answers; a free slot answers `TLS_RECORD_DONE`.
444
+ * slot lingers (`lingering`), `TLS_RECORD_WANT_READ` until its linger is
445
+ * over and then `TLS_RECORD_DONE`; a free slot answers `TLS_RECORD_DONE`.
446
+ *
447
+ * @param slot The slot whose socket `pollWait` reported readable.
448
+ * @returns The slot's interest: `TLS_RECORD_*` bits saying what to wait for next.
411
449
  */
412
450
  readable(slot: i32): i32 {
413
451
  if (!this.holds(slot)) {
@@ -438,7 +476,12 @@ export class TlsTcpServer {
438
476
  return this.flush(slot)
439
477
  }
440
478
 
441
- /** The socket of `slot` is writable: sends what the connection holds. Answers its `interest`. */
479
+ /**
480
+ * The socket of `slot` is writable: sends what the connection holds. Answers its `interest`.
481
+ *
482
+ * @param slot The slot whose socket `pollWait` reported writable.
483
+ * @returns The slot's interest; `TLS_RECORD_DONE` for a free slot.
484
+ */
442
485
  writable(slot: i32): i32 {
443
486
  if (!this.holds(slot)) {
444
487
  return TLS_RECORD_DONE
@@ -449,8 +492,12 @@ export class TlsTcpServer {
449
492
  /**
450
493
  * Writes the connection's output until the socket would block or it is all
451
494
  * sent, and answers its `interest`; once the connection is over and the
452
- * program has read what the client sent, the slot lingers (`linger`). A
495
+ * program has read what the client sent, the slot lingers (`lingering`). A
453
496
  * free slot answers `TLS_RECORD_DONE`.
497
+ *
498
+ * @param slot The slot whose output to send.
499
+ * @returns The slot's interest; once the connection is over, `TLS_RECORD_WANT_READ` while the slot
500
+ * lingers and `TLS_RECORD_DONE` after.
454
501
  */
455
502
  flush(slot: i32): i32 {
456
503
  if (!this.holds(slot)) {
@@ -466,7 +513,7 @@ export class TlsTcpServer {
466
513
  }
467
514
 
468
515
  /** Writes `slot`'s output until the socket would block or it is all sent; answers the connection's `interest`. */
469
- send(slot: i32): i32 {
516
+ private send(slot: i32): i32 {
470
517
  const fd: i32 = this.fds[slot]
471
518
  const conn: TlsRecordServer = this.connections[slot]
472
519
  while (conn.outputEnd > conn.outputStart) {
@@ -489,7 +536,7 @@ export class TlsTcpServer {
489
536
  * every record buffer given back, and what the client sends is drained
490
537
  * (`drain`). Any other is done.
491
538
  */
492
- linger(slot: i32): i32 {
539
+ private linger(slot: i32): i32 {
493
540
  const conn: TlsRecordServer = this.connections[slot]
494
541
  if (
495
542
  conn.state !== TLS_RECORD_STATE_CLOSED ||
@@ -512,7 +559,7 @@ export class TlsTcpServer {
512
559
  * socket fails or more than `TLS_TCP_LINGER_LIMIT` bytes came
513
560
  * (`TLS_RECORD_DONE`).
514
561
  */
515
- drain(slot: i32): i32 {
562
+ private drain(slot: i32): i32 {
516
563
  while (this.lingered[slot] >= 0) {
517
564
  const n: i32 = netRead(this.fds[slot], this.scratch, TLS_TCP_FROM, toI32(this.scratch.length))
518
565
  if (n === TLS_RECORD_AGAIN) {
@@ -528,17 +575,25 @@ export class TlsTcpServer {
528
575
  * Whether `slot` lingers: its connection is over and it waits for the
529
576
  * client to hang up. A carrier's idle clock does not move for what a
530
577
  * lingering slot drains, so its idle timeout bounds the wait.
578
+ *
579
+ * @param slot A slot number, as `accept` answered it.
580
+ * @returns True while the slot holds a connection that is draining its client before `close`.
531
581
  */
532
582
  lingering(slot: i32): boolean {
533
583
  return this.holds(slot) && this.lingered[slot] >= 0
534
584
  }
535
585
 
536
586
  /** A lingering `slot`'s interest: to be read, or `TLS_RECORD_DONE` once its linger is over. */
537
- lingerInterest(slot: i32): i32 {
587
+ private lingerInterest(slot: i32): i32 {
538
588
  return this.lingered[slot] >= 0 ? TLS_RECORD_WANT_READ : TLS_RECORD_DONE
539
589
  }
540
590
 
541
- /** What `slot`'s connection wants now, without touching the socket; `TLS_RECORD_DONE` for a free slot. */
591
+ /**
592
+ * What `slot`'s connection wants now, without touching the socket; `TLS_RECORD_DONE` for a free slot.
593
+ *
594
+ * @param slot A slot number, as `accept` answered it.
595
+ * @returns The slot's `TLS_RECORD_*` interest bits; the low two are the events for `pollModify`.
596
+ */
542
597
  interest(slot: i32): i32 {
543
598
  if (!this.holds(slot)) {
544
599
  return TLS_RECORD_DONE
@@ -548,12 +603,25 @@ export class TlsTcpServer {
548
603
  : this.connections[slot].interest()
549
604
  }
550
605
 
551
- /** What `slot`'s CertificateVerify signs, while its `interest` says `TLS_RECORD_SIGN`; `null` otherwise. */
606
+ /**
607
+ * What `slot`'s CertificateVerify signs, while its `interest` says `TLS_RECORD_SIGN`; `null` otherwise.
608
+ *
609
+ * @param slot A slot number, as `accept` answered it.
610
+ * @returns The bytes to sign with the certificate's private key, or `null` for a free slot or one not
611
+ * waiting for a signature.
612
+ */
552
613
  signatureInput(slot: i32): u8[] | null {
553
614
  return this.holds(slot) ? this.connections[slot].signatureInput() : null
554
615
  }
555
616
 
556
- /** Hands `slot` the signature over `signatureInput(slot)`, seals the rest of its flight and sends it. Answers its `interest`. */
617
+ /**
618
+ * Hands `slot` the signature over `signatureInput(slot)`, seals the rest of its flight and sends it.
619
+ * Answers its `interest`.
620
+ *
621
+ * @param slot The slot whose interest says `TLS_RECORD_SIGN`.
622
+ * @param signature The signature the certificate's private key made over `signatureInput(slot)`.
623
+ * @returns The slot's interest after sending; `TLS_RECORD_DONE` for a free slot.
624
+ */
557
625
  sign(slot: i32, signature: u8[]): i32 {
558
626
  if (!this.holds(slot)) {
559
627
  return TLS_RECORD_DONE
@@ -568,6 +636,10 @@ export class TlsTcpServer {
568
636
  * handshake's signature keeps nothing. The key is borrowed: the caller made
569
637
  * the `Secret`, and wipes it. A malformed key fails the connection with
570
638
  * `internal_error`.
639
+ *
640
+ * @param slot The slot whose interest says `TLS_RECORD_SIGN`.
641
+ * @param key The certificate's P-256 private key; borrowed, still the caller's to wipe.
642
+ * @returns The slot's interest after sending; the current interest when the slot wants no signature.
571
643
  */
572
644
  signP256(slot: i32, key: Secret<u8[]>): i32 {
573
645
  const input: u8[] | null = this.signatureInput(slot)
@@ -602,6 +674,15 @@ export class TlsTcpServer {
602
674
  * the flight that answers the hello, resumed or in full
603
675
  * (`TlsRecordServer.resume`). The keys and the register are borrowed for
604
676
  * the call. Answers the slot's interest.
677
+ *
678
+ * @param slot The slot whose interest says `TLS_RECORD_TICKET` during its handshake.
679
+ * @param current The ticket key that seals new tickets; borrowed, still the caller's to wipe.
680
+ * @param currentInfo The public record of `current`: its id, when it was drawn and how much it has sealed.
681
+ * @param previous The key before `current`, so tickets sealed under it still resume; borrowed.
682
+ * @param previousInfo The public record of `previous`.
683
+ * @param strikes The register of tickets already resumed, so a ticket resumes at most once.
684
+ * @param now The time now, in `monotonicNanos()`, which key windows and ticket ages are judged against.
685
+ * @returns The slot's interest after sending; `TLS_RECORD_DONE` for a free slot.
605
686
  */
606
687
  resume(
607
688
  slot: i32,
@@ -623,6 +704,12 @@ export class TlsTcpServer {
623
704
  * While `slot`'s interest says `TLS_RECORD_TICKET` on an open connection:
624
705
  * seals one ticket under the borrowed `key` and sends its
625
706
  * NewSessionTicket (`TlsRecordServer.issue`). Answers the slot's interest.
707
+ *
708
+ * @param slot The slot whose interest says `TLS_RECORD_TICKET` on an open connection.
709
+ * @param key The ticket key to seal under; borrowed, still the caller's to wipe.
710
+ * @param info The public record of `key`, whose sealed count the ticket adds to.
711
+ * @param now The time now, in `monotonicNanos()`, sealed into the ticket as its issue time.
712
+ * @returns The slot's interest after sending; `TLS_RECORD_DONE` for a free slot.
626
713
  */
627
714
  issue(slot: i32, key: Secret<u8[]>, info: TlsTicketKeyInfo, now: i64): i32 {
628
715
  if (!this.holds(slot)) {
@@ -639,6 +726,12 @@ export class TlsTcpServer {
639
726
  * room, and the records that were waiting for it may be handled and
640
727
  * answered (a KeyUpdate), which is sent here; the connection may then want
641
728
  * the socket read again, so re-arm it with `interest(slot)`.
729
+ *
730
+ * @param slot The slot to read from.
731
+ * @param buf The program's buffer the plaintext is copied into.
732
+ * @param off Where in `buf` the first byte goes.
733
+ * @param len The most bytes to take.
734
+ * @returns The bytes copied, or 0, -11 or -104 as above; `TLS_RECORD_INVALID` (-22) for a free slot.
642
735
  */
643
736
  read(slot: i32, buf: u8[], off: i32, len: i32): i32 {
644
737
  if (!this.holds(slot)) {
@@ -654,6 +747,13 @@ export class TlsTcpServer {
654
747
  * much as fits and sends what the socket takes, and answers how much was
655
748
  * taken, -11 when nothing fits yet or the handshake is under way, -32 once
656
749
  * the server has closed, as `TlsRecordServer.write` says.
750
+ *
751
+ * @param slot The slot to write to.
752
+ * @param data The program's plaintext; it is sealed into the connection's own buffer, not kept.
753
+ * @param off Where in `data` the bytes to send start.
754
+ * @param len How many bytes, from `off`, to send.
755
+ * @returns The bytes taken, fewer than `len` when the rest does not fit yet, or -11 or -32 as above;
756
+ * `TLS_RECORD_INVALID` (-22) for a free slot.
657
757
  */
658
758
  write(slot: i32, data: u8[], off: i32, len: i32): i32 {
659
759
  if (!this.holds(slot)) {
@@ -664,7 +764,15 @@ export class TlsTcpServer {
664
764
  return n
665
765
  }
666
766
 
667
- /** Sends a KeyUpdate on `slot`, asking the client for one too when `requestPeer`; answers as `TlsRecordServer.keyUpdate`. */
767
+ /**
768
+ * Sends a KeyUpdate on `slot`, asking the client for one too when `requestPeer`; answers as
769
+ * `TlsRecordServer.keyUpdate`.
770
+ *
771
+ * @param slot The slot whose sending keys to update.
772
+ * @param requestPeer True to ask the client to update its sending keys as well (§4.6.3).
773
+ * @returns 0 once the KeyUpdate is sealed, or a negative `TLS_RECORD_*` failure as
774
+ * `TlsRecordServer.keyUpdate` says; `TLS_RECORD_INVALID` (-22) for a free slot.
775
+ */
668
776
  keyUpdate(slot: i32, requestPeer: boolean): i32 {
669
777
  if (!this.holds(slot)) {
670
778
  return TLS_RECORD_INVALID
@@ -678,9 +786,13 @@ export class TlsTcpServer {
678
786
  * Closes the server's side of `slot`'s connection (§6.1): a `close_notify`
679
787
  * after whatever is already written, sent as the socket takes it. The slot
680
788
  * stays the program's until its interest says `TLS_RECORD_DONE` — every
681
- * byte is sent and, once the slot lingers (`linger`), the client has hung
789
+ * byte is sent and, once the slot lingers (`lingering`), the client has hung
682
790
  * up — and the program calls `close`. Meanwhile the interest asks for the
683
791
  * socket to be read. Answers the interest.
792
+ *
793
+ * @param slot The slot whose connection to close.
794
+ * @returns The slot's interest; `TLS_RECORD_DONE` for a free slot, or once there is nothing left to
795
+ * wait for.
684
796
  */
685
797
  shutdown(slot: i32): i32 {
686
798
  if (!this.holds(slot)) {
@@ -697,6 +809,8 @@ export class TlsTcpServer {
697
809
  * out of every loop. A program that must know every byte and the
698
810
  * `close_notify` arrived calls `shutdown` instead, and `close` once the
699
811
  * slot's interest says `TLS_RECORD_DONE`.
812
+ *
813
+ * @param slot The slot to free; a free or unknown slot is left alone.
700
814
  */
701
815
  close(slot: i32): void {
702
816
  if (!this.holds(slot)) {
@@ -746,29 +860,37 @@ export class TlsTcpServer {
746
860
  * The socket is still connecting when the constructor returns, so the first
747
861
  * interest is writable alone; the first event on it, writable or a hang-up,
748
862
  * says how the connection went (`connectResult`), and a socket that did not
749
- * connect leaves the client done, its failure in `connectError`.
863
+ * connect leaves the client done, its failure in `failure()`.
750
864
  */
751
865
  export class TlsTcpClient {
752
866
  conn: TlsRecordClient
753
- trust: TlsClientTrust
867
+ private trust: TlsClientTrust
754
868
  /** The socket, -1 once `close` has run or when `tcpConnect` refused at once. */
755
- socket: i32 = -1
869
+ private socket: i32 = -1
756
870
  /** Whether the socket has yet to say how its connection went. */
757
- connecting: boolean = true
871
+ private connecting: boolean = true
758
872
  /**
759
873
  * Why the connection failed below TLS, or 0: `tcpConnect`'s or
760
874
  * `connectResult`'s failure, or what ended `tlsConnect`'s wait — its
761
875
  * timeout (`TLS_TCP_TIMED_OUT`) or its loop's failure.
876
+ *
877
+ * @internal Read by the free functions of this module.
762
878
  */
763
879
  connectError: i32 = 0
764
880
  /** Where the socket is read into before the connection takes the bytes: the largest record. */
765
- scratch: u8[]
881
+ private scratch: u8[]
766
882
 
767
883
  /**
768
884
  * A connection to the server at `addr` (`nish:net`'s 18-byte form, which
769
885
  * `netAddress` writes) under `config`, authenticated against `trust`. The
770
886
  * name the certificate must carry is `config.serverName`, never one taken
771
887
  * from `addr`. The ClientHello is sealed here and waits for the socket.
888
+ *
889
+ * @param addr The server's address and port in `nish:net`'s 18-byte form; a shorter array fails the
890
+ * client with `failure()` -22 and nothing dialled.
891
+ * @param config The server name, ALPN protocols and the rest of the ClientHello; a config `TlsClient`
892
+ * refuses leaves the client failed before any socket is made.
893
+ * @param trust The roots and policy the server's certificate chain is checked against.
772
894
  */
773
895
  constructor(addr: u8[], config: TlsClientConfig, trust: TlsClientTrust) {
774
896
  const random: u8[] = new Array<u8>(TLS_TCP_KEY_SIZE)
@@ -797,22 +919,41 @@ export class TlsTcpClient {
797
919
  this.socket = fd
798
920
  }
799
921
 
800
- /** The socket, for `pollAdd`; -1 when there is none. */
922
+ /**
923
+ * The socket, for `pollAdd`; -1 when there is none.
924
+ *
925
+ * @returns The socket descriptor, which the client still owns and closes in `close`; -1 when there is none.
926
+ */
801
927
  fd(): i32 {
802
928
  return this.socket
803
929
  }
804
930
 
805
- /** Whether the handshake is done and the server authenticated, so application data flows. */
931
+ /**
932
+ * Whether the handshake is done and the server authenticated, so application data flows.
933
+ *
934
+ * @returns True from the server's Finished until the connection fails or `shutdown` or `close` closes it.
935
+ */
806
936
  open(): boolean {
807
937
  return this.conn.state === TLS_RECORD_STATE_OPEN
808
938
  }
809
939
 
810
- /** Why a failed connection failed: `connectError` when TCP did not connect, else the alert (`TlsRecordClient.failure`). */
940
+ /**
941
+ * Why a failed connection failed: the TCP failure when TCP did not connect, else the alert
942
+ * (`TlsRecordClient.failure`).
943
+ *
944
+ * @returns A negative `nish:net` failure or `TLS_TCP_TIMED_OUT` when the trouble was below TLS,
945
+ * otherwise what `TlsRecordClient.failure` answers.
946
+ */
811
947
  failure(): i32 {
812
948
  return this.connectError !== 0 ? this.connectError : this.conn.failure()
813
949
  }
814
950
 
815
- /** What the connection wants now, as `TLS_RECORD_*` bits, without touching the socket. */
951
+ /**
952
+ * What the connection wants now, as `TLS_RECORD_*` bits, without touching the socket.
953
+ *
954
+ * @returns The interest bits; the low two are the events for `pollModify`, and `TLS_RECORD_DONE` once
955
+ * there is no socket.
956
+ */
816
957
  interest(): i32 {
817
958
  if (this.socket < 0) {
818
959
  return TLS_RECORD_DONE
@@ -825,7 +966,7 @@ export class TlsTcpClient {
825
966
  * did not leaves the client failed with nothing sent, its reason in
826
967
  * `connectError`.
827
968
  */
828
- connected(): boolean {
969
+ private connected(): boolean {
829
970
  this.connecting = false
830
971
  const result: i32 = connectResult(this.socket)
831
972
  if (result !== 0) {
@@ -840,6 +981,9 @@ export class TlsTcpClient {
840
981
  * The socket is readable, or hung up: reads it until it would block or the
841
982
  * connection has no room, handing each read to the connection, then sends
842
983
  * what that produced. Answers the `interest`.
984
+ *
985
+ * @returns The interest after reading and sending; `TLS_RECORD_DONE` when there is no socket or it
986
+ * did not connect.
843
987
  */
844
988
  readable(): i32 {
845
989
  if (this.socket < 0) {
@@ -869,7 +1013,12 @@ export class TlsTcpClient {
869
1013
  return this.flush()
870
1014
  }
871
1015
 
872
- /** The socket is writable: on the first event, how the connection went; then sends what the connection holds. Answers the `interest`. */
1016
+ /**
1017
+ * The socket is writable: on the first event, how the connection went; then sends what the
1018
+ * connection holds. Answers the `interest`.
1019
+ *
1020
+ * @returns The interest after sending; `TLS_RECORD_DONE` when there is no socket or it did not connect.
1021
+ */
873
1022
  writable(): i32 {
874
1023
  if (this.socket < 0) {
875
1024
  return TLS_RECORD_DONE
@@ -885,7 +1034,7 @@ export class TlsTcpClient {
885
1034
  * connection's output until the socket would block or it is all sent, and
886
1035
  * answers the `interest`.
887
1036
  */
888
- flush(): i32 {
1037
+ private flush(): i32 {
889
1038
  if (this.socket < 0) {
890
1039
  return TLS_RECORD_DONE
891
1040
  }
@@ -915,6 +1064,11 @@ export class TlsTcpClient {
915
1064
  * connection has failed, as `TlsRecordClient.read` says, and -32 once
916
1065
  * `close` has run. What reading makes room for is answered and sent here;
917
1066
  * re-arm with `interest()`.
1067
+ *
1068
+ * @param buf The program's buffer the plaintext is copied into.
1069
+ * @param off Where in `buf` the first byte goes.
1070
+ * @param len The most bytes to take.
1071
+ * @returns The bytes copied, or 0, -11, -104 or -32 as above.
918
1072
  */
919
1073
  read(buf: u8[], off: i32, len: i32): i32 {
920
1074
  if (this.socket < 0) {
@@ -926,7 +1080,7 @@ export class TlsTcpClient {
926
1080
  }
927
1081
 
928
1082
  /** What a call on a client that is done answers: -104 when it failed, else -32. */
929
- ended(): i32 {
1083
+ private ended(): i32 {
930
1084
  return this.conn.state === TLS_RECORD_STATE_FAILED ? TLS_RECORD_RESET : TLS_RECORD_PIPE
931
1085
  }
932
1086
 
@@ -937,6 +1091,12 @@ export class TlsTcpClient {
937
1091
  * nothing came in time (the connection stays usable), -104 once it has
938
1092
  * failed, -32 once it is done otherwise, or the loop's failure. It drives
939
1093
  * the handshake too, so it may follow `new TlsTcpClient` directly.
1094
+ *
1095
+ * @param buf The program's buffer the plaintext is copied into.
1096
+ * @param off Where in `buf` the first byte goes.
1097
+ * @param len The most bytes to take.
1098
+ * @param timeoutMs How long to wait, in milliseconds on the monotonic clock, from the call.
1099
+ * @returns The bytes copied, or 0, `TLS_TCP_TIMED_OUT`, -104, -32 or the loop's failure as above.
940
1100
  */
941
1101
  readWait(buf: u8[], off: i32, len: i32, timeoutMs: i32): i32 {
942
1102
  const end: i64 = tlsTcpDeadline(timeoutMs)
@@ -978,6 +1138,12 @@ export class TlsTcpClient {
978
1138
  * sent, and the same bytes written again would reach the server twice:
979
1139
  * once the handshake is done, a timeout leaves the stream unusable, and
980
1140
  * the client is for `close`.
1141
+ *
1142
+ * @param data The program's plaintext; it is sealed into the connection's own buffer, not kept.
1143
+ * @param off Where in `data` the bytes to send start.
1144
+ * @param len How many bytes, from `off`, to send.
1145
+ * @param timeoutMs How long to wait for all of it, in milliseconds on the monotonic clock, from the call.
1146
+ * @returns `len` once the socket has taken every byte, or one of the negative failures above.
981
1147
  */
982
1148
  writeAll(data: u8[], off: i32, len: i32, timeoutMs: i32): i32 {
983
1149
  const end: i64 = tlsTcpDeadline(timeoutMs)
@@ -1020,12 +1186,20 @@ export class TlsTcpClient {
1020
1186
  return result < 0 ? result : done
1021
1187
  }
1022
1188
 
1023
- /** Whether sealed bytes are still waiting for the socket. */
1189
+ /**
1190
+ * Whether sealed bytes are still waiting for the socket.
1191
+ *
1192
+ * @returns True while the connection's output holds bytes the socket has not taken yet.
1193
+ */
1024
1194
  sending(): boolean {
1025
1195
  return this.conn.outputEnd > this.conn.outputStart
1026
1196
  }
1027
1197
 
1028
- /** A readiness loop of the client's own holding its socket, for the waiting calls, or the loop's failure. */
1198
+ /**
1199
+ * A readiness loop of the client's own holding its socket, for the waiting calls, or the loop's failure.
1200
+ *
1201
+ * @internal Read by the free functions of this module.
1202
+ */
1029
1203
  ownLoop(): i32 {
1030
1204
  const loop: i32 = pollCreate()
1031
1205
  if (loop >= 0) {
@@ -1039,6 +1213,8 @@ export class TlsTcpClient {
1039
1213
  * until `end` on the monotonic clock, and handles what came. Answers 0,
1040
1214
  * `TLS_RECORD_DONE` once the connection wants nothing more,
1041
1215
  * `TLS_TCP_TIMED_OUT`, or the loop's failure.
1216
+ *
1217
+ * @internal Read by the free functions of this module.
1042
1218
  */
1043
1219
  turn(loop: i32, end: i64): i32 {
1044
1220
  const wants: i32 = this.interest()
@@ -1074,6 +1250,12 @@ export class TlsTcpClient {
1074
1250
  * as fits and sends what the socket takes, and answers how much was taken,
1075
1251
  * -11 when nothing fits yet or the handshake is under way, -32 once the
1076
1252
  * client has closed, as `TlsRecordClient.write` says.
1253
+ *
1254
+ * @param data The program's plaintext; it is sealed into the connection's own buffer, not kept.
1255
+ * @param off Where in `data` the bytes to send start.
1256
+ * @param len How many bytes, from `off`, to send.
1257
+ * @returns The bytes taken, fewer than `len` when the rest does not fit yet, or a negative failure as
1258
+ * `TlsRecordClient.write` says.
1077
1259
  */
1078
1260
  write(data: u8[], off: i32, len: i32): i32 {
1079
1261
  const n: i32 = this.conn.write(data, off, len)
@@ -1081,7 +1263,13 @@ export class TlsTcpClient {
1081
1263
  return n
1082
1264
  }
1083
1265
 
1084
- /** Sends a KeyUpdate, asking the server for one too when `requestPeer`; answers as `TlsRecordClient.keyUpdate`. */
1266
+ /**
1267
+ * Sends a KeyUpdate, asking the server for one too when `requestPeer`; answers as
1268
+ * `TlsRecordClient.keyUpdate`.
1269
+ *
1270
+ * @param requestPeer True to ask the server to update its sending keys as well (§4.6.3).
1271
+ * @returns 0 once the KeyUpdate is sealed, or a negative failure as `TlsRecordClient.keyUpdate` says.
1272
+ */
1085
1273
  keyUpdate(requestPeer: boolean): i32 {
1086
1274
  const r: i32 = this.conn.keyUpdate(requestPeer)
1087
1275
  this.flush()
@@ -1095,6 +1283,8 @@ export class TlsTcpClient {
1095
1283
  * still reads, and once the `close_notify` is sent the interest says
1096
1284
  * `TLS_RECORD_DONE` and `readWait` answers -32; call `close` then.
1097
1285
  * Answers the interest.
1286
+ *
1287
+ * @returns The interest after sending what the socket takes; `TLS_RECORD_DONE` once nothing is left.
1098
1288
  */
1099
1289
  shutdown(): i32 {
1100
1290
  this.conn.close()
@@ -1138,13 +1328,28 @@ const tlsTcpDeadline = (timeoutMs: i32): i64 => monotonicNanos() + toI64(timeout
1138
1328
  * program adds it to its own, or uses `writeAll` and `readWait`. The roots
1139
1329
  * are read on every call; a program that connects often reads them once with
1140
1330
  * `tlsClientSystemTrust` and passes them to `tlsConnectWith`.
1331
+ *
1332
+ * @param addr The server's address and port in `nish:net`'s 18-byte form.
1333
+ * @param serverName The name the server's certificate must carry, sent as `server_name` unless it is an
1334
+ * IP literal.
1335
+ * @param alpn The ALPN protocols to offer, most preferred first, each 1 to 255 bytes; empty offers none.
1336
+ * @param timeoutMs How long the whole connect may take, in milliseconds on the monotonic clock.
1337
+ * @returns The client, open or failed; the caller owns it and calls `close` when done with it.
1141
1338
  */
1142
1339
  export const tlsConnect = (addr: u8[], serverName: string, alpn: string[], timeoutMs: i32): TlsTcpClient => {
1143
1340
  const end: i64 = tlsTcpDeadline(timeoutMs)
1144
1341
  return tlsConnectBy(addr, new TlsClientConfig(serverName, alpn), tlsClientSystemTrust(), end)
1145
1342
  }
1146
1343
 
1147
- /** `tlsConnect` under a `config` and `trust` of the caller's. */
1344
+ /**
1345
+ * `tlsConnect` under a `config` and `trust` of the caller's.
1346
+ *
1347
+ * @param addr The server's address and port in `nish:net`'s 18-byte form.
1348
+ * @param config The server name, ALPN protocols and the rest of the ClientHello.
1349
+ * @param trust The roots the server's chain is checked against, such as one `tlsClientSystemTrust` read.
1350
+ * @param timeoutMs How long the whole connect may take, in milliseconds on the monotonic clock.
1351
+ * @returns The client, open or failed; the caller owns it and calls `close` when done with it.
1352
+ */
1148
1353
  export const tlsConnectWith = (
1149
1354
  addr: u8[],
1150
1355
  config: TlsClientConfig,