nish 0.36.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/docs/AI.md +43 -4
  2. package/docs/INSTALL.md +67 -58
  3. package/package.json +9 -5
  4. package/runtime/async.d.mts +2 -0
  5. package/runtime/async.mjs +7 -0
  6. package/runtime/nish.d.ts +736 -499
  7. package/runtime/nish.h +25 -0
  8. package/runtime/page.d.ts +53 -0
  9. package/runtime/page.mjs +146 -0
  10. package/runtime/runtime-async.c +557 -0
  11. package/runtime/runtime-wasm.c +63 -7
  12. package/scripts/build.sh +147 -5
  13. package/scripts/fuzz.sh +1 -0
  14. package/scripts/undocumented.txt +14 -0
  15. package/std/README.md +107 -82
  16. package/std/bytes.ts +23 -19
  17. package/std/compress/checksum.ts +1 -1
  18. package/std/crypto/aes.ts +48 -43
  19. package/std/crypto/base64url.ts +7 -5
  20. package/std/crypto/bcrypt-batch.ts +13 -13
  21. package/std/crypto/bcrypt.ts +33 -27
  22. package/std/crypto/chacha20poly1305.ts +16 -14
  23. package/std/crypto/ed25519.ts +19 -16
  24. package/std/crypto/hmac.ts +7 -6
  25. package/std/crypto/jwt.ts +23 -20
  26. package/std/crypto/p256.ts +63 -56
  27. package/std/crypto/p384.ts +23 -20
  28. package/std/crypto/pki.ts +18 -15
  29. package/std/crypto/rsa.ts +23 -19
  30. package/std/crypto/x25519.ts +11 -10
  31. package/std/crypto/x509.ts +69 -62
  32. package/std/host/ring.ts +124 -0
  33. package/std/json-equal.ts +12 -11
  34. package/std/json-number.ts +12 -12
  35. package/std/json-tokenizer.ts +26 -22
  36. package/std/json-typed.ts +92 -78
  37. package/std/json-value.ts +38 -33
  38. package/std/json.ts +32 -28
  39. package/std/net/fetch.ts +1117 -230
  40. package/std/net/handler.ts +18 -0
  41. package/std/net/hpack.ts +29 -26
  42. package/std/net/http-compress.ts +27 -21
  43. package/std/net/http1-client-codec.ts +29 -25
  44. package/std/net/http1-codec.ts +44 -87
  45. package/std/net/http1-server.ts +66 -54
  46. package/std/net/http2-frame.ts +17 -16
  47. package/std/net/http2-tls.ts +17 -15
  48. package/std/net/http2.ts +67 -54
  49. package/std/net/http3-frame.ts +11 -10
  50. package/std/net/http3-server.ts +36 -35
  51. package/std/net/http3.ts +70 -58
  52. package/std/net/postgres.ts +178 -160
  53. package/std/net/prefork.ts +38 -34
  54. package/std/net/qpack.ts +15 -14
  55. package/std/net/quic-conn-ack.ts +1 -0
  56. package/std/net/quic-conn-cid.ts +11 -0
  57. package/std/net/quic-conn-params.ts +41 -14
  58. package/std/net/quic-datagram.ts +7 -0
  59. package/std/net/quic-frame.ts +48 -12
  60. package/std/net/quic-listener.ts +61 -44
  61. package/std/net/quic-packet.ts +71 -52
  62. package/std/net/quic-recovery.ts +59 -21
  63. package/std/net/quic-stream.ts +83 -55
  64. package/std/net/quic.ts +204 -152
  65. package/std/net/tls/client.ts +1 -0
  66. package/std/net/tls/codec.ts +81 -12
  67. package/std/net/tls/record-client.ts +30 -18
  68. package/std/net/tls/record-server.ts +74 -46
  69. package/std/net/tls/record.ts +33 -24
  70. package/std/net/tls/ticket.ts +2 -0
  71. package/std/net/tls/verify.ts +8 -7
  72. package/std/net/tls-tcp.ts +58 -50
  73. package/std/net/tls.ts +55 -43
  74. package/std/net/url.ts +12 -9
  75. package/std/net/websocket.ts +23 -19
  76. package/std/net/webtransport.ts +24 -18
  77. package/std/random.ts +8 -6
  78. package/std/regex.ts +70 -59
  79. package/std/sync/ring.ts +24 -21
  80. package/std/sync/shm.ts +9 -7
  81. package/std/term-io.ts +17 -14
  82. package/std/term-probe.ts +11 -10
  83. package/std/term-rate.ts +71 -61
  84. package/std/term.ts +73 -59
  85. package/std/text.ts +11 -10
  86. package/std/threads.ts +19 -15
  87. package/std/yaml-json.ts +17 -15
  88. package/std/yaml-write.ts +21 -14
@@ -41,36 +41,39 @@
41
41
  * a callback. With `serviceWorker` set, every other worker gets a pair to that
42
42
  * worker. The ends go through `spawnWithFds`, from descriptor 3 in a fixed
43
43
  * order: the supervisor channel, then a worker's service channel, or in the
44
- * service worker its end to each other worker by index, skipping its own. Where
45
- * they landed rides in `--nish-prefork-channels=` just before the worker flag,
46
- * and `preforkSupervisorChannel`, `preforkServiceChannel` and
47
- * `preforkServiceChannelOf` read it back. A pair cannot be handed to a worker
48
- * already running, so service channels make the workers one group: when any of
49
- * them exits, the supervisor sends the rest SIGTERM (SIGKILL after `graceMs`),
50
- * and once none is left, starts them all again on fresh pairs, after the
51
- * backoff of the one that exited first. No worker keeps an end whose peer is
52
- * gone longer than that. With `restart` off nothing is signalled; a worker sees
53
- * its peer's exit as a hang-up.
44
+ * service worker its end to each other worker by index, skipping its own.
54
45
  *
55
- * **Listeners and pinning.** With `listenPort` set (0 or above), the
56
- * supervisor binds one `SO_REUSEPORT` listener per worker itself, in index
57
- * order, keeps them for its whole run and hands listener `k` to every start of
58
- * worker `k` after the channel ends; `preforkListener(args)` answers it in the
59
- * worker (`--nish-prefork-listener=`, before the channels flag), so a worker
60
- * serves that descriptor instead of binding its own. Port 0 binds the first
61
- * listener anywhere and the rest to its port. With `pin` on as well, each
62
- * listener steers by CPU (`tcpListenFlags`' flag 4): the kernel hands a
63
- * connection that arrived on CPU `k` to listener `k`, and a worker that is
64
- * restarted takes the same listener, so the order holds across restarts,
65
- * which it would not if each worker bound its own. `pin` alone makes each
66
- * worker call `setCpuAffinity(k)` before `preforkWith` answers it, so worker
67
- * `k` runs on CPU `k` only; a worker the kernel refuses (CPU `k` outside the
68
- * process's mask) says so on stderr and runs unpinned. More workers than
69
- * `availableParallelism()` is refused with a panic, since a worker past the
70
- * last CPU would be pinned nowhere and, steered, handed nothing. Pinning and
71
- * steering are Linux's: elsewhere the supervisor says once that the workers
72
- * run unpinned, and the listeners are plain `SO_REUSEPORT` ones. Both are off
73
- * by default.
46
+ * Where they landed rides in `--nish-prefork-channels=` just before the worker
47
+ * flag, and `preforkSupervisorChannel`, `preforkServiceChannel` and
48
+ * `preforkServiceChannelOf` read it back. A pair cannot be handed to a worker
49
+ * already running, so service channels make the workers one group: when any of
50
+ * them exits, the supervisor sends the rest SIGTERM (SIGKILL after `graceMs`),
51
+ * and once none is left, starts them all again on fresh pairs, after the
52
+ * backoff of the one that exited first. No worker keeps an end whose peer is
53
+ * gone longer than that. With `restart` off nothing is signalled; a worker sees
54
+ * its peer's exit as a hang-up.
55
+ *
56
+ * **Listeners and pinning.** With `listenPort` set (0 or above), the supervisor
57
+ * binds one `SO_REUSEPORT` listener per worker itself, in index order, keeps
58
+ * them for its whole run and hands listener `k` to every start of worker `k`
59
+ * after the channel ends; `preforkListener(args)` answers it in the worker
60
+ * (`--nish-prefork-listener=`, before the channels flag), so a worker serves
61
+ * that descriptor instead of binding its own. Port 0 binds the first listener
62
+ * anywhere and the rest to its port.
63
+ *
64
+ * With `pin` on as well, each listener steers by CPU (`tcpListenFlags`' flag
65
+ * 4): the kernel hands a connection that arrived on CPU `k` to listener `k`,
66
+ * and a worker that is restarted takes the same listener, so the order holds
67
+ * across restarts, which it would not if each worker bound its own. `pin` alone
68
+ * makes each worker call `setCpuAffinity(k)` before `preforkWith` answers it,
69
+ * so worker `k` runs on CPU `k` only; a worker the kernel refuses (CPU `k`
70
+ * outside the process's mask) says so on stderr and runs unpinned.
71
+ *
72
+ * More workers than `availableParallelism()` is refused with a panic, since a
73
+ * worker past the last CPU would be pinned nowhere and, steered, handed
74
+ * nothing. Pinning and steering are Linux's: elsewhere the supervisor says once
75
+ * that the workers run unpinned, and the listeners are plain `SO_REUSEPORT`
76
+ * ones. Both are off by default.
74
77
  *
75
78
  * **How many.** `NISH_WORKERS` when it is a positive whole number, else
76
79
  * `PreforkConfig.workers` when that is positive, else
@@ -908,11 +911,12 @@ export const preforkWith = (args: string[], config: PreforkConfig): i32 => {
908
911
  * error. The callback reads what is waiting, since an end left readable wakes
909
912
  * the loop again at once, and may write back, but never closes `fd`: the
910
913
  * supervisor closes it after a hang-up, when the worker is reaped and when it
911
- * stops, and a restarted worker comes with a new one. Panics, besides, when
912
- * `serviceWorker` is not below the worker count or the count is above 64 with
913
- * a service worker (63 with `listenPort`), the most `spawnWithFds` hands one
914
- * child; when `pin`, on Linux, asks for more workers than
915
- * `availableParallelism()`; and, through `preforkInvalid`, on seqpacket
914
+ * stops, and a restarted worker comes with a new one.
915
+ *
916
+ * Panics, besides, when `serviceWorker` is not below the worker count or the
917
+ * count is above 64 with a service worker (63 with `listenPort`), the most
918
+ * `spawnWithFds` hands one child; when `pin`, on Linux, asks for more workers
919
+ * than `availableParallelism()`; and, through `preforkInvalid`, on seqpacket
916
920
  * channels off Linux. A listener that cannot bind answers
917
921
  * `PREFORK_SPAWN_FAILED` before any worker starts.
918
922
  *
package/std/net/qpack.ts CHANGED
@@ -31,20 +31,21 @@
31
31
  * block on (§2.1.2) and nothing to acknowledge (§4.4.1).
32
32
  *
33
33
  * **What is refused.** `QpackDecoder.decode` answers QPACK_DECOMPRESSION_FAILED
34
- * for a section that ends inside a representation, an integer past 2^62 - 1
35
- * (or padded past the nine continuation bytes such an integer needs), a
36
- * non-zero Required Insert Count (§4.5.1.1), a negative Base (§4.5.1.2), any
37
- * reference to the dynamic table, relative or post-Base (§2.2.3), a static
38
- * index past 98, and Huffman padding that is not EOS's prefix or an EOS inside
39
- * a string. `receiveEncoderStream` answers QPACK_ENCODER_STREAM_ERROR for a Set
40
- * Dynamic Table Capacity above 0 (§4.3.1), any Insert (§3.2.2: every entry is
41
- * larger than the table) and any Duplicate (§2.2.3: there is nothing to
42
- * duplicate). The encoder's `receiveDecoderStream` answers
43
- * QPACK_DECODER_STREAM_ERROR for a Section Acknowledgment (§4.4.1: no section
44
- * it sent needs one), any Insert Count Increment (§4.4.3: it has inserted
45
- * nothing) and a stream ID past 2^62 - 1. Each of those is a connection error,
46
- * so each is sticky: the same answer comes back from every later call without
47
- * reading anything, and `reason` says which rule it was (`QPACK_REASON_*`).
34
+ * for a section that ends inside a representation, an integer past 2^62 - 1 (or
35
+ * padded past the nine continuation bytes such an integer needs), a non-zero
36
+ * Required Insert Count (§4.5.1.1), a negative Base (§4.5.1.2), any reference
37
+ * to the dynamic table, relative or post-Base (§2.2.3), a static index past 98,
38
+ * and Huffman padding that is not EOS's prefix or an EOS inside a string.
39
+ *
40
+ * `receiveEncoderStream` answers QPACK_ENCODER_STREAM_ERROR for a Set Dynamic
41
+ * Table Capacity above 0 (§4.3.1), any Insert (§3.2.2: every entry is larger
42
+ * than the table) and any Duplicate (§2.2.3: there is nothing to duplicate).
43
+ * The encoder's `receiveDecoderStream` answers QPACK_DECODER_STREAM_ERROR for a
44
+ * Section Acknowledgment (§4.4.1: no section it sent needs one), any Insert
45
+ * Count Increment (§4.4.3: it has inserted nothing) and a stream ID past
46
+ * 2^62 - 1. Each of those is a connection error, so each is sticky: the same answer
47
+ * comes back from every later call without reading anything, and `reason` says
48
+ * which rule it was (`QPACK_REASON_*`).
48
49
  *
49
50
  * **What is accepted.** A Set Dynamic Table Capacity of 0 on the encoder
50
51
  * stream, and a Stream Cancellation on the decoder stream, read a byte at a
@@ -47,6 +47,7 @@ export class QuicAckRanges {
47
47
  /** Every packet number at or below this is answered as already received; -1 until a range is dropped. */
48
48
  floor: i64 = -1
49
49
  private ranges: i64[]
50
+ /** How many disjoint ranges are held, at most `QUIC_ACK_MAX_RANGES`. */
50
51
  count: i32 = 0
51
52
  /** Whether an ack-eliciting packet arrived since the last `pushAck`. */
52
53
  ackPending: boolean = false
@@ -60,12 +60,17 @@ const QUIC_CID_ZERO: i32 = 0
60
60
  * says whether the slot holds an ID at all.
61
61
  */
62
62
  export class QuicCidEntry {
63
+ /** The ID's sequence number, as issued in NEW_CONNECTION_ID; 0 for the ID of the handshake. */
63
64
  sequence: i64 = 0
65
+ /** @internal Read by `net/quic.ts` and `net/http3-server.ts`. */
64
66
  cid: u8[]
65
67
  /** The stateless reset token issued with it (RFC 9000 §10.3); `hasToken` is false for a peer's sequence 0. */
66
68
  resetToken: u8[]
69
+ /** How many bytes of `cid` the ID takes, 0 to 20. */
67
70
  length: i32 = 0
71
+ /** @internal Read by `net/quic.ts` and `net/http3-server.ts`. */
68
72
  used: boolean = false
73
+ /** @internal Read only by this module's table. */
69
74
  hasToken: boolean = false
70
75
  /** For a local ID: whether a NEW_CONNECTION_ID frame announced it yet. Sequence 0 needs none. */
71
76
  announced: boolean = false
@@ -148,7 +153,12 @@ const quicCidSlots = (count: i32): QuicCidEntry[] => {
148
153
  * `active_connection_id_limit` this side advertised, which bounds the peer's.
149
154
  */
150
155
  export class QuicCidTable {
156
+ /** The slots for the IDs this side issued, so a caller can walk them, used or not. */
151
157
  local: QuicCidEntry[]
158
+ /**
159
+ * The slots for the IDs the peer issued, used or not, as `limit` bounds them; a caller walks
160
+ * them to see each ID and its reset token.
161
+ */
152
162
  peer: QuicCidEntry[]
153
163
  /** Peer sequence numbers this side must send RETIRE_CONNECTION_ID for, oldest first, as a ring of `retireCount` from `retireHead`. */
154
164
  private retirePending: i64[]
@@ -160,6 +170,7 @@ export class QuicCidTable {
160
170
  /** The index in `peer` of the ID this side sends to, or -1 before the peer gave one. */
161
171
  private current: i32 = -1
162
172
  private retireHead: i32 = 0
173
+ /** How many RETIRE_CONNECTION_ID frames this side still owes the peer, at most twice `limit`. */
163
174
  retireCount: i32 = 0
164
175
 
165
176
  /**
@@ -20,20 +20,23 @@
20
20
  * says it then has. The encoder writes a parameter only when it differs from
21
21
  * that default, plus the connection IDs, which have none.
22
22
  *
23
- * **What a parse refuses.** Each of these is a TRANSPORT_PARAMETER_ERROR
24
- * (§7.4, §18.2): an ID or a length that does not fit, a parameter running past the bytes, a
25
- * known parameter sent twice, a varint-valued parameter whose value is not
26
- * exactly one varint, `disable_active_migration` with a value, a server-only
27
- * parameter from a client (`original_destination_connection_id`,
28
- * `preferred_address`, `retry_source_connection_id`,
29
- * `stateless_reset_token`), a stateless reset token that is not 16 bytes, a
30
- * connection ID over 20 bytes, `max_udp_payload_size` below 1200,
31
- * `ack_delay_exponent` above 20, `max_ack_delay` at 2^14 or above,
32
- * `active_connection_id_limit` below 2, and a stream count above 2^60.
33
- * Unknown parameters, the reserved ones (31 × N + 27) among them, are
34
- * skipped, as §18.1 asks; a second copy of an unknown one is not noticed.
35
- * Whether `initial_source_connection_id` is present and right is the
36
- * connection's check, because it needs the packet the ID came in.
23
+ * **What a parse refuses.**
24
+ *
25
+ * Each of these is a TRANSPORT_PARAMETER_ERROR (§7.4, §18.2): an ID or a length
26
+ * that does not fit, a parameter running past the bytes, a known parameter sent
27
+ * twice, a varint-valued parameter whose value is not exactly one varint,
28
+ * `disable_active_migration` with a value, a server-only parameter from a
29
+ * client (`original_destination_connection_id`, `preferred_address`,
30
+ * `retry_source_connection_id`, `stateless_reset_token`), a stateless reset
31
+ * token that is not 16 bytes, a connection ID over 20 bytes,
32
+ * `max_udp_payload_size` below 1200, `ack_delay_exponent` above 20,
33
+ * `max_ack_delay` at 2^14 or above, `active_connection_id_limit` below 2, and a
34
+ * stream count above 2^60.
35
+ *
36
+ * Unknown parameters, the reserved ones (31 × N + 27) among them, are skipped,
37
+ * as §18.1 asks; a second copy of an unknown one is not noticed. Whether
38
+ * `initial_source_connection_id` is present and right is the connection's
39
+ * check, because it needs the packet the ID came in.
37
40
  *
38
41
  * **Provenance.** Written from RFC 9000 §18, in this module's own structure; nothing here is
39
42
  * ported from another implementation. Private names carry the
@@ -131,31 +134,55 @@ const QUIC_PARAMS_MAX_STREAMS: i64 = 1073741824 * 1073741824
131
134
  * frame).
132
135
  */
133
136
  export class QuicTransportParameters {
137
+ /** What the last parse answered: `QUIC_ERROR_NO_ERROR`, or `QUIC_ERROR_TRANSPORT_PARAMETER` with `errorParameter` set. */
134
138
  error: i64 = 0
139
+ /** `max_idle_timeout`, in milliseconds; 0, the default, for no idle timeout. */
135
140
  maxIdleTimeout: i64 = 0
141
+ /** `max_udp_payload_size`: the largest UDP payload the sender takes, at least 1200 bytes; 65527 by default. */
136
142
  maxUdpPayloadSize: i64 = 65527
143
+ /** `initial_max_data`: the connection's first receive credit, in bytes, across every stream. */
137
144
  initialMaxData: i64 = 0
145
+ /** `initial_max_stream_data_bidi_local`: the first credit, in bytes, on a bidirectional stream the sender opens. */
138
146
  initialMaxStreamDataBidiLocal: i64 = 0
147
+ /** `initial_max_stream_data_bidi_remote`: the first credit, in bytes, on a bidirectional stream its peer opens. */
139
148
  initialMaxStreamDataBidiRemote: i64 = 0
149
+ /** `initial_max_stream_data_uni`: the first credit, in bytes, on a unidirectional stream its peer opens. */
140
150
  initialMaxStreamDataUni: i64 = 0
151
+ /** `initial_max_streams_bidi`: how many bidirectional streams the peer may open at first, at most 2^60. */
141
152
  initialMaxStreamsBidi: i64 = 0
153
+ /** `initial_max_streams_uni`: how many unidirectional streams the peer may open at first, at most 2^60. */
142
154
  initialMaxStreamsUni: i64 = 0
155
+ /** `ack_delay_exponent`: the power of two an ACK frame's delay is scaled by, at most 20; 3 by default. */
143
156
  ackDelayExponent: i64 = 3
157
+ /** `max_ack_delay`, in milliseconds: how long the sender may hold an acknowledgement, below 2^14; 25 by default. */
144
158
  maxAckDelay: i64 = 25
159
+ /** `active_connection_id_limit`: how many of its peer's connection IDs the sender keeps, at least 2. */
145
160
  activeConnectionIdLimit: i64 = 2
146
161
  /** RFC 9221's `max_datagram_frame_size`: 0, the default, for no DATAGRAM frames. */
147
162
  maxDatagramFrameSize: i64 = 0
163
+ /** The ID of the parameter a failed parse is about, or -1 when the bytes did not frame; read beside `error`. */
148
164
  errorParameter: i64 = 0
165
+ /** `original_destination_connection_id`: the client's first Destination Connection ID; server only. */
149
166
  originalDcid: u8[]
167
+ /** `stateless_reset_token`: 16 bytes that end a stateless reset for the server's connection ID; server only. */
150
168
  statelessResetToken: u8[]
169
+ /** `preferred_address`, kept as the bytes the server sent, since this side never acts on it; server only. */
151
170
  preferredAddress: u8[]
171
+ /** `initial_source_connection_id`: the Source Connection ID of the sender's first Initial packet. */
152
172
  initialScid: u8[]
173
+ /** `retry_source_connection_id`: the Source Connection ID of the server's Retry; server only. */
153
174
  retryScid: u8[]
175
+ /** Whether `originalDcid` was set or read; an empty ID is a value, so its length cannot say. */
154
176
  hasOriginalDcid: boolean = false
177
+ /** Whether `statelessResetToken` was set or read. */
155
178
  hasStatelessResetToken: boolean = false
179
+ /** Whether `preferredAddress` was set or read. */
156
180
  hasPreferredAddress: boolean = false
181
+ /** Whether `initialScid` was set or read; an empty ID is a value, so its length cannot say. */
157
182
  hasInitialScid: boolean = false
183
+ /** Whether `retryScid` was set or read; an empty ID is a value, so its length cannot say. */
158
184
  hasRetryScid: boolean = false
185
+ /** `disable_active_migration`: the sender does not support active connection migration (RFC 9000 §9) from the address the handshake used. */
159
186
  disableActiveMigration: boolean = false
160
187
 
161
188
  /**
@@ -33,9 +33,16 @@ import { quicPacketCopy } from "nish/net/quic-packet"
33
33
  export class QuicDatagramQueue {
34
34
  /** Entry `k`'s bytes are `data[k * entrySize ..]`, `lengths[k]` of them. */
35
35
  data: u8[]
36
+ /** Each entry's payload length in bytes, 0 to `entrySize`; entry `k`'s is `lengths[k]`. */
36
37
  lengths: i32[]
38
+ /**
39
+ * The largest payload an entry holds, in bytes, fixed when the ring is made; it is also the
40
+ * stride of `data`. A longer datagram is refused and counted in `dropped`.
41
+ */
37
42
  entrySize: i32 = 0
43
+ /** The index of the oldest entry; the `j`-th oldest is entry `(head + j) % capacity()`. */
38
44
  head: i32 = 0
45
+ /** How many datagrams wait in the ring, from 0 to its entry count. */
39
46
  count: i32 = 0
40
47
  /** Datagrams refused for want of room or for their size, which the peer's side counts as dropped. */
41
48
  dropped: i32 = 0
@@ -14,18 +14,21 @@
14
14
  * at = frame.end;
15
15
  * }
16
16
  *
17
- * **Coverage.** Every frame type of RFC 9000 is read, 0x00 to 0x1e, and RFC 9221's
18
- * DATAGRAM, 0x30 and 0x31, so a type outside those is the one parse error a
19
- * well-formed packet cannot produce. What
20
- * a frame *means* is `nish/net/quic`'s: this module checks only what §19 says
21
- * of the encoding itself, and answers the transport error the RFC names for
22
- * each break — `QUIC_ERROR_FRAME_ENCODING` for a truncated field, a length
23
- * past the packet, an ACK range below packet number zero, an offset past
24
- * 2^62 − 1, a stream count past 2^60, an empty NEW_TOKEN, a connection ID
25
- * outside 1 to 20 bytes or a Retire Prior To above its own sequence number;
26
- * `QUIC_ERROR_PROTOCOL_VIOLATION` for a frame type spelled in more bytes than
27
- * it needs (§12.4). Every read is bounds-checked against the window the caller
28
- * gives, so nothing a peer sends can make this module panic.
17
+ * **Coverage.** Every frame type of RFC 9000 is read, 0x00 to 0x1e, and RFC
18
+ * 9221's DATAGRAM, 0x30 and 0x31, so a type outside those is the one parse
19
+ * error a well-formed packet cannot produce.
20
+ *
21
+ * What a frame *means* is `nish/net/quic`'s: this module checks only what §19
22
+ * says of the encoding itself, and answers the transport error the RFC names
23
+ * for each break — `QUIC_ERROR_FRAME_ENCODING` for a truncated field, a length
24
+ * past the packet, an ACK range below packet number zero, an offset past 2^62 −
25
+ * 1, a stream count past 2^60, an empty NEW_TOKEN, a connection ID outside 1 to
26
+ * 20 bytes or a Retire Prior To above its own sequence number;
27
+ * `QUIC_ERROR_PROTOCOL_VIOLATION` for a frame type spelled in more bytes than
28
+ * it needs (§12.4).
29
+ *
30
+ * Every read is bounds-checked against the window the caller gives, so nothing
31
+ * a peer sends can make this module panic.
29
32
  *
30
33
  * **No copy of the bulk.** A CRYPTO, STREAM or DATAGRAM frame's data, a
31
34
  * NEW_TOKEN's token, a PATH_CHALLENGE's eight bytes and a CONNECTION_CLOSE's
@@ -208,29 +211,62 @@ export const QUIC_PATH_DATA_SIZE: i32 = 8
208
211
  * phrase as the data.
209
212
  */
210
213
  export class QuicFrame {
214
+ /** The stream a STREAM, RESET_STREAM, STOP_SENDING, MAX_STREAM_DATA or STREAM_DATA_BLOCKED frame names. */
211
215
  streamId: i64 = 0
216
+ /** The byte offset in the stream (STREAM) or the handshake stream (CRYPTO) where the data starts. */
212
217
  offset: i64 = 0
218
+ /** The frame's one number: a limit, a final size or a sequence number, as the list above gives per type. */
213
219
  value: i64 = 0
220
+ /** The application or transport error code of a RESET_STREAM, STOP_SENDING or CONNECTION_CLOSE. */
214
221
  errorCode: i64 = 0
222
+ /** The frame type a transport CONNECTION_CLOSE (0x1c) blames; 0 for the application form. */
215
223
  frameType: i64 = 0
224
+ /** A NEW_CONNECTION_ID's Retire Prior To: every peer ID with a lower sequence number is to be retired. */
216
225
  retirePriorTo: i64 = 0
226
+ /** The largest packet number an ACK acknowledges. */
217
227
  largest: i64 = 0
228
+ /** An ACK's delay as sent, not yet scaled by the peer's `ack_delay_exponent`. */
218
229
  ackDelay: i64 = 0
230
+ /**
231
+ * An ACK's ranges as `[smallest, largest]` packet-number pairs, highest
232
+ * first. Only the first `ackRangeCount` pairs are meaningful; the array
233
+ * keeps its `QUIC_FRAME_MAX_ACK_RANGES * 2` length.
234
+ */
219
235
  ackRanges: i64[]
236
+ /**
237
+ * How many ranges the ACK carried, which can exceed `ackRangeCount` when ranges past the stored cap
238
+ * were read and dropped.
239
+ */
220
240
  ackRangeTotal: i64 = 0
241
+ /** An ACK_ECN's cumulative count of packets received marked ECT(0). */
221
242
  ect0: i64 = 0
243
+ /** An ACK_ECN's cumulative count of packets received marked ECT(1). */
222
244
  ect1: i64 = 0
245
+ /** An ACK_ECN's cumulative count of packets received marked ECN-CE (congestion experienced). */
223
246
  ce: i64 = 0
224
247
  /** The frame type, with every STREAM type answered as `QUIC_FRAME_STREAM`. */
225
248
  type: i32 = 0
226
249
  /** One past the frame's last byte, where the next frame starts. */
227
250
  end: i32 = 0
251
+ /**
252
+ * Where the frame's data (stream or CRYPTO bytes, token, path data, datagram or reason phrase)
253
+ * starts in the payload.
254
+ */
228
255
  dataStart: i32 = 0
256
+ /** How many bytes of data start at `dataStart`. */
229
257
  dataLength: i32 = 0
258
+ /**
259
+ * How many `[smallest, largest]` pairs of `ackRanges` hold an ACK range, at most
260
+ * `QUIC_FRAME_MAX_ACK_RANGES`.
261
+ */
230
262
  ackRangeCount: i32 = 0
263
+ /** Where a NEW_CONNECTION_ID's connection ID starts in the payload. */
231
264
  connectionIdStart: i32 = 0
265
+ /** A NEW_CONNECTION_ID's connection ID length in bytes, 1 to 20. */
232
266
  connectionIdLength: i32 = 0
267
+ /** Where a NEW_CONNECTION_ID's 16-byte stateless reset token starts in the payload. */
233
268
  resetTokenStart: i32 = 0
269
+ /** A STREAM frame's FIN bit (the stream ends with this data), or a DATAGRAM that carried a Length. */
234
270
  fin: boolean = false
235
271
 
236
272
  /**
@@ -35,30 +35,34 @@
35
35
  * listener a packet for a connection that is still alive would give that
36
36
  * token, which ends the connection, to whoever sent it (RFC 9000 §21.11).
37
37
  *
38
- * **Retry tokens** (§8.1.4). They are this server's own format: a marker byte, the
39
- * time the token was issued, and the client's original Destination
38
+ * **Retry tokens** (§8.1.4). They are this server's own format: a marker byte,
39
+ * the time the token was issued, and the client's original Destination
40
40
  * Connection ID, then 16 bytes of HMAC-SHA256 under the configuration's
41
- * `retryTokenKey` over all of that, the Retry's Source Connection ID (which
42
- * the client's next Initial must be sent to) and the client's address as the
43
- * caller gives it, port included. So a token is good only from the address
44
- * it was sent to, only for the connection it was issued for, and only for
45
- * `retryTokenLifetime` milliseconds. A token that carries the marker and fails
46
- * any of that is an invalid Retry token, which closes the attempt with
47
- * INVALID_TOKEN (§8.1.2); a token without the marker, which some other
48
- * server issued, is treated as no token at all (§8.1.3).
41
+ * `retryTokenKey` over all of that, the Retry's Source Connection ID (which the
42
+ * client's next Initial must be sent to) and the client's address as the caller
43
+ * gives it, port included. So a token is good only from the address it was sent
44
+ * to, only for the connection it was issued for, and only for
45
+ * `retryTokenLifetime` milliseconds.
46
+ *
47
+ * A token that carries the marker and fails any of that is an invalid Retry
48
+ * token, which closes the attempt with INVALID_TOKEN (§8.1.2); a token without
49
+ * the marker, which some other server issued, is treated as no token at all
50
+ * (§8.1.3).
49
51
  *
50
52
  * **Stateless resets** (§10.3). They end in the token `quicStatelessResetToken`
51
- * derives (computed in place, by `quicConnResetTokenInto`) from the configuration's static key and the packet's connection
52
- * ID, so a server that restarted with the same key can end the connections
53
- * it lost. RFC 9000 §10.3 and §10.3.3 bound them, and each bound is held
54
- * here. A reset is sent only for a short header, and is always shorter than
55
- * the datagram that caused it, so two endpoints resetting each other shrink
56
- * to nothing; one byte shorter for a datagram of 43 bytes or fewer, and
57
- * never under 21 bytes, so a datagram of 21 or fewer gets none. That also
58
- * bounds amplification at less than one to one. They are rate limited:
59
- * `QUIC_LISTENER_RESET_BURST` at once, then one per
60
- * `QUIC_LISTENER_RESET_INTERVAL` milliseconds. Each reset's other bytes are
61
- * unpredictable, from the listener's own generator.
53
+ * derives (computed in place, by `quicConnResetTokenInto`) from the
54
+ * configuration's static key and the packet's connection ID, so a server that
55
+ * restarted with the same key can end the connections it lost. RFC 9000 §10.3
56
+ * and §10.3.3 bound them, and each bound is held here.
57
+ *
58
+ * A reset is sent only for a short header, and is always shorter than the
59
+ * datagram that caused it, so two endpoints resetting each other shrink to
60
+ * nothing; one byte shorter for a datagram of 43 bytes or fewer, and never
61
+ * under 21 bytes, so a datagram of 21 or fewer gets none. That also bounds
62
+ * amplification at less than one to one. They are rate limited:
63
+ * `QUIC_LISTENER_RESET_BURST` at once, then one per
64
+ * `QUIC_LISTENER_RESET_INTERVAL` milliseconds. Each reset's other bytes are
65
+ * unpredictable, from the listener's own generator.
62
66
  *
63
67
  * **Pacing** (RFC 9002 §7.7). It is the carrier's send loop, so it is here
64
68
  * beside the rest of what the carrier calls: `quicListenerTakePaced` takes a
@@ -78,18 +82,19 @@
78
82
  *
79
83
  * **GSO and GRO** (`nish:net`'s `UDP_SEGMENT` and `UDP_GRO`). A carrier that
80
84
  * sends with segmentation offload takes a whole flight at once:
81
- * `quicListenerTakeFlight` writes as many datagrams as the pacer has credit
82
- * for back to back into one buffer, every one `QUIC_CONN_DATAGRAM_SIZE`
83
- * bytes but the last, which is what one `udpSendTo` with that segment size
84
- * cuts back into datagrams; each is written in place, so a flight allocates
85
- * nothing. The flight is paced as a whole: it is as long as the pacer's
86
- * credit, and charged to it datagram by datagram, so a burst is never more
87
- * than the initial window however the carrier sends it. It starts only on a
88
- * millisecond's earning, up to four datagrams, so a fast connection's pacer,
89
- * which earns by the microsecond, does not let it out a datagram per send;
90
- * `quicListenerFlightTime` says when. On the receiving
91
- * side, `quicListenerReceiveSegments` hands a connection the datagrams of
92
- * one GRO receive, segment by segment, in place.
85
+ * `quicListenerTakeFlight` writes as many datagrams as the pacer has credit for
86
+ * back to back into one buffer, every one `QUIC_CONN_DATAGRAM_SIZE` bytes but
87
+ * the last, which is what one `udpSendTo` with that segment size cuts back into
88
+ * datagrams; each is written in place, so a flight allocates nothing.
89
+ *
90
+ * The flight is paced as a whole: it is as long as the pacer's credit, and
91
+ * charged to it datagram by datagram, so a burst is never more than the initial
92
+ * window however the carrier sends it. It starts only on a millisecond's
93
+ * earning, up to four datagrams, so a fast connection's pacer, which earns by
94
+ * the microsecond, does not let it out a datagram per send;
95
+ * `quicListenerFlightTime` says when. On the receiving side,
96
+ * `quicListenerReceiveSegments` hands a connection the datagrams of one GRO
97
+ * receive, segment by segment, in place.
93
98
  *
94
99
  * **ECN** (RFC 9000 §13.4). A carrier whose platform sends the ECN bits it
95
100
  * is given sets the connection's `ecnMarking` and sends each datagram with
@@ -223,10 +228,15 @@ export const QUIC_LISTENER_ANSWER_SIZE: i32 = QUIC_CONN_DATAGRAM_SIZE
223
228
  * with exactly the room its reply takes.
224
229
  */
225
230
  export class QuicListenerAnswer {
231
+ /** The datagram to send back to the peer's address; empty when the answer sends nothing. */
226
232
  reply: u8[]
233
+ /** After a Retry, the DCID of the client's first Initial, which `QuicConnection.acceptRetry` checks. */
227
234
  originalDcid: u8[]
235
+ /** After a Retry, the SCID the Retry chose, which `QuicConnection.acceptRetry` checks. */
228
236
  retryScid: u8[]
237
+ /** What to do with the datagram: a `QUIC_LISTEN_*`, `QUIC_LISTEN_DROP` after `wipe`. */
229
238
  kind: i32 = 0
239
+ /** Whether an accepted Initial carried a valid Retry token, so `originalDcid` and `retryScid` are set. */
230
240
  retried: boolean = false
231
241
  private room: i32 = 0
232
242
 
@@ -342,6 +352,7 @@ const quicListenerWindowFits = (length: i32, at: i32, len: i32): boolean =>
342
352
  */
343
353
  export class QuicListener {
344
354
  private config: QuicServerConfig
355
+ /** The 32 bytes of entropy every unpredictable byte is made from; `release` wipes it. */
345
356
  seed: u8[]
346
357
  /** Unpredictable bytes not yet used, from `poolHead`, and the counter the next block is made from. */
347
358
  pool: u8[]
@@ -357,8 +368,11 @@ export class QuicListener {
357
368
  private lengths: u8[]
358
369
  /** The Initial secrets of an INVALID_TOKEN close, and its keys, both directions. */
359
370
  initialClient: u8[]
371
+ /** The server's Initial secret of an INVALID_TOKEN close; `initialClient` is the client's. */
360
372
  initialServer: u8[]
373
+ /** The INVALID_TOKEN close's keys for reading the client's Initial. */
361
374
  readSlot: QuicKeysSlot
375
+ /** The INVALID_TOKEN close's keys for sealing the server's Initial. */
362
376
  writeSlot: QuicKeysSlot
363
377
  private packet: QuicPacket
364
378
  /** The client's SCID and the DCID it sent to, copied out for `quicPutLongHeader`, which reads IDs from 0. */
@@ -376,9 +390,11 @@ export class QuicListener {
376
390
  /** When the reset budget was last topped up (-1: never yet), and the stateless resets that may go now. */
377
391
  private resetRefilled: i64 = -1
378
392
  private resetBudget: i32 = 0
393
+ /** Where the unused bytes of `pool` start: `QUIC_LISTENER_POOL` when it is spent. */
379
394
  poolHead: i32 = 0
380
395
  /** Stateless resets sent, and ones the rate limit held back. */
381
396
  resetsSent: i32 = 0
397
+ /** Stateless resets the rate limit held back, which a log or test reads to see the limit working. */
382
398
  resetsLimited: i32 = 0
383
399
  /** Whether the configuration and entropy were usable. */
384
400
  private usable: boolean = false
@@ -992,17 +1008,18 @@ export class QuicFlight {
992
1008
 
993
1009
  /**
994
1010
  * Writes the next flight `conn` sends at `now` into `buf[at .. at + cap)`,
995
- * datagram after datagram, for one GSO send, and answers its length in
996
- * bytes (0 for none); `flight` says how many datagrams it holds and the
997
- * segment size. A flight starts only once the pacer has the credit
998
- * `quicListenerFlightTime` waits for, a millisecond's earning of one to four
999
- * datagrams; then each datagram goes only when the pacer has credit for a
1000
- * full one (RFC 9002 §7.7) and is charged at its real size, as
1001
- * `quicListenerTakePaced` does, so the flight is the pacer's whole credit
1002
- * and no more. It stops at the first datagram shorter than
1003
- * `QUIC_CONN_DATAGRAM_SIZE`, which can only be the last of a segmented send,
1004
- * at `QUIC_LISTENER_FLIGHT_MAX` datagrams, when `cap` has no room for
1005
- * another, and before a datagram whose ECN codepoint would not be the first's.
1011
+ * datagram after datagram, for one GSO send, and answers its length in bytes (0
1012
+ * for none); `flight` says how many datagrams it holds and the segment size.
1013
+ *
1014
+ * A flight starts only once the pacer has the credit `quicListenerFlightTime`
1015
+ * waits for, a millisecond's earning of one to four datagrams; then each
1016
+ * datagram goes only when the pacer has credit for a full one (RFC 9002 §7.7)
1017
+ * and is charged at its real size, as `quicListenerTakePaced` does, so the
1018
+ * flight is the pacer's whole credit and no more. It stops at the first
1019
+ * datagram shorter than `QUIC_CONN_DATAGRAM_SIZE`, which can only be the last
1020
+ * of a segmented send, at `QUIC_LISTENER_FLIGHT_MAX` datagrams, when `cap` has
1021
+ * no room for another, and before a datagram whose ECN codepoint would not be
1022
+ * the first's.
1006
1023
  *
1007
1024
  * @param conn The connection to send for.
1008
1025
  * @param now The caller's time in milliseconds.