@amritk/nish-aarch64-linux 0.35.0 → 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 (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -169,9 +169,10 @@ const TLS_RECORD_PLAIN_SIZE: i32 = 2 * TLS_MAX_PLAINTEXT
169
169
  /** Where a record's content is opened to before it is handed on: the most content a record carries. */
170
170
  const TLS_RECORD_SCRATCH_SIZE: i32 = TLS_MAX_PLAINTEXT
171
171
  /**
172
- * The four pools a connection's record buffers are borrowed from: `output`,
173
- * `plain` and `scratch` for `TlsRecordServer`'s fields of those names, and
174
- * `reader` for its `TlsRecordReader`. A carrier with many slots makes one set
172
+ * The four pools a connection's record buffers are borrowed from: `output`
173
+ * and `plain` for `TlsRecordServer`'s fields of those names, `scratch` for the
174
+ * buffer it opens a record's content into, and `reader` for its record
175
+ * reader's buffer. A carrier with many slots makes one set
175
176
  * with a ceiling of its slot count and points every slot at it with
176
177
  * `TlsRecordServer.borrowFrom`, so a full server holds what preallocating
177
178
  * every slot would have held and an idle one holds only what earlier
@@ -184,7 +185,11 @@ export class TlsRecordPools {
184
185
  scratch: BufferPool
185
186
  reader: BufferPool
186
187
 
187
- /** Four empty pools of `ceiling` buffers each; nothing is made until a slot needs it. */
188
+ /**
189
+ * Four empty pools of `ceiling` buffers each; nothing is made until a slot needs it.
190
+ *
191
+ * @param ceiling How many buffers each pool may lend at once: the carrier's slot count.
192
+ */
188
193
  constructor(ceiling: i32) {
189
194
  this.output = new BufferPool("TlsRecordServer.output", TLS_RECORD_OUTPUT_SIZE, ceiling)
190
195
  this.plain = new BufferPool("TlsRecordServer.plain", TLS_RECORD_PLAIN_SIZE, ceiling)
@@ -192,12 +197,20 @@ export class TlsRecordPools {
192
197
  this.reader = new BufferPool("TlsRecordReader.buffer", TLS_MAX_RECORD, ceiling)
193
198
  }
194
199
 
195
- /** How many buffers of the four pools are lent now: 0 once every connection has given its back. */
200
+ /**
201
+ * How many buffers of the four pools are lent now: 0 once every connection has given its back.
202
+ *
203
+ * @returns The buffers lent and not yet given back, summed over the four pools.
204
+ */
196
205
  lent(): i32 {
197
206
  return this.output.lent() + this.plain.lent() + this.scratch.lent() + this.reader.lent()
198
207
  }
199
208
 
200
- /** How many buffers the four pools have made: their high-water mark together. */
209
+ /**
210
+ * How many buffers the four pools have made: their high-water mark together.
211
+ *
212
+ * @returns The buffers ever made, summed over the four pools.
213
+ */
201
214
  made(): i32 {
202
215
  return this.output.made + this.plain.made + this.scratch.made + this.reader.made
203
216
  }
@@ -287,7 +300,7 @@ const tlsAdvanceHeld = (protection: TlsRecordProtection, held: Secret<u8[]>): bo
287
300
  */
288
301
  export class TlsRecordServer {
289
302
  tls: TlsServer
290
- reader: TlsRecordReader
303
+ private reader: TlsRecordReader
291
304
  readProtection: TlsRecordProtection
292
305
  writeProtection: TlsRecordProtection
293
306
  /** Bytes to send are `output[outputStart .. outputEnd)`; `empty` until a record is sealed. */
@@ -295,11 +308,11 @@ export class TlsRecordServer {
295
308
  /** Application data the caller has not read is `plain[plainStart .. plainEnd)`; `empty` until a record brings some. */
296
309
  plain: u8[]
297
310
  /** Where a record is opened to; `empty` until the first protected record. */
298
- scratch: u8[]
311
+ private scratch: u8[]
299
312
  /** The zero-length array a buffer field is while it holds no borrowed buffer. */
300
- empty: u8[]
313
+ private empty: u8[]
301
314
  /** Where `output`, `plain`, `scratch` and the reader's buffer are borrowed from. */
302
- pools: TlsRecordPools
315
+ private pools: TlsRecordPools
303
316
  /**
304
317
  * The client's and the server's current application traffic secrets, for
305
318
  * KeyUpdate, each held at both hash lengths: `readHash` and `writeHash`
@@ -312,50 +325,61 @@ export class TlsRecordServer {
312
325
  readHash: i32 = 0
313
326
  writeHash: i32 = 0
314
327
  /** A post-handshake message the client has sent part of. */
315
- postHandshake: u8[]
328
+ private postHandshake: u8[]
316
329
  state: i32 = 0
317
330
  alert: i32 = 0
318
331
  peerAlert: i32 = -1
319
332
  padding: i32 = 0
320
333
  /** The `TLS_LEVEL_*` the client's records are read at: Initial in the clear, then Handshake, then Application. */
321
- readLevel: i32 = 0
334
+ private readLevel: i32 = 0
322
335
  /** The `TLS_LEVEL_*` the server's records are written at. */
323
336
  writeLevel: i32 = 0
324
337
  outputStart: i32 = 0
325
338
  outputEnd: i32 = 0
326
339
  /** How far into `output` this borrowing has written, which says whether its give-back zeroes. */
327
- outputUsed: i32 = 0
340
+ private outputUsed: i32 = 0
328
341
  plainStart: i32 = 0
329
342
  plainEnd: i32 = 0
330
343
  /** How far into `plain` this borrowing has written. */
331
- plainUsed: i32 = 0
344
+ private plainUsed: i32 = 0
332
345
  /** How much of `TlsServer`'s Initial-level output is already in records. */
333
- pendingInitialAt: i32 = 0
346
+ private pendingInitialAt: i32 = 0
334
347
  /** How much of `TlsServer`'s Handshake-level output is already in records. */
335
- pendingHandshakeAt: i32 = 0
348
+ private pendingHandshakeAt: i32 = 0
336
349
  /** How much of `TlsServer`'s Application-level output, a NewSessionTicket, is already in records. */
337
- pendingApplicationAt: i32 = 0
338
- postHandshakeLength: i32 = 0
350
+ private pendingApplicationAt: i32 = 0
351
+ private postHandshakeLength: i32 = 0
339
352
  /** Records in a row that carried nothing for the caller, against `TLS_RECORD_IDLE_LIMIT`. */
340
- idleRecords: i32 = 0
353
+ private idleRecords: i32 = 0
341
354
  /** KeyUpdates taken from the client, against `TLS_RECORD_MAX_KEY_UPDATES`. */
342
355
  keyUpdates: i32 = 0
343
356
  /** The server's flight is signed and finished, so its Handshake-level output may be sealed. */
344
- flightReady: boolean = false
357
+ private flightReady: boolean = false
345
358
  peerClosed: boolean = false
346
- /** The client's TCP stream ended. Without a `close_notify` before it, that is truncation and the connection failed. */
359
+ /**
360
+ * The client's TCP stream ended. Without a `close_notify` before it, that is truncation and the connection failed.
361
+ *
362
+ * @internal Read by `net/tls-tcp.ts`.
363
+ */
347
364
  ended: boolean = false
348
365
  /** Whether the server's first handshake message has been seen, which decides the compatibility record. */
349
- firstMessageSeen: boolean = false
366
+ private firstMessageSeen: boolean = false
350
367
  /** The compatibility `change_cipher_spec` is owed, after the first handshake message (§D.4). */
351
- ccsDue: boolean = false
368
+ private ccsDue: boolean = false
352
369
  /** The client's first handshake byte has arrived, which opens the window its `change_cipher_spec` may come in. */
353
- started: boolean = false
370
+ private started: boolean = false
354
371
  /** The server owes a KeyUpdate before its next application data (§4.6.3). */
355
- keyUpdateDue: boolean = false
372
+ private keyUpdateDue: boolean = false
356
373
  /** The server has closed and its `close_notify` waits for room in `output`. */
357
374
  closeDue: boolean = false
358
375
 
376
+ /**
377
+ * Makes a slot for `tls` with a pool set of its own, one buffer of each
378
+ * kind, and starts the connection as `start` does. A carrier that shares
379
+ * its pools calls `borrowFrom` right after.
380
+ *
381
+ * @param tls The handshake this slot carries, fresh or just restarted.
382
+ */
359
383
  constructor(tls: TlsServer) {
360
384
  this.tls = tls
361
385
  this.reader = new TlsRecordReader()
@@ -384,6 +408,8 @@ export class TlsRecordServer {
384
408
  * `tls` that has already failed — a configuration
385
409
  * or randomness `TlsServer` refused — fails this connection at once, with
386
410
  * its alert queued in the clear.
411
+ *
412
+ * @param tls The handshake the new connection runs, fresh or just restarted.
387
413
  */
388
414
  start(tls: TlsServer): void {
389
415
  // The last connection's traffic keys. Its ephemeral key was wiped when it
@@ -417,6 +443,8 @@ export class TlsRecordServer {
417
443
  * slots share. Whatever is held from the old pools is given back first,
418
444
  * zeroed, and dropped with it, so call it on a slot with nothing in flight:
419
445
  * the carrier does, right after the constructor.
446
+ *
447
+ * @param pools The carrier's pool set, shared by all of its slots.
420
448
  */
421
449
  borrowFrom(pools: TlsRecordPools): void {
422
450
  this.release()
@@ -446,7 +474,7 @@ export class TlsRecordServer {
446
474
  * reads zeros. Not called between records, since `giveSecret` zeroes the
447
475
  * whole buffer however little of it a record used.
448
476
  */
449
- giveBackIdle(): void {
477
+ private giveBackIdle(): void {
450
478
  if (this.output.length > 0 && this.outputStart === this.outputEnd) {
451
479
  this.pools.output.giveSecret(this.output, this.outputUsed)
452
480
  this.output = this.empty
@@ -465,7 +493,7 @@ export class TlsRecordServer {
465
493
  }
466
494
 
467
495
  /** Forgets the application data the caller has not read. */
468
- dropPlain(): void {
496
+ private dropPlain(): void {
469
497
  this.plainStart = 0
470
498
  this.plainEnd = 0
471
499
  }
@@ -501,13 +529,13 @@ export class TlsRecordServer {
501
529
  }
502
530
 
503
531
  /** Wipes the two application traffic secrets and both directions' keys. */
504
- wipeTraffic(): void {
532
+ private wipeTraffic(): void {
505
533
  this.wipeRead()
506
534
  this.wipeWrite()
507
535
  }
508
536
 
509
537
  /** Wipes the client's application traffic secret and the read keys. */
510
- wipeRead(): void {
538
+ private wipeRead(): void {
511
539
  // Whichever of the pair held the secret, both are zeroed.
512
540
  this.wipeReadSecret()
513
541
  this.readHash = 0
@@ -515,20 +543,20 @@ export class TlsRecordServer {
515
543
  }
516
544
 
517
545
  /** Wipes the server's application traffic secret and its write keys. */
518
- wipeWrite(): void {
546
+ private wipeWrite(): void {
519
547
  this.wipeWriteSecret()
520
548
  this.writeHash = 0
521
549
  this.writeProtection.clear()
522
550
  }
523
551
 
524
552
  /** Zeroes both of the client's traffic secret fields. */
525
- wipeReadSecret(): void {
553
+ private wipeReadSecret(): void {
526
554
  exposeWith(this.readTraffic256, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
527
555
  exposeWith(this.readTraffic384, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
528
556
  }
529
557
 
530
558
  /** Zeroes both of the server's traffic secret fields. */
531
- wipeWriteSecret(): void {
559
+ private wipeWriteSecret(): void {
532
560
  exposeWith(this.writeTraffic256, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
533
561
  exposeWith(this.writeTraffic384, TLS_RECORD_SERVER_FROM, tlsTrafficZero)
534
562
  }
@@ -539,7 +567,7 @@ export class TlsRecordServer {
539
567
  * read keys from it. False when the secret is not known or the keys do not
540
568
  * install.
541
569
  */
542
- keepRead(): boolean {
570
+ private keepRead(): boolean {
543
571
  const tls: TlsServer = this.tls
544
572
  if (!tls.readKnown(TLS_LEVEL_APPLICATION)) {
545
573
  return false
@@ -560,7 +588,7 @@ export class TlsRecordServer {
560
588
  }
561
589
 
562
590
  /** `keepRead` for the server's application traffic secret and the write keys. */
563
- keepWrite(): boolean {
591
+ private keepWrite(): boolean {
564
592
  const tls: TlsServer = this.tls
565
593
  if (!tls.writeKnown(TLS_LEVEL_APPLICATION)) {
566
594
  return false
@@ -580,7 +608,11 @@ export class TlsRecordServer {
580
608
  return installed
581
609
  }
582
610
 
583
- /** How many bytes `receive` would take now: what the socket should read at most. */
611
+ /**
612
+ * How many bytes `receive` would take now: what the socket should read at most.
613
+ *
614
+ * @returns The room left in the record reader's buffer, in bytes.
615
+ */
584
616
  space(): i32 {
585
617
  return this.reader.space()
586
618
  }
@@ -592,6 +624,12 @@ export class TlsRecordServer {
592
624
  * socket again), or `TLS_RECORD_INVALID` for a window outside `data`. Once
593
625
  * the connection has failed, or the client has sent `close_notify`, bytes
594
626
  * are taken and dropped (§6.1).
627
+ *
628
+ * @param data The array that holds the bytes read from the socket.
629
+ * @param off Where in `data` the bytes start.
630
+ * @param len How many bytes, from `off`, to offer.
631
+ * @returns How many bytes were taken, from 0 up to `len` (all of them once the connection has failed or
632
+ * the client has closed); `TLS_RECORD_INVALID` for a window outside `data`.
595
633
  */
596
634
  receive(data: u8[], off: i32, len: i32): i32 {
597
635
  if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
@@ -624,7 +662,7 @@ export class TlsRecordServer {
624
662
  * signature, are handled first, so a `close_notify` among them counts.
625
663
  * Every key the connection holds is wiped, as `fail` wipes them.
626
664
  */
627
- checkTruncation(): void {
665
+ private checkTruncation(): void {
628
666
  if (this.ended && !this.peerClosed && this.state !== TLS_RECORD_STATE_FAILED && this.reader.next() <= 0) {
629
667
  this.state = TLS_RECORD_STATE_FAILED
630
668
  this.dropPending()
@@ -640,7 +678,7 @@ export class TlsRecordServer {
640
678
  }
641
679
 
642
680
  /** Forgets everything waiting to be sent. */
643
- dropOutput(): void {
681
+ private dropOutput(): void {
644
682
  this.outputStart = 0
645
683
  this.outputEnd = 0
646
684
  this.dropPending()
@@ -650,7 +688,7 @@ export class TlsRecordServer {
650
688
  * Forgets what is owed but not yet sealed: the handshake queues, a
651
689
  * NewSessionTicket, the compatibility record, a KeyUpdate and a close.
652
690
  */
653
- dropPending(): void {
691
+ private dropPending(): void {
654
692
  this.tls.clearOutput(TLS_LEVEL_INITIAL)
655
693
  this.tls.clearOutput(TLS_LEVEL_HANDSHAKE)
656
694
  this.tls.clearOutput(TLS_LEVEL_APPLICATION)
@@ -670,7 +708,7 @@ export class TlsRecordServer {
670
708
  * nothing to send then but a `change_cipher_spec`, and holding the rest
671
709
  * means bytes mean the same thing however the stream was cut.
672
710
  */
673
- process(): void {
711
+ private process(): void {
674
712
  while (this.state !== TLS_RECORD_STATE_FAILED && !this.peerClosed) {
675
713
  if (this.tls.state === TLS_STATE_WAIT_SIGNATURE || this.tls.state === TLS_STATE_WAIT_TICKET) {
676
714
  break
@@ -702,7 +740,7 @@ export class TlsRecordServer {
702
740
  * read, moving that to the front when it would then fit and only then, so
703
741
  * a caller reading a byte at a time does not move the buffer each time.
704
742
  */
705
- plainHasRoom(): boolean {
743
+ private plainHasRoom(): boolean {
706
744
  const capacity: i32 = TLS_RECORD_PLAIN_SIZE
707
745
  if (capacity - (this.plainEnd - this.plainStart) < TLS_MAX_PLAINTEXT) {
708
746
  return false
@@ -722,7 +760,7 @@ export class TlsRecordServer {
722
760
  }
723
761
 
724
762
  /** Counts a record that carried nothing for the caller: 0, or `unexpected_message` once there are too many in a row. */
725
- idle(): i32 {
763
+ private idle(): i32 {
726
764
  this.idleRecords = this.idleRecords + 1
727
765
  return this.idleRecords > TLS_RECORD_IDLE_LIMIT ? TLS_ALERT_UNEXPECTED_MESSAGE : 0
728
766
  }
@@ -733,7 +771,7 @@ export class TlsRecordServer {
733
771
  * into `scratch`, and its content goes to `handleApplicationRecord` once the
734
772
  * handshake is done, to the handshake before. Answers 0 or the alert.
735
773
  */
736
- handleRecord(buf: u8[], off: i32, n: i32): i32 {
774
+ private handleRecord(buf: u8[], off: i32, n: i32): i32 {
737
775
  if (toI32(buf[off]) === TLS_CONTENT_CHANGE_CIPHER_SPEC) {
738
776
  return this.handleChangeCipherSpec(buf, off, n - TLS_RECORD_HEADER_SIZE)
739
777
  }
@@ -766,7 +804,7 @@ export class TlsRecordServer {
766
804
  * records of one handshake message. It is dropped; anything else is
767
805
  * `unexpected_message`.
768
806
  */
769
- handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
807
+ private handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
770
808
  if (length !== 1 || toI32(buf[off + TLS_RECORD_HEADER_SIZE]) !== 1) {
771
809
  return TLS_ALERT_UNEXPECTED_MESSAGE
772
810
  }
@@ -781,7 +819,7 @@ export class TlsRecordServer {
781
819
  * they were read at. An empty fragment is `unexpected_message` (§5.1 forbids
782
820
  * sending one); `TlsServer`'s own answer otherwise.
783
821
  */
784
- handleHandshake(length: i32): i32 {
822
+ private handleHandshake(length: i32): i32 {
785
823
  if (length === 0) {
786
824
  return TLS_ALERT_UNEXPECTED_MESSAGE
787
825
  }
@@ -802,7 +840,7 @@ export class TlsRecordServer {
802
840
  * post-handshake message are handled here; and a post-handshake message the
803
841
  * client has sent part of may not be interrupted by anything else (§5.1).
804
842
  */
805
- handleApplicationRecord(length: i32): i32 {
843
+ private handleApplicationRecord(length: i32): i32 {
806
844
  const type: i32 = this.readProtection.contentType
807
845
  if (this.postHandshakeLength > 0 && type !== TLS_CONTENT_HANDSHAKE) {
808
846
  return TLS_ALERT_UNEXPECTED_MESSAGE
@@ -838,7 +876,7 @@ export class TlsRecordServer {
838
876
  * this module does not know included (§6.2) — ends the connection with
839
877
  * nothing more sent.
840
878
  */
841
- handleAlert(length: i32): i32 {
879
+ private handleAlert(length: i32): i32 {
842
880
  if (length !== 2) {
843
881
  return TLS_ALERT_DECODE_ERROR
844
882
  }
@@ -871,7 +909,7 @@ export class TlsRecordServer {
871
909
  * KeyUpdate that does not end its record `unexpected_message`, since the
872
910
  * keys change after it (§5.1). The message may start in an earlier record.
873
911
  */
874
- handlePostHandshake(length: i32): i32 {
912
+ private handlePostHandshake(length: i32): i32 {
875
913
  const idle: i32 = this.idle()
876
914
  if (idle !== 0) {
877
915
  return idle
@@ -920,7 +958,7 @@ export class TlsRecordServer {
920
958
  * Answers 0, or `internal_error` when no read secret is kept (`readHash`
921
959
  * 0) or the keys do not install.
922
960
  */
923
- updateReadKeys(requested: boolean): i32 {
961
+ private updateReadKeys(requested: boolean): i32 {
924
962
  const advanced: boolean =
925
963
  this.readHash === SHA384_SIZE
926
964
  ? tlsAdvanceHeld(this.readProtection, this.readTraffic384)
@@ -941,7 +979,7 @@ export class TlsRecordServer {
941
979
  * hands over are copied or installed and then wiped in `TlsServer`, so
942
980
  * that only this connection holds them.
943
981
  */
944
- advance(): void {
982
+ private advance(): void {
945
983
  const tls: TlsServer = this.tls
946
984
  if (tls.outputInitialLength > 0 && !this.firstMessageSeen) {
947
985
  // §D.4: in compatibility mode, which a client asks for with a session
@@ -997,7 +1035,7 @@ export class TlsRecordServer {
997
1035
  * front first. Borrows `output` when none is held, since a record is about
998
1036
  * to be sealed into it.
999
1037
  */
1000
- outputHasRoom(need: i32): boolean {
1038
+ private outputHasRoom(need: i32): boolean {
1001
1039
  if (this.output.length === 0) {
1002
1040
  this.output = this.pools.output.take()
1003
1041
  }
@@ -1019,17 +1057,17 @@ export class TlsRecordServer {
1019
1057
  * the content, its type and the padding together may not pass 2^14 + 1
1020
1058
  * (§5.4). Below 1 when the padding leaves no room at all.
1021
1059
  */
1022
- maxChunk(): i32 {
1060
+ private maxChunk(): i32 {
1023
1061
  return TLS_MAX_PLAINTEXT - this.writePadding()
1024
1062
  }
1025
1063
 
1026
1064
  /** The padding the next record carries: `padding` once records are protected, none in the clear. */
1027
- writePadding(): i32 {
1065
+ private writePadding(): i32 {
1028
1066
  return this.writeProtection.isProtected() ? this.padding : 0
1029
1067
  }
1030
1068
 
1031
1069
  /** The room a record of `len` content bytes takes in `output` under the write protection as it stands. */
1032
- recordRoom(len: i32): i32 {
1070
+ private recordRoom(len: i32): i32 {
1033
1071
  if (!this.writeProtection.isProtected()) {
1034
1072
  return TLS_RECORD_HEADER_SIZE + len
1035
1073
  }
@@ -1041,6 +1079,12 @@ export class TlsRecordServer {
1041
1079
  * under the write protection, padded when it is protected. False, writing
1042
1080
  * nothing, when it does not fit; a refusal from `seal` itself, which only a
1043
1081
  * mistake here could cause, fails the connection.
1082
+ *
1083
+ * @param type The content type to send, such as `TLS_CONTENT_HANDSHAKE`.
1084
+ * @param data The array that holds the content.
1085
+ * @param off Where in `data` the content starts.
1086
+ * @param len How many bytes of content, from `off`, to seal into this one record.
1087
+ * @returns `true` when the record was added to `output`; `false` when it did not fit or `seal` refused it.
1044
1088
  */
1045
1089
  sealRecord(type: i32, data: u8[], off: i32, len: i32): boolean {
1046
1090
  const room: i32 = this.recordRoom(len)
@@ -1073,7 +1117,7 @@ export class TlsRecordServer {
1073
1117
  * `maxChunk()` bytes, and answers where it stopped. A padding that leaves
1074
1118
  * no room for content fails the connection with `internal_error`.
1075
1119
  */
1076
- sealQueue(queue: u8[], at: i32, length: i32): i32 {
1120
+ private sealQueue(queue: u8[], at: i32, length: i32): i32 {
1077
1121
  let next: i32 = at
1078
1122
  const most: i32 = this.maxChunk()
1079
1123
  if (most < 1 && next < length) {
@@ -1099,7 +1143,7 @@ export class TlsRecordServer {
1099
1143
  * application secrets wiped in `TlsServer` once they are installed here. A
1100
1144
  * KeyUpdate owed to the client goes last.
1101
1145
  */
1102
- pump(): void {
1146
+ private pump(): void {
1103
1147
  if (this.state === TLS_RECORD_STATE_FAILED) {
1104
1148
  return
1105
1149
  }
@@ -1191,7 +1235,7 @@ export class TlsRecordServer {
1191
1235
  * to the next secret (§4.6.3, §7.2), the old one wiped. False, sending
1192
1236
  * nothing, when it does not fit in `output` yet.
1193
1237
  */
1194
- sendKeyUpdate(request: boolean): boolean {
1238
+ private sendKeyUpdate(request: boolean): boolean {
1195
1239
  const message: u8[] = [toU8(TLS_HANDSHAKE_KEY_UPDATE), toU8(0), toU8(0), toU8(1), toU8(request ? 1 : 0)]
1196
1240
  if (!this.sealRecord(TLS_CONTENT_HANDSHAKE, message, TLS_RECORD_SERVER_FROM, TLS_KEY_UPDATE_SIZE)) {
1197
1241
  return false
@@ -1213,6 +1257,10 @@ export class TlsRecordServer {
1213
1257
  * is under way or `output` is full, `TLS_RECORD_PIPE` once the server has
1214
1258
  * closed or the connection failed, or `TLS_RECORD_RESET` when the next keys
1215
1259
  * do not install and the connection fails on it.
1260
+ *
1261
+ * @param requestPeer `true` to set `request_update`, so that the client sends a KeyUpdate of its own.
1262
+ * @returns 0 when the KeyUpdate was sealed and the write keys moved on, or one of the negative
1263
+ * `TLS_RECORD_*` codes above.
1216
1264
  */
1217
1265
  keyUpdate(requestPeer: boolean): i32 {
1218
1266
  if (this.state === TLS_RECORD_STATE_FAILED || this.state === TLS_RECORD_STATE_CLOSED) {
@@ -1234,6 +1282,10 @@ export class TlsRecordServer {
1234
1282
  * was still owed is dropped: the alert is the last thing the server sends,
1235
1283
  * so once it is sealed every key the connection holds is wiped, the
1236
1284
  * traffic keys and secrets as well as what `TlsServer` holds.
1285
+ *
1286
+ * @param alert The `TLS_ALERT_*` description to send, such as `TLS_ALERT_INTERNAL_ERROR`. It is not sent
1287
+ * once the connection is `TLS_RECORD_STATE_CLOSED` (set when `close` is called, before its
1288
+ * `close_notify` is sealed), and a second call does nothing.
1237
1289
  */
1238
1290
  fail(alert: i32): void {
1239
1291
  if (this.state === TLS_RECORD_STATE_FAILED) {
@@ -1254,7 +1306,7 @@ export class TlsRecordServer {
1254
1306
  }
1255
1307
 
1256
1308
  /** Seals an alert record of `level` and `description`; false, sending nothing, when `output` has no room for it. */
1257
- sendAlert(level: i32, description: i32): boolean {
1309
+ private sendAlert(level: i32, description: i32): boolean {
1258
1310
  const body: u8[] = [toU8(level), toU8(description)]
1259
1311
  const two: i32 = 2
1260
1312
  return this.sealRecord(TLS_CONTENT_ALERT, body, TLS_RECORD_SERVER_FROM, two)
@@ -1263,6 +1315,8 @@ export class TlsRecordServer {
1263
1315
  /**
1264
1316
  * Lets the first `n` bytes of `output` go, once the socket has taken them,
1265
1317
  * and seals what was waiting for the room.
1318
+ *
1319
+ * @param n How many bytes the socket wrote. It is clamped to 0 and to the bytes `output` holds.
1266
1320
  */
1267
1321
  consume(n: i32): void {
1268
1322
  const held: i32 = this.outputEnd - this.outputStart
@@ -1286,6 +1340,13 @@ export class TlsRecordServer {
1286
1340
  * `TLS_RECORD_AGAIN` while the handshake is under way or nothing fits,
1287
1341
  * `TLS_RECORD_PIPE` once the server has closed or the connection failed,
1288
1342
  * and `TLS_RECORD_INVALID` for a window outside `data`.
1343
+ *
1344
+ * @param data The array that holds the application data.
1345
+ * @param off Where in `data` the bytes start.
1346
+ * @param len How many bytes, from `off`, to send.
1347
+ * @returns How many bytes were sealed, from 1 up to `len` (0 for a `len` of 0 on an open connection
1348
+ * with Application keys and no KeyUpdate pending); `TLS_RECORD_RESET` when nothing was sealed
1349
+ * because the connection failed; or one of the negative codes above, whatever `len` is.
1289
1350
  */
1290
1351
  write(data: u8[], off: i32, len: i32): i32 {
1291
1352
  if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
@@ -1338,6 +1399,12 @@ export class TlsRecordServer {
1338
1399
  * waiting; `TLS_RECORD_RESET` once the connection has failed; and
1339
1400
  * `TLS_RECORD_INVALID` for a window outside `buf`. Reading makes room, so
1340
1401
  * records the buffer was full for are handled before it answers.
1402
+ *
1403
+ * @param buf Where the application data is written.
1404
+ * @param off Where in `buf` to start writing.
1405
+ * @param len How many bytes, at most, to read.
1406
+ * @returns How many bytes were written, which may be fewer than `len`, or 0 or a negative
1407
+ * `TLS_RECORD_*` code as above.
1341
1408
  */
1342
1409
  read(buf: u8[], off: i32, len: i32): i32 {
1343
1410
  if (!tlsRecordWindowFits(toI32(buf.length), off, len)) {
@@ -1392,14 +1459,20 @@ export class TlsRecordServer {
1392
1459
  * server's traffic secret are wiped as soon as it is sealed. The read keys
1393
1460
  * stay: the client may still send until its own `close_notify`.
1394
1461
  */
1395
- sealClose(): void {
1462
+ private sealClose(): void {
1396
1463
  if (this.closeDue && this.sendAlert(TLS_ALERT_LEVEL_WARNING, TLS_ALERT_CLOSE_NOTIFY)) {
1397
1464
  this.closeDue = false
1398
1465
  this.wipeWrite()
1399
1466
  }
1400
1467
  }
1401
1468
 
1402
- /** What the CertificateVerify signs, while `interest` says `TLS_RECORD_SIGN`; `null` otherwise. */
1469
+ /**
1470
+ * What the CertificateVerify signs, while `interest` says `TLS_RECORD_SIGN`; `null` otherwise.
1471
+ *
1472
+ * @returns The bytes to sign, the handshake's own array (read it, do not keep it), as
1473
+ * `TlsServer.signatureInput` answers them; `null` whenever the handshake is not in
1474
+ * `TLS_STATE_WAIT_SIGNATURE`, the connection being out of its handshake included.
1475
+ */
1403
1476
  signatureInput(): u8[] | null {
1404
1477
  if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
1405
1478
  return null
@@ -1412,6 +1485,12 @@ export class TlsRecordServer {
1412
1485
  * `tlsSignEcdsaP256` answers it) and seals the rest of the flight. Answers
1413
1486
  * 0, or the alert the connection failed with — `internal_error` when no
1414
1487
  * signature is due.
1488
+ *
1489
+ * @param signature The signature over `signatureInput()`, made with the certificate's private key.
1490
+ * @returns 0 when the flight is sealed, or the `TLS_ALERT_*` value the connection failed with;
1491
+ * `internal_error`, without failing it, on a connection already open or closed. A connection
1492
+ * that failed without an alert (a truncated stream, the peer's alert, `abort`) also answers 0:
1493
+ * `state` tells it from a sealed flight.
1415
1494
  */
1416
1495
  sign(signature: u8[]): i32 {
1417
1496
  if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
@@ -1433,6 +1512,18 @@ export class TlsRecordServer {
1433
1512
  * and seals the flight that answers it, resumed or in full. Answers 0, or
1434
1513
  * the alert the connection failed with — `internal_error` when no ticket
1435
1514
  * waits.
1515
+ *
1516
+ * @param current The carrier's current ticket key. It is read, not kept, and stays the carrier's to wipe.
1517
+ * @param currentInfo What the carrier knows of `current`: its id, when it was drawn, how many it sealed.
1518
+ * @param previous The carrier's previous ticket key, or `current` again when it has only one. It stays
1519
+ * the carrier's to wipe.
1520
+ * @param previousInfo What the carrier knows of `previous`.
1521
+ * @param strikes The carrier's record of tickets already resumed, where the one resumed now is recorded.
1522
+ * @param now The time now, in `monotonicNanos()`.
1523
+ * @returns 0 when the answering flight is sealed, or the `TLS_ALERT_*` value the connection failed with;
1524
+ * `internal_error`, without failing it, on a connection already open or closed. A connection
1525
+ * that failed without an alert (a truncated stream, the peer's alert, `abort`) also answers 0:
1526
+ * `state` tells it from a sealed flight.
1436
1527
  */
1437
1528
  resume(
1438
1529
  current: Secret<u8[]>,
@@ -1460,6 +1551,12 @@ export class TlsRecordServer {
1460
1551
  * ticket under `key` (`TlsServer.issue`) and queues its NewSessionTicket.
1461
1552
  * Whether one was queued; either way the resumption master secret is
1462
1553
  * wiped, and no second ticket is issued.
1554
+ *
1555
+ * @param key The carrier's current ticket key. It is read, not kept, and stays the carrier's to wipe.
1556
+ * @param info What the carrier knows of `key`: its id, when it was drawn, how many it sealed.
1557
+ * @param now The time now, in `monotonicNanos()`.
1558
+ * @returns `true` when a NewSessionTicket was queued; `false` when none was, including on a connection
1559
+ * that is not open.
1463
1560
  */
1464
1561
  issue(key: Secret<u8[]>, info: TlsTicketKeyInfo, now: i64): boolean {
1465
1562
  if (this.state !== TLS_RECORD_STATE_OPEN) {
@@ -1476,6 +1573,9 @@ export class TlsRecordServer {
1476
1573
  * bytes), `read` (application data, or the end of the client's stream),
1477
1574
  * sign, handle a ticket, or nothing more ever (`TLS_RECORD_DONE`: failed
1478
1575
  * or closed, and everything is sent).
1576
+ *
1577
+ * @returns `TLS_RECORD_WANT_READ`, `TLS_RECORD_WANT_WRITE`, `TLS_RECORD_DATA`, `TLS_RECORD_SIGN`,
1578
+ * `TLS_RECORD_TICKET` and `TLS_RECORD_DONE` ORed together; 0 when there is nothing to do yet.
1479
1579
  */
1480
1580
  interest(): i32 {
1481
1581
  let bits: i32 = 0