nish 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 (118) hide show
  1. package/docs/AI.md +9 -2
  2. package/docs/INSTALL.md +6 -1
  3. package/package.json +15 -5
  4. package/runtime/nish.d.ts +4612 -430
  5. package/runtime/vite.d.ts +30 -0
  6. package/runtime/vite.mjs +150 -0
  7. package/scripts/build.sh +108 -13
  8. package/std/base64.ts +34 -5
  9. package/std/bytes.ts +256 -40
  10. package/std/cli-args.ts +296 -70
  11. package/std/cli-flags.ts +184 -19
  12. package/std/cli.ts +475 -54
  13. package/std/collections.ts +121 -23
  14. package/std/compress/checksum.ts +44 -14
  15. package/std/compress/deflate.ts +224 -111
  16. package/std/compress/gzip.ts +217 -53
  17. package/std/compress/zlib.ts +63 -7
  18. package/std/crypto/aes.ts +80 -9
  19. package/std/crypto/base64url.ts +22 -0
  20. package/std/crypto/bcrypt-batch.ts +88 -21
  21. package/std/crypto/bcrypt.ts +60 -0
  22. package/std/crypto/chacha20poly1305.ts +53 -0
  23. package/std/crypto/ct.ts +32 -13
  24. package/std/crypto/ed25519.ts +47 -0
  25. package/std/crypto/hkdf.ts +198 -13
  26. package/std/crypto/hmac.ts +235 -31
  27. package/std/crypto/jwt.ts +192 -63
  28. package/std/crypto/p256.ts +159 -16
  29. package/std/crypto/p384.ts +28 -3
  30. package/std/crypto/pki-roots.ts +32 -2
  31. package/std/crypto/pki.ts +81 -15
  32. package/std/crypto/rsa.ts +33 -3
  33. package/std/crypto/sha1.ts +3 -0
  34. package/std/crypto/sha256.ts +24 -1
  35. package/std/crypto/sha512.ts +99 -14
  36. package/std/crypto/x25519.ts +152 -14
  37. package/std/crypto/x509-random.ts +10 -0
  38. package/std/crypto/x509.ts +67 -7
  39. package/std/json-equal.ts +19 -0
  40. package/std/json-format.ts +112 -13
  41. package/std/json-number.ts +64 -14
  42. package/std/json-pointer.ts +23 -1
  43. package/std/json-tokenizer.ts +100 -29
  44. package/std/json-typed.ts +713 -66
  45. package/std/json-value.ts +400 -124
  46. package/std/json.ts +138 -10
  47. package/std/map.ts +25 -7
  48. package/std/math.ts +36 -4
  49. package/std/net/buffer-pool.ts +42 -11
  50. package/std/net/dns-config.ts +109 -23
  51. package/std/net/dns-message.ts +62 -7
  52. package/std/net/dns-resolver.ts +247 -53
  53. package/std/net/dns.ts +203 -35
  54. package/std/net/fetch.ts +151 -61
  55. package/std/net/handler.ts +278 -80
  56. package/std/net/hpack.ts +262 -52
  57. package/std/net/http-compress.ts +125 -15
  58. package/std/net/http-fields.ts +85 -9
  59. package/std/net/http1-client-codec.ts +100 -19
  60. package/std/net/http1-codec.ts +235 -27
  61. package/std/net/http1-server.ts +343 -85
  62. package/std/net/http2-frame.ts +272 -35
  63. package/std/net/http2-tls.ts +135 -34
  64. package/std/net/http2.ts +247 -106
  65. package/std/net/http3-frame.ts +154 -17
  66. package/std/net/http3-server.ts +246 -96
  67. package/std/net/http3.ts +307 -115
  68. package/std/net/outbound.ts +37 -3
  69. package/std/net/postgres-carrier.ts +144 -8
  70. package/std/net/postgres.ts +723 -103
  71. package/std/net/prefork.ts +164 -31
  72. package/std/net/qpack.ts +105 -18
  73. package/std/net/quic-conn-ack.ts +59 -12
  74. package/std/net/quic-conn-cid.ts +124 -27
  75. package/std/net/quic-conn-params.ts +70 -10
  76. package/std/net/quic-datagram.ts +43 -3
  77. package/std/net/quic-frame.ts +286 -12
  78. package/std/net/quic-listener.ts +101 -32
  79. package/std/net/quic-packet.ts +302 -21
  80. package/std/net/quic-recovery.ts +228 -44
  81. package/std/net/quic-stream.ts +353 -91
  82. package/std/net/quic.ts +489 -171
  83. package/std/net/router.ts +87 -40
  84. package/std/net/tls/client.ts +166 -51
  85. package/std/net/tls/codec.ts +413 -56
  86. package/std/net/tls/record-client.ts +110 -55
  87. package/std/net/tls/record-server.ts +159 -59
  88. package/std/net/tls/record.ts +109 -12
  89. package/std/net/tls/schedule.ts +235 -26
  90. package/std/net/tls/ticket.ts +159 -40
  91. package/std/net/tls/verify.ts +49 -3
  92. package/std/net/tls-tcp.ts +250 -45
  93. package/std/net/tls.ts +221 -59
  94. package/std/net/upstream.ts +81 -45
  95. package/std/net/url.ts +97 -13
  96. package/std/net/websocket.ts +122 -12
  97. package/std/net/webtransport.ts +220 -89
  98. package/std/pair.ts +10 -2
  99. package/std/random.ts +34 -9
  100. package/std/regex.ts +352 -198
  101. package/std/region.ts +70 -15
  102. package/std/secret.ts +52 -11
  103. package/std/sort.ts +17 -2
  104. package/std/sqlite.ts +256 -35
  105. package/std/sync/ring.ts +205 -35
  106. package/std/sync/shm.ts +194 -16
  107. package/std/term-io.ts +137 -64
  108. package/std/term-pixels.ts +144 -19
  109. package/std/term-probe.ts +66 -25
  110. package/std/term-rate.ts +128 -57
  111. package/std/term.ts +332 -121
  112. package/std/testing.ts +130 -32
  113. package/std/text.ts +73 -7
  114. package/std/threads.ts +229 -41
  115. package/std/yaml-json.ts +94 -82
  116. package/std/yaml-lines.ts +47 -15
  117. package/std/yaml-write.ts +24 -16
  118. package/std/yaml.ts +764 -384
@@ -10,17 +10,22 @@
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
30
  * **Events.** `next()` answers a `WT_*` event, or one of `nish/net/http3`'s
26
31
  * for a plain request, which passes through untouched so one connection can
@@ -36,7 +41,7 @@
36
41
  * capsule was held back, can take more), `WT_DRAIN` (the client sent
37
42
  * DRAIN_WEBTRANSPORT_SESSION) and `WT_CLOSED` (the session is over:
38
43
  * `errorCode`, the CLOSE capsule's code or 0, the reason a window onto
39
- * `reason`, and `closedByPeer`). Each event names its session in `sessionId`
44
+ * `data`, and `closedByPeer`). Each event names its session in `sessionId`
40
45
  * and in `session`, its index in the table, `0 .. webtransportSessions - 1`,
41
46
  * for a program's own per-session arrays. A window is valid until the next
42
47
  * call.
@@ -161,7 +166,9 @@ export const WT_STREAM_RESET: i32 = 21
161
166
  export const WT_STREAM_STOPPED: i32 = 22
162
167
  /** `stream` — a session's stream, or its CONNECT stream — can take more. */
163
168
  export const WT_WRITABLE: i32 = 23
164
- /** The session is over: `errorCode`, `reason[reasonStart .. reasonStart + reasonLength)`, `closedByPeer`. */
169
+ /**
170
+ * The session is over: `errorCode`, the reason `data[dataStart .. dataStart + dataLength)`, `closedByPeer`.
171
+ */
165
172
  export const WT_CLOSED: i32 = 24
166
173
  /** The client sent DRAIN_WEBTRANSPORT_SESSION: it will close the session soon. */
167
174
  export const WT_DRAIN: i32 = 25
@@ -223,13 +230,26 @@ export class WebTransportConfig {
223
230
  maxPending: i32 = 16
224
231
  }
225
232
 
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). */
233
+ /**
234
+ * The HTTP/3 code an application's 32-bit `code` is sent as (`WT_FIRST_APP_CODE` + n + n / 0x1e, skipping
235
+ * the greased values).
236
+ * @param code The application's code; only its low 32 bits are used, so a value past 2^32 - 1 or below 0
237
+ * is taken as those bits.
238
+ * @returns The HTTP/3 error code, from `WT_FIRST_APP_CODE` to `wtCodeToHttp3(0xffffffff)`, never a greased
239
+ * value.
240
+ */
227
241
  export const wtCodeToHttp3 = (code: i64): i64 => {
228
242
  const n: i64 = code & WT_U32
229
243
  return WT_FIRST_APP_CODE + n + n / toI64(0x1e)
230
244
  }
231
245
 
232
- /** The application code an HTTP/3 `code` carries, or -1 when it is outside the range, or one of its greased values. */
246
+ /**
247
+ * The application code an HTTP/3 `code` carries, or -1 when it is outside the range, or one of its greased
248
+ * values.
249
+ * @param code An HTTP/3 error code, such as a peer's RESET_STREAM or STOP_SENDING code; any value.
250
+ * @returns The 32-bit application code, 0 to 2^32 - 1, the inverse of `wtCodeToHttp3`; -1 for a code below
251
+ * `WT_FIRST_APP_CODE`, above `wtCodeToHttp3(0xffffffff)`, or greased.
252
+ */
233
253
  export const wtCodeFromHttp3 = (code: i64): i64 => {
234
254
  const last: i64 = wtCodeToHttp3(WT_U32)
235
255
  if (code < WT_FIRST_APP_CODE || code > last || h3Greased(code)) {
@@ -256,17 +276,22 @@ const webtransportCheckCap = (what: string, value: i32, low: i32, high: i32): vo
256
276
  */
257
277
  export class WebTransportWaiting {
258
278
  /** 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[]
279
+ private streams: i64[]
280
+ private sessions: i64[]
281
+ private next: i32[]
282
+ private prev: i32[]
263
283
  /** Each bucket's first and last entry, or -1. */
264
- heads: i32[]
265
- tails: i32[]
266
- salt: i64 = 0
267
- freeHead: i32 = -1
284
+ private heads: i32[]
285
+ private tails: i32[]
286
+ private salt: i64 = 0
287
+ private freeHead: i32 = -1
288
+ /** How many streams are waiting now. */
268
289
  count: i32 = 0
269
290
 
291
+ /**
292
+ * Makes an empty room, its hash salted with 4 bytes from `crypto.getRandomValues`.
293
+ * @param size How many streams may wait at once; 0 or less makes room for 1.
294
+ */
270
295
  constructor(size: i32) {
271
296
  const n: i32 = size > 0 ? size : WT_ONE
272
297
  this.streams = new Array<i64>(n)
@@ -287,7 +312,7 @@ export class WebTransportWaiting {
287
312
  this.reset()
288
313
  }
289
314
 
290
- /** Empties the room. */
315
+ /** @internal Empties the room; called by `WebTransport` in this module, not for callers. */
291
316
  reset(): void {
292
317
  this.heads.fill(WT_NONE)
293
318
  this.tails.fill(WT_NONE)
@@ -300,13 +325,16 @@ export class WebTransportWaiting {
300
325
  }
301
326
 
302
327
  /** The bucket of session ID `session`: its stream number, salted, times an odd constant, high bits. */
303
- bucket(session: i64): i32 {
328
+ private bucket(session: i64): i32 {
304
329
  const x: i64 = ((session >> toI64(2)) ^ this.salt) & toI64(0x7fffffff)
305
330
  const mixed: i64 = (x * WT_HASH_MULTIPLIER) >> toI64(16)
306
331
  return toI32(mixed & toI64(toI32(this.heads.length) - 1))
307
332
  }
308
333
 
309
- /** Files stream `stream`, waiting for session `session`; answers whether there was room. */
334
+ /**
335
+ * Files stream `stream`, waiting for session `session`; answers whether there was room.
336
+ * @internal Called by `WebTransport` in this module; not for callers.
337
+ */
310
338
  add(stream: i64, session: i64): boolean {
311
339
  const e: i32 = this.freeHead
312
340
  if (e < 0 || e >= toI32(this.next.length)) {
@@ -329,7 +357,10 @@ export class WebTransportWaiting {
329
357
  return true
330
358
  }
331
359
 
332
- /** The oldest entry waiting for session `session`, or -1. */
360
+ /**
361
+ * The oldest entry waiting for session `session`, or -1.
362
+ * @internal Called by `WebTransport` in this module; not for callers.
363
+ */
333
364
  first(session: i64): i32 {
334
365
  let e: i32 = this.heads[this.bucket(session)]
335
366
  for (
@@ -345,7 +376,10 @@ export class WebTransportWaiting {
345
376
  return WT_NONE
346
377
  }
347
378
 
348
- /** Takes entry `e` out of the room and answers its stream. */
379
+ /**
380
+ * Takes entry `e` out of the room and answers its stream.
381
+ * @internal Called by `WebTransport` in this module; not for callers.
382
+ */
349
383
  take(e: i32): i64 {
350
384
  const stream: i64 = this.streams[e]
351
385
  const b: i32 = this.bucket(this.sessions[e])
@@ -374,87 +408,122 @@ export class WebTransportWaiting {
374
408
  * which.
375
409
  */
376
410
  export class WebTransport {
411
+ /** The caps this layer was made with; the arrays they size are made by the constructor. */
377
412
  config: WebTransportConfig
378
- h3: Http3Connection
379
- quic: QuicConnection
380
- /** The request fields of the last WT_SESSION: `h3.fields`. */
413
+ private h3: Http3Connection
414
+ private quic: QuicConnection
415
+ /** The request fields of the last WT_SESSION: the same object as the `Http3Connection`'s `fields`. */
381
416
  fields: HttpFields
382
- header: Http3FrameHeader
417
+ private header: Http3FrameHeader
383
418
  /** 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`. */
419
+ private datagram: u8[]
420
+ private outgoing: u8[]
421
+ private capsuleOut: u8[]
422
+ /**
423
+ * The buffer the window of WT_DATAGRAM, WT_STREAM_DATA and WT_CLOSED lies in: this layer's own
424
+ * datagram buffer, the `Http3Connection`'s `data`, or this layer's store of CLOSE reasons. Valid
425
+ * until the next call.
426
+ */
388
427
  data: u8[]
389
428
  /** Each session's CLOSE payload as it arrives, `WT_REASON_MAX + 4` bytes a session. */
390
- reason: u8[]
429
+ private reason: u8[]
391
430
  /** The response fields of an accepted session, and of a refusal. */
392
- acceptNames: u8[][]
393
- acceptValues: u8[][]
394
- none: u8[][]
431
+ private acceptNames: u8[][]
432
+ private acceptValues: u8[][]
433
+ private none: u8[][]
395
434
  // Per session.
435
+ /** Each session's ID, its CONNECT stream's ID, by session index; -1 for a free index. */
396
436
  sessionIds: i64[]
397
437
  /** 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[]
438
+ private capType: i64[]
439
+ private capLeft: i64[]
440
+ private states: i32[]
401
441
  /** The CONNECT stream's QUIC slot. */
402
- sessionSlot: i32[]
442
+ private sessionSlot: i32[]
403
443
  /** The first of its streams (a QUIC slot, threaded through `nextStream`), and how many. */
404
- firstStream: i32[]
444
+ private firstStream: i32[]
445
+ /**
446
+ * How many streams each session holds, the client's and this side's together, up to `maxStreams`;
447
+ * a stream stops counting once both of its sides are over.
448
+ */
405
449
  streamCount: i32[]
406
450
  /** Whether the client's CLOSE has been read: nothing may follow it. */
407
- closeRead: boolean[]
451
+ private closeRead: boolean[]
408
452
  /** 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[]
453
+ private capHeadLen: i32[]
454
+ private capHead: u8[]
455
+ private capFill: i32[]
412
456
  /** Counters for a log or a rate cap, each saturating: datagrams in and out. */
413
457
  datagramsIn: i32[]
458
+ /** How many datagrams each session sent, saturating at 2^31 - 1; zeroed when a session is asked for. */
414
459
  datagramsOut: i32[]
415
460
  /** The free sessions, a stack. */
416
- free: i32[]
461
+ private free: i32[]
417
462
  // Per QUIC stream slot.
418
463
  /** The session whose CONNECT stream the slot holds, or -1. */
419
464
  sessionAt: i32[]
420
465
  /** 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[]
466
+ private streamIds: i64[]
467
+ private streamSession: i32[]
468
+ private nextStream: i32[]
469
+ private prevStream: i32[]
470
+ private streamBits: i32[]
426
471
  /** The streams waiting for a session. */
427
472
  waiting: WebTransportWaiting
428
473
  /** Sessions accepted while streams waited for them, `adoptCount` of them: `next` takes those streams first. */
429
- adoptions: i32[]
474
+ private adoptions: i32[]
430
475
  /** The last event's session ID, stream, and codes. */
431
476
  sessionId: i64 = -1
477
+ /**
478
+ * The stream ID of the last event about a stream: the client's or this side's stream, or the
479
+ * session's CONNECT stream; a WT_DATAGRAM leaves it as it was.
480
+ */
432
481
  stream: i64 = -1
482
+ /**
483
+ * The HTTP/3 code of the last WT_STREAM_RESET or WT_STREAM_STOPPED, or the 32-bit code of the
484
+ * last WT_CLOSED's CLOSE capsule (0 when the session ended without one).
485
+ */
433
486
  errorCode: i64 = 0
487
+ /**
488
+ * The WebTransport code `errorCode` maps to (`wtCodeFromHttp3`), or -1 when it maps to none; for
489
+ * WT_CLOSED, `errorCode` itself.
490
+ */
434
491
  appCode: i64 = -1
435
492
  /** 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
493
+ private capAt: i32 = 0
494
+ private capEnd: i32 = 0
495
+ private capSession: i32 = -1
439
496
  /** The last event's session index, and its window onto `data`. */
440
497
  session: i32 = -1
498
+ /** Where the last event's window starts in `data`. */
441
499
  dataStart: i32 = 0
500
+ /** How many bytes the last event's window has. */
442
501
  dataLength: i32 = 0
502
+ /** How many session indexes are free; a valid request for a session while none is free is answered 429. */
443
503
  freeCount: i32 = 0
444
- adoptCount: i32 = 0
504
+ private adoptCount: i32 = 0
445
505
  /** The `h3.generation` this layer's state belongs to. */
446
- generation: i32 = 0
506
+ private generation: i32 = 0
447
507
  /** Counters for a log or a test, each saturating: datagrams dropped, sessions refused here, streams refused. */
448
508
  datagramsDropped: i32 = 0
509
+ /** How many requests for a session this layer answered with a 400, 429 or 501, or `refuse` refused. */
449
510
  sessionsRefused: i32 = 0
450
- streamsRefused: i32 = 0
511
+ private streamsRefused: i32 = 0
451
512
  /** WT_STREAM's kind, and whether WT_CLOSED came from the client. */
452
513
  bidirectional: boolean = false
514
+ /**
515
+ * Whether the last WT_CLOSED came from the client: its CLOSE capsule, its FIN, or a reset the
516
+ * client sent; false when this side ended the session, including when it reset the stream for a
517
+ * malformed capsule or a FIN inside one.
518
+ */
453
519
  closedByPeer: boolean = false
454
520
 
455
521
  /**
456
522
  * The WebTransport layer of `h3`, whose `Http3Config` must have
457
523
  * `webtransportSessions` above 0. A cap out of range panics.
524
+ * @param config The caps: `maxStreams` 1 to 4096 and `maxPending` 0 to 4096; kept, not copied.
525
+ * @param h3 The connection this layer reads and writes, kept for the layer's life; its restarts
526
+ * restart this layer too.
458
527
  */
459
528
  constructor(config: WebTransportConfig, h3: Http3Connection) {
460
529
  const sessions: i32 = h3.config.webtransportSessions
@@ -506,7 +575,7 @@ export class WebTransport {
506
575
  }
507
576
 
508
577
  /** Forgets every session and stream, for the connection's next peer; `next` calls it when `h3` restarted. */
509
- restart(): void {
578
+ private restart(): void {
510
579
  this.generation = this.h3.generation
511
580
  this.sessionIds.fill(WT_NONE64)
512
581
  this.states.fill(WT_FREE)
@@ -538,7 +607,7 @@ export class WebTransport {
538
607
  }
539
608
 
540
609
  /** Restarts this layer when its connection restarted since the last call. */
541
- current(): void {
610
+ private current(): void {
542
611
  if (this.generation !== this.h3.generation) {
543
612
  this.restart()
544
613
  }
@@ -547,7 +616,7 @@ export class WebTransport {
547
616
  // ---- Lookups ---------------------------------------------------------------------
548
617
 
549
618
  /** The index of the session whose CONNECT stream is `id`, or -1: one probe of QUIC's stream index. */
550
- sessionOf(id: i64): i32 {
619
+ private sessionOf(id: i64): i32 {
551
620
  const k: i32 = this.quic.streams.slotOf(id)
552
621
  if (k < 0 || k >= toI32(this.sessionAt.length)) {
553
622
  return WT_NONE
@@ -557,7 +626,7 @@ export class WebTransport {
557
626
  }
558
627
 
559
628
  /** The QUIC slot of a session's stream `id` that this layer tracks, or -1. */
560
- streamSlot(id: i64): i32 {
629
+ private streamSlot(id: i64): i32 {
561
630
  const k: i32 = this.quic.streams.slotOf(id)
562
631
  if (k < 0 || k >= toI32(this.streamIds.length) || this.streamIds[k] !== id) {
563
632
  return WT_NONE
@@ -566,7 +635,7 @@ export class WebTransport {
566
635
  }
567
636
 
568
637
  /** The session of `id` when it is accepted and not gone, else -1. */
569
- openSession(id: i64): i32 {
638
+ private openSession(id: i64): i32 {
570
639
  this.current()
571
640
  const s: i32 = this.sessionOf(id)
572
641
  return s >= 0 && this.states[s] === WT_OPEN ? s : WT_NONE
@@ -578,6 +647,9 @@ export class WebTransport {
578
647
  * Reads what the connection has and answers the next event (see the
579
648
  * module comment): a `WT_*` one, or a plain request's `H3_*` one. Call it
580
649
  * until it answers H3_NEED_MORE or H3_ERROR.
650
+ * @returns A `WT_*` event; an `Http3Connection` event passed through, for a plain request or
651
+ * H3_GOAWAY; H3_NEED_MORE when there is nothing more to read now; or H3_ERROR, when
652
+ * `Http3Connection.next` answers it.
581
653
  */
582
654
  next(): i32 {
583
655
  this.current()
@@ -622,7 +694,7 @@ export class WebTransport {
622
694
  * Reads one QUIC datagram, if one is waiting: answers WT_DATAGRAM for one
623
695
  * of an accepted session, 1 for one dropped, or 0 when none is there.
624
696
  */
625
- receiveDatagram(): i32 {
697
+ private receiveDatagram(): i32 {
626
698
  const n: i32 = this.quic.readDatagram(this.datagram, WT_ZERO, toI32(this.datagram.length))
627
699
  if (n === QUIC_DATAGRAM_NONE || n < 0) {
628
700
  return WT_ZERO
@@ -650,7 +722,7 @@ export class WebTransport {
650
722
  }
651
723
 
652
724
  /** What an `Http3Connection` event means here: a `WT_*` event, the same event passed through, or H3_NEED_MORE. */
653
- route(event: i32): i32 {
725
+ private route(event: i32): i32 {
654
726
  const h3: Http3Connection = this.h3
655
727
  const id: i64 = h3.stream
656
728
  if (event === H3_STREAM) {
@@ -710,13 +782,13 @@ export class WebTransport {
710
782
  }
711
783
 
712
784
  /** Sets `errorCode` to `code` and `appCode` to what it maps to. */
713
- codes(code: i64): void {
785
+ private codes(code: i64): void {
714
786
  this.errorCode = code
715
787
  this.appCode = wtCodeFromHttp3(code)
716
788
  }
717
789
 
718
790
  /** An event of session `s`'s CONNECT stream. */
719
- connectEvent(s: i32, event: i32): i32 {
791
+ private connectEvent(s: i32, event: i32): i32 {
720
792
  const h3: Http3Connection = this.h3
721
793
  this.session = s
722
794
  this.sessionId = this.sessionIds[s]
@@ -763,7 +835,7 @@ export class WebTransport {
763
835
  * no head for it to follow. Answers whether the FIN went: after a reset
764
836
  * the stream is dropped unread, so no event will end the session later.
765
837
  */
766
- finish(id: i64): boolean {
838
+ private finish(id: i64): boolean {
767
839
  if (this.h3.writeData(id, this.reason, WT_ZERO, WT_ZERO, true) >= 0) {
768
840
  return true
769
841
  }
@@ -772,7 +844,7 @@ export class WebTransport {
772
844
  }
773
845
 
774
846
  /** 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 {
847
+ private closedEvent(s: i32, code: i64, length: i32, byPeer: boolean): i32 {
776
848
  this.session = s
777
849
  this.errorCode = code & WT_U32
778
850
  this.appCode = this.errorCode
@@ -789,7 +861,7 @@ export class WebTransport {
789
861
  * An extended CONNECT arrived on `id`: a session when it is one this side
790
862
  * takes, answered here when not.
791
863
  */
792
- asked(id: i64): i32 {
864
+ private asked(id: i64): i32 {
793
865
  const h3: Http3Connection = this.h3
794
866
  const f: HttpFields = h3.fields
795
867
  const k: i32 = this.quic.streams.slotOf(id)
@@ -844,7 +916,7 @@ export class WebTransport {
844
916
  }
845
917
 
846
918
  /** Answers the request on `id` with `status` and stops reading it: a session refused. */
847
- answer(id: i64, status: i32): void {
919
+ private answer(id: i64, status: i32): void {
848
920
  this.sessionsRefused = h3Count(this.sessionsRefused)
849
921
  if (this.h3.respond(id, status, this.none, this.none, true) !== 0) {
850
922
  this.h3.reset(id, H3_REQUEST_REJECTED)
@@ -857,6 +929,11 @@ export class WebTransport {
857
929
  * streams reach the program from now on, the streams that waited for it
858
930
  * first. Answers 0, H3_AGAIN when the response does not fit yet, or
859
931
  * H3_CLOSED for one that is not a session asked for.
932
+ * @param id The session's ID, the `sessionId` of its WT_SESSION.
933
+ * @returns 0 once the 200 is written; H3_AGAIN while the stream has no room for it; H3_TOO_LARGE
934
+ * when the head is past the client's SETTINGS_MAX_FIELD_SECTION_SIZE or the stream's buffer; or
935
+ * H3_CLOSED for an ID that is not a session asked for and not yet answered, or a stream that can
936
+ * no longer be written.
860
937
  */
861
938
  accept(id: i64): i32 {
862
939
  this.current()
@@ -881,6 +958,10 @@ export class WebTransport {
881
958
  * Refuses session `id` with `status`, 400 to 599 (draft-02 §3.2): the
882
959
  * response ends the stream, and what the client still sends is dropped.
883
960
  * Answers 0, H3_INVALID for a status out of range, or H3_CLOSED.
961
+ * @param id The session's ID, the `sessionId` of its WT_SESSION.
962
+ * @param status The response status, 400 to 599.
963
+ * @returns 0 once the session is let go; H3_INVALID for a status outside 400 to 599; or H3_CLOSED
964
+ * for an ID that is not a session asked for and not yet answered.
884
965
  */
885
966
  refuse(id: i64, status: i32): i32 {
886
967
  this.current()
@@ -900,7 +981,7 @@ export class WebTransport {
900
981
  * Ends session `s` here: every stream reset with WT_SESSION_GONE and asked
901
982
  * to stop, the streams waiting for it refused, and its slot freed.
902
983
  */
903
- release(s: i32): void {
984
+ private release(s: i32): void {
904
985
  this.end(s)
905
986
  const k: i32 = this.sessionSlot[s]
906
987
  if (k >= 0 && k < toI32(this.sessionAt.length) && this.sessionAt[k] === s) {
@@ -918,7 +999,7 @@ export class WebTransport {
918
999
  }
919
1000
 
920
1001
  /** Resets every stream of session `s` and refuses those waiting for it; the session is gone. */
921
- end(s: i32): void {
1002
+ private end(s: i32): void {
922
1003
  if (this.states[s] === WT_FREE) {
923
1004
  return
924
1005
  }
@@ -937,7 +1018,7 @@ export class WebTransport {
937
1018
  }
938
1019
 
939
1020
  /** Refuses with WT_SESSION_GONE every stream waiting for session `id`. */
940
- dropPending(id: i64): void {
1021
+ private dropPending(id: i64): void {
941
1022
  if (this.waiting.count === 0) {
942
1023
  return
943
1024
  }
@@ -955,6 +1036,16 @@ export class WebTransport {
955
1036
  * with WT_SESSION_GONE. Answers 0; H3_INVALID for a reason too long;
956
1037
  * H3_AGAIN while the CONNECT stream has no room for the capsule, and
957
1038
  * WT_WRITABLE then names it; or H3_CLOSED.
1039
+ * @param id The session's ID.
1040
+ * @param code The application's code; only its low 32 bits are sent.
1041
+ * @param buf The buffer holding the reason; only read, and copied into the capsule. A window outside
1042
+ * it panics, whatever the session's state.
1043
+ * @param off Where the reason starts in `buf`.
1044
+ * @param len How many bytes the reason has, at most 1024.
1045
+ * @returns 0 once the capsule and the FIN are written; H3_INVALID for a reason past 1024 bytes;
1046
+ * H3_AGAIN while the CONNECT stream has no room for the capsule; H3_TOO_LARGE for a capsule past
1047
+ * the stream's buffer; or H3_CLOSED for a session not accepted or already gone, or a CONNECT
1048
+ * stream that can no longer be written.
958
1049
  */
959
1050
  close(id: i64, code: i64, buf: u8[], off: i32, len: i32): i32 {
960
1051
  h3CheckWindow("WebTransport.close", buf, off, len)
@@ -986,6 +1077,10 @@ export class WebTransport {
986
1077
  /**
987
1078
  * Sends DRAIN_WEBTRANSPORT_SESSION on session `id`: the client should
988
1079
  * finish and close it. Answers 0, H3_AGAIN, or H3_CLOSED.
1080
+ * @param id The session's ID.
1081
+ * @returns 0 once the capsule is written; H3_AGAIN while the CONNECT stream has no room for it; or
1082
+ * H3_CLOSED for a session not accepted or already gone, or a CONNECT stream that can no longer be
1083
+ * written.
989
1084
  */
990
1085
  drain(id: i64): i32 {
991
1086
  const s: i32 = this.openSession(id)
@@ -1005,7 +1100,7 @@ export class WebTransport {
1005
1100
  * session `capSession`'s CONNECT stream, and answers the first event they
1006
1101
  * make, or H3_NEED_MORE once the window is read.
1007
1102
  */
1008
- capsules(): i32 {
1103
+ private capsules(): i32 {
1009
1104
  const s: i32 = this.capSession
1010
1105
  const bytes: u8[] = this.h3.data
1011
1106
  const base: i32 = s * WT_HEAD
@@ -1061,7 +1156,7 @@ export class WebTransport {
1061
1156
  }
1062
1157
 
1063
1158
  /** A whole capsule of `type` on session `s`'s CONNECT stream. */
1064
- capsule(s: i32, type: i64): i32 {
1159
+ private capsule(s: i32, type: i64): i32 {
1065
1160
  this.session = s
1066
1161
  this.sessionId = this.sessionIds[s]
1067
1162
  this.stream = this.sessionIds[s]
@@ -1092,7 +1187,7 @@ export class WebTransport {
1092
1187
  }
1093
1188
 
1094
1189
  /** 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 {
1190
+ private malformed(s: i32, code: i64): i32 {
1096
1191
  const id: i64 = this.sessionIds[s]
1097
1192
  const wasGone: boolean = this.states[s] === WT_GONE
1098
1193
  this.capSession = -1
@@ -1107,7 +1202,7 @@ export class WebTransport {
1107
1202
  // ---- Streams ---------------------------------------------------------------------
1108
1203
 
1109
1204
  /** A stream named session `session` in its first bytes (H3_STREAM): taken, kept waiting or refused. */
1110
- arrived(id: i64, session: i64): i32 {
1205
+ private arrived(id: i64, session: i64): i32 {
1111
1206
  const s: i32 = this.sessionOf(session)
1112
1207
  if (s >= 0 && this.states[s] === WT_OPEN) {
1113
1208
  return this.adopt(s, id)
@@ -1129,7 +1224,7 @@ export class WebTransport {
1129
1224
  * with streams waiting: answers its WT_STREAM, or H3_NEED_MORE once that
1130
1225
  * session has none left (or is gone), when the next session's turn comes.
1131
1226
  */
1132
- adoptWaiting(): i32 {
1227
+ private adoptWaiting(): i32 {
1133
1228
  const s: i32 = this.adoptions[this.adoptCount - 1]
1134
1229
  if (s >= 0 && s < toI32(this.states.length) && this.states[s] === WT_OPEN) {
1135
1230
  const e: i32 = this.waiting.first(this.sessionIds[s])
@@ -1142,7 +1237,7 @@ export class WebTransport {
1142
1237
  }
1143
1238
 
1144
1239
  /** Refuses stream `id` with `code`, counted. */
1145
- refuseStream(id: i64, code: i64): void {
1240
+ private refuseStream(id: i64, code: i64): void {
1146
1241
  this.streamsRefused = h3Count(this.streamsRefused)
1147
1242
  this.h3.refuseStream(id, code)
1148
1243
  }
@@ -1152,7 +1247,7 @@ export class WebTransport {
1152
1247
  * holds `maxStreams` already: answers WT_STREAM, or H3_NEED_MORE for one
1153
1248
  * refused with H3_REQUEST_REJECTED.
1154
1249
  */
1155
- adopt(s: i32, id: i64): i32 {
1250
+ private adopt(s: i32, id: i64): i32 {
1156
1251
  if (this.streamCount[s] >= this.config.maxStreams) {
1157
1252
  this.refuseStream(id, H3_REQUEST_REJECTED)
1158
1253
  return H3_NEED_MORE
@@ -1169,7 +1264,7 @@ export class WebTransport {
1169
1264
  }
1170
1265
 
1171
1266
  /** 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 {
1267
+ private link(s: i32, id: i64, bits: i32): i32 {
1173
1268
  const k: i32 = this.quic.streams.slotOf(id)
1174
1269
  if (k < 0 || k >= toI32(this.streamIds.length)) {
1175
1270
  return WT_NONE
@@ -1193,7 +1288,7 @@ export class WebTransport {
1193
1288
  }
1194
1289
 
1195
1290
  /** 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 {
1291
+ private settle(k: i32, bits: i32): void {
1197
1292
  this.streamBits[k] = this.streamBits[k] | bits
1198
1293
  if ((this.streamBits[k] & (WT_SENT | WT_RECEIVED)) === (WT_SENT | WT_RECEIVED)) {
1199
1294
  this.unlink(k)
@@ -1201,7 +1296,7 @@ export class WebTransport {
1201
1296
  }
1202
1297
 
1203
1298
  /** Takes the stream in slot `k` off its session's list. */
1204
- unlink(k: i32): void {
1299
+ private unlink(k: i32): void {
1205
1300
  const s: i32 = this.streamSession[k]
1206
1301
  const before: i32 = this.prevStream[k]
1207
1302
  const after: i32 = this.nextStream[k]
@@ -1224,6 +1319,12 @@ export class WebTransport {
1224
1319
  * Opens a stream of this side's for session `id` (draft-02 §4.1, §4.2).
1225
1320
  * Answers its ID; WT_LIMIT when the session holds `maxStreams`; H3_AGAIN
1226
1321
  * while the client's MAX_STREAMS allows no more; or H3_CLOSED.
1322
+ * @param id The session's ID.
1323
+ * @param bidirectional Whether the stream is bidirectional; a unidirectional one has no side for
1324
+ * the client to write.
1325
+ * @returns The new stream's ID; WT_LIMIT when the session holds `maxStreams`; H3_AGAIN while the
1326
+ * client's MAX_STREAMS allows no more; or H3_CLOSED for a session not accepted, or one already
1327
+ * gone.
1227
1328
  */
1228
1329
  openStream(id: i64, bidirectional: boolean): i64 {
1229
1330
  const s: i32 = this.openSession(id)
@@ -1245,6 +1346,14 @@ export class WebTransport {
1245
1346
  * Writes up to `len` bytes of `buf` from `off` to session stream `id`, with
1246
1347
  * its FIN when `fin` and all were taken. Answers how many it took, or
1247
1348
  * H3_AGAIN, or H3_CLOSED (see `Http3Connection.writeStream`).
1349
+ * @param id The stream's ID, one of a session this layer took or opened.
1350
+ * @param buf The buffer holding the bytes; only read, and what is taken is copied.
1351
+ * @param off Where the bytes start in `buf`.
1352
+ * @param len How many bytes to write.
1353
+ * @param fin Whether to end this side of the stream once every byte is taken.
1354
+ * @returns How many bytes it took, fewer than `len` only when the send buffer filled (0 for a bare
1355
+ * FIN); H3_AGAIN when it took none; or H3_CLOSED for a stream this layer does not hold or with no
1356
+ * side here to write.
1248
1357
  */
1249
1358
  write(id: i64, buf: u8[], off: i32, len: i32, fin: boolean): i32 {
1250
1359
  this.current()
@@ -1259,7 +1368,12 @@ export class WebTransport {
1259
1368
  return n
1260
1369
  }
1261
1370
 
1262
- /** Resets this side of session stream `id` with the application's `code` (mapped, `wtCodeToHttp3`). Answers 0 or H3_CLOSED. */
1371
+ /**
1372
+ * Resets this side of session stream `id` with the application's `code` (mapped, `wtCodeToHttp3`).
1373
+ * @param id The stream's ID, one of a session this layer took or opened.
1374
+ * @param code The application's code; only its low 32 bits are sent, mapped.
1375
+ * @returns 0 once the reset is sent, or H3_CLOSED for a stream this layer does not hold.
1376
+ */
1263
1377
  resetStream(id: i64, code: i64): i32 {
1264
1378
  this.current()
1265
1379
  const k: i32 = this.streamSlot(id)
@@ -1274,7 +1388,14 @@ export class WebTransport {
1274
1388
  return 0
1275
1389
  }
1276
1390
 
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. */
1391
+ /**
1392
+ * Asks the client to stop its side of session stream `id` with the application's `code`, and drops
1393
+ * what it sends.
1394
+ * @param id The stream's ID, one of a session this layer took or opened.
1395
+ * @param code The application's code; only its low 32 bits are sent, mapped (`wtCodeToHttp3`).
1396
+ * @returns 0 once STOP_SENDING is sent, or H3_CLOSED for a stream this layer does not hold or
1397
+ * whose client side is already over.
1398
+ */
1278
1399
  stopSending(id: i64, code: i64): i32 {
1279
1400
  this.current()
1280
1401
  const k: i32 = this.streamSlot(id)
@@ -1295,6 +1416,8 @@ export class WebTransport {
1295
1416
  * The largest payload `sendDatagram` takes for session `id` now: what the
1296
1417
  * QUIC connection's `maxDatagramPayload` leaves after the quarter stream
1297
1418
  * ID; 0 for a session not accepted.
1419
+ * @param id The session's ID.
1420
+ * @returns The most payload bytes one datagram may carry now, 0 or more.
1298
1421
  */
1299
1422
  maxDatagramPayload(id: i64): i32 {
1300
1423
  if (this.openSession(id) < 0) {
@@ -1310,6 +1433,14 @@ export class WebTransport {
1310
1433
  * frame, never sent again if lost. Answers 0; H3_TOO_LARGE past
1311
1434
  * `maxDatagramPayload`; H3_AGAIN while QUIC's queue is full; or H3_CLOSED
1312
1435
  * for a session not accepted.
1436
+ * @param id The session's ID.
1437
+ * @param buf The buffer holding the payload; only read, and copied into the datagram. A window
1438
+ * outside it panics, whatever the session's state.
1439
+ * @param off Where the payload starts in `buf`.
1440
+ * @param len How many bytes the payload has.
1441
+ * @returns 0 once the datagram is queued; H3_AGAIN while QUIC's queue is full; H3_TOO_LARGE past
1442
+ * `maxDatagramPayload`, or when QUIC refuses it for any other reason; or H3_CLOSED for a session
1443
+ * not accepted, or one already gone.
1313
1444
  */
1314
1445
  sendDatagram(id: i64, buf: u8[], off: i32, len: i32): i32 {
1315
1446
  h3CheckWindow("WebTransport.sendDatagram", buf, off, len)