nish 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 (127) hide show
  1. package/README.md +7 -6
  2. package/docs/AI.md +86 -16
  3. package/docs/INSTALL.md +22 -13
  4. package/llms.txt +5 -2
  5. package/package.json +15 -5
  6. package/runtime/async.d.mts +49 -0
  7. package/runtime/async.mjs +27 -0
  8. package/runtime/nish.d.ts +4681 -427
  9. package/runtime/nish.h +6 -2
  10. package/runtime/nish.mjs +4 -0
  11. package/runtime/runtime-net.c +22 -0
  12. package/runtime/runtime-shm.c +93 -2
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +108 -13
  16. package/std/README.md +131 -131
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +258 -42
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +80 -9
  28. package/std/crypto/base64url.ts +22 -0
  29. package/std/crypto/bcrypt-batch.ts +88 -21
  30. package/std/crypto/bcrypt.ts +60 -0
  31. package/std/crypto/chacha20poly1305.ts +53 -0
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +47 -0
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +235 -31
  36. package/std/crypto/jwt.ts +192 -63
  37. package/std/crypto/p256.ts +159 -16
  38. package/std/crypto/p384.ts +28 -3
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +81 -15
  41. package/std/crypto/rsa.ts +33 -3
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +152 -14
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +67 -7
  48. package/std/json-equal.ts +19 -0
  49. package/std/json-format.ts +112 -13
  50. package/std/json-number.ts +64 -14
  51. package/std/json-pointer.ts +23 -1
  52. package/std/json-tokenizer.ts +100 -29
  53. package/std/json-typed.ts +713 -66
  54. package/std/json-value.ts +400 -124
  55. package/std/json.ts +138 -10
  56. package/std/map.ts +25 -7
  57. package/std/math.ts +36 -4
  58. package/std/net/buffer-pool.ts +42 -11
  59. package/std/net/dns-config.ts +109 -23
  60. package/std/net/dns-message.ts +62 -7
  61. package/std/net/dns-resolver.ts +247 -53
  62. package/std/net/dns.ts +203 -35
  63. package/std/net/fetch.ts +151 -61
  64. package/std/net/handler.ts +278 -80
  65. package/std/net/hpack.ts +262 -52
  66. package/std/net/http-compress.ts +125 -15
  67. package/std/net/http-fields.ts +85 -9
  68. package/std/net/http1-client-codec.ts +100 -19
  69. package/std/net/http1-codec.ts +297 -77
  70. package/std/net/http1-server.ts +430 -94
  71. package/std/net/http2-frame.ts +272 -35
  72. package/std/net/http2-tls.ts +135 -34
  73. package/std/net/http2.ts +247 -106
  74. package/std/net/http3-frame.ts +154 -17
  75. package/std/net/http3-server.ts +275 -101
  76. package/std/net/http3.ts +307 -115
  77. package/std/net/outbound.ts +37 -3
  78. package/std/net/postgres-carrier.ts +144 -8
  79. package/std/net/postgres.ts +801 -112
  80. package/std/net/prefork.ts +164 -31
  81. package/std/net/qpack.ts +105 -18
  82. package/std/net/quic-conn-ack.ts +59 -12
  83. package/std/net/quic-conn-cid.ts +124 -27
  84. package/std/net/quic-conn-params.ts +70 -10
  85. package/std/net/quic-datagram.ts +43 -3
  86. package/std/net/quic-frame.ts +286 -12
  87. package/std/net/quic-listener.ts +101 -32
  88. package/std/net/quic-packet.ts +302 -21
  89. package/std/net/quic-recovery.ts +264 -52
  90. package/std/net/quic-stream.ts +353 -91
  91. package/std/net/quic.ts +1124 -201
  92. package/std/net/router.ts +87 -40
  93. package/std/net/tls/client.ts +166 -51
  94. package/std/net/tls/codec.ts +413 -56
  95. package/std/net/tls/record-client.ts +110 -55
  96. package/std/net/tls/record-server.ts +159 -59
  97. package/std/net/tls/record.ts +109 -12
  98. package/std/net/tls/schedule.ts +235 -26
  99. package/std/net/tls/ticket.ts +159 -40
  100. package/std/net/tls/verify.ts +49 -3
  101. package/std/net/tls-tcp.ts +250 -45
  102. package/std/net/tls.ts +221 -59
  103. package/std/net/upstream.ts +81 -45
  104. package/std/net/url.ts +97 -13
  105. package/std/net/websocket.ts +122 -12
  106. package/std/net/webtransport.ts +220 -89
  107. package/std/pair.ts +10 -2
  108. package/std/random.ts +34 -9
  109. package/std/regex.ts +352 -198
  110. package/std/region.ts +70 -15
  111. package/std/secret.ts +52 -11
  112. package/std/sort.ts +17 -2
  113. package/std/sqlite.ts +256 -35
  114. package/std/sync/ring.ts +430 -40
  115. package/std/sync/shm.ts +222 -15
  116. package/std/term-io.ts +137 -64
  117. package/std/term-pixels.ts +144 -19
  118. package/std/term-probe.ts +66 -25
  119. package/std/term-rate.ts +128 -57
  120. package/std/term.ts +332 -121
  121. package/std/testing.ts +130 -32
  122. package/std/text.ts +73 -7
  123. package/std/threads.ts +229 -41
  124. package/std/yaml-json.ts +94 -82
  125. package/std/yaml-lines.ts +47 -15
  126. package/std/yaml-write.ts +24 -16
  127. 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,57 +521,71 @@ 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
- * Each slot's client address, as its first datagram came from. Not
448
- * validated until `quic(slot).addressValidated` is set (RFC 9000 §8.1): a
449
- * Retry token the listener checked, with `retry` on, or a Handshake packet
529
+ * Where each slot sends now: its client's address as the first datagram
530
+ * came from, then wherever the connection follows the client to
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
450
534
  * from the client. Before then it is the source of one UDP datagram, which
451
535
  * anyone can forge, so a cap or an allow-list keyed on it waits for that.
536
+ * After the handshake it changes when the client's address does (a NAT
537
+ * rebinding, §9.3), first to an address not yet validated, so a cap keyed
538
+ * on the address a session started from keeps its own copy.
452
539
  */
453
540
  addresses: u8[][]
454
541
  /** What `receive` reads into, and where `flush` writes a flight. */
455
- rx: u8[]
456
- tx: u8[]
457
- from: u8[]
458
- 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
+ */
459
550
  entropy: u8[]
460
- flight: QuicFlight
551
+ private flight: QuicFlight
461
552
  /** Signs every slot's CertificateVerify in `serve`, one for the server, keeping nothing per signature. */
462
- signer: TlsP256Signer
553
+ private signer: TlsP256Signer
463
554
  /** Each slot's connection-ID index signature, so it is refiled only when its IDs changed. */
464
- signatures: i64[]
465
- state: i32[]
555
+ private signatures: i64[]
556
+ private state: i32[]
466
557
  /** The slots with datagrams for the program, a ring, and the slots with something to send, a stack. */
467
- readyRing: i32[]
468
- dirtyStack: i32[]
558
+ private readyRing: i32[]
559
+ private dirtyStack: i32[]
469
560
  /** Released slots, a stack: a slot never built is taken from `used` instead. */
470
- free: i32[]
561
+ private free: i32[]
471
562
  /** Per slot: 1 while on the ready ring, 2 while on the dirty stack. */
472
- queued: i32[]
563
+ private queued: i32[]
473
564
  /** When the answer budget was last topped up, -1 before the first answer. */
474
- answerRefilled: i64 = -1
565
+ private answerRefilled: i64 = -1
475
566
  /** The caller's clock in microseconds, from `clock`, or -1: what each connection's pacer counts. */
476
- 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. */
477
569
  fd: i32 = -1
478
- readyHead: i32 = 0
479
- readyCount: i32 = 0
480
- dirtyCount: i32 = 0
481
- freeCount: i32 = 0
570
+ private readyHead: i32 = 0
571
+ private readyCount: i32 = 0
572
+ private dirtyCount: i32 = 0
573
+ private freeCount: i32 = 0
482
574
  /** How many slots the pool may hold, which `size` answers. */
483
- slots: i32 = 1
575
+ private slots: i32 = 1
484
576
  /** The slots ever accepted into, 0 up: one past it has never held a connection, so is free without being on `free`. */
485
- used: i32 = 0
577
+ private used: i32 = 0
486
578
  /** Set by `stop`: a slot built after it is released as it is made, so it holds no keys. */
487
- stopped: boolean = false
579
+ private stopped: boolean = false
488
580
  /** What is left of the answer budget (see `answerRefilled`). */
489
- answerBudget: i32 = 0
490
- /** 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. */
491
583
  accepted: i32 = 0
584
+ /** Initials dropped because no slot was free, saturating at 2^31 - 1. */
492
585
  refused: i32 = 0
586
+ /** Datagrams no slot owns dropped past the answer budget before the listener read them, saturating. */
493
587
  limited: i32 = 0
494
- sendErrors: i32 = 0
588
+ private sendErrors: i32 = 0
495
589
  /** Whether the platform sends ECN marks: true until a marked send answers -95, and every slot's `ecnMarking` with it. */
496
590
  ecn: boolean = true
497
591
  /**
@@ -499,8 +593,8 @@ export class Http3Server {
499
593
  * constructor for the stream table `quicConfig` gives every slot: `null`
500
594
  * when its streams have none. Each pool makes a buffer only when a stream
501
595
  * first needs one, so an idle server holds none (QUIC-3). It is made even
502
- * for a configuration `quicConnConfigFits` refuses; `build` shares it only
503
- * 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.
504
598
  */
505
599
  streamPools: QuicStreamPools | null = null
506
600
  /** The `bodyChunk`-byte read buffers every slot's `Http3Connection` borrows, one at most each. */
@@ -517,6 +611,12 @@ export class Http3Server {
517
611
  * built on the first Initial accepted into it. From here on
518
612
  * `quicConfig`'s `statelessResetKey` and `retryTokenKey` are the server's:
519
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.
520
620
  */
521
621
  constructor(quicConfig: QuicServerConfig, config: Http3Config, fd: i32, size: i32, listenerEntropy: u8[]) {
522
622
  if (size < 1 || size > 65536) {
@@ -585,7 +685,7 @@ export class Http3Server {
585
685
  * were made for, its HTTP/3 connection on the pool's buffers and `date`,
586
686
  * and its address.
587
687
  */
588
- build(): void {
688
+ private build(): void {
589
689
  crypto.getRandomValues(this.entropy)
590
690
  const quic = new QuicConnection(this.quicConfig, this.entropy)
591
691
  quic.ecnMarking = this.ecn
@@ -605,7 +705,7 @@ export class Http3Server {
605
705
  }
606
706
 
607
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. */
608
- slotAt(slot: i32): i32 {
708
+ private slotAt(slot: i32): i32 {
609
709
  const at: i32 = slot >= 0 && slot < this.slots ? slot : H3_SERVER_ZERO
610
710
  while (toI32(this.quics.length) <= at) {
611
711
  this.build()
@@ -614,21 +714,31 @@ export class Http3Server {
614
714
  }
615
715
 
616
716
  /** Whether every slot holds a connection: none released, and none left never used. */
617
- full(): boolean {
717
+ private full(): boolean {
618
718
  return this.freeCount <= 0 && this.used >= this.slots
619
719
  }
620
720
 
621
- /** 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
+ */
622
725
  size(): i32 {
623
726
  return this.slots
624
727
  }
625
728
 
626
- /** How many slots hold a connection. */
729
+ /**
730
+ * How many slots hold a connection.
731
+ * @returns 0 to `size()`.
732
+ */
627
733
  busy(): i32 {
628
734
  return this.used - this.freeCount
629
735
  }
630
736
 
631
- /** 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
+ */
632
742
  holds(slot: i32): boolean {
633
743
  return slot >= 0 && slot < toI32(this.state.length) && this.state[slot] === H3_SERVER_BUSY
634
744
  }
@@ -638,18 +748,29 @@ export class Http3Server {
638
748
  * of range names the first. A slot not built yet is built here, with every
639
749
  * slot before it, so a program that binds something to each slot's
640
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.
641
753
  */
642
754
  connection(slot: i32): Http3Connection {
643
755
  this.date.current()
644
756
  return this.conns[this.slotAt(slot)]
645
757
  }
646
758
 
647
- /** 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
+ */
648
764
  quic(slot: i32): QuicConnection {
649
765
  return this.quics[this.slotAt(slot)]
650
766
  }
651
767
 
652
- /** 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
+ */
653
774
  touch(slot: i32): void {
654
775
  if (!this.holds(slot)) {
655
776
  return
@@ -664,7 +785,7 @@ export class Http3Server {
664
785
  }
665
786
 
666
787
  /** Puts `slot` on the dirty stack, for `flush`. */
667
- dirty(slot: i32): void {
788
+ private dirty(slot: i32): void {
668
789
  if ((this.queued[slot] & 2) === 0 && this.dirtyCount < toI32(this.dirtyStack.length)) {
669
790
  this.dirtyStack[this.dirtyCount] = slot
670
791
  this.dirtyCount = this.dirtyCount + 1
@@ -672,7 +793,11 @@ export class Http3Server {
672
793
  }
673
794
  }
674
795
 
675
- /** 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
+ */
676
801
  ready(): i32 {
677
802
  this.date.current()
678
803
  while (this.readyCount > 0) {
@@ -688,11 +813,15 @@ export class Http3Server {
688
813
  }
689
814
 
690
815
  /**
691
- * Reads every datagram waiting on the socket, at most
692
- * `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,
693
818
  * signing a handshake's CertificateVerify with `key` (ECDSA P-256) when one
694
819
  * asks. A GRO receive is cut into its datagrams in place. Answers how many
695
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.
696
825
  */
697
826
  receive(now: i64, key: Secret<u8[]>): i32 {
698
827
  let count: i32 = 0
@@ -723,6 +852,14 @@ export class Http3Server {
723
852
  /**
724
853
  * One datagram, `buf[at .. at + len)`, from `address`: to the slot that
725
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.
726
863
  */
727
864
  datagram(buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
728
865
  if (len < 1 || at < 0 || at + len > toI32(buf.length)) {
@@ -743,7 +880,7 @@ export class Http3Server {
743
880
  }
744
881
  const slot: i32 = this.index.find(buf, dcidAt, dcidLength)
745
882
  if (slot >= 0) {
746
- this.serve(slot, buf, at, len, now, key)
883
+ this.serve(slot, buf, at, len, address, now, key)
747
884
  return
748
885
  }
749
886
  this.unowned(buf, at, len, address, now, key)
@@ -754,7 +891,7 @@ export class Http3Server {
754
891
  * in `buf` and answering into `answer`, and may make a connection in a
755
892
  * free slot, which reads the same window. Nothing is copied (H3-3).
756
893
  */
757
- 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 {
758
895
  if (!this.worthListening(buf, at, len, now)) {
759
896
  return
760
897
  }
@@ -793,7 +930,7 @@ export class Http3Server {
793
930
  this.state[slot] = H3_SERVER_BUSY
794
931
  this.signatures[slot] = H3_SERVER_NONE
795
932
  this.accepted = h3Count(this.accepted)
796
- this.serve(slot, buf, at, len, now, key)
933
+ this.serve(slot, buf, at, len, address, now, key)
797
934
  }
798
935
 
799
936
  /**
@@ -807,7 +944,7 @@ export class Http3Server {
807
944
  * A stateless reset and a Version Negotiation packet, the answers that do
808
945
  * not start a connection, are also held to `takeAnswer`'s budget.
809
946
  */
810
- worthListening(buf: u8[], at: i32, len: i32, now: i64): boolean {
947
+ private worthListening(buf: u8[], at: i32, len: i32, now: i64): boolean {
811
948
  const first: i32 = toI32(buf[at])
812
949
  if ((first & 0x80) === 0) {
813
950
  return len > QUIC_LISTENER_MIN_RESET && this.takeAnswer(now)
@@ -839,7 +976,7 @@ export class Http3Server {
839
976
  * the datagram is dropped before the listener sees it, so a flood of
840
977
  * garbage costs no answer and no MAC past the budget (H3-3); counted in `limited`.
841
978
  */
842
- takeAnswer(now: i64): boolean {
979
+ private takeAnswer(now: i64): boolean {
843
980
  if (this.answerRefilled < 0 || now < this.answerRefilled) {
844
981
  this.answerRefilled = now
845
982
  this.answerBudget = H3_SERVER_ANSWER_BURST
@@ -864,16 +1001,30 @@ export class Http3Server {
864
1001
  * 1000` where `now` is `monotonicNanos() / 1000000`. The pacer counts it;
865
1002
  * every timer still counts `now`. One that is not inside `now`'s
866
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.
867
1006
  */
868
1007
  clock(micros: i64): void {
869
1008
  this.micros = micros
870
1009
  }
871
1010
 
872
- /** Hands `slot`'s connection a datagram, signs when the handshake asks, and refiles its IDs and timer. */
873
- serve(slot: i32, buf: u8[], at: i32, len: i32, now: i64, key: Secret<u8[]>): void {
1011
+ /**
1012
+ * Hands `slot`'s connection a datagram from `address`, signs when the
1013
+ * handshake asks, and refiles its IDs and timer. The connection decides
1014
+ * from the address whether the client moved (RFC 9000 §9.3); an address
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.
1023
+ */
1024
+ serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
874
1025
  const quic: QuicConnection = this.quics[slot]
875
1026
  quic.recovery.micros = this.micros
876
- quic.receiveWindow(buf, at, len, now)
1027
+ quic.receiveWindowFrom(buf, at, len, address, now)
877
1028
  const input: u8[] | null = quic.signatureInput()
878
1029
  if (input !== null) {
879
1030
  const signature: u8[] | null = this.signer.sign(key, input)
@@ -886,7 +1037,7 @@ export class Http3Server {
886
1037
  }
887
1038
 
888
1039
  /** Files `slot`'s connection IDs in the index again when they changed. */
889
- refile(slot: i32): void {
1040
+ private refile(slot: i32): void {
890
1041
  const quic: QuicConnection = this.quics[slot]
891
1042
  const signature: i64 =
892
1043
  (quic.cids.nextLocal << 16) |
@@ -919,7 +1070,7 @@ export class Http3Server {
919
1070
  * Sends `buf[at .. at + len)` to `to` with the ECN codepoint `ecn`, cut
920
1071
  * into `segment`-byte datagrams when it is above 0; answers whether it went.
921
1072
  */
922
- 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 {
923
1074
  const sent: i32 = udpSendTo(this.fd, buf, at, len, to, segment, ecn)
924
1075
  if (sent >= 0) {
925
1076
  return true
@@ -970,6 +1121,7 @@ export class Http3Server {
970
1121
  * slot whose HTTP/3 side is done after GOAWAY, frees a slot whose
971
1122
  * connection has closed, and files each in the wheel under its next
972
1123
  * deadline.
1124
+ * @param now The current millisecond on the caller's monotonic clock.
973
1125
  */
974
1126
  flush(now: i64): void {
975
1127
  while (this.dirtyCount > 0) {
@@ -983,13 +1135,27 @@ export class Http3Server {
983
1135
  }
984
1136
 
985
1137
  /** `flush` of one slot. */
986
- flushSlot(slot: i32, now: i64): void {
1138
+ private flushSlot(slot: i32, now: i64): void {
987
1139
  const quic: QuicConnection = this.quics[slot]
988
1140
  quic.recovery.micros = this.micros
989
1141
  if (this.conns[slot].isDone() && !quic.closed()) {
990
1142
  quic.close(H3_NO_ERROR)
991
1143
  }
1144
+ // A PATH_RESPONSE owed on a path the connection did not move to goes
1145
+ // there, alone (RFC 9000 §9.1); asking for it runs the connection's
1146
+ // timers first, so a validation that ran out has moved it back before
1147
+ // the flight below picks where it goes.
1148
+ const probe: i32 = quic.takeProbeInto(this.tx, H3_SERVER_ZERO, now)
1149
+ if (probe > 0) {
1150
+ this.send(this.tx, H3_SERVER_ZERO, probe, quic.probeAddress, H3_SERVER_ZERO, H3_SERVER_ZERO)
1151
+ }
992
1152
  const to: u8[] = this.addresses[slot]
1153
+ if (quic.pathKnown) {
1154
+ const current: u8[] = quic.sendAddress()
1155
+ for (let k: i32 = 0; k < toI32(to.length) && k < toI32(current.length); k++) {
1156
+ to[k] = current[k]
1157
+ }
1158
+ }
993
1159
  let retried: boolean = false
994
1160
  for (let guard: i32 = 0; guard < 64; guard++) {
995
1161
  const n: i32 = quicListenerTakeFlight(
@@ -1036,7 +1202,7 @@ export class Http3Server {
1036
1202
  }
1037
1203
 
1038
1204
  /** Takes every one of `slot`'s connection IDs out of the index. */
1039
- unindex(slot: i32): void {
1205
+ private unindex(slot: i32): void {
1040
1206
  const first: i32 = slot * H3_SERVER_KEYS
1041
1207
  for (let j: i32 = 0; j < H3_SERVER_KEYS; j++) {
1042
1208
  this.index.remove(first + j)
@@ -1044,7 +1210,7 @@ export class Http3Server {
1044
1210
  }
1045
1211
 
1046
1212
  /** Frees `slot`: its IDs leave the index and its timer the wheel. */
1047
- release(slot: i32): void {
1213
+ private release(slot: i32): void {
1048
1214
  this.unindex(slot)
1049
1215
  this.wheel.unfile(slot)
1050
1216
  this.conns[slot].release()
@@ -1088,7 +1254,10 @@ export class Http3Server {
1088
1254
  this.answer.wipe()
1089
1255
  }
1090
1256
 
1091
- /** 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
+ */
1092
1261
  tick(now: i64): void {
1093
1262
  const wheel: Http3Wheel = this.wheel
1094
1263
  if (wheel.cursor < 0) {
@@ -1122,7 +1291,12 @@ export class Http3Server {
1122
1291
  }
1123
1292
  }
1124
1293
 
1125
- /** 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
+ */
1126
1300
  timeout(now: i64): i32 {
1127
1301
  return this.wheel.next(now)
1128
1302
  }