@amritk/nish-x86_64-linux 0.34.1 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/INSTALL.md +22 -13
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +86 -16
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +49 -0
  6. package/runtime/async.mjs +27 -0
  7. package/runtime/nish.d.ts +4681 -427
  8. package/runtime/nish.h +6 -2
  9. package/runtime/nish.mjs +4 -0
  10. package/runtime/runtime-net.c +22 -0
  11. package/runtime/runtime-shm.c +93 -2
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +108 -13
  15. package/std/README.md +131 -131
  16. package/std/base64.ts +34 -5
  17. package/std/bytes.ts +258 -42
  18. package/std/cli-args.ts +296 -70
  19. package/std/cli-flags.ts +184 -19
  20. package/std/cli.ts +475 -54
  21. package/std/collections.ts +121 -23
  22. package/std/compress/checksum.ts +44 -14
  23. package/std/compress/deflate.ts +224 -111
  24. package/std/compress/gzip.ts +217 -53
  25. package/std/compress/zlib.ts +63 -7
  26. package/std/crypto/aes.ts +80 -9
  27. package/std/crypto/base64url.ts +22 -0
  28. package/std/crypto/bcrypt-batch.ts +88 -21
  29. package/std/crypto/bcrypt.ts +60 -0
  30. package/std/crypto/chacha20poly1305.ts +53 -0
  31. package/std/crypto/ct.ts +32 -13
  32. package/std/crypto/ed25519.ts +47 -0
  33. package/std/crypto/hkdf.ts +198 -13
  34. package/std/crypto/hmac.ts +235 -31
  35. package/std/crypto/jwt.ts +192 -63
  36. package/std/crypto/p256.ts +159 -16
  37. package/std/crypto/p384.ts +28 -3
  38. package/std/crypto/pki-roots.ts +32 -2
  39. package/std/crypto/pki.ts +81 -15
  40. package/std/crypto/rsa.ts +33 -3
  41. package/std/crypto/sha1.ts +3 -0
  42. package/std/crypto/sha256.ts +24 -1
  43. package/std/crypto/sha512.ts +99 -14
  44. package/std/crypto/x25519.ts +152 -14
  45. package/std/crypto/x509-random.ts +10 -0
  46. package/std/crypto/x509.ts +67 -7
  47. package/std/json-equal.ts +19 -0
  48. package/std/json-format.ts +112 -13
  49. package/std/json-number.ts +64 -14
  50. package/std/json-pointer.ts +23 -1
  51. package/std/json-tokenizer.ts +100 -29
  52. package/std/json-typed.ts +713 -66
  53. package/std/json-value.ts +400 -124
  54. package/std/json.ts +138 -10
  55. package/std/map.ts +25 -7
  56. package/std/math.ts +36 -4
  57. package/std/net/buffer-pool.ts +42 -11
  58. package/std/net/dns-config.ts +109 -23
  59. package/std/net/dns-message.ts +62 -7
  60. package/std/net/dns-resolver.ts +247 -53
  61. package/std/net/dns.ts +203 -35
  62. package/std/net/fetch.ts +151 -61
  63. package/std/net/handler.ts +278 -80
  64. package/std/net/hpack.ts +262 -52
  65. package/std/net/http-compress.ts +125 -15
  66. package/std/net/http-fields.ts +85 -9
  67. package/std/net/http1-client-codec.ts +100 -19
  68. package/std/net/http1-codec.ts +297 -77
  69. package/std/net/http1-server.ts +430 -94
  70. package/std/net/http2-frame.ts +272 -35
  71. package/std/net/http2-tls.ts +135 -34
  72. package/std/net/http2.ts +247 -106
  73. package/std/net/http3-frame.ts +154 -17
  74. package/std/net/http3-server.ts +275 -101
  75. package/std/net/http3.ts +307 -115
  76. package/std/net/outbound.ts +37 -3
  77. package/std/net/postgres-carrier.ts +144 -8
  78. package/std/net/postgres.ts +801 -112
  79. package/std/net/prefork.ts +164 -31
  80. package/std/net/qpack.ts +105 -18
  81. package/std/net/quic-conn-ack.ts +59 -12
  82. package/std/net/quic-conn-cid.ts +124 -27
  83. package/std/net/quic-conn-params.ts +70 -10
  84. package/std/net/quic-datagram.ts +43 -3
  85. package/std/net/quic-frame.ts +286 -12
  86. package/std/net/quic-listener.ts +101 -32
  87. package/std/net/quic-packet.ts +302 -21
  88. package/std/net/quic-recovery.ts +264 -52
  89. package/std/net/quic-stream.ts +353 -91
  90. package/std/net/quic.ts +1124 -201
  91. package/std/net/router.ts +87 -40
  92. package/std/net/tls/client.ts +166 -51
  93. package/std/net/tls/codec.ts +413 -56
  94. package/std/net/tls/record-client.ts +110 -55
  95. package/std/net/tls/record-server.ts +159 -59
  96. package/std/net/tls/record.ts +109 -12
  97. package/std/net/tls/schedule.ts +235 -26
  98. package/std/net/tls/ticket.ts +159 -40
  99. package/std/net/tls/verify.ts +49 -3
  100. package/std/net/tls-tcp.ts +250 -45
  101. package/std/net/tls.ts +221 -59
  102. package/std/net/upstream.ts +81 -45
  103. package/std/net/url.ts +97 -13
  104. package/std/net/websocket.ts +122 -12
  105. package/std/net/webtransport.ts +220 -89
  106. package/std/pair.ts +10 -2
  107. package/std/random.ts +34 -9
  108. package/std/regex.ts +352 -198
  109. package/std/region.ts +70 -15
  110. package/std/secret.ts +52 -11
  111. package/std/sort.ts +17 -2
  112. package/std/sqlite.ts +256 -35
  113. package/std/sync/ring.ts +430 -40
  114. package/std/sync/shm.ts +222 -15
  115. package/std/term-io.ts +137 -64
  116. package/std/term-pixels.ts +144 -19
  117. package/std/term-probe.ts +66 -25
  118. package/std/term-rate.ts +128 -57
  119. package/std/term.ts +332 -121
  120. package/std/testing.ts +130 -32
  121. package/std/text.ts +73 -7
  122. package/std/threads.ts +229 -41
  123. package/std/yaml-json.ts +94 -82
  124. package/std/yaml-lines.ts +47 -15
  125. package/std/yaml-write.ts +24 -16
  126. package/std/yaml.ts +764 -384
@@ -14,7 +14,7 @@
14
14
  * at = frame.end;
15
15
  * }
16
16
  *
17
- * **Every frame type of RFC 9000 is read**, 0x00 to 0x1e, and RFC 9221's
17
+ * **Coverage.** Every frame type of RFC 9000 is read, 0x00 to 0x1e, and RFC 9221's
18
18
  * DATAGRAM, 0x30 and 0x31, so a type outside those is the one parse error a
19
19
  * well-formed packet cannot produce. What
20
20
  * a frame *means* is `nish/net/quic`'s: this module checks only what §19 says
@@ -36,7 +36,7 @@
36
36
  * every field it reads and resets the rest, and stores nothing but numbers,
37
37
  * so a payload made inside an arena block can be read inside it.
38
38
  *
39
- * **The writers** come in two forms. A `put` writer writes one frame into a
39
+ * **Writers.** They come in two forms. A `put` writer writes one frame into a
40
40
  * caller's buffer at an offset, before an end, and answers the offset past
41
41
  * it, or -1 with nothing written; a `push` writer appends one to an array
42
42
  * and answers whether it could, by growing the array and calling the `put`
@@ -67,15 +67,21 @@ import {
67
67
 
68
68
  // ---- Frame types (RFC 9000 §19, Table 3 of §12.4) ------------------------------
69
69
 
70
+ /** PADDING (§19.1): a single zero byte that means nothing, used to make a packet bigger. */
70
71
  export const QUIC_FRAME_PADDING: i32 = 0x00
72
+ /** PING (§19.2): the type alone, sent to get an acknowledgement or to keep a connection alive. */
71
73
  export const QUIC_FRAME_PING: i32 = 0x01
72
74
  /** ACK without ECN counts. */
73
75
  export const QUIC_FRAME_ACK: i32 = 0x02
74
76
  /** ACK with the three ECN counts after the ranges. */
75
77
  export const QUIC_FRAME_ACK_ECN: i32 = 0x03
78
+ /** RESET_STREAM (§19.4): the sender abandons a stream, with an error code and the final size. */
76
79
  export const QUIC_FRAME_RESET_STREAM: i32 = 0x04
80
+ /** STOP_SENDING (§19.5): asks the peer to stop sending on a stream, with an error code. */
77
81
  export const QUIC_FRAME_STOP_SENDING: i32 = 0x05
82
+ /** CRYPTO (§19.6): TLS handshake bytes at an offset in one packet number space's crypto stream. */
78
83
  export const QUIC_FRAME_CRYPTO: i32 = 0x06
84
+ /** NEW_TOKEN (§19.7): a server's token for the client to present in a later connection's Initial. */
79
85
  export const QUIC_FRAME_NEW_TOKEN: i32 = 0x07
80
86
  /**
81
87
  * The first of the eight STREAM types, 0x08 to 0x0f. `quicParseFrame` answers
@@ -83,22 +89,35 @@ export const QUIC_FRAME_NEW_TOKEN: i32 = 0x07
83
89
  * `offset`, `dataLength` and `fin` (§19.8).
84
90
  */
85
91
  export const QUIC_FRAME_STREAM: i32 = 0x08
92
+ /** MAX_DATA (§19.9): the connection's flow-control limit, in bytes across every stream. */
86
93
  export const QUIC_FRAME_MAX_DATA: i32 = 0x10
94
+ /** MAX_STREAM_DATA (§19.10): one stream's flow-control limit, in bytes. */
87
95
  export const QUIC_FRAME_MAX_STREAM_DATA: i32 = 0x11
96
+ /** MAX_STREAMS (§19.11) for bidirectional streams: how many the peer may open in all. */
88
97
  export const QUIC_FRAME_MAX_STREAMS_BIDI: i32 = 0x12
98
+ /** MAX_STREAMS (§19.11) for unidirectional streams: how many the peer may open in all. */
89
99
  export const QUIC_FRAME_MAX_STREAMS_UNI: i32 = 0x13
100
+ /** DATA_BLOCKED (§19.12): the sender has data to send but the connection limit holds it back. */
90
101
  export const QUIC_FRAME_DATA_BLOCKED: i32 = 0x14
102
+ /** STREAM_DATA_BLOCKED (§19.13): the sender is held back by one stream's limit. */
91
103
  export const QUIC_FRAME_STREAM_DATA_BLOCKED: i32 = 0x15
104
+ /** STREAMS_BLOCKED (§19.14) for bidirectional streams: the sender wants to open more than it may. */
92
105
  export const QUIC_FRAME_STREAMS_BLOCKED_BIDI: i32 = 0x16
106
+ /** STREAMS_BLOCKED (§19.14) for unidirectional streams: the sender wants to open more than it may. */
93
107
  export const QUIC_FRAME_STREAMS_BLOCKED_UNI: i32 = 0x17
108
+ /** NEW_CONNECTION_ID (§19.15): another connection ID the peer may use, with its reset token. */
94
109
  export const QUIC_FRAME_NEW_CONNECTION_ID: i32 = 0x18
110
+ /** RETIRE_CONNECTION_ID (§19.16): the sender stops using the peer's connection ID of a sequence number. */
95
111
  export const QUIC_FRAME_RETIRE_CONNECTION_ID: i32 = 0x19
112
+ /** PATH_CHALLENGE (§19.17): eight bytes the peer must echo in a PATH_RESPONSE to validate a path. */
96
113
  export const QUIC_FRAME_PATH_CHALLENGE: i32 = 0x1a
114
+ /** PATH_RESPONSE (§19.18): the eight bytes of a PATH_CHALLENGE, echoed back. */
97
115
  export const QUIC_FRAME_PATH_RESPONSE: i32 = 0x1b
98
116
  /** CONNECTION_CLOSE with a transport error code and the frame type that caused it. */
99
117
  export const QUIC_FRAME_CONNECTION_CLOSE: i32 = 0x1c
100
118
  /** CONNECTION_CLOSE with an application error code, allowed only in 1-RTT packets. */
101
119
  export const QUIC_FRAME_CONNECTION_CLOSE_APP: i32 = 0x1d
120
+ /** HANDSHAKE_DONE (§19.20): the server confirms the handshake; only a server sends it. */
102
121
  export const QUIC_FRAME_HANDSHAKE_DONE: i32 = 0x1e
103
122
  /**
104
123
  * DATAGRAM (RFC 9221 §4): an unreliable application payload. Type 0x30 runs
@@ -111,22 +130,39 @@ export const QUIC_FRAME_DATAGRAM_LENGTH: i32 = 0x31
111
130
 
112
131
  // ---- Transport error codes (RFC 9000 §20.1) ------------------------------------
113
132
 
133
+ /** NO_ERROR: the connection closes without an error; also `quicParseFrame`'s answer for a good frame. */
114
134
  export const QUIC_ERROR_NO_ERROR: i64 = 0x00
135
+ /** INTERNAL_ERROR: the endpoint broke on its own, not because of anything the peer sent. */
115
136
  export const QUIC_ERROR_INTERNAL: i64 = 0x01
137
+ /** CONNECTION_REFUSED: the server will not accept the connection. */
116
138
  export const QUIC_ERROR_CONNECTION_REFUSED: i64 = 0x02
139
+ /** FLOW_CONTROL_ERROR: the peer sent more data than the advertised limits allow. */
117
140
  export const QUIC_ERROR_FLOW_CONTROL: i64 = 0x03
141
+ /** STREAM_LIMIT_ERROR: the peer opened a stream past the limit it was given. */
118
142
  export const QUIC_ERROR_STREAM_LIMIT: i64 = 0x04
143
+ /** STREAM_STATE_ERROR: a frame arrived for a stream not in a state that permits it. */
119
144
  export const QUIC_ERROR_STREAM_STATE: i64 = 0x05
145
+ /** FINAL_SIZE_ERROR: a stream's final size changed, or data arrived past it. */
120
146
  export const QUIC_ERROR_FINAL_SIZE: i64 = 0x06
147
+ /** FRAME_ENCODING_ERROR: a frame is badly formed, such as truncated or with a length past the packet. */
121
148
  export const QUIC_ERROR_FRAME_ENCODING: i64 = 0x07
149
+ /** TRANSPORT_PARAMETER_ERROR: the peer's transport parameters are malformed or invalid. */
122
150
  export const QUIC_ERROR_TRANSPORT_PARAMETER: i64 = 0x08
151
+ /** CONNECTION_ID_LIMIT_ERROR: the peer gave more connection IDs than `active_connection_id_limit`. */
123
152
  export const QUIC_ERROR_CONNECTION_ID_LIMIT: i64 = 0x09
153
+ /** PROTOCOL_VIOLATION: the peer broke the protocol in a way no more specific code names. */
124
154
  export const QUIC_ERROR_PROTOCOL_VIOLATION: i64 = 0x0a
155
+ /** INVALID_TOKEN: a server received a client Initial whose Token field it cannot accept, from a Retry or NEW_TOKEN. */
125
156
  export const QUIC_ERROR_INVALID_TOKEN: i64 = 0x0b
157
+ /** APPLICATION_ERROR: the application closed, reported in a transport close where 0x1d may not be sent. */
126
158
  export const QUIC_ERROR_APPLICATION: i64 = 0x0c
159
+ /** CRYPTO_BUFFER_EXCEEDED: the peer sent more CRYPTO data than the endpoint buffers. */
127
160
  export const QUIC_ERROR_CRYPTO_BUFFER_EXCEEDED: i64 = 0x0d
161
+ /** KEY_UPDATE_ERROR: the peer made a mistake in a key update (RFC 9001 §6). */
128
162
  export const QUIC_ERROR_KEY_UPDATE: i64 = 0x0e
163
+ /** AEAD_LIMIT_REACHED: the keys reached their confidentiality or integrity limit (RFC 9001 §6.6). */
129
164
  export const QUIC_ERROR_AEAD_LIMIT_REACHED: i64 = 0x0f
165
+ /** NO_VIABLE_PATH: no network path is fit for QUIC, such as one whose MTU is too small. */
130
166
  export const QUIC_ERROR_NO_VIABLE_PATH: i64 = 0x10
131
167
  /** CRYPTO_ERROR is this plus the TLS alert description (RFC 9001 §4.8): 0x0100 to 0x01ff. */
132
168
  export const QUIC_ERROR_CRYPTO: i64 = 0x0100
@@ -197,6 +233,10 @@ export class QuicFrame {
197
233
  resetTokenStart: i32 = 0
198
234
  fin: boolean = false
199
235
 
236
+ /**
237
+ * Makes an empty frame with room for `QUIC_FRAME_MAX_ACK_RANGES` ACK
238
+ * ranges, for `quicParseFrame` to fill.
239
+ */
200
240
  constructor() {
201
241
  // Sized once, so reading an ACK stores nothing new into the frame.
202
242
  this.ackRanges = new Array<i64>(QUIC_FRAME_MAX_ACK_RANGES * 2)
@@ -212,8 +252,11 @@ export class QuicFrame {
212
252
  * read inside it.
213
253
  */
214
254
  class QuicFrameReader {
255
+ /** @internal Read in quic-frame.ts outside the class. */
215
256
  at: i32
257
+ /** @internal Read in quic-frame.ts outside the class. */
216
258
  end: i32
259
+ /** @internal Read in quic-frame.ts outside the class. */
217
260
  failed: boolean = false
218
261
 
219
262
  constructor(at: i32, end: i32) {
@@ -496,6 +539,15 @@ const quicFrameReadBody = (r: QuicFrameReader, d: u8[], frame: QuicFrame, type:
496
539
  * further, and `frame.type` still holds the type when it was read, which is
497
540
  * what CONNECTION_CLOSE reports. A window outside `payload` is
498
541
  * `QUIC_ERROR_INTERNAL`, the caller's fault rather than the peer's.
542
+ *
543
+ * @param frame The frame to fill, reused across frames and packets; its windows (`dataStart` and the
544
+ * like) are offsets into `payload`, so they mean something only while `payload` is unchanged.
545
+ * @param payload The decrypted packet payload, as the peer sent it; only read, never kept.
546
+ * @param at The offset of the frame's first byte, its type.
547
+ * @param end One past the last byte the frame may use, normally the payload's end; a frame reaching past
548
+ * it is `QUIC_ERROR_FRAME_ENCODING`.
549
+ * @returns `QUIC_ERROR_NO_ERROR` (0) for a good frame, otherwise the `QUIC_ERROR_*` code to close the
550
+ * connection with.
499
551
  */
500
552
  export const quicParseFrame = (frame: QuicFrame, payload: u8[], at: i32, end: i32): i64 => {
501
553
  quicFrameReset(frame, at)
@@ -538,6 +590,13 @@ export const quicParseFrame = (frame: QuicFrame, payload: u8[], at: i32, end: i3
538
590
  * ACK, CRYPTO and the transport CONNECTION_CLOSE; a 0-RTT packet anything
539
591
  * but ACK, CRYPTO, NEW_TOKEN, HANDSHAKE_DONE and the retire and response
540
592
  * frames a client cannot have a reason to send yet; a 1-RTT packet anything.
593
+ *
594
+ * @param type The frame type as `quicParseFrame` answers it in `QuicFrame.type`, every STREAM type as
595
+ * `QUIC_FRAME_STREAM`.
596
+ * @param packetType The packet's `QUIC_PACKET_*` type from `nish/net/quic-packet`, `QUIC_PACKET_SHORT`
597
+ * for 1-RTT.
598
+ * @returns True when §12.4 permits the frame there; false when it does not permit that frame in that
599
+ * packet type, and for a packet type that carries no frames, such as Retry.
541
600
  */
542
601
  export const quicFrameAllowed = (type: i32, packetType: i32): boolean => {
543
602
  if (packetType === QUIC_PACKET_SHORT) {
@@ -570,6 +629,10 @@ export const quicFrameAllowed = (type: i32, packetType: i32): boolean => {
570
629
  /**
571
630
  * Whether a frame of `type` asks for an acknowledgement (RFC 9000 §13.2):
572
631
  * every frame but ACK, PADDING and CONNECTION_CLOSE.
632
+ *
633
+ * @param type The frame type as `quicParseFrame` answers it.
634
+ * @returns True when a packet holding this frame is ack-eliciting, which is what decides whether the
635
+ * peer must acknowledge it promptly.
573
636
  */
574
637
  export const quicFrameAckEliciting = (type: i32): boolean =>
575
638
  !(
@@ -621,7 +684,12 @@ const quicFrameGrow = (out: u8[], size: i32): i32 => {
621
684
  const quicFrameVarints3 = (a: i64, b: i64, c: i64): boolean =>
622
685
  quicVarintSize(a) !== 0 && quicVarintSize(b) !== 0 && quicVarintSize(c) !== 0
623
686
 
624
- /** Appends `count` PADDING frames, which are `count` zero bytes. A count below one appends nothing. */
687
+ /**
688
+ * Appends `count` PADDING frames, which are `count` zero bytes. A count below one appends nothing.
689
+ *
690
+ * @param out The bytes to grow, such as a packet payload being built.
691
+ * @param count How many bytes of padding to append.
692
+ */
625
693
  export const quicPushPadding = (out: u8[], count: i32): void => {
626
694
  quicPacketGrow(out, count)
627
695
  }
@@ -629,6 +697,12 @@ export const quicPushPadding = (out: u8[], count: i32): void => {
629
697
  /**
630
698
  * Writes `count` PADDING frames, zero bytes, into `buf[at .. at + count)`,
631
699
  * clamped to `buf`, and answers the offset past them.
700
+ *
701
+ * @param buf The buffer to write into.
702
+ * @param at The offset of the first zero byte.
703
+ * @param count How many zero bytes to write; fewer are written where `buf` ends first.
704
+ * @returns The offset past the last byte written, which is short of `at + count` when `buf` was too small and
705
+ * `at` itself when nothing could be written.
632
706
  */
633
707
  export const quicPutPadding = (buf: u8[], at: i32, count: i32): i32 => {
634
708
  let k: i32 = 0
@@ -639,7 +713,15 @@ export const quicPutPadding = (buf: u8[], at: i32, count: i32): i32 => {
639
713
  return at + k
640
714
  }
641
715
 
642
- /** Writes a frame that is its type alone, PING or HANDSHAKE_DONE; -1 for another type or no room. */
716
+ /**
717
+ * Writes a frame that is its type alone, PING or HANDSHAKE_DONE; -1 for another type or no room.
718
+ *
719
+ * @param buf The buffer to write into.
720
+ * @param at The offset to write the one byte at.
721
+ * @param end One past the last byte the frame may use, such as the end of the packet's room.
722
+ * @param type `QUIC_FRAME_PING` or `QUIC_FRAME_HANDSHAKE_DONE`.
723
+ * @returns `at + 1`, or -1 with nothing written.
724
+ */
643
725
  export const quicPutTypeOnly = (buf: u8[], at: i32, end: i32, type: i32): i32 => {
644
726
  if ((type !== QUIC_FRAME_PING && type !== QUIC_FRAME_HANDSHAKE_DONE) || !quicFrameRoom(buf, at, end, 1)) {
645
727
  return -1
@@ -647,7 +729,13 @@ export const quicPutTypeOnly = (buf: u8[], at: i32, end: i32, type: i32): i32 =>
647
729
  return quicFramePutByte(buf, at, type)
648
730
  }
649
731
 
650
- /** Appends a frame that is its type alone: PING or HANDSHAKE_DONE. Answers `false` for another type. */
732
+ /**
733
+ * Appends a frame that is its type alone: PING or HANDSHAKE_DONE. Answers `false` for another type.
734
+ *
735
+ * @param out The bytes to append the frame's one byte to.
736
+ * @param type `QUIC_FRAME_PING` or `QUIC_FRAME_HANDSHAKE_DONE`.
737
+ * @returns True when the frame was appended; false, appending nothing, for any other type.
738
+ */
651
739
  export const quicPushTypeOnly = (out: u8[], type: i32): boolean => {
652
740
  if (type !== QUIC_FRAME_PING && type !== QUIC_FRAME_HANDSHAKE_DONE) {
653
741
  return false
@@ -660,6 +748,11 @@ export const quicPushTypeOnly = (out: u8[], type: i32): boolean => {
660
748
  * The size of the ACK frame `quicPutAck` writes for these ranges, or 0 when
661
749
  * it would refuse them: no ranges, ranges out of order or touching, a pair
662
750
  * the array does not hold, or a value no varint holds.
751
+ *
752
+ * @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
753
+ * @param rangeCount How many pairs to read from the front of `ranges`; the rest of the array is ignored.
754
+ * @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
755
+ * @returns The frame's size in bytes, or 0 when `quicPutAck` would refuse these arguments.
663
756
  */
664
757
  export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32 => {
665
758
  if (rangeCount < 1 || rangeCount * 2 > toI32(ranges.length) || quicVarintSize(ackDelay) === 0) {
@@ -700,6 +793,15 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
700
793
  * largest at least two below the smallest of the pair before it), with
701
794
  * `ackDelay` already scaled by this endpoint's exponent. Answers -1, writing
702
795
  * nothing, where `quicAckSize` answers 0 or the frame does not fit.
796
+ *
797
+ * @param buf The buffer to write into.
798
+ * @param at The offset of the frame's first byte.
799
+ * @param end One past the last byte the frame may use.
800
+ * @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
801
+ * @param rangeCount How many pairs to read from the front of `ranges`.
802
+ * @param ackDelay The ACK Delay field, already scaled: microseconds shifted right by the exponent.
803
+ * @returns The offset past the frame, or -1 with nothing written. The frame is always type 0x02, without ECN
804
+ * counts.
703
805
  */
704
806
  export const quicPutAck = (
705
807
  buf: u8[],
@@ -734,7 +836,15 @@ export const quicPutAck = (
734
836
  return cursor
735
837
  }
736
838
 
737
- /** Appends the ACK frame `quicPutAck` writes. Answers `false`, appending nothing, where it refuses. */
839
+ /**
840
+ * Appends the ACK frame `quicPutAck` writes. Answers `false`, appending nothing, where it refuses.
841
+ *
842
+ * @param out The bytes to append the frame to.
843
+ * @param ranges Packet number pairs laid out flat, `[smallest, largest]` for each range, highest range first.
844
+ * @param rangeCount How many pairs to read from the front of `ranges`.
845
+ * @param ackDelay The ACK Delay field, already scaled by this endpoint's `ack_delay_exponent`.
846
+ * @returns True when the frame was appended.
847
+ */
738
848
  export const quicPushAck = (out: u8[], ranges: i64[], rangeCount: i32, ackDelay: i64): boolean => {
739
849
  const size: i32 = quicAckSize(ranges, rangeCount, ackDelay)
740
850
  if (size === 0) {
@@ -747,6 +857,11 @@ export const quicPushAck = (out: u8[], ranges: i64[], rangeCount: i32, ackDelay:
747
857
  /**
748
858
  * The bytes a CRYPTO frame's header takes for `length` bytes of data at
749
859
  * `offset`: the type, the offset and the length.
860
+ *
861
+ * @param offset The crypto stream offset, in bytes, of the frame's first data byte, up to 2^62 − 1.
862
+ * @param length How many data bytes the frame carries.
863
+ * @returns The header's size in bytes, not counting the data, which is what a sender subtracts from a
864
+ * packet's room to see how much data fits.
750
865
  */
751
866
  export const quicCryptoOverhead = (offset: i64, length: i32): i32 =>
752
867
  1 + quicVarintSize(offset) + quicVarintSize(toI64(length))
@@ -755,6 +870,15 @@ export const quicCryptoOverhead = (offset: i64, length: i32): i32 =>
755
870
  * Writes a CRYPTO frame (§19.6) carrying `data[from .. from + length)` at
756
871
  * stream offset `offset`. Answers -1, writing nothing, for a window outside
757
872
  * `data`, an offset whose end passes 2^62 − 1, or no room.
873
+ *
874
+ * @param buf The buffer to write into.
875
+ * @param at The offset of the frame's first byte.
876
+ * @param end One past the last byte the frame may use.
877
+ * @param offset The crypto stream offset, in bytes, of the first byte carried.
878
+ * @param data The handshake bytes to take the window from; copied, not kept.
879
+ * @param from The offset in `data` of the first byte to carry.
880
+ * @param length How many bytes to carry.
881
+ * @returns The offset past the frame, or -1 with nothing written.
758
882
  */
759
883
  export const quicPutCrypto = (
760
884
  buf: u8[],
@@ -780,7 +904,16 @@ export const quicPutCrypto = (
780
904
  return cursor + length
781
905
  }
782
906
 
783
- /** Appends the CRYPTO frame `quicPutCrypto` writes. Answers `false`, appending nothing, where it refuses. */
907
+ /**
908
+ * Appends the CRYPTO frame `quicPutCrypto` writes. Answers `false`, appending nothing, where it refuses.
909
+ *
910
+ * @param out The bytes to append the frame to.
911
+ * @param offset The crypto stream offset, in bytes, of the first byte carried.
912
+ * @param data The handshake bytes to take the window from; copied, not kept.
913
+ * @param from The offset in `data` of the first byte to carry.
914
+ * @param length How many bytes to carry.
915
+ * @returns True when the frame was appended.
916
+ */
784
917
  export const quicPushCrypto = (out: u8[], offset: i64, data: u8[], from: i32, length: i32): boolean => {
785
918
  if (!quicFrameWindowFits(data, from, length) || offset < 0 || offset > QUIC_MAX_VARINT - toI64(length)) {
786
919
  return false
@@ -794,6 +927,11 @@ export const quicPushCrypto = (out: u8[], offset: i64, data: u8[], from: i32, le
794
927
  * The bytes a STREAM frame's header takes for `length` bytes of data on
795
928
  * `streamId` at `offset`: the type, the stream ID, the offset when it is not
796
929
  * zero, and the length, which `quicPutStream` always writes.
930
+ *
931
+ * @param streamId The stream the frame is for; meaningful up to 2^62 − 1.
932
+ * @param offset The stream offset, in bytes, of the frame's first data byte.
933
+ * @param length How many data bytes the frame carries.
934
+ * @returns The header's size in bytes, not counting the data.
797
935
  */
798
936
  export const quicStreamOverhead = (streamId: i64, offset: i64, length: i32): i32 =>
799
937
  1 + quicVarintSize(streamId) + (offset === 0 ? 0 : quicVarintSize(offset)) + quicVarintSize(toI64(length))
@@ -807,6 +945,17 @@ export const quicStreamOverhead = (streamId: i64, offset: i64, length: i32): i32
807
945
  * always written, so another frame may follow; the Offset field only when it
808
946
  * is not zero. Answers -1, writing nothing, for a `length` past the array, a
809
947
  * stream ID that is not a varint, an end past 2^62 − 1, or no room.
948
+ *
949
+ * @param buf The buffer to write into.
950
+ * @param at The offset of the frame's first byte.
951
+ * @param end One past the last byte the frame may use.
952
+ * @param streamId The stream the data belongs to.
953
+ * @param offset The stream offset, in bytes, of the first byte carried.
954
+ * @param data The bytes to carry, read as a ring; copied, not kept.
955
+ * @param from The index in `data` of the first byte to carry, inside the array when `length` is above zero.
956
+ * @param length How many bytes to carry, at most `data.length`; 0 for a frame that only carries FIN.
957
+ * @param fin Whether these bytes end the stream, so that `offset + length` is its final size.
958
+ * @returns The offset past the frame, or -1 with nothing written.
810
959
  */
811
960
  export const quicPutStream = (
812
961
  buf: u8[],
@@ -852,6 +1001,15 @@ export const quicPutStream = (
852
1001
  * `streamId` at `offset`, with FIN when `fin`, as `quicPutStream` writes it.
853
1002
  * Answers `false`, appending nothing, for a window outside `data`, a stream
854
1003
  * ID that is not a varint or an end past 2^62 − 1.
1004
+ *
1005
+ * @param out The bytes to append the frame to.
1006
+ * @param streamId The stream the data belongs to.
1007
+ * @param offset The stream offset, in bytes, of the first byte carried.
1008
+ * @param data The bytes to take the window from; copied, not kept, and not read as a ring.
1009
+ * @param from The offset in `data` of the first byte to carry.
1010
+ * @param length How many bytes to carry; 0 for a frame that only carries FIN.
1011
+ * @param fin Whether these bytes end the stream.
1012
+ * @returns True when the frame was appended.
855
1013
  */
856
1014
  export const quicPushStream = (
857
1015
  out: u8[],
@@ -896,6 +1054,14 @@ const quicFrameValueFits = (type: i32, value: i64): boolean => {
896
1054
  * MAX_STREAMS_UNI, DATA_BLOCKED, STREAMS_BLOCKED_BIDI, STREAMS_BLOCKED_UNI or
897
1055
  * RETIRE_CONNECTION_ID. Answers -1 for another type, a value no varint holds,
898
1056
  * a stream count past 2^60, or no room.
1057
+ *
1058
+ * @param buf The buffer to write into.
1059
+ * @param at The offset of the frame's first byte.
1060
+ * @param end One past the last byte the frame may use.
1061
+ * @param type One of the seven `QUIC_FRAME_*` types above.
1062
+ * @param value What the type carries: a limit in bytes for MAX_DATA and DATA_BLOCKED, a count of streams for
1063
+ * the four stream-count types, a sequence number for RETIRE_CONNECTION_ID.
1064
+ * @returns The offset past the frame, or -1 with nothing written.
899
1065
  */
900
1066
  export const quicPutValue = (buf: u8[], at: i32, end: i32, type: i32, value: i64): i32 => {
901
1067
  if (!quicFrameValueFits(type, value) || !quicFrameRoom(buf, at, end, 1 + quicVarintSize(value))) {
@@ -904,7 +1070,14 @@ export const quicPutValue = (buf: u8[], at: i32, end: i32, type: i32, value: i64
904
1070
  return quicFramePutVarint(buf, quicFramePutByte(buf, at, type), value)
905
1071
  }
906
1072
 
907
- /** Appends the frame `quicPutValue` writes. Answers `false`, appending nothing, where it refuses. */
1073
+ /**
1074
+ * Appends the frame `quicPutValue` writes. Answers `false`, appending nothing, where it refuses.
1075
+ *
1076
+ * @param out The bytes to append the frame to.
1077
+ * @param type One of the seven types `quicPutValue` writes.
1078
+ * @param value What the type carries: bytes, a count of streams or a sequence number, as for `quicPutValue`.
1079
+ * @returns True when the frame was appended.
1080
+ */
908
1081
  export const quicPushValue = (out: u8[], type: i32, value: i64): boolean => {
909
1082
  if (!quicFrameValueFits(type, value)) {
910
1083
  return false
@@ -917,6 +1090,15 @@ export const quicPushValue = (out: u8[], type: i32, value: i64): boolean => {
917
1090
  /**
918
1091
  * Writes a MAX_STREAM_DATA or STREAM_DATA_BLOCKED frame: the stream ID and
919
1092
  * the limit. Answers -1 for another type, a value no varint holds, or no room.
1093
+ *
1094
+ * @param buf The buffer to write into.
1095
+ * @param at The offset of the frame's first byte.
1096
+ * @param end One past the last byte the frame may use.
1097
+ * @param type `QUIC_FRAME_MAX_STREAM_DATA` or `QUIC_FRAME_STREAM_DATA_BLOCKED`.
1098
+ * @param streamId The stream the limit is for.
1099
+ * @param value The stream's limit in bytes: the new one for MAX_STREAM_DATA, the one the sender is blocked at
1100
+ * for STREAM_DATA_BLOCKED.
1101
+ * @returns The offset past the frame, or -1 with nothing written.
920
1102
  */
921
1103
  export const quicPutStreamValue = (
922
1104
  buf: u8[],
@@ -938,7 +1120,15 @@ export const quicPutStreamValue = (
938
1120
  return quicFramePutVarint(buf, cursor, value)
939
1121
  }
940
1122
 
941
- /** Appends the frame `quicPutStreamValue` writes. Answers `false`, appending nothing, where it refuses. */
1123
+ /**
1124
+ * Appends the frame `quicPutStreamValue` writes. Answers `false`, appending nothing, where it refuses.
1125
+ *
1126
+ * @param out The bytes to append the frame to.
1127
+ * @param type `QUIC_FRAME_MAX_STREAM_DATA` or `QUIC_FRAME_STREAM_DATA_BLOCKED`.
1128
+ * @param streamId The stream the limit is for.
1129
+ * @param value The stream's limit in bytes.
1130
+ * @returns True when the frame was appended.
1131
+ */
942
1132
  export const quicPushStreamValue = (out: u8[], type: i32, streamId: i64, value: i64): boolean => {
943
1133
  if (
944
1134
  (type !== QUIC_FRAME_MAX_STREAM_DATA && type !== QUIC_FRAME_STREAM_DATA_BLOCKED) ||
@@ -972,6 +1162,16 @@ const quicFrameNewCidSize = (sequence: i64, retirePriorTo: i64, cidLength: i32,
972
1162
  * of `connectionId`. Answers -1 for a connection ID outside 1 to 20 bytes or
973
1163
  * past the array, a reset token that is not 16 bytes, a Retire Prior To
974
1164
  * above the sequence number, a value no varint holds, or no room.
1165
+ *
1166
+ * @param buf The buffer to write into.
1167
+ * @param at The offset of the frame's first byte.
1168
+ * @param end One past the last byte the frame may use.
1169
+ * @param sequence The sequence number this endpoint gives the new connection ID.
1170
+ * @param retirePriorTo The peer is to retire every connection ID of a lower sequence number; 0 retires none.
1171
+ * @param connectionId The bytes of the new ID, which may be a longer buffer; copied, not kept.
1172
+ * @param cidLength How many bytes of `connectionId` are the ID, 1 to 20.
1173
+ * @param resetToken The 16-byte stateless reset token tied to the ID; copied, not kept.
1174
+ * @returns The offset past the frame, or -1 with nothing written.
975
1175
  */
976
1176
  export const quicPutNewConnectionId = (
977
1177
  buf: u8[],
@@ -1000,6 +1200,13 @@ export const quicPutNewConnectionId = (
1000
1200
  * Appends a NEW_CONNECTION_ID frame (§19.15). Answers `false` for a
1001
1201
  * connection ID outside 1 to 20 bytes, a reset token that is not 16 bytes, a
1002
1202
  * Retire Prior To above the sequence number, or a value no varint holds.
1203
+ *
1204
+ * @param out The bytes to append the frame to.
1205
+ * @param sequence The sequence number this endpoint gives the new connection ID.
1206
+ * @param retirePriorTo The peer is to retire every connection ID of a lower sequence number; 0 retires none.
1207
+ * @param connectionId The new ID, the whole array; copied, not kept.
1208
+ * @param resetToken The 16-byte stateless reset token tied to the ID; copied, not kept.
1209
+ * @returns True when the frame was appended; false, appending nothing, otherwise.
1003
1210
  */
1004
1211
  export const quicPushNewConnectionId = (
1005
1212
  out: u8[],
@@ -1032,6 +1239,14 @@ export const quicPushNewConnectionId = (
1032
1239
  * Writes a PATH_CHALLENGE or PATH_RESPONSE frame carrying the eight bytes
1033
1240
  * `data[from .. from + 8)`. Answers -1 for another type, a window outside
1034
1241
  * `data`, or no room.
1242
+ *
1243
+ * @param buf The buffer to write into.
1244
+ * @param at The offset of the frame's first byte.
1245
+ * @param end One past the last byte the frame may use.
1246
+ * @param type `QUIC_FRAME_PATH_CHALLENGE` or `QUIC_FRAME_PATH_RESPONSE`.
1247
+ * @param data Holds the eight bytes: fresh random ones for a challenge, the challenge's own for a response.
1248
+ * @param from The offset in `data` of the first of the eight bytes.
1249
+ * @returns The offset past the frame, nine bytes on, or -1 with nothing written.
1035
1250
  */
1036
1251
  export const quicPutPathData = (buf: u8[], at: i32, end: i32, type: i32, data: u8[], from: i32): i32 => {
1037
1252
  if (
@@ -1046,7 +1261,15 @@ export const quicPutPathData = (buf: u8[], at: i32, end: i32, type: i32, data: u
1046
1261
  return cursor + QUIC_PATH_DATA_SIZE
1047
1262
  }
1048
1263
 
1049
- /** Appends the frame `quicPutPathData` writes. Answers `false`, appending nothing, where it refuses. */
1264
+ /**
1265
+ * Appends the frame `quicPutPathData` writes. Answers `false`, appending nothing, where it refuses.
1266
+ *
1267
+ * @param out The bytes to append the frame to.
1268
+ * @param type `QUIC_FRAME_PATH_CHALLENGE` or `QUIC_FRAME_PATH_RESPONSE`.
1269
+ * @param data Holds the eight bytes to carry.
1270
+ * @param from The offset in `data` of the first of the eight bytes.
1271
+ * @returns True when the frame was appended.
1272
+ */
1050
1273
  export const quicPushPathData = (out: u8[], type: i32, data: u8[], from: i32): boolean => {
1051
1274
  if (
1052
1275
  (type !== QUIC_FRAME_PATH_CHALLENGE && type !== QUIC_FRAME_PATH_RESPONSE) ||
@@ -1061,6 +1284,15 @@ export const quicPushPathData = (out: u8[], type: i32, data: u8[], from: i32): b
1061
1284
  /**
1062
1285
  * Writes a RESET_STREAM frame (§19.4) or, with `finalSize` at -1, a
1063
1286
  * STOP_SENDING frame (§19.5). Answers -1 for a value no varint holds, or no room.
1287
+ *
1288
+ * @param buf The buffer to write into.
1289
+ * @param at The offset of the frame's first byte.
1290
+ * @param end One past the last byte the frame may use.
1291
+ * @param streamId The stream being reset or asked to stop.
1292
+ * @param errorCode The application's error code for why.
1293
+ * @param finalSize For RESET_STREAM, the stream's final size: how many bytes of it were ever sent.
1294
+ * -1 asks for STOP_SENDING instead.
1295
+ * @returns The offset past the frame, or -1 with nothing written.
1064
1296
  */
1065
1297
  export const quicPutStreamError = (
1066
1298
  buf: u8[],
@@ -1088,7 +1320,15 @@ export const quicPutStreamError = (
1088
1320
  return cursor
1089
1321
  }
1090
1322
 
1091
- /** Appends the frame `quicPutStreamError` writes. Answers `false`, appending nothing, where it refuses. */
1323
+ /**
1324
+ * Appends the frame `quicPutStreamError` writes. Answers `false`, appending nothing, where it refuses.
1325
+ *
1326
+ * @param out The bytes to append the frame to.
1327
+ * @param streamId The stream being reset or asked to stop.
1328
+ * @param errorCode The application's error code for why.
1329
+ * @param finalSize The stream's final size in bytes for RESET_STREAM, or -1 for STOP_SENDING.
1330
+ * @returns True when the frame was appended.
1331
+ */
1092
1332
  export const quicPushStreamError = (out: u8[], streamId: i64, errorCode: i64, finalSize: i64): boolean => {
1093
1333
  const reset: boolean = finalSize !== -1
1094
1334
  if (!quicFrameVarints3(streamId, errorCode, reset ? finalSize : 0)) {
@@ -1120,6 +1360,15 @@ const quicFrameCloseSize = (application: boolean, errorCode: i64, frameType: i64
1120
1360
  * caused the error (0 when none did); with `application` true the 0x1d form,
1121
1361
  * which has no frame type. `reason` is the phrase, UTF-8, possibly empty.
1122
1362
  * Answers -1 for a value no varint holds, or no room.
1363
+ *
1364
+ * @param buf The buffer to write into.
1365
+ * @param at The offset of the frame's first byte.
1366
+ * @param end One past the last byte the frame may use.
1367
+ * @param application Whether the close is the application's (0x1d) rather than the transport's (0x1c).
1368
+ * @param errorCode A `QUIC_ERROR_*` code for a transport close, the application's own code otherwise.
1369
+ * @param frameType The frame type that caused a transport error, 0 when none did; ignored when `application`.
1370
+ * @param reason The whole array is the reason phrase; copied, not kept.
1371
+ * @returns The offset past the frame, or -1 with nothing written.
1123
1372
  */
1124
1373
  export const quicPutConnectionClose = (
1125
1374
  buf: u8[],
@@ -1149,7 +1398,17 @@ export const quicPutConnectionClose = (
1149
1398
  return cursor + reasonLength
1150
1399
  }
1151
1400
 
1152
- /** Appends the frame `quicPutConnectionClose` writes. Answers `false`, appending nothing, where it refuses. */
1401
+ /**
1402
+ * Appends the frame `quicPutConnectionClose` writes. Answers `false`, appending nothing, where it
1403
+ * refuses.
1404
+ *
1405
+ * @param out The bytes to append the frame to.
1406
+ * @param application Whether the close is the application's (0x1d) rather than the transport's (0x1c).
1407
+ * @param errorCode A `QUIC_ERROR_*` code for a transport close, the application's own code otherwise.
1408
+ * @param frameType The frame type that caused a transport error, 0 when none did; ignored when `application`.
1409
+ * @param reason The whole array is the reason phrase, UTF-8, possibly empty; copied, not kept.
1410
+ * @returns True when the frame was appended.
1411
+ */
1153
1412
  export const quicPushConnectionClose = (
1154
1413
  out: u8[],
1155
1414
  application: boolean,
@@ -1168,6 +1427,10 @@ export const quicPushConnectionClose = (
1168
1427
  /**
1169
1428
  * Appends a NEW_TOKEN frame (§19.7) carrying `token`. Answers `false` for an
1170
1429
  * empty token, which §19.7 forbids.
1430
+ *
1431
+ * @param out The bytes to append the frame to.
1432
+ * @param token The whole array is the token the client is to present later; copied, not kept.
1433
+ * @returns True when the frame was appended; false, appending nothing, for an empty token.
1171
1434
  */
1172
1435
  export const quicPushNewToken = (out: u8[], token: u8[]): boolean => {
1173
1436
  const length: i32 = toI32(token.length)
@@ -1185,12 +1448,23 @@ export const quicPushNewToken = (out: u8[], token: u8[]): boolean => {
1185
1448
  * The size of the DATAGRAM frame `quicPutDatagram` writes for a payload of
1186
1449
  * `length` bytes: the type, the Length and the payload. This is the size RFC
1187
1450
  * 9221 §3 holds to `max_datagram_frame_size`.
1451
+ *
1452
+ * @param length The payload's size in bytes.
1453
+ * @returns The whole frame's size in bytes.
1188
1454
  */
1189
1455
  export const quicDatagramSize = (length: i32): i32 => 1 + quicVarintSize(toI64(length)) + length
1190
1456
 
1191
1457
  /**
1192
1458
  * Writes a DATAGRAM frame with a Length (type 0x31, RFC 9221 §4) carrying
1193
1459
  * `data[from .. from + length)`. Answers -1 for a window outside `data` or no room.
1460
+ *
1461
+ * @param buf The buffer to write into.
1462
+ * @param at The offset of the frame's first byte.
1463
+ * @param end One past the last byte the frame may use.
1464
+ * @param data The payload's bytes; copied, not kept.
1465
+ * @param from The offset in `data` of the payload's first byte.
1466
+ * @param length The payload's size in bytes.
1467
+ * @returns The offset past the frame, or -1 with nothing written.
1194
1468
  */
1195
1469
  export const quicPutDatagram = (buf: u8[], at: i32, end: i32, data: u8[], from: i32, length: i32): i32 => {
1196
1470
  if (!quicFrameWindowFits(data, from, length) || !quicFrameRoom(buf, at, end, quicDatagramSize(length))) {