@amritk/nish-aarch64-linux 0.35.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/INSTALL.md +73 -59
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +50 -4
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/nish.d.ts +4849 -430
  8. package/runtime/nish.h +25 -0
  9. package/runtime/page.d.ts +53 -0
  10. package/runtime/page.mjs +146 -0
  11. package/runtime/runtime-async.c +557 -0
  12. package/runtime/runtime-wasm.c +63 -7
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +252 -15
  16. package/std/README.md +107 -82
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +279 -59
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +128 -52
  28. package/std/crypto/base64url.ts +29 -5
  29. package/std/crypto/bcrypt-batch.ts +101 -34
  30. package/std/crypto/bcrypt.ts +93 -27
  31. package/std/crypto/chacha20poly1305.ts +69 -14
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +66 -16
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +242 -37
  36. package/std/crypto/jwt.ts +204 -72
  37. package/std/crypto/p256.ts +222 -72
  38. package/std/crypto/p384.ts +51 -23
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +99 -30
  41. package/std/crypto/rsa.ts +56 -22
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +163 -24
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +133 -66
  48. package/std/host/ring.ts +124 -0
  49. package/std/json-equal.ts +31 -11
  50. package/std/json-format.ts +112 -13
  51. package/std/json-number.ts +76 -26
  52. package/std/json-pointer.ts +23 -1
  53. package/std/json-tokenizer.ts +126 -51
  54. package/std/json-typed.ts +757 -96
  55. package/std/json-value.ts +438 -157
  56. package/std/json.ts +170 -38
  57. package/std/map.ts +25 -7
  58. package/std/math.ts +36 -4
  59. package/std/net/buffer-pool.ts +42 -11
  60. package/std/net/dns-config.ts +109 -23
  61. package/std/net/dns-message.ts +62 -7
  62. package/std/net/dns-resolver.ts +247 -53
  63. package/std/net/dns.ts +203 -35
  64. package/std/net/fetch.ts +1267 -290
  65. package/std/net/handler.ts +296 -80
  66. package/std/net/hpack.ts +290 -77
  67. package/std/net/http-compress.ts +152 -36
  68. package/std/net/http-fields.ts +85 -9
  69. package/std/net/http1-client-codec.ts +129 -44
  70. package/std/net/http1-codec.ts +279 -114
  71. package/std/net/http1-server.ts +409 -139
  72. package/std/net/http2-frame.ts +289 -51
  73. package/std/net/http2-tls.ts +152 -49
  74. package/std/net/http2.ts +314 -160
  75. package/std/net/http3-frame.ts +165 -27
  76. package/std/net/http3-server.ts +282 -131
  77. package/std/net/http3.ts +377 -173
  78. package/std/net/outbound.ts +37 -3
  79. package/std/net/postgres-carrier.ts +144 -8
  80. package/std/net/postgres.ts +898 -260
  81. package/std/net/prefork.ts +202 -65
  82. package/std/net/qpack.ts +120 -32
  83. package/std/net/quic-conn-ack.ts +60 -12
  84. package/std/net/quic-conn-cid.ts +135 -27
  85. package/std/net/quic-conn-params.ts +109 -22
  86. package/std/net/quic-datagram.ts +50 -3
  87. package/std/net/quic-frame.ts +333 -23
  88. package/std/net/quic-listener.ts +161 -75
  89. package/std/net/quic-packet.ts +373 -73
  90. package/std/net/quic-recovery.ts +287 -65
  91. package/std/net/quic-stream.ts +436 -146
  92. package/std/net/quic.ts +693 -323
  93. package/std/net/router.ts +87 -40
  94. package/std/net/tls/client.ts +167 -51
  95. package/std/net/tls/codec.ts +494 -68
  96. package/std/net/tls/record-client.ts +140 -73
  97. package/std/net/tls/record-server.ts +233 -105
  98. package/std/net/tls/record.ts +142 -36
  99. package/std/net/tls/schedule.ts +235 -26
  100. package/std/net/tls/ticket.ts +161 -40
  101. package/std/net/tls/verify.ts +57 -10
  102. package/std/net/tls-tcp.ts +308 -95
  103. package/std/net/tls.ts +276 -102
  104. package/std/net/upstream.ts +81 -45
  105. package/std/net/url.ts +109 -22
  106. package/std/net/websocket.ts +140 -26
  107. package/std/net/webtransport.ts +243 -106
  108. package/std/pair.ts +10 -2
  109. package/std/random.ts +42 -15
  110. package/std/regex.ts +422 -257
  111. package/std/region.ts +70 -15
  112. package/std/secret.ts +52 -11
  113. package/std/sort.ts +17 -2
  114. package/std/sqlite.ts +256 -35
  115. package/std/sync/ring.ts +229 -56
  116. package/std/sync/shm.ts +203 -23
  117. package/std/term-io.ts +154 -78
  118. package/std/term-pixels.ts +144 -19
  119. package/std/term-probe.ts +77 -35
  120. package/std/term-rate.ts +199 -118
  121. package/std/term.ts +400 -175
  122. package/std/testing.ts +130 -32
  123. package/std/text.ts +84 -17
  124. package/std/threads.ts +248 -56
  125. package/std/yaml-json.ts +111 -97
  126. package/std/yaml-lines.ts +47 -15
  127. package/std/yaml-write.ts +45 -30
  128. package/std/yaml.ts +764 -384
@@ -5,25 +5,28 @@
5
5
  *
6
6
  * import { QuicCidTable } from "nish/net/quic-conn-cid";
7
7
  *
8
- * const ids = new QuicCidTable(4, 4); // the active_connection_id_limit advertised; local slots
9
- * ids.addLocal(serverScid, token); // sequence 0
10
- * ids.addPeer(0, 0, clientScid, none); // the client's first SCID, sequence 0
11
- * const error: i64 = ids.retireLocal(frame.value, packetDcid);
8
+ * const ids = new QuicCidTable(4, 4) // the limit advertised; local slots
9
+ * ids.addLocal(serverScid, []) // sequence 0, sent in the SCID, so no token
10
+ * ids.addLocal(nextCid, resetToken) // sequence 1, for a NEW_CONNECTION_ID frame
11
+ * ids.addPeer(0, 0, clientScid, []) // the client's first SCID, sequence 0
12
+ * const dcid: u8[] = ids.currentPeer() // the ID this side sends to
13
+ * const error: i64 = ids.retireLocal(sequence, packet, dcidAt, dcidLength)
14
+ * // error is QUIC_ERROR_NO_ERROR, or QUIC_ERROR_PROTOCOL_VIOLATION to close with
12
15
  *
13
- * **Local IDs** are the ones this side handed out: sequence 0 is the Source
16
+ * **Local IDs.** They are the ones this side handed out: sequence 0 is the Source
14
17
  * Connection ID of its first packet, and each later one goes out in a
15
18
  * NEW_CONNECTION_ID frame (`nextUnannounced` answers the next one still to). The peer
16
19
  * retires one with RETIRE_CONNECTION_ID: a sequence number never issued, or
17
20
  * the one the retiring packet was itself sent to, is a PROTOCOL_VIOLATION
18
21
  * (§19.16).
19
22
  *
20
- * **Peer IDs** come from the peer's first packet and its NEW_CONNECTION_ID
23
+ * **Peer IDs.** They come from the peer's first packet and its NEW_CONNECTION_ID
21
24
  * frames (§19.15). A sequence number seen before must carry the same ID and
22
25
  * token, or it is a PROTOCOL_VIOLATION; an ID already given another sequence
23
26
  * number is one too. More active IDs than the limit this side advertised is
24
27
  * a CONNECTION_ID_LIMIT_ERROR (§5.1.1). Retire Prior To retires every older ID
25
- * at once: each is queued in `retirePending`, for this side to send
26
- * RETIRE_CONNECTION_ID for, and `current` moves to the oldest still active.
28
+ * at once: each is queued for this side to send RETIRE_CONNECTION_ID for,
29
+ * and the ID this side sends to moves to the oldest still active.
27
30
  * Queued retirements past twice the limit are a CONNECTION_ID_LIMIT_ERROR too
28
31
  * (§5.1.2), which bounds what a peer can make this side owe it.
29
32
  *
@@ -57,23 +60,39 @@ const QUIC_CID_ZERO: i32 = 0
57
60
  * says whether the slot holds an ID at all.
58
61
  */
59
62
  export class QuicCidEntry {
63
+ /** The ID's sequence number, as issued in NEW_CONNECTION_ID; 0 for the ID of the handshake. */
60
64
  sequence: i64 = 0
65
+ /** @internal Read by `net/quic.ts` and `net/http3-server.ts`. */
61
66
  cid: u8[]
62
67
  /** The stateless reset token issued with it (RFC 9000 §10.3); `hasToken` is false for a peer's sequence 0. */
63
68
  resetToken: u8[]
69
+ /** How many bytes of `cid` the ID takes, 0 to 20. */
64
70
  length: i32 = 0
71
+ /** @internal Read by `net/quic.ts` and `net/http3-server.ts`. */
65
72
  used: boolean = false
73
+ /** @internal Read only by this module's table. */
66
74
  hasToken: boolean = false
67
75
  /** For a local ID: whether a NEW_CONNECTION_ID frame announced it yet. Sequence 0 needs none. */
68
76
  announced: boolean = false
69
77
 
78
+ /**
79
+ * Makes an unused slot, with room for the longest connection ID and its
80
+ * reset token.
81
+ */
70
82
  constructor() {
71
83
  this.cid = new Array<u8>(QUIC_MAX_CID_LENGTH)
72
84
  this.resetToken = new Array<u8>(QUIC_RESET_TOKEN_SIZE)
73
85
  }
74
86
  }
75
87
 
76
- /** Copies `length` bytes of `from` at `at` into the start of `to`, and zeroes the rest of `to`. */
88
+ /**
89
+ * Copies `length` bytes of `from` at `at` into the start of `to`, and zeroes the rest of `to`.
90
+ *
91
+ * @param to The destination; every one of its bytes is written, so it is fully overwritten.
92
+ * @param from The source; bytes it does not have (an index before 0 or past its end) are copied as zero.
93
+ * @param at The offset in `from` of the first byte to copy.
94
+ * @param length How many bytes to copy; past `to.length` the extra bytes are dropped.
95
+ */
77
96
  export const quicCidCopy = (to: u8[], from: u8[], at: i32, length: i32): void => {
78
97
  for (let k: i32 = 0; k < toI32(to.length); k += 1) {
79
98
  to[k] = k < length && at + k >= 0 && at + k < toI32(from.length) ? from[at + k] : toU8(0)
@@ -134,21 +153,33 @@ const quicCidSlots = (count: i32): QuicCidEntry[] => {
134
153
  * `active_connection_id_limit` this side advertised, which bounds the peer's.
135
154
  */
136
155
  export class QuicCidTable {
156
+ /** The slots for the IDs this side issued, so a caller can walk them, used or not. */
137
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
+ */
138
162
  peer: QuicCidEntry[]
139
163
  /** Peer sequence numbers this side must send RETIRE_CONNECTION_ID for, oldest first, as a ring of `retireCount` from `retireHead`. */
140
- retirePending: i64[]
164
+ private retirePending: i64[]
141
165
  /** The next local sequence number to issue. */
142
166
  nextLocal: i64 = 0
143
167
  /** The peer's highest Retire Prior To so far. */
144
- peerRetirePriorTo: i64 = 0
145
- limit: i64 = 2
168
+ private peerRetirePriorTo: i64 = 0
169
+ private limit: i64 = 2
146
170
  /** The index in `peer` of the ID this side sends to, or -1 before the peer gave one. */
147
- current: i32 = -1
148
- retireHead: i32 = 0
171
+ private current: i32 = -1
172
+ private retireHead: i32 = 0
173
+ /** How many RETIRE_CONNECTION_ID frames this side still owes the peer, at most twice `limit`. */
149
174
  retireCount: i32 = 0
150
175
 
151
- /** A table for an advertised `limit` (2 to 8 in practice) with `localCapacity` slots for this side's IDs. */
176
+ /**
177
+ * A table for an advertised `limit` (2 to 8 in practice) with `localCapacity` slots for this side's IDs.
178
+ *
179
+ * @param limit The `active_connection_id_limit` this side advertised: how many peer IDs may be active at
180
+ * once, and half the size of the retirement ring. Below 1 still gets one peer slot.
181
+ * @param localCapacity How many of this side's IDs can be active at once; `addLocal` answers -1 past it.
182
+ */
152
183
  constructor(limit: i64, localCapacity: i32) {
153
184
  const peerSlots: i32 = limit < 1 ? 1 : toI32(limit)
154
185
  this.local = quicCidSlots(localCapacity)
@@ -173,9 +204,15 @@ export class QuicCidTable {
173
204
  }
174
205
 
175
206
  /**
176
- * Issues the first `length` bytes of `cid` as the next local ID, with its
207
+ * Issues `cid`, up to its first 20 bytes, as the next local ID, with its
177
208
  * 16-byte reset token (or none, empty, for sequence 0), and answers its
178
209
  * sequence number; -1 when every local slot is taken.
210
+ *
211
+ * @param cid The ID's bytes, copied into the slot; past 20 bytes (`QUIC_MAX_CID_LENGTH`) the rest is
212
+ * dropped.
213
+ * @param resetToken The 16-byte stateless reset token, copied; any other length, empty included, means
214
+ * none.
215
+ * @returns The ID's sequence number, counting up from 0, or -1 when every local slot is taken.
179
216
  */
180
217
  addLocal(cid: u8[], resetToken: u8[]): i64 {
181
218
  const k: i32 = quicCidFree(this.local)
@@ -197,7 +234,11 @@ export class QuicCidTable {
197
234
  return sequence
198
235
  }
199
236
 
200
- /** How many local IDs are active: a retired one leaves its slot. */
237
+ /**
238
+ * How many local IDs are active: a retired one leaves its slot.
239
+ *
240
+ * @returns The count of issued, not yet retired local IDs, from 0 to `localCapacity`.
241
+ */
201
242
  activeLocal(): i32 {
202
243
  let n: i32 = 0
203
244
  for (const entry of this.local) {
@@ -208,12 +249,26 @@ export class QuicCidTable {
208
249
  return n
209
250
  }
210
251
 
211
- /** Whether `dcid` is an active local ID: whether a packet sent to it is this connection's. */
252
+ /**
253
+ * Whether `dcid` is an active local ID: whether a packet sent to it is this connection's.
254
+ *
255
+ * @param dcid A packet's whole Destination Connection ID.
256
+ * @returns True when it matches one of this side's active IDs byte for byte.
257
+ */
212
258
  ownsLocal(dcid: u8[]): boolean {
213
259
  return this.ownsLocalAt(dcid, QUIC_CID_ZERO, toI32(dcid.length))
214
260
  }
215
261
 
216
- /** Whether `buf[at .. at + length)` is an active local ID. Every slot is compared, each in constant time. */
262
+ /**
263
+ * Whether `buf[at .. at + length)` is an active local ID. Each active slot
264
+ * of that length is compared in constant time, and the loop does not stop
265
+ * at a match; unused slots and slots of another length are skipped.
266
+ *
267
+ * @param buf The bytes holding the ID, such as a received datagram; only read.
268
+ * @param at The offset of the ID's first byte in `buf`.
269
+ * @param length The ID's length in bytes.
270
+ * @returns True when the bytes match one of this side's active IDs of that length.
271
+ */
217
272
  ownsLocalAt(buf: u8[], at: i32, length: i32): boolean {
218
273
  let owned: boolean = false
219
274
  for (const entry of this.local) {
@@ -231,6 +286,12 @@ export class QuicCidTable {
231
286
  * ID the packet itself was sent to (§19.16). Retiring one twice is fine. A
232
287
  * retired ID leaves its slot, so a peer that retires one ID after another
233
288
  * cannot grow the table.
289
+ *
290
+ * @param sequence The Sequence Number field of the RETIRE_CONNECTION_ID frame.
291
+ * @param buf The bytes holding the Destination Connection ID of the packet the frame came in.
292
+ * @param at The offset of that ID's first byte in `buf`.
293
+ * @param length That ID's length in bytes.
294
+ * @returns `QUIC_ERROR_NO_ERROR`, or `QUIC_ERROR_PROTOCOL_VIOLATION` to close the connection with.
234
295
  */
235
296
  retireLocal(sequence: i64, buf: u8[], at: i32, length: i32): i64 {
236
297
  if (sequence < 0 || sequence >= this.nextLocal) {
@@ -247,7 +308,11 @@ export class QuicCidTable {
247
308
  return QUIC_ERROR_NO_ERROR
248
309
  }
249
310
 
250
- /** How many peer IDs are active: retired ones leave their slots. */
311
+ /**
312
+ * How many peer IDs are active: retired ones leave their slots.
313
+ *
314
+ * @returns The count of the peer's IDs not yet retired, from 0 to `limit`.
315
+ */
251
316
  activePeer(): i32 {
252
317
  let n: i32 = 0
253
318
  for (const entry of this.peer) {
@@ -258,7 +323,15 @@ export class QuicCidTable {
258
323
  return n
259
324
  }
260
325
 
261
- /** `addPeerAt` of whole arrays: `resetToken` empty for none. */
326
+ /**
327
+ * `addPeerAt` of whole arrays: `resetToken` empty for none.
328
+ *
329
+ * @param sequence The ID's sequence number: 0 for the SCID of the peer's first packet.
330
+ * @param retirePriorTo The frame's Retire Prior To field, 0 for the first packet's SCID.
331
+ * @param cid The whole ID, copied into a slot.
332
+ * @param resetToken The 16-byte stateless reset token; any other length, empty included, means none.
333
+ * @returns `QUIC_ERROR_NO_ERROR`, or the error to close the connection with, as `addPeerAt` answers.
334
+ */
262
335
  addPeer(sequence: i64, retirePriorTo: i64, cid: u8[], resetToken: u8[]): i64 {
263
336
  return this.addPeerAt(
264
337
  sequence,
@@ -282,6 +355,17 @@ export class QuicCidTable {
282
355
  * or the queued retirements would pass what §5.1 allows, in which case the
283
356
  * table is left as it was. The tokens are compared in constant time, since
284
357
  * a token is what a stateless reset is checked against.
358
+ *
359
+ * @param sequence The ID's sequence number from the NEW_CONNECTION_ID frame, or 0 for the first packet's
360
+ * SCID.
361
+ * @param retirePriorTo The frame's Retire Prior To field; a value below an earlier one changes nothing.
362
+ * @param cid The bytes holding the ID, such as the received packet; only read, and the ID is copied out.
363
+ * @param cidAt The offset of the ID's first byte in `cid`.
364
+ * @param cidLength The ID's length in bytes, 0 to 20; longer is a PROTOCOL_VIOLATION.
365
+ * @param token The bytes holding the reset token; read only when `hasToken`.
366
+ * @param tokenAt The offset of the token's first byte in `token`.
367
+ * @param hasToken Whether a 16-byte token comes with the ID: false only for the first packet's SCID.
368
+ * @returns `QUIC_ERROR_NO_ERROR`, `QUIC_ERROR_PROTOCOL_VIOLATION` or `QUIC_ERROR_CONNECTION_ID_LIMIT`.
285
369
  */
286
370
  addPeerAt(
287
371
  sequence: i64,
@@ -363,18 +447,28 @@ export class QuicCidTable {
363
447
  return QUIC_ERROR_NO_ERROR
364
448
  }
365
449
 
366
- /** Points `current` at the oldest active peer ID, or -1 when none is. */
450
+ /** Makes the oldest active peer ID the current one, or none when no peer ID is active. */
367
451
  prune(): void {
368
452
  this.current = quicCidOldest(this.peer, -1, false)
369
453
  }
370
454
 
371
- /** The peer ID this side sends to: its slot, or `null` before the peer gave any. */
455
+ /**
456
+ * The peer ID this side sends to: its slot, or `null` before the peer gave any.
457
+ *
458
+ * @returns The table's own entry, not a copy, so it changes when the peer's IDs do; `null` when no
459
+ * peer ID is active.
460
+ */
372
461
  currentPeerEntry(): QuicCidEntry | null {
373
462
  const at: i32 = this.current
374
463
  return at >= 0 && at < toI32(this.peer.length) ? this.peer[at] : null
375
464
  }
376
465
 
377
- /** A copy of the peer ID this side sends to, or empty before the peer gave any. */
466
+ /**
467
+ * A copy of the peer ID this side sends to, or empty before the peer gave any.
468
+ *
469
+ * @returns A new array of the ID's bytes, the caller's to keep; empty when no peer ID is active, and
470
+ * also when the active one is zero-length (use `currentPeerEntry` to tell the two apart).
471
+ */
378
472
  currentPeer(): u8[] {
379
473
  const entry: QuicCidEntry | null = this.currentPeerEntry()
380
474
  const length: i32 = entry !== null ? entry.length : 0
@@ -385,7 +479,12 @@ export class QuicCidTable {
385
479
  return out
386
480
  }
387
481
 
388
- /** Queues a RETIRE_CONNECTION_ID for peer sequence `sequence`, unless it is queued already or the ring is full. */
482
+ /**
483
+ * Queues a RETIRE_CONNECTION_ID for peer sequence `sequence`, unless it is queued already or the
484
+ * ring is full.
485
+ *
486
+ * @param sequence The sequence number of the peer ID to retire; `takeRetire` hands it back.
487
+ */
389
488
  queueRetire(sequence: i64): void {
390
489
  const size: i32 = toI32(this.retirePending.length)
391
490
  for (let k: i32 = 0; k < this.retireCount; k += 1) {
@@ -404,7 +503,11 @@ export class QuicCidTable {
404
503
  }
405
504
  }
406
505
 
407
- /** The oldest queued retirement, or -1 when none is queued. Taking it removes it. */
506
+ /**
507
+ * The oldest queued retirement, or -1 when none is queued. Taking it removes it.
508
+ *
509
+ * @returns The peer sequence number to send RETIRE_CONNECTION_ID for, or -1 when none is queued.
510
+ */
408
511
  takeRetire(): i64 {
409
512
  const size: i32 = toI32(this.retirePending.length)
410
513
  if (this.retireCount === 0 || size === 0) {
@@ -417,7 +520,12 @@ export class QuicCidTable {
417
520
  return first
418
521
  }
419
522
 
420
- /** The first local ID still to be announced in a NEW_CONNECTION_ID frame, oldest first, or `null`. */
523
+ /**
524
+ * The first local ID still to be announced in a NEW_CONNECTION_ID frame, oldest first, or `null`.
525
+ *
526
+ * @returns The table's own entry, whose `announced` the caller sets once the frame is sent; `null` when
527
+ * every active local ID was announced.
528
+ */
421
529
  nextUnannounced(): QuicCidEntry | null {
422
530
  const k: i32 = quicCidOldest(this.local, -1, true)
423
531
  return k >= 0 && k < toI32(this.local.length) ? this.local[k] : null
@@ -6,33 +6,39 @@
6
6
  * import { QuicTransportParameters, quicEncodeTransportParameters,
7
7
  * quicParseTransportParameters } from "nish/net/quic-conn-params";
8
8
  *
9
- * const mine = new QuicTransportParameters();
10
- * mine.initialMaxData = 65536;
11
- * const bytes: u8[] = quicEncodeTransportParameters(mine); // for TlsServerConfig
12
- * const theirs = quicParseTransportParameters(clientBytes, false);
13
- * if (theirs.error !== 0) { … close with theirs.error … }
9
+ * const mine: QuicTransportParameters = new QuicTransportParameters()
10
+ * mine.initialMaxData = 65536
11
+ * const bytes: u8[] = quicEncodeTransportParameters(mine) // for TlsServerConfig
12
+ * // bytes is [0x04, 0x04, 0x80, 0x01, 0x00, 0x00]: initial_max_data, length 4, 65536
13
+ * const theirs: QuicTransportParameters = quicParseTransportParameters(bytes, false)
14
+ * if (theirs.error !== 0) {
15
+ * // close the connection with theirs.error; theirs.errorParameter names the parameter
16
+ * }
14
17
  *
15
- * A `QuicTransportParameters` starts at every parameter's default (§18.2),
16
- * so a parse leaves a parameter the peer did not send at the value the RFC
18
+ * **Defaults.** A `QuicTransportParameters` starts at every parameter's
19
+ * default (§18.2), so a parse leaves a parameter the peer did not send at the value the RFC
17
20
  * says it then has. The encoder writes a parameter only when it differs from
18
21
  * that default, plus the connection IDs, which have none.
19
22
  *
20
- * **What a parse refuses**, each a TRANSPORT_PARAMETER_ERROR (§7.4, §18.2):
21
- * an ID or a length that does not fit, a parameter running past the bytes, a
22
- * known parameter sent twice, a varint-valued parameter whose value is not
23
- * exactly one varint, `disable_active_migration` with a value, a server-only
24
- * parameter from a client (`original_destination_connection_id`,
25
- * `preferred_address`, `retry_source_connection_id`,
26
- * `stateless_reset_token`), a stateless reset token that is not 16 bytes, a
27
- * connection ID over 20 bytes, `max_udp_payload_size` below 1200,
28
- * `ack_delay_exponent` above 20, `max_ack_delay` at 2^14 or above,
29
- * `active_connection_id_limit` below 2, and a stream count above 2^60.
30
- * Unknown parameters, the reserved ones (31 × N + 27) among them, are
31
- * skipped, as §18.1 asks; a second copy of an unknown one is not noticed.
32
- * Whether `initial_source_connection_id` is present and right is the
33
- * connection's check, because it needs the packet the ID came in.
23
+ * **What a parse refuses.**
34
24
  *
35
- * Written from RFC 9000 §18, in this module's own structure; nothing here is
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.
40
+ *
41
+ * **Provenance.** Written from RFC 9000 §18, in this module's own structure; nothing here is
36
42
  * ported from another implementation. Private names carry the
37
43
  * `quicParams` prefix (`docs/wp26-stdlib.md` §3e).
38
44
  */
@@ -51,22 +57,60 @@ import {
51
57
 
52
58
  // ---- Parameter IDs (RFC 9000 §18.2) ---------------------------------------------
53
59
 
60
+ /**
61
+ * `original_destination_connection_id`: the Destination Connection ID of the client's first Initial; server
62
+ * only.
63
+ */
54
64
  export const QUIC_TP_ORIGINAL_DCID: i32 = 0x00
65
+ /** `max_idle_timeout`, in milliseconds; 0 or absent for no idle timeout. */
55
66
  export const QUIC_TP_MAX_IDLE_TIMEOUT: i32 = 0x01
67
+ /** `stateless_reset_token`: the 16-byte token for the server's first connection ID; server only. */
56
68
  export const QUIC_TP_STATELESS_RESET_TOKEN: i32 = 0x02
69
+ /**
70
+ * `max_udp_payload_size`: the largest UDP payload, in bytes, the sender takes; at least 1200, default 65527.
71
+ */
57
72
  export const QUIC_TP_MAX_UDP_PAYLOAD_SIZE: i32 = 0x03
73
+ /** `initial_max_data`: the connection-wide flow-control limit, in bytes. */
58
74
  export const QUIC_TP_INITIAL_MAX_DATA: i32 = 0x04
75
+ /**
76
+ * `initial_max_stream_data_bidi_local`: the flow-control limit, in bytes, for bidirectional streams the
77
+ * sender opens.
78
+ */
59
79
  export const QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL: i32 = 0x05
80
+ /**
81
+ * `initial_max_stream_data_bidi_remote`: the flow-control limit, in bytes, for bidirectional streams the peer
82
+ * opens.
83
+ */
60
84
  export const QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE: i32 = 0x06
85
+ /**
86
+ * `initial_max_stream_data_uni`: the flow-control limit, in bytes, for unidirectional streams the peer opens.
87
+ */
61
88
  export const QUIC_TP_INITIAL_MAX_STREAM_DATA_UNI: i32 = 0x07
89
+ /** `initial_max_streams_bidi`: how many bidirectional streams the peer may open; at most 2^60. */
62
90
  export const QUIC_TP_INITIAL_MAX_STREAMS_BIDI: i32 = 0x08
91
+ /** `initial_max_streams_uni`: how many unidirectional streams the peer may open; at most 2^60. */
63
92
  export const QUIC_TP_INITIAL_MAX_STREAMS_UNI: i32 = 0x09
93
+ /** `ack_delay_exponent`: an ACK Delay field counts units of 2^this microseconds; at most 20, default 3. */
64
94
  export const QUIC_TP_ACK_DELAY_EXPONENT: i32 = 0x0a
95
+ /**
96
+ * `max_ack_delay`: the longest the sender delays an acknowledgement, in milliseconds; below 2^14, default 25.
97
+ */
65
98
  export const QUIC_TP_MAX_ACK_DELAY: i32 = 0x0b
99
+ /** `disable_active_migration`: present, with no value, when the sender takes no connection migration. */
66
100
  export const QUIC_TP_DISABLE_ACTIVE_MIGRATION: i32 = 0x0c
101
+ /** `preferred_address`: an address the server would have the client move to; server only. */
67
102
  export const QUIC_TP_PREFERRED_ADDRESS: i32 = 0x0d
103
+ /**
104
+ * `active_connection_id_limit`: how many of the peer's connection IDs the sender keeps; at least 2, the
105
+ * default.
106
+ */
68
107
  export const QUIC_TP_ACTIVE_CONNECTION_ID_LIMIT: i32 = 0x0e
108
+ /** `initial_source_connection_id`: the Source Connection ID of the sender's first Initial. */
69
109
  export const QUIC_TP_INITIAL_SCID: i32 = 0x0f
110
+ /**
111
+ * `retry_source_connection_id`: the Source Connection ID of the server's Retry, when it sent one; server
112
+ * only.
113
+ */
70
114
  export const QUIC_TP_RETRY_SCID: i32 = 0x10
71
115
  /**
72
116
  * `max_datagram_frame_size` (RFC 9221 §3): the largest DATAGRAM frame, type
@@ -90,33 +134,61 @@ const QUIC_PARAMS_MAX_STREAMS: i64 = 1073741824 * 1073741824
90
134
  * frame).
91
135
  */
92
136
  export class QuicTransportParameters {
137
+ /** What the last parse answered: `QUIC_ERROR_NO_ERROR`, or `QUIC_ERROR_TRANSPORT_PARAMETER` with `errorParameter` set. */
93
138
  error: i64 = 0
139
+ /** `max_idle_timeout`, in milliseconds; 0, the default, for no idle timeout. */
94
140
  maxIdleTimeout: i64 = 0
141
+ /** `max_udp_payload_size`: the largest UDP payload the sender takes, at least 1200 bytes; 65527 by default. */
95
142
  maxUdpPayloadSize: i64 = 65527
143
+ /** `initial_max_data`: the connection's first receive credit, in bytes, across every stream. */
96
144
  initialMaxData: i64 = 0
145
+ /** `initial_max_stream_data_bidi_local`: the first credit, in bytes, on a bidirectional stream the sender opens. */
97
146
  initialMaxStreamDataBidiLocal: i64 = 0
147
+ /** `initial_max_stream_data_bidi_remote`: the first credit, in bytes, on a bidirectional stream its peer opens. */
98
148
  initialMaxStreamDataBidiRemote: i64 = 0
149
+ /** `initial_max_stream_data_uni`: the first credit, in bytes, on a unidirectional stream its peer opens. */
99
150
  initialMaxStreamDataUni: i64 = 0
151
+ /** `initial_max_streams_bidi`: how many bidirectional streams the peer may open at first, at most 2^60. */
100
152
  initialMaxStreamsBidi: i64 = 0
153
+ /** `initial_max_streams_uni`: how many unidirectional streams the peer may open at first, at most 2^60. */
101
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. */
102
156
  ackDelayExponent: i64 = 3
157
+ /** `max_ack_delay`, in milliseconds: how long the sender may hold an acknowledgement, below 2^14; 25 by default. */
103
158
  maxAckDelay: i64 = 25
159
+ /** `active_connection_id_limit`: how many of its peer's connection IDs the sender keeps, at least 2. */
104
160
  activeConnectionIdLimit: i64 = 2
105
161
  /** RFC 9221's `max_datagram_frame_size`: 0, the default, for no DATAGRAM frames. */
106
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`. */
107
164
  errorParameter: i64 = 0
165
+ /** `original_destination_connection_id`: the client's first Destination Connection ID; server only. */
108
166
  originalDcid: u8[]
167
+ /** `stateless_reset_token`: 16 bytes that end a stateless reset for the server's connection ID; server only. */
109
168
  statelessResetToken: u8[]
169
+ /** `preferred_address`, kept as the bytes the server sent, since this side never acts on it; server only. */
110
170
  preferredAddress: u8[]
171
+ /** `initial_source_connection_id`: the Source Connection ID of the sender's first Initial packet. */
111
172
  initialScid: u8[]
173
+ /** `retry_source_connection_id`: the Source Connection ID of the server's Retry; server only. */
112
174
  retryScid: u8[]
175
+ /** Whether `originalDcid` was set or read; an empty ID is a value, so its length cannot say. */
113
176
  hasOriginalDcid: boolean = false
177
+ /** Whether `statelessResetToken` was set or read. */
114
178
  hasStatelessResetToken: boolean = false
179
+ /** Whether `preferredAddress` was set or read. */
115
180
  hasPreferredAddress: boolean = false
181
+ /** Whether `initialScid` was set or read; an empty ID is a value, so its length cannot say. */
116
182
  hasInitialScid: boolean = false
183
+ /** Whether `retryScid` was set or read; an empty ID is a value, so its length cannot say. */
117
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. */
118
186
  disableActiveMigration: boolean = false
119
187
 
188
+ /**
189
+ * Makes the parameters an endpoint has before it sets or reads any: §18.2's
190
+ * defaults, every connection ID and token empty.
191
+ */
120
192
  constructor() {
121
193
  this.originalDcid = []
122
194
  this.statelessResetToken = []
@@ -191,6 +263,9 @@ const quicParamsPushBytes = (out: u8[], id: i32, bytes: u8[], present: boolean):
191
263
  * default, every byte-valued one only when its `has*` flag is set, and
192
264
  * `disable_active_migration` only when true. Nothing is checked: these are
193
265
  * this endpoint's own values, and a value no varint holds is left out.
266
+ *
267
+ * @param p This endpoint's parameters, only read.
268
+ * @returns A new array holding the extension body; empty when every parameter is at its default.
194
269
  */
195
270
  export const quicEncodeTransportParameters = (p: QuicTransportParameters): u8[] => {
196
271
  const out: u8[] = []
@@ -203,6 +278,9 @@ export const quicEncodeTransportParameters = (p: QuicTransportParameters): u8[]
203
278
  * encoding is pushed into the room the array already has, so a connection
204
279
  * slot that keeps one array for its parameters grows it only when they
205
280
  * outgrow every earlier connection's.
281
+ *
282
+ * @param p This endpoint's parameters, only read.
283
+ * @param out The array the extension body replaces the contents of.
206
284
  */
207
285
  export const quicEncodeTransportParametersInto = (p: QuicTransportParameters, out: u8[]): void => {
208
286
  quicParamsEmpty(out)
@@ -387,6 +465,10 @@ const quicParamsIsVarint = (id: i32): boolean =>
387
465
  * `QUIC_ERROR_TRANSPORT_PARAMETER` for any of the breaks the module header
388
466
  * lists, with `errorParameter` naming the parameter; on an error the other
389
467
  * fields hold what was read so far and are not to be used.
468
+ *
469
+ * @param bytes The extension body as the peer sent it, only read.
470
+ * @param fromServer True when the peer is the server, so that the server-only parameters are allowed.
471
+ * @returns A new `QuicTransportParameters`; read its `error` before any other field.
390
472
  */
391
473
  export const quicParseTransportParameters = (bytes: u8[], fromServer: boolean): QuicTransportParameters => {
392
474
  const p: QuicTransportParameters = new QuicTransportParameters()
@@ -399,6 +481,11 @@ export const quicParseTransportParameters = (bytes: u8[], fromServer: boolean):
399
481
  * default first, its byte-valued fields emptied and refilled in the room
400
482
  * their arrays have: a connection slot parses each client's parameters into
401
483
  * one object of its own. Answers `p`.
484
+ *
485
+ * @param p The object to overwrite with the parsed parameters and the verdict in `error`.
486
+ * @param bytes The extension body as the peer sent it, only read.
487
+ * @param fromServer True when the peer is the server, so that the server-only parameters are allowed.
488
+ * @returns `p` itself; read its `error` before any other field.
402
489
  */
403
490
  export const quicParseTransportParametersInto = (
404
491
  p: QuicTransportParameters,
@@ -6,6 +6,15 @@
6
6
  * of `nish/net`; `nish/net/quic`'s `QuicConnection` owns both rings and the
7
7
  * application reaches them through its `sendDatagram` and `readDatagram`.
8
8
  *
9
+ * import { QuicDatagramQueue } from "nish/net/quic-datagram";
10
+ *
11
+ * const outgoing: QuicDatagramQueue = new QuicDatagramQueue(16, 1200); // 16 payloads, each 1200 bytes at most
12
+ * if (!outgoing.push(message, 0, toI32(message.length))) { … full or too big: dropped … }
13
+ * at = outgoing.putFrame(packet, at, packetEnd); // `at` again when nothing fit
14
+ *
15
+ * const incoming: QuicDatagramQueue = new QuicDatagramQueue(16, 1200);
16
+ * const length: i32 = incoming.pop(into, 0, toI32(into.length)); // -1 empty, -2 `into` too small
17
+ *
9
18
  * **Fixed.** A ring is `entries` payloads of at most `entrySize` bytes,
10
19
  * allocated once; a payload is copied in and out, and nothing allocates
11
20
  * after the ring is made. A datagram is unreliable by definition (RFC 9221
@@ -24,13 +33,27 @@ import { quicPacketCopy } from "nish/net/quic-packet"
24
33
  export class QuicDatagramQueue {
25
34
  /** Entry `k`'s bytes are `data[k * entrySize ..]`, `lengths[k]` of them. */
26
35
  data: u8[]
36
+ /** Each entry's payload length in bytes, 0 to `entrySize`; entry `k`'s is `lengths[k]`. */
27
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
+ */
28
42
  entrySize: i32 = 0
43
+ /** The index of the oldest entry; the `j`-th oldest is entry `(head + j) % capacity()`. */
29
44
  head: i32 = 0
45
+ /** How many datagrams wait in the ring, from 0 to its entry count. */
30
46
  count: i32 = 0
31
47
  /** Datagrams refused for want of room or for their size, which the peer's side counts as dropped. */
32
48
  dropped: i32 = 0
33
49
 
50
+ /**
51
+ * Makes an empty ring, allocating all of its storage now: `entries * entrySize` bytes of
52
+ * payload and a 4-byte length per entry.
53
+ *
54
+ * @param entries How many datagrams the ring holds at most before `push` refuses the next.
55
+ * @param entrySize The largest payload, in bytes, an entry holds; a longer one is dropped, not split.
56
+ */
34
57
  constructor(entries: i32, entrySize: i32) {
35
58
  this.data = new Array<u8>(entries * entrySize)
36
59
  this.lengths = new Array<i32>(entries)
@@ -44,7 +67,11 @@ export class QuicDatagramQueue {
44
67
  this.dropped = 0
45
68
  }
46
69
 
47
- /** How many datagrams the ring holds at most. */
70
+ /**
71
+ * How many datagrams the ring holds at most.
72
+ *
73
+ * @returns The `entries` the ring was made with, however many it holds now (that is `count`).
74
+ */
48
75
  capacity(): i32 {
49
76
  return toI32(this.lengths.length)
50
77
  }
@@ -53,6 +80,11 @@ export class QuicDatagramQueue {
53
80
  * Copies `buf[from .. from + length)` in as the newest datagram. Answers
54
81
  * whether it could: not when the ring is full, nor for a payload past
55
82
  * `entrySize` or a window outside `buf`, each of which counts in `dropped`.
83
+ *
84
+ * @param buf The bytes to take the payload from; copied, not kept.
85
+ * @param from The offset in `buf` of the payload's first byte.
86
+ * @param length The payload's size in bytes, 0 to `entrySize`.
87
+ * @returns True when the datagram was queued; false when it was dropped instead.
56
88
  */
57
89
  push(buf: u8[], from: i32, length: i32): boolean {
58
90
  const entries: i32 = this.capacity()
@@ -78,14 +110,18 @@ export class QuicDatagramQueue {
78
110
  return true
79
111
  }
80
112
 
81
- /** The oldest datagram's length, or -1 when the ring is empty. */
113
+ /**
114
+ * The oldest datagram's length, or -1 when the ring is empty.
115
+ *
116
+ * @returns The size in bytes of the datagram `pop` or `putFrame` would take next, or -1 when there is none.
117
+ */
82
118
  peekLength(): i32 {
83
119
  const k: i32 = this.head
84
120
  return this.count > 0 && k >= 0 && k < toI32(this.lengths.length) ? this.lengths[k] : -1
85
121
  }
86
122
 
87
123
  /** Forgets the oldest datagram. */
88
- drop(): void {
124
+ private drop(): void {
89
125
  if (this.count > 0) {
90
126
  this.head = (this.head + 1) % this.capacity()
91
127
  this.count = this.count - 1
@@ -96,6 +132,12 @@ export class QuicDatagramQueue {
96
132
  * Copies the oldest datagram into `buf` at `at` and forgets it. Answers
97
133
  * its length, -1 when the ring is empty, or -2, keeping it, when it does
98
134
  * not fit in `cap` bytes from `at` inside `buf`.
135
+ *
136
+ * @param buf The buffer to copy the payload into.
137
+ * @param at The offset in `buf` to copy it to.
138
+ * @param cap How many bytes from `at` the caller lets the payload take.
139
+ * @returns The payload's size in bytes, -1 when the ring is empty, or -2 when it does not fit in `cap` or
140
+ * `at` puts it outside `buf`, in which case it stays queued for a bigger buffer or a good window.
99
141
  */
100
142
  pop(buf: u8[], at: i32, cap: i32): i32 {
101
143
  const length: i32 = this.peekLength()
@@ -114,6 +156,11 @@ export class QuicDatagramQueue {
114
156
  * Writes the oldest datagram as a DATAGRAM frame into `buf[at .. end)` and
115
157
  * forgets it. Answers the offset past the frame, or `at` when the ring is
116
158
  * empty or the frame does not fit, keeping it for the next packet.
159
+ *
160
+ * @param buf The packet payload being built.
161
+ * @param at The offset to write the frame at.
162
+ * @param end One past the last byte the frame may use, the packet's room or the congestion window's.
163
+ * @returns The offset past the DATAGRAM frame, or `at` itself when nothing was written.
117
164
  */
118
165
  putFrame(buf: u8[], at: i32, end: i32): i32 {
119
166
  const length: i32 = this.peekLength()