@amritk/nish-x86_64-linux 0.35.0 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/INSTALL.md +6 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +9 -2
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +4612 -430
  6. package/runtime/vite.d.ts +30 -0
  7. package/runtime/vite.mjs +150 -0
  8. package/scripts/build.sh +108 -13
  9. package/std/base64.ts +34 -5
  10. package/std/bytes.ts +256 -40
  11. package/std/cli-args.ts +296 -70
  12. package/std/cli-flags.ts +184 -19
  13. package/std/cli.ts +475 -54
  14. package/std/collections.ts +121 -23
  15. package/std/compress/checksum.ts +44 -14
  16. package/std/compress/deflate.ts +224 -111
  17. package/std/compress/gzip.ts +217 -53
  18. package/std/compress/zlib.ts +63 -7
  19. package/std/crypto/aes.ts +80 -9
  20. package/std/crypto/base64url.ts +22 -0
  21. package/std/crypto/bcrypt-batch.ts +88 -21
  22. package/std/crypto/bcrypt.ts +60 -0
  23. package/std/crypto/chacha20poly1305.ts +53 -0
  24. package/std/crypto/ct.ts +32 -13
  25. package/std/crypto/ed25519.ts +47 -0
  26. package/std/crypto/hkdf.ts +198 -13
  27. package/std/crypto/hmac.ts +235 -31
  28. package/std/crypto/jwt.ts +192 -63
  29. package/std/crypto/p256.ts +159 -16
  30. package/std/crypto/p384.ts +28 -3
  31. package/std/crypto/pki-roots.ts +32 -2
  32. package/std/crypto/pki.ts +81 -15
  33. package/std/crypto/rsa.ts +33 -3
  34. package/std/crypto/sha1.ts +3 -0
  35. package/std/crypto/sha256.ts +24 -1
  36. package/std/crypto/sha512.ts +99 -14
  37. package/std/crypto/x25519.ts +152 -14
  38. package/std/crypto/x509-random.ts +10 -0
  39. package/std/crypto/x509.ts +67 -7
  40. package/std/json-equal.ts +19 -0
  41. package/std/json-format.ts +112 -13
  42. package/std/json-number.ts +64 -14
  43. package/std/json-pointer.ts +23 -1
  44. package/std/json-tokenizer.ts +100 -29
  45. package/std/json-typed.ts +713 -66
  46. package/std/json-value.ts +400 -124
  47. package/std/json.ts +138 -10
  48. package/std/map.ts +25 -7
  49. package/std/math.ts +36 -4
  50. package/std/net/buffer-pool.ts +42 -11
  51. package/std/net/dns-config.ts +109 -23
  52. package/std/net/dns-message.ts +62 -7
  53. package/std/net/dns-resolver.ts +247 -53
  54. package/std/net/dns.ts +203 -35
  55. package/std/net/fetch.ts +151 -61
  56. package/std/net/handler.ts +278 -80
  57. package/std/net/hpack.ts +262 -52
  58. package/std/net/http-compress.ts +125 -15
  59. package/std/net/http-fields.ts +85 -9
  60. package/std/net/http1-client-codec.ts +100 -19
  61. package/std/net/http1-codec.ts +235 -27
  62. package/std/net/http1-server.ts +343 -85
  63. package/std/net/http2-frame.ts +272 -35
  64. package/std/net/http2-tls.ts +135 -34
  65. package/std/net/http2.ts +247 -106
  66. package/std/net/http3-frame.ts +154 -17
  67. package/std/net/http3-server.ts +246 -96
  68. package/std/net/http3.ts +307 -115
  69. package/std/net/outbound.ts +37 -3
  70. package/std/net/postgres-carrier.ts +144 -8
  71. package/std/net/postgres.ts +723 -103
  72. package/std/net/prefork.ts +164 -31
  73. package/std/net/qpack.ts +105 -18
  74. package/std/net/quic-conn-ack.ts +59 -12
  75. package/std/net/quic-conn-cid.ts +124 -27
  76. package/std/net/quic-conn-params.ts +70 -10
  77. package/std/net/quic-datagram.ts +43 -3
  78. package/std/net/quic-frame.ts +286 -12
  79. package/std/net/quic-listener.ts +101 -32
  80. package/std/net/quic-packet.ts +302 -21
  81. package/std/net/quic-recovery.ts +228 -44
  82. package/std/net/quic-stream.ts +353 -91
  83. package/std/net/quic.ts +489 -171
  84. package/std/net/router.ts +87 -40
  85. package/std/net/tls/client.ts +166 -51
  86. package/std/net/tls/codec.ts +413 -56
  87. package/std/net/tls/record-client.ts +110 -55
  88. package/std/net/tls/record-server.ts +159 -59
  89. package/std/net/tls/record.ts +109 -12
  90. package/std/net/tls/schedule.ts +235 -26
  91. package/std/net/tls/ticket.ts +159 -40
  92. package/std/net/tls/verify.ts +49 -3
  93. package/std/net/tls-tcp.ts +250 -45
  94. package/std/net/tls.ts +221 -59
  95. package/std/net/upstream.ts +81 -45
  96. package/std/net/url.ts +97 -13
  97. package/std/net/websocket.ts +122 -12
  98. package/std/net/webtransport.ts +220 -89
  99. package/std/pair.ts +10 -2
  100. package/std/random.ts +34 -9
  101. package/std/regex.ts +352 -198
  102. package/std/region.ts +70 -15
  103. package/std/secret.ts +52 -11
  104. package/std/sort.ts +17 -2
  105. package/std/sqlite.ts +256 -35
  106. package/std/sync/ring.ts +205 -35
  107. package/std/sync/shm.ts +194 -16
  108. package/std/term-io.ts +137 -64
  109. package/std/term-pixels.ts +144 -19
  110. package/std/term-probe.ts +66 -25
  111. package/std/term-rate.ts +128 -57
  112. package/std/term.ts +332 -121
  113. package/std/testing.ts +130 -32
  114. package/std/text.ts +73 -7
  115. package/std/threads.ts +229 -41
  116. package/std/yaml-json.ts +94 -82
  117. package/std/yaml-lines.ts +47 -15
  118. package/std/yaml-write.ts +24 -16
  119. package/std/yaml.ts +764 -384
@@ -4,24 +4,46 @@
4
4
  * it, a `QuicListener` in front for the datagrams no slot owns, for a
5
5
  * program that owns its readiness loop.
6
6
  *
7
- * const fd = udpBind("0.0.0.0", 4433, 0);
8
- * const server = new Http3Server(quicConfig, new Http3Config(), fd, 64, listenerEntropy);
9
- * // quicConfig.alpn is ["h3"]; pollAdd(loop, fd, 1, 0)
10
- * // each time round the loop, with `now` in milliseconds:
11
- * pollWait(loop, ready, server.timeout(now));
12
- * server.clock(micros); // the same instant in microseconds, for the pacer
13
- * server.receive(now, key); // every datagram waiting; signs handshakes with `key`
14
- * server.tick(now); // the connections' timers
15
- * let slot: i32 = server.ready();
16
- * while (slot >= 0) {
17
- * const h3: Http3Connection = server.connection(slot);
18
- * let event: i32 = h3.next();
19
- * while (event !== H3_NEED_MORE && event !== H3_ERROR) { … ; event = h3.next(); }
20
- * slot = server.ready();
21
- * }
22
- * server.flush(now); // what every touched slot has to send
23
- * // at shutdown:
24
- * server.stop(); // releases every slot, wipes the static keys
7
+ * import { pollAdd, pollCreate, pollWait, udpBind } from "nish:net";
8
+ * import { monotonicNanos } from "nish:process";
9
+ * import { secret, wipe } from "nish:secret";
10
+ * import { H3_ERROR, H3_NEED_MORE, Http3Config, Http3Connection } from "nish/net/http3";
11
+ * import { Http3Server } from "nish/net/http3-server";
12
+ * import { QuicServerConfig } from "nish/net/quic";
13
+ * import { QUIC_LISTENER_ENTROPY_SIZE } from "nish/net/quic-listener";
14
+ *
15
+ * // `quicConfig.alpn` is ["h3"]; `privateKey` is the P-256 key of its certificate.
16
+ * const run = (quicConfig: QuicServerConfig, privateKey: u8[], rounds: i32): void => {
17
+ * const fd: i32 = udpBind("0.0.0.0", 4433, 0);
18
+ * const listenerEntropy: u8[] = new Array<u8>(QUIC_LISTENER_ENTROPY_SIZE);
19
+ * crypto.getRandomValues(listenerEntropy);
20
+ * const server = new Http3Server(quicConfig, new Http3Config(), fd, 64, listenerEntropy);
21
+ * const loop: i32 = pollCreate();
22
+ * pollAdd(loop, fd, 1, 0);
23
+ * const ready: i32[] = new Array<i32>(8);
24
+ * const key = secret(privateKey);
25
+ * for (let round: i32 = 0; round < rounds; round++) {
26
+ * pollWait(loop, ready, server.timeout(monotonicNanos() / 1000000));
27
+ * const nanos: i64 = monotonicNanos();
28
+ * const now: i64 = nanos / 1000000; // milliseconds
29
+ * server.clock(nanos / 1000); // the same instant in microseconds, for the pacer
30
+ * server.receive(now, key); // every datagram waiting; signs handshakes with `key`
31
+ * server.tick(now); // the connections' timers
32
+ * let slot: i32 = server.ready();
33
+ * while (slot >= 0) {
34
+ * const h3: Http3Connection = server.connection(slot);
35
+ * let event: i32 = h3.next();
36
+ * while (event !== H3_NEED_MORE && event !== H3_ERROR) {
37
+ * // answer the event here: `h3.stream`, `h3.fields`, `h3.respond`, ...
38
+ * event = h3.next();
39
+ * }
40
+ * slot = server.ready();
41
+ * }
42
+ * server.flush(now); // what every touched slot has to send
43
+ * }
44
+ * wipe(key);
45
+ * server.stop(); // releases every slot, wipes the static keys
46
+ * };
25
47
  *
26
48
  * **Routing.** A datagram goes to the slot that owns its first packet's
27
49
  * Destination Connection ID, found through a hash index of every slot's
@@ -123,8 +145,12 @@ export const H3_SERVER_WHEEL: i32 = 2048
123
145
  /** The most datagrams one `receive` reads, so one busy socket cannot hold the loop. */
124
146
  export const H3_SERVER_RECEIVE_BURST: i32 = 256
125
147
 
126
- /** The answers to datagrams no slot owns that may go at once, and the milliseconds that earn one more. */
148
+ /**
149
+ * The answer budget's size: how many datagrams no slot owns that could get a stateless reset or a Version
150
+ * Negotiation packet may reach the listener at once.
151
+ */
127
152
  export const H3_SERVER_ANSWER_BURST: i32 = 16
153
+ /** The milliseconds that earn the answer budget one more, up to `H3_SERVER_ANSWER_BURST`. */
128
154
  export const H3_SERVER_ANSWER_INTERVAL: i64 = 100
129
155
 
130
156
  /** The address form `nish:net` reads and writes. */
@@ -155,18 +181,26 @@ const H3_SERVER_BUSY: i32 = 1
155
181
  const http3ServerBucket = (time: i64): i32 => toI32(time % toI64(H3_SERVER_WHEEL))
156
182
 
157
183
  /**
158
- * The connection IDs of every slot, by hash: open addressing over
159
- * `table`, whose entries are an index into `keys` plus one. Entry `e`
160
- * belongs to slot `e / H3_SERVER_KEYS`.
184
+ * The connection IDs of every slot, by hash: an open-addressed table keyed
185
+ * with `salt`, each ID copied into the entry it was filed as. Each slot has
186
+ * `QUIC_CONN_LOCAL_CIDS + 1` entries, slot `s`'s from
187
+ * `s * (QUIC_CONN_LOCAL_CIDS + 1)` up: its local connection IDs, then the
188
+ * client's original DCID. The table has at least twice as many places as
189
+ * entries, so it never fills.
161
190
  */
162
191
  export class Http3CidIndex {
163
- table: i32[]
192
+ private table: i32[]
164
193
  /** Entry `e`'s ID is `keys[e * 20 .. e * 20 + lengths[e])`; a length of 0 is no entry. */
165
- keys: u8[]
166
- lengths: i32[]
194
+ private keys: u8[]
195
+ private lengths: i32[]
167
196
  /** The hash's key: eight bytes from the listener's entropy. */
168
197
  salt: u8[]
169
198
 
199
+ /**
200
+ * An empty index for `slots` slots.
201
+ * @param slots How many slots it files IDs for, each `QUIC_CONN_LOCAL_CIDS + 1` entries.
202
+ * @param salt The hash's key: its first eight bytes are copied (zeros past a shorter one); not kept.
203
+ */
170
204
  constructor(slots: i32, salt: u8[]) {
171
205
  const entries: i32 = slots * H3_SERVER_KEYS
172
206
  let size: i32 = 8
@@ -183,7 +217,7 @@ export class Http3CidIndex {
183
217
  }
184
218
 
185
219
  /** The bucket the ID `buf[at .. at + length)` starts its probe at. */
186
- bucket(buf: u8[], at: i32, length: i32): i32 {
220
+ private bucket(buf: u8[], at: i32, length: i32): i32 {
187
221
  let h: i64 = toI64(length)
188
222
  for (let k: i32 = 0; k < length && at + k >= 0 && at + k < toI32(buf.length); k++) {
189
223
  const j: i32 = k & 7
@@ -195,7 +229,7 @@ export class Http3CidIndex {
195
229
  }
196
230
 
197
231
  /** Whether entry `e` holds the ID `buf[at .. at + length)`. */
198
- holds(e: i32, buf: u8[], at: i32, length: i32): boolean {
232
+ private holds(e: i32, buf: u8[], at: i32, length: i32): boolean {
199
233
  if (e < 0 || e >= toI32(this.lengths.length) || this.lengths[e] !== length) {
200
234
  return false
201
235
  }
@@ -208,7 +242,13 @@ export class Http3CidIndex {
208
242
  return true
209
243
  }
210
244
 
211
- /** The slot owning the ID `buf[at .. at + length)`, or -1. */
245
+ /**
246
+ * The slot owning the ID `buf[at .. at + length)`.
247
+ * @param buf The bytes holding the ID, read in place and not kept.
248
+ * @param at Where the ID starts in `buf`.
249
+ * @param length The ID's length in bytes.
250
+ * @returns The slot whose entry holds exactly this ID, or -1 when none does (always for a length of 0).
251
+ */
212
252
  find(buf: u8[], at: i32, length: i32): i32 {
213
253
  const size: i32 = toI32(this.table.length)
214
254
  let h: i32 = this.bucket(buf, at, length)
@@ -225,7 +265,15 @@ export class Http3CidIndex {
225
265
  return -1
226
266
  }
227
267
 
228
- /** Files entry `e` as the ID `buf[at .. at + length)`; an ID already filed under another entry is left there. */
268
+ /**
269
+ * Files entry `e` as the ID `buf[at .. at + length)`; an ID already filed
270
+ * under any entry is left there, and nothing is filed.
271
+ * @param e The entry: slot `s`'s `j`th is `s * (QUIC_CONN_LOCAL_CIDS + 1) + j`. It must not be filed
272
+ * already (`remove` it first); one out of range files nothing.
273
+ * @param buf The bytes holding the ID, copied into the entry.
274
+ * @param at Where the ID starts in `buf`.
275
+ * @param length The ID's length in bytes, 1 to `QUIC_MAX_CID_LENGTH`; any other files nothing.
276
+ */
229
277
  insert(e: i32, buf: u8[], at: i32, length: i32): void {
230
278
  if (length <= 0 || length > QUIC_MAX_CID_LENGTH || e < 0 || e >= toI32(this.lengths.length)) {
231
279
  return
@@ -249,7 +297,10 @@ export class Http3CidIndex {
249
297
  }
250
298
  }
251
299
 
252
- /** Takes entry `e` out, moving later entries of its probe run back so none is lost. */
300
+ /**
301
+ * Takes entry `e` out, moving later entries of its probe run back so none is lost.
302
+ * @param e The entry, numbered as `insert` numbers it; one out of range or not filed is ignored.
303
+ */
253
304
  remove(e: i32): void {
254
305
  if (e < 0 || e >= toI32(this.lengths.length) || this.lengths[e] === 0) {
255
306
  return
@@ -294,21 +345,27 @@ export class Http3CidIndex {
294
345
  }
295
346
 
296
347
  /**
297
- * Slots filed by the millisecond of their next deadline: `head[b]` is a
298
- * bucket's first slot plus one, `nextOf` and `prevOf` link a bucket's slots,
299
- * and `bits` marks the buckets that hold any.
348
+ * Slots filed by the millisecond of their next deadline: `H3_SERVER_WHEEL`
349
+ * buckets, each a linked list of its slots, and a bitmap of the buckets that
350
+ * hold any, so `next` passes 64 empty buckets at a time. A slot is filed in
351
+ * at most one bucket.
300
352
  */
301
353
  export class Http3Wheel {
302
- head: i32[]
303
- nextOf: i32[]
304
- prevOf: i32[]
305
- /** The bucket a slot is filed in, or -1, and the deadline it was filed for. */
306
- bucketOf: i32[]
354
+ private head: i32[]
355
+ private nextOf: i32[]
356
+ private prevOf: i32[]
357
+ /** The bucket a slot is filed in, or -1. */
358
+ private bucketOf: i32[]
359
+ /** Per slot, the deadline in milliseconds `file` last filed it for, before any clamp to the horizon. */
307
360
  dueAt: i64[]
308
- bits: i64[]
361
+ private bits: i64[]
309
362
  /** Every bucket before this time has been run. */
310
363
  cursor: i64 = -1
311
364
 
365
+ /**
366
+ * An empty wheel whose cursor is not started.
367
+ * @param slots How many slots it files, 0 to `slots - 1`.
368
+ */
312
369
  constructor(slots: i32) {
313
370
  this.head = new Array<i32>(H3_SERVER_WHEEL)
314
371
  this.nextOf = new Array<i32>(slots)
@@ -320,7 +377,7 @@ export class Http3Wheel {
320
377
  }
321
378
 
322
379
  /** Marks bucket `b` filled or not. */
323
- flag(b: i32, on: boolean): void {
380
+ private flag(b: i32, on: boolean): void {
324
381
  const word: i32 = b >> 6
325
382
  if (word < 0 || word >= toI32(this.bits.length)) {
326
383
  return
@@ -329,7 +386,10 @@ export class Http3Wheel {
329
386
  this.bits[word] = on ? this.bits[word] | bit : this.bits[word] & ~bit
330
387
  }
331
388
 
332
- /** Takes `slot` out of its bucket. */
389
+ /**
390
+ * Takes `slot` out of its bucket.
391
+ * @param slot The slot; one out of range or not filed is ignored.
392
+ */
333
393
  unfile(slot: i32): void {
334
394
  if (slot < 0 || slot >= toI32(this.bucketOf.length)) {
335
395
  return
@@ -354,7 +414,14 @@ export class Http3Wheel {
354
414
  this.bucketOf[slot] = -1
355
415
  }
356
416
 
357
- /** Files `slot` under `due`, kept inside the wheel's horizon from `now`; a negative `due` files nothing. */
417
+ /**
418
+ * Files `slot` under `due`, taking it out of any bucket it was in first. A
419
+ * `due` before the cursor is filed at the cursor, one past the horizon
420
+ * (`H3_SERVER_WHEEL - 1` milliseconds after the cursor) at the horizon.
421
+ * @param slot The slot; one out of range files nothing.
422
+ * @param due The deadline in milliseconds, on `now`'s clock; a negative one leaves the slot unfiled.
423
+ * @param now The current millisecond: it starts the cursor when no file or tick has yet.
424
+ */
358
425
  file(slot: i32, due: i64, now: i64): void {
359
426
  this.unfile(slot)
360
427
  if (due < 0 || slot < 0 || slot >= toI32(this.bucketOf.length)) {
@@ -384,7 +451,12 @@ export class Http3Wheel {
384
451
  this.flag(b, true)
385
452
  }
386
453
 
387
- /** The slot at the head of the bucket for `time`, taken out of it, or -1. */
454
+ /**
455
+ * The slot at the head of the bucket for `time`, taken out of it.
456
+ * @param time A millisecond: its bucket is `time` modulo `H3_SERVER_WHEEL`, which also holds a slot
457
+ * filed at the horizon for a later deadline, so the caller compares `dueAt`.
458
+ * @returns The slot taken, or -1 when the bucket is empty.
459
+ */
388
460
  take(time: i64): i32 {
389
461
  const b: i32 = http3ServerBucket(time)
390
462
  const slot: i32 = b >= 0 && b < H3_SERVER_WHEEL ? this.head[b] - 1 : -1
@@ -394,7 +466,12 @@ export class Http3Wheel {
394
466
  return slot
395
467
  }
396
468
 
397
- /** Milliseconds from `now` to the first filled bucket, at most the horizon, or -1 when none is. */
469
+ /**
470
+ * Milliseconds from `now` to the first filled bucket, at most the horizon.
471
+ * @param now The current millisecond; the search starts at the cursor, or at `now` before it starts.
472
+ * @returns 0 when that bucket's time is at or before `now`, the milliseconds until it otherwise, or -1
473
+ * when every bucket is empty.
474
+ */
398
475
  next(now: i64): i32 {
399
476
  // From the cursor: a bucket before it has run, and one at `now` after a
400
477
  // tick is the horizon's, a full turn of the wheel away.
@@ -426,14 +503,17 @@ export class Http3Wheel {
426
503
  * is the HTTP/3 side of each and `quic(slot)` the QUIC side.
427
504
  */
428
505
  export class Http3Server {
506
+ /** The QUIC configuration of the listener and every slot's connection; `stop` wipes its two keys. */
429
507
  quicConfig: QuicServerConfig
430
508
  /** The HTTP/3 configuration each slot's `Http3Connection` is built with. */
431
- config: Http3Config
509
+ private config: Http3Config
510
+ /** Answers the datagrams no slot owns, and accepts new connections into free slots; `stop` releases it. */
432
511
  listener: QuicListener
433
512
  /** What the listener answers into, made once: its reply and IDs are refilled in their own room. */
434
513
  answer: QuicListenerAnswer
435
514
  /** The slots built so far, 0 up: each built on the first Initial accepted into it, or the first ask for it. */
436
515
  quics: QuicConnection[]
516
+ /** The HTTP/3 side of each slot in `quics`, at the same index: what `connection` answers. */
437
517
  conns: Http3Connection[]
438
518
  /**
439
519
  * The `date` every slot's responses carry, formatted once a second for all
@@ -441,14 +521,16 @@ export class Http3Server {
441
521
  * `connection` and `ready`. `date.pin` fixes its clock.
442
522
  */
443
523
  date: HttpDate
524
+ /** The connection IDs of every slot holding a connection, which route each datagram to its slot. */
444
525
  index: Http3CidIndex
526
+ /** Each slot's next deadline, filed by `flush` and run by `tick`; `timeout` reads it. */
445
527
  wheel: Http3Wheel
446
528
  /**
447
529
  * Where each slot sends now: its client's address as the first datagram
448
530
  * came from, then wherever the connection follows the client to
449
- * (`QuicConnection.sendAddress`, copied here by `flushSlot`). Not
450
- * validated until `quic(slot).addressValidated` is set (RFC 9000 §8.1): a
451
- * Retry token the listener checked, with `retry` on, or a Handshake packet
531
+ * (`QuicConnection.sendAddress`, copied here by `flush` before each slot's flight once
532
+ * `quic(slot).pathKnown`). Not validated until `quic(slot).addressValidated` is set (RFC 9000 §8.1): a
533
+ * Retry token the listener checked, with `quicConfig.retry` on, or a Handshake packet
452
534
  * from the client. Before then it is the source of one UDP datagram, which
453
535
  * anyone can forge, so a cap or an allow-list keyed on it waits for that.
454
536
  * After the handshake it changes when the client's address does (a NAT
@@ -457,46 +539,53 @@ export class Http3Server {
457
539
  */
458
540
  addresses: u8[][]
459
541
  /** What `receive` reads into, and where `flush` writes a flight. */
460
- rx: u8[]
461
- tx: u8[]
462
- from: u8[]
463
- meta: i32[]
542
+ private rx: u8[]
543
+ private tx: u8[]
544
+ private from: u8[]
545
+ private meta: i32[]
546
+ /**
547
+ * `QUIC_CONN_ENTROPY_SIZE` bytes of scratch, refilled from `crypto.getRandomValues` for each slot built
548
+ * and each connection accepted; it holds the last one's until the next, and `stop` zeroes it.
549
+ */
464
550
  entropy: u8[]
465
- flight: QuicFlight
551
+ private flight: QuicFlight
466
552
  /** Signs every slot's CertificateVerify in `serve`, one for the server, keeping nothing per signature. */
467
- signer: TlsP256Signer
553
+ private signer: TlsP256Signer
468
554
  /** Each slot's connection-ID index signature, so it is refiled only when its IDs changed. */
469
- signatures: i64[]
470
- state: i32[]
555
+ private signatures: i64[]
556
+ private state: i32[]
471
557
  /** The slots with datagrams for the program, a ring, and the slots with something to send, a stack. */
472
- readyRing: i32[]
473
- dirtyStack: i32[]
558
+ private readyRing: i32[]
559
+ private dirtyStack: i32[]
474
560
  /** Released slots, a stack: a slot never built is taken from `used` instead. */
475
- free: i32[]
561
+ private free: i32[]
476
562
  /** Per slot: 1 while on the ready ring, 2 while on the dirty stack. */
477
- queued: i32[]
563
+ private queued: i32[]
478
564
  /** When the answer budget was last topped up, -1 before the first answer. */
479
- answerRefilled: i64 = -1
565
+ private answerRefilled: i64 = -1
480
566
  /** The caller's clock in microseconds, from `clock`, or -1: what each connection's pacer counts. */
481
- micros: i64 = -1
567
+ private micros: i64 = -1
568
+ /** The UDP socket every datagram is read from and sent on, as the constructor was given it. */
482
569
  fd: i32 = -1
483
- readyHead: i32 = 0
484
- readyCount: i32 = 0
485
- dirtyCount: i32 = 0
486
- freeCount: i32 = 0
570
+ private readyHead: i32 = 0
571
+ private readyCount: i32 = 0
572
+ private dirtyCount: i32 = 0
573
+ private freeCount: i32 = 0
487
574
  /** How many slots the pool may hold, which `size` answers. */
488
- slots: i32 = 1
575
+ private slots: i32 = 1
489
576
  /** The slots ever accepted into, 0 up: one past it has never held a connection, so is free without being on `free`. */
490
- used: i32 = 0
577
+ private used: i32 = 0
491
578
  /** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
492
- stopped: boolean = false
579
+ private stopped: boolean = false
493
580
  /** What is left of the answer budget (see `answerRefilled`). */
494
- answerBudget: i32 = 0
495
- /** Counters for a log or a test, saturating: connections accepted, Initials dropped for want of a slot, datagrams past the answer budget, sends that failed. */
581
+ private answerBudget: i32 = 0
582
+ /** Connections accepted into a slot, for a log or a test; saturating at 2^31 - 1. */
496
583
  accepted: i32 = 0
584
+ /** Initials dropped because no slot was free, saturating at 2^31 - 1. */
497
585
  refused: i32 = 0
586
+ /** Datagrams no slot owns dropped past the answer budget before the listener read them, saturating. */
498
587
  limited: i32 = 0
499
- sendErrors: i32 = 0
588
+ private sendErrors: i32 = 0
500
589
  /** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
501
590
  ecn: boolean = true
502
591
  /**
@@ -504,8 +593,8 @@ export class Http3Server {
504
593
  * constructor for the stream table `quicConfig` gives every slot: `null`
505
594
  * when its streams have none. Each pool makes a buffer only when a stream
506
595
  * first needs one, so an idle server holds none (QUIC-3). It is made even
507
- * for a configuration `quicConnConfigFits` refuses; `build` shares it only
508
- * with a connection whose stream buffers are its size.
596
+ * for a configuration `quicConnConfigFits` refuses; a slot's connection
597
+ * shares it only when its stream buffers are the pools' size.
509
598
  */
510
599
  streamPools: QuicStreamPools | null = null
511
600
  /** The `bodyChunk`-byte read buffers every slot's `Http3Connection` borrows, one at most each. */
@@ -522,6 +611,12 @@ export class Http3Server {
522
611
  * built on the first Initial accepted into it. From here on
523
612
  * `quicConfig`'s `statelessResetKey` and `retryTokenKey` are the server's:
524
613
  * `stop` wipes them, so the configuration is this server's alone.
614
+ * @param quicConfig The QUIC configuration, kept by reference and shared by the listener and every slot.
615
+ * @param config The HTTP/3 configuration, kept by reference; its `bodyChunk` and `maxFieldSectionSize`
616
+ * size the shared buffer pools.
617
+ * @param fd The bound UDP socket, kept; the server does not close it.
618
+ * @param size How many slots, 1 to 65536; any other panics.
619
+ * @param listenerEntropy `QUIC_LISTENER_ENTROPY_SIZE` random bytes, copied and then zeroed in this array.
525
620
  */
526
621
  constructor(quicConfig: QuicServerConfig, config: Http3Config, fd: i32, size: i32, listenerEntropy: u8[]) {
527
622
  if (size < 1 || size > 65536) {
@@ -590,7 +685,7 @@ export class Http3Server {
590
685
  * were made for, its HTTP/3 connection on the pool's buffers and `date`,
591
686
  * and its address.
592
687
  */
593
- build(): void {
688
+ private build(): void {
594
689
  crypto.getRandomValues(this.entropy)
595
690
  const quic = new QuicConnection(this.quicConfig, this.entropy)
596
691
  quic.ecnMarking = this.ecn
@@ -610,7 +705,7 @@ export class Http3Server {
610
705
  }
611
706
 
612
707
  /** `slot` when it is one of the pool's, built with every slot before it if it was not; 0 when it is out of range. */
613
- slotAt(slot: i32): i32 {
708
+ private slotAt(slot: i32): i32 {
614
709
  const at: i32 = slot >= 0 && slot < this.slots ? slot : H3_SERVER_ZERO
615
710
  while (toI32(this.quics.length) <= at) {
616
711
  this.build()
@@ -619,21 +714,31 @@ export class Http3Server {
619
714
  }
620
715
 
621
716
  /** Whether every slot holds a connection: none released, and none left never used. */
622
- full(): boolean {
717
+ private full(): boolean {
623
718
  return this.freeCount <= 0 && this.used >= this.slots
624
719
  }
625
720
 
626
- /** How many slots the pool has, built or not. */
721
+ /**
722
+ * How many slots the pool has, built or not.
723
+ * @returns The constructor's `size`, 1 to 65536.
724
+ */
627
725
  size(): i32 {
628
726
  return this.slots
629
727
  }
630
728
 
631
- /** How many slots hold a connection. */
729
+ /**
730
+ * How many slots hold a connection.
731
+ * @returns 0 to `size()`.
732
+ */
632
733
  busy(): i32 {
633
734
  return this.used - this.freeCount
634
735
  }
635
736
 
636
- /** Whether `slot` holds a connection. */
737
+ /**
738
+ * Whether `slot` holds a connection.
739
+ * @param slot Any number.
740
+ * @returns True from the accept into it until it is freed; false for a slot out of range.
741
+ */
637
742
  holds(slot: i32): boolean {
638
743
  return slot >= 0 && slot < toI32(this.state.length) && this.state[slot] === H3_SERVER_BUSY
639
744
  }
@@ -643,18 +748,29 @@ export class Http3Server {
643
748
  * of range names the first. A slot not built yet is built here, with every
644
749
  * slot before it, so a program that binds something to each slot's
645
750
  * connection up front gets the one its slot keeps.
751
+ * @param slot The slot, 0 to `size() - 1`.
752
+ * @returns The slot's connection, kept by the pool and reused by every connection the slot holds.
646
753
  */
647
754
  connection(slot: i32): Http3Connection {
648
755
  this.date.current()
649
756
  return this.conns[this.slotAt(slot)]
650
757
  }
651
758
 
652
- /** The QUIC connection of `slot`, built as `connection` builds it; a slot out of range names the first. */
759
+ /**
760
+ * The QUIC connection of `slot`, built as `connection` builds it; a slot out of range names the first.
761
+ * @param slot The slot, 0 to `size() - 1`.
762
+ * @returns The slot's QUIC connection, kept by the pool and reset for each connection the slot holds.
763
+ */
653
764
  quic(slot: i32): QuicConnection {
654
765
  return this.quics[this.slotAt(slot)]
655
766
  }
656
767
 
657
- /** Puts `slot` on the ready ring and the dirty stack, unless it is on them. */
768
+ /**
769
+ * Marks `slot` as having news for the program, so that `ready` answers it,
770
+ * and something to send, so that the next `flush` sends it; a slot already
771
+ * marked is not marked twice.
772
+ * @param slot The slot; one that holds no connection is ignored.
773
+ */
658
774
  touch(slot: i32): void {
659
775
  if (!this.holds(slot)) {
660
776
  return
@@ -669,7 +785,7 @@ export class Http3Server {
669
785
  }
670
786
 
671
787
  /** Puts `slot` on the dirty stack, for `flush`. */
672
- dirty(slot: i32): void {
788
+ private dirty(slot: i32): void {
673
789
  if ((this.queued[slot] & 2) === 0 && this.dirtyCount < toI32(this.dirtyStack.length)) {
674
790
  this.dirtyStack[this.dirtyCount] = slot
675
791
  this.dirtyCount = this.dirtyCount + 1
@@ -677,7 +793,11 @@ export class Http3Server {
677
793
  }
678
794
  }
679
795
 
680
- /** The next slot with news for the program, or -1: call its connection's `next` until it has none. */
796
+ /**
797
+ * The next slot with news for the program: call its connection's `next` until it has none.
798
+ * @returns A slot holding a connection that a datagram or `touch` marked since `ready` last answered it,
799
+ * each such slot once, or -1 when none is left.
800
+ */
681
801
  ready(): i32 {
682
802
  this.date.current()
683
803
  while (this.readyCount > 0) {
@@ -693,11 +813,15 @@ export class Http3Server {
693
813
  }
694
814
 
695
815
  /**
696
- * Reads every datagram waiting on the socket, at most
697
- * `H3_SERVER_RECEIVE_BURST`, and hands each to its slot or the listener,
816
+ * Reads every datagram waiting on the socket, in at most
817
+ * `H3_SERVER_RECEIVE_BURST` receives, and hands each to its slot or the listener,
698
818
  * signing a handshake's CertificateVerify with `key` (ECDSA P-256) when one
699
819
  * asks. A GRO receive is cut into its datagrams in place. Answers how many
700
820
  * datagrams it read.
821
+ * @param now The current millisecond on the caller's monotonic clock.
822
+ * @param key The certificate's private key; read only to sign, and not kept.
823
+ * @returns How many datagrams it handed on, each GRO segment counted, those then dropped included; it
824
+ * stops at the first receive that finds nothing or fails.
701
825
  */
702
826
  receive(now: i64, key: Secret<u8[]>): i32 {
703
827
  let count: i32 = 0
@@ -728,6 +852,14 @@ export class Http3Server {
728
852
  /**
729
853
  * One datagram, `buf[at .. at + len)`, from `address`: to the slot that
730
854
  * owns its first packet's Destination Connection ID, or to the listener.
855
+ * One too short to carry its DCID, or whose DCID is longer than
856
+ * `QUIC_MAX_CID_LENGTH`, or a window outside `buf`, is dropped.
857
+ * @param buf The bytes holding the datagram, read in place.
858
+ * @param at Where the datagram starts in `buf`.
859
+ * @param len Its length in bytes.
860
+ * @param address Where it came from, in `nish:net`'s 18-byte form; copied when it starts a connection.
861
+ * @param now The current millisecond on the caller's monotonic clock.
862
+ * @param key The certificate's private key, as `receive` takes it.
731
863
  */
732
864
  datagram(buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
733
865
  if (len < 1 || at < 0 || at + len > toI32(buf.length)) {
@@ -759,7 +891,7 @@ export class Http3Server {
759
891
  * in `buf` and answering into `answer`, and may make a connection in a
760
892
  * free slot, which reads the same window. Nothing is copied (H3-3).
761
893
  */
762
- unowned(buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
894
+ private unowned(buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
763
895
  if (!this.worthListening(buf, at, len, now)) {
764
896
  return
765
897
  }
@@ -812,7 +944,7 @@ export class Http3Server {
812
944
  * A stateless reset and a Version Negotiation packet, the answers that do
813
945
  * not start a connection, are also held to `takeAnswer`'s budget.
814
946
  */
815
- worthListening(buf: u8[], at: i32, len: i32, now: i64): boolean {
947
+ private worthListening(buf: u8[], at: i32, len: i32, now: i64): boolean {
816
948
  const first: i32 = toI32(buf[at])
817
949
  if ((first & 0x80) === 0) {
818
950
  return len > QUIC_LISTENER_MIN_RESET && this.takeAnswer(now)
@@ -844,7 +976,7 @@ export class Http3Server {
844
976
  * the datagram is dropped before the listener sees it, so a flood of
845
977
  * garbage costs no answer and no MAC past the budget (H3-3); counted in `limited`.
846
978
  */
847
- takeAnswer(now: i64): boolean {
979
+ private takeAnswer(now: i64): boolean {
848
980
  if (this.answerRefilled < 0 || now < this.answerRefilled) {
849
981
  this.answerRefilled = now
850
982
  this.answerBudget = H3_SERVER_ANSWER_BURST
@@ -869,6 +1001,8 @@ export class Http3Server {
869
1001
  * 1000` where `now` is `monotonicNanos() / 1000000`. The pacer counts it;
870
1002
  * every timer still counts `now`. One that is not inside `now`'s
871
1003
  * millisecond is not read.
1004
+ * @param micros The instant in microseconds; one outside the millisecond `now` names, such as -1, makes
1005
+ * the pacer count whole milliseconds.
872
1006
  */
873
1007
  clock(micros: i64): void {
874
1008
  this.micros = micros
@@ -879,6 +1013,13 @@ export class Http3Server {
879
1013
  * handshake asks, and refiles its IDs and timer. The connection decides
880
1014
  * from the address whether the client moved (RFC 9000 §9.3); an address
881
1015
  * that is not 18 bytes long leaves it where it is.
1016
+ * @param slot A slot that holds a connection, as `datagram` finds it; the slot is not checked.
1017
+ * @param buf The bytes holding the datagram, read in place.
1018
+ * @param at Where the datagram starts in `buf`.
1019
+ * @param len Its length in bytes.
1020
+ * @param address Where it came from, in `nish:net`'s 18-byte form.
1021
+ * @param now The current millisecond on the caller's monotonic clock.
1022
+ * @param key The certificate's private key, as `receive` takes it.
882
1023
  */
883
1024
  serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
884
1025
  const quic: QuicConnection = this.quics[slot]
@@ -896,7 +1037,7 @@ export class Http3Server {
896
1037
  }
897
1038
 
898
1039
  /** Files `slot`'s connection IDs in the index again when they changed. */
899
- refile(slot: i32): void {
1040
+ private refile(slot: i32): void {
900
1041
  const quic: QuicConnection = this.quics[slot]
901
1042
  const signature: i64 =
902
1043
  (quic.cids.nextLocal << 16) |
@@ -929,7 +1070,7 @@ export class Http3Server {
929
1070
  * Sends `buf[at .. at + len)` to `to` with the ECN codepoint `ecn`, cut
930
1071
  * into `segment`-byte datagrams when it is above 0; answers whether it went.
931
1072
  */
932
- send(buf: u8[], at: i32, len: i32, to: u8[], segment: i32, ecn: i32): boolean {
1073
+ private send(buf: u8[], at: i32, len: i32, to: u8[], segment: i32, ecn: i32): boolean {
933
1074
  const sent: i32 = udpSendTo(this.fd, buf, at, len, to, segment, ecn)
934
1075
  if (sent >= 0) {
935
1076
  return true
@@ -980,6 +1121,7 @@ export class Http3Server {
980
1121
  * slot whose HTTP/3 side is done after GOAWAY, frees a slot whose
981
1122
  * connection has closed, and files each in the wheel under its next
982
1123
  * deadline.
1124
+ * @param now The current millisecond on the caller's monotonic clock.
983
1125
  */
984
1126
  flush(now: i64): void {
985
1127
  while (this.dirtyCount > 0) {
@@ -993,7 +1135,7 @@ export class Http3Server {
993
1135
  }
994
1136
 
995
1137
  /** `flush` of one slot. */
996
- flushSlot(slot: i32, now: i64): void {
1138
+ private flushSlot(slot: i32, now: i64): void {
997
1139
  const quic: QuicConnection = this.quics[slot]
998
1140
  quic.recovery.micros = this.micros
999
1141
  if (this.conns[slot].isDone() && !quic.closed()) {
@@ -1060,7 +1202,7 @@ export class Http3Server {
1060
1202
  }
1061
1203
 
1062
1204
  /** Takes every one of `slot`'s connection IDs out of the index. */
1063
- unindex(slot: i32): void {
1205
+ private unindex(slot: i32): void {
1064
1206
  const first: i32 = slot * H3_SERVER_KEYS
1065
1207
  for (let j: i32 = 0; j < H3_SERVER_KEYS; j++) {
1066
1208
  this.index.remove(first + j)
@@ -1068,7 +1210,7 @@ export class Http3Server {
1068
1210
  }
1069
1211
 
1070
1212
  /** Frees `slot`: its IDs leave the index and its timer the wheel. */
1071
- release(slot: i32): void {
1213
+ private release(slot: i32): void {
1072
1214
  this.unindex(slot)
1073
1215
  this.wheel.unfile(slot)
1074
1216
  this.conns[slot].release()
@@ -1112,7 +1254,10 @@ export class Http3Server {
1112
1254
  this.answer.wipe()
1113
1255
  }
1114
1256
 
1115
- /** Runs the timers due by `now`, and marks each slot it ran for `flush`. */
1257
+ /**
1258
+ * Runs the timers due by `now`, and marks each slot it ran for `flush`.
1259
+ * @param now The current millisecond on the caller's monotonic clock.
1260
+ */
1116
1261
  tick(now: i64): void {
1117
1262
  const wheel: Http3Wheel = this.wheel
1118
1263
  if (wheel.cursor < 0) {
@@ -1146,7 +1291,12 @@ export class Http3Server {
1146
1291
  }
1147
1292
  }
1148
1293
 
1149
- /** Milliseconds until `tick` has a timer to run, for `pollWait`; -1 when no slot has one. */
1294
+ /**
1295
+ * Milliseconds until `tick` has a timer to run, for `pollWait`.
1296
+ * @param now The current millisecond on the caller's monotonic clock.
1297
+ * @returns 0 when a timer is due now, the milliseconds until the first otherwise (at most about
1298
+ * `H3_SERVER_WHEEL`), or -1 when no slot is filed.
1299
+ */
1150
1300
  timeout(now: i64): i32 {
1151
1301
  return this.wheel.next(now)
1152
1302
  }