@amritk/nish-aarch64-linux 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/INSTALL.md +73 -59
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +50 -4
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/nish.d.ts +4849 -430
  8. package/runtime/nish.h +25 -0
  9. package/runtime/page.d.ts +53 -0
  10. package/runtime/page.mjs +146 -0
  11. package/runtime/runtime-async.c +557 -0
  12. package/runtime/runtime-wasm.c +63 -7
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +252 -15
  16. package/std/README.md +107 -82
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +279 -59
  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 +128 -52
  28. package/std/crypto/base64url.ts +29 -5
  29. package/std/crypto/bcrypt-batch.ts +101 -34
  30. package/std/crypto/bcrypt.ts +93 -27
  31. package/std/crypto/chacha20poly1305.ts +69 -14
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +66 -16
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +242 -37
  36. package/std/crypto/jwt.ts +204 -72
  37. package/std/crypto/p256.ts +222 -72
  38. package/std/crypto/p384.ts +51 -23
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +99 -30
  41. package/std/crypto/rsa.ts +56 -22
  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 +163 -24
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +133 -66
  48. package/std/host/ring.ts +124 -0
  49. package/std/json-equal.ts +31 -11
  50. package/std/json-format.ts +112 -13
  51. package/std/json-number.ts +76 -26
  52. package/std/json-pointer.ts +23 -1
  53. package/std/json-tokenizer.ts +126 -51
  54. package/std/json-typed.ts +757 -96
  55. package/std/json-value.ts +438 -157
  56. package/std/json.ts +170 -38
  57. package/std/map.ts +25 -7
  58. package/std/math.ts +36 -4
  59. package/std/net/buffer-pool.ts +42 -11
  60. package/std/net/dns-config.ts +109 -23
  61. package/std/net/dns-message.ts +62 -7
  62. package/std/net/dns-resolver.ts +247 -53
  63. package/std/net/dns.ts +203 -35
  64. package/std/net/fetch.ts +1267 -290
  65. package/std/net/handler.ts +296 -80
  66. package/std/net/hpack.ts +290 -77
  67. package/std/net/http-compress.ts +152 -36
  68. package/std/net/http-fields.ts +85 -9
  69. package/std/net/http1-client-codec.ts +129 -44
  70. package/std/net/http1-codec.ts +279 -114
  71. package/std/net/http1-server.ts +409 -139
  72. package/std/net/http2-frame.ts +289 -51
  73. package/std/net/http2-tls.ts +152 -49
  74. package/std/net/http2.ts +314 -160
  75. package/std/net/http3-frame.ts +165 -27
  76. package/std/net/http3-server.ts +282 -131
  77. package/std/net/http3.ts +377 -173
  78. package/std/net/outbound.ts +37 -3
  79. package/std/net/postgres-carrier.ts +144 -8
  80. package/std/net/postgres.ts +898 -260
  81. package/std/net/prefork.ts +202 -65
  82. package/std/net/qpack.ts +120 -32
  83. package/std/net/quic-conn-ack.ts +60 -12
  84. package/std/net/quic-conn-cid.ts +135 -27
  85. package/std/net/quic-conn-params.ts +109 -22
  86. package/std/net/quic-datagram.ts +50 -3
  87. package/std/net/quic-frame.ts +333 -23
  88. package/std/net/quic-listener.ts +161 -75
  89. package/std/net/quic-packet.ts +373 -73
  90. package/std/net/quic-recovery.ts +287 -65
  91. package/std/net/quic-stream.ts +436 -146
  92. package/std/net/quic.ts +693 -323
  93. package/std/net/router.ts +87 -40
  94. package/std/net/tls/client.ts +167 -51
  95. package/std/net/tls/codec.ts +494 -68
  96. package/std/net/tls/record-client.ts +140 -73
  97. package/std/net/tls/record-server.ts +233 -105
  98. package/std/net/tls/record.ts +142 -36
  99. package/std/net/tls/schedule.ts +235 -26
  100. package/std/net/tls/ticket.ts +161 -40
  101. package/std/net/tls/verify.ts +57 -10
  102. package/std/net/tls-tcp.ts +308 -95
  103. package/std/net/tls.ts +276 -102
  104. package/std/net/upstream.ts +81 -45
  105. package/std/net/url.ts +109 -22
  106. package/std/net/websocket.ts +140 -26
  107. package/std/net/webtransport.ts +243 -106
  108. package/std/pair.ts +10 -2
  109. package/std/random.ts +42 -15
  110. package/std/regex.ts +422 -257
  111. package/std/region.ts +70 -15
  112. package/std/secret.ts +52 -11
  113. package/std/sort.ts +17 -2
  114. package/std/sqlite.ts +256 -35
  115. package/std/sync/ring.ts +229 -56
  116. package/std/sync/shm.ts +203 -23
  117. package/std/term-io.ts +154 -78
  118. package/std/term-pixels.ts +144 -19
  119. package/std/term-probe.ts +77 -35
  120. package/std/term-rate.ts +199 -118
  121. package/std/term.ts +400 -175
  122. package/std/testing.ts +130 -32
  123. package/std/text.ts +84 -17
  124. package/std/threads.ts +248 -56
  125. package/std/yaml-json.ts +111 -97
  126. package/std/yaml-lines.ts +47 -15
  127. package/std/yaml-write.ts +45 -30
  128. package/std/yaml.ts +764 -384
@@ -10,36 +10,47 @@
10
10
  * WebTransport on with `webtransportSessions`, and is sans-IO like it.
11
11
  *
12
12
  * import { WebTransport, WebTransportConfig, WT_SESSION, WT_DATAGRAM } from "nish/net/webtransport";
13
+ * import { Http3Connection, H3_NEED_MORE, H3_ERROR } from "nish/net/http3";
13
14
  *
14
- * const wt = new WebTransport(new WebTransportConfig(), h3); // once per slot, beside its Http3Connection
15
- * // after quic.receive(...):
16
- * let event: i32 = wt.next();
17
- * while (event !== H3_NEED_MORE && event !== H3_ERROR) {
18
- * // WT_SESSION: wt.sessionId, wt.fields (path, get("origin")) — accept or refuse it
19
- * // WT_DATAGRAM: wt.sessionId, wt.data[wt.dataStart .. wt.dataStart + wt.dataLength)
20
- * if (event === WT_SESSION) { wt.accept(wt.sessionId); }
21
- * if (event === WT_DATAGRAM) { wt.sendDatagram(wt.sessionId, wt.data, wt.dataStart, wt.dataLength); }
22
- * event = wt.next();
23
- * }
15
+ * // h3's Http3Config has webtransportSessions above 0.
16
+ * const serve = (h3: Http3Connection): void => {
17
+ * // once per slot, beside its Http3Connection:
18
+ * const wt = new WebTransport(new WebTransportConfig(), h3);
19
+ * // after quic.receive(...):
20
+ * let event: i32 = wt.next();
21
+ * while (event !== H3_NEED_MORE && event !== H3_ERROR) {
22
+ * // WT_SESSION: wt.sessionId, wt.fields (path, get("origin")) — accept or refuse it
23
+ * // WT_DATAGRAM: wt.sessionId, wt.data[wt.dataStart .. wt.dataStart + wt.dataLength)
24
+ * if (event === WT_SESSION) { wt.accept(wt.sessionId); }
25
+ * if (event === WT_DATAGRAM) { wt.sendDatagram(wt.sessionId, wt.data, wt.dataStart, wt.dataLength); }
26
+ * event = wt.next();
27
+ * }
28
+ * };
24
29
  *
25
- * **Events.** `next()` answers a `WT_*` event, or one of `nish/net/http3`'s
26
- * for a plain request, which passes through untouched so one connection can
27
- * serve both: `WT_SESSION` (a session asked for: `sessionId`, the CONNECT
28
- * stream's ID, and `fields`; the program `accept`s or `refuse`s it),
29
- * `WT_DATAGRAM` (a datagram of an accepted session, a window onto `data`),
30
- * `WT_STREAM` (the client opened a stream of an accepted session: `stream`,
31
- * `bidirectional`), `WT_STREAM_DATA` (its bytes, a window onto `data`),
32
- * `WT_STREAM_END` (its FIN), `WT_STREAM_RESET` (the client reset its side:
33
- * `errorCode`, the HTTP/3 code, and `appCode`, the WebTransport code it maps
34
- * to or -1), `WT_STREAM_STOPPED` (the client asked this side to stop writing:
35
- * the same two codes), `WT_WRITABLE` (a stream, or a CONNECT stream whose
36
- * capsule was held back, can take more), `WT_DRAIN` (the client sent
37
- * DRAIN_WEBTRANSPORT_SESSION) and `WT_CLOSED` (the session is over:
38
- * `errorCode`, the CLOSE capsule's code or 0, the reason a window onto
39
- * `reason`, and `closedByPeer`). Each event names its session in `sessionId`
40
- * and in `session`, its index in the table, `0 .. webtransportSessions - 1`,
41
- * for a program's own per-session arrays. A window is valid until the next
42
- * call.
30
+ * **Events.**
31
+ *
32
+ * `next()` answers a `WT_*` event, or one of `nish/net/http3`'s for a plain
33
+ * request, which passes through untouched so one connection can serve both:
34
+ * - `WT_SESSION` (a session asked for: `sessionId`, the CONNECT stream's ID,
35
+ * and `fields`; the program `accept`s or `refuse`s it),
36
+ * - `WT_DATAGRAM` (a datagram of an accepted session, a window onto `data`),
37
+ * - `WT_STREAM` (the client opened a stream of an accepted session: `stream`,
38
+ * `bidirectional`),
39
+ * - `WT_STREAM_DATA` (its bytes, a window onto `data`),
40
+ * - `WT_STREAM_END` (its FIN),
41
+ * - `WT_STREAM_RESET` (the client reset its side: `errorCode`, the HTTP/3 code,
42
+ * and `appCode`, the WebTransport code it maps to or -1),
43
+ * - `WT_STREAM_STOPPED` (the client asked this side to stop writing: the same
44
+ * two codes),
45
+ * - `WT_WRITABLE` (a stream, or a CONNECT stream whose capsule was held back,
46
+ * can take more),
47
+ * - `WT_DRAIN` (the client sent DRAIN_WEBTRANSPORT_SESSION) and `WT_CLOSED`
48
+ * (the session is over: `errorCode`, the CLOSE capsule's code or 0, the
49
+ * reason a window onto `data`, and `closedByPeer`).
50
+ *
51
+ * Each event names its session in `sessionId` and in `session`, its index in
52
+ * the table, `0 .. webtransportSessions - 1`, for a program's own per-session
53
+ * arrays. A window is valid until the next call.
43
54
  *
44
55
  * **Writes.** `sendDatagram` (the quarter stream ID, then the payload, one
45
56
  * QUIC DATAGRAM frame; `maxDatagramPayload` says how much fits), `openStream`,
@@ -161,7 +172,9 @@ export const WT_STREAM_RESET: i32 = 21
161
172
  export const WT_STREAM_STOPPED: i32 = 22
162
173
  /** `stream` — a session's stream, or its CONNECT stream — can take more. */
163
174
  export const WT_WRITABLE: i32 = 23
164
- /** The session is over: `errorCode`, `reason[reasonStart .. reasonStart + reasonLength)`, `closedByPeer`. */
175
+ /**
176
+ * The session is over: `errorCode`, the reason `data[dataStart .. dataStart + dataLength)`, `closedByPeer`.
177
+ */
165
178
  export const WT_CLOSED: i32 = 24
166
179
  /** The client sent DRAIN_WEBTRANSPORT_SESSION: it will close the session soon. */
167
180
  export const WT_DRAIN: i32 = 25
@@ -223,13 +236,26 @@ export class WebTransportConfig {
223
236
  maxPending: i32 = 16
224
237
  }
225
238
 
226
- /** The HTTP/3 code an application's 32-bit `code` is sent as (`WT_FIRST_APP_CODE` + n + n / 0x1e, skipping the greased values). */
239
+ /**
240
+ * The HTTP/3 code an application's 32-bit `code` is sent as (`WT_FIRST_APP_CODE` + n + n / 0x1e, skipping
241
+ * the greased values).
242
+ * @param code The application's code; only its low 32 bits are used, so a value past 2^32 - 1 or below 0
243
+ * is taken as those bits.
244
+ * @returns The HTTP/3 error code, from `WT_FIRST_APP_CODE` to `wtCodeToHttp3(0xffffffff)`, never a greased
245
+ * value.
246
+ */
227
247
  export const wtCodeToHttp3 = (code: i64): i64 => {
228
248
  const n: i64 = code & WT_U32
229
249
  return WT_FIRST_APP_CODE + n + n / toI64(0x1e)
230
250
  }
231
251
 
232
- /** The application code an HTTP/3 `code` carries, or -1 when it is outside the range, or one of its greased values. */
252
+ /**
253
+ * The application code an HTTP/3 `code` carries, or -1 when it is outside the range, or one of its greased
254
+ * values.
255
+ * @param code An HTTP/3 error code, such as a peer's RESET_STREAM or STOP_SENDING code; any value.
256
+ * @returns The 32-bit application code, 0 to 2^32 - 1, the inverse of `wtCodeToHttp3`; -1 for a code below
257
+ * `WT_FIRST_APP_CODE`, above `wtCodeToHttp3(0xffffffff)`, or greased.
258
+ */
233
259
  export const wtCodeFromHttp3 = (code: i64): i64 => {
234
260
  const last: i64 = wtCodeToHttp3(WT_U32)
235
261
  if (code < WT_FIRST_APP_CODE || code > last || h3Greased(code)) {
@@ -256,17 +282,22 @@ const webtransportCheckCap = (what: string, value: i32, low: i32, high: i32): vo
256
282
  */
257
283
  export class WebTransportWaiting {
258
284
  /** Entry `e`: the stream, the session it named, its bucket's neighbours; a free entry's `next` threads the free list. */
259
- streams: i64[]
260
- sessions: i64[]
261
- next: i32[]
262
- prev: i32[]
285
+ private streams: i64[]
286
+ private sessions: i64[]
287
+ private next: i32[]
288
+ private prev: i32[]
263
289
  /** Each bucket's first and last entry, or -1. */
264
- heads: i32[]
265
- tails: i32[]
266
- salt: i64 = 0
267
- freeHead: i32 = -1
290
+ private heads: i32[]
291
+ private tails: i32[]
292
+ private salt: i64 = 0
293
+ private freeHead: i32 = -1
294
+ /** How many streams are waiting now. */
268
295
  count: i32 = 0
269
296
 
297
+ /**
298
+ * Makes an empty room, its hash salted with 4 bytes from `crypto.getRandomValues`.
299
+ * @param size How many streams may wait at once; 0 or less makes room for 1.
300
+ */
270
301
  constructor(size: i32) {
271
302
  const n: i32 = size > 0 ? size : WT_ONE
272
303
  this.streams = new Array<i64>(n)
@@ -287,7 +318,7 @@ export class WebTransportWaiting {
287
318
  this.reset()
288
319
  }
289
320
 
290
- /** Empties the room. */
321
+ /** @internal Empties the room; called by `WebTransport` in this module, not for callers. */
291
322
  reset(): void {
292
323
  this.heads.fill(WT_NONE)
293
324
  this.tails.fill(WT_NONE)
@@ -300,13 +331,16 @@ export class WebTransportWaiting {
300
331
  }
301
332
 
302
333
  /** The bucket of session ID `session`: its stream number, salted, times an odd constant, high bits. */
303
- bucket(session: i64): i32 {
334
+ private bucket(session: i64): i32 {
304
335
  const x: i64 = ((session >> toI64(2)) ^ this.salt) & toI64(0x7fffffff)
305
336
  const mixed: i64 = (x * WT_HASH_MULTIPLIER) >> toI64(16)
306
337
  return toI32(mixed & toI64(toI32(this.heads.length) - 1))
307
338
  }
308
339
 
309
- /** Files stream `stream`, waiting for session `session`; answers whether there was room. */
340
+ /**
341
+ * Files stream `stream`, waiting for session `session`; answers whether there was room.
342
+ * @internal Called by `WebTransport` in this module; not for callers.
343
+ */
310
344
  add(stream: i64, session: i64): boolean {
311
345
  const e: i32 = this.freeHead
312
346
  if (e < 0 || e >= toI32(this.next.length)) {
@@ -329,7 +363,10 @@ export class WebTransportWaiting {
329
363
  return true
330
364
  }
331
365
 
332
- /** The oldest entry waiting for session `session`, or -1. */
366
+ /**
367
+ * The oldest entry waiting for session `session`, or -1.
368
+ * @internal Called by `WebTransport` in this module; not for callers.
369
+ */
333
370
  first(session: i64): i32 {
334
371
  let e: i32 = this.heads[this.bucket(session)]
335
372
  for (
@@ -345,7 +382,10 @@ export class WebTransportWaiting {
345
382
  return WT_NONE
346
383
  }
347
384
 
348
- /** Takes entry `e` out of the room and answers its stream. */
385
+ /**
386
+ * Takes entry `e` out of the room and answers its stream.
387
+ * @internal Called by `WebTransport` in this module; not for callers.
388
+ */
349
389
  take(e: i32): i64 {
350
390
  const stream: i64 = this.streams[e]
351
391
  const b: i32 = this.bucket(this.sessions[e])
@@ -374,87 +414,122 @@ export class WebTransportWaiting {
374
414
  * which.
375
415
  */
376
416
  export class WebTransport {
417
+ /** The caps this layer was made with; the arrays they size are made by the constructor. */
377
418
  config: WebTransportConfig
378
- h3: Http3Connection
379
- quic: QuicConnection
380
- /** The request fields of the last WT_SESSION: `h3.fields`. */
419
+ private h3: Http3Connection
420
+ private quic: QuicConnection
421
+ /** The request fields of the last WT_SESSION: the same object as the `Http3Connection`'s `fields`. */
381
422
  fields: HttpFields
382
- header: Http3FrameHeader
423
+ private header: Http3FrameHeader
383
424
  /** A datagram read whole, and one being sent; a CLOSE capsule being written. */
384
- datagram: u8[]
385
- outgoing: u8[]
386
- capsuleOut: u8[]
387
- /** The window of WT_DATAGRAM, WT_STREAM_DATA and WT_CLOSED: `datagram`, `h3.data`, or `reason`. */
425
+ private datagram: u8[]
426
+ private outgoing: u8[]
427
+ private capsuleOut: u8[]
428
+ /**
429
+ * The buffer the window of WT_DATAGRAM, WT_STREAM_DATA and WT_CLOSED lies in: this layer's own
430
+ * datagram buffer, the `Http3Connection`'s `data`, or this layer's store of CLOSE reasons. Valid
431
+ * until the next call.
432
+ */
388
433
  data: u8[]
389
434
  /** Each session's CLOSE payload as it arrives, `WT_REASON_MAX + 4` bytes a session. */
390
- reason: u8[]
435
+ private reason: u8[]
391
436
  /** The response fields of an accepted session, and of a refusal. */
392
- acceptNames: u8[][]
393
- acceptValues: u8[][]
394
- none: u8[][]
437
+ private acceptNames: u8[][]
438
+ private acceptValues: u8[][]
439
+ private none: u8[][]
395
440
  // Per session.
441
+ /** Each session's ID, its CONNECT stream's ID, by session index; -1 for a free index. */
396
442
  sessionIds: i64[]
397
443
  /** The capsule being read: its type, and what is left of its payload (-1 between capsules). */
398
- capType: i64[]
399
- capLeft: i64[]
400
- states: i32[]
444
+ private capType: i64[]
445
+ private capLeft: i64[]
446
+ private states: i32[]
401
447
  /** The CONNECT stream's QUIC slot. */
402
- sessionSlot: i32[]
448
+ private sessionSlot: i32[]
403
449
  /** The first of its streams (a QUIC slot, threaded through `nextStream`), and how many. */
404
- firstStream: i32[]
450
+ private firstStream: i32[]
451
+ /**
452
+ * How many streams each session holds, the client's and this side's together, up to `maxStreams`;
453
+ * a stream stops counting once both of its sides are over.
454
+ */
405
455
  streamCount: i32[]
406
456
  /** Whether the client's CLOSE has been read: nothing may follow it. */
407
- closeRead: boolean[]
457
+ private closeRead: boolean[]
408
458
  /** A capsule header being read, `capHeadLen[s]` bytes of `capHead[s * 16 ..]`, and the CLOSE payload's fill. */
409
- capHeadLen: i32[]
410
- capHead: u8[]
411
- capFill: i32[]
459
+ private capHeadLen: i32[]
460
+ private capHead: u8[]
461
+ private capFill: i32[]
412
462
  /** Counters for a log or a rate cap, each saturating: datagrams in and out. */
413
463
  datagramsIn: i32[]
464
+ /** How many datagrams each session sent, saturating at 2^31 - 1; zeroed when a session is asked for. */
414
465
  datagramsOut: i32[]
415
466
  /** The free sessions, a stack. */
416
- free: i32[]
467
+ private free: i32[]
417
468
  // Per QUIC stream slot.
418
469
  /** The session whose CONNECT stream the slot holds, or -1. */
419
470
  sessionAt: i32[]
420
471
  /** The stream the slot's stream state is for, its session, its neighbours in that session's list, and its bits. */
421
- streamIds: i64[]
422
- streamSession: i32[]
423
- nextStream: i32[]
424
- prevStream: i32[]
425
- streamBits: i32[]
472
+ private streamIds: i64[]
473
+ private streamSession: i32[]
474
+ private nextStream: i32[]
475
+ private prevStream: i32[]
476
+ private streamBits: i32[]
426
477
  /** The streams waiting for a session. */
427
478
  waiting: WebTransportWaiting
428
479
  /** Sessions accepted while streams waited for them, `adoptCount` of them: `next` takes those streams first. */
429
- adoptions: i32[]
480
+ private adoptions: i32[]
430
481
  /** The last event's session ID, stream, and codes. */
431
482
  sessionId: i64 = -1
483
+ /**
484
+ * The stream ID of the last event about a stream: the client's or this side's stream, or the
485
+ * session's CONNECT stream; a WT_DATAGRAM leaves it as it was.
486
+ */
432
487
  stream: i64 = -1
488
+ /**
489
+ * The HTTP/3 code of the last WT_STREAM_RESET or WT_STREAM_STOPPED, or the 32-bit code of the
490
+ * last WT_CLOSED's CLOSE capsule (0 when the session ended without one).
491
+ */
433
492
  errorCode: i64 = 0
493
+ /**
494
+ * The WebTransport code `errorCode` maps to (`wtCodeFromHttp3`), or -1 when it maps to none; for
495
+ * WT_CLOSED, `errorCode` itself.
496
+ */
434
497
  appCode: i64 = -1
435
498
  /** Where the capsules of a DATA window are being read: `h3.data[capAt .. capEnd)`, for session `capSession`. */
436
- capAt: i32 = 0
437
- capEnd: i32 = 0
438
- capSession: i32 = -1
499
+ private capAt: i32 = 0
500
+ private capEnd: i32 = 0
501
+ private capSession: i32 = -1
439
502
  /** The last event's session index, and its window onto `data`. */
440
503
  session: i32 = -1
504
+ /** Where the last event's window starts in `data`. */
441
505
  dataStart: i32 = 0
506
+ /** How many bytes the last event's window has. */
442
507
  dataLength: i32 = 0
508
+ /** How many session indexes are free; a valid request for a session while none is free is answered 429. */
443
509
  freeCount: i32 = 0
444
- adoptCount: i32 = 0
510
+ private adoptCount: i32 = 0
445
511
  /** The `h3.generation` this layer's state belongs to. */
446
- generation: i32 = 0
512
+ private generation: i32 = 0
447
513
  /** Counters for a log or a test, each saturating: datagrams dropped, sessions refused here, streams refused. */
448
514
  datagramsDropped: i32 = 0
515
+ /** How many requests for a session this layer answered with a 400, 429 or 501, or `refuse` refused. */
449
516
  sessionsRefused: i32 = 0
450
- streamsRefused: i32 = 0
517
+ private streamsRefused: i32 = 0
451
518
  /** WT_STREAM's kind, and whether WT_CLOSED came from the client. */
452
519
  bidirectional: boolean = false
520
+ /**
521
+ * Whether the last WT_CLOSED came from the client: its CLOSE capsule, its FIN, or a reset the
522
+ * client sent; false when this side ended the session, including when it reset the stream for a
523
+ * malformed capsule or a FIN inside one.
524
+ */
453
525
  closedByPeer: boolean = false
454
526
 
455
527
  /**
456
528
  * The WebTransport layer of `h3`, whose `Http3Config` must have
457
529
  * `webtransportSessions` above 0. A cap out of range panics.
530
+ * @param config The caps: `maxStreams` 1 to 4096 and `maxPending` 0 to 4096; kept, not copied.
531
+ * @param h3 The connection this layer reads and writes, kept for the layer's life; its restarts
532
+ * restart this layer too.
458
533
  */
459
534
  constructor(config: WebTransportConfig, h3: Http3Connection) {
460
535
  const sessions: i32 = h3.config.webtransportSessions
@@ -506,7 +581,7 @@ export class WebTransport {
506
581
  }
507
582
 
508
583
  /** Forgets every session and stream, for the connection's next peer; `next` calls it when `h3` restarted. */
509
- restart(): void {
584
+ private restart(): void {
510
585
  this.generation = this.h3.generation
511
586
  this.sessionIds.fill(WT_NONE64)
512
587
  this.states.fill(WT_FREE)
@@ -538,7 +613,7 @@ export class WebTransport {
538
613
  }
539
614
 
540
615
  /** Restarts this layer when its connection restarted since the last call. */
541
- current(): void {
616
+ private current(): void {
542
617
  if (this.generation !== this.h3.generation) {
543
618
  this.restart()
544
619
  }
@@ -547,7 +622,7 @@ export class WebTransport {
547
622
  // ---- Lookups ---------------------------------------------------------------------
548
623
 
549
624
  /** The index of the session whose CONNECT stream is `id`, or -1: one probe of QUIC's stream index. */
550
- sessionOf(id: i64): i32 {
625
+ private sessionOf(id: i64): i32 {
551
626
  const k: i32 = this.quic.streams.slotOf(id)
552
627
  if (k < 0 || k >= toI32(this.sessionAt.length)) {
553
628
  return WT_NONE
@@ -557,7 +632,7 @@ export class WebTransport {
557
632
  }
558
633
 
559
634
  /** The QUIC slot of a session's stream `id` that this layer tracks, or -1. */
560
- streamSlot(id: i64): i32 {
635
+ private streamSlot(id: i64): i32 {
561
636
  const k: i32 = this.quic.streams.slotOf(id)
562
637
  if (k < 0 || k >= toI32(this.streamIds.length) || this.streamIds[k] !== id) {
563
638
  return WT_NONE
@@ -566,7 +641,7 @@ export class WebTransport {
566
641
  }
567
642
 
568
643
  /** The session of `id` when it is accepted and not gone, else -1. */
569
- openSession(id: i64): i32 {
644
+ private openSession(id: i64): i32 {
570
645
  this.current()
571
646
  const s: i32 = this.sessionOf(id)
572
647
  return s >= 0 && this.states[s] === WT_OPEN ? s : WT_NONE
@@ -578,6 +653,9 @@ export class WebTransport {
578
653
  * Reads what the connection has and answers the next event (see the
579
654
  * module comment): a `WT_*` one, or a plain request's `H3_*` one. Call it
580
655
  * until it answers H3_NEED_MORE or H3_ERROR.
656
+ * @returns A `WT_*` event; an `Http3Connection` event passed through, for a plain request or
657
+ * H3_GOAWAY; H3_NEED_MORE when there is nothing more to read now; or H3_ERROR, when
658
+ * `Http3Connection.next` answers it.
581
659
  */
582
660
  next(): i32 {
583
661
  this.current()
@@ -622,7 +700,7 @@ export class WebTransport {
622
700
  * Reads one QUIC datagram, if one is waiting: answers WT_DATAGRAM for one
623
701
  * of an accepted session, 1 for one dropped, or 0 when none is there.
624
702
  */
625
- receiveDatagram(): i32 {
703
+ private receiveDatagram(): i32 {
626
704
  const n: i32 = this.quic.readDatagram(this.datagram, WT_ZERO, toI32(this.datagram.length))
627
705
  if (n === QUIC_DATAGRAM_NONE || n < 0) {
628
706
  return WT_ZERO
@@ -650,7 +728,7 @@ export class WebTransport {
650
728
  }
651
729
 
652
730
  /** What an `Http3Connection` event means here: a `WT_*` event, the same event passed through, or H3_NEED_MORE. */
653
- route(event: i32): i32 {
731
+ private route(event: i32): i32 {
654
732
  const h3: Http3Connection = this.h3
655
733
  const id: i64 = h3.stream
656
734
  if (event === H3_STREAM) {
@@ -710,13 +788,13 @@ export class WebTransport {
710
788
  }
711
789
 
712
790
  /** Sets `errorCode` to `code` and `appCode` to what it maps to. */
713
- codes(code: i64): void {
791
+ private codes(code: i64): void {
714
792
  this.errorCode = code
715
793
  this.appCode = wtCodeFromHttp3(code)
716
794
  }
717
795
 
718
796
  /** An event of session `s`'s CONNECT stream. */
719
- connectEvent(s: i32, event: i32): i32 {
797
+ private connectEvent(s: i32, event: i32): i32 {
720
798
  const h3: Http3Connection = this.h3
721
799
  this.session = s
722
800
  this.sessionId = this.sessionIds[s]
@@ -763,7 +841,7 @@ export class WebTransport {
763
841
  * no head for it to follow. Answers whether the FIN went: after a reset
764
842
  * the stream is dropped unread, so no event will end the session later.
765
843
  */
766
- finish(id: i64): boolean {
844
+ private finish(id: i64): boolean {
767
845
  if (this.h3.writeData(id, this.reason, WT_ZERO, WT_ZERO, true) >= 0) {
768
846
  return true
769
847
  }
@@ -772,7 +850,7 @@ export class WebTransport {
772
850
  }
773
851
 
774
852
  /** WT_CLOSED for session `s` with `code` and the reason `reason[s * 1028 + 4 ..][0 .. length)`. */
775
- closedEvent(s: i32, code: i64, length: i32, byPeer: boolean): i32 {
853
+ private closedEvent(s: i32, code: i64, length: i32, byPeer: boolean): i32 {
776
854
  this.session = s
777
855
  this.errorCode = code & WT_U32
778
856
  this.appCode = this.errorCode
@@ -789,7 +867,7 @@ export class WebTransport {
789
867
  * An extended CONNECT arrived on `id`: a session when it is one this side
790
868
  * takes, answered here when not.
791
869
  */
792
- asked(id: i64): i32 {
870
+ private asked(id: i64): i32 {
793
871
  const h3: Http3Connection = this.h3
794
872
  const f: HttpFields = h3.fields
795
873
  const k: i32 = this.quic.streams.slotOf(id)
@@ -844,7 +922,7 @@ export class WebTransport {
844
922
  }
845
923
 
846
924
  /** Answers the request on `id` with `status` and stops reading it: a session refused. */
847
- answer(id: i64, status: i32): void {
925
+ private answer(id: i64, status: i32): void {
848
926
  this.sessionsRefused = h3Count(this.sessionsRefused)
849
927
  if (this.h3.respond(id, status, this.none, this.none, true) !== 0) {
850
928
  this.h3.reset(id, H3_REQUEST_REJECTED)
@@ -857,6 +935,11 @@ export class WebTransport {
857
935
  * streams reach the program from now on, the streams that waited for it
858
936
  * first. Answers 0, H3_AGAIN when the response does not fit yet, or
859
937
  * H3_CLOSED for one that is not a session asked for.
938
+ * @param id The session's ID, the `sessionId` of its WT_SESSION.
939
+ * @returns 0 once the 200 is written; H3_AGAIN while the stream has no room for it; H3_TOO_LARGE
940
+ * when the head is past the client's SETTINGS_MAX_FIELD_SECTION_SIZE or the stream's buffer; or
941
+ * H3_CLOSED for an ID that is not a session asked for and not yet answered, or a stream that can
942
+ * no longer be written.
860
943
  */
861
944
  accept(id: i64): i32 {
862
945
  this.current()
@@ -881,6 +964,10 @@ export class WebTransport {
881
964
  * Refuses session `id` with `status`, 400 to 599 (draft-02 §3.2): the
882
965
  * response ends the stream, and what the client still sends is dropped.
883
966
  * Answers 0, H3_INVALID for a status out of range, or H3_CLOSED.
967
+ * @param id The session's ID, the `sessionId` of its WT_SESSION.
968
+ * @param status The response status, 400 to 599.
969
+ * @returns 0 once the session is let go; H3_INVALID for a status outside 400 to 599; or H3_CLOSED
970
+ * for an ID that is not a session asked for and not yet answered.
884
971
  */
885
972
  refuse(id: i64, status: i32): i32 {
886
973
  this.current()
@@ -900,7 +987,7 @@ export class WebTransport {
900
987
  * Ends session `s` here: every stream reset with WT_SESSION_GONE and asked
901
988
  * to stop, the streams waiting for it refused, and its slot freed.
902
989
  */
903
- release(s: i32): void {
990
+ private release(s: i32): void {
904
991
  this.end(s)
905
992
  const k: i32 = this.sessionSlot[s]
906
993
  if (k >= 0 && k < toI32(this.sessionAt.length) && this.sessionAt[k] === s) {
@@ -918,7 +1005,7 @@ export class WebTransport {
918
1005
  }
919
1006
 
920
1007
  /** Resets every stream of session `s` and refuses those waiting for it; the session is gone. */
921
- end(s: i32): void {
1008
+ private end(s: i32): void {
922
1009
  if (this.states[s] === WT_FREE) {
923
1010
  return
924
1011
  }
@@ -937,7 +1024,7 @@ export class WebTransport {
937
1024
  }
938
1025
 
939
1026
  /** Refuses with WT_SESSION_GONE every stream waiting for session `id`. */
940
- dropPending(id: i64): void {
1027
+ private dropPending(id: i64): void {
941
1028
  if (this.waiting.count === 0) {
942
1029
  return
943
1030
  }
@@ -955,6 +1042,16 @@ export class WebTransport {
955
1042
  * with WT_SESSION_GONE. Answers 0; H3_INVALID for a reason too long;
956
1043
  * H3_AGAIN while the CONNECT stream has no room for the capsule, and
957
1044
  * WT_WRITABLE then names it; or H3_CLOSED.
1045
+ * @param id The session's ID.
1046
+ * @param code The application's code; only its low 32 bits are sent.
1047
+ * @param buf The buffer holding the reason; only read, and copied into the capsule. A window outside
1048
+ * it panics, whatever the session's state.
1049
+ * @param off Where the reason starts in `buf`.
1050
+ * @param len How many bytes the reason has, at most 1024.
1051
+ * @returns 0 once the capsule and the FIN are written; H3_INVALID for a reason past 1024 bytes;
1052
+ * H3_AGAIN while the CONNECT stream has no room for the capsule; H3_TOO_LARGE for a capsule past
1053
+ * the stream's buffer; or H3_CLOSED for a session not accepted or already gone, or a CONNECT
1054
+ * stream that can no longer be written.
958
1055
  */
959
1056
  close(id: i64, code: i64, buf: u8[], off: i32, len: i32): i32 {
960
1057
  h3CheckWindow("WebTransport.close", buf, off, len)
@@ -986,6 +1083,10 @@ export class WebTransport {
986
1083
  /**
987
1084
  * Sends DRAIN_WEBTRANSPORT_SESSION on session `id`: the client should
988
1085
  * finish and close it. Answers 0, H3_AGAIN, or H3_CLOSED.
1086
+ * @param id The session's ID.
1087
+ * @returns 0 once the capsule is written; H3_AGAIN while the CONNECT stream has no room for it; or
1088
+ * H3_CLOSED for a session not accepted or already gone, or a CONNECT stream that can no longer be
1089
+ * written.
989
1090
  */
990
1091
  drain(id: i64): i32 {
991
1092
  const s: i32 = this.openSession(id)
@@ -1005,7 +1106,7 @@ export class WebTransport {
1005
1106
  * session `capSession`'s CONNECT stream, and answers the first event they
1006
1107
  * make, or H3_NEED_MORE once the window is read.
1007
1108
  */
1008
- capsules(): i32 {
1109
+ private capsules(): i32 {
1009
1110
  const s: i32 = this.capSession
1010
1111
  const bytes: u8[] = this.h3.data
1011
1112
  const base: i32 = s * WT_HEAD
@@ -1061,7 +1162,7 @@ export class WebTransport {
1061
1162
  }
1062
1163
 
1063
1164
  /** A whole capsule of `type` on session `s`'s CONNECT stream. */
1064
- capsule(s: i32, type: i64): i32 {
1165
+ private capsule(s: i32, type: i64): i32 {
1065
1166
  this.session = s
1066
1167
  this.sessionId = this.sessionIds[s]
1067
1168
  this.stream = this.sessionIds[s]
@@ -1092,7 +1193,7 @@ export class WebTransport {
1092
1193
  }
1093
1194
 
1094
1195
  /** A capsule that breaks a rule on session `s`: its CONNECT stream reset both ways with `code`, and the session over. */
1095
- malformed(s: i32, code: i64): i32 {
1196
+ private malformed(s: i32, code: i64): i32 {
1096
1197
  const id: i64 = this.sessionIds[s]
1097
1198
  const wasGone: boolean = this.states[s] === WT_GONE
1098
1199
  this.capSession = -1
@@ -1107,7 +1208,7 @@ export class WebTransport {
1107
1208
  // ---- Streams ---------------------------------------------------------------------
1108
1209
 
1109
1210
  /** A stream named session `session` in its first bytes (H3_STREAM): taken, kept waiting or refused. */
1110
- arrived(id: i64, session: i64): i32 {
1211
+ private arrived(id: i64, session: i64): i32 {
1111
1212
  const s: i32 = this.sessionOf(session)
1112
1213
  if (s >= 0 && this.states[s] === WT_OPEN) {
1113
1214
  return this.adopt(s, id)
@@ -1129,7 +1230,7 @@ export class WebTransport {
1129
1230
  * with streams waiting: answers its WT_STREAM, or H3_NEED_MORE once that
1130
1231
  * session has none left (or is gone), when the next session's turn comes.
1131
1232
  */
1132
- adoptWaiting(): i32 {
1233
+ private adoptWaiting(): i32 {
1133
1234
  const s: i32 = this.adoptions[this.adoptCount - 1]
1134
1235
  if (s >= 0 && s < toI32(this.states.length) && this.states[s] === WT_OPEN) {
1135
1236
  const e: i32 = this.waiting.first(this.sessionIds[s])
@@ -1142,7 +1243,7 @@ export class WebTransport {
1142
1243
  }
1143
1244
 
1144
1245
  /** Refuses stream `id` with `code`, counted. */
1145
- refuseStream(id: i64, code: i64): void {
1246
+ private refuseStream(id: i64, code: i64): void {
1146
1247
  this.streamsRefused = h3Count(this.streamsRefused)
1147
1248
  this.h3.refuseStream(id, code)
1148
1249
  }
@@ -1152,7 +1253,7 @@ export class WebTransport {
1152
1253
  * holds `maxStreams` already: answers WT_STREAM, or H3_NEED_MORE for one
1153
1254
  * refused with H3_REQUEST_REJECTED.
1154
1255
  */
1155
- adopt(s: i32, id: i64): i32 {
1256
+ private adopt(s: i32, id: i64): i32 {
1156
1257
  if (this.streamCount[s] >= this.config.maxStreams) {
1157
1258
  this.refuseStream(id, H3_REQUEST_REJECTED)
1158
1259
  return H3_NEED_MORE
@@ -1169,7 +1270,7 @@ export class WebTransport {
1169
1270
  }
1170
1271
 
1171
1272
  /** Puts stream `id` at the head of session `s`'s list with `bits`; answers its slot, or -1. */
1172
- link(s: i32, id: i64, bits: i32): i32 {
1273
+ private link(s: i32, id: i64, bits: i32): i32 {
1173
1274
  const k: i32 = this.quic.streams.slotOf(id)
1174
1275
  if (k < 0 || k >= toI32(this.streamIds.length)) {
1175
1276
  return WT_NONE
@@ -1193,7 +1294,7 @@ export class WebTransport {
1193
1294
  }
1194
1295
 
1195
1296
  /** Marks `bits` done on the stream in slot `k`, and takes it off its session once both sides are. */
1196
- settle(k: i32, bits: i32): void {
1297
+ private settle(k: i32, bits: i32): void {
1197
1298
  this.streamBits[k] = this.streamBits[k] | bits
1198
1299
  if ((this.streamBits[k] & (WT_SENT | WT_RECEIVED)) === (WT_SENT | WT_RECEIVED)) {
1199
1300
  this.unlink(k)
@@ -1201,7 +1302,7 @@ export class WebTransport {
1201
1302
  }
1202
1303
 
1203
1304
  /** Takes the stream in slot `k` off its session's list. */
1204
- unlink(k: i32): void {
1305
+ private unlink(k: i32): void {
1205
1306
  const s: i32 = this.streamSession[k]
1206
1307
  const before: i32 = this.prevStream[k]
1207
1308
  const after: i32 = this.nextStream[k]
@@ -1224,6 +1325,12 @@ export class WebTransport {
1224
1325
  * Opens a stream of this side's for session `id` (draft-02 §4.1, §4.2).
1225
1326
  * Answers its ID; WT_LIMIT when the session holds `maxStreams`; H3_AGAIN
1226
1327
  * while the client's MAX_STREAMS allows no more; or H3_CLOSED.
1328
+ * @param id The session's ID.
1329
+ * @param bidirectional Whether the stream is bidirectional; a unidirectional one has no side for
1330
+ * the client to write.
1331
+ * @returns The new stream's ID; WT_LIMIT when the session holds `maxStreams`; H3_AGAIN while the
1332
+ * client's MAX_STREAMS allows no more; or H3_CLOSED for a session not accepted, or one already
1333
+ * gone.
1227
1334
  */
1228
1335
  openStream(id: i64, bidirectional: boolean): i64 {
1229
1336
  const s: i32 = this.openSession(id)
@@ -1245,6 +1352,14 @@ export class WebTransport {
1245
1352
  * Writes up to `len` bytes of `buf` from `off` to session stream `id`, with
1246
1353
  * its FIN when `fin` and all were taken. Answers how many it took, or
1247
1354
  * H3_AGAIN, or H3_CLOSED (see `Http3Connection.writeStream`).
1355
+ * @param id The stream's ID, one of a session this layer took or opened.
1356
+ * @param buf The buffer holding the bytes; only read, and what is taken is copied.
1357
+ * @param off Where the bytes start in `buf`.
1358
+ * @param len How many bytes to write.
1359
+ * @param fin Whether to end this side of the stream once every byte is taken.
1360
+ * @returns How many bytes it took, fewer than `len` only when the send buffer filled (0 for a bare
1361
+ * FIN); H3_AGAIN when it took none; or H3_CLOSED for a stream this layer does not hold or with no
1362
+ * side here to write.
1248
1363
  */
1249
1364
  write(id: i64, buf: u8[], off: i32, len: i32, fin: boolean): i32 {
1250
1365
  this.current()
@@ -1259,7 +1374,12 @@ export class WebTransport {
1259
1374
  return n
1260
1375
  }
1261
1376
 
1262
- /** Resets this side of session stream `id` with the application's `code` (mapped, `wtCodeToHttp3`). Answers 0 or H3_CLOSED. */
1377
+ /**
1378
+ * Resets this side of session stream `id` with the application's `code` (mapped, `wtCodeToHttp3`).
1379
+ * @param id The stream's ID, one of a session this layer took or opened.
1380
+ * @param code The application's code; only its low 32 bits are sent, mapped.
1381
+ * @returns 0 once the reset is sent, or H3_CLOSED for a stream this layer does not hold.
1382
+ */
1263
1383
  resetStream(id: i64, code: i64): i32 {
1264
1384
  this.current()
1265
1385
  const k: i32 = this.streamSlot(id)
@@ -1274,7 +1394,14 @@ export class WebTransport {
1274
1394
  return 0
1275
1395
  }
1276
1396
 
1277
- /** Asks the client to stop its side of session stream `id` with the application's `code`, and drops what it sends. Answers 0 or H3_CLOSED. */
1397
+ /**
1398
+ * Asks the client to stop its side of session stream `id` with the application's `code`, and drops
1399
+ * what it sends.
1400
+ * @param id The stream's ID, one of a session this layer took or opened.
1401
+ * @param code The application's code; only its low 32 bits are sent, mapped (`wtCodeToHttp3`).
1402
+ * @returns 0 once STOP_SENDING is sent, or H3_CLOSED for a stream this layer does not hold or
1403
+ * whose client side is already over.
1404
+ */
1278
1405
  stopSending(id: i64, code: i64): i32 {
1279
1406
  this.current()
1280
1407
  const k: i32 = this.streamSlot(id)
@@ -1295,6 +1422,8 @@ export class WebTransport {
1295
1422
  * The largest payload `sendDatagram` takes for session `id` now: what the
1296
1423
  * QUIC connection's `maxDatagramPayload` leaves after the quarter stream
1297
1424
  * ID; 0 for a session not accepted.
1425
+ * @param id The session's ID.
1426
+ * @returns The most payload bytes one datagram may carry now, 0 or more.
1298
1427
  */
1299
1428
  maxDatagramPayload(id: i64): i32 {
1300
1429
  if (this.openSession(id) < 0) {
@@ -1310,6 +1439,14 @@ export class WebTransport {
1310
1439
  * frame, never sent again if lost. Answers 0; H3_TOO_LARGE past
1311
1440
  * `maxDatagramPayload`; H3_AGAIN while QUIC's queue is full; or H3_CLOSED
1312
1441
  * for a session not accepted.
1442
+ * @param id The session's ID.
1443
+ * @param buf The buffer holding the payload; only read, and copied into the datagram. A window
1444
+ * outside it panics, whatever the session's state.
1445
+ * @param off Where the payload starts in `buf`.
1446
+ * @param len How many bytes the payload has.
1447
+ * @returns 0 once the datagram is queued; H3_AGAIN while QUIC's queue is full; H3_TOO_LARGE past
1448
+ * `maxDatagramPayload`, or when QUIC refuses it for any other reason; or H3_CLOSED for a session
1449
+ * not accepted, or one already gone.
1313
1450
  */
1314
1451
  sendDatagram(id: i64, buf: u8[], off: i32, len: i32): i32 {
1315
1452
  h3CheckWindow("WebTransport.sendDatagram", buf, off, len)