@amritk/nish-aarch64-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
@@ -51,7 +51,9 @@
51
51
  * connection answers `H1_NEED_MORE` until the socket has drained half the
52
52
  * output, then `H1_WRITE`. Until then it reads nothing, so the window the
53
53
  * program is writing from stays valid: back-pressure on the way out. Nothing
54
- * buffers a whole body.
54
+ * buffers a whole body. On an `Http1Server` slot `sendBody` sends a body with
55
+ * a length from the program's own array, the queued output with it, and
56
+ * copies only what the socket did not take, under the same rules.
55
57
  *
56
58
  * **What the server decides.** The framing field (`Content-Length` or
57
59
  * `Transfer-Encoding: chunked`) and the `Connection` field are the server's;
@@ -127,7 +129,7 @@ import {
127
129
  HttpCompressor,
128
130
  httpAcceptEncoding,
129
131
  } from "nish/net/http-compress"
130
- import { netClose, netMss, netRead, netWrite, tcpAccept } from "nish:net"
132
+ import { netClose, netMss, netRead, netWrite, netWrite2, tcpAccept } from "nish:net"
131
133
  import {
132
134
  HTTP1_BODY,
133
135
  HTTP1_CHUNKED,
@@ -195,6 +197,7 @@ export const H1_POOL_FULL: i32 = -105
195
197
 
196
198
  /** Interest bits, the same as `nish/net/tls/record-server`'s: the low two are `pollModify`'s events. */
197
199
  export const H1_WANT_READ: i32 = 1
200
+ /** The slot has output waiting for the socket: ask the readiness loop for writable. */
198
201
  export const H1_WANT_WRITE: i32 = 2
199
202
  /** The slot's connection is over and every byte is sent: `close` it. */
200
203
  export const H1_DONE: i32 = 16
@@ -247,7 +250,7 @@ const http1ServerCheckCap = (name: string, value: i32, low: i32, high: i32): voi
247
250
  }
248
251
 
249
252
  /** Panics unless `buf[off .. off + len)` is inside `buf`: a window past its buffer is the caller's mistake. */
250
- const http1ServerCheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
253
+ const http1ServerCheckWindow = (what: string, buf: readonly u8[], off: i32, len: i32): void => {
251
254
  const size: i32 = toI32(buf.length)
252
255
  if (off < 0 || len < 0 || off > size || len > size - off) {
253
256
  panic(`${what}: the window is outside the buffer`)
@@ -255,7 +258,7 @@ const http1ServerCheckWindow = (what: string, buf: u8[], off: i32, len: i32): vo
255
258
  }
256
259
 
257
260
  /** Copies `src[off .. off + n)` to `out` at `at`, which the caller has made sure holds it, and answers where it ends. */
258
- const http1ServerCopy = (out: u8[], at: i32, src: u8[], off: i32, n: i32): i32 => {
261
+ const http1ServerCopy = (out: u8[], at: i32, src: readonly u8[], off: i32, n: i32): i32 => {
259
262
  if (n > 0 && at >= 0 && off >= 0 && at <= toI32(out.length) - n && off <= toI32(src.length) - n) {
260
263
  // Both windows fit, as they do for every caller: one move, not a byte loop.
261
264
  copyBytes(out, at, src, off, n)
@@ -284,19 +287,19 @@ const http1ServerCopy = (out: u8[], at: i32, src: u8[], off: i32, n: i32): i32 =
284
287
  */
285
288
  export class HttpDate {
286
289
  /** The IMF-fixdate of `second`: what `current` answers, made once. */
287
- bytes: u8[]
290
+ private bytes: u8[]
288
291
  /** The second `bytes` holds, since 1970; -1 before the first `current`. */
289
- second: i64 = -1
292
+ private second: i64 = -1
290
293
  /** The instant `pin` fixed, in milliseconds since 1970, while `pinned`. */
291
- pinnedMs: i64 = 0
294
+ private pinnedMs: i64 = 0
292
295
  /** The clock reading `hold` took, in milliseconds since 1970, while `held`. */
293
- heldMs: i64 = 0
296
+ private heldMs: i64 = 0
294
297
  /** How many times `bytes` has been rewritten. */
295
- rewrites: i64 = 0
298
+ private rewrites: i64 = 0
296
299
  /** How many times the wall clock has been read. */
297
- reads: i64 = 0
298
- pinned: boolean = false
299
- held: boolean = false
300
+ private reads: i64 = 0
301
+ private pinned: boolean = false
302
+ private held: boolean = false
300
303
 
301
304
  /** A date on the wall clock, with nothing formatted yet. */
302
305
  constructor() {
@@ -309,6 +312,8 @@ export class HttpDate {
309
312
  * steps back. It allocates nothing, and the array is the same one each time,
310
313
  * so a caller copies it out rather than keeping it. A wall clock before 1970
311
314
  * or past 9999 answers the nearer of the two ends.
315
+ *
316
+ * @returns the object's own 29-byte IMF-fixdate of the current second, to copy before the next call rewrites it
312
317
  */
313
318
  current(): u8[] {
314
319
  let ms: i64 = this.pinnedMs
@@ -333,6 +338,9 @@ export class HttpDate {
333
338
  * Makes `current` answer `unixMs`, milliseconds since 1970, until `unpin`.
334
339
  * False, and nothing changed, for an instant before 1970-01-01T00:00:00Z or
335
340
  * after 9999-12-31T23:59:59.999Z, which an IMF-fixdate cannot name.
341
+ *
342
+ * @param unixMs the instant every later `current` names, in milliseconds since 1970
343
+ * @returns true once the date is pinned; false when the instant is out of the IMF-fixdate's range
336
344
  */
337
345
  pin(unixMs: i64): boolean {
338
346
  if (unixMs < 0 || unixMs / 1000 > HTTP_DATE_LAST_SECOND) {
@@ -366,17 +374,25 @@ export class HttpDate {
366
374
  }
367
375
 
368
376
  /** The wall clock, in milliseconds since 1970. */
369
- now(): i64 {
377
+ private now(): i64 {
370
378
  this.reads += 1
371
379
  return toI64(Date.now())
372
380
  }
373
381
 
374
- /** How many times the wall clock has been read: once per `hold`, and once per `current` outside one. */
382
+ /**
383
+ * How many times the wall clock has been read: once per `hold`, and once per `current` outside one.
384
+ *
385
+ * @returns the count of `Date.now()` reads since the date was made, for a test that checks a serve call reads once
386
+ */
375
387
  clockReads(): i64 {
376
388
  return this.reads
377
389
  }
378
390
 
379
- /** How many times the bytes have been rewritten: one per second that `current` was asked for, at most. */
391
+ /**
392
+ * How many times the bytes have been rewritten: one per second that `current` was asked for, at most.
393
+ *
394
+ * @returns the count of IMF-fixdate formats since the date was made, for a test that checks the once-a-second promise
395
+ */
380
396
  formats(): i64 {
381
397
  return this.rewrites
382
398
  }
@@ -493,16 +509,25 @@ const http1ServerReserved = (name: u8[]): boolean =>
493
509
  export class Http1OutputPool {
494
510
  // Widest first, so the layout packs without padding.
495
511
  free: u8[][]
496
- size: i32 = 0
512
+ private size: i32 = 0
497
513
  /** How many buffers the pool has made. */
498
514
  made: i32 = 0
499
515
 
516
+ /**
517
+ * An empty pool: no buffer is made until the first `take`.
518
+ *
519
+ * @param size the length in bytes of every buffer the pool makes
520
+ */
500
521
  constructor(size: i32) {
501
522
  this.size = size
502
523
  this.free = []
503
524
  }
504
525
 
505
- /** A buffer of `size` bytes: the one given back last, or a new one. */
526
+ /**
527
+ * A buffer of `size` bytes: the one given back last, or a new one.
528
+ *
529
+ * @internal Called by `Http1Connection` in this module, when it first needs room to write and when it unparks its input and head buffers.
530
+ */
506
531
  take(): u8[] {
507
532
  if (this.free.length > 0) {
508
533
  const buf: u8[] = this.free[this.free.length - 1]
@@ -513,7 +538,11 @@ export class Http1OutputPool {
513
538
  return new Array<u8>(this.size)
514
539
  }
515
540
 
516
- /** Takes back a buffer `take` gave. */
541
+ /**
542
+ * Takes back a buffer `take` gave.
543
+ *
544
+ * @internal Called by `Http1Connection` in this module, from `release` and `park`, once a borrowed buffer holds nothing it still needs.
545
+ */
517
546
  give(buf: u8[]): void {
518
547
  this.free.push(buf)
519
548
  }
@@ -562,14 +591,20 @@ export class Http1Connection {
562
591
  lastActive: i64 = 0
563
592
  /** When the head being read began to arrive, in `monotonicNanos`; meaningful while `headTimed`. */
564
593
  headSince: i64 = 0
565
- state: i32 = 0
566
- response: i32 = 0
594
+ private state: i32 = 0
595
+ private response: i32 = 0
567
596
  /**
568
597
  * How the response's body is framed: a length (0 or more), `HTTP1_CHUNKED`,
569
598
  * or `HTTP1_NO_BODY` for one that ends with the connection.
599
+ *
600
+ * @internal Read by `std/net/handler.ts` to tell a sized response from a chunked one when it checks a write against what is owed.
570
601
  */
571
602
  responseFraming: i32 = 0
572
- /** A response with a length: the bytes it still owes. */
603
+ /**
604
+ * A response with a length: the bytes it still owes.
605
+ *
606
+ * @internal Read by `std/net/handler.ts` to know how much of a sized body is still to write.
607
+ */
573
608
  responseLeft: i32 = 0
574
609
  outputStart: i32 = 0
575
610
  outputEnd: i32 = 0
@@ -579,24 +614,24 @@ export class Http1Connection {
579
614
  * the empty one is out of cache by the time a resting connection's next
580
615
  * request comes. Only the connection assigns `output`, and with it this.
581
616
  */
582
- outputCap: i32 = 0
617
+ private outputCap: i32 = 0
583
618
  /** The most input the connection buffers, unread: the cap `inputRoom` holds it to. */
584
619
  bufferLimit: i32 = 0
585
620
  /** A request's head has been read and its exchange is not over. */
586
- requestOpen: boolean = false
621
+ private requestOpen: boolean = false
587
622
  /** The request's body has been read to its end. */
588
- requestEnded: boolean = false
623
+ private requestEnded: boolean = false
589
624
  /** The response carries no body (HEAD, 204, 304): what is written is dropped. */
590
625
  responseEmpty: boolean = false
591
626
  /** The connection closes once this exchange is over. */
592
627
  closeAfter: boolean = false
593
628
  /** The program accepted the request's WebSocket handshake. */
594
- upgraded: boolean = false
629
+ private upgraded: boolean = false
595
630
  /** A write was held back; `next` answers nothing but `H1_WRITE` until there is room. */
596
631
  writeHeld: boolean = false
597
- pongPending: boolean = false
632
+ private pongPending: boolean = false
598
633
  /** The peer's byte stream has ended. */
599
- inputEnded: boolean = false
634
+ private inputEnded: boolean = false
600
635
  /** A head has begun to arrive and is not read whole yet: `headSince` is its deadline's start. */
601
636
  headTimed: boolean = false
602
637
  dataStart: i32 = 0
@@ -605,7 +640,7 @@ export class Http1Connection {
605
640
  opcode: i32 = 0
606
641
  /** `H1_ERROR`'s HTTP status or WebSocket close code, or `H1_WS_CLOSE`'s close code. */
607
642
  status: i32 = 0
608
- pongLength: i32 = 0
643
+ private pongLength: i32 = 0
609
644
  /**
610
645
  * The TCP maximum segment size the program sets for this connection, for
611
646
  * `respondEncoded` under `HttpCompressor.segmentMode`; 0, the default,
@@ -615,7 +650,15 @@ export class Http1Connection {
615
650
  mss: i32 = 0
616
651
  /** The MSS `Http1Server.accept` read from this connection's socket (`netMss`), or 0 when it reports none. */
617
652
  socketMss: i32 = 0
618
- config: Http1Config
653
+ /**
654
+ * The plain TCP socket the carrier sends `output` on, which `sendBody`
655
+ * writes to directly: `Http1Server.accept` sets it, and `restart` and
656
+ * `abort` put it back to -1. -1 for a connection with no such socket (a
657
+ * sans-IO carrier, `Http1TlsServer`'s TLS records), where `sendBody` copies
658
+ * as `write` does.
659
+ */
660
+ fd: i32 = -1
661
+ private config: Http1Config
619
662
  /**
620
663
  * The frame decoder: made when the slot first switches to WebSocket, so a
621
664
  * slot that only ever speaks HTTP never holds its buffers, then kept for
@@ -627,9 +670,13 @@ export class Http1Connection {
627
670
  /** A ping's payload, kept until its pong fits: empty until the first ping. */
628
671
  pong: u8[]
629
672
  /** No fields, for the responses the connection writes itself: made once. */
630
- noFields: u8[][]
673
+ private noFields: u8[][]
631
674
 
632
- /** A connection under `config`. A cap outside what the connection can work with panics. */
675
+ /**
676
+ * A connection under `config`. A cap outside what the connection can work with panics.
677
+ *
678
+ * @param config the caps the connection is held to, read now and kept
679
+ */
633
680
  constructor(config: Http1Config) {
634
681
  const big: i32 = 1073741824
635
682
  const one: i32 = 1
@@ -656,8 +703,10 @@ export class Http1Connection {
656
703
  big - H1_LINE_OVERHEAD
657
704
  )
658
705
  this.config = config
659
- this.date = new HttpDate()
706
+ // The parser is made first: the arena places each block after the last,
707
+ // so its front fields sit on the lines just behind the connection's own.
660
708
  this.parser = new Http1Parser(config.maxTarget, config.maxHeaderBytes, config.maxHeaders, config.maxBody)
709
+ this.date = new HttpDate()
661
710
  this.parser.keepText = false
662
711
  this.parser.maxChunk = config.chunkSize
663
712
  this.ws = null
@@ -713,6 +762,7 @@ export class Http1Connection {
713
762
  this.headSince = 0
714
763
  this.mss = 0
715
764
  this.socketMss = 0
765
+ this.fd = -1
716
766
  this.release()
717
767
  this.park()
718
768
  }
@@ -745,28 +795,42 @@ export class Http1Connection {
745
795
  }
746
796
  }
747
797
 
748
- /** Takes back the buffers `park` gave away, before bytes are fed. */
798
+ /**
799
+ * Takes back the buffers `park` gave away, before bytes are fed. A parked
800
+ * buffer is `noOutput` itself, which a pointer compare finds without
801
+ * loading the empty array's header from a line the request touches nowhere
802
+ * else; any other empty buffer is still swapped for a lent one.
803
+ */
749
804
  unpark(): void {
750
805
  const inputs: Http1OutputPool | null = this.inputs
751
806
  const heads: Http1OutputPool | null = this.heads
752
807
  const p: Http1Parser = this.parser
753
- if (inputs !== null && toI32(p.buf.length) === 0) {
808
+ const none: u8[] = this.noOutput
809
+ if (inputs !== null && (p.buf === none || toI32(p.buf.length) === 0)) {
754
810
  p.buf = inputs.take()
755
811
  p.body = p.buf
756
812
  }
757
- if (heads !== null && toI32(p.head.length) === 0) {
813
+ if (heads !== null && (p.head === none || toI32(p.head.length) === 0)) {
758
814
  p.head = heads.take()
759
815
  }
760
816
  }
761
817
 
762
818
  // ---- Bytes in and out ---------------------------------------------------------
763
819
 
764
- /** The most input the connection holds unread: what `inputRoom` counts against. */
820
+ /**
821
+ * The most input the connection holds unread: what `inputRoom` counts against.
822
+ *
823
+ * @returns the cap in bytes on unread input: the buffer limit for HTTP, the message cap plus a frame header for WebSocket
824
+ */
765
825
  inputLimit(): i32 {
766
826
  return this.state === H1_S_WEBSOCKET ? this.config.maxMessage + 14 : this.bufferLimit
767
827
  }
768
828
 
769
- /** How many bytes `feed` would take now: none while a write is held back or once the connection is closing. */
829
+ /**
830
+ * How many bytes `feed` would take now: none while a write is held back or once the connection is closing.
831
+ *
832
+ * @returns how many more input bytes the connection takes now; 0 asks the carrier to stop reading
833
+ */
770
834
  inputRoom(): i32 {
771
835
  if (this.writeHeld || this.inputEnded || this.state === H1_S_CLOSING || this.state === H1_S_FAILED) {
772
836
  return 0
@@ -787,6 +851,11 @@ export class Http1Connection {
787
851
  * Takes as much of `buf[off .. off + len)` as `inputRoom` allows and
788
852
  * answers how much; the rest is the caller's to feed again later. A window
789
853
  * outside `buf` panics.
854
+ *
855
+ * @param buf the bytes the carrier read from the peer
856
+ * @param off where in `buf` the new bytes start
857
+ * @param len how many new bytes there are
858
+ * @returns how many of them the connection took, from `off`; the rest is the caller's to feed again
790
859
  */
791
860
  feed(buf: u8[], off: i32, len: i32): i32 {
792
861
  http1ServerCheckWindow("Http1Connection.feed", buf, off, len)
@@ -816,6 +885,7 @@ export class Http1Connection {
816
885
  this.outputEnd = 0
817
886
  this.pongPending = false
818
887
  this.writeHeld = false
888
+ this.fd = -1
819
889
  this.release()
820
890
  }
821
891
 
@@ -825,7 +895,7 @@ export class Http1Connection {
825
895
  * is written into it, which is why `room` calls it. It allocates only when
826
896
  * the pool has no free buffer, or for a connection's own, once.
827
897
  */
828
- acquire(): void {
898
+ private acquire(): void {
829
899
  if (this.outputCap > 0) {
830
900
  return
831
901
  }
@@ -854,7 +924,11 @@ export class Http1Connection {
854
924
  this.outputEnd = 0
855
925
  }
856
926
 
857
- /** Marks the first `n` bytes of the output sent. */
927
+ /**
928
+ * Marks the first `n` bytes of the output sent.
929
+ *
930
+ * @param n how many bytes from `outputStart` the carrier sent; more than are waiting counts as all of them
931
+ */
858
932
  consume(n: i32): void {
859
933
  const pending: i32 = this.outputEnd - this.outputStart
860
934
  this.outputStart = this.outputStart + (n < pending ? n : pending)
@@ -864,12 +938,20 @@ export class Http1Connection {
864
938
  }
865
939
  }
866
940
 
867
- /** Whether the output holds bytes to send. */
941
+ /**
942
+ * Whether the output holds bytes to send.
943
+ *
944
+ * @returns true while `output[outputStart .. outputEnd)` is not empty, so the carrier should wait for writable
945
+ */
868
946
  wantsWrite(): boolean {
869
947
  return this.outputEnd > this.outputStart
870
948
  }
871
949
 
872
- /** Whether the connection is over and everything is sent: the carrier closes it. */
950
+ /**
951
+ * Whether the connection is over and everything is sent: the carrier closes it.
952
+ *
953
+ * @returns true once the connection is closing or failed and no byte or pong is left to send
954
+ */
873
955
  isDone(): boolean {
874
956
  return (
875
957
  (this.state === H1_S_CLOSING || this.state === H1_S_FAILED) && !this.wantsWrite() && !this.pongPending
@@ -877,7 +959,7 @@ export class Http1Connection {
877
959
  }
878
960
 
879
961
  /** The room at the end of the output, once what was sent is moved out of the way. */
880
- room(): i32 {
962
+ private room(): i32 {
881
963
  this.acquire()
882
964
  const output: u8[] = this.output
883
965
  const live: i32 = this.outputEnd - this.outputStart
@@ -892,14 +974,18 @@ export class Http1Connection {
892
974
  }
893
975
 
894
976
  /** The room a response or a frame may use: all but the control reserve. */
895
- bodyRoom(): i32 {
977
+ private bodyRoom(): i32 {
896
978
  const r: i32 = this.room() - H1_CONTROL_RESERVE
897
979
  return r > 0 ? r : 0
898
980
  }
899
981
 
900
982
  // ---- Events -------------------------------------------------------------------
901
983
 
902
- /** The next event; see the module comment. */
984
+ /**
985
+ * The next event; see the module comment.
986
+ *
987
+ * @returns one of the `H1_` events: `H1_NEED_MORE` when there is nothing to hand over yet, `H1_ERROR` once the connection has failed
988
+ */
903
989
  next(): i32 {
904
990
  if (this.state === H1_S_FAILED) {
905
991
  return H1_ERROR
@@ -923,7 +1009,7 @@ export class Http1Connection {
923
1009
  }
924
1010
 
925
1011
  /** `next` while the connection speaks HTTP. */
926
- nextRequest(): i32 {
1012
+ private nextRequest(): i32 {
927
1013
  while (true) {
928
1014
  if (this.requestEnded) {
929
1015
  if (this.response !== H1_R_DONE) {
@@ -994,7 +1080,7 @@ export class Http1Connection {
994
1080
  * Refuses the request with `status`: the response that says so, when none
995
1081
  * has started, and the close after it. Answers `H1_ERROR`.
996
1082
  */
997
- fail(status: i32): i32 {
1083
+ private fail(status: i32): i32 {
998
1084
  this.status = status
999
1085
  if (this.response === H1_R_NONE) {
1000
1086
  // The control reserve keeps room for this whatever the output holds.
@@ -1020,7 +1106,7 @@ export class Http1Connection {
1020
1106
  }
1021
1107
 
1022
1108
  /** The frame decoder, made on the slot's first switch to WebSocket. */
1023
- decoder(): WsDecoder {
1109
+ private decoder(): WsDecoder {
1024
1110
  const ws: WsDecoder | null = this.ws
1025
1111
  if (ws !== null) {
1026
1112
  return ws
@@ -1031,7 +1117,7 @@ export class Http1Connection {
1031
1117
  }
1032
1118
 
1033
1119
  /** `next` once the slot has switched to WebSocket. */
1034
- nextFrame(): i32 {
1120
+ private nextFrame(): i32 {
1035
1121
  while (true) {
1036
1122
  if (this.pongPending && !this.sendPong()) {
1037
1123
  return H1_NEED_MORE
@@ -1076,7 +1162,7 @@ export class Http1Connection {
1076
1162
  }
1077
1163
 
1078
1164
  /** Writes the pending pong if it fits, from the control reserve; answers whether it went. */
1079
- sendPong(): boolean {
1165
+ private sendPong(): boolean {
1080
1166
  this.room()
1081
1167
  const end: i32 = websocketWriteFrame(
1082
1168
  this.output,
@@ -1097,7 +1183,7 @@ export class Http1Connection {
1097
1183
  }
1098
1184
 
1099
1185
  /** Writes a close frame with `code` (none for 1005), from the control reserve. */
1100
- sendClose(code: i32): void {
1186
+ private sendClose(code: i32): void {
1101
1187
  this.room()
1102
1188
  const payload: u8[] | null = websocketClosePayload(code, "")
1103
1189
  // A code that may not be sent (1005, for a close that had none) is
@@ -1121,7 +1207,11 @@ export class Http1Connection {
1121
1207
 
1122
1208
  // ---- The response -------------------------------------------------------------
1123
1209
 
1124
- /** Whether the program may answer the current request now. */
1210
+ /**
1211
+ * Whether the program may answer the current request now.
1212
+ *
1213
+ * @returns true while a request's head has been read and no response to it has been started
1214
+ */
1125
1215
  answerable(): boolean {
1126
1216
  return this.state === H1_S_HTTP && this.requestOpen && this.response === H1_R_NONE
1127
1217
  }
@@ -1135,6 +1225,13 @@ export class Http1Connection {
1135
1225
  * name or value `http1WriteResponseHead` refuses, or a head larger than the
1136
1226
  * output; `H1_AGAIN` when the output has no room for it yet (`H1_WRITE`
1137
1227
  * says when to try again); `H1_CLOSED` with no request to answer.
1228
+ *
1229
+ * @param status the HTTP status code to send, 100 to 599 but not 101
1230
+ * @param names the program's field names, one per field; nothing checks their
1231
+ * case, and the reserved names are refused in any ASCII case
1232
+ * @param values each field's value, at the same index as its name
1233
+ * @param bodyLength the body's length in bytes, `HTTP1_CHUNKED`, or `HTTP1_NO_BODY`
1234
+ * @returns 0 once the head is queued, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
1138
1235
  */
1139
1236
  respond(status: i32, names: u8[][], values: u8[][], bodyLength: i32): i32 {
1140
1237
  if (!this.answerable()) {
@@ -1212,8 +1309,13 @@ export class Http1Connection {
1212
1309
  * when nothing was. A response with no body takes everything and sends
1213
1310
  * nothing. `H1_CLOSED` with no response under way. A window outside `buf`
1214
1311
  * panics.
1312
+ *
1313
+ * @param buf the body bytes to send, which stay the caller's
1314
+ * @param off where in `buf` the window starts
1315
+ * @param len how many bytes the window holds
1316
+ * @returns how many bytes from `off` were taken, or `H1_AGAIN`, `H1_INVALID` or `H1_CLOSED` as above
1215
1317
  */
1216
- write(buf: u8[], off: i32, len: i32): i32 {
1318
+ write(buf: readonly u8[], off: i32, len: i32): i32 {
1217
1319
  http1ServerCheckWindow("Http1Connection.write", buf, off, len)
1218
1320
  if (this.response !== H1_R_BODY || this.state !== H1_S_HTTP) {
1219
1321
  return H1_CLOSED
@@ -1252,11 +1354,75 @@ export class Http1Connection {
1252
1354
  return take
1253
1355
  }
1254
1356
 
1357
+ /**
1358
+ * `write` of `buf[off .. off + len)`, sent from `buf` itself where it can
1359
+ * be: under a `Content-Length` response on a connection whose carrier
1360
+ * named its socket (`fd`, set by `Http1Server.accept`) and with no write
1361
+ * held back, the queued output and the body leave in one `netWrite2`, and
1362
+ * only what the socket did not take is copied into the output, the way
1363
+ * `write` copies it (what does not fit is answered short, and `H1_WRITE`
1364
+ * says when to send the rest). Once it returns the connection holds no
1365
+ * reference to `buf`, so the caller may reuse or give back its bytes at
1366
+ * once. A chunked or empty response, a write held back (`H1_AGAIN` or a
1367
+ * short answer not yet followed by `H1_WRITE`), and a connection with no
1368
+ * socket (`Http1TlsServer`, a sans-IO carrier) copy all of it: it is
1369
+ * `write`, with the same bytes on the wire. Answers what `write` answers,
1370
+ * `H1_INVALID` for more than the response still owes; `H1_CLOSED` also
1371
+ * when the socket failed during the send, which aborts the connection. A
1372
+ * window outside `buf` panics.
1373
+ *
1374
+ * @param buf the body bytes to send, free for the caller to reuse once this returns
1375
+ * @param off where in `buf` the window starts
1376
+ * @param len how many bytes the window holds
1377
+ * @returns how many bytes from `off` were sent or queued, or the error `write` would answer
1378
+ */
1379
+ sendBody(buf: readonly u8[], off: i32, len: i32): i32 {
1380
+ http1ServerCheckWindow("Http1Connection.sendBody", buf, off, len)
1381
+ if (
1382
+ this.fd < 0 ||
1383
+ this.writeHeld ||
1384
+ this.response !== H1_R_BODY ||
1385
+ this.state !== H1_S_HTTP ||
1386
+ this.responseEmpty ||
1387
+ this.responseFraming < 0 ||
1388
+ len === 0 ||
1389
+ len > this.responseLeft
1390
+ ) {
1391
+ return this.write(buf, off, len)
1392
+ }
1393
+ const pending: i32 = this.outputEnd - this.outputStart
1394
+ const n: i32 = netWrite2(this.fd, this.output, this.outputStart, pending, buf, off, len)
1395
+ if (n < 0 && n !== H1_AGAIN) {
1396
+ this.abort()
1397
+ return H1_CLOSED
1398
+ }
1399
+ let sent: i32 = 0
1400
+ // pending + len > 0 here (len > 0 above), so netWrite2 never answers 0: it is a count, H1_AGAIN or a failure.
1401
+ if (n > 0) {
1402
+ this.lastActive = monotonicNanos()
1403
+ this.consume(n)
1404
+ sent = n > pending ? n - pending : 0
1405
+ this.responseLeft = this.responseLeft - sent
1406
+ }
1407
+ if (sent < len) {
1408
+ const copied: i32 = this.write(buf, off + sent, len - sent)
1409
+ if (copied > 0) {
1410
+ sent = sent + copied
1411
+ } else if (sent === 0) {
1412
+ return copied
1413
+ }
1414
+ }
1415
+ this.release()
1416
+ return sent
1417
+ }
1418
+
1255
1419
  /**
1256
1420
  * Ends the response: the last chunk of a chunked body. Answers 0;
1257
1421
  * `H1_INVALID` for a response with a length that still owes bytes;
1258
1422
  * `H1_AGAIN` when the last chunk does not fit yet; `H1_CLOSED` with no
1259
1423
  * response under way.
1424
+ *
1425
+ * @returns 0 once the response is complete, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
1260
1426
  */
1261
1427
  end(): i32 {
1262
1428
  if (this.response !== H1_R_BODY || this.state !== H1_S_HTTP) {
@@ -1294,6 +1460,8 @@ export class Http1Connection {
1294
1460
  * `HTTP_CODING_NONE` when the client refuses identity and does not take
1295
1461
  * gzip. Allocates nothing. Read at `H1_REQUEST`, `H1_BODY` or `H1_END`
1296
1462
  * alike: the head stays the request's until its response is done.
1463
+ *
1464
+ * @returns the `HTTP_CODING_` value the response should use, ready to pass to `respondEncoded`
1297
1465
  */
1298
1466
  acceptEncoding(): i32 {
1299
1467
  return this.acceptEncodingOf(HTTP_OFFER_GZIP)
@@ -1302,13 +1470,15 @@ export class Http1Connection {
1302
1470
  /**
1303
1471
  * Whether the current request's `Accept-Encoding` refuses the body as it
1304
1472
  * is (`httpIdentityRefused`); `respondEncoded` then gzips any body it can.
1473
+ *
1474
+ * @returns true when the client's `Accept-Encoding` rules out the body as it is, so only a compressed one is acceptable
1305
1475
  */
1306
1476
  identityRefused(): boolean {
1307
1477
  return this.acceptEncodingOf(toI32(0)) === HTTP_CODING_NONE
1308
1478
  }
1309
1479
 
1310
1480
  /** `httpAcceptEncoding` of the current request's `Accept-Encoding`, with `offered`. */
1311
- acceptEncodingOf(offered: i32): i32 {
1481
+ private acceptEncodingOf(offered: i32): i32 {
1312
1482
  const parser: Http1Parser = this.parser
1313
1483
  const i: i32 = parser.headerIndex("accept-encoding")
1314
1484
  const spans: i32[] = parser.spans
@@ -1328,6 +1498,9 @@ export class Http1Connection {
1328
1498
  * connection's `mss` when the program set it, else `enc.mss` when the
1329
1499
  * program set it to other than `HTTP_DEFAULT_MSS`, else the socket's
1330
1500
  * (`socketMss`), else `HTTP_DEFAULT_MSS`.
1501
+ *
1502
+ * @param enc the compressor whose own `mss` is the fallback after this connection's
1503
+ * @returns the segment size in bytes that segment counting uses for this connection
1331
1504
  */
1332
1505
  segmentMss(enc: HttpCompressor): i32 {
1333
1506
  if (this.mss > 0) {
@@ -1360,6 +1533,16 @@ export class Http1Connection {
1360
1533
  * larger than the output could ever hold or one `respond` refuses,
1361
1534
  * `H1_CLOSED` with no request to answer; 0 once it is queued. A window
1362
1535
  * outside `body` panics.
1536
+ *
1537
+ * @param status the HTTP status code to send, as `respond` takes it
1538
+ * @param names the program's field names, without `Content-Encoding` (or `Vary` under `enc.vary`)
1539
+ * @param values each field's value, at the same index as its name
1540
+ * @param body the program's bytes the body is in, read but not kept
1541
+ * @param off where in `body` the response body starts
1542
+ * @param len how many bytes the response body holds as it is
1543
+ * @param enc the compressor that gzips the body and adds the coding fields; its scratch output is reused per call
1544
+ * @param coding the coding chosen for this request, normally what `acceptEncoding` answered
1545
+ * @returns 0 once the whole response is queued, else `H1_AGAIN`, `H1_INVALID` or `H1_CLOSED` as above
1363
1546
  */
1364
1547
  respondEncoded(
1365
1548
  status: i32,
@@ -1447,7 +1630,7 @@ export class Http1Connection {
1447
1630
  }
1448
1631
 
1449
1632
  /** Takes back a head `respond` just queued: `outputEnd` and `closeAfter` as they were before it. */
1450
- unrespond(outputEnd: i32, closeAfter: boolean): void {
1633
+ private unrespond(outputEnd: i32, closeAfter: boolean): void {
1451
1634
  this.outputEnd = outputEnd
1452
1635
  this.closeAfter = closeAfter
1453
1636
  this.response = H1_R_NONE
@@ -1465,6 +1648,9 @@ export class Http1Connection {
1465
1648
  * request is not a valid opening handshake (the program answers 400, or 426
1466
1649
  * for the wrong version) or `protocol` is not a token; `H1_AGAIN` when the
1467
1650
  * output has no room yet; `H1_CLOSED` with no request to answer.
1651
+ *
1652
+ * @param protocol the subprotocol to name in `Sec-WebSocket-Protocol`, one the client offered, or "" for none
1653
+ * @returns 0 once the 101 is queued, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
1468
1654
  */
1469
1655
  acceptWebSocket(protocol: string): i32 {
1470
1656
  if (!this.answerable()) {
@@ -1497,6 +1683,13 @@ export class Http1Connection {
1497
1683
  * `websocketWriteFrame` refuses, a close (that is `closeWebSocket`), or one
1498
1684
  * larger than the output; `H1_AGAIN` when it does not fit yet; `H1_CLOSED`
1499
1685
  * when the slot is not an open WebSocket. A window outside `buf` panics.
1686
+ *
1687
+ * @param fin true when this frame ends its message; false for a fragment that continuations follow
1688
+ * @param opcode the frame's `WS_OP_` opcode: text, binary, continuation, ping or pong, never close
1689
+ * @param buf the payload bytes, copied into the output
1690
+ * @param off where in `buf` the window starts
1691
+ * @param len how many bytes the window holds
1692
+ * @returns 0 once the frame is queued, else `H1_INVALID`, `H1_AGAIN` or `H1_CLOSED` as above
1500
1693
  */
1501
1694
  sendFrame(fin: boolean, opcode: i32, buf: u8[], off: i32, len: i32): i32 {
1502
1695
  http1ServerCheckWindow("Http1Connection.sendFrame", buf, off, len)
@@ -1523,6 +1716,9 @@ export class Http1Connection {
1523
1716
  * then the connection is done once it is sent. Answers 0, `H1_INVALID` for a
1524
1717
  * code that may not be sent, or `H1_CLOSED` when the slot is not an open
1525
1718
  * WebSocket.
1719
+ *
1720
+ * @param code the close status code to send (RFC 6455 §7.4), such as 1000 for a normal close
1721
+ * @returns 0 once the close frame is queued, else `H1_INVALID` or `H1_CLOSED` as above
1526
1722
  */
1527
1723
  closeWebSocket(code: i32): i32 {
1528
1724
  if (this.state !== H1_S_WEBSOCKET) {
@@ -1536,13 +1732,17 @@ export class Http1Connection {
1536
1732
  return 0
1537
1733
  }
1538
1734
 
1539
- /** Whether the slot has switched to WebSocket and is open. */
1735
+ /**
1736
+ * Whether the slot has switched to WebSocket and is open.
1737
+ *
1738
+ * @returns true while `next` reads frames rather than requests and `sendFrame` may be called
1739
+ */
1540
1740
  isWebSocket(): boolean {
1541
1741
  return this.state === H1_S_WEBSOCKET
1542
1742
  }
1543
1743
 
1544
1744
  /** Whether nothing has moved for longer than the idle timeout, at `now` (`monotonicNanos`). */
1545
- idle(now: i64): boolean {
1745
+ private idle(now: i64): boolean {
1546
1746
  return now - this.lastActive > toI64(this.config.idleTimeout) * toI64(1000000)
1547
1747
  }
1548
1748
 
@@ -1552,6 +1752,8 @@ export class Http1Connection {
1552
1752
  * call it after each `feed`; a program that feeds a connection itself calls
1553
1753
  * it the same way. Bytes that wait while the previous exchange is still
1554
1754
  * open start no clock: they count from when it is over (`nextRequest`).
1755
+ *
1756
+ * @param now the current time in `monotonicNanos`, which the deadline counts from
1555
1757
  */
1556
1758
  headArrived(now: i64): void {
1557
1759
  if (this.parser.buffered() > 0) {
@@ -1566,6 +1768,8 @@ export class Http1Connection {
1566
1768
  * the handshake: a record trickled a byte at a time hands no plaintext to
1567
1769
  * `headArrived` until it is whole, so its bytes would otherwise only keep
1568
1770
  * the slot from going idle.
1771
+ *
1772
+ * @internal Called by `headArrived` and by `Http1TlsServer` in this module to start the slowloris clock on ciphertext.
1569
1773
  */
1570
1774
  headAwaited(now: i64): void {
1571
1775
  if (!this.headTimed && this.state === H1_S_HTTP && !this.requestOpen) {
@@ -1575,11 +1779,15 @@ export class Http1Connection {
1575
1779
  }
1576
1780
 
1577
1781
  /** Whether a head has been arriving for longer than `headTimeout` at `now` (`monotonicNanos`). */
1578
- headLate(now: i64): boolean {
1782
+ private headLate(now: i64): boolean {
1579
1783
  return this.headTimed && now - this.headSince > toI64(this.config.headTimeout) * toI64(1000000)
1580
1784
  }
1581
1785
 
1582
- /** Whether `expire` closes the slot at `now`: idle, or a head past its deadline. */
1786
+ /**
1787
+ * Whether `expire` closes the slot at `now`: idle, or a head past its deadline.
1788
+ *
1789
+ * @internal Called by `Http1Server.expire` and `Http1TlsServer.expire` in this module to pick the slots to close.
1790
+ */
1583
1791
  expired(now: i64): boolean {
1584
1792
  return this.idle(now) || this.headLate(now)
1585
1793
  }
@@ -1592,6 +1800,10 @@ export class Http1Connection {
1592
1800
  * `lastActive` when it ends. Counting a quiet keep-alive's clock from
1593
1801
  * `lastActive` put it in the past once it was quiet for longer than
1594
1802
  * `headTimeout`, so each `expire` walked every slot.
1803
+ *
1804
+ * @param now the current time in `monotonicNanos`
1805
+ * @returns the earliest `monotonicNanos` instant at which `expired` could answer true, so `expire` can skip the slot until then
1806
+ * @internal Called by `Http1Server.expire` and `Http1TlsServer.expire` in this module to skip the slots that cannot expire yet.
1595
1807
  */
1596
1808
  expiresNoSooner(now: i64): i64 {
1597
1809
  const idle: i64 = this.lastActive + toI64(this.config.idleTimeout) * toI64(1000000)
@@ -1623,7 +1835,7 @@ const http1ExpireFloor = (config: Http1Config): i64 => {
1623
1835
  * `accept` until it calls `close`, and its descriptor is `fd(slot)`.
1624
1836
  */
1625
1837
  export class Http1Server {
1626
- config: Http1Config
1838
+ private config: Http1Config
1627
1839
  /**
1628
1840
  * The connections built so far, slot `k`'s at `k`: one per slot ever
1629
1841
  * accepted into, and slot 0's once `connection` has asked for it.
@@ -1642,7 +1854,7 @@ export class Http1Server {
1642
1854
  */
1643
1855
  fds: i32[]
1644
1856
  /** Each used slot's interest as the loop last armed it, for `rearm`. */
1645
- armed: i32[]
1857
+ private armed: i32[]
1646
1858
  /** The output buffers the slots borrow while they write. */
1647
1859
  outputs: Http1OutputPool
1648
1860
  /** The input and head buffers resting slots lend, with `Http1Config.idleInput`. */
@@ -1656,19 +1868,19 @@ export class Http1Server {
1656
1868
  */
1657
1869
  free: i32[]
1658
1870
  /** Where the socket is read into before the connection takes the bytes. */
1659
- scratch: u8[]
1871
+ private scratch: u8[]
1660
1872
  /** The last accepted peer's address, in `nish:net`'s 18-byte form. */
1661
- peer: u8[]
1873
+ private peer: u8[]
1662
1874
  /** How many slots the pool may hold, which `size` answers. */
1663
- slots: i32 = 1
1875
+ private slots: i32 = 1
1664
1876
  /** The listening socket, which the program made with `tcpListen` and still owns. */
1665
- listener: i32 = -1
1877
+ private listener: i32 = -1
1666
1878
  /**
1667
1879
  * No slot can expire before this (`monotonicNanos`), so `expire` called
1668
1880
  * sooner walks none: it walks every slot only about once per the shorter
1669
1881
  * timeout, however many there are.
1670
1882
  */
1671
- expireAfter: i64 = 0
1883
+ private expireAfter: i64 = 0
1672
1884
  /** How many calls of `expire` walked every slot, for a program measuring its cost. */
1673
1885
  expireScans: i32 = 0
1674
1886
 
@@ -1678,6 +1890,10 @@ export class Http1Server {
1678
1890
  * allocated by the first `accept` into it and kept, so a pool sized for a
1679
1891
  * peak costs at start-up only the read scratch, the shared `Date` and the
1680
1892
  * output pool, and at most `size` connections once the peak has come.
1893
+ *
1894
+ * @param config the caps every slot's connection is held to
1895
+ * @param listener the listening socket from `tcpListen`, which stays the program's to close
1896
+ * @param size how many connections the pool serves at once; less than 1 counts as 1
1681
1897
  */
1682
1898
  constructor(config: Http1Config, listener: i32, size: i32) {
1683
1899
  this.config = config
@@ -1696,7 +1912,7 @@ export class Http1Server {
1696
1912
  }
1697
1913
 
1698
1914
  /** A new connection on this pool's outputs and `Date`, for the next slot built. */
1699
- build(): Http1Connection {
1915
+ private build(): Http1Connection {
1700
1916
  const conn: Http1Connection = new Http1Connection(this.config)
1701
1917
  conn.pool = this.outputs
1702
1918
  conn.date = this.date
@@ -1708,12 +1924,20 @@ export class Http1Server {
1708
1924
  return conn
1709
1925
  }
1710
1926
 
1711
- /** How many slots the pool has, built or not. */
1927
+ /**
1928
+ * How many slots the pool has, built or not.
1929
+ *
1930
+ * @returns the pool's capacity in connections, as the constructor fixed it
1931
+ */
1712
1932
  size(): i32 {
1713
1933
  return this.slots
1714
1934
  }
1715
1935
 
1716
- /** How many slots hold a connection. */
1936
+ /**
1937
+ * How many slots hold a connection.
1938
+ *
1939
+ * @returns the count of slots accepted into and not yet closed
1940
+ */
1717
1941
  busy(): i32 {
1718
1942
  let n: i32 = 0
1719
1943
  for (const fd of this.fds) {
@@ -1724,12 +1948,22 @@ export class Http1Server {
1724
1948
  return n
1725
1949
  }
1726
1950
 
1727
- /** Whether `slot` is a slot of this pool that holds a connection. */
1951
+ /**
1952
+ * Whether `slot` is a slot of this pool that holds a connection.
1953
+ *
1954
+ * @param slot the slot index to ask about, any value
1955
+ * @returns true when the slot is in range and accepted into, not closed
1956
+ */
1728
1957
  holds(slot: i32): boolean {
1729
1958
  return slot >= 0 && slot < toI32(this.fds.length) && this.fds[slot] >= 0
1730
1959
  }
1731
1960
 
1732
- /** The descriptor of `slot`, for `pollAdd`; -1 for a free or unknown one. */
1961
+ /**
1962
+ * The descriptor of `slot`, for `pollAdd`; -1 for a free or unknown one.
1963
+ *
1964
+ * @param slot the slot `accept` answered
1965
+ * @returns the slot's socket descriptor, which the pool owns and `close` closes; -1 when the slot holds none
1966
+ */
1733
1967
  fd(slot: i32): i32 {
1734
1968
  return this.holds(slot) ? this.fds[slot] : -1
1735
1969
  }
@@ -1738,6 +1972,9 @@ export class Http1Server {
1738
1972
  * The connection of `slot`. A slot never accepted into, or out of range,
1739
1973
  * names slot 0's connection, which this builds when no slot has been yet
1740
1974
  * (the first `accept` then takes it, restarted).
1975
+ *
1976
+ * @param slot the slot `accept` answered
1977
+ * @returns the slot's connection, which the pool keeps; read its events and write its response through it
1741
1978
  */
1742
1979
  connection(slot: i32): Http1Connection {
1743
1980
  if (slot >= 0 && slot < toI32(this.connections.length)) {
@@ -1751,6 +1988,8 @@ export class Http1Server {
1751
1988
  * slot, which the program adds to its loop with `H1_WANT_READ`; -11 when
1752
1989
  * none is waiting, `H1_POOL_FULL` when every slot is busy (the connection
1753
1990
  * is closed), or `tcpAccept`'s own failure.
1991
+ *
1992
+ * @returns the new connection's slot, or a negative code as above
1754
1993
  */
1755
1994
  accept(): i32 {
1756
1995
  const fd: i32 = tcpAccept(this.listener, this.peer)
@@ -1777,6 +2016,7 @@ export class Http1Server {
1777
2016
  this.fds[k] = fd
1778
2017
  this.armed[k] = H1_WANT_READ
1779
2018
  conn.restart()
2019
+ conn.fd = fd
1780
2020
  // Once a connection, not a request, so a keep-alive request costs no
1781
2021
  // syscall: a path whose MSS moves after the handshake is not followed.
1782
2022
  const mss: i32 = netMss(fd)
@@ -1792,6 +2032,9 @@ export class Http1Server {
1792
2032
  * and the socket has more, then sends. Answers the interest. It begins a
1793
2033
  * serve call, which the program's `flush` of the slot ends: the responses
1794
2034
  * written in between carry one clock reading as their `Date`.
2035
+ *
2036
+ * @param slot the slot `accept` answered
2037
+ * @returns the `H1_WANT_` bits the slot now waits for, with `H1_DONE` once it should be closed
1795
2038
  */
1796
2039
  readable(slot: i32): i32 {
1797
2040
  if (!this.holds(slot)) {
@@ -1834,7 +2077,12 @@ export class Http1Server {
1834
2077
  return this.settle(slot)
1835
2078
  }
1836
2079
 
1837
- /** The socket of `slot` is writable: sends, and begins a serve call as `readable` does. Answers the interest. */
2080
+ /**
2081
+ * The socket of `slot` is writable: sends, and begins a serve call as `readable` does. Answers the interest.
2082
+ *
2083
+ * @param slot the slot `accept` answered
2084
+ * @returns the `H1_WANT_` bits the slot now waits for, with `H1_DONE` once it should be closed
2085
+ */
1838
2086
  writable(slot: i32): i32 {
1839
2087
  if (!this.holds(slot)) {
1840
2088
  return H1_DONE
@@ -1849,6 +2097,9 @@ export class Http1Server {
1849
2097
  * for is made here and `H1_WRITE` comes in the same pass; otherwise what
1850
2098
  * the program writes in answer to one event and the next goes out together
1851
2099
  * at `flush`, as one segment rather than several.
2100
+ *
2101
+ * @param slot the slot `accept` answered
2102
+ * @returns the connection's next `H1_` event, as `Http1Connection.next` answers it
1852
2103
  */
1853
2104
  next(slot: i32): i32 {
1854
2105
  if (!this.holds(slot)) {
@@ -1866,6 +2117,9 @@ export class Http1Server {
1866
2117
  * answers the interest: `H1_DONE` once the connection is over and sent,
1867
2118
  * otherwise read while it has room and write while it holds bytes. It ends
1868
2119
  * the serve call, so a response written after it reads the clock anew.
2120
+ *
2121
+ * @param slot the slot `accept` answered
2122
+ * @returns the `H1_WANT_` bits to arm the slot with, or `H1_DONE` when the program should `close` it
1869
2123
  */
1870
2124
  flush(slot: i32): i32 {
1871
2125
  this.date.unhold()
@@ -1873,7 +2127,7 @@ export class Http1Server {
1873
2127
  }
1874
2128
 
1875
2129
  /** `flush` within a serve call: sends, answers the interest, and keeps the clock reading. */
1876
- settle(slot: i32): i32 {
2130
+ private settle(slot: i32): i32 {
1877
2131
  if (!this.holds(slot)) {
1878
2132
  return H1_DONE
1879
2133
  }
@@ -1894,7 +2148,7 @@ export class Http1Server {
1894
2148
  }
1895
2149
 
1896
2150
  /** Writes what `slot`'s connection holds, which it holds, until the socket would block. */
1897
- send(slot: i32): void {
2151
+ private send(slot: i32): void {
1898
2152
  const conn: Http1Connection = this.connections[slot]
1899
2153
  const fd: i32 = this.fds[slot]
1900
2154
  const pending: i32 = conn.outputEnd - conn.outputStart
@@ -1920,6 +2174,12 @@ export class Http1Server {
1920
2174
  * armed when it must. A keep-alive connection answers the same interest
1921
2175
  * request after request, and each `pollModify` is a system call
1922
2176
  * (`epoll_ctl`) that changes nothing.
2177
+ *
2178
+ * @param slot the slot `accept` answered
2179
+ * @param wants the interest bits a serve call answered; only `H1_WANT_READ` and `H1_WANT_WRITE` count
2180
+ * @returns true when the interest changed and the program should call
2181
+ * `pollModify`; false when `slot` is already armed for it, or is free or
2182
+ * out of range
1923
2183
  */
1924
2184
  rearm(slot: i32, wants: i32): boolean {
1925
2185
  const interest: i32 = wants & (H1_WANT_READ | H1_WANT_WRITE)
@@ -1930,7 +2190,11 @@ export class Http1Server {
1930
2190
  return true
1931
2191
  }
1932
2192
 
1933
- /** Closes `slot`'s socket, whatever is left unsent, and frees the slot. */
2193
+ /**
2194
+ * Closes `slot`'s socket, whatever is left unsent, and frees the slot.
2195
+ *
2196
+ * @param slot the slot to close; a free or unknown one is ignored
2197
+ */
1934
2198
  close(slot: i32): void {
1935
2199
  if (!this.holds(slot)) {
1936
2200
  return
@@ -1946,6 +2210,9 @@ export class Http1Server {
1946
2210
  /**
1947
2211
  * Closes every slot that has been idle past `idleTimeout`, or reading one
1948
2212
  * head for longer than `headTimeout`, at `now` (`monotonicNanos`); answers how many.
2213
+ *
2214
+ * @param now the current time in `monotonicNanos`, which every timeout is measured against
2215
+ * @returns how many slots this call closed
1949
2216
  */
1950
2217
  expire(now: i64): i32 {
1951
2218
  if (now < this.expireAfter) {
@@ -1980,7 +2247,7 @@ export class Http1Server {
1980
2247
  * `Http1Server`'s is.
1981
2248
  */
1982
2249
  export class Http1TlsServer {
1983
- config: Http1Config
2250
+ private config: Http1Config
1984
2251
  tls: TlsTcpServer
1985
2252
  /**
1986
2253
  * The connections built so far, slot `k`'s at `k`: one per slot ever
@@ -1994,18 +2261,18 @@ export class Http1TlsServer {
1994
2261
  * another slot's live request. Its first read allocates it, once per
1995
2262
  * server, and a caller must not change it.
1996
2263
  */
1997
- vacant: Http1Connection | null = null
2264
+ private vacant: Http1Connection | null = null
1998
2265
  /** The `Date` every slot's responses carry, as `Http1Server.date` is. */
1999
2266
  date: HttpDate
2000
2267
  /** Where TLS decrypts into before the connection takes the bytes. */
2001
- scratch: u8[]
2268
+ private scratch: u8[]
2002
2269
  /** The output buffers the slots borrow while they write. */
2003
2270
  outputs: Http1OutputPool
2004
2271
  /** The input and head buffers resting slots lend, with `Http1Config.idleInput`. */
2005
- inputs: Http1OutputPool
2006
- heads: Http1OutputPool
2272
+ private inputs: Http1OutputPool
2273
+ private heads: Http1OutputPool
2007
2274
  /** No slot can expire before this (`monotonicNanos`): `Http1Server.expireAfter`. */
2008
- expireAfter: i64 = 0
2275
+ private expireAfter: i64 = 0
2009
2276
  /** How many calls of `expire` walked every slot. */
2010
2277
  expireScans: i32 = 0
2011
2278
 
@@ -2015,6 +2282,11 @@ export class Http1TlsServer {
2015
2282
  * a slot builds its connection and, in `tls`, its handshake state, and
2016
2283
  * keeps them, so a pool sized for a peak costs at start-up only the read
2017
2284
  * scratches, the shared `Date` and the empty pools.
2285
+ *
2286
+ * @param tlsConfig the certificate chain and TLS settings every handshake uses
2287
+ * @param config the caps every slot's connection is held to
2288
+ * @param listener the listening socket from `tcpListen`, which stays the program's to close
2289
+ * @param size how many connections the pool serves at once
2018
2290
  */
2019
2291
  constructor(tlsConfig: TlsServerConfig, config: Http1Config, listener: i32, size: i32) {
2020
2292
  this.config = config
@@ -2032,14 +2304,14 @@ export class Http1TlsServer {
2032
2304
  * built. Called by `accept` alone, as `tls` builds slots: slot `k`'s
2033
2305
  * connection must sit at index `k` of `connections`.
2034
2306
  */
2035
- build(): Http1Connection {
2307
+ private build(): Http1Connection {
2036
2308
  const conn: Http1Connection = this.make()
2037
2309
  this.connections.push(conn)
2038
2310
  return conn
2039
2311
  }
2040
2312
 
2041
2313
  /** A connection on this pool's outputs and `Date` that no slot holds yet. */
2042
- make(): Http1Connection {
2314
+ private make(): Http1Connection {
2043
2315
  const conn: Http1Connection = new Http1Connection(this.config)
2044
2316
  conn.pool = this.outputs
2045
2317
  conn.date = this.date
@@ -2050,22 +2322,40 @@ export class Http1TlsServer {
2050
2322
  return conn
2051
2323
  }
2052
2324
 
2053
- /** How many slots the pool has, built or not. */
2325
+ /**
2326
+ * How many slots the pool has, built or not.
2327
+ *
2328
+ * @returns the pool's capacity in connections, as `tls` holds it
2329
+ */
2054
2330
  size(): i32 {
2055
2331
  return this.tls.size()
2056
2332
  }
2057
2333
 
2058
- /** How many slots hold a connection. */
2334
+ /**
2335
+ * How many slots hold a connection.
2336
+ *
2337
+ * @returns the count of slots accepted into and not yet closed, handshakes included
2338
+ */
2059
2339
  busy(): i32 {
2060
2340
  return this.tls.busy()
2061
2341
  }
2062
2342
 
2063
- /** Whether `slot` holds a connection. */
2343
+ /**
2344
+ * Whether `slot` holds a connection.
2345
+ *
2346
+ * @param slot the slot index to ask about, any value
2347
+ * @returns true when the slot is in range and accepted into, not closed
2348
+ */
2064
2349
  holds(slot: i32): boolean {
2065
2350
  return this.tls.holds(slot)
2066
2351
  }
2067
2352
 
2068
- /** The descriptor of `slot`, for `pollAdd`; -1 for a free one. */
2353
+ /**
2354
+ * The descriptor of `slot`, for `pollAdd`; -1 for a free one.
2355
+ *
2356
+ * @param slot the slot `accept` answered
2357
+ * @returns the slot's socket descriptor, which the pool owns and `close` closes; -1 when the slot holds none
2358
+ */
2069
2359
  fd(slot: i32): i32 {
2070
2360
  return this.tls.fd(slot)
2071
2361
  }
@@ -2075,7 +2365,7 @@ export class Http1TlsServer {
2075
2365
  * as `Http2TlsServer.live` asks: a slot accepted on `tls` directly holds a
2076
2366
  * TLS session and no `Http1Connection`, and reads as free here.
2077
2367
  */
2078
- live(slot: i32): boolean {
2368
+ private live(slot: i32): boolean {
2079
2369
  return this.tls.holds(slot) && slot < toI32(this.connections.length)
2080
2370
  }
2081
2371
 
@@ -2083,6 +2373,9 @@ export class Http1TlsServer {
2083
2373
  * The HTTP/1.1 connection of `slot`. A slot never accepted into answers
2084
2374
  * `vacant`, as a free slot answers a connection that holds nothing, and
2085
2375
  * builds no slot; a slot out of range names slot 0's.
2376
+ *
2377
+ * @param slot the slot `accept` answered
2378
+ * @returns the slot's connection, which the pool keeps; the shared `vacant` one, not to be changed, for a slot never built
2086
2379
  */
2087
2380
  connection(slot: i32): Http1Connection {
2088
2381
  const index: i32 = slot >= 0 && slot < this.tls.size() ? slot : 0
@@ -2098,12 +2391,22 @@ export class Http1TlsServer {
2098
2391
  return made
2099
2392
  }
2100
2393
 
2101
- /** The ALPN protocol `slot`'s handshake chose, or empty. */
2394
+ /**
2395
+ * The ALPN protocol `slot`'s handshake chose, or empty.
2396
+ *
2397
+ * @param slot the slot `accept` answered
2398
+ * @returns the protocol identifier the handshake agreed on, such as "http/1.1", or "" when none was chosen yet or at all
2399
+ */
2102
2400
  alpn(slot: i32): string {
2103
2401
  return this.tls.connection(slot).tls.alpn
2104
2402
  }
2105
2403
 
2106
- /** Whether `slot`'s handshake is done and chose a protocol other than `http/1.1`. */
2404
+ /**
2405
+ * Whether `slot`'s handshake is done and chose a protocol other than `http/1.1`.
2406
+ *
2407
+ * @param slot the slot `accept` answered
2408
+ * @returns true when the slot will be shut down rather than served, because its client wants another protocol
2409
+ */
2107
2410
  refused(slot: i32): boolean {
2108
2411
  const chosen: string = this.alpn(slot)
2109
2412
  return this.tls.connection(slot).state === TLS_RECORD_STATE_OPEN && chosen !== "" && chosen !== H1_ALPN
@@ -2114,6 +2417,10 @@ export class Http1TlsServer {
2114
2417
  * the caller's 32-byte server random and x25519 key, and gives it a fresh
2115
2418
  * connection, building the slot's on its first accept. Answers the slot, or
2116
2419
  * what `TlsTcpServer.accept` answers.
2420
+ *
2421
+ * @param serverRandom 32 fresh random bytes for the ServerHello
2422
+ * @param ephemeralPrivate a fresh x25519 private key for this handshake alone
2423
+ * @returns the new connection's slot, or the negative code `TlsTcpServer.accept` answers
2117
2424
  */
2118
2425
  accept(serverRandom: u8[], ephemeralPrivate: u8[]): i32 {
2119
2426
  const slot: i32 = this.tls.accept(serverRandom, ephemeralPrivate)
@@ -2130,7 +2437,12 @@ export class Http1TlsServer {
2130
2437
  return slot
2131
2438
  }
2132
2439
 
2133
- /** The socket of `slot` is readable: TLS reads it, and what the connection has to send goes after. Answers the interest. */
2440
+ /**
2441
+ * The socket of `slot` is readable: TLS reads it, and what the connection has to send goes after. Answers the interest.
2442
+ *
2443
+ * @param slot the slot `accept` answered
2444
+ * @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
2445
+ */
2134
2446
  readable(slot: i32): i32 {
2135
2447
  const live: boolean = this.live(slot)
2136
2448
  const now: i64 = monotonicNanos()
@@ -2150,13 +2462,24 @@ export class Http1TlsServer {
2150
2462
  return this.flush(slot)
2151
2463
  }
2152
2464
 
2153
- /** The socket of `slot` is writable. Answers the interest. */
2465
+ /**
2466
+ * The socket of `slot` is writable. Answers the interest.
2467
+ *
2468
+ * @param slot the slot `accept` answered
2469
+ * @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
2470
+ */
2154
2471
  writable(slot: i32): i32 {
2155
2472
  this.tls.writable(slot)
2156
2473
  return this.flush(slot)
2157
2474
  }
2158
2475
 
2159
- /** Signs `slot`'s CertificateVerify with `key`, as `TlsTcpServer.signP256` does. Answers the interest. */
2476
+ /**
2477
+ * Signs `slot`'s CertificateVerify with `key`, as `TlsTcpServer.signP256` does. Answers the interest.
2478
+ *
2479
+ * @param slot the slot whose handshake waits for its signature
2480
+ * @param key the certificate's P-256 private key, which stays the caller's to wipe
2481
+ * @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
2482
+ */
2160
2483
  signP256(slot: i32, key: Secret<u8[]>): i32 {
2161
2484
  this.tls.signP256(slot, key)
2162
2485
  return this.flush(slot)
@@ -2166,6 +2489,9 @@ export class Http1TlsServer {
2166
2489
  * Moves what TLS has decrypted for `slot` into its connection, as far as
2167
2490
  * the connection has room, and answers the connection's next event. A free
2168
2491
  * slot, or one whose ALPN was another protocol, answers `H1_ERROR`.
2492
+ *
2493
+ * @param slot the slot `accept` answered
2494
+ * @returns the connection's next `H1_` event, as `Http1Connection.next` answers it
2169
2495
  */
2170
2496
  next(slot: i32): i32 {
2171
2497
  if (!this.live(slot) || this.refused(slot)) {
@@ -2199,6 +2525,9 @@ export class Http1TlsServer {
2199
2525
  * and sends it; shuts TLS down once the connection is over or ALPN chose
2200
2526
  * another protocol. Answers TLS's interest, with the write bit while the
2201
2527
  * connection still holds bytes.
2528
+ *
2529
+ * @param slot the slot `accept` answered
2530
+ * @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
2202
2531
  */
2203
2532
  flush(slot: i32): i32 {
2204
2533
  if (!this.live(slot)) {
@@ -2221,7 +2550,7 @@ export class Http1TlsServer {
2221
2550
  * Writes what `slot`'s connection holds into TLS, as far as TLS takes it;
2222
2551
  * a slot never built has nothing to write.
2223
2552
  */
2224
- send(slot: i32): void {
2553
+ private send(slot: i32): void {
2225
2554
  if (slot < 0 || slot >= toI32(this.connections.length)) {
2226
2555
  return
2227
2556
  }
@@ -2240,7 +2569,11 @@ export class Http1TlsServer {
2240
2569
  conn.release()
2241
2570
  }
2242
2571
 
2243
- /** Ends `slot`'s connection and frees the slot at once, as `TlsTcpServer.close` does. */
2572
+ /**
2573
+ * Ends `slot`'s connection and frees the slot at once, as `TlsTcpServer.close` does.
2574
+ *
2575
+ * @param slot the slot to close
2576
+ */
2244
2577
  close(slot: i32): void {
2245
2578
  this.tls.close(slot)
2246
2579
  if (slot >= 0 && slot < toI32(this.connections.length)) {
@@ -2251,6 +2584,9 @@ export class Http1TlsServer {
2251
2584
  /**
2252
2585
  * Closes every slot that has been idle past `idleTimeout`, or reading one
2253
2586
  * head for longer than `headTimeout`, at `now` (`monotonicNanos`); answers how many.
2587
+ *
2588
+ * @param now the current time in `monotonicNanos`, which every timeout is measured against
2589
+ * @returns how many slots this call closed
2254
2590
  */
2255
2591
  expire(now: i64): i32 {
2256
2592
  if (now < this.expireAfter) {