nish 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/docs/AI.md +50 -4
  2. package/docs/INSTALL.md +73 -59
  3. package/package.json +19 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +4849 -430
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +252 -15
  15. package/scripts/fuzz.sh +1 -0
  16. package/scripts/undocumented.txt +14 -0
  17. package/std/README.md +107 -82
  18. package/std/base64.ts +34 -5
  19. package/std/bytes.ts +279 -59
  20. package/std/cli-args.ts +296 -70
  21. package/std/cli-flags.ts +184 -19
  22. package/std/cli.ts +475 -54
  23. package/std/collections.ts +121 -23
  24. package/std/compress/checksum.ts +44 -14
  25. package/std/compress/deflate.ts +224 -111
  26. package/std/compress/gzip.ts +217 -53
  27. package/std/compress/zlib.ts +63 -7
  28. package/std/crypto/aes.ts +128 -52
  29. package/std/crypto/base64url.ts +29 -5
  30. package/std/crypto/bcrypt-batch.ts +101 -34
  31. package/std/crypto/bcrypt.ts +93 -27
  32. package/std/crypto/chacha20poly1305.ts +69 -14
  33. package/std/crypto/ct.ts +32 -13
  34. package/std/crypto/ed25519.ts +66 -16
  35. package/std/crypto/hkdf.ts +198 -13
  36. package/std/crypto/hmac.ts +242 -37
  37. package/std/crypto/jwt.ts +204 -72
  38. package/std/crypto/p256.ts +222 -72
  39. package/std/crypto/p384.ts +51 -23
  40. package/std/crypto/pki-roots.ts +32 -2
  41. package/std/crypto/pki.ts +99 -30
  42. package/std/crypto/rsa.ts +56 -22
  43. package/std/crypto/sha1.ts +3 -0
  44. package/std/crypto/sha256.ts +24 -1
  45. package/std/crypto/sha512.ts +99 -14
  46. package/std/crypto/x25519.ts +163 -24
  47. package/std/crypto/x509-random.ts +10 -0
  48. package/std/crypto/x509.ts +133 -66
  49. package/std/host/ring.ts +124 -0
  50. package/std/json-equal.ts +31 -11
  51. package/std/json-format.ts +112 -13
  52. package/std/json-number.ts +76 -26
  53. package/std/json-pointer.ts +23 -1
  54. package/std/json-tokenizer.ts +126 -51
  55. package/std/json-typed.ts +757 -96
  56. package/std/json-value.ts +438 -157
  57. package/std/json.ts +170 -38
  58. package/std/map.ts +25 -7
  59. package/std/math.ts +36 -4
  60. package/std/net/buffer-pool.ts +42 -11
  61. package/std/net/dns-config.ts +109 -23
  62. package/std/net/dns-message.ts +62 -7
  63. package/std/net/dns-resolver.ts +247 -53
  64. package/std/net/dns.ts +203 -35
  65. package/std/net/fetch.ts +1267 -290
  66. package/std/net/handler.ts +296 -80
  67. package/std/net/hpack.ts +290 -77
  68. package/std/net/http-compress.ts +152 -36
  69. package/std/net/http-fields.ts +85 -9
  70. package/std/net/http1-client-codec.ts +129 -44
  71. package/std/net/http1-codec.ts +279 -114
  72. package/std/net/http1-server.ts +409 -139
  73. package/std/net/http2-frame.ts +289 -51
  74. package/std/net/http2-tls.ts +152 -49
  75. package/std/net/http2.ts +314 -160
  76. package/std/net/http3-frame.ts +165 -27
  77. package/std/net/http3-server.ts +282 -131
  78. package/std/net/http3.ts +377 -173
  79. package/std/net/outbound.ts +37 -3
  80. package/std/net/postgres-carrier.ts +144 -8
  81. package/std/net/postgres.ts +898 -260
  82. package/std/net/prefork.ts +202 -65
  83. package/std/net/qpack.ts +120 -32
  84. package/std/net/quic-conn-ack.ts +60 -12
  85. package/std/net/quic-conn-cid.ts +135 -27
  86. package/std/net/quic-conn-params.ts +109 -22
  87. package/std/net/quic-datagram.ts +50 -3
  88. package/std/net/quic-frame.ts +333 -23
  89. package/std/net/quic-listener.ts +161 -75
  90. package/std/net/quic-packet.ts +373 -73
  91. package/std/net/quic-recovery.ts +287 -65
  92. package/std/net/quic-stream.ts +436 -146
  93. package/std/net/quic.ts +693 -323
  94. package/std/net/router.ts +87 -40
  95. package/std/net/tls/client.ts +167 -51
  96. package/std/net/tls/codec.ts +494 -68
  97. package/std/net/tls/record-client.ts +140 -73
  98. package/std/net/tls/record-server.ts +233 -105
  99. package/std/net/tls/record.ts +142 -36
  100. package/std/net/tls/schedule.ts +235 -26
  101. package/std/net/tls/ticket.ts +161 -40
  102. package/std/net/tls/verify.ts +57 -10
  103. package/std/net/tls-tcp.ts +308 -95
  104. package/std/net/tls.ts +276 -102
  105. package/std/net/upstream.ts +81 -45
  106. package/std/net/url.ts +109 -22
  107. package/std/net/websocket.ts +140 -26
  108. package/std/net/webtransport.ts +243 -106
  109. package/std/pair.ts +10 -2
  110. package/std/random.ts +42 -15
  111. package/std/regex.ts +422 -257
  112. package/std/region.ts +70 -15
  113. package/std/secret.ts +52 -11
  114. package/std/sort.ts +17 -2
  115. package/std/sqlite.ts +256 -35
  116. package/std/sync/ring.ts +229 -56
  117. package/std/sync/shm.ts +203 -23
  118. package/std/term-io.ts +154 -78
  119. package/std/term-pixels.ts +144 -19
  120. package/std/term-probe.ts +77 -35
  121. package/std/term-rate.ts +199 -118
  122. package/std/term.ts +400 -175
  123. package/std/testing.ts +130 -32
  124. package/std/text.ts +84 -17
  125. package/std/threads.ts +248 -56
  126. package/std/yaml-json.ts +111 -97
  127. package/std/yaml-lines.ts +47 -15
  128. package/std/yaml-write.ts +45 -30
  129. package/std/yaml.ts +764 -384
@@ -14,24 +14,25 @@
14
14
  * }
15
15
  * const got: i32 = conn.read(buf, 0, buf.length); // application data
16
16
  *
17
- * **What it adds to `TlsClient`.** Records around the handshake messages:
18
- * the ClientHello in the clear, then the client's Finished under the
19
- * handshake keys, and every key installed at the record boundary RFC 8446
20
- * §5.1 puts it on. The middlebox-compatibility `change_cipher_spec` of §D.4,
21
- * which a client that sends a session id (this one always does) owes: one,
22
- * in the clear, before its second ClientHello after a HelloRetryRequest, or
23
- * else before its first protected record; the server's is dropped from its
24
- * first handshake record until its Finished. Alerts (§6) both ways, as the
25
- * server has them: `close_notify` ends the server's stream, `user_canceled`
26
- * is passed over, any other alert ends the connection, and a refusal of this
27
- * module's or of `TlsClient`'s is sent as a fatal alert under whatever keys
28
- * the client writes with. KeyUpdate (§4.6.3) both ways, with the server's
29
- * limits: `TLS_RECORD_IDLE_LIMIT` records in a row that carry nothing, and
30
- * `TLS_RECORD_MAX_KEY_UPDATES` KeyUpdates. A NewSessionTicket is handed to
31
- * `TlsClient`, which checks and drops it; any other post-handshake message is
32
- * `unexpected_message` (no client certificate is offered, so no
33
- * CertificateRequest). A stream that ends without `close_notify` is
34
- * truncation.
17
+ * **What it adds to `TlsClient`.** Records around the handshake messages: the
18
+ * ClientHello in the clear, then the client's Finished under the handshake
19
+ * keys, and every key installed at the record boundary RFC 8446 §5.1 puts it
20
+ * on. The middlebox-compatibility `change_cipher_spec` of §D.4, which a client
21
+ * that sends a session id (this one always does) owes: one, in the clear,
22
+ * before its second ClientHello after a HelloRetryRequest, or else before its
23
+ * first protected record; the server's is dropped from its first handshake
24
+ * record until its Finished.
25
+ *
26
+ * Alerts (§6) both ways, as the server has them: `close_notify` ends the
27
+ * server's stream, `user_canceled` is passed over, any other alert ends the
28
+ * connection, and a refusal of this module's or of `TlsClient`'s is sent as a
29
+ * fatal alert under whatever keys the client writes with. KeyUpdate (§4.6.3)
30
+ * both ways, with the server's limits: `TLS_RECORD_IDLE_LIMIT` records in a row
31
+ * that carry nothing, and `TLS_RECORD_MAX_KEY_UPDATES` KeyUpdates. A
32
+ * NewSessionTicket is handed to `TlsClient`, which checks and drops it; any
33
+ * other post-handshake message is `unexpected_message` (no client certificate
34
+ * is offered, so no CertificateRequest). A stream that ends without
35
+ * `close_notify` is truncation.
35
36
  *
36
37
  * **Authentication is the caller's.** At `TlsClient`'s `WAIT_AUTH` the
37
38
  * records after the server's CertificateVerify stay in the reader and
@@ -186,63 +187,81 @@ const tlsClientAdvanceHeld = (protection: TlsRecordProtection, held: Secret<u8[]
186
187
  * record the client writes carries after its content type (§5.4).
187
188
  */
188
189
  export class TlsRecordClient {
190
+ /** The handshake underneath: read it for the suite, ALPN, the server's certificates and the exporter secret. */
189
191
  tls: TlsClient
192
+ /** @internal Read by `net/fetch.ts`. */
190
193
  reader: TlsRecordReader
194
+ /** Opens the server's records; its sequence number counts the records read at the current keys. */
191
195
  readProtection: TlsRecordProtection
196
+ /** Seals the client's records; its sequence number counts the records written at the current keys. */
192
197
  writeProtection: TlsRecordProtection
193
198
  /** Never installed: seals the compatibility record in the clear once the write side is protected. */
194
- clearProtection: TlsRecordProtection
199
+ private clearProtection: TlsRecordProtection
195
200
  /** Bytes to send are `output[outputStart .. outputEnd)`. */
196
201
  output: u8[]
197
202
  /** Application data the caller has not read is `plain[plainStart .. plainEnd)`. */
198
- plain: u8[]
203
+ private plain: u8[]
199
204
  /** Where a record is opened to. */
200
- scratch: u8[]
205
+ private scratch: u8[]
201
206
  /** The server's and the client's current application traffic secrets, at both hash lengths. */
202
- readonly readTraffic256: Secret<u8[]>
203
- readonly readTraffic384: Secret<u8[]>
204
- readonly writeTraffic256: Secret<u8[]>
205
- readonly writeTraffic384: Secret<u8[]>
206
- readHash: i32 = 0
207
- writeHash: i32 = 0
207
+ private readonly readTraffic256: Secret<u8[]>
208
+ private readonly readTraffic384: Secret<u8[]>
209
+ private readonly writeTraffic256: Secret<u8[]>
210
+ private readonly writeTraffic384: Secret<u8[]>
211
+ private readHash: i32 = 0
212
+ private writeHash: i32 = 0
208
213
  /** The header of a post-handshake message the server has sent part of, and a KeyUpdate's body. */
209
- postHandshake: u8[]
214
+ private postHandshake: u8[]
215
+ /** Where the connection stands: one of the `TLS_RECORD_STATE_*` constants. */
210
216
  state: i32 = 0
217
+ /** The alert this client sent, or 0. */
211
218
  alert: i32 = 0
219
+ /** The error alert the server sent, or -1 when it sent none. */
212
220
  peerAlert: i32 = -1
221
+ /** How many zero bytes each protected record the client writes carries after its content type (§5.4). */
213
222
  padding: i32 = 0
214
223
  /** The `TLS_LEVEL_*` the server's records are read at. */
215
- readLevel: i32 = 0
224
+ private readLevel: i32 = 0
216
225
  /** The `TLS_LEVEL_*` the client's records are written at. */
217
- writeLevel: i32 = 0
226
+ private writeLevel: i32 = 0
227
+ /** The index in `output` of the first byte still to send; `consume(sent)` moves it on. */
218
228
  outputStart: i32 = 0
229
+ /** One past the last byte still to send in `output`; `outputStart` when nothing is pending. */
219
230
  outputEnd: i32 = 0
231
+ /** @internal Read by `net/fetch.ts`. */
220
232
  plainStart: i32 = 0
233
+ /** @internal Read by `net/fetch.ts`. */
221
234
  plainEnd: i32 = 0
222
235
  /** How much of `TlsClient`'s Initial-level output is already in records. */
223
- pendingInitialAt: i32 = 0
236
+ private pendingInitialAt: i32 = 0
224
237
  /** How much of `TlsClient`'s Handshake-level output is already in records. */
225
- pendingHandshakeAt: i32 = 0
238
+ private pendingHandshakeAt: i32 = 0
226
239
  /** How many bytes of the current post-handshake message's header or KeyUpdate are in `postHandshake`. */
227
- postHandshakeLength: i32 = 0
240
+ private postHandshakeLength: i32 = 0
228
241
  /** How many body bytes of a NewSessionTicket are still to come, handed to `TlsClient` as they arrive. */
229
- ticketLeft: i32 = 0
230
- idleRecords: i32 = 0
242
+ private ticketLeft: i32 = 0
243
+ private idleRecords: i32 = 0
244
+ /** How many KeyUpdates from the server this client has taken, up to `TLS_RECORD_MAX_KEY_UPDATES`. */
231
245
  keyUpdates: i32 = 0
246
+ /** True once the server's `close_notify` has arrived. */
232
247
  peerClosed: boolean = false
248
+ /** @internal Read by `net/fetch.ts`. */
233
249
  ended: boolean = false
234
250
  /** The server's first handshake record has arrived, which opens the window its `change_cipher_spec` may come in. */
235
- started: boolean = false
251
+ private started: boolean = false
236
252
  /** The client's compatibility `change_cipher_spec` is sent (§D.4). */
237
- ccsSent: boolean = false
238
- keyUpdateDue: boolean = false
239
- closeDue: boolean = false
253
+ private ccsSent: boolean = false
254
+ private keyUpdateDue: boolean = false
255
+ private closeDue: boolean = false
240
256
 
241
257
  /**
242
258
  * Wraps `tls`, which should be fresh, and starts it: the ClientHello is
243
259
  * sealed into `output` at once. A `tls` that has already failed, or whose
244
260
  * ClientHello cannot be written, fails this connection with its alert
245
261
  * queued in the clear.
262
+ *
263
+ * @param tls The handshake to carry, normally built and not yet started (a started one is carried from
264
+ * where it is); the connection keeps it as `tls` and wipes it in `wipe`.
246
265
  */
247
266
  constructor(tls: TlsClient) {
248
267
  this.tls = tls
@@ -294,7 +313,7 @@ export class TlsRecordClient {
294
313
  }
295
314
 
296
315
  /** Wipes the server's application traffic secret and the read keys. */
297
- wipeRead(): void {
316
+ private wipeRead(): void {
298
317
  exposeWith(this.readTraffic256, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
299
318
  exposeWith(this.readTraffic384, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
300
319
  this.readHash = 0
@@ -302,7 +321,7 @@ export class TlsRecordClient {
302
321
  }
303
322
 
304
323
  /** Wipes the client's application traffic secret and its write keys. */
305
- wipeWrite(): void {
324
+ private wipeWrite(): void {
306
325
  exposeWith(this.writeTraffic256, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
307
326
  exposeWith(this.writeTraffic384, TLS_RECORD_CLIENT_FROM, tlsZeroHeld)
308
327
  this.writeHash = 0
@@ -315,7 +334,7 @@ export class TlsRecordClient {
315
334
  * read keys from it. False when the secret is not known or the keys do not
316
335
  * install.
317
336
  */
318
- keepRead(): boolean {
337
+ private keepRead(): boolean {
319
338
  const tls: TlsClient = this.tls
320
339
  if (!tls.readKnown(TLS_LEVEL_APPLICATION)) {
321
340
  return false
@@ -336,7 +355,7 @@ export class TlsRecordClient {
336
355
  }
337
356
 
338
357
  /** `keepRead` for the client's application traffic secret and the write keys. */
339
- keepWrite(): boolean {
358
+ private keepWrite(): boolean {
340
359
  const tls: TlsClient = this.tls
341
360
  if (!tls.writeKnown(TLS_LEVEL_APPLICATION)) {
342
361
  return false
@@ -356,7 +375,11 @@ export class TlsRecordClient {
356
375
  return installed
357
376
  }
358
377
 
359
- /** How many bytes `receive` would take now: what the socket should read at most. */
378
+ /**
379
+ * How many bytes `receive` would take now: what the socket should read at most.
380
+ *
381
+ * @returns The free room in the record reader, in bytes: one largest record less what it holds unhandled.
382
+ */
360
383
  space(): i32 {
361
384
  return this.reader.space()
362
385
  }
@@ -368,6 +391,11 @@ export class TlsRecordClient {
368
391
  * outside `data`. Once the connection has failed, the server has sent
369
392
  * `close_notify`, or the client closed before the handshake was done, bytes
370
393
  * are taken and dropped (§6.1).
394
+ *
395
+ * @param data The buffer the socket was read into; the bytes are copied, not kept.
396
+ * @param off Where in `data` the bytes start.
397
+ * @param len How many bytes, from `off`, the socket gave.
398
+ * @returns How many of the `len` bytes were taken, or `TLS_RECORD_INVALID` (-22).
371
399
  */
372
400
  receive(data: u8[], off: i32, len: i32): i32 {
373
401
  if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
@@ -392,7 +420,7 @@ export class TlsRecordClient {
392
420
  * handled, an end without `close_notify` is truncation: the connection
393
421
  * fails with nothing more sent, and every key it holds is wiped.
394
422
  */
395
- checkTruncation(): void {
423
+ private checkTruncation(): void {
396
424
  if (this.ended && !this.peerClosed && this.state !== TLS_RECORD_STATE_FAILED && this.reader.next() <= 0) {
397
425
  this.state = TLS_RECORD_STATE_FAILED
398
426
  this.dropPending()
@@ -410,7 +438,7 @@ export class TlsRecordClient {
410
438
  }
411
439
 
412
440
  /** Forgets what is owed but not yet sealed: the handshake queues, a KeyUpdate and a close. */
413
- dropPending(): void {
441
+ private dropPending(): void {
414
442
  this.tls.clearOutput(TLS_LEVEL_INITIAL)
415
443
  this.tls.clearOutput(TLS_LEVEL_HANDSHAKE)
416
444
  this.pendingInitialAt = 0
@@ -425,7 +453,7 @@ export class TlsRecordClient {
425
453
  * verdict on the server's certificate the records stay where they are, and
426
454
  * `authenticate` comes back for them.
427
455
  */
428
- process(): void {
456
+ private process(): void {
429
457
  while (this.state !== TLS_RECORD_STATE_FAILED && !this.peerClosed && !this.abandoned()) {
430
458
  if (this.tls.state === TLS_CLIENT_STATE_WAIT_AUTH) {
431
459
  break
@@ -453,7 +481,7 @@ export class TlsRecordClient {
453
481
  }
454
482
 
455
483
  /** Whether a whole record's content fits beside what the caller has not read, moving that to the front when it would then fit. */
456
- plainHasRoom(): boolean {
484
+ private plainHasRoom(): boolean {
457
485
  const capacity: i32 = TLS_RECORD_CLIENT_PLAIN_SIZE
458
486
  if (capacity - (this.plainEnd - this.plainStart) < TLS_MAX_PLAINTEXT) {
459
487
  return false
@@ -470,7 +498,7 @@ export class TlsRecordClient {
470
498
  }
471
499
 
472
500
  /** Counts a record that carried nothing for the caller: 0, or `unexpected_message` once there are too many in a row. */
473
- idle(): i32 {
501
+ private idle(): i32 {
474
502
  this.idleRecords = this.idleRecords + 1
475
503
  return this.idleRecords > TLS_RECORD_IDLE_LIMIT ? TLS_ALERT_UNEXPECTED_MESSAGE : 0
476
504
  }
@@ -481,7 +509,7 @@ export class TlsRecordClient {
481
509
  * `scratch`, and its content goes to `handleApplicationRecord` once the
482
510
  * server's Finished is read, to the handshake before. Answers 0 or the alert.
483
511
  */
484
- handleRecord(buf: u8[], off: i32, n: i32): i32 {
512
+ private handleRecord(buf: u8[], off: i32, n: i32): i32 {
485
513
  if (toI32(buf[off]) === TLS_CONTENT_CHANGE_CIPHER_SPEC) {
486
514
  return this.handleChangeCipherSpec(buf, off, n - TLS_RECORD_HEADER_SIZE)
487
515
  }
@@ -511,7 +539,7 @@ export class TlsRecordClient {
511
539
  * between the records of one handshake message. Dropped; anything else is
512
540
  * `unexpected_message`.
513
541
  */
514
- handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
542
+ private handleChangeCipherSpec(buf: u8[], off: i32, length: i32): i32 {
515
543
  if (length !== 1 || toI32(buf[off + TLS_RECORD_HEADER_SIZE]) !== 1) {
516
544
  return TLS_ALERT_UNEXPECTED_MESSAGE
517
545
  }
@@ -522,7 +550,7 @@ export class TlsRecordClient {
522
550
  }
523
551
 
524
552
  /** Handshake bytes, `scratch[0 .. length)`, for `TlsClient` at the level they were read at; an empty fragment is `unexpected_message` (§5.1). */
525
- handleHandshake(length: i32): i32 {
553
+ private handleHandshake(length: i32): i32 {
526
554
  if (length === 0) {
527
555
  return TLS_ALERT_UNEXPECTED_MESSAGE
528
556
  }
@@ -541,7 +569,7 @@ export class TlsRecordClient {
541
569
  * content in `scratch`: application data for the caller, an alert, or
542
570
  * post-handshake handshake bytes, which nothing else may interrupt (§5.1).
543
571
  */
544
- handleApplicationRecord(length: i32): i32 {
572
+ private handleApplicationRecord(length: i32): i32 {
545
573
  const type: i32 = this.readProtection.contentType
546
574
  if ((this.postHandshakeLength > 0 || this.ticketLeft > 0) && type !== TLS_CONTENT_HANDSHAKE) {
547
575
  return TLS_ALERT_UNEXPECTED_MESSAGE
@@ -575,7 +603,7 @@ export class TlsRecordClient {
575
603
  * connection that never authenticated cannot end like one that did — ends
576
604
  * the connection with nothing more sent and every key wiped.
577
605
  */
578
- handleAlert(length: i32): i32 {
606
+ private handleAlert(length: i32): i32 {
579
607
  if (length !== 2) {
580
608
  return TLS_ALERT_DECODE_ERROR
581
609
  }
@@ -607,7 +635,7 @@ export class TlsRecordClient {
607
635
  * other type is `unexpected_message`. Each message counts against the idle
608
636
  * limit.
609
637
  */
610
- handlePostHandshake(length: i32): i32 {
638
+ private handlePostHandshake(length: i32): i32 {
611
639
  let k: i32 = 0
612
640
  while (k < length) {
613
641
  if (this.ticketLeft > 0) {
@@ -684,7 +712,7 @@ export class TlsRecordClient {
684
712
  * which `pump` sends. Answers 0, or `internal_error` when the keys do not
685
713
  * install.
686
714
  */
687
- updateReadKeys(requested: boolean): i32 {
715
+ private updateReadKeys(requested: boolean): i32 {
688
716
  const advanced: boolean =
689
717
  this.readHash === SHA384_SIZE
690
718
  ? tlsClientAdvanceHeld(this.readProtection, this.readTraffic384)
@@ -704,7 +732,7 @@ export class TlsRecordClient {
704
732
  * server's flight, after which its handshake secrets have done their work
705
733
  * and are wiped in `TlsClient`.
706
734
  */
707
- advance(): void {
735
+ private advance(): void {
708
736
  const tls: TlsClient = this.tls
709
737
  if (this.readLevel === TLS_LEVEL_INITIAL && tls.readKnown(TLS_LEVEL_HANDSHAKE)) {
710
738
  if (!tlsClientInstallHeld(this.readProtection, tls.suite, tls.secrets().serverHandshake)) {
@@ -731,7 +759,7 @@ export class TlsRecordClient {
731
759
  }
732
760
 
733
761
  /** Whether `need` more bytes fit in `output`, moving what is unsent to the front first. */
734
- outputHasRoom(need: i32): boolean {
762
+ private outputHasRoom(need: i32): boolean {
735
763
  const capacity: i32 = TLS_RECORD_CLIENT_OUTPUT_SIZE
736
764
  if (this.outputEnd + need > capacity && this.outputStart > 0) {
737
765
  const held: i32 = this.outputEnd - this.outputStart
@@ -745,17 +773,17 @@ export class TlsRecordClient {
745
773
  }
746
774
 
747
775
  /** The most content one record may carry under the write protection as it stands (§5.4). */
748
- maxChunk(): i32 {
776
+ private maxChunk(): i32 {
749
777
  return TLS_MAX_PLAINTEXT - this.writePadding()
750
778
  }
751
779
 
752
780
  /** The padding the next record carries: `padding` once records are protected, none in the clear. */
753
- writePadding(): i32 {
781
+ private writePadding(): i32 {
754
782
  return this.writeProtection.isProtected() ? this.padding : 0
755
783
  }
756
784
 
757
785
  /** The room a record of `len` content bytes takes in `output` under the write protection as it stands. */
758
- recordRoom(len: i32): i32 {
786
+ private recordRoom(len: i32): i32 {
759
787
  if (!this.writeProtection.isProtected()) {
760
788
  return TLS_RECORD_HEADER_SIZE + len
761
789
  }
@@ -767,7 +795,7 @@ export class TlsRecordClient {
767
795
  * under `protection`. False, writing nothing, when it does not fit; a
768
796
  * refusal from `seal` itself fails the connection.
769
797
  */
770
- sealWith(protection: TlsRecordProtection, type: i32, data: u8[], off: i32, len: i32): boolean {
798
+ private sealWith(protection: TlsRecordProtection, type: i32, data: u8[], off: i32, len: i32): boolean {
771
799
  const sealed: boolean = protection.isProtected()
772
800
  const padding: i32 = sealed ? this.padding : 0
773
801
  const room: i32 = sealed
@@ -786,7 +814,7 @@ export class TlsRecordClient {
786
814
  }
787
815
 
788
816
  /** `sealWith` under the write protection. */
789
- sealRecord(type: i32, data: u8[], off: i32, len: i32): boolean {
817
+ private sealRecord(type: i32, data: u8[], off: i32, len: i32): boolean {
790
818
  return this.sealWith(this.writeProtection, type, data, off, len)
791
819
  }
792
820
 
@@ -794,7 +822,7 @@ export class TlsRecordClient {
794
822
  * The compatibility `change_cipher_spec` (§D.4), in the clear whatever the
795
823
  * write side is under, once a connection. False when `output` has no room.
796
824
  */
797
- sealChangeCipherSpec(): boolean {
825
+ private sealChangeCipherSpec(): boolean {
798
826
  if (this.ccsSent) {
799
827
  return true
800
828
  }
@@ -815,7 +843,7 @@ export class TlsRecordClient {
815
843
  * `maxChunk()` bytes, and answers where it stopped. A padding that leaves
816
844
  * no room for content fails the connection with `internal_error`.
817
845
  */
818
- sealQueue(queue: u8[], at: i32, length: i32): i32 {
846
+ private sealQueue(queue: u8[], at: i32, length: i32): i32 {
819
847
  let next: i32 = at
820
848
  const most: i32 = this.maxChunk()
821
849
  if (most < 1 && next < length) {
@@ -842,7 +870,7 @@ export class TlsRecordClient {
842
870
  * application secrets wiped in `TlsClient` once they are installed here. A
843
871
  * KeyUpdate owed to the server goes last.
844
872
  */
845
- pump(): void {
873
+ private pump(): void {
846
874
  if (this.state === TLS_RECORD_STATE_FAILED) {
847
875
  return
848
876
  }
@@ -911,7 +939,7 @@ export class TlsRecordClient {
911
939
  * to the next secret (§4.6.3, §7.2), the old one wiped. False, sending
912
940
  * nothing, when it does not fit in `output` yet.
913
941
  */
914
- sendKeyUpdate(request: boolean): boolean {
942
+ private sendKeyUpdate(request: boolean): boolean {
915
943
  const message: u8[] = [toU8(TLS_HANDSHAKE_KEY_UPDATE), toU8(0), toU8(0), toU8(1), toU8(request ? 1 : 0)]
916
944
  if (!this.sealRecord(TLS_CONTENT_HANDSHAKE, message, TLS_RECORD_CLIENT_FROM, TLS_KEY_UPDATE_SIZE)) {
917
945
  return false
@@ -933,6 +961,10 @@ export class TlsRecordClient {
933
961
  * is under way or `output` is full, `TLS_RECORD_PIPE` once the client has
934
962
  * closed or the connection failed, or `TLS_RECORD_RESET` when the next keys
935
963
  * do not install.
964
+ *
965
+ * @param requestPeer True to ask the server to update its sending keys as well.
966
+ * @returns 0 once the KeyUpdate is sealed and the new write keys installed, or one of the negative
967
+ * `TLS_RECORD_*` values above.
936
968
  */
937
969
  keyUpdate(requestPeer: boolean): i32 {
938
970
  if (this.state === TLS_RECORD_STATE_FAILED || this.state === TLS_RECORD_STATE_CLOSED) {
@@ -953,7 +985,7 @@ export class TlsRecordClient {
953
985
  * the client writes with now. What was still owed is dropped, and once the
954
986
  * alert is sealed every key the connection holds is wiped.
955
987
  */
956
- fail(alert: i32): void {
988
+ private fail(alert: i32): void {
957
989
  if (this.state === TLS_RECORD_STATE_FAILED) {
958
990
  return
959
991
  }
@@ -969,13 +1001,19 @@ export class TlsRecordClient {
969
1001
  }
970
1002
 
971
1003
  /** Seals an alert record of `level` and `description`; false, sending nothing, when `output` has no room for it. */
972
- sendAlert(level: i32, description: i32): boolean {
1004
+ private sendAlert(level: i32, description: i32): boolean {
973
1005
  const body: u8[] = [toU8(level), toU8(description)]
974
1006
  const two: i32 = 2
975
1007
  return this.sealRecord(TLS_CONTENT_ALERT, body, TLS_RECORD_CLIENT_FROM, two)
976
1008
  }
977
1009
 
978
- /** Lets the first `n` bytes of `output` go, once the socket has taken them, and seals what was waiting for the room. */
1010
+ /**
1011
+ * Lets the first `n` bytes of `output` go, once the socket has taken them, and seals what was waiting
1012
+ * for the room.
1013
+ *
1014
+ * @param n How many bytes from `outputStart` the socket took; clamped to what `output` holds, and a
1015
+ * negative count lets nothing go.
1016
+ */
979
1017
  consume(n: i32): void {
980
1018
  const held: i32 = this.outputEnd - this.outputStart
981
1019
  let take: i32 = n < held ? n : held
@@ -998,6 +1036,13 @@ export class TlsRecordClient {
998
1036
  * handshake is under way or nothing fits, `TLS_RECORD_PIPE` once the client
999
1037
  * has closed or the connection failed, `TLS_RECORD_INVALID` for a window
1000
1038
  * outside `data`.
1039
+ *
1040
+ * @param data The bytes to send; only read, never kept.
1041
+ * @param off Where the bytes start in `data`.
1042
+ * @param len How many bytes to send; with `off`, a window that must lie inside `data`.
1043
+ * @returns How many bytes were sealed (0 only for a `len` of 0 on an open connection with Application
1044
+ * keys and no KeyUpdate pending), or a negative `TLS_RECORD_*` value, whatever `len` is:
1045
+ * `TLS_RECORD_RESET` when the connection failed during this call.
1001
1046
  */
1002
1047
  write(data: u8[], off: i32, len: i32): i32 {
1003
1048
  if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
@@ -1049,6 +1094,12 @@ export class TlsRecordClient {
1049
1094
  * 0; `TLS_RECORD_AGAIN` when nothing is waiting; `TLS_RECORD_RESET` once
1050
1095
  * the connection has failed; `TLS_RECORD_INVALID` for a window outside
1051
1096
  * `buf`.
1097
+ *
1098
+ * @param buf Where the bytes go.
1099
+ * @param off Where in `buf` the first byte goes.
1100
+ * @param len The most bytes to copy; with `off`, a window that must lie inside `buf`.
1101
+ * @returns How many bytes were copied, 0 at the server's `close_notify` or for a `len` of 0, or a
1102
+ * negative `TLS_RECORD_*` value as above.
1052
1103
  */
1053
1104
  read(buf: u8[], off: i32, len: i32): i32 {
1054
1105
  if (!tlsRecordWindowFits(toI32(buf.length), off, len)) {
@@ -1104,12 +1155,12 @@ export class TlsRecordClient {
1104
1155
  }
1105
1156
 
1106
1157
  /** Whether the client closed before its Finished was sealed, so the server's records are not read. */
1107
- abandoned(): boolean {
1158
+ private abandoned(): boolean {
1108
1159
  return this.state === TLS_RECORD_STATE_CLOSED && this.writeLevel !== TLS_LEVEL_APPLICATION
1109
1160
  }
1110
1161
 
1111
1162
  /** Seals the `close_notify` `close` owes once `output` has room, then wipes the write direction. */
1112
- sealClose(): void {
1163
+ private sealClose(): void {
1113
1164
  if (this.closeDue && this.sendAlert(TLS_ALERT_LEVEL_WARNING, TLS_ALERT_CLOSE_NOTIFY)) {
1114
1165
  this.closeDue = false
1115
1166
  this.wipeWrite()
@@ -1124,6 +1175,10 @@ export class TlsRecordClient {
1124
1175
  * sent. Answers 0, or a non-zero alert once the connection has failed
1125
1176
  * (`failure`) — a verdict given before one is due answers
1126
1177
  * `internal_error`.
1178
+ *
1179
+ * @param ok True when the caller accepts the server's certificate chain and CertificateVerify.
1180
+ * @returns 0 when the handshake goes on, or the alert the connection failed with; `internal_error`,
1181
+ * without failing it, on a connection already open or closed.
1127
1182
  */
1128
1183
  authenticate(ok: boolean): i32 {
1129
1184
  if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
@@ -1139,7 +1194,15 @@ export class TlsRecordClient {
1139
1194
  return this.state === TLS_RECORD_STATE_FAILED ? this.failure() : 0
1140
1195
  }
1141
1196
 
1142
- /** Refuses the server with the caller's `alert` (`TlsClient.reject`), sent; answers the alert. */
1197
+ /**
1198
+ * Refuses the server with the caller's `alert` (`TlsClient.reject`), sent; answers the alert.
1199
+ *
1200
+ * @param alert The TLS alert to send, a code from 1 to 255; `TlsClient.reject` takes any other value
1201
+ * as `bad_certificate`.
1202
+ * @returns The alert the connection failed with (see `TlsClient.reject`); when the connection is not
1203
+ * in its handshake, nothing is sent and the answer is `failure` for a failed connection, else
1204
+ * `internal_error`.
1205
+ */
1143
1206
  reject(alert: i32): i32 {
1144
1207
  if (this.state !== TLS_RECORD_STATE_HANDSHAKE) {
1145
1208
  return this.state === TLS_RECORD_STATE_FAILED ? this.failure() : TLS_ALERT_INTERNAL_ERROR
@@ -1153,6 +1216,8 @@ export class TlsRecordClient {
1153
1216
  * Why a failed connection failed, never 0: the alert the client sent, else
1154
1217
  * the server's error alert, else `internal_error` for a failure with no
1155
1218
  * alert of its own (truncation, `abort`, `close_notify` mid-handshake).
1219
+ *
1220
+ * @returns A TLS alert code, never 0.
1156
1221
  */
1157
1222
  failure(): i32 {
1158
1223
  if (this.alert !== 0) {
@@ -1165,6 +1230,8 @@ export class TlsRecordClient {
1165
1230
  * What the connection wants next, as `TLS_RECORD_*` bits: read the socket,
1166
1231
  * write it, `read`, the caller's verdict on the server (`TLS_RECORD_AUTH`),
1167
1232
  * or nothing more ever (`TLS_RECORD_DONE`).
1233
+ *
1234
+ * @returns The `TLS_RECORD_*` interest bits OR-ed together.
1168
1235
  */
1169
1236
  interest(): i32 {
1170
1237
  let bits: i32 = 0