@amritk/nish-aarch64-linux 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.
- package/INSTALL.md +67 -58
- package/bin/nish +0 -0
- package/docs/AI.md +43 -4
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +736 -499
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/scripts/build.sh +147 -5
- package/std/README.md +107 -82
- package/std/bytes.ts +23 -19
- package/std/compress/checksum.ts +1 -1
- package/std/crypto/aes.ts +48 -43
- package/std/crypto/base64url.ts +7 -5
- package/std/crypto/bcrypt-batch.ts +13 -13
- package/std/crypto/bcrypt.ts +33 -27
- package/std/crypto/chacha20poly1305.ts +16 -14
- package/std/crypto/ed25519.ts +19 -16
- package/std/crypto/hmac.ts +7 -6
- package/std/crypto/jwt.ts +23 -20
- package/std/crypto/p256.ts +63 -56
- package/std/crypto/p384.ts +23 -20
- package/std/crypto/pki.ts +18 -15
- package/std/crypto/rsa.ts +23 -19
- package/std/crypto/x25519.ts +11 -10
- package/std/crypto/x509.ts +69 -62
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +12 -11
- package/std/json-number.ts +12 -12
- package/std/json-tokenizer.ts +26 -22
- package/std/json-typed.ts +92 -78
- package/std/json-value.ts +38 -33
- package/std/json.ts +32 -28
- package/std/net/fetch.ts +1117 -230
- package/std/net/handler.ts +18 -0
- package/std/net/hpack.ts +29 -26
- package/std/net/http-compress.ts +27 -21
- package/std/net/http1-client-codec.ts +29 -25
- package/std/net/http1-codec.ts +44 -87
- package/std/net/http1-server.ts +66 -54
- package/std/net/http2-frame.ts +17 -16
- package/std/net/http2-tls.ts +17 -15
- package/std/net/http2.ts +67 -54
- package/std/net/http3-frame.ts +11 -10
- package/std/net/http3-server.ts +36 -35
- package/std/net/http3.ts +70 -58
- package/std/net/postgres.ts +178 -160
- package/std/net/prefork.ts +38 -34
- package/std/net/qpack.ts +15 -14
- package/std/net/quic-conn-ack.ts +1 -0
- package/std/net/quic-conn-cid.ts +11 -0
- package/std/net/quic-conn-params.ts +41 -14
- package/std/net/quic-datagram.ts +7 -0
- package/std/net/quic-frame.ts +48 -12
- package/std/net/quic-listener.ts +61 -44
- package/std/net/quic-packet.ts +71 -52
- package/std/net/quic-recovery.ts +59 -21
- package/std/net/quic-stream.ts +83 -55
- package/std/net/quic.ts +204 -152
- package/std/net/tls/client.ts +1 -0
- package/std/net/tls/codec.ts +81 -12
- package/std/net/tls/record-client.ts +30 -18
- package/std/net/tls/record-server.ts +74 -46
- package/std/net/tls/record.ts +33 -24
- package/std/net/tls/ticket.ts +2 -0
- package/std/net/tls/verify.ts +8 -7
- package/std/net/tls-tcp.ts +58 -50
- package/std/net/tls.ts +55 -43
- package/std/net/url.ts +12 -9
- package/std/net/websocket.ts +23 -19
- package/std/net/webtransport.ts +24 -18
- package/std/random.ts +8 -6
- package/std/regex.ts +70 -59
- package/std/sync/ring.ts +24 -21
- package/std/sync/shm.ts +9 -7
- package/std/term-io.ts +17 -14
- package/std/term-probe.ts +11 -10
- package/std/term-rate.ts +71 -61
- package/std/term.ts +73 -59
- package/std/text.ts +11 -10
- package/std/threads.ts +19 -15
- package/std/yaml-json.ts +17 -15
- package/std/yaml-write.ts +21 -14
package/std/net/quic-packet.ts
CHANGED
|
@@ -41,18 +41,19 @@
|
|
|
41
41
|
* which is what a server needs to send Version Negotiation, and reported as
|
|
42
42
|
* `QUIC_ERR_VERSION`.
|
|
43
43
|
*
|
|
44
|
-
* **What a parse refuses.** Every read is bounds-checked against the
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* `
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
44
|
+
* **What a parse refuses.** Every read is bounds-checked against the datagram,
|
|
45
|
+
* so nothing a peer sends can make this module panic.
|
|
46
|
+
*
|
|
47
|
+
* A header that runs past the datagram is `QUIC_ERR_TRUNCATED`; a clear fixed
|
|
48
|
+
* bit `QUIC_ERR_FIXED_BIT`; a connection ID over 20 bytes in a version 1 header
|
|
49
|
+
* `QUIC_ERR_CID_LENGTH`; a Length field reaching past the datagram
|
|
50
|
+
* `QUIC_ERR_LENGTH`; a packet too short to sample for header protection
|
|
51
|
+
* `QUIC_ERR_SAMPLE`; a payload that does not authenticate `QUIC_ERR_DECRYPT`;
|
|
52
|
+
* and nonzero reserved bits once both protections are off
|
|
53
|
+
* `QUIC_ERR_RESERVED_BITS`, which RFC 9000 §17.2 makes a connection error
|
|
54
|
+
* rather than a drop, so the payload is still handed back with it. The
|
|
55
|
+
* builders, whose inputs come from this side rather than from a peer, answer
|
|
56
|
+
* `null` for an argument out of range.
|
|
56
57
|
*
|
|
57
58
|
* **In place.** For a connection that allocates nothing per packet,
|
|
58
59
|
* `quicParseHeaderInto` reads a header as windows into the datagram, the
|
|
@@ -68,24 +69,24 @@
|
|
|
68
69
|
* datagram starts. A short header has none, so it runs to the datagram's end.
|
|
69
70
|
*
|
|
70
71
|
* **Secrets.** `QuicKeys` holds a packet key, an IV and a header-protection
|
|
71
|
-
* key, pointing into a `QuicKeysSlot`'s arrays. `quicKeys` and
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* `
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* secret, because anyone who reads the client's first
|
|
80
|
-
* (RFC 9001 §5.2), so `quicInitialSecrets` answers plain
|
|
81
|
-
* `quicKeysInto`, `quicKeysUpdateInto`, `quicKeyUpdateSecretInto`
|
|
82
|
-
* `quicInitialSecretsInto` derive from plain secrets over a caller's
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
72
|
+
* key, pointing into a `QuicKeysSlot`'s arrays. `quicKeys` and `quicKeysUpdate`
|
|
73
|
+
* take a traffic secret as a `Secret` from `nish:secret` and a slot the caller
|
|
74
|
+
* owns, and derive into the slot; a slot the caller owns is the caller's to
|
|
75
|
+
* wipe, and `QuicKeysSlot.wipe` zeroes it with stores no optimiser removes, AES
|
|
76
|
+
* schedules included (QUIC-1). `quicKeyUpdateSecret` answers the next secret as
|
|
77
|
+
* a `Secret`, and every derivation wipes its HKDF intermediates before it
|
|
78
|
+
* returns. `quicKeysWipe` zeroes the arrays one `QuicKeys` points at.
|
|
79
|
+
*
|
|
80
|
+
* The Initial keys are not secret, because anyone who reads the client's first
|
|
81
|
+
* DCID can derive them (RFC 9001 §5.2), so `quicInitialSecrets` answers plain
|
|
82
|
+
* arrays, and `quicKeysInto`, `quicKeysUpdateInto`, `quicKeyUpdateSecretInto`
|
|
83
|
+
* and `quicInitialSecretsInto` derive from plain secrets over a caller's
|
|
84
|
+
* `HkdfScratch`; the Handshake and 1-RTT keys are secret. Each derivation's own
|
|
85
|
+
* temporaries go with its arena block. A slot whose traffic secret is held as a
|
|
86
|
+
* `Secret` may also derive with `quicKeysHeldInto`, and ratchet the secret in
|
|
87
|
+
* place with `quicKeyUpdateSecretHeldInto`. Sealing and opening a packet wipe
|
|
88
|
+
* the nonce, which gives the IV away, and the header-protection mask once each
|
|
89
|
+
* is used (QUIC-2).
|
|
89
90
|
*
|
|
90
91
|
* **Constant time.** The AEADs and the header-protection masks are
|
|
91
92
|
* `nish/crypto/aes` and `nish/crypto/chacha20poly1305`, with their guarantees.
|
|
@@ -250,10 +251,18 @@ export class QuicHeader {
|
|
|
250
251
|
* windows out into the arrays above.
|
|
251
252
|
*/
|
|
252
253
|
dcidStart: i32 = 0
|
|
254
|
+
/**
|
|
255
|
+
* The Destination Connection ID's length in bytes, 0 to 20; a short header's is the length the
|
|
256
|
+
* reader passed.
|
|
257
|
+
*/
|
|
253
258
|
dcidLength: i32 = 0
|
|
259
|
+
/** Where the Source Connection ID starts in the datagram; 0 for a short header. */
|
|
254
260
|
scidStart: i32 = 0
|
|
261
|
+
/** The Source Connection ID's length in bytes, 0 to 20; 0 for a short header. */
|
|
255
262
|
scidLength: i32 = 0
|
|
263
|
+
/** Where an Initial's or a Retry's token starts in the datagram; 0 for every other type. */
|
|
256
264
|
tokenStart: i32 = 0
|
|
265
|
+
/** An Initial's or a Retry's token length in bytes; 0 for every other type. */
|
|
257
266
|
tokenLength: i32 = 0
|
|
258
267
|
|
|
259
268
|
/**
|
|
@@ -1199,21 +1208,22 @@ const quicPacketKeysUsable = (keys: QuicKeys): boolean => {
|
|
|
1199
1208
|
}
|
|
1200
1209
|
|
|
1201
1210
|
/**
|
|
1202
|
-
* The packet-protection keys of RFC 9001 §5.1 from a traffic secret, held as
|
|
1203
|
-
*
|
|
1211
|
+
* The packet-protection keys of RFC 9001 §5.1 from a traffic secret, held as a
|
|
1212
|
+
* `Secret` the caller still owns and wipes, derived into `slot`, which the
|
|
1204
1213
|
* caller owns too: the AEAD key (`quic key`), the IV (`quic iv`) and the
|
|
1205
1214
|
* header-protection key (`quic hp`), each HKDF-Expand-Label with an empty
|
|
1206
|
-
* context under the hash `aead` pairs with, written into the slot's arrays
|
|
1207
|
-
*
|
|
1208
|
-
*
|
|
1209
|
-
*
|
|
1210
|
-
*
|
|
1211
|
-
* derivation
|
|
1212
|
-
* Answers the slot's keys, which the next
|
|
1213
|
-
* `QuicKeysSlot.wipe` zeroes, or `null`,
|
|
1214
|
-
* this module does not know or a secret that is
|
|
1215
|
-
* Initial secrets are public (RFC 9001 §5.2), so a
|
|
1216
|
-
* keys wraps a copy of one in `secret`, or uses
|
|
1215
|
+
* context under the hash `aead` pairs with, written into the slot's arrays and
|
|
1216
|
+
* the AES schedules expanded there.
|
|
1217
|
+
*
|
|
1218
|
+
* The secret is read through a plain copy inside an arena block, which is
|
|
1219
|
+
* zeroed, and the derivation's temporaries go with the block, so the call keeps
|
|
1220
|
+
* in the arena only the `HkdfScratch` it makes for the derivation, which wipes
|
|
1221
|
+
* itself before the derivation returns. Answers the slot's keys, which the next
|
|
1222
|
+
* derivation into it overwrites and `QuicKeysSlot.wipe` zeroes, or `null`,
|
|
1223
|
+
* deriving nothing, for an `aead` this module does not know or a secret that is
|
|
1224
|
+
* not that hash's length. The Initial secrets are public (RFC 9001 §5.2), so a
|
|
1225
|
+
* caller deriving Initial keys wraps a copy of one in `secret`, or uses
|
|
1226
|
+
* `quicKeysInto`.
|
|
1217
1227
|
*
|
|
1218
1228
|
* @param slot The caller's slot to derive into; its earlier keys are overwritten.
|
|
1219
1229
|
* @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
|
|
@@ -1255,15 +1265,16 @@ export const quicKeyUpdateSecret = (aead: i32, trafficSecret: Secret<u8[]>): Sec
|
|
|
1255
1265
|
|
|
1256
1266
|
/**
|
|
1257
1267
|
* The next generation's keys in a key update (RFC 9001 §6), derived into
|
|
1258
|
-
* `slot`, which the caller owns and which must not be the slot `keys` live
|
|
1259
|
-
*
|
|
1260
|
-
*
|
|
1261
|
-
*
|
|
1262
|
-
*
|
|
1263
|
-
*
|
|
1264
|
-
*
|
|
1265
|
-
*
|
|
1266
|
-
*
|
|
1268
|
+
* `slot`, which the caller owns and which must not be the slot `keys` live in:
|
|
1269
|
+
* the packet key and IV from `nextSecret`, which `quicKeyUpdateSecret` made
|
|
1270
|
+
* from the current secret and the caller still owns, read through a plain copy
|
|
1271
|
+
* inside an arena block that is zeroed, and `keys`' header-protection key kept,
|
|
1272
|
+
* since header protection never changes with the phase.
|
|
1273
|
+
*
|
|
1274
|
+
* The new generation holds its own copy of that key and its own schedule, in
|
|
1275
|
+
* the slot, so wiping one generation leaves the other usable. Answers the
|
|
1276
|
+
* slot's keys, or `null`, deriving nothing, for keys `quicKeys` did not make,
|
|
1277
|
+
* keys that live in `slot`, or a secret of the wrong length.
|
|
1267
1278
|
*
|
|
1268
1279
|
* @param slot The caller's slot for the next generation; a different slot from the one `keys` live in.
|
|
1269
1280
|
* @param keys The current generation's keys, whose header-protection key is copied; left unchanged.
|
|
@@ -1335,13 +1346,21 @@ export const quicKeysWipe = (keys: QuicKeys): void => {
|
|
|
1335
1346
|
export class QuicKeysSlot {
|
|
1336
1347
|
/** The keys, pointing into the arrays below; `aead` is 0 until something is derived. */
|
|
1337
1348
|
keys: QuicKeys
|
|
1349
|
+
/** The 16-byte packet key an AES-128-GCM suite uses. */
|
|
1338
1350
|
key16: u8[]
|
|
1351
|
+
/** The 32-byte packet key an AES-256-GCM or ChaCha20-Poly1305 suite uses. */
|
|
1339
1352
|
key32: u8[]
|
|
1353
|
+
/** The 16-byte header-protection key an AES-128-GCM suite uses. */
|
|
1340
1354
|
hp16: u8[]
|
|
1355
|
+
/** The 32-byte header-protection key an AES-256-GCM or ChaCha20-Poly1305 suite uses. */
|
|
1341
1356
|
hp32: u8[]
|
|
1357
|
+
/** The AES-128 key schedule for packet protection, expanded from `key16`. */
|
|
1342
1358
|
packet128: AesKey
|
|
1359
|
+
/** The AES-256 key schedule for packet protection, expanded from `key32`. */
|
|
1343
1360
|
packet256: AesKey
|
|
1361
|
+
/** The AES-128 key schedule for header protection, expanded from `hp16`. */
|
|
1344
1362
|
hp128: AesKey
|
|
1363
|
+
/** The AES-256 key schedule for header protection, expanded from `hp32`. */
|
|
1345
1364
|
hp256: AesKey
|
|
1346
1365
|
|
|
1347
1366
|
/**
|
package/std/net/quic-recovery.ts
CHANGED
|
@@ -14,15 +14,16 @@
|
|
|
14
14
|
* if (now >= recovery.deadline(blocked)) { recovery.onTimeout(now, blocked); }
|
|
15
15
|
*
|
|
16
16
|
* **What it tracks.** Each space keeps its ack-eliciting packets in a fixed
|
|
17
|
-
* ring (`QuicSentPackets`), in the order they were sent, so the memory is
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* `
|
|
25
|
-
*
|
|
17
|
+
* ring (`QuicSentPackets`), in the order they were sent, so the memory is fixed
|
|
18
|
+
* when the connection is made and nothing is allocated per packet. A packet
|
|
19
|
+
* that elicits no acknowledgement (one that carries only ACK, PADDING or
|
|
20
|
+
* CONNECTION_CLOSE) is not recorded: it is never in flight, and neither an RTT
|
|
21
|
+
* sample nor a loss is ever taken from it (RFC 9002 §2, §5.1).
|
|
22
|
+
*
|
|
23
|
+
* The caller keeps what each packet carried in its own arrays, indexed by the
|
|
24
|
+
* slot `onPacketSent` answers, and reads `acked` and `lost` after each `onAck`
|
|
25
|
+
* and `onTimeout` to learn which slots were settled. A slot named there keeps
|
|
26
|
+
* its packet number, time and size until the next call.
|
|
26
27
|
*
|
|
27
28
|
* **Time and units.** Every time and every duration is the caller's
|
|
28
29
|
* monotonic clock in milliseconds, which is also the timer granularity
|
|
@@ -52,18 +53,21 @@
|
|
|
52
53
|
* **ECN** (§7.1, B.7; RFC 9000 §13.4.2 and its Appendix A.4). A caller that
|
|
53
54
|
* marks its packets ECT(0) asks `ecnCodepoint()` what to mark with, says so
|
|
54
55
|
* with `onEctSent` for every packet it marked, and hands an ACK_ECN frame's
|
|
55
|
-
* counts to `onAckEcn`.
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
56
|
+
* counts to `onAckEcn`.
|
|
57
|
+
*
|
|
58
|
+
* The counts are validated against the packets marked before they are used: an
|
|
59
|
+
* ACK that newly acknowledges a marked packet without counts, a count that
|
|
60
|
+
* falls, counts past the packets marked, or an increase in ECT(0) and ECN-CE
|
|
61
|
+
* short of the marked packets newly acknowledged fails validation, as does
|
|
62
|
+
* every packet of the testing period being lost, and a failed path is marked no
|
|
63
|
+
* more (`QUIC_RECOVERY_ECN_FAILED`). Only the first
|
|
64
|
+
* `QUIC_RECOVERY_ECN_TESTING_PACKETS` are marked until one validates.
|
|
65
|
+
*
|
|
66
|
+
* A rise in the ECN-CE count is a congestion event, from the largest packet
|
|
67
|
+
* newly acknowledged, so it halves the window once per recovery period as a
|
|
68
|
+
* loss does. Counts that arrive with an ACK that does not raise the largest
|
|
69
|
+
* acknowledged are neither validated nor used: they may be older than those
|
|
70
|
+
* already read.
|
|
67
71
|
*
|
|
68
72
|
* **What a peer cannot do.** Every input is checked: the ranges of an
|
|
69
73
|
* acknowledgement are read only up to the count given and the array's
|
|
@@ -180,10 +184,16 @@ const quicRecoveryMax = (a: i64, b: i64): i64 => (a > b ? a : b)
|
|
|
180
184
|
* acknowledged (§7.6.2).
|
|
181
185
|
*/
|
|
182
186
|
export class QuicSentPackets {
|
|
187
|
+
/** @internal Read only by this module; `pnAt` answers for a slot. */
|
|
183
188
|
pn: i64[]
|
|
189
|
+
/**
|
|
190
|
+
* When each slot's packet was sent, the `now` `onPacketSent` was given, in milliseconds;
|
|
191
|
+
* loss detection and RTT samples read it.
|
|
192
|
+
*/
|
|
184
193
|
timeSent: i64[]
|
|
185
194
|
/** When each packet was sent on the pacer's clock, in microseconds. */
|
|
186
195
|
fineSent: i64[]
|
|
196
|
+
/** @internal Read only by this module: each slot's packet size in bytes, for the congestion window. */
|
|
187
197
|
size: i32[]
|
|
188
198
|
/** Each slot's state, with `QUIC_RECOVERY_MARKED` set if its packet was sent marked ECT(0). */
|
|
189
199
|
state: u8[]
|
|
@@ -203,16 +213,24 @@ export class QuicSentPackets {
|
|
|
203
213
|
bytesInFlight: i64 = 0
|
|
204
214
|
private capacity: i32 = 0
|
|
205
215
|
private head: i32 = 0
|
|
216
|
+
/**
|
|
217
|
+
* How many packets the ring keeps, in flight and settled; a test reads it to see what a flight
|
|
218
|
+
* recorded.
|
|
219
|
+
*/
|
|
206
220
|
count: i32 = 0
|
|
207
221
|
/** How many recorded packets are still in flight: neither acknowledged nor lost. */
|
|
208
222
|
inFlight: i32 = 0
|
|
223
|
+
/** @internal Read by `net/quic.ts`: how many slots of `acked` the last `onAck` filled. */
|
|
209
224
|
ackedCount: i32 = 0
|
|
225
|
+
/** How many slots of `lost` the last `onAck` or `onTimeout` filled. */
|
|
210
226
|
lostCount: i32 = 0
|
|
211
227
|
/** How many of this space's packets, recorded or not, were sent marked ECT(0). */
|
|
212
228
|
ectSent: i64 = 0
|
|
213
229
|
/** The ECN counts of the last ACK_ECN frame validated in this space (RFC 9000 §19.3.2). */
|
|
214
230
|
ect0: i64 = 0
|
|
231
|
+
/** The ECT(1) count of the last ACK_ECN frame validated in this space. */
|
|
215
232
|
ect1: i64 = 0
|
|
233
|
+
/** The ECN-CE count of the last ACK_ECN frame validated in this space; a rise is a congestion event. */
|
|
216
234
|
ce: i64 = 0
|
|
217
235
|
|
|
218
236
|
/**
|
|
@@ -474,6 +492,10 @@ const quicRecoveryInRanges = (ranges: i64[], count: i32, pn: i64): boolean => {
|
|
|
474
492
|
* counts a test or a log reads (`congestionEvents`, `persistentCongestions`).
|
|
475
493
|
*/
|
|
476
494
|
export class QuicRecovery {
|
|
495
|
+
/**
|
|
496
|
+
* The sent-packet ring of each packet number space, indexed by `QUIC_RECOVERY_INITIAL` and its
|
|
497
|
+
* siblings.
|
|
498
|
+
*/
|
|
477
499
|
spaces: QuicSentPackets[]
|
|
478
500
|
/** latest_rtt (§5.1): the last sample, in milliseconds. */
|
|
479
501
|
latestRtt: i64 = 0
|
|
@@ -495,6 +517,10 @@ export class QuicRecovery {
|
|
|
495
517
|
recoveryStart: i64 = -1
|
|
496
518
|
/** The pacer's credit in bytes (§7.7), and the time it was last topped up in microseconds, or -1. */
|
|
497
519
|
pacerBudget: i64 = 0
|
|
520
|
+
/**
|
|
521
|
+
* When the pacer's credit was last topped up, in microseconds on the pacer's clock, or -1 before the
|
|
522
|
+
* first time.
|
|
523
|
+
*/
|
|
498
524
|
pacerTime: i64 = -1
|
|
499
525
|
/** The smoothed RTT the pacer's rate is taken from, in microseconds: `smoothedRtt` on the pacer's clock. */
|
|
500
526
|
pacerRtt: i64 = 0
|
|
@@ -506,6 +532,10 @@ export class QuicRecovery {
|
|
|
506
532
|
timeoutSpace: i32 = -1
|
|
507
533
|
/** How many congestion events reduced the window, and how many of them were persistent congestion. */
|
|
508
534
|
congestionEvents: i32 = 0
|
|
535
|
+
/**
|
|
536
|
+
* How many of `congestionEvents` were persistent congestion (§7.6), which collapses the window to
|
|
537
|
+
* its minimum.
|
|
538
|
+
*/
|
|
509
539
|
persistentCongestions: i32 = 0
|
|
510
540
|
/** How many congestion events a rise in the ECN-CE count caused (§7.1). */
|
|
511
541
|
ecnCongestionEvents: i32 = 0
|
|
@@ -513,7 +543,15 @@ export class QuicRecovery {
|
|
|
513
543
|
ecnState: i32 = 0
|
|
514
544
|
/** The packets marked while testing, and of those recorded, how many and how many of them were lost. */
|
|
515
545
|
ecnTestSent: i32 = 0
|
|
546
|
+
/**
|
|
547
|
+
* How many of the packets marked while testing ECN are recorded in a space's ring, so their loss can
|
|
548
|
+
* be seen.
|
|
549
|
+
*/
|
|
516
550
|
ecnTestRecorded: i32 = 0
|
|
551
|
+
/**
|
|
552
|
+
* How many marked packets were lost before ECN validated: once it reaches `ecnTestRecorded`, ECN
|
|
553
|
+
* fails (RFC 9000 A.4).
|
|
554
|
+
*/
|
|
517
555
|
ecnTestLost: i32 = 0
|
|
518
556
|
/** Whether the handshake is confirmed (RFC 9001 §4.1.2), which the caller sets: the peer's max_ack_delay then bounds its ACK delays, and the Application Data space has a probe timeout. */
|
|
519
557
|
handshakeConfirmed: boolean = false
|
package/std/net/quic-stream.ts
CHANGED
|
@@ -23,15 +23,17 @@
|
|
|
23
23
|
* const next: i32 = streams.putNextChunk(out, at, end) // `at` when no frame fits
|
|
24
24
|
*
|
|
25
25
|
* **IDs** (§2.1). The low bit says who opened a stream (0 the client, 1 the
|
|
26
|
-
* server) and the next one whether it carries data both ways (0) or only
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
26
|
+
* server) and the next one whether it carries data both ways (0) or only from
|
|
27
|
+
* its opener (1).
|
|
28
|
+
*
|
|
29
|
+
* This side is a server: it opens IDs 1 and 3 modulo 4, and the client 0 and 2.
|
|
30
|
+
* A frame for a stream of the client's that is past the limit this side
|
|
31
|
+
* advertised is STREAM_LIMIT_ERROR; for one of the server's that it has not
|
|
32
|
+
* opened, STREAM_STATE_ERROR; and a frame that only makes sense on a side the
|
|
33
|
+
* stream does not have (STREAM, RESET_STREAM or STREAM_DATA_BLOCKED on the
|
|
34
|
+
* server's unidirectional stream; MAX_STREAM_DATA or STOP_SENDING on the
|
|
35
|
+
* client's) is STREAM_STATE_ERROR (§19.4-§19.13). A client's stream opens every
|
|
36
|
+
* lower one of its type with it (§3.2).
|
|
35
37
|
*
|
|
36
38
|
* **Fixed slots.** The table is sized once, from the configuration: one slot
|
|
37
39
|
* per stream that may be open at once — as many client-opened bidirectional
|
|
@@ -43,31 +45,34 @@
|
|
|
43
45
|
*
|
|
44
46
|
* **Lent buffers.** A slot holds no buffers of its own. A side borrows a
|
|
45
47
|
* `bufferSize`-byte buffer, and a bit per byte beside it, from the table's
|
|
46
|
-
* `QuicStreamPools` when it first needs one: the receive side on the first
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
48
|
+
* `QuicStreamPools` when it first needs one: the receive side on the first byte
|
|
49
|
+
* that lands, the send side on the first byte written.
|
|
50
|
+
*
|
|
51
|
+
* Each goes back, its used extent zeroed, once it is done with: the receive
|
|
52
|
+
* side once the application has read to the final size or the stream is reset,
|
|
53
|
+
* the send side once every byte up to the final size is acknowledged or the
|
|
54
|
+
* stream is reset, never before, since a lost byte is resent from the send
|
|
55
|
+
* buffer.
|
|
56
|
+
*
|
|
57
|
+
* The pools hold two buffers per slot at most, the reserve's included, so a
|
|
58
|
+
* borrow never fails, and a server shares one pool across all its connections
|
|
59
|
+
* (`share`), so what it holds follows the streams moving bytes, not the slots
|
|
60
|
+
* it was sized for. Once the pools are warm, nothing a peer sends and nothing
|
|
61
|
+
* the application writes allocates.
|
|
58
62
|
*
|
|
59
63
|
* **Receiving** (§3.2, §2.2). Data lands in the receive buffer at its offset
|
|
60
64
|
* modulo the buffer's size, in any order and overlapping, with a bit set per
|
|
61
|
-
* byte; what continues the stream from the last byte read is what
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
65
|
+
* byte; what continues the stream from the last byte read is what `read` hands
|
|
66
|
+
* out. The credit this side gives a stream (MAX_STREAM_DATA) is never more than
|
|
67
|
+
* what the buffer can hold past the last byte read, so a byte beyond it is
|
|
68
|
+
* FLOW_CONTROL_ERROR and the buffer cannot overflow; it is raised by a whole
|
|
69
|
+
* buffer once the application has read half of one.
|
|
70
|
+
*
|
|
71
|
+
* The connection's credit (MAX_DATA) is raised the same way, by `windowData`
|
|
72
|
+
* once half of it is read. A FIN or RESET_STREAM fixes the final size, and a
|
|
73
|
+
* frame that disagrees with it, or a size below what already arrived, is
|
|
74
|
+
* FINAL_SIZE_ERROR (§4.5). A reset stream's bytes count as read for the
|
|
75
|
+
* connection's credit.
|
|
71
76
|
*
|
|
72
77
|
* **Sending** (§3.1). The application writes into the send buffer as far as
|
|
73
78
|
* it has room, a chunk at a time: what `write` takes is its back-pressure,
|
|
@@ -247,39 +252,43 @@ export const quicStreamSetBit = (bits: u8[], k: i32, on: boolean): void => {
|
|
|
247
252
|
const quicStreamMin = (a: i64, b: i64): i64 => (a < b ? a : b)
|
|
248
253
|
|
|
249
254
|
/**
|
|
250
|
-
* The buffers a connection's streams borrow: `data` lends the receive and
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
* send side that finds the pools
|
|
259
|
-
* (`lendSend`): with that side holding
|
|
260
|
-
* the one left over is in the reserve.
|
|
255
|
+
* The buffers a connection's streams borrow: `data` lends the receive and send
|
|
256
|
+
* buffers of `bufferSize` bytes, `bits` the bitmaps beside them, a bit per
|
|
257
|
+
* byte. One table makes a private set for itself; a server makes one set for
|
|
258
|
+
* every slot and hands it to each connection's table with `QuicStreams.share`,
|
|
259
|
+
* so the memory follows the streams moving bytes rather than the slots the
|
|
260
|
+
* server was sized for. Each pool's ceiling is two buffers for every stream it
|
|
261
|
+
* serves, one a side, which is what holding every buffer at once costs.
|
|
262
|
+
*
|
|
263
|
+
* The reserve below counts against it too, so a send side that finds the pools
|
|
264
|
+
* out takes the reserve's last buffer (`lendSend`): with that side holding
|
|
265
|
+
* none, not every buffer is lent, and the one left over is in the reserve.
|
|
261
266
|
*
|
|
262
267
|
* Frames are handled inside the arena scope a packet is opened in, where
|
|
263
268
|
* nothing may allocate, grow a list or push onto one. So the frames lend and
|
|
264
|
-
* take back receive buffers through a reserve the pools keep beside their
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
269
|
+
* take back receive buffers through a reserve the pools keep beside their free
|
|
270
|
+
* lists, by index alone: `lend` hands one out, `keep` takes one back zeroed.
|
|
271
|
+
* Before each scope `prime` trims the reserve to `hold` buffers, giving the
|
|
272
|
+
* rest to the pools, and tops it up to as many as the packet could start
|
|
273
|
+
* receive sides, taking them.
|
|
274
|
+
*
|
|
275
|
+
* `hold` is one connection's stream count, and the reserve three times that
|
|
276
|
+
* long, so a packet that lends nothing and takes back every buffer its
|
|
277
|
+
* connection held still fits. Once warm, the reserve mostly circulates on its
|
|
278
|
+
* own and `prime` costs a handful of comparisons, with no take or give. The
|
|
279
|
+
* pools count the reserve's buffers as lent. `prime` and the `drain` before
|
|
280
|
+
* each give-back outside a packet trim it to `hold` for the whole server, not
|
|
281
|
+
* each connection, but `keep` inside a packet can raise it to three times
|
|
282
|
+
* `hold`, and it stays there until the next `prime` or `drain`.
|
|
277
283
|
*/
|
|
278
284
|
export class QuicStreamPools {
|
|
285
|
+
/** The pool of stream buffers, `bufferSize` bytes each, that the send and receive sides borrow. */
|
|
279
286
|
data: BufferPool
|
|
287
|
+
/** The pool of bitmaps beside them, a bit per buffer byte: which bytes are unread or acknowledged. */
|
|
280
288
|
bits: BufferPool
|
|
281
289
|
/** The reserve: `ready` buffers and their bitmaps, taken from the pools and not lent to a stream; the rest of each list is `none`. */
|
|
282
290
|
readyData: u8[][]
|
|
291
|
+
/** The reserve's bitmaps, one beside each `readyData` buffer at the same index. */
|
|
283
292
|
readyBits: u8[][]
|
|
284
293
|
/** The empty array a free reserve entry holds. */
|
|
285
294
|
private none: u8[]
|
|
@@ -460,6 +469,7 @@ export const quicStreamGrowFree = (free: u8[][], target: i32, none: u8[]): void
|
|
|
460
469
|
* it asked this side to stop — and written only by `QuicStreams`.
|
|
461
470
|
*/
|
|
462
471
|
export class QuicStream {
|
|
472
|
+
/** The stream ID (RFC 9000 §2.1), or -1 while the slot is free. */
|
|
463
473
|
id: i64 = -1
|
|
464
474
|
/**
|
|
465
475
|
* The receive buffer: byte `o` of the stream at `o % size`, its bit in
|
|
@@ -467,6 +477,7 @@ export class QuicStream {
|
|
|
467
477
|
* first byte arrives and after the side is done, and borrowed in between.
|
|
468
478
|
*/
|
|
469
479
|
recvBuf: u8[]
|
|
480
|
+
/** The bitmap beside `recvBuf`: a bit per buffer byte, set while that byte has arrived and is unread. */
|
|
470
481
|
recvHave: u8[]
|
|
471
482
|
/**
|
|
472
483
|
* How far the application has read.
|
|
@@ -495,6 +506,7 @@ export class QuicStream {
|
|
|
495
506
|
* packet is sent again from it.
|
|
496
507
|
*/
|
|
497
508
|
sendBuf: u8[]
|
|
509
|
+
/** The bitmap beside `sendBuf`: a bit per buffer byte, set once acknowledged and not yet released. */
|
|
498
510
|
sendAcked: u8[]
|
|
499
511
|
/** Every byte below this was acknowledged, and its room released. */
|
|
500
512
|
sendBase: i64 = 0
|
|
@@ -509,6 +521,7 @@ export class QuicStream {
|
|
|
509
521
|
sendLimit: i64 = 0
|
|
510
522
|
/** Bytes a lost packet carried, `[resendLow, resendHigh)`, or -1; `resendFin` when the FIN was among them. */
|
|
511
523
|
resendLow: i64 = -1
|
|
524
|
+
/** One past the last byte to send again, or -1 when nothing is owed; see `resendLow`. */
|
|
512
525
|
resendHigh: i64 = -1
|
|
513
526
|
/**
|
|
514
527
|
* The error code of this side's RESET_STREAM, and its final size.
|
|
@@ -529,7 +542,9 @@ export class QuicStream {
|
|
|
529
542
|
* @internal Read in quic-stream.ts outside the class.
|
|
530
543
|
*/
|
|
531
544
|
size: i32 = 0
|
|
545
|
+
/** The send side's state (RFC 9000 §3.1), a `QUIC_SEND_*` constant. */
|
|
532
546
|
sendState: i32 = 0
|
|
547
|
+
/** The receive side's state (RFC 9000 §3.2), a `QUIC_RECV_*` constant. */
|
|
533
548
|
recvState: i32 = 0
|
|
534
549
|
/**
|
|
535
550
|
* Whether the application wrote the FIN, a frame carried it, and the peer acknowledged it.
|
|
@@ -715,6 +730,7 @@ export class QuicStream {
|
|
|
715
730
|
* counters are readable for a test or a log.
|
|
716
731
|
*/
|
|
717
732
|
export class QuicStreams {
|
|
733
|
+
/** The stream slots, a fixed count set at construction; a free one has `id` -1. */
|
|
718
734
|
slots: QuicStream[]
|
|
719
735
|
/** Open addressing by ID: `index[h]` is a slot plus one, 0 for empty. */
|
|
720
736
|
private index: i32[]
|
|
@@ -747,10 +763,15 @@ export class QuicStreams {
|
|
|
747
763
|
localStreams: i64 = 0
|
|
748
764
|
/** How many of each type the client opened, the limit advertised, and how many of them finished. */
|
|
749
765
|
peerBidiOpened: i64 = 0
|
|
766
|
+
/** How many unidirectional streams the client opened. */
|
|
750
767
|
peerUniOpened: i64 = 0
|
|
768
|
+
/** The bidirectional stream limit this side last advertised to the client (MAX_STREAMS). */
|
|
751
769
|
peerBidiLimit: i64 = 0
|
|
770
|
+
/** The unidirectional stream limit this side last advertised to the client (MAX_STREAMS). */
|
|
752
771
|
peerUniLimit: i64 = 0
|
|
772
|
+
/** How many of the client's bidirectional streams finished, which is what lets the limit rise. */
|
|
753
773
|
peerBidiClosed: i64 = 0
|
|
774
|
+
/** How many of the client's unidirectional streams finished, which is what lets the limit rise. */
|
|
754
775
|
peerUniClosed: i64 = 0
|
|
755
776
|
/** How many of each type this side opened, the client's limits, and how many of this side's are open. */
|
|
756
777
|
private localBidiOpened: i64 = 0
|
|
@@ -764,12 +785,15 @@ export class QuicStreams {
|
|
|
764
785
|
private peerUniCredit: i64 = 0
|
|
765
786
|
/** The connection's receive credit: what was advertised, its window, what arrived, and what was read. */
|
|
766
787
|
recvMaxData: i64 = 0
|
|
788
|
+
/** The connection's receive window in bytes: how far past what was read the credit is raised. */
|
|
767
789
|
windowData: i64 = 0
|
|
790
|
+
/** Bytes the connection received, counted at each stream's highest offset, against `recvMaxData`. */
|
|
768
791
|
recvTotal: i64 = 0
|
|
769
792
|
private recvConsumed: i64 = 0
|
|
770
793
|
/** The connection's send credit from the peer, what was sent against it, and what is written but not sent. */
|
|
771
794
|
sendMaxData: i64 = 0
|
|
772
795
|
private sendTotal: i64 = 0
|
|
796
|
+
/** Bytes the application wrote to every stream, sent or not; a write may not take it past `sendMaxData`. */
|
|
773
797
|
writtenTotal: i64 = 0
|
|
774
798
|
/** The transport error `locate` answered -2 for. */
|
|
775
799
|
private locateError: i64 = 0
|
|
@@ -780,11 +804,15 @@ export class QuicStreams {
|
|
|
780
804
|
private streamsBlockedUniAt: i64 = -1
|
|
781
805
|
/** What the last `putNextChunk` or `putNextControl` wrote, for the packet's record. */
|
|
782
806
|
lastId: i64 = 0
|
|
807
|
+
/** The stream offset of the last chunk `putNextChunk` wrote. */
|
|
783
808
|
lastOffset: i64 = 0
|
|
809
|
+
/** The stream the last control frame `putNextControl` wrote names, or 0 for a connection-level one. */
|
|
784
810
|
lastValue: i64 = 0
|
|
785
811
|
/** The bytes of each stream's buffer, each way. */
|
|
786
812
|
bufferSize: i32 = 0
|
|
813
|
+
/** The bytes of the last chunk `putNextChunk` wrote. */
|
|
787
814
|
lastLength: i32 = 0
|
|
815
|
+
/** Which control frame `putNextControl` last wrote, a `QUIC_STREAM_CONTROL_*` constant. */
|
|
788
816
|
lastKind: i32 = 0
|
|
789
817
|
private eventHead: i32 = 0
|
|
790
818
|
private eventCount: i32 = 0
|