@amritk/nish-aarch64-linux 0.42.0 → 0.45.1

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 (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
@@ -169,7 +169,8 @@ import {
169
169
  Http3Connection,
170
170
  } from "nish/net/http3"
171
171
  import { QUIC_DATAGRAM_ERR_FULL, QUIC_DATAGRAM_NONE, QUIC_DATAGRAM_OK, QuicConnection } from "nish/net/quic"
172
- import { quicPacketCopy, quicVarintSize } from "nish/net/quic-packet"
172
+ import { tryCopyBytes } from "nish/bytes"
173
+ import { quicVarintSize } from "nish/net/quic-packet"
173
174
  import { quicStreamIsUni } from "nish/net/quic-stream"
174
175
  import { monotonicNanos } from "nish:process"
175
176
 
@@ -251,8 +252,13 @@ const WT_ONE: i32 = 1
251
252
  const WT_NONE: i32 = -1
252
253
  const WT_NONE64: i64 = -1
253
254
  const WT_U32: i64 = 0xffffffff
254
- /** Knuth's multiplicative constant, 2^32 over the golden ratio: odd, so the product keeps every bit of the salted number. */
255
- const WT_HASH_MULTIPLIER: i64 = 2654435761
255
+ /**
256
+ * The golden ratio's fractional part: the fraction of a salted session
257
+ * number times it, scaled to the bucket count, is its bucket (Knuth's
258
+ * multiplicative hashing, worked in `f64`, which holds the 31-bit number's
259
+ * product exactly enough for the 14 bits a bucket takes).
260
+ */
261
+ const WT_GOLDEN: f64 = 0.6180339887498949
256
262
 
257
263
  /** The caps of a WebTransport layer, fixed when it is made. */
258
264
  export class WebTransportConfig {
@@ -285,8 +291,9 @@ export class WebTransportConfig {
285
291
  * value.
286
292
  */
287
293
  export const wtCodeToHttp3 = (code: i64): i64 => {
288
- const n: i64 = code & WT_U32
289
- return WT_FIRST_APP_CODE + n + n / toI64(0x1e)
294
+ // Worked in `f64`, which holds every value here exactly: the largest is under 2^47.
295
+ const n: f64 = toF64(code & WT_U32)
296
+ return toI64(toF64(WT_FIRST_APP_CODE) + n + Math.floor(n / 30.0))
290
297
  }
291
298
 
292
299
  /**
@@ -301,8 +308,9 @@ export const wtCodeFromHttp3 = (code: i64): i64 => {
301
308
  if (code < WT_FIRST_APP_CODE || code > last || h3Greased(code)) {
302
309
  return WT_NONE64
303
310
  }
304
- const shifted: i64 = code - WT_FIRST_APP_CODE
305
- return shifted - shifted / toI64(0x1f)
311
+ // `code` lies in the range checked above, under 2^47, so `f64` holds every value exactly.
312
+ const shifted: f64 = toF64(code) - toF64(WT_FIRST_APP_CODE)
313
+ return toI64(shifted - Math.floor(shifted / 31.0))
306
314
  }
307
315
 
308
316
  /** Panics unless `value` lies in `[low, high]`: a cap out of range is the program's mistake. */
@@ -312,6 +320,76 @@ const webtransportCheckCap = (what: string, value: i32, low: i32, high: i32): vo
312
320
  }
313
321
  }
314
322
 
323
+ /**
324
+ * `a + b`, worked in `i64` and held to `i32`. Every caller adds an offset
325
+ * and a length, or a count and one, that lie inside one array or one cap,
326
+ * so the sum is an index into it or just past it and always fits.
327
+ */
328
+ const wtAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
329
+
330
+ /** `a - b`, worked as `wtAdd` is, for offsets and lengths inside one array. */
331
+ const wtSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
332
+
333
+ /**
334
+ * A zeroed array of `n` bytes, or of none when `n` is below zero. Up to
335
+ * 65535 bytes is one allocation; past that the array grows a byte at a time
336
+ * from a 65535-byte start, which an `i32` length cannot make fail.
337
+ */
338
+ const wtBytes = (n: i32): u8[] => {
339
+ if (n < 0) {
340
+ return []
341
+ }
342
+ if (n <= 65535) {
343
+ return new Array<u8>(toU16(n))
344
+ }
345
+ const out: u8[] = new Array<u8>(65535)
346
+ for (let k: i32 = 65535; k < n; k += 1) {
347
+ out.push(0)
348
+ }
349
+ return out
350
+ }
351
+
352
+ /** A zeroed array of `n` `i32`s, made as `wtBytes` makes one. */
353
+ const wtI32s = (n: i32): i32[] => {
354
+ if (n < 0) {
355
+ return []
356
+ }
357
+ if (n <= 65535) {
358
+ return new Array<i32>(toU16(n))
359
+ }
360
+ const out: i32[] = new Array<i32>(65535)
361
+ for (let k: i32 = 65535; k < n; k += 1) {
362
+ out.push(0)
363
+ }
364
+ return out
365
+ }
366
+
367
+ /** A zeroed array of `n` `i64`s, made as `wtBytes` makes one. */
368
+ const wtI64s = (n: i32): i64[] => {
369
+ if (n < 0) {
370
+ return []
371
+ }
372
+ if (n <= 65535) {
373
+ return new Array<i64>(toU16(n))
374
+ }
375
+ const out: i64[] = new Array<i64>(65535)
376
+ for (let k: i32 = 65535; k < n; k += 1) {
377
+ out.push(0)
378
+ }
379
+ return out
380
+ }
381
+
382
+ /** The bucket count for `n` waiting streams: the least power of two at least `2 * n`, and at least 2. */
383
+ const wtBuckets = (n: i32): i32 => {
384
+ for (let shift: i32 = 0; shift < 30; shift++) {
385
+ const size: i32 = 1 << shift
386
+ if (size >= n) {
387
+ return 2 << shift
388
+ }
389
+ }
390
+ return 1073741824
391
+ }
392
+
315
393
  /**
316
394
  * The streams waiting for a session (draft-02 §4.5): at most `size`, filed
317
395
  * by the session ID they named in a hash keyed with bytes of entropy, each
@@ -340,17 +418,14 @@ export class WebTransportWaiting {
340
418
  */
341
419
  constructor(size: i32) {
342
420
  const n: i32 = size > 0 ? size : WT_ONE
343
- this.streams = new Array<i64>(n)
344
- this.sessions = new Array<i64>(n)
345
- this.next = new Array<i32>(n)
346
- this.prev = new Array<i32>(n)
347
- let buckets: i32 = 2
348
- while (buckets < n * 2) {
349
- buckets = buckets * 2
350
- }
351
- this.heads = new Array<i32>(buckets)
352
- this.tails = new Array<i32>(buckets)
353
- const entropy: u8[] = new Array<u8>(4)
421
+ this.streams = wtI64s(n)
422
+ this.sessions = wtI64s(n)
423
+ this.next = wtI32s(n)
424
+ this.prev = wtI32s(n)
425
+ const buckets: i32 = wtBuckets(n)
426
+ this.heads = wtI32s(buckets)
427
+ this.tails = wtI32s(buckets)
428
+ const entropy: u8[] = wtBytes(4)
354
429
  crypto.getRandomValues(entropy)
355
430
  for (let j: i32 = 0; j < toI32(entropy.length); j++) {
356
431
  this.salt = (this.salt << toI64(8)) | toI64(toI32(entropy[j]))
@@ -370,11 +445,11 @@ export class WebTransportWaiting {
370
445
  this.count = 0
371
446
  }
372
447
 
373
- /** The bucket of session ID `session`: its stream number, salted, times an odd constant, high bits. */
448
+ /** The bucket of session ID `session`: its stream number, salted, hashed by `WT_GOLDEN`. */
374
449
  private bucket(session: i64): i32 {
375
450
  const x: i64 = ((session >> toI64(2)) ^ this.salt) & toI64(0x7fffffff)
376
- const mixed: i64 = (x * WT_HASH_MULTIPLIER) >> toI64(16)
377
- return toI32(mixed & toI64(toI32(this.heads.length) - 1))
451
+ const scaled: f64 = toF64(x) * WT_GOLDEN
452
+ return toI32((scaled - Math.floor(scaled)) * toF64(toI32(this.heads.length)))
378
453
  }
379
454
 
380
455
  /**
@@ -382,24 +457,43 @@ export class WebTransportWaiting {
382
457
  * @internal Called by `WebTransport` in this module; not for callers.
383
458
  */
384
459
  add(stream: i64, session: i64): boolean {
460
+ const next: i32[] = this.next
461
+ const prev: i32[] = this.prev
462
+ const heads: i32[] = this.heads
463
+ const tails: i32[] = this.tails
464
+ const streams: i64[] = this.streams
465
+ const sessions: i64[] = this.sessions
385
466
  const e: i32 = this.freeHead
386
- if (e < 0 || e >= toI32(this.next.length)) {
467
+ const b: i32 = this.bucket(session)
468
+ // Every entry array has the room's size and both bucket arrays the bucket count, so only a full room fails here.
469
+ if (
470
+ e < 0 ||
471
+ e >= toI32(next.length) ||
472
+ e >= toI32(prev.length) ||
473
+ e >= toI32(streams.length) ||
474
+ e >= toI32(sessions.length) ||
475
+ b < 0 ||
476
+ b >= toI32(heads.length) ||
477
+ b >= toI32(tails.length)
478
+ ) {
387
479
  return false
388
480
  }
389
- this.freeHead = this.next[e]
390
- const b: i32 = this.bucket(session)
391
- const tail: i32 = this.tails[b]
392
- this.streams[e] = stream
393
- this.sessions[e] = session
394
- this.next[e] = -1
395
- this.prev[e] = tail
396
- if (tail >= 0 && tail < toI32(this.next.length)) {
397
- this.next[tail] = e
481
+ this.freeHead = next[e]
482
+ const tail: i32 = tails[b]
483
+ streams[e] = stream
484
+ sessions[e] = session
485
+ next[e] = -1
486
+ prev[e] = tail
487
+ if (tail >= 0 && tail < toI32(next.length)) {
488
+ next[tail] = e
398
489
  } else {
399
- this.heads[b] = e
490
+ heads[b] = e
491
+ }
492
+ tails[b] = e
493
+ const count: i32 = this.count
494
+ if (count >= 0 && count < toI32(next.length)) {
495
+ this.count = count + 1
400
496
  }
401
- this.tails[b] = e
402
- this.count = this.count + 1
403
497
  return true
404
498
  }
405
499
 
@@ -408,46 +502,139 @@ export class WebTransportWaiting {
408
502
  * @internal Called by `WebTransport` in this module; not for callers.
409
503
  */
410
504
  first(session: i64): i32 {
411
- let e: i32 = this.heads[this.bucket(session)]
505
+ const heads: i32[] = this.heads
506
+ const next: i32[] = this.next
507
+ const sessions: i64[] = this.sessions
508
+ const b: i32 = this.bucket(session)
509
+ if (b < 0 || b >= toI32(heads.length)) {
510
+ return WT_NONE
511
+ }
512
+ let e: i32 = heads[b]
412
513
  for (
413
514
  let guard: i32 = 0;
414
- guard < toI32(this.next.length) && e >= 0 && e < toI32(this.next.length);
515
+ guard < toI32(next.length) && e >= 0 && e < toI32(next.length) && e < toI32(sessions.length);
415
516
  guard++
416
517
  ) {
417
- if (this.sessions[e] === session) {
518
+ if (sessions[e] === session) {
418
519
  return e
419
520
  }
420
- e = this.next[e]
521
+ e = next[e]
421
522
  }
422
523
  return WT_NONE
423
524
  }
424
525
 
425
526
  /**
426
- * Takes entry `e` out of the room and answers its stream.
527
+ * Takes entry `e`, one `first` answered, out of the room and answers its
528
+ * stream, or -1 for an `e` that is not an entry of the room.
427
529
  * @internal Called by `WebTransport` in this module; not for callers.
428
530
  */
429
531
  take(e: i32): i64 {
430
- const stream: i64 = this.streams[e]
431
- const b: i32 = this.bucket(this.sessions[e])
432
- const before: i32 = this.prev[e]
433
- const after: i32 = this.next[e]
434
- if (before >= 0 && before < toI32(this.next.length)) {
435
- this.next[before] = after
532
+ const next: i32[] = this.next
533
+ const prev: i32[] = this.prev
534
+ const heads: i32[] = this.heads
535
+ const tails: i32[] = this.tails
536
+ const streams: i64[] = this.streams
537
+ const sessions: i64[] = this.sessions
538
+ if (
539
+ e < 0 ||
540
+ e >= toI32(next.length) ||
541
+ e >= toI32(prev.length) ||
542
+ e >= toI32(streams.length) ||
543
+ e >= toI32(sessions.length)
544
+ ) {
545
+ return WT_NONE64
546
+ }
547
+ const b: i32 = this.bucket(sessions[e])
548
+ if (b < 0 || b >= toI32(heads.length) || b >= toI32(tails.length)) {
549
+ return WT_NONE64
550
+ }
551
+ const stream: i64 = streams[e]
552
+ const before: i32 = prev[e]
553
+ const after: i32 = next[e]
554
+ if (before >= 0 && before < toI32(next.length)) {
555
+ next[before] = after
436
556
  } else {
437
- this.heads[b] = after
557
+ heads[b] = after
438
558
  }
439
- if (after >= 0 && after < toI32(this.prev.length)) {
440
- this.prev[after] = before
559
+ if (after >= 0 && after < toI32(prev.length)) {
560
+ prev[after] = before
441
561
  } else {
442
- this.tails[b] = before
562
+ tails[b] = before
443
563
  }
444
- this.next[e] = this.freeHead
564
+ next[e] = this.freeHead
445
565
  this.freeHead = e
446
- this.count = this.count - 1
566
+ const count: i32 = this.count
567
+ if (count > 0) {
568
+ this.count = count - 1
569
+ }
447
570
  return stream
448
571
  }
449
572
  }
450
573
 
574
+ /** One session index's state, kept by `WebTransport` in its table. */
575
+ class WebTransportSession {
576
+ /** The session's ID, its CONNECT stream's ID; -1 while free. */
577
+ id: i64 = -1
578
+ /** The type of the capsule being read. */
579
+ capType: i64 = -1
580
+ /** What is left of the capsule's payload, or -1 between capsules. */
581
+ capLeft: i64 = -1
582
+ /** A capsule header being read, which may be split across DATA frames. */
583
+ capHead: u8[]
584
+ /** When it was last active (`monotonicNanos`). */
585
+ lastActive: i64 = 0
586
+ /** When its datagram second began (`monotonicNanos`). */
587
+ rateSince: i64 = 0
588
+ /** Its CLOSE payload as it arrives: the 32-bit code, then the reason, `WT_REASON_MAX + 4` bytes. */
589
+ reason: u8[]
590
+ /** Its index in the table, the `session` of its events. */
591
+ index: i32 = 0
592
+ /** WT_FREE, WT_ASKED, WT_OPEN or WT_GONE. */
593
+ state: i32 = 0
594
+ /** The CONNECT stream's QUIC slot. */
595
+ slot: i32 = -1
596
+ /** The first of its streams, a QUIC slot threaded through each stream's `next`, or -1. */
597
+ firstStream: i32 = -1
598
+ /** How many streams it holds, up to `maxStreams`. */
599
+ streams: i32 = 0
600
+ /** How many bytes of a capsule header `capHead` holds. */
601
+ capHeadLen: i32 = 0
602
+ /** How much of `reason` the CLOSE payload has filled. */
603
+ capFill: i32 = 0
604
+ /** Datagrams in, saturating. */
605
+ datagramsIn: i32 = 0
606
+ /** Datagrams out, saturating. */
607
+ datagramsOut: i32 = 0
608
+ /** How many datagrams its second has taken. */
609
+ rateCount: i32 = 0
610
+ /** Whether the client's CLOSE has been read: nothing may follow it. */
611
+ closeRead: boolean = false
612
+
613
+ /**
614
+ * A free session.
615
+ * @param index Its index in the table.
616
+ */
617
+ constructor(index: i32) {
618
+ this.index = index
619
+ this.capHead = wtBytes(WT_HEAD)
620
+ this.reason = wtBytes(WT_REASON_STRIDE)
621
+ }
622
+ }
623
+
624
+ /** One QUIC stream slot's state, kept by `WebTransport` for a session's stream. */
625
+ class WebTransportStream {
626
+ /** The stream this slot's state is for, or -1. */
627
+ id: i64 = -1
628
+ /** Its session's index, or -1. */
629
+ session: i32 = -1
630
+ /** The next stream in its session's list, a QUIC slot, or -1. */
631
+ next: i32 = -1
632
+ /** The stream before it in its session's list, a QUIC slot, or -1. */
633
+ prev: i32 = -1
634
+ /** WT_SENT and WT_RECEIVED, once each side is done. */
635
+ bits: i32 = 0
636
+ }
637
+
451
638
  /**
452
639
  * The WebTransport sessions of one HTTP/3 connection. The fields after the
453
640
  * tables are the last event's; the module comment says which event sets
@@ -467,46 +654,30 @@ export class WebTransport {
467
654
  private capsuleOut: u8[]
468
655
  /**
469
656
  * The buffer the window of WT_DATAGRAM, WT_STREAM_DATA and WT_CLOSED lies in: this layer's own
470
- * datagram buffer, the `Http3Connection`'s `data`, or this layer's store of CLOSE reasons. Valid
471
- * until the next call.
657
+ * datagram buffer, the `Http3Connection`'s `data`, or the session's own store of its CLOSE
658
+ * reason. Valid until the next call.
472
659
  */
473
660
  data: u8[]
474
- /** Each session's CLOSE payload as it arrives, `WT_REASON_MAX + 4` bytes a session. */
475
- private reason: u8[]
476
661
  /** The response fields of an accepted session, and of a refusal. */
477
662
  private acceptNames: u8[][]
478
663
  private acceptValues: u8[][]
479
664
  private none: u8[][]
480
- // Per session.
481
- /** Each session's ID, its CONNECT stream's ID, by session index; -1 for a free index. */
665
+ // Per session. The four public arrays below are a read-only view of each
666
+ // session's own state, copied after every event that changes it: a write to
667
+ // one is overwritten at that session's next event.
668
+ /** Each session's state, by session index. */
669
+ private table: WebTransportSession[]
670
+ /** Each session's ID, its CONNECT stream's ID, by session index; -1 for a free index. Read-only: copied from the session at each event. */
482
671
  sessionIds: i64[]
483
- /** The capsule being read: its type, and what is left of its payload (-1 between capsules). */
484
- private capType: i64[]
485
- private capLeft: i64[]
486
- private states: i32[]
487
- /** The CONNECT stream's QUIC slot. */
488
- private sessionSlot: i32[]
489
- /** The first of its streams (a QUIC slot, threaded through `nextStream`), and how many. */
490
- private firstStream: i32[]
491
672
  /**
492
673
  * How many streams each session holds, the client's and this side's together, up to `maxStreams`;
493
- * a stream stops counting once both of its sides are over.
674
+ * a stream stops counting once both of its sides are over. Read-only: copied from the session at each event.
494
675
  */
495
676
  streamCount: i32[]
496
- /** Whether the client's CLOSE has been read: nothing may follow it. */
497
- private closeRead: boolean[]
498
- /** A capsule header being read, `capHeadLen[s]` bytes of `capHead[s * 16 ..]`, and the CLOSE payload's fill. */
499
- private capHeadLen: i32[]
500
- private capHead: u8[]
501
- private capFill: i32[]
502
- /** Counters for a log or a rate cap, each saturating: datagrams in and out. */
677
+ /** Counters for a log or a rate cap, each saturating: datagrams in and out. Read-only: copied from the session at each event. */
503
678
  datagramsIn: i32[]
504
- /** How many datagrams each session sent, saturating at 2^31 - 1; zeroed when a session is asked for. */
679
+ /** How many datagrams each session sent, saturating at 2^31 - 1; zeroed when a session is asked for. Read-only: copied from the session at each event. */
505
680
  datagramsOut: i32[]
506
- /** When each session was last active, and when its datagram second began and how many it took (`monotonicNanos`). */
507
- private lastActive: i64[]
508
- private rateSince: i64[]
509
- private rateCount: i32[]
510
681
  /** The free sessions, a stack. */
511
682
  private free: i32[]
512
683
  /** Sessions `expire` ended that `next` has not yet reported, `expiredCount` of them, with their IDs. */
@@ -518,12 +689,8 @@ export class WebTransport {
518
689
  // Per QUIC stream slot.
519
690
  /** The session whose CONNECT stream the slot holds, or -1. */
520
691
  sessionAt: i32[]
521
- /** The stream the slot's stream state is for, its session, its neighbours in that session's list, and its bits. */
522
- private streamIds: i64[]
523
- private streamSession: i32[]
524
- private nextStream: i32[]
525
- private prevStream: i32[]
526
- private streamBits: i32[]
692
+ /** The state of the session stream each slot holds. */
693
+ private slots: WebTransportStream[]
527
694
  /** The streams waiting for a session. */
528
695
  waiting: WebTransportWaiting
529
696
  /** Sessions accepted while streams waited for them, `adoptCount` of them: `next` takes those streams first. */
@@ -600,60 +767,65 @@ export class WebTransport {
600
767
  this.fields = h3.fields
601
768
  this.header = new Http3FrameHeader()
602
769
  const entry: i32 = h3.quic.datagramsIn.entrySize
603
- this.datagram = new Array<u8>(entry > 0 ? entry : WT_ONE)
604
- this.outgoing = new Array<u8>(h3.quic.datagramsOut.entrySize + 8)
605
- this.capsuleOut = new Array<u8>(WT_REASON_MAX + 4 + WT_HEAD)
770
+ this.datagram = wtBytes(entry > 0 ? entry : WT_ONE)
771
+ this.outgoing = wtBytes(wtAdd(h3.quic.datagramsOut.entrySize, 8))
772
+ this.capsuleOut = wtBytes(WT_REASON_MAX + 4 + WT_HEAD)
606
773
  this.data = this.datagram
607
- this.reason = new Array<u8>(sessions * WT_REASON_STRIDE)
608
774
  this.acceptNames = [httpFieldBytes("sec-webtransport-http3-draft")]
609
775
  this.acceptValues = [httpFieldBytes("draft02")]
610
776
  this.none = []
611
- this.sessionIds = new Array<i64>(sessions)
612
- this.capType = new Array<i64>(sessions)
613
- this.capLeft = new Array<i64>(sessions)
614
- this.states = new Array<i32>(sessions)
615
- this.sessionSlot = new Array<i32>(sessions)
616
- this.firstStream = new Array<i32>(sessions)
617
- this.streamCount = new Array<i32>(sessions)
618
- this.closeRead = new Array<boolean>(sessions)
619
- this.capHeadLen = new Array<i32>(sessions)
620
- this.capHead = new Array<u8>(sessions * WT_HEAD)
621
- this.capFill = new Array<i32>(sessions)
622
- this.datagramsIn = new Array<i32>(sessions)
623
- this.datagramsOut = new Array<i32>(sessions)
624
- this.lastActive = new Array<i64>(sessions)
625
- this.rateSince = new Array<i64>(sessions)
626
- this.rateCount = new Array<i32>(sessions)
627
- this.free = new Array<i32>(sessions)
628
- this.expiredSessions = new Array<i32>(sessions)
629
- this.expiredIds = new Array<i64>(sessions)
777
+ const table: WebTransportSession[] = []
778
+ for (let s: i32 = 0; s < sessions; s++) {
779
+ table.push(new WebTransportSession(s))
780
+ }
781
+ this.table = table
782
+ this.sessionIds = wtI64s(sessions)
783
+ this.streamCount = wtI32s(sessions)
784
+ this.datagramsIn = wtI32s(sessions)
785
+ this.datagramsOut = wtI32s(sessions)
786
+ this.free = wtI32s(sessions)
787
+ this.expiredSessions = wtI32s(sessions)
788
+ this.expiredIds = wtI64s(sessions)
630
789
  this.now = monotonicNanos()
631
790
  const n: i32 = toI32(h3.quic.streams.slots.length)
632
- const slots: i32 = n > 0 ? n : WT_ONE
633
- this.sessionAt = new Array<i32>(slots)
634
- this.streamIds = new Array<i64>(slots)
635
- this.streamSession = new Array<i32>(slots)
636
- this.nextStream = new Array<i32>(slots)
637
- this.prevStream = new Array<i32>(slots)
638
- this.streamBits = new Array<i32>(slots)
791
+ const count: i32 = n > 0 ? n : WT_ONE
792
+ this.sessionAt = wtI32s(count)
793
+ const slots: WebTransportStream[] = []
794
+ for (let k: i32 = 0; k < count; k++) {
795
+ slots.push(new WebTransportStream())
796
+ }
797
+ this.slots = slots
639
798
  this.waiting = new WebTransportWaiting(config.maxPending)
640
- this.adoptions = new Array<i32>(sessions)
799
+ this.adoptions = wtI32s(sessions)
641
800
  this.restart()
642
801
  }
643
802
 
644
803
  /** Forgets every session and stream, for the connection's next peer; `next` calls it when `h3` restarted. */
645
804
  private restart(): void {
646
805
  this.generation = this.h3.generation
647
- this.sessionIds.fill(WT_NONE64)
648
- this.states.fill(WT_FREE)
806
+ const table: WebTransportSession[] = this.table
807
+ for (let s: i32 = 0; s < toI32(table.length); s++) {
808
+ const w: WebTransportSession = table[s]
809
+ w.state = WT_FREE
810
+ w.id = -1
811
+ this.publish(w)
812
+ }
649
813
  this.sessionAt.fill(WT_NONE)
650
- this.streamIds.fill(WT_NONE64)
651
- this.streamSession.fill(WT_NONE)
652
- this.freeCount = 0
653
- for (let s: i32 = toI32(this.free.length) - 1; s >= 0; s--) {
654
- this.free[this.freeCount] = s
655
- this.freeCount = this.freeCount + 1
814
+ const slots: WebTransportStream[] = this.slots
815
+ for (let k: i32 = 0; k < toI32(slots.length); k++) {
816
+ const t: WebTransportStream = slots[k]
817
+ t.id = -1
818
+ t.session = -1
819
+ }
820
+ const free: i32[] = this.free
821
+ let count: i32 = 0
822
+ for (let s: i32 = toI32(free.length) - 1; s >= 0; s--) {
823
+ if (count >= 0 && count < toI32(free.length)) {
824
+ free[count] = s
825
+ count = count + 1
826
+ }
656
827
  }
828
+ this.freeCount = count
657
829
  this.waiting.reset()
658
830
  this.adoptCount = 0
659
831
  this.expiredCount = 0
@@ -684,30 +856,76 @@ export class WebTransport {
684
856
 
685
857
  // ---- Lookups ---------------------------------------------------------------------
686
858
 
687
- /** The index of the session whose CONNECT stream is `id`, or -1: one probe of QUIC's stream index. */
688
- private sessionOf(id: i64): i32 {
689
- const k: i32 = this.quic.streams.slotOf(id)
690
- if (k < 0 || k >= toI32(this.sessionAt.length)) {
691
- return WT_NONE
859
+ /** Session index `s`, or null for one out of range. */
860
+ private at(s: i32): WebTransportSession | null {
861
+ const table: WebTransportSession[] = this.table
862
+ if (s >= 0 && s < toI32(table.length)) {
863
+ return table[s]
692
864
  }
693
- const s: i32 = this.sessionAt[k]
694
- return s >= 0 && s < toI32(this.sessionIds.length) && this.sessionIds[s] === id ? s : WT_NONE
865
+ return null
695
866
  }
696
867
 
697
- /** The QUIC slot of a session's stream `id` that this layer tracks, or -1. */
698
- private streamSlot(id: i64): i32 {
868
+ /** The stream state of QUIC slot `k`, or null for one out of range. */
869
+ private slotAt(k: i32): WebTransportStream | null {
870
+ const slots: WebTransportStream[] = this.slots
871
+ if (k >= 0 && k < toI32(slots.length)) {
872
+ return slots[k]
873
+ }
874
+ return null
875
+ }
876
+
877
+ /** Copies session `w`'s ID and counters into the public per-session arrays. */
878
+ private publish(w: WebTransportSession): void {
879
+ const s: i32 = w.index
880
+ const ids: i64[] = this.sessionIds
881
+ if (s >= 0 && s < toI32(ids.length)) {
882
+ ids[s] = w.id
883
+ }
884
+ const streams: i32[] = this.streamCount
885
+ if (s >= 0 && s < toI32(streams.length)) {
886
+ streams[s] = w.streams
887
+ }
888
+ const into: i32[] = this.datagramsIn
889
+ if (s >= 0 && s < toI32(into.length)) {
890
+ into[s] = w.datagramsIn
891
+ }
892
+ const out: i32[] = this.datagramsOut
893
+ if (s >= 0 && s < toI32(out.length)) {
894
+ out[s] = w.datagramsOut
895
+ }
896
+ }
897
+
898
+ /** The session whose CONNECT stream is `id`, or null: one probe of QUIC's stream index. */
899
+ private sessionOf(id: i64): WebTransportSession | null {
699
900
  const k: i32 = this.quic.streams.slotOf(id)
700
- if (k < 0 || k >= toI32(this.streamIds.length) || this.streamIds[k] !== id) {
701
- return WT_NONE
901
+ const at: i32[] = this.sessionAt
902
+ if (k < 0 || k >= toI32(at.length)) {
903
+ return null
702
904
  }
703
- return k
905
+ const w: WebTransportSession | null = this.at(at[k])
906
+ if (w === null || w.id !== id) {
907
+ return null
908
+ }
909
+ return w
910
+ }
911
+
912
+ /** The state of a session's stream `id` that this layer tracks, or null. */
913
+ private streamOf(id: i64): WebTransportStream | null {
914
+ const t: WebTransportStream | null = this.slotAt(this.quic.streams.slotOf(id))
915
+ if (t === null || t.id !== id) {
916
+ return null
917
+ }
918
+ return t
704
919
  }
705
920
 
706
- /** The session of `id` when it is accepted and not gone, else -1. */
707
- private openSession(id: i64): i32 {
921
+ /** The session of `id` when it is accepted and not gone, else null. */
922
+ private openSession(id: i64): WebTransportSession | null {
708
923
  this.current()
709
- const s: i32 = this.sessionOf(id)
710
- return s >= 0 && this.states[s] === WT_OPEN ? s : WT_NONE
924
+ const w: WebTransportSession | null = this.sessionOf(id)
925
+ if (w === null || w.state !== WT_OPEN) {
926
+ return null
927
+ }
928
+ return w
711
929
  }
712
930
 
713
931
  // ---- Events ----------------------------------------------------------------------
@@ -725,12 +943,21 @@ export class WebTransport {
725
943
  const clock: i64 = monotonicNanos()
726
944
  // The clock never runs back, so a session's deadline only moves later.
727
945
  this.now = clock > this.now ? clock : this.now
728
- if (this.expiredCount > 0) {
729
- this.expiredCount = this.expiredCount - 1
730
- const id: i64 = this.expiredIds[this.expiredCount]
731
- this.sessionId = id
732
- this.stream = id
733
- return this.closedEvent(this.expiredSessions[this.expiredCount], WT_ZERO64, WT_ZERO, false)
946
+ const expired: i32 = this.expiredCount
947
+ if (expired > 0) {
948
+ const i: i32 = expired - 1
949
+ this.expiredCount = i
950
+ const ids: i64[] = this.expiredIds
951
+ const sessions: i32[] = this.expiredSessions
952
+ if (i < toI32(ids.length) && i < toI32(sessions.length)) {
953
+ const w: WebTransportSession | null = this.at(sessions[i])
954
+ if (w !== null) {
955
+ const id: i64 = ids[i]
956
+ this.sessionId = id
957
+ this.stream = id
958
+ return this.closedEvent(w, WT_ZERO64, WT_ZERO, false)
959
+ }
960
+ }
734
961
  }
735
962
  for (let guard: i32 = 0; guard < 1000000; guard++) {
736
963
  if (this.adoptCount > 0) {
@@ -774,40 +1001,47 @@ export class WebTransport {
774
1001
  * of an accepted session, 1 for one dropped, or 0 when none is there.
775
1002
  */
776
1003
  private receiveDatagram(): i32 {
777
- const n: i32 = this.quic.readDatagram(this.datagram, WT_ZERO, toI32(this.datagram.length))
1004
+ const buf: u8[] = this.datagram
1005
+ const n: i32 = this.quic.readDatagram(buf, WT_ZERO, toI32(buf.length))
778
1006
  if (n === QUIC_DATAGRAM_NONE || n < 0) {
779
1007
  return WT_ZERO
780
1008
  }
781
- const quarter: i64 = h3ReadVarint(this.datagram, WT_ZERO, n)
1009
+ const quarter: i64 = h3ReadVarint(buf, WT_ZERO, n)
782
1010
  if (quarter < 0 || quarter > WT_QUARTER_MAX) {
783
1011
  // RFC 9297 §2.1: a quarter stream ID that does not parse, or is too large.
784
1012
  this.h3.fail(H3_DATAGRAM_ERROR)
785
1013
  return WT_ONE
786
1014
  }
787
- const s: i32 = this.sessionOf(quarter * 4)
788
- if (s < 0 || this.states[s] !== WT_OPEN) {
1015
+ // Under 2^60, so the stream ID it names is under 2^62.
1016
+ const w: WebTransportSession | null = this.sessionOf(quarter << toI64(2))
1017
+ if (w === null || w.state !== WT_OPEN) {
789
1018
  // §2.1.1: no session to take it; dropped.
790
1019
  this.datagramsDropped = h3Count(this.datagramsDropped)
791
1020
  return WT_ONE
792
1021
  }
793
- if (this.now - this.rateSince[s] >= WT_SECOND) {
794
- this.rateSince[s] = this.now
795
- this.rateCount[s] = 0
1022
+ const now: i64 = this.now
1023
+ const since: i64 = w.rateSince
1024
+ // `rateSince` is a reading of the same clock as `now`, which never runs back.
1025
+ if (now < 0 || since < 0 || since > now || now - since >= WT_SECOND) {
1026
+ w.rateSince = now
1027
+ w.rateCount = 0
796
1028
  }
797
- if (this.rateCount[s] >= this.config.maxDatagramsPerSecond) {
1029
+ const taken: i32 = w.rateCount
1030
+ if (taken < 0 || taken >= this.config.maxDatagramsPerSecond) {
798
1031
  // Past the session's datagrams a second: dropped, as one QUIC could not queue would be.
799
1032
  this.datagramsDropped = h3Count(this.datagramsDropped)
800
1033
  return WT_ONE
801
1034
  }
802
- this.rateCount[s] = this.rateCount[s] + 1
803
- this.lastActive[s] = this.now
804
- const at: i32 = h3VarintLength(this.datagram, WT_ZERO)
805
- this.datagramsIn[s] = h3Count(this.datagramsIn[s])
806
- this.session = s
807
- this.sessionId = this.sessionIds[s]
808
- this.data = this.datagram
1035
+ w.rateCount = wtAdd(taken, 1)
1036
+ w.lastActive = now
1037
+ const at: i32 = h3VarintLength(buf, WT_ZERO)
1038
+ w.datagramsIn = h3Count(w.datagramsIn)
1039
+ this.publish(w)
1040
+ this.session = w.index
1041
+ this.sessionId = w.id
1042
+ this.data = buf
809
1043
  this.dataStart = at
810
- this.dataLength = n - at
1044
+ this.dataLength = wtSub(n, at)
811
1045
  return WT_DATAGRAM
812
1046
  }
813
1047
 
@@ -835,20 +1069,24 @@ export class WebTransport {
835
1069
  return event
836
1070
  }
837
1071
  // The slot the event's stream was in: QUIC may have freed it with the read that ended it.
838
- const k: i32 = h3.slot >= 0 && h3.slot < toI32(this.streamIds.length) ? h3.slot : WT_NONE
839
- const s: i32 = k >= 0 ? this.sessionAt[k] : WT_NONE
840
- if (s >= 0 && s < toI32(this.sessionIds.length) && this.sessionIds[s] === id) {
841
- return this.connectEvent(s, event)
1072
+ const k: i32 = h3.slot
1073
+ const at: i32[] = this.sessionAt
1074
+ if (k >= 0 && k < toI32(at.length)) {
1075
+ const w: WebTransportSession | null = this.at(at[k])
1076
+ if (w !== null && w.id === id) {
1077
+ return this.connectEvent(w, event)
1078
+ }
842
1079
  }
843
- if (k < 0 || this.streamIds[k] !== id) {
1080
+ const t: WebTransportStream | null = this.slotAt(k)
1081
+ if (t === null || t.id !== id) {
844
1082
  // A plain request's, or one about a stream this layer refused or let go of.
845
1083
  return event === H3_STOPPED ? H3_NEED_MORE : event
846
1084
  }
847
- const t: i32 = this.streamSession[k]
848
- this.touch(k)
1085
+ const owner: WebTransportSession | null = this.at(t.session)
1086
+ this.touch(t)
849
1087
  this.stream = id
850
- this.session = t
851
- this.sessionId = t >= 0 && t < toI32(this.sessionIds.length) ? this.sessionIds[t] : WT_NONE64
1088
+ this.session = t.session
1089
+ this.sessionId = owner !== null ? owner.id : WT_NONE64
852
1090
  if (event === H3_DATA) {
853
1091
  this.data = h3.data
854
1092
  this.dataStart = h3.dataStart
@@ -856,17 +1094,17 @@ export class WebTransport {
856
1094
  return WT_STREAM_DATA
857
1095
  }
858
1096
  if (event === H3_END) {
859
- this.settle(k, WT_RECEIVED)
1097
+ this.settle(t, WT_RECEIVED)
860
1098
  return WT_STREAM_END
861
1099
  }
862
1100
  if (event === H3_RESET) {
863
1101
  this.codes(h3.errorCode)
864
- this.settle(k, WT_RECEIVED)
1102
+ this.settle(t, WT_RECEIVED)
865
1103
  return WT_STREAM_RESET
866
1104
  }
867
1105
  if (event === H3_STOPPED) {
868
1106
  this.codes(h3.errorCode)
869
- this.settle(k, WT_SENT)
1107
+ this.settle(t, WT_SENT)
870
1108
  return WT_STREAM_STOPPED
871
1109
  }
872
1110
  return event === H3_WRITABLE ? WT_WRITABLE : H3_NEED_MORE
@@ -878,44 +1116,44 @@ export class WebTransport {
878
1116
  this.appCode = wtCodeFromHttp3(code)
879
1117
  }
880
1118
 
881
- /** An event of session `s`'s CONNECT stream. */
882
- private connectEvent(s: i32, event: i32): i32 {
1119
+ /** An event of session `w`'s CONNECT stream. */
1120
+ private connectEvent(w: WebTransportSession, event: i32): i32 {
883
1121
  const h3: Http3Connection = this.h3
884
- this.lastActive[s] = this.now
885
- this.session = s
886
- this.sessionId = this.sessionIds[s]
887
- this.stream = this.sessionIds[s]
1122
+ w.lastActive = this.now
1123
+ this.session = w.index
1124
+ this.sessionId = w.id
1125
+ this.stream = w.id
888
1126
  if (event === H3_DATA) {
889
- if (this.states[s] === WT_GONE && !this.closeRead[s]) {
1127
+ if (w.state === WT_GONE && !w.closeRead) {
890
1128
  // This side closed it: what the client still sends is not read.
891
1129
  return H3_NEED_MORE
892
1130
  }
893
- this.capSession = s
1131
+ this.capSession = w.index
894
1132
  this.capAt = h3.dataStart
895
- this.capEnd = h3.dataStart + h3.dataLength
1133
+ this.capEnd = wtAdd(h3.dataStart, h3.dataLength)
896
1134
  return this.capsules()
897
1135
  }
898
1136
  if (event === H3_END || event === H3_RESET) {
899
1137
  // The client's half is over, and the session with it (draft-02 §5).
900
- const id: i64 = this.sessionIds[s]
901
- const wasGone: boolean = this.states[s] === WT_GONE
902
- const inside: boolean = this.capLeft[s] >= 0 || this.capHeadLen[s] > 0
903
- this.release(s)
1138
+ const id: i64 = w.id
1139
+ const wasGone: boolean = w.state === WT_GONE
1140
+ const inside: boolean = w.capLeft >= 0 || w.capHeadLen > 0
1141
+ this.release(w)
904
1142
  if (wasGone) {
905
1143
  // This side's half is finished already.
906
1144
  return H3_NEED_MORE
907
1145
  }
908
1146
  if (event === H3_RESET) {
909
1147
  // `nish/net/http3` abandoned this side's half with the reset, the client's or its own.
910
- return this.closedEvent(s, WT_ZERO64, WT_ZERO, h3.resetByPeer)
1148
+ return this.closedEvent(w, WT_ZERO64, WT_ZERO, h3.resetByPeer)
911
1149
  }
912
1150
  if (inside) {
913
1151
  // RFC 9297 §3.3: the stream ended inside a capsule.
914
1152
  h3.reset(id, H3_DATAGRAM_ERROR)
915
- return this.closedEvent(s, WT_ZERO64, WT_ZERO, false)
1153
+ return this.closedEvent(w, WT_ZERO64, WT_ZERO, false)
916
1154
  }
917
1155
  this.finish(id)
918
- return this.closedEvent(s, WT_ZERO64, WT_ZERO, true)
1156
+ return this.closedEvent(w, WT_ZERO64, WT_ZERO, true)
919
1157
  }
920
1158
  // Trailers on a CONNECT stream mean nothing to a session.
921
1159
  return event === H3_WRITABLE ? WT_WRITABLE : H3_NEED_MORE
@@ -928,21 +1166,21 @@ export class WebTransport {
928
1166
  * the stream is dropped unread, so no event will end the session later.
929
1167
  */
930
1168
  private finish(id: i64): boolean {
931
- if (this.h3.writeData(id, this.reason, WT_ZERO, WT_ZERO, true) >= 0) {
1169
+ if (this.h3.writeData(id, this.capsuleOut, WT_ZERO, WT_ZERO, true) >= 0) {
932
1170
  return true
933
1171
  }
934
1172
  this.h3.reset(id, H3_NO_ERROR)
935
1173
  return false
936
1174
  }
937
1175
 
938
- /** WT_CLOSED for session `s` with `code` and the reason `reason[s * 1028 + 4 ..][0 .. length)`. */
939
- private closedEvent(s: i32, code: i64, length: i32, byPeer: boolean): i32 {
940
- this.session = s
1176
+ /** WT_CLOSED for session `w` with `code` and the reason `w.reason[4 .. 4 + length)`. */
1177
+ private closedEvent(w: WebTransportSession, code: i64, length: i32, byPeer: boolean): i32 {
1178
+ this.session = w.index
941
1179
  this.errorCode = code & WT_U32
942
1180
  this.appCode = this.errorCode
943
1181
  this.closedByPeer = byPeer
944
- this.data = this.reason
945
- this.dataStart = s * WT_REASON_STRIDE + 4
1182
+ this.data = w.reason
1183
+ this.dataStart = 4
946
1184
  this.dataLength = length
947
1185
  return WT_CLOSED
948
1186
  }
@@ -957,9 +1195,10 @@ export class WebTransport {
957
1195
  const h3: Http3Connection = this.h3
958
1196
  const f: HttpFields = h3.fields
959
1197
  const k: i32 = this.quic.streams.slotOf(id)
960
- if (k >= 0 && k < toI32(this.sessionAt.length)) {
961
- const stale: i32 = this.sessionAt[k]
962
- if (stale >= 0 && stale < toI32(this.sessionIds.length) && this.sessionIds[stale] >= 0) {
1198
+ const at: i32[] = this.sessionAt
1199
+ if (k >= 0 && k < toI32(at.length)) {
1200
+ const stale: WebTransportSession | null = this.at(at[k])
1201
+ if (stale !== null && stale.id >= 0) {
963
1202
  // QUIC freed this slot under a session's CONNECT stream with no event to say so: let go of
964
1203
  // that session now, before the cap below counts it.
965
1204
  this.release(stale)
@@ -983,28 +1222,37 @@ export class WebTransport {
983
1222
  this.dropPending(id)
984
1223
  return H3_NEED_MORE
985
1224
  }
986
- if (k < 0 || k >= toI32(this.sessionAt.length)) {
1225
+ if (k < 0 || k >= toI32(at.length)) {
1226
+ return H3_NEED_MORE
1227
+ }
1228
+ const free: i32[] = this.free
1229
+ const i: i32 = wtSub(this.freeCount, 1)
1230
+ if (i < 0 || i >= toI32(free.length)) {
987
1231
  return H3_NEED_MORE
988
1232
  }
989
- this.freeCount = this.freeCount - 1
990
- const s: i32 = this.free[this.freeCount]
991
- this.sessionIds[s] = id
992
- this.states[s] = WT_ASKED
993
- this.sessionSlot[s] = k
994
- this.firstStream[s] = -1
995
- this.streamCount[s] = 0
996
- this.capType[s] = -1
997
- this.capLeft[s] = -1
998
- this.closeRead[s] = false
999
- this.capHeadLen[s] = 0
1000
- this.capFill[s] = 0
1001
- this.datagramsIn[s] = 0
1002
- this.datagramsOut[s] = 0
1003
- this.lastActive[s] = this.now
1004
- this.rateSince[s] = this.now
1005
- this.rateCount[s] = 0
1006
- this.sessionAt[k] = s
1007
- this.session = s
1233
+ const w: WebTransportSession | null = this.at(free[i])
1234
+ if (w === null) {
1235
+ return H3_NEED_MORE
1236
+ }
1237
+ this.freeCount = i
1238
+ w.id = id
1239
+ w.state = WT_ASKED
1240
+ w.slot = k
1241
+ w.firstStream = -1
1242
+ w.streams = 0
1243
+ w.capType = -1
1244
+ w.capLeft = -1
1245
+ w.closeRead = false
1246
+ w.capHeadLen = 0
1247
+ w.capFill = 0
1248
+ w.datagramsIn = 0
1249
+ w.datagramsOut = 0
1250
+ w.lastActive = this.now
1251
+ w.rateSince = this.now
1252
+ w.rateCount = 0
1253
+ this.publish(w)
1254
+ at[k] = w.index
1255
+ this.session = w.index
1008
1256
  this.sessionId = id
1009
1257
  this.stream = id
1010
1258
  return WT_SESSION
@@ -1032,20 +1280,22 @@ export class WebTransport {
1032
1280
  */
1033
1281
  accept(id: i64): i32 {
1034
1282
  this.current()
1035
- const s: i32 = this.sessionOf(id)
1036
- if (s < 0 || this.states[s] !== WT_ASKED) {
1283
+ const w: WebTransportSession | null = this.sessionOf(id)
1284
+ if (w === null || w.state !== WT_ASKED) {
1037
1285
  return H3_CLOSED
1038
1286
  }
1039
1287
  const result: i32 = this.h3.respond(id, WT_STATUS_OK, this.acceptNames, this.acceptValues, false)
1040
1288
  if (result !== 0) {
1041
1289
  return result
1042
1290
  }
1043
- this.states[s] = WT_OPEN
1044
- this.lastActive[s] = this.now
1291
+ w.state = WT_OPEN
1292
+ w.lastActive = this.now
1045
1293
  // The streams that waited for it are taken by the next calls of `next`, each with its WT_STREAM.
1046
- if (this.waiting.first(id) >= 0 && this.adoptCount < toI32(this.adoptions.length)) {
1047
- this.adoptions[this.adoptCount] = s
1048
- this.adoptCount = this.adoptCount + 1
1294
+ const list: i32[] = this.adoptions
1295
+ const c: i32 = this.adoptCount
1296
+ if (this.waiting.first(id) >= 0 && c >= 0 && c < toI32(list.length)) {
1297
+ list[c] = w.index
1298
+ this.adoptCount = c + 1
1049
1299
  }
1050
1300
  return 0
1051
1301
  }
@@ -1061,58 +1311,67 @@ export class WebTransport {
1061
1311
  */
1062
1312
  refuse(id: i64, status: i32): i32 {
1063
1313
  this.current()
1064
- const s: i32 = this.sessionOf(id)
1065
- if (s < 0 || this.states[s] !== WT_ASKED) {
1314
+ const w: WebTransportSession | null = this.sessionOf(id)
1315
+ if (w === null || w.state !== WT_ASKED) {
1066
1316
  return H3_CLOSED
1067
1317
  }
1068
1318
  if (status < 400 || status > 599) {
1069
1319
  return H3_INVALID
1070
1320
  }
1071
1321
  this.answer(id, status)
1072
- this.release(s)
1322
+ this.release(w)
1073
1323
  return 0
1074
1324
  }
1075
1325
 
1076
1326
  /**
1077
- * Ends session `s` here: every stream reset with WT_SESSION_GONE and asked
1327
+ * Ends session `w` here: every stream reset with WT_SESSION_GONE and asked
1078
1328
  * to stop, the streams waiting for it refused, and its slot freed.
1079
1329
  */
1080
- private release(s: i32): void {
1081
- this.end(s)
1082
- const k: i32 = this.sessionSlot[s]
1083
- if (k >= 0 && k < toI32(this.sessionAt.length) && this.sessionAt[k] === s) {
1084
- this.sessionAt[k] = -1
1085
- }
1086
- this.sessionIds[s] = -1
1087
- this.states[s] = WT_FREE
1088
- if (this.capSession === s) {
1330
+ private release(w: WebTransportSession): void {
1331
+ this.end(w)
1332
+ const k: i32 = w.slot
1333
+ const at: i32[] = this.sessionAt
1334
+ if (k >= 0 && k < toI32(at.length) && at[k] === w.index) {
1335
+ at[k] = -1
1336
+ }
1337
+ w.id = -1
1338
+ w.state = WT_FREE
1339
+ this.publish(w)
1340
+ if (this.capSession === w.index) {
1089
1341
  this.capSession = -1
1090
1342
  }
1091
- if (this.freeCount < toI32(this.free.length)) {
1092
- this.free[this.freeCount] = s
1093
- this.freeCount = this.freeCount + 1
1343
+ const free: i32[] = this.free
1344
+ const c: i32 = this.freeCount
1345
+ if (c >= 0 && c < toI32(free.length)) {
1346
+ free[c] = w.index
1347
+ this.freeCount = c + 1
1094
1348
  }
1095
1349
  }
1096
1350
 
1097
- /** Resets every stream of session `s` and refuses those waiting for it; the session is gone. */
1098
- private end(s: i32): void {
1099
- if (this.states[s] === WT_FREE) {
1351
+ /** Resets every stream of session `w` and refuses those waiting for it; the session is gone. */
1352
+ private end(w: WebTransportSession): void {
1353
+ if (w.state === WT_FREE) {
1100
1354
  return
1101
1355
  }
1102
- let k: i32 = this.firstStream[s]
1103
- for (let guard: i32 = 0; guard < WT_MAX_CAP && k >= 0 && k < toI32(this.streamIds.length); guard++) {
1104
- const following: i32 = this.nextStream[k]
1105
- this.h3.refuseStream(this.streamIds[k], WT_SESSION_GONE)
1106
- this.streamIds[k] = -1
1107
- this.streamSession[k] = -1
1356
+ let k: i32 = w.firstStream
1357
+ for (let guard: i32 = 0; guard < WT_MAX_CAP; guard++) {
1358
+ const t: WebTransportStream | null = this.slotAt(k)
1359
+ if (t === null) {
1360
+ break
1361
+ }
1362
+ const following: i32 = t.next
1363
+ this.h3.refuseStream(t.id, WT_SESSION_GONE)
1364
+ t.id = -1
1365
+ t.session = -1
1108
1366
  k = following
1109
1367
  }
1110
- this.firstStream[s] = -1
1111
- this.streamCount[s] = 0
1112
- this.dropPending(this.sessionIds[s])
1113
- this.states[s] = WT_GONE
1368
+ w.firstStream = -1
1369
+ w.streams = 0
1370
+ this.publish(w)
1371
+ this.dropPending(w.id)
1372
+ w.state = WT_GONE
1114
1373
  // A closed session's clock restarts: the client has `idleTimeout` to finish its half.
1115
- this.lastActive[s] = this.now
1374
+ w.lastActive = this.now
1116
1375
  }
1117
1376
 
1118
1377
  /** Refuses with WT_SESSION_GONE every stream waiting for session `id`. */
@@ -1122,7 +1381,11 @@ export class WebTransport {
1122
1381
  }
1123
1382
  let e: i32 = this.waiting.first(id)
1124
1383
  for (let guard: i32 = 0; guard < WT_MAX_CAP && e >= 0; guard++) {
1125
- this.refuseStream(this.waiting.take(e), WT_SESSION_GONE)
1384
+ const stream: i64 = this.waiting.take(e)
1385
+ if (stream < 0) {
1386
+ return
1387
+ }
1388
+ this.refuseStream(stream, WT_SESSION_GONE)
1126
1389
  e = this.waiting.first(id)
1127
1390
  }
1128
1391
  }
@@ -1147,28 +1410,32 @@ export class WebTransport {
1147
1410
  */
1148
1411
  close(id: i64, code: i64, buf: u8[], off: i32, len: i32): i32 {
1149
1412
  h3CheckWindow("WebTransport.close", buf, off, len)
1150
- const s: i32 = this.openSession(id)
1151
- if (s < 0) {
1413
+ const w: WebTransportSession | null = this.openSession(id)
1414
+ if (w === null) {
1152
1415
  return H3_CLOSED
1153
1416
  }
1154
- if (len > WT_REASON_MAX) {
1417
+ if (len < 0 || len > 1024) {
1155
1418
  return H3_INVALID
1156
1419
  }
1157
1420
  const out: u8[] = this.capsuleOut
1158
1421
  const end: i32 = toI32(out.length)
1159
- let p: i32 = h3PutFrameHeader(out, WT_ZERO, end, WT_CAPSULE_CLOSE, toI64(4 + len))
1422
+ const p: i32 = h3PutFrameHeader(out, WT_ZERO, end, WT_CAPSULE_CLOSE, toI64(len + 4))
1160
1423
  const value: i64 = code & WT_U32
1161
- out[p] = toU8(toI32((value >> toI64(24)) & toI64(255)))
1162
- out[p + 1] = toU8(toI32((value >> toI64(16)) & toI64(255)))
1163
- out[p + 2] = toU8(toI32((value >> toI64(8)) & toI64(255)))
1164
- out[p + 3] = toU8(toI32(value & toI64(255)))
1165
- p = p + 4
1166
- quicPacketCopy(out, p, buf, off, len)
1167
- const result: i32 = this.h3.writeDataWhole(id, out, WT_ZERO, p + len, true)
1424
+ const head: u8[] = [
1425
+ toU8(toI32((value >> toI64(24)) & toI64(255))),
1426
+ toU8(toI32((value >> toI64(16)) & toI64(255))),
1427
+ toU8(toI32((value >> toI64(8)) & toI64(255))),
1428
+ toU8(toI32(value & toI64(255))),
1429
+ ]
1430
+ const at: i32 = wtAdd(p, 4)
1431
+ if (p < 0 || !tryCopyBytes(out, p, head, WT_ZERO, 4) || !tryCopyBytes(out, at, buf, off, len)) {
1432
+ return H3_TOO_LARGE
1433
+ }
1434
+ const result: i32 = this.h3.writeDataWhole(id, out, WT_ZERO, wtAdd(at, len), true)
1168
1435
  if (result !== 0) {
1169
1436
  return result
1170
1437
  }
1171
- this.end(s)
1438
+ this.end(w)
1172
1439
  return 0
1173
1440
  }
1174
1441
 
@@ -1181,8 +1448,7 @@ export class WebTransport {
1181
1448
  * written.
1182
1449
  */
1183
1450
  drain(id: i64): i32 {
1184
- const s: i32 = this.openSession(id)
1185
- if (s < 0) {
1451
+ if (this.openSession(id) === null) {
1186
1452
  return H3_CLOSED
1187
1453
  }
1188
1454
  const out: u8[] = this.capsuleOut
@@ -1191,16 +1457,21 @@ export class WebTransport {
1191
1457
  return this.h3.writeDataWhole(id, out, WT_ZERO, p, false)
1192
1458
  }
1193
1459
 
1194
- /** Stamps the session of the stream in QUIC slot `k` as active now. */
1195
- private touch(k: i32): void {
1196
- const t: i32 = this.streamSession[k]
1197
- if (t >= 0 && t < toI32(this.lastActive.length)) {
1198
- this.lastActive[t] = this.now
1460
+ /** Stamps the session of stream `t` as active now. */
1461
+ private touch(t: WebTransportStream): void {
1462
+ const w: WebTransportSession | null = this.at(t.session)
1463
+ if (w !== null) {
1464
+ w.lastActive = this.now
1199
1465
  }
1200
1466
  }
1201
1467
 
1202
1468
  // ---- The clock -------------------------------------------------------------------
1203
1469
 
1470
+ /** `idleTimeout` in nanoseconds: at most a day's, so worked exactly in `f64`. */
1471
+ private idleNanos(): i64 {
1472
+ return toI64(toF64(this.config.idleTimeout) * toF64(WT_MILLI))
1473
+ }
1474
+
1204
1475
  /**
1205
1476
  * Ends every session quiet for longer than `idleTimeout` at `now` (see
1206
1477
  * the module comment): one never answered is refused with 503, an
@@ -1208,31 +1479,37 @@ export class WebTransport {
1208
1479
  * never finished its half let go. Call it from the program's loop; it
1209
1480
  * returns at once before the soonest deadline it last saw. The next calls
1210
1481
  * of `next` report each session the program held as WT_CLOSED.
1211
- * @param now The current time, from `monotonicNanos`.
1482
+ * @param now The current time, from `monotonicNanos`; a negative one ends none.
1212
1483
  * @returns How many sessions it ended.
1213
1484
  */
1214
1485
  expire(now: i64): i32 {
1215
1486
  this.current()
1216
- if (now < this.expireAfter) {
1487
+ const idle: i64 = this.idleNanos()
1488
+ if (now < 0 || idle < 0 || now < this.expireAfter) {
1217
1489
  return 0
1218
1490
  }
1219
1491
  this.now = now > this.now ? now : this.now
1220
- const idle: i64 = toI64(this.config.idleTimeout) * WT_MILLI
1221
- let soonest: i64 = now + idle
1492
+ // The least time left to any session, from `now`.
1493
+ let wait: i64 = idle
1222
1494
  let n: i32 = 0
1223
- for (let s: i32 = 0; s < toI32(this.states.length); s++) {
1224
- const state: i32 = this.states[s]
1495
+ const table: WebTransportSession[] = this.table
1496
+ for (let s: i32 = 0; s < toI32(table.length); s++) {
1497
+ const w: WebTransportSession = table[s]
1498
+ const state: i32 = w.state
1225
1499
  if (state !== WT_FREE) {
1226
- const deadline: i64 = this.lastActive[s] + idle
1227
- if (now > deadline) {
1228
- this.timeOut(s, state)
1229
- n = n + 1
1230
- } else if (deadline < soonest) {
1231
- soonest = deadline
1500
+ const last: i64 = w.lastActive
1501
+ // Activity stamped after `now` (by a later `next`) is no quiet at all.
1502
+ const quiet: i64 = last >= 0 && last <= now ? now - last : WT_ZERO64
1503
+ if (quiet > idle) {
1504
+ this.timeOut(w, state)
1505
+ n = wtAdd(n, 1)
1506
+ } else if (quiet >= 0 && idle - quiet < wait) {
1507
+ wait = idle - quiet
1232
1508
  }
1233
1509
  }
1234
1510
  }
1235
- this.expireAfter = soonest
1511
+ // A clock reading plus at most a day: under 2^63 for 290 years of uptime.
1512
+ this.expireAfter = now + wait
1236
1513
  return n
1237
1514
  }
1238
1515
 
@@ -1241,41 +1518,57 @@ export class WebTransport {
1241
1518
  * only moves a session's deadline later.
1242
1519
  * @param now The current time, from `monotonicNanos`.
1243
1520
  * @returns The earliest `monotonicNanos` instant at which `expire` could end a session; `now` plus
1244
- * `idleTimeout` when no session is held.
1521
+ * `idleTimeout` when no session is held, and `now` itself for a negative `now`.
1245
1522
  */
1246
1523
  expiresNoSooner(now: i64): i64 {
1247
- const idle: i64 = toI64(this.config.idleTimeout) * WT_MILLI
1248
- let soonest: i64 = now + idle
1249
- for (let s: i32 = 0; s < toI32(this.states.length); s++) {
1250
- if (this.states[s] !== WT_FREE && this.lastActive[s] + idle < soonest) {
1251
- soonest = this.lastActive[s] + idle
1524
+ const idle: i64 = this.idleNanos()
1525
+ if (now < 0 || idle < 0) {
1526
+ return now
1527
+ }
1528
+ let wait: i64 = idle
1529
+ const table: WebTransportSession[] = this.table
1530
+ for (let s: i32 = 0; s < toI32(table.length); s++) {
1531
+ const w: WebTransportSession = table[s]
1532
+ if (w.state !== WT_FREE) {
1533
+ const last: i64 = w.lastActive
1534
+ const quiet: i64 = last >= 0 && last <= now ? now - last : WT_ZERO64
1535
+ if (quiet >= idle) {
1536
+ return now
1537
+ }
1538
+ if (quiet >= 0 && idle - quiet < wait) {
1539
+ wait = idle - quiet
1540
+ }
1252
1541
  }
1253
1542
  }
1254
- return soonest > now ? soonest : now
1543
+ // As in `expire`: a clock reading plus at most a day.
1544
+ return now + wait
1255
1545
  }
1256
1546
 
1257
- /** Ends session `s`, in `state`, for its idleness, and queues its WT_CLOSED unless the program let go of it already. */
1258
- private timeOut(s: i32, state: i32): void {
1259
- const id: i64 = this.sessionIds[s]
1547
+ /** Ends session `w`, in `state`, for its idleness, and queues its WT_CLOSED unless the program let go of it already. */
1548
+ private timeOut(w: WebTransportSession, state: i32): void {
1549
+ const id: i64 = w.id
1260
1550
  if (state === WT_ASKED) {
1261
1551
  this.answer(id, WT_STATUS_UNANSWERED)
1262
- this.release(s)
1552
+ this.release(w)
1263
1553
  } else if (state === WT_OPEN) {
1264
- if (this.close(id, WT_ZERO64, this.reason, WT_ZERO, WT_ZERO) !== 0) {
1554
+ if (this.close(id, WT_ZERO64, this.capsuleOut, WT_ZERO, WT_ZERO) !== 0) {
1265
1555
  // No room for the capsule: reset the CONNECT stream both ways, after which no event ends the session.
1266
1556
  this.h3.reset(id, H3_NO_ERROR)
1267
- this.release(s)
1557
+ this.release(w)
1268
1558
  }
1269
1559
  } else {
1270
1560
  // Closed here, or by the client's CLOSE, and the client never finished its half: stop reading it.
1271
1561
  this.h3.discardRequest(id, H3_NO_ERROR)
1272
- this.release(s)
1562
+ this.release(w)
1273
1563
  return
1274
1564
  }
1275
- if (this.expiredCount < toI32(this.expiredSessions.length)) {
1276
- this.expiredSessions[this.expiredCount] = s
1277
- this.expiredIds[this.expiredCount] = id
1278
- this.expiredCount = this.expiredCount + 1
1565
+ const sessions: i32[] = this.expiredSessions
1566
+ const ids: i64[] = this.expiredIds
1567
+ const c: i32 = this.expiredCount
1568
+ if (c >= 0 && c < toI32(sessions.length) && c < toI32(ids.length)) {
1569
+ sessions[c] = w.index
1570
+ ids[c] = id
1571
+ this.expiredCount = c + 1
1279
1572
  }
1280
1573
  }
1281
1574
 
@@ -1287,51 +1580,63 @@ export class WebTransport {
1287
1580
  * make, or H3_NEED_MORE once the window is read.
1288
1581
  */
1289
1582
  private capsules(): i32 {
1290
- const s: i32 = this.capSession
1583
+ const w: WebTransportSession | null = this.at(this.capSession)
1584
+ if (w === null) {
1585
+ this.capSession = -1
1586
+ return H3_NEED_MORE
1587
+ }
1291
1588
  const bytes: u8[] = this.h3.data
1292
- const base: i32 = s * WT_HEAD
1293
1589
  while (this.capAt < this.capEnd && this.capAt >= 0 && this.capAt < toI32(bytes.length)) {
1294
- if (this.closeRead[s]) {
1590
+ const at: i32 = this.capAt
1591
+ if (w.closeRead) {
1295
1592
  // §5: nothing may follow a CLOSE.
1296
- return this.malformed(s, H3_MESSAGE_ERROR)
1593
+ return this.malformed(w, H3_MESSAGE_ERROR)
1297
1594
  }
1298
- if (this.capLeft[s] < 0) {
1595
+ const left: i64 = w.capLeft
1596
+ if (left < 0) {
1299
1597
  // The header, a byte at a time into the session's own buffer: it may be split across DATA frames.
1300
- const have: i32 = this.capHeadLen[s]
1301
- if (have >= WT_HEAD || base + have >= toI32(this.capHead.length)) {
1302
- return this.malformed(s, H3_DATAGRAM_ERROR)
1598
+ const have: i32 = w.capHeadLen
1599
+ const head: u8[] = w.capHead
1600
+ if (have < 0 || have >= toI32(head.length) || at < 0 || at >= toI32(bytes.length)) {
1601
+ return this.malformed(w, H3_DATAGRAM_ERROR)
1303
1602
  }
1304
- this.capHead[base + have] = bytes[this.capAt]
1305
- this.capAt = this.capAt + 1
1306
- this.capHeadLen[s] = have + 1
1307
- if (h3ReadFrameHeader(this.header, this.capHead, base, have + 1) === 0) {
1603
+ head[have] = bytes[at]
1604
+ this.capAt = wtAdd(at, 1)
1605
+ w.capHeadLen = have + 1
1606
+ if (h3ReadFrameHeader(this.header, head, WT_ZERO, have + 1) === 0) {
1308
1607
  continue
1309
1608
  }
1310
- this.capHeadLen[s] = 0
1609
+ w.capHeadLen = 0
1311
1610
  const type: i64 = this.header.type
1312
1611
  const length: i64 = this.header.length
1313
- if (type === WT_CAPSULE_CLOSE && (length < 4 || length > toI64(WT_REASON_MAX + 4))) {
1314
- return this.malformed(s, H3_DATAGRAM_ERROR)
1612
+ if (type === WT_CAPSULE_CLOSE && (length < 4 || length > toI64(WT_REASON_STRIDE))) {
1613
+ return this.malformed(w, H3_DATAGRAM_ERROR)
1315
1614
  }
1316
1615
  if (type === WT_CAPSULE_DRAIN && length !== toI64(0)) {
1317
- return this.malformed(s, H3_DATAGRAM_ERROR)
1616
+ return this.malformed(w, H3_DATAGRAM_ERROR)
1318
1617
  }
1319
- this.capType[s] = type
1320
- this.capLeft[s] = length
1321
- this.capFill[s] = 0
1618
+ w.capType = type
1619
+ w.capLeft = length
1620
+ w.capFill = 0
1322
1621
  } else {
1323
- const want: i64 = toI64(this.capEnd - this.capAt)
1324
- const take: i32 = toI32(this.capLeft[s] < want ? this.capLeft[s] : want)
1325
- if (this.capType[s] === WT_CAPSULE_CLOSE) {
1326
- quicPacketCopy(this.reason, s * WT_REASON_STRIDE + this.capFill[s], bytes, this.capAt, take)
1327
- this.capFill[s] = this.capFill[s] + take
1622
+ const want: i64 = toI64(wtSub(this.capEnd, at))
1623
+ const take: i32 = toI32(left < want ? left : want)
1624
+ if (take < 0) {
1625
+ return this.malformed(w, H3_DATAGRAM_ERROR)
1626
+ }
1627
+ // The CLOSE's length was held to `reason`'s size above, so the copy fits.
1628
+ if (w.capType === WT_CAPSULE_CLOSE && !tryCopyBytes(w.reason, w.capFill, bytes, at, take)) {
1629
+ return this.malformed(w, H3_DATAGRAM_ERROR)
1328
1630
  }
1329
- this.capAt = this.capAt + take
1330
- this.capLeft[s] = this.capLeft[s] - toI64(take)
1631
+ if (w.capType === WT_CAPSULE_CLOSE) {
1632
+ w.capFill = wtAdd(w.capFill, take)
1633
+ }
1634
+ this.capAt = wtAdd(at, take)
1635
+ w.capLeft = left - toI64(take)
1331
1636
  }
1332
- if (this.capLeft[s] === toI64(0)) {
1333
- this.capLeft[s] = -1
1334
- const event: i32 = this.capsule(s, this.capType[s])
1637
+ if (w.capLeft === toI64(0)) {
1638
+ w.capLeft = -1
1639
+ const event: i32 = this.capsule(w, w.capType)
1335
1640
  if (event !== H3_NEED_MORE) {
1336
1641
  return event
1337
1642
  }
@@ -1341,59 +1646,61 @@ export class WebTransport {
1341
1646
  return H3_NEED_MORE
1342
1647
  }
1343
1648
 
1344
- /** A whole capsule of `type` on session `s`'s CONNECT stream. */
1345
- private capsule(s: i32, type: i64): i32 {
1346
- this.session = s
1347
- this.sessionId = this.sessionIds[s]
1348
- this.stream = this.sessionIds[s]
1649
+ /** A whole capsule of `type` on session `w`'s CONNECT stream. */
1650
+ private capsule(w: WebTransportSession, type: i64): i32 {
1651
+ this.session = w.index
1652
+ this.sessionId = w.id
1653
+ this.stream = w.id
1349
1654
  if (type === WT_CAPSULE_DRAIN) {
1350
- return this.states[s] === WT_GONE ? H3_NEED_MORE : WT_DRAIN
1655
+ return w.state === WT_GONE ? H3_NEED_MORE : WT_DRAIN
1351
1656
  }
1352
1657
  if (type !== WT_CAPSULE_CLOSE) {
1353
1658
  // RFC 9297 §3.2: a capsule type this side does not know is skipped.
1354
1659
  return H3_NEED_MORE
1355
1660
  }
1356
- this.closeRead[s] = true
1357
- if (this.states[s] === WT_GONE) {
1661
+ w.closeRead = true
1662
+ if (w.state === WT_GONE) {
1358
1663
  // This side closed it first, and finished its half then.
1359
1664
  return H3_NEED_MORE
1360
1665
  }
1361
- const at: i32 = s * WT_REASON_STRIDE
1666
+ const reason: u8[] = w.reason
1362
1667
  let code: i64 = 0
1363
- for (let j: i32 = 0; j < 4 && at + j < toI32(this.reason.length); j++) {
1364
- code = (code << toI64(8)) | toI64(toI32(this.reason[at + j]))
1668
+ for (let j: i32 = 0; j < 4 && j < toI32(reason.length); j++) {
1669
+ code = (code << toI64(8)) | toI64(toI32(reason[j]))
1365
1670
  }
1366
- this.end(s)
1367
- const length: i32 = this.capFill[s] - 4
1671
+ this.end(w)
1672
+ // A CLOSE's payload is its 4-byte code and then the reason, 1028 bytes at most.
1673
+ const fill: i32 = w.capFill
1674
+ const length: i32 = fill >= 4 && fill <= 1028 ? fill - 4 : WT_ZERO
1368
1675
  // §5: the CLOSE is answered by finishing this side's half; when that cannot go, the session is over here and now.
1369
- if (!this.finish(this.sessionIds[s])) {
1370
- this.release(s)
1676
+ if (!this.finish(w.id)) {
1677
+ this.release(w)
1371
1678
  }
1372
- return this.closedEvent(s, code, length, true)
1679
+ return this.closedEvent(w, code, length, true)
1373
1680
  }
1374
1681
 
1375
- /** A capsule that breaks a rule on session `s`: its CONNECT stream reset both ways with `code`, and the session over. */
1376
- private malformed(s: i32, code: i64): i32 {
1377
- const id: i64 = this.sessionIds[s]
1378
- const wasGone: boolean = this.states[s] === WT_GONE
1682
+ /** A capsule that breaks a rule on session `w`: its CONNECT stream reset both ways with `code`, and the session over. */
1683
+ private malformed(w: WebTransportSession, code: i64): i32 {
1684
+ const id: i64 = w.id
1685
+ const wasGone: boolean = w.state === WT_GONE
1379
1686
  this.capSession = -1
1380
- this.release(s)
1687
+ this.release(w)
1381
1688
  this.h3.reset(id, code)
1382
1689
  this.h3.discardRequest(id, code)
1383
1690
  this.stream = id
1384
1691
  this.sessionId = id
1385
- return wasGone ? H3_NEED_MORE : this.closedEvent(s, WT_ZERO64, WT_ZERO, false)
1692
+ return wasGone ? H3_NEED_MORE : this.closedEvent(w, WT_ZERO64, WT_ZERO, false)
1386
1693
  }
1387
1694
 
1388
1695
  // ---- Streams ---------------------------------------------------------------------
1389
1696
 
1390
1697
  /** A stream named session `session` in its first bytes (H3_STREAM): taken, kept waiting or refused. */
1391
1698
  private arrived(id: i64, session: i64): i32 {
1392
- const s: i32 = this.sessionOf(session)
1393
- if (s >= 0 && this.states[s] === WT_OPEN) {
1394
- return this.adopt(s, id)
1699
+ const w: WebTransportSession | null = this.sessionOf(session)
1700
+ if (w !== null && w.state === WT_OPEN) {
1701
+ return this.adopt(w, id)
1395
1702
  }
1396
- if ((s >= 0 && this.states[s] === WT_ASKED) || (s < 0 && this.h3.awaitsRequest(session))) {
1703
+ if ((w !== null && w.state === WT_ASKED) || (w === null && this.h3.awaitsRequest(session))) {
1397
1704
  // draft-02 §4.5: it waits, unread, for its session.
1398
1705
  if (this.waiting.count < this.config.maxPending && this.waiting.add(id, session)) {
1399
1706
  return H3_NEED_MORE
@@ -1411,14 +1718,21 @@ export class WebTransport {
1411
1718
  * session has none left (or is gone), when the next session's turn comes.
1412
1719
  */
1413
1720
  private adoptWaiting(): i32 {
1414
- const s: i32 = this.adoptions[this.adoptCount - 1]
1415
- if (s >= 0 && s < toI32(this.states.length) && this.states[s] === WT_OPEN) {
1416
- const e: i32 = this.waiting.first(this.sessionIds[s])
1417
- if (e >= 0) {
1418
- return this.adopt(s, this.waiting.take(e))
1721
+ const list: i32[] = this.adoptions
1722
+ const i: i32 = wtSub(this.adoptCount, 1)
1723
+ if (i < 0 || i >= toI32(list.length)) {
1724
+ this.adoptCount = 0
1725
+ return H3_NEED_MORE
1726
+ }
1727
+ const w: WebTransportSession | null = this.at(list[i])
1728
+ if (w !== null && w.state === WT_OPEN) {
1729
+ const e: i32 = this.waiting.first(w.id)
1730
+ const stream: i64 = e >= 0 ? this.waiting.take(e) : WT_NONE64
1731
+ if (stream >= 0) {
1732
+ return this.adopt(w, stream)
1419
1733
  }
1420
1734
  }
1421
- this.adoptCount = this.adoptCount - 1
1735
+ this.adoptCount = i
1422
1736
  return H3_NEED_MORE
1423
1737
  }
1424
1738
 
@@ -1429,76 +1743,81 @@ export class WebTransport {
1429
1743
  }
1430
1744
 
1431
1745
  /**
1432
- * Takes the client's stream `id` into session `s`, unless the session
1746
+ * Takes the client's stream `id` into session `w`, unless the session
1433
1747
  * holds `maxStreams` already: answers WT_STREAM, or H3_NEED_MORE for one
1434
1748
  * refused with H3_REQUEST_REJECTED.
1435
1749
  */
1436
- private adopt(s: i32, id: i64): i32 {
1437
- if (this.streamCount[s] >= this.config.maxStreams) {
1750
+ private adopt(w: WebTransportSession, id: i64): i32 {
1751
+ if (w.streams >= this.config.maxStreams) {
1438
1752
  this.refuseStream(id, H3_REQUEST_REJECTED)
1439
1753
  return H3_NEED_MORE
1440
1754
  }
1441
1755
  const bidi: boolean = !quicStreamIsUni(id)
1442
- if (this.h3.acceptStream(id) !== 0 || this.link(s, id, bidi ? WT_ZERO : WT_SENT) < 0) {
1756
+ if (this.h3.acceptStream(id) !== 0 || this.link(w, id, bidi ? WT_ZERO : WT_SENT) < 0) {
1443
1757
  return H3_NEED_MORE
1444
1758
  }
1445
- this.session = s
1446
- this.sessionId = this.sessionIds[s]
1759
+ this.session = w.index
1760
+ this.sessionId = w.id
1447
1761
  this.stream = id
1448
1762
  this.bidirectional = bidi
1449
1763
  return WT_STREAM
1450
1764
  }
1451
1765
 
1452
- /** Puts stream `id` at the head of session `s`'s list with `bits`; answers its slot, or -1. */
1453
- private link(s: i32, id: i64, bits: i32): i32 {
1766
+ /** Puts stream `id` at the head of session `w`'s list with `bits`; answers its slot, or -1. */
1767
+ private link(w: WebTransportSession, id: i64, bits: i32): i32 {
1454
1768
  const k: i32 = this.quic.streams.slotOf(id)
1455
- if (k < 0 || k >= toI32(this.streamIds.length)) {
1769
+ const t: WebTransportStream | null = this.slotAt(k)
1770
+ if (t === null) {
1456
1771
  return WT_NONE
1457
1772
  }
1458
- if (this.streamIds[k] >= 0) {
1773
+ if (t.id >= 0) {
1459
1774
  // QUIC freed this slot under a stream whose end no event told (a STOP_SENDING it answered and saw acknowledged): let go of it now.
1460
- this.unlink(k)
1461
- }
1462
- const head: i32 = this.firstStream[s]
1463
- this.streamIds[k] = id
1464
- this.streamSession[k] = s
1465
- this.streamBits[k] = bits
1466
- this.prevStream[k] = -1
1467
- this.nextStream[k] = head
1468
- if (head >= 0 && head < toI32(this.prevStream.length)) {
1469
- this.prevStream[head] = k
1470
- }
1471
- this.firstStream[s] = k
1472
- this.streamCount[s] = this.streamCount[s] + 1
1775
+ this.unlink(t)
1776
+ }
1777
+ const head: i32 = w.firstStream
1778
+ t.id = id
1779
+ t.session = w.index
1780
+ t.bits = bits
1781
+ t.prev = -1
1782
+ t.next = head
1783
+ const first: WebTransportStream | null = this.slotAt(head)
1784
+ if (first !== null) {
1785
+ first.prev = k
1786
+ }
1787
+ w.firstStream = k
1788
+ // At most `maxStreams`, 4096: `adopt` and `openStream` check it first.
1789
+ w.streams = wtAdd(w.streams, 1)
1790
+ this.publish(w)
1473
1791
  return k
1474
1792
  }
1475
1793
 
1476
- /** Marks `bits` done on the stream in slot `k`, and takes it off its session once both sides are. */
1477
- private settle(k: i32, bits: i32): void {
1478
- this.streamBits[k] = this.streamBits[k] | bits
1479
- if ((this.streamBits[k] & (WT_SENT | WT_RECEIVED)) === (WT_SENT | WT_RECEIVED)) {
1480
- this.unlink(k)
1794
+ /** Marks `bits` done on stream `t`, and takes it off its session once both sides are. */
1795
+ private settle(t: WebTransportStream, bits: i32): void {
1796
+ t.bits = t.bits | bits
1797
+ if ((t.bits & (WT_SENT | WT_RECEIVED)) === (WT_SENT | WT_RECEIVED)) {
1798
+ this.unlink(t)
1481
1799
  }
1482
1800
  }
1483
1801
 
1484
- /** Takes the stream in slot `k` off its session's list. */
1485
- private unlink(k: i32): void {
1486
- const s: i32 = this.streamSession[k]
1487
- const before: i32 = this.prevStream[k]
1488
- const after: i32 = this.nextStream[k]
1489
- if (before >= 0 && before < toI32(this.nextStream.length)) {
1490
- this.nextStream[before] = after
1491
- } else if (s >= 0 && s < toI32(this.firstStream.length)) {
1492
- this.firstStream[s] = after
1493
- }
1494
- if (after >= 0 && after < toI32(this.prevStream.length)) {
1495
- this.prevStream[after] = before
1496
- }
1497
- if (s >= 0 && s < toI32(this.streamCount.length)) {
1498
- this.streamCount[s] = this.streamCount[s] - 1
1499
- }
1500
- this.streamIds[k] = -1
1501
- this.streamSession[k] = -1
1802
+ /** Takes stream `t` off its session's list. */
1803
+ private unlink(t: WebTransportStream): void {
1804
+ const w: WebTransportSession | null = this.at(t.session)
1805
+ const before: WebTransportStream | null = this.slotAt(t.prev)
1806
+ const after: WebTransportStream | null = this.slotAt(t.next)
1807
+ if (before !== null) {
1808
+ before.next = t.next
1809
+ } else if (w !== null) {
1810
+ w.firstStream = t.next
1811
+ }
1812
+ if (after !== null) {
1813
+ after.prev = t.prev
1814
+ }
1815
+ if (w !== null) {
1816
+ w.streams = wtSub(w.streams, 1)
1817
+ this.publish(w)
1818
+ }
1819
+ t.id = -1
1820
+ t.session = -1
1502
1821
  }
1503
1822
 
1504
1823
  /**
@@ -1513,18 +1832,18 @@ export class WebTransport {
1513
1832
  * gone.
1514
1833
  */
1515
1834
  openStream(id: i64, bidirectional: boolean): i64 {
1516
- const s: i32 = this.openSession(id)
1517
- if (s < 0) {
1835
+ const w: WebTransportSession | null = this.openSession(id)
1836
+ if (w === null) {
1518
1837
  return toI64(H3_CLOSED)
1519
1838
  }
1520
- if (this.streamCount[s] >= this.config.maxStreams) {
1839
+ if (w.streams >= this.config.maxStreams) {
1521
1840
  return toI64(WT_LIMIT)
1522
1841
  }
1523
1842
  const stream: i64 = this.h3.openStream(id, bidirectional)
1524
1843
  if (stream < 0) {
1525
1844
  return stream
1526
1845
  }
1527
- this.link(s, stream, bidirectional ? WT_ZERO : WT_RECEIVED)
1846
+ this.link(w, stream, bidirectional ? WT_ZERO : WT_RECEIVED)
1528
1847
  return stream
1529
1848
  }
1530
1849
 
@@ -1543,16 +1862,16 @@ export class WebTransport {
1543
1862
  */
1544
1863
  write(id: i64, buf: u8[], off: i32, len: i32, fin: boolean): i32 {
1545
1864
  this.current()
1546
- const k: i32 = this.streamSlot(id)
1547
- if (k < 0) {
1865
+ const t: WebTransportStream | null = this.streamOf(id)
1866
+ if (t === null) {
1548
1867
  return H3_CLOSED
1549
1868
  }
1550
1869
  const n: i32 = this.h3.writeStream(id, buf, off, len, fin)
1551
1870
  if (n >= 0) {
1552
- this.touch(k)
1871
+ this.touch(t)
1553
1872
  }
1554
1873
  if (fin && n === len) {
1555
- this.settle(k, WT_SENT)
1874
+ this.settle(t, WT_SENT)
1556
1875
  }
1557
1876
  return n
1558
1877
  }
@@ -1565,15 +1884,15 @@ export class WebTransport {
1565
1884
  */
1566
1885
  resetStream(id: i64, code: i64): i32 {
1567
1886
  this.current()
1568
- const k: i32 = this.streamSlot(id)
1569
- if (k < 0) {
1887
+ const t: WebTransportStream | null = this.streamOf(id)
1888
+ if (t === null) {
1570
1889
  return H3_CLOSED
1571
1890
  }
1572
1891
  const result: i32 = this.h3.resetStream(id, wtCodeToHttp3(code))
1573
1892
  if (result !== 0) {
1574
1893
  return result
1575
1894
  }
1576
- this.settle(k, WT_SENT)
1895
+ this.settle(t, WT_SENT)
1577
1896
  return 0
1578
1897
  }
1579
1898
 
@@ -1587,15 +1906,15 @@ export class WebTransport {
1587
1906
  */
1588
1907
  stopSending(id: i64, code: i64): i32 {
1589
1908
  this.current()
1590
- const k: i32 = this.streamSlot(id)
1591
- if (k < 0 || (this.streamBits[k] & WT_RECEIVED) !== 0) {
1909
+ const t: WebTransportStream | null = this.streamOf(id)
1910
+ if (t === null || (t.bits & WT_RECEIVED) !== 0) {
1592
1911
  return H3_CLOSED
1593
1912
  }
1594
1913
  const result: i32 = this.h3.stopStream(id, wtCodeToHttp3(code))
1595
1914
  if (result !== 0) {
1596
1915
  return result
1597
1916
  }
1598
- this.settle(k, WT_RECEIVED)
1917
+ this.settle(t, WT_RECEIVED)
1599
1918
  return 0
1600
1919
  }
1601
1920
 
@@ -1609,10 +1928,10 @@ export class WebTransport {
1609
1928
  * @returns The most payload bytes one datagram may carry now, 0 or more.
1610
1929
  */
1611
1930
  maxDatagramPayload(id: i64): i32 {
1612
- if (this.openSession(id) < 0) {
1931
+ if (this.openSession(id) === null) {
1613
1932
  return WT_ZERO
1614
1933
  }
1615
- const room: i32 = this.quic.maxDatagramPayload() - quicVarintSize(id >> toI64(2))
1934
+ const room: i32 = wtSub(this.quic.maxDatagramPayload(), quicVarintSize(id >> toI64(2)))
1616
1935
  return room > 0 ? room : WT_ZERO
1617
1936
  }
1618
1937
 
@@ -1633,21 +1952,21 @@ export class WebTransport {
1633
1952
  */
1634
1953
  sendDatagram(id: i64, buf: u8[], off: i32, len: i32): i32 {
1635
1954
  h3CheckWindow("WebTransport.sendDatagram", buf, off, len)
1636
- const s: i32 = this.openSession(id)
1637
- if (s < 0) {
1955
+ const w: WebTransportSession | null = this.openSession(id)
1956
+ if (w === null) {
1638
1957
  return H3_CLOSED
1639
1958
  }
1640
1959
  const out: u8[] = this.outgoing
1641
1960
  const p: i32 = h3PutVarint(out, WT_ZERO, toI32(out.length), id >> toI64(2))
1642
1961
  // The quarter stream ID and the payload must fit what QUIC sends now (`maxDatagramPayload`, without the ID's room).
1643
- if (p < 0 || len > this.quic.maxDatagramPayload() - p || p + len > toI32(out.length)) {
1962
+ if (p < 0 || len > wtSub(this.quic.maxDatagramPayload(), p) || !tryCopyBytes(out, p, buf, off, len)) {
1644
1963
  return H3_TOO_LARGE
1645
1964
  }
1646
- quicPacketCopy(out, p, buf, off, len)
1647
- const result: i32 = this.quic.sendDatagram(out, WT_ZERO, p + len)
1965
+ const result: i32 = this.quic.sendDatagram(out, WT_ZERO, wtAdd(p, len))
1648
1966
  if (result === QUIC_DATAGRAM_OK) {
1649
- this.lastActive[s] = this.now
1650
- this.datagramsOut[s] = h3Count(this.datagramsOut[s])
1967
+ w.lastActive = this.now
1968
+ w.datagramsOut = h3Count(w.datagramsOut)
1969
+ this.publish(w)
1651
1970
  return 0
1652
1971
  }
1653
1972
  return result === QUIC_DATAGRAM_ERR_FULL ? H3_AGAIN : H3_TOO_LARGE