nish 0.44.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/docs/AI.md +18 -16
- package/package.json +5 -5
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +47 -5
- package/runtime/nish.mjs +4 -0
- package/std/README.md +3 -3
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1275 -217
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/net/quic-stream.ts
CHANGED
|
@@ -98,6 +98,7 @@
|
|
|
98
98
|
import {
|
|
99
99
|
QUIC_ERROR_FINAL_SIZE,
|
|
100
100
|
QUIC_ERROR_FLOW_CONTROL,
|
|
101
|
+
QUIC_ERROR_INTERNAL,
|
|
101
102
|
QUIC_ERROR_NO_ERROR,
|
|
102
103
|
QUIC_ERROR_STREAM_LIMIT,
|
|
103
104
|
QUIC_ERROR_STREAM_STATE,
|
|
@@ -295,9 +296,14 @@ const quicStreamScan = (bits: u8[], from: i32, to: i32, on: boolean): i32 => {
|
|
|
295
296
|
let at: i32 = k >> 3
|
|
296
297
|
// `at` is `k >> 3` and `whole` is within the array, so the bounds on `at` hold whenever `k`'s do; they
|
|
297
298
|
// are what the bounds pass reads to drop the check on `bits[at]`.
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
299
|
+
// `k` is a multiple of 8 below `whole` here, so the loop steps `at` alone, a byte at a time, and
|
|
300
|
+
// `k` follows it: `at` stays below `whole >> 3`, so neither can wrap.
|
|
301
|
+
if (k < whole) {
|
|
302
|
+
const wholeAt: i32 = whole >> 3
|
|
303
|
+
while (at >= 0 && at < bytes && at < wholeAt && toI32(bits[at]) === full) {
|
|
304
|
+
at = at + 1
|
|
305
|
+
}
|
|
306
|
+
k = at << 3
|
|
301
307
|
}
|
|
302
308
|
while (k < to) {
|
|
303
309
|
if (quicStreamBit(bits, k) !== on) {
|
|
@@ -312,26 +318,37 @@ const quicStreamScan = (bits: u8[], from: i32, to: i32, on: boolean): i32 => {
|
|
|
312
318
|
* The first of `count` bytes of a ring of `size` from index `slot`: they run
|
|
313
319
|
* to the ring's end at most, and the rest wrap to index 0.
|
|
314
320
|
*/
|
|
315
|
-
const quicStreamFirstRun = (size: i32, slot: i32, count: i32): i32 =>
|
|
316
|
-
|
|
321
|
+
const quicStreamFirstRun = (size: i32, slot: i32, count: i32): i32 => {
|
|
322
|
+
// In `i64`, where `size - slot` cannot wrap; the result is `count` or a room no larger than it.
|
|
323
|
+
const room: i64 = toI64(size) - toI64(slot)
|
|
324
|
+
return toI64(count) < room ? count : toI32(room)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* `a + b` and `a - b` for ring offsets, summed in `i64`: every caller's operands are an index into a
|
|
329
|
+
* ring or a source and a run inside it, so the true result fits an `i32` and the conversion back is
|
|
330
|
+
* exact.
|
|
331
|
+
*/
|
|
332
|
+
const quicStreamAdd = (a: i32, b: i32): i32 => toI32(toI64(a) + toI64(b))
|
|
333
|
+
const quicStreamSub = (a: i32, b: i32): i32 => toI32(toI64(a) - toI64(b))
|
|
317
334
|
|
|
318
335
|
/** Sets or clears the bits of `count` ring indexes from `slot`, wrapping at `size`: at most two runs. */
|
|
319
336
|
const quicStreamMarkRing = (bits: u8[], size: i32, slot: i32, count: i32, on: boolean): void => {
|
|
320
337
|
const first: i32 = quicStreamFirstRun(size, slot, count)
|
|
321
|
-
quicStreamSetBits(bits, slot, slot
|
|
338
|
+
quicStreamSetBits(bits, slot, quicStreamAdd(slot, first), on)
|
|
322
339
|
if (count > first) {
|
|
323
|
-
quicStreamSetBits(bits, 0, count
|
|
340
|
+
quicStreamSetBits(bits, 0, quicStreamSub(count, first), on)
|
|
324
341
|
}
|
|
325
342
|
}
|
|
326
343
|
|
|
327
344
|
/** How many of `count` ring indexes from `slot`, wrapping at `size`, have their bit `on` before the first that does not. */
|
|
328
345
|
const quicStreamRingRun = (bits: u8[], size: i32, slot: i32, count: i32, on: boolean): i32 => {
|
|
329
346
|
const first: i32 = quicStreamFirstRun(size, slot, count)
|
|
330
|
-
const got: i32 = quicStreamScan(bits, slot, slot
|
|
347
|
+
const got: i32 = quicStreamSub(quicStreamScan(bits, slot, quicStreamAdd(slot, first), on), slot)
|
|
331
348
|
if (got < first || count <= first) {
|
|
332
349
|
return got
|
|
333
350
|
}
|
|
334
|
-
return first
|
|
351
|
+
return quicStreamAdd(first, quicStreamScan(bits, 0, quicStreamSub(count, first), on))
|
|
335
352
|
}
|
|
336
353
|
|
|
337
354
|
/**
|
|
@@ -344,7 +361,7 @@ const quicStreamRingIn = (ring: u8[], slot: i32, src: u8[], from: i32, count: i3
|
|
|
344
361
|
const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
|
|
345
362
|
copyBytes(ring, slot, src, from, first)
|
|
346
363
|
if (count > first) {
|
|
347
|
-
copyBytes(ring, 0, src, from
|
|
364
|
+
copyBytes(ring, 0, src, quicStreamAdd(from, first), quicStreamSub(count, first))
|
|
348
365
|
}
|
|
349
366
|
}
|
|
350
367
|
|
|
@@ -357,8 +374,82 @@ const quicStreamRingOut = (dst: u8[], at: i32, ring: u8[], slot: i32, count: i32
|
|
|
357
374
|
const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
|
|
358
375
|
copyBytes(dst, at, ring, slot, first)
|
|
359
376
|
if (count > first) {
|
|
360
|
-
copyBytes(dst, at
|
|
377
|
+
copyBytes(dst, quicStreamAdd(at, first), ring, 0, quicStreamSub(count, first))
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** The bytes of a bitmap holding a bit for each of `bytes` bytes: `bytes` over 8, rounded up. */
|
|
382
|
+
const quicStreamBitBytes = (bytes: i32): i32 => toI32((toI64(bytes) + 7) >> 3)
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Fills a new reserve list with `none`, three entries for each of `streams` streams: a packet that
|
|
386
|
+
* lends nothing and takes back every buffer its connection held still fits.
|
|
387
|
+
*/
|
|
388
|
+
const quicStreamFillReserve = (list: u8[][], none: u8[], streams: i32): void => {
|
|
389
|
+
for (let k: i32 = 0; k < streams; k += 1) {
|
|
390
|
+
list.push(none)
|
|
391
|
+
list.push(none)
|
|
392
|
+
list.push(none)
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* A list of `count` zeros, pushed one at a time.
|
|
398
|
+
*
|
|
399
|
+
* @param count How many zeros; none when 0 or less.
|
|
400
|
+
* @returns The list.
|
|
401
|
+
*/
|
|
402
|
+
const quicStreamZeros = (count: i32): i32[] => {
|
|
403
|
+
const list: i32[] = []
|
|
404
|
+
for (let k: i32 = 0; k < count; k += 1) {
|
|
405
|
+
list.push(0)
|
|
406
|
+
}
|
|
407
|
+
return list
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** 2^63 − 1, the largest `i64`, which a sum past it is held at. */
|
|
411
|
+
const quicStreamTop = (): i64 => toI64((toU64(1) << toU64(63)) - toU64(1))
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* `a + b`, held at 2^63 − 1 when the true sum is past it and at −2^63 when it is below. Stream
|
|
415
|
+
* offsets, counts and limits are varints, below 2^62, so a held sum is one no peer can send:
|
|
416
|
+
* a limit that is never reached. Taken in `u64`, whose `+` wraps, and read back as the `i64`
|
|
417
|
+
* with the same bits, which is the true sum whenever that fits.
|
|
418
|
+
*/
|
|
419
|
+
const quicStreamSum = (a: i64, b: i64): i64 => {
|
|
420
|
+
const sum: i64 = toI64(toU64(a) + toU64(b))
|
|
421
|
+
if (a >= 0 && b >= 0 && sum < 0) {
|
|
422
|
+
return quicStreamTop()
|
|
423
|
+
}
|
|
424
|
+
if (a < 0 && b < 0 && sum >= 0) {
|
|
425
|
+
return toI64(toU64(1) << toU64(63))
|
|
426
|
+
}
|
|
427
|
+
return sum
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** `a − b`, held at 2^63 − 1 / −2^63 where the true difference passes either, as `quicStreamSum` holds a sum. */
|
|
431
|
+
const quicStreamLess = (a: i64, b: i64): i64 => {
|
|
432
|
+
const diff: i64 = toI64(toU64(a) - toU64(b))
|
|
433
|
+
if (a >= 0 && b < 0 && diff < 0) {
|
|
434
|
+
return quicStreamTop()
|
|
435
|
+
}
|
|
436
|
+
if (a < 0 && b >= 0 && diff >= 0) {
|
|
437
|
+
return toI64(toU64(1) << toU64(63))
|
|
438
|
+
}
|
|
439
|
+
return diff
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* A pool's ceiling, two buffers for each of `streams` streams on each of `connections`
|
|
444
|
+
* connections, held at 2^31 − 1 (an `i32`, as `BufferPool` counts): a ceiling no server's
|
|
445
|
+
* memory reaches. 0 when either count is.
|
|
446
|
+
*/
|
|
447
|
+
const quicStreamCeiling = (streams: i32, connections: i32): i32 => {
|
|
448
|
+
if (streams <= 0 || connections <= 0) {
|
|
449
|
+
return 0
|
|
361
450
|
}
|
|
451
|
+
const want: u64 = toU64(streams) * toU64(connections) * toU64(2)
|
|
452
|
+
return want >= toU64(2147483647) ? 2147483647 : toI32(want)
|
|
362
453
|
}
|
|
363
454
|
|
|
364
455
|
/** The smaller of two `i64`s. */
|
|
@@ -424,16 +515,18 @@ export class QuicStreamPools {
|
|
|
424
515
|
* `streams * 2 * connections` buffers.
|
|
425
516
|
*/
|
|
426
517
|
constructor(bufferSize: i32, streams: i32, connections: i32) {
|
|
427
|
-
this.data = new BufferPool("QuicStreams.data", bufferSize, streams
|
|
428
|
-
this.bits = new BufferPool(
|
|
518
|
+
this.data = new BufferPool("QuicStreams.data", bufferSize, quicStreamCeiling(streams, connections))
|
|
519
|
+
this.bits = new BufferPool(
|
|
520
|
+
"QuicStreams.bits",
|
|
521
|
+
quicStreamBitBytes(bufferSize),
|
|
522
|
+
quicStreamCeiling(streams, connections)
|
|
523
|
+
)
|
|
429
524
|
this.none = []
|
|
430
525
|
this.hold = streams
|
|
431
526
|
this.readyData = []
|
|
432
527
|
this.readyBits = []
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
this.readyBits.push(this.none)
|
|
436
|
-
}
|
|
528
|
+
quicStreamFillReserve(this.readyData, this.none, streams)
|
|
529
|
+
quicStreamFillReserve(this.readyBits, this.none, streams)
|
|
437
530
|
}
|
|
438
531
|
|
|
439
532
|
/**
|
|
@@ -444,10 +537,24 @@ export class QuicStreamPools {
|
|
|
444
537
|
*/
|
|
445
538
|
prime(need: i32): void {
|
|
446
539
|
this.drain(this.hold)
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
540
|
+
const data: u8[][] = this.readyData
|
|
541
|
+
const bits: u8[][] = this.readyBits
|
|
542
|
+
// The two lists are made the same length and `ready` never leaves `0..length`, so the bounds
|
|
543
|
+
// below stop the loop only where `need` or the list's length would.
|
|
544
|
+
let ready: i32 = this.ready
|
|
545
|
+
while (ready >= 0 && ready < need && ready < toI32(data.length) && ready < toI32(bits.length)) {
|
|
546
|
+
// Taken first: the bounds above hold for the stores, which nothing between them can move.
|
|
547
|
+
const buf: u8[] = this.data.take()
|
|
548
|
+
const have: u8[] = this.bits.take()
|
|
549
|
+
if (ready >= toI32(data.length) || ready >= toI32(bits.length)) {
|
|
550
|
+
this.data.give(buf, QUIC_STREAM_NO_BYTES)
|
|
551
|
+
this.bits.give(have, QUIC_STREAM_NO_BYTES)
|
|
552
|
+
break
|
|
553
|
+
}
|
|
554
|
+
data[ready] = buf
|
|
555
|
+
bits[ready] = have
|
|
556
|
+
ready = ready + 1
|
|
557
|
+
this.ready = ready
|
|
451
558
|
}
|
|
452
559
|
this.fit()
|
|
453
560
|
}
|
|
@@ -457,12 +564,21 @@ export class QuicStreamPools {
|
|
|
457
564
|
* @internal Read in quic-stream.ts outside the class.
|
|
458
565
|
*/
|
|
459
566
|
drain(most: i32): void {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
567
|
+
const data: u8[][] = this.readyData
|
|
568
|
+
const bits: u8[][] = this.readyBits
|
|
569
|
+
// `ready` never leaves `1..length` while the reserve holds a buffer, so the bounds below stop
|
|
570
|
+
// the loop only where `most` would.
|
|
571
|
+
let ready: i32 = this.ready
|
|
572
|
+
while (ready > most && ready > 0 && ready <= toI32(data.length) && ready <= toI32(bits.length)) {
|
|
573
|
+
const at: i32 = ready - 1
|
|
574
|
+
const buf: u8[] = data[at]
|
|
575
|
+
const have: u8[] = bits[at]
|
|
576
|
+
data[at] = this.none
|
|
577
|
+
bits[at] = this.none
|
|
578
|
+
this.data.give(buf, QUIC_STREAM_NO_BYTES)
|
|
579
|
+
this.bits.give(have, QUIC_STREAM_NO_BYTES)
|
|
580
|
+
ready = at
|
|
581
|
+
this.ready = ready
|
|
466
582
|
}
|
|
467
583
|
}
|
|
468
584
|
|
|
@@ -479,7 +595,14 @@ export class QuicStreamPools {
|
|
|
479
595
|
if (made <= this.room) {
|
|
480
596
|
return
|
|
481
597
|
}
|
|
482
|
-
|
|
598
|
+
const room: i32 = this.room
|
|
599
|
+
// `room` is never above `made` (it is set to at most `made` or the ceiling), so doubling it
|
|
600
|
+
// stays inside an `i32` while `made` does; in `i64` it cannot wrap regardless.
|
|
601
|
+
let target: i32 = made
|
|
602
|
+
const doubled: i64 = toI64(room) + toI64(room)
|
|
603
|
+
if (doubled > toI64(made)) {
|
|
604
|
+
target = toI32(doubled)
|
|
605
|
+
}
|
|
483
606
|
if (target < made) {
|
|
484
607
|
target = made
|
|
485
608
|
}
|
|
@@ -492,18 +615,28 @@ export class QuicStreamPools {
|
|
|
492
615
|
}
|
|
493
616
|
|
|
494
617
|
/**
|
|
495
|
-
* Lends the reserve's last buffer, into `stream`'s receive side.
|
|
618
|
+
* Lends the reserve's last buffer, into `stream`'s receive side. Answers false, lending nothing,
|
|
619
|
+
* when the reserve is empty: a packet handled without `prime`, where a take would allocate inside
|
|
620
|
+
* the arena scope.
|
|
621
|
+
*
|
|
622
|
+
* @param stream The stream whose receive side takes the buffer and its bitmap.
|
|
623
|
+
* @returns Whether a buffer was lent.
|
|
496
624
|
* @internal Read in quic-stream.ts outside the class.
|
|
497
625
|
*/
|
|
498
|
-
lend(stream: QuicStream):
|
|
499
|
-
|
|
500
|
-
|
|
626
|
+
lend(stream: QuicStream): boolean {
|
|
627
|
+
const data: u8[][] = this.readyData
|
|
628
|
+
const bits: u8[][] = this.readyBits
|
|
629
|
+
const ready: i32 = this.ready
|
|
630
|
+
if (ready <= 0 || ready > toI32(data.length) || ready > toI32(bits.length)) {
|
|
631
|
+
return false
|
|
501
632
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
stream.
|
|
505
|
-
|
|
506
|
-
|
|
633
|
+
const at: i32 = ready - 1
|
|
634
|
+
this.ready = at
|
|
635
|
+
stream.recvBuf = data[at]
|
|
636
|
+
stream.recvHave = bits[at]
|
|
637
|
+
data[at] = this.none
|
|
638
|
+
bits[at] = this.none
|
|
639
|
+
return true
|
|
507
640
|
}
|
|
508
641
|
|
|
509
642
|
/**
|
|
@@ -515,12 +648,24 @@ export class QuicStreamPools {
|
|
|
515
648
|
* @internal Read in quic-stream.ts outside the class.
|
|
516
649
|
*/
|
|
517
650
|
lendSend(stream: QuicStream): void {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
651
|
+
const data: u8[][] = this.readyData
|
|
652
|
+
const bits: u8[][] = this.readyBits
|
|
653
|
+
const ready: i32 = this.ready
|
|
654
|
+
// `ready` never leaves `0..length`, so the two length bounds are never what sends a side with the
|
|
655
|
+
// reserve holding a buffer on to `take`.
|
|
656
|
+
if (
|
|
657
|
+
this.data.free.length === 0 &&
|
|
658
|
+
this.data.made >= this.data.ceiling &&
|
|
659
|
+
ready > 0 &&
|
|
660
|
+
ready <= toI32(data.length) &&
|
|
661
|
+
ready <= toI32(bits.length)
|
|
662
|
+
) {
|
|
663
|
+
const at: i32 = ready - 1
|
|
664
|
+
this.ready = at
|
|
665
|
+
stream.sendBuf = data[at]
|
|
666
|
+
stream.sendAcked = bits[at]
|
|
667
|
+
data[at] = this.none
|
|
668
|
+
bits[at] = this.none
|
|
524
669
|
return
|
|
525
670
|
}
|
|
526
671
|
stream.sendBuf = this.data.take()
|
|
@@ -531,19 +676,30 @@ export class QuicStreamPools {
|
|
|
531
676
|
/**
|
|
532
677
|
* Takes `buf` and its bitmap `have` back into the reserve, zeroing the
|
|
533
678
|
* first `used` bytes of the buffer and the bits of them. Safe inside an
|
|
534
|
-
* arena scope.
|
|
535
|
-
*
|
|
679
|
+
* arena scope. A reserve already full, more given back between two
|
|
680
|
+
* `drain`s than three times one connection's streams, takes nothing and
|
|
681
|
+
* answers false: the caller's stream keeps the pair, zeroed as a fresh
|
|
682
|
+
* loan is, since storing it anywhere else could allocate inside the scope.
|
|
683
|
+
*
|
|
684
|
+
* @param buf The receive buffer given back.
|
|
685
|
+
* @param have Its bitmap.
|
|
686
|
+
* @param used How many bytes of `buf` a frame reached, the extent zeroed.
|
|
687
|
+
* @returns Whether the reserve took the pair.
|
|
536
688
|
* @internal Read in quic-stream.ts outside the class.
|
|
537
689
|
*/
|
|
538
|
-
keep(buf: u8[], have: u8[], used: i32):
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
690
|
+
keep(buf: u8[], have: u8[], used: i32): boolean {
|
|
691
|
+
const data: u8[][] = this.readyData
|
|
692
|
+
const bits: u8[][] = this.readyBits
|
|
693
|
+
const ready: i32 = this.ready
|
|
542
694
|
buf.fill(0, 0, used)
|
|
543
|
-
have.fill(0, 0, (used
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
695
|
+
have.fill(0, 0, quicStreamBitBytes(used))
|
|
696
|
+
if (ready < 0 || ready >= toI32(data.length) || ready >= toI32(bits.length)) {
|
|
697
|
+
return false
|
|
698
|
+
}
|
|
699
|
+
data[ready] = buf
|
|
700
|
+
bits[ready] = have
|
|
701
|
+
this.ready = ready + 1
|
|
702
|
+
return true
|
|
547
703
|
}
|
|
548
704
|
|
|
549
705
|
/**
|
|
@@ -553,7 +709,11 @@ export class QuicStreamPools {
|
|
|
553
709
|
* @returns The buffers plus the bitmaps out on loan, so a side holding both counts 2.
|
|
554
710
|
*/
|
|
555
711
|
lent(): i32 {
|
|
556
|
-
|
|
712
|
+
// In `i64`, where four `i32`s cannot wrap; the count is between 0 and the two pools' ceilings.
|
|
713
|
+
const data: i32 = this.data.lent()
|
|
714
|
+
const bits: i32 = this.bits.lent()
|
|
715
|
+
const ready: i32 = this.ready
|
|
716
|
+
return toI32(toI64(data) + toI64(bits) - toI64(ready) - toI64(ready))
|
|
557
717
|
}
|
|
558
718
|
}
|
|
559
719
|
|
|
@@ -571,7 +731,8 @@ export const quicStreamGrowFree = (free: u8[][], target: i32, none: u8[]): void
|
|
|
571
731
|
while (toI32(free.length) < target) {
|
|
572
732
|
free.push(none)
|
|
573
733
|
}
|
|
574
|
-
|
|
734
|
+
// `length` is the list's own starting length, never below 0, so the second test never stops it.
|
|
735
|
+
while (toI32(free.length) > length && free.length > 0) {
|
|
575
736
|
free.pop()
|
|
576
737
|
}
|
|
577
738
|
}
|
|
@@ -714,7 +875,8 @@ export class QuicStream {
|
|
|
714
875
|
* @returns `offset` modulo the buffer size, or 0 when the size is 0.
|
|
715
876
|
*/
|
|
716
877
|
at(offset: i64): i32 {
|
|
717
|
-
|
|
878
|
+
const size: i64 = toI64(this.size)
|
|
879
|
+
return size > 0 ? toI32(offset % size) : 0
|
|
718
880
|
}
|
|
719
881
|
|
|
720
882
|
/**
|
|
@@ -722,7 +884,7 @@ export class QuicStream {
|
|
|
722
884
|
* @internal Read by http3.ts and quic.ts, and in quic-stream.ts outside the class.
|
|
723
885
|
*/
|
|
724
886
|
readable(): i64 {
|
|
725
|
-
return this.recvContiguous
|
|
887
|
+
return quicStreamLess(this.recvContiguous, this.recvRead)
|
|
726
888
|
}
|
|
727
889
|
|
|
728
890
|
/**
|
|
@@ -735,7 +897,7 @@ export class QuicStream {
|
|
|
735
897
|
* @returns The buffer size less the bytes written and not yet acknowledged.
|
|
736
898
|
*/
|
|
737
899
|
room(): i64 {
|
|
738
|
-
return toI64(this.size)
|
|
900
|
+
return quicStreamLess(toI64(this.size), quicStreamLess(this.sendEnd, this.sendBase))
|
|
739
901
|
}
|
|
740
902
|
|
|
741
903
|
/**
|
|
@@ -821,7 +983,7 @@ export class QuicStream {
|
|
|
821
983
|
}
|
|
822
984
|
// Bytes below the buffer's start were acknowledged and their room reused.
|
|
823
985
|
const low: i64 = offset < this.sendBase ? this.sendBase : offset
|
|
824
|
-
const high: i64 = offset
|
|
986
|
+
const high: i64 = quicStreamSum(offset, toI64(length))
|
|
825
987
|
if (high > low) {
|
|
826
988
|
if (this.resendLow < 0) {
|
|
827
989
|
this.resendLow = low
|
|
@@ -964,19 +1126,21 @@ export class QuicStreams {
|
|
|
964
1126
|
* @param windowData The connection's receive window in bytes, the MAX_DATA step.
|
|
965
1127
|
*/
|
|
966
1128
|
constructor(maxBidi: i64, maxUni: i64, localStreams: i64, bufferSize: i32, windowData: i64) {
|
|
967
|
-
const count: i32 = toI32(maxBidi
|
|
1129
|
+
const count: i32 = toI32(quicStreamSum(quicStreamSum(maxBidi, maxUni), localStreams))
|
|
968
1130
|
this.none = []
|
|
969
1131
|
this.slots = []
|
|
970
1132
|
for (let k: i32 = 0; k < count; k += 1) {
|
|
971
1133
|
this.slots.push(new QuicStream(bufferSize, this.none))
|
|
972
1134
|
}
|
|
1135
|
+
// The smallest power of two from 4 up that is at least twice `count`. The cap at 2^30 is never
|
|
1136
|
+
// what stops it: `count` above 2^29 would have made more than half a billion slots above.
|
|
973
1137
|
let size: i32 = 4
|
|
974
|
-
while (size < count
|
|
975
|
-
size = size
|
|
1138
|
+
while (size >> 1 < count && size < 1073741824) {
|
|
1139
|
+
size = size << 1
|
|
976
1140
|
}
|
|
977
|
-
this.index =
|
|
978
|
-
this.events =
|
|
979
|
-
this.free =
|
|
1141
|
+
this.index = quicStreamZeros(size)
|
|
1142
|
+
this.events = quicStreamZeros(count > 0 ? count : QUIC_STREAM_ONE)
|
|
1143
|
+
this.free = quicStreamZeros(count > 0 ? count : QUIC_STREAM_ONE)
|
|
980
1144
|
this.bufferSize = bufferSize
|
|
981
1145
|
this.maxBidi = maxBidi
|
|
982
1146
|
this.maxUni = maxUni
|
|
@@ -1049,7 +1213,8 @@ export class QuicStreams {
|
|
|
1049
1213
|
prime(bytes: i32): void {
|
|
1050
1214
|
const pools: QuicStreamPools | null = this.ownPools()
|
|
1051
1215
|
if (pools !== null) {
|
|
1052
|
-
|
|
1216
|
+
// `recvLent` counts slots, so it lies in `0..slots.length` and the difference is exact.
|
|
1217
|
+
const idle: i32 = quicStreamSub(toI32(this.slots.length), this.recvLent)
|
|
1053
1218
|
const most: i32 = (bytes > 0 ? bytes : QUIC_STREAM_NO_BYTES) / 3 + 1
|
|
1054
1219
|
pools.prime(idle < most ? idle : most)
|
|
1055
1220
|
}
|
|
@@ -1072,38 +1237,53 @@ export class QuicStreams {
|
|
|
1072
1237
|
|
|
1073
1238
|
/**
|
|
1074
1239
|
* Lends `stream` a receive buffer and bitmap from the reserve `prime`
|
|
1075
|
-
* filled, unless it holds them already or the streams have none.
|
|
1240
|
+
* filled, unless it holds them already or the streams have none. Fails
|
|
1076
1241
|
* when the reserve is empty: a packet handled without `prime`, where a
|
|
1077
1242
|
* take would allocate inside the scope.
|
|
1243
|
+
*
|
|
1244
|
+
* @param stream The stream wanting a receive buffer.
|
|
1245
|
+
* @returns False when no buffer could be lent: no pools, an empty reserve,
|
|
1246
|
+
* or a lent count out of step with the slots. None happens with `prime`
|
|
1247
|
+
* called first; the caller closes the connection with an internal error.
|
|
1078
1248
|
*/
|
|
1079
|
-
private borrowRecv(stream: QuicStream):
|
|
1249
|
+
private borrowRecv(stream: QuicStream): boolean {
|
|
1080
1250
|
const pools: QuicStreamPools | null = this.pools
|
|
1081
1251
|
if (stream.recvBuf.length > 0 || this.bufferSize <= 0) {
|
|
1082
|
-
return
|
|
1252
|
+
return true
|
|
1083
1253
|
}
|
|
1084
|
-
|
|
1085
|
-
|
|
1254
|
+
// `recvLent` counts the slots holding a receive buffer and `stream` holds none, so it is below
|
|
1255
|
+
// the slot count: the bound is never what fails.
|
|
1256
|
+
const lent: i32 = this.recvLent
|
|
1257
|
+
if (pools === null || lent < 0 || lent >= toI32(this.slots.length)) {
|
|
1258
|
+
return false
|
|
1086
1259
|
}
|
|
1087
|
-
pools.lend(stream)
|
|
1088
|
-
|
|
1260
|
+
if (!pools.lend(stream)) {
|
|
1261
|
+
return false
|
|
1262
|
+
}
|
|
1263
|
+
this.recvLent = lent + 1
|
|
1264
|
+
return true
|
|
1089
1265
|
}
|
|
1090
1266
|
|
|
1091
1267
|
/**
|
|
1092
1268
|
* Gives `stream`'s receive buffer and bitmap back to the pools, zeroing
|
|
1093
1269
|
* every byte a frame reached: below `recvHighest`, which past a whole
|
|
1094
|
-
* buffer is all of it. Nothing when it holds none
|
|
1270
|
+
* buffer is all of it. Nothing when it holds none, or when the reserve is
|
|
1271
|
+
* full: then `stream` keeps them until the next give-back.
|
|
1095
1272
|
*/
|
|
1096
1273
|
private giveRecv(stream: QuicStream): void {
|
|
1097
1274
|
const pools: QuicStreamPools | null = this.pools
|
|
1098
1275
|
if (pools !== null && stream.recvBuf.length > 0) {
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
)
|
|
1276
|
+
const used: i32 = toI32(quicStreamMin(stream.recvHighest, toI64(stream.size)))
|
|
1277
|
+
if (!pools.keep(stream.recvBuf, stream.recvHave, used)) {
|
|
1278
|
+
return
|
|
1279
|
+
}
|
|
1104
1280
|
stream.recvBuf = this.none
|
|
1105
1281
|
stream.recvHave = this.none
|
|
1106
|
-
|
|
1282
|
+
// `stream` held a buffer, so it is one of the `recvLent`: the count is at least 1.
|
|
1283
|
+
const lent: i32 = this.recvLent
|
|
1284
|
+
if (lent > 0) {
|
|
1285
|
+
this.recvLent = lent - 1
|
|
1286
|
+
}
|
|
1107
1287
|
}
|
|
1108
1288
|
}
|
|
1109
1289
|
|
|
@@ -1120,12 +1300,16 @@ export class QuicStreams {
|
|
|
1120
1300
|
* every byte a write reached: below `sendEnd`, which past a whole buffer is
|
|
1121
1301
|
* all of it. Called only once nothing can be sent again from it — every
|
|
1122
1302
|
* byte acknowledged, or the side reset — and before a reset moves
|
|
1123
|
-
* `sendEnd` back.
|
|
1303
|
+
* `sendEnd` back. Nothing when the reserve is full: then `stream` keeps
|
|
1304
|
+
* them, zeroed, until the next give-back.
|
|
1124
1305
|
*/
|
|
1125
1306
|
private giveSend(stream: QuicStream): void {
|
|
1126
1307
|
const pools: QuicStreamPools | null = this.pools
|
|
1127
1308
|
if (pools !== null && stream.sendBuf.length > 0) {
|
|
1128
|
-
|
|
1309
|
+
const used: i32 = toI32(quicStreamMin(stream.sendEnd, toI64(stream.size)))
|
|
1310
|
+
if (!pools.keep(stream.sendBuf, stream.sendAcked, used)) {
|
|
1311
|
+
return
|
|
1312
|
+
}
|
|
1129
1313
|
stream.sendBuf = this.none
|
|
1130
1314
|
stream.sendAcked = this.none
|
|
1131
1315
|
}
|
|
@@ -1251,10 +1435,12 @@ export class QuicStreams {
|
|
|
1251
1435
|
if (h < 0 || h >= size) {
|
|
1252
1436
|
return -1
|
|
1253
1437
|
}
|
|
1254
|
-
|
|
1255
|
-
|
|
1438
|
+
// The index holds 0 or a slot plus one.
|
|
1439
|
+
const stored: i32 = this.index[h]
|
|
1440
|
+
if (stored <= 0) {
|
|
1256
1441
|
return -1
|
|
1257
1442
|
}
|
|
1443
|
+
const entry: i32 = stored - 1
|
|
1258
1444
|
if (entry < toI32(this.slots.length) && this.slots[entry].id === id) {
|
|
1259
1445
|
return entry
|
|
1260
1446
|
}
|
|
@@ -1277,6 +1463,10 @@ export class QuicStreams {
|
|
|
1277
1463
|
/** Puts slot `k` in the index under its stream's ID. */
|
|
1278
1464
|
private indexInsert(k: i32, id: i64): void {
|
|
1279
1465
|
const size: i32 = toI32(this.index.length)
|
|
1466
|
+
// `k` comes from the free stack, a slot index: never out of range.
|
|
1467
|
+
if (k < 0 || k >= toI32(this.slots.length)) {
|
|
1468
|
+
return
|
|
1469
|
+
}
|
|
1280
1470
|
let h: i32 = this.bucket(id)
|
|
1281
1471
|
for (let probe: i32 = 0; probe < size; probe += 1) {
|
|
1282
1472
|
if (h < 0 || h >= size) {
|
|
@@ -1294,6 +1484,10 @@ export class QuicStreams {
|
|
|
1294
1484
|
private indexRemove(k: i32, id: i64): void {
|
|
1295
1485
|
const size: i32 = toI32(this.index.length)
|
|
1296
1486
|
const mask: i32 = size - 1
|
|
1487
|
+
// `k` is the slot being released, checked by `release`: never out of range.
|
|
1488
|
+
if (k < 0 || k >= toI32(this.slots.length)) {
|
|
1489
|
+
return
|
|
1490
|
+
}
|
|
1297
1491
|
let h: i32 = this.bucket(id)
|
|
1298
1492
|
let found: i32 = -1
|
|
1299
1493
|
for (let probe: i32 = 0; probe < size && h >= 0 && h < size; probe += 1) {
|
|
@@ -1306,7 +1500,7 @@ export class QuicStreams {
|
|
|
1306
1500
|
}
|
|
1307
1501
|
h = (h + 1) & mask
|
|
1308
1502
|
}
|
|
1309
|
-
if (found < 0) {
|
|
1503
|
+
if (found < 0 || found >= size) {
|
|
1310
1504
|
return
|
|
1311
1505
|
}
|
|
1312
1506
|
let hole: i32 = found
|
|
@@ -1316,14 +1510,16 @@ export class QuicStreams {
|
|
|
1316
1510
|
probe < size && next >= 0 && next < size && hole >= 0 && hole < size;
|
|
1317
1511
|
probe += 1
|
|
1318
1512
|
) {
|
|
1319
|
-
const
|
|
1320
|
-
if (
|
|
1513
|
+
const stored: i32 = this.index[next]
|
|
1514
|
+
if (stored <= 0) {
|
|
1321
1515
|
break
|
|
1322
1516
|
}
|
|
1517
|
+
const entry: i32 = stored - 1
|
|
1323
1518
|
const home: i32 = entry < toI32(this.slots.length) ? this.bucket(this.slots[entry].id) : next
|
|
1324
|
-
// The entry may move into the hole when its home is not between the hole and it.
|
|
1325
|
-
|
|
1326
|
-
const
|
|
1519
|
+
// The entry may move into the hole when its home is not between the hole and it. `next`,
|
|
1520
|
+
// `hole` and `home` all lie in `0..size`, so the differences are exact.
|
|
1521
|
+
const distanceNext: i32 = quicStreamSub(next, home) & mask
|
|
1522
|
+
const distanceHole: i32 = quicStreamSub(hole, home) & mask
|
|
1327
1523
|
if (distanceHole < distanceNext) {
|
|
1328
1524
|
this.index[hole] = this.index[next]
|
|
1329
1525
|
hole = next
|
|
@@ -1341,10 +1537,16 @@ export class QuicStreams {
|
|
|
1341
1537
|
if (k < 0 || k >= toI32(this.slots.length) || this.slots[k].queued || this.eventCount >= size) {
|
|
1342
1538
|
return
|
|
1343
1539
|
}
|
|
1344
|
-
const
|
|
1540
|
+
const head: i32 = this.eventHead
|
|
1541
|
+
const count: i32 = this.eventCount
|
|
1542
|
+
// The head lies in `0..size` and the count in `0..size`, so neither bound is ever what returns.
|
|
1543
|
+
if (head < 0 || count < 0 || size <= 0 || count >= size) {
|
|
1544
|
+
return
|
|
1545
|
+
}
|
|
1546
|
+
const at: i32 = quicStreamAdd(head, count) % size
|
|
1345
1547
|
if (at >= 0 && at < size) {
|
|
1346
1548
|
this.events[at] = k
|
|
1347
|
-
this.eventCount =
|
|
1549
|
+
this.eventCount = count + 1
|
|
1348
1550
|
this.slots[k].queued = true
|
|
1349
1551
|
}
|
|
1350
1552
|
}
|
|
@@ -1359,11 +1561,21 @@ export class QuicStreams {
|
|
|
1359
1561
|
*/
|
|
1360
1562
|
nextEvent(): i64 {
|
|
1361
1563
|
const size: i32 = toI32(this.events.length)
|
|
1362
|
-
while (
|
|
1564
|
+
while (size > 0) {
|
|
1565
|
+
const count: i32 = this.eventCount
|
|
1566
|
+
if (count <= 0) {
|
|
1567
|
+
break
|
|
1568
|
+
}
|
|
1363
1569
|
const at: i32 = this.eventHead
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1570
|
+
let k: i32 = -1
|
|
1571
|
+
// The head lies in `0..size`; the reset to 0 is never taken.
|
|
1572
|
+
if (at >= 0 && at < size) {
|
|
1573
|
+
this.eventHead = (at + 1) % size
|
|
1574
|
+
k = this.events[at]
|
|
1575
|
+
} else {
|
|
1576
|
+
this.eventHead = 0
|
|
1577
|
+
}
|
|
1578
|
+
this.eventCount = count - 1
|
|
1367
1579
|
if (k >= 0 && k < toI32(this.slots.length)) {
|
|
1368
1580
|
const stream: QuicStream = this.slots[k]
|
|
1369
1581
|
stream.queued = false
|
|
@@ -1386,10 +1598,11 @@ export class QuicStreams {
|
|
|
1386
1598
|
|
|
1387
1599
|
/** Takes a free slot off the stack, or -1 when every slot is in use. */
|
|
1388
1600
|
private takeFree(): i32 {
|
|
1389
|
-
const
|
|
1390
|
-
if (
|
|
1601
|
+
const count: i32 = this.freeCount
|
|
1602
|
+
if (count <= 0 || count > toI32(this.free.length)) {
|
|
1391
1603
|
return -1
|
|
1392
1604
|
}
|
|
1605
|
+
const at: i32 = count - 1
|
|
1393
1606
|
this.freeCount = at
|
|
1394
1607
|
return this.free[at]
|
|
1395
1608
|
}
|
|
@@ -1435,20 +1648,26 @@ export class QuicStreams {
|
|
|
1435
1648
|
stream.id = -1
|
|
1436
1649
|
this.pushFree(k)
|
|
1437
1650
|
if (quicStreamIsLocal(id)) {
|
|
1438
|
-
|
|
1651
|
+
// `stream` was one of the `localOpen`, so the count is at least 1.
|
|
1652
|
+
const open: i64 = this.localOpen
|
|
1653
|
+
if (open > 0) {
|
|
1654
|
+
this.localOpen = open - 1
|
|
1655
|
+
}
|
|
1439
1656
|
return
|
|
1440
1657
|
}
|
|
1441
1658
|
if (quicStreamIsUni(id)) {
|
|
1442
|
-
this.peerUniClosed = this.peerUniClosed
|
|
1443
|
-
|
|
1444
|
-
|
|
1659
|
+
this.peerUniClosed = quicStreamSum(this.peerUniClosed, 1)
|
|
1660
|
+
const raised: i64 = quicStreamSum(this.peerUniClosed, this.maxUni)
|
|
1661
|
+
if (quicStreamLess(raised, this.peerUniLimit) >= quicStreamSum(this.maxUni, 1) / 2) {
|
|
1662
|
+
this.peerUniLimit = raised
|
|
1445
1663
|
this.maxStreamsUniOwed = true
|
|
1446
1664
|
}
|
|
1447
1665
|
return
|
|
1448
1666
|
}
|
|
1449
|
-
this.peerBidiClosed = this.peerBidiClosed
|
|
1450
|
-
|
|
1451
|
-
|
|
1667
|
+
this.peerBidiClosed = quicStreamSum(this.peerBidiClosed, 1)
|
|
1668
|
+
const raised: i64 = quicStreamSum(this.peerBidiClosed, this.maxBidi)
|
|
1669
|
+
if (quicStreamLess(raised, this.peerBidiLimit) >= quicStreamSum(this.maxBidi, 1) / 2) {
|
|
1670
|
+
this.peerBidiLimit = raised
|
|
1452
1671
|
this.maxStreamsBidiOwed = true
|
|
1453
1672
|
}
|
|
1454
1673
|
}
|
|
@@ -1493,7 +1712,7 @@ export class QuicStreams {
|
|
|
1493
1712
|
while (opened <= sequence) {
|
|
1494
1713
|
const next: i64 = (opened << 2) | (id & 3)
|
|
1495
1714
|
const k: i32 = this.openSlot(next)
|
|
1496
|
-
opened = opened
|
|
1715
|
+
opened = quicStreamSum(opened, 1)
|
|
1497
1716
|
if (k >= 0) {
|
|
1498
1717
|
this.queue(k)
|
|
1499
1718
|
found = k
|
|
@@ -1516,8 +1735,13 @@ export class QuicStreams {
|
|
|
1516
1735
|
if (end > stream.recvLimit) {
|
|
1517
1736
|
return QUIC_ERROR_FLOW_CONTROL
|
|
1518
1737
|
}
|
|
1519
|
-
|
|
1520
|
-
|
|
1738
|
+
const highest: i64 = stream.recvHighest
|
|
1739
|
+
// Both are stream offsets (callers pass a varint or a checked frame's end), never negative: never taken.
|
|
1740
|
+
if (end < 0 || highest < 0) {
|
|
1741
|
+
return QUIC_ERROR_INTERNAL
|
|
1742
|
+
}
|
|
1743
|
+
if (end > highest) {
|
|
1744
|
+
this.recvTotal = quicStreamSum(this.recvTotal, quicStreamLess(end, highest))
|
|
1521
1745
|
stream.recvHighest = end
|
|
1522
1746
|
if (this.recvTotal > this.recvMaxData) {
|
|
1523
1747
|
return QUIC_ERROR_FLOW_CONTROL
|
|
@@ -1539,9 +1763,14 @@ export class QuicStreams {
|
|
|
1539
1763
|
* @param length How many bytes the frame carries.
|
|
1540
1764
|
* @param fin Whether the frame carries the FIN, fixing the final size at `offset + length`.
|
|
1541
1765
|
* @returns `QUIC_ERROR_NO_ERROR` (0), or `QUIC_ERROR_STREAM_LIMIT`, `QUIC_ERROR_STREAM_STATE`,
|
|
1542
|
-
* `QUIC_ERROR_FINAL_SIZE` or `QUIC_ERROR_FLOW_CONTROL` to close the connection with
|
|
1766
|
+
* `QUIC_ERROR_FINAL_SIZE` or `QUIC_ERROR_FLOW_CONTROL` to close the connection with;
|
|
1767
|
+
* `QUIC_ERROR_INTERNAL` for a negative argument or a `length` past the end of `buf`.
|
|
1543
1768
|
*/
|
|
1544
1769
|
onStream(id: i64, offset: i64, buf: u8[], from: i32, length: i32, fin: boolean): i64 {
|
|
1770
|
+
// The frame parser hands a frame that lies inside its packet, at a varint offset: never taken.
|
|
1771
|
+
if (offset < 0 || from < 0 || length < 0 || length > toI32(buf.length) - from) {
|
|
1772
|
+
return QUIC_ERROR_INTERNAL
|
|
1773
|
+
}
|
|
1545
1774
|
const k: i32 = this.locate(id, true)
|
|
1546
1775
|
if (k === -2) {
|
|
1547
1776
|
return this.locateError
|
|
@@ -1550,7 +1779,7 @@ export class QuicStreams {
|
|
|
1550
1779
|
return QUIC_ERROR_NO_ERROR
|
|
1551
1780
|
}
|
|
1552
1781
|
const stream: QuicStream = this.slots[k]
|
|
1553
|
-
const end: i64 = offset
|
|
1782
|
+
const end: i64 = quicStreamSum(offset, toI64(length))
|
|
1554
1783
|
if (stream.recvFinal >= 0 && (end > stream.recvFinal || (fin && end !== stream.recvFinal))) {
|
|
1555
1784
|
return QUIC_ERROR_FINAL_SIZE
|
|
1556
1785
|
}
|
|
@@ -1570,38 +1799,54 @@ export class QuicStreams {
|
|
|
1570
1799
|
stream.recvState = QUIC_RECV_SIZE_KNOWN
|
|
1571
1800
|
}
|
|
1572
1801
|
const before: i64 = stream.recvContiguous
|
|
1573
|
-
if (length > 0 && end > stream.recvContiguous) {
|
|
1574
|
-
|
|
1802
|
+
if (length > 0 && end > stream.recvContiguous && !this.borrowRecv(stream)) {
|
|
1803
|
+
// Never taken with `prime` called before the packet: see `borrowRecv`.
|
|
1804
|
+
return QUIC_ERROR_INTERNAL
|
|
1575
1805
|
}
|
|
1576
1806
|
const size: i32 = toI32(stream.recvBuf.length)
|
|
1577
1807
|
// A side holds either no buffer (length 0) or one from the pools, whose buffers are the table's
|
|
1578
1808
|
// `bufferSize` (`share` refuses pools of another size), so the size test fails only with no buffer.
|
|
1579
1809
|
if (size > 0 && size === stream.size) {
|
|
1580
|
-
// The
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1810
|
+
// The contiguous prefix is never negative nor past the highest byte seen: never taken.
|
|
1811
|
+
const contiguous: i64 = stream.recvContiguous
|
|
1812
|
+
const highest: i64 = stream.recvHighest
|
|
1813
|
+
if (contiguous < 0 || highest < 0) {
|
|
1814
|
+
return QUIC_ERROR_INTERNAL
|
|
1815
|
+
}
|
|
1816
|
+
// The frame's bytes from the first not yet contiguous, inside `buf` (the
|
|
1817
|
+
// check on entry keeps `from + length` inside it); of more than a
|
|
1818
|
+
// buffer, the last buffer's worth, which a byte-wise copy would have
|
|
1819
|
+
// left there. That clamp is defensive: the credit check above caps a
|
|
1820
|
+
// frame's end at `recvRead` plus a ring, so no peer frame reaches it.
|
|
1821
|
+
const ahead: i64 = contiguous - offset
|
|
1822
|
+
let low: i32 = 0
|
|
1823
|
+
if (ahead >= toI64(length)) {
|
|
1824
|
+
low = length
|
|
1825
|
+
} else if (ahead > 0) {
|
|
1826
|
+
low = toI32(ahead)
|
|
1589
1827
|
}
|
|
1590
|
-
|
|
1828
|
+
// `ahead` lies in `1..length` on the second branch, so `low` is in `0..length`: never taken.
|
|
1829
|
+
if (low < 0 || low > length) {
|
|
1830
|
+
return QUIC_ERROR_INTERNAL
|
|
1831
|
+
}
|
|
1832
|
+
const high: i32 = length
|
|
1591
1833
|
if (high - low > size) {
|
|
1592
1834
|
low = high - size
|
|
1593
1835
|
}
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1836
|
+
// The clamp leaves `low` at `high - size`, above zero, so `low >= 0` always holds here.
|
|
1837
|
+
if (low >= 0 && low < high) {
|
|
1838
|
+
const slot: i32 = stream.at(quicStreamSum(offset, toI64(low)))
|
|
1839
|
+
// `from + low` is at most `from + length`, inside `buf`: exact.
|
|
1840
|
+
quicStreamRingIn(stream.recvBuf, slot, buf, quicStreamAdd(from, low), high - low)
|
|
1597
1841
|
quicStreamMarkRing(stream.recvHave, size, slot, high - low, true)
|
|
1598
1842
|
}
|
|
1599
|
-
if (
|
|
1600
|
-
const pending: i64 =
|
|
1843
|
+
if (contiguous < highest) {
|
|
1844
|
+
const pending: i64 = highest - contiguous
|
|
1601
1845
|
const count: i32 = toI32(quicStreamMin(pending, toI64(size)))
|
|
1602
|
-
stream.recvContiguous =
|
|
1603
|
-
stream.recvContiguous
|
|
1846
|
+
stream.recvContiguous = quicStreamSum(
|
|
1847
|
+
stream.recvContiguous,
|
|
1604
1848
|
toI64(quicStreamRingRun(stream.recvHave, size, stream.at(stream.recvContiguous), count, true))
|
|
1849
|
+
)
|
|
1605
1850
|
}
|
|
1606
1851
|
}
|
|
1607
1852
|
if (stream.recvFinal >= 0 && stream.recvContiguous === stream.recvFinal) {
|
|
@@ -1644,7 +1889,12 @@ export class QuicStreams {
|
|
|
1644
1889
|
return QUIC_ERROR_NO_ERROR
|
|
1645
1890
|
}
|
|
1646
1891
|
// §4.5: a reset stream's bytes count as consumed for the connection.
|
|
1647
|
-
|
|
1892
|
+
// The final size is a varint and the read offset a count, never negative: never taken.
|
|
1893
|
+
const read: i64 = stream.recvRead
|
|
1894
|
+
if (finalSize < 0 || read < 0) {
|
|
1895
|
+
return QUIC_ERROR_INTERNAL
|
|
1896
|
+
}
|
|
1897
|
+
this.consume(finalSize - read)
|
|
1648
1898
|
stream.recvRead = finalSize
|
|
1649
1899
|
stream.recvContiguous = finalSize
|
|
1650
1900
|
// Nothing of a reset stream is read again (§3.2 "Reset Recvd").
|
|
@@ -1746,12 +1996,13 @@ export class QuicStreams {
|
|
|
1746
1996
|
|
|
1747
1997
|
/** Counts `bytes` read (or abandoned by a reset) toward the connection's credit, raising it once half the window is used. */
|
|
1748
1998
|
private consume(bytes: i64): void {
|
|
1749
|
-
this.recvConsumed = this.recvConsumed
|
|
1999
|
+
this.recvConsumed = quicStreamSum(this.recvConsumed, bytes)
|
|
1750
2000
|
if (
|
|
1751
|
-
this.recvConsumed
|
|
2001
|
+
quicStreamLess(quicStreamSum(this.recvConsumed, this.windowData), this.recvMaxData) >=
|
|
2002
|
+
this.windowData / 2 &&
|
|
1752
2003
|
this.windowData > 0
|
|
1753
2004
|
) {
|
|
1754
|
-
this.recvMaxData = this.recvConsumed
|
|
2005
|
+
this.recvMaxData = quicStreamSum(this.recvConsumed, this.windowData)
|
|
1755
2006
|
this.maxDataOwed = true
|
|
1756
2007
|
}
|
|
1757
2008
|
}
|
|
@@ -1782,11 +2033,11 @@ export class QuicStreams {
|
|
|
1782
2033
|
return toI64(QUIC_STREAM_ERR_LIMIT)
|
|
1783
2034
|
}
|
|
1784
2035
|
if (bidi) {
|
|
1785
|
-
this.localBidiOpened = opened
|
|
2036
|
+
this.localBidiOpened = quicStreamSum(opened, 1)
|
|
1786
2037
|
} else {
|
|
1787
|
-
this.localUniOpened = opened
|
|
2038
|
+
this.localUniOpened = quicStreamSum(opened, 1)
|
|
1788
2039
|
}
|
|
1789
|
-
this.localOpen = this.localOpen
|
|
2040
|
+
this.localOpen = quicStreamSum(this.localOpen, 1)
|
|
1790
2041
|
return id
|
|
1791
2042
|
}
|
|
1792
2043
|
|
|
@@ -1836,8 +2087,8 @@ export class QuicStreams {
|
|
|
1836
2087
|
if (n > 0 && toI32(stream.sendBuf.length) === stream.size) {
|
|
1837
2088
|
quicStreamRingIn(stream.sendBuf, stream.at(stream.sendEnd), buf, from, n)
|
|
1838
2089
|
}
|
|
1839
|
-
stream.sendEnd = stream.sendEnd
|
|
1840
|
-
this.writtenTotal = this.writtenTotal
|
|
2090
|
+
stream.sendEnd = quicStreamSum(stream.sendEnd, toI64(n))
|
|
2091
|
+
this.writtenTotal = quicStreamSum(this.writtenTotal, toI64(n))
|
|
1841
2092
|
stream.wantsWrite = n < length
|
|
1842
2093
|
if (fin && n === length) {
|
|
1843
2094
|
stream.finQueued = true
|
|
@@ -1894,7 +2145,7 @@ export class QuicStreams {
|
|
|
1894
2145
|
quicStreamRingOut(buf, at, stream.recvBuf, slot, n)
|
|
1895
2146
|
quicStreamMarkRing(stream.recvHave, size, slot, n, false)
|
|
1896
2147
|
}
|
|
1897
|
-
stream.recvRead = stream.recvRead
|
|
2148
|
+
stream.recvRead = quicStreamSum(stream.recvRead, toI64(n))
|
|
1898
2149
|
if (n > 0) {
|
|
1899
2150
|
this.consume(toI64(n))
|
|
1900
2151
|
}
|
|
@@ -1902,9 +2153,9 @@ export class QuicStreams {
|
|
|
1902
2153
|
const window: i32 = stream.size
|
|
1903
2154
|
if (
|
|
1904
2155
|
stream.recvState === QUIC_RECV_RECV &&
|
|
1905
|
-
stream.recvRead
|
|
2156
|
+
quicStreamLess(quicStreamSum(stream.recvRead, toI64(window)), stream.recvLimit) >= toI64(window / 2)
|
|
1906
2157
|
) {
|
|
1907
|
-
stream.recvLimit = stream.recvRead
|
|
2158
|
+
stream.recvLimit = quicStreamSum(stream.recvRead, toI64(window))
|
|
1908
2159
|
stream.maxDataOwed = true
|
|
1909
2160
|
this.streamOwes = true
|
|
1910
2161
|
}
|
|
@@ -1938,7 +2189,7 @@ export class QuicStreams {
|
|
|
1938
2189
|
// Nothing is sent again once reset (§3.1), and the used extent is the
|
|
1939
2190
|
// one written, so the buffer goes back before `sendEnd` moves.
|
|
1940
2191
|
this.giveSend(stream)
|
|
1941
|
-
this.writtenTotal = this.writtenTotal
|
|
2192
|
+
this.writtenTotal = quicStreamLess(this.writtenTotal, quicStreamLess(stream.sendEnd, stream.sendNext))
|
|
1942
2193
|
stream.sendEnd = stream.sendNext
|
|
1943
2194
|
stream.sendState = QUIC_SEND_RESET_SENT
|
|
1944
2195
|
stream.resetSentCode = code
|
|
@@ -2026,11 +2277,18 @@ export class QuicStreams {
|
|
|
2026
2277
|
*/
|
|
2027
2278
|
putNextChunk(buf: u8[], at: i32, end: i32): i32 {
|
|
2028
2279
|
const count: i32 = toI32(this.slots.length)
|
|
2029
|
-
while (
|
|
2280
|
+
while (count > 0) {
|
|
2281
|
+
const scanned: i32 = this.scanned
|
|
2282
|
+
if (scanned >= count) {
|
|
2283
|
+
break
|
|
2284
|
+
}
|
|
2030
2285
|
const k: i32 = this.cursor % count
|
|
2031
|
-
this.
|
|
2032
|
-
|
|
2033
|
-
if (k
|
|
2286
|
+
this.scanned = scanned + 1
|
|
2287
|
+
// The cursor is only ever set to `0..count`, so `k` is in range and the reset is never taken.
|
|
2288
|
+
if (k < 0 || k >= count) {
|
|
2289
|
+
this.cursor = 0
|
|
2290
|
+
} else {
|
|
2291
|
+
this.cursor = (k + 1) % count
|
|
2034
2292
|
const stream: QuicStream = this.slots[k]
|
|
2035
2293
|
if (stream.id >= 0 && stream.wantsToSend()) {
|
|
2036
2294
|
let next: i32 = at
|
|
@@ -2060,23 +2318,38 @@ export class QuicStreams {
|
|
|
2060
2318
|
*/
|
|
2061
2319
|
private putResend(stream: QuicStream, buf: u8[], at: i32, end: i32): i32 {
|
|
2062
2320
|
if (stream.resendLow >= 0) {
|
|
2321
|
+
const high: i64 = stream.resendHigh
|
|
2063
2322
|
let low: i64 = stream.resendLow < stream.sendBase ? stream.sendBase : stream.resendLow
|
|
2064
|
-
|
|
2065
|
-
|
|
2323
|
+
// Stream offsets and the frame's bounds in `buf` are never negative: never taken.
|
|
2324
|
+
if (low < 0 || high < 0 || at < 0 || end < 0 || end < at) {
|
|
2325
|
+
return at
|
|
2326
|
+
}
|
|
2327
|
+
if (low < high && stream.size > 0) {
|
|
2328
|
+
const span: i64 = high - low
|
|
2066
2329
|
const count: i32 = toI32(quicStreamMin(span, toI64(stream.size)))
|
|
2067
|
-
low =
|
|
2330
|
+
low = quicStreamSum(
|
|
2331
|
+
low,
|
|
2332
|
+
toI64(quicStreamRingRun(stream.sendAcked, stream.size, stream.at(low), count, true))
|
|
2333
|
+
)
|
|
2068
2334
|
}
|
|
2069
|
-
|
|
2335
|
+
// A run of acknowledged bytes is never negative, so `low` stays above zero: never taken.
|
|
2336
|
+
if (low < 0) {
|
|
2337
|
+
return at
|
|
2338
|
+
}
|
|
2339
|
+
const want: i32 = toI32(high - low)
|
|
2070
2340
|
if (want <= 0) {
|
|
2071
2341
|
stream.resendLow = -1
|
|
2072
2342
|
stream.resendHigh = -1
|
|
2073
2343
|
} else {
|
|
2074
|
-
const
|
|
2075
|
-
|
|
2344
|
+
const space: i32 = end - at
|
|
2345
|
+
const over: i32 = quicStreamOverhead(stream.id, low, want)
|
|
2346
|
+
// No room past the frame's header: `over` and `space` are never negative.
|
|
2347
|
+
if (over < 0 || space < 0 || space <= over) {
|
|
2076
2348
|
return at
|
|
2077
2349
|
}
|
|
2350
|
+
const room: i32 = space - over
|
|
2078
2351
|
const n: i32 = room < want ? room : want
|
|
2079
|
-
const fin: boolean = stream.resendFin && n === want && low
|
|
2352
|
+
const fin: boolean = stream.resendFin && n === want && quicStreamSum(low, toI64(n)) === stream.sendEnd
|
|
2080
2353
|
const next: i32 = quicPutStream(buf, at, end, stream.id, low, stream.sendBuf, stream.at(low), n, fin)
|
|
2081
2354
|
if (next < 0) {
|
|
2082
2355
|
return at
|
|
@@ -2086,7 +2359,7 @@ export class QuicStreams {
|
|
|
2086
2359
|
stream.resendLow = -1
|
|
2087
2360
|
stream.resendHigh = -1
|
|
2088
2361
|
} else {
|
|
2089
|
-
stream.resendLow = low
|
|
2362
|
+
stream.resendLow = quicStreamSum(low, toI64(n))
|
|
2090
2363
|
}
|
|
2091
2364
|
if (fin) {
|
|
2092
2365
|
stream.resendFin = false
|
|
@@ -2129,9 +2402,27 @@ export class QuicStreams {
|
|
|
2129
2402
|
* held back by a credit owes the matching BLOCKED frame, once per limit.
|
|
2130
2403
|
*/
|
|
2131
2404
|
private putNew(stream: QuicStream, k: i32, buf: u8[], at: i32, end: i32): i32 {
|
|
2132
|
-
const
|
|
2133
|
-
const
|
|
2134
|
-
const
|
|
2405
|
+
const sendEnd: i64 = stream.sendEnd
|
|
2406
|
+
const sendNext: i64 = stream.sendNext
|
|
2407
|
+
const sendLimit: i64 = stream.sendLimit
|
|
2408
|
+
const maxData: i64 = this.sendMaxData
|
|
2409
|
+
const total: i64 = this.sendTotal
|
|
2410
|
+
// Offsets, limits and the frame's bounds in `buf` are never negative: never taken.
|
|
2411
|
+
if (
|
|
2412
|
+
sendEnd < 0 ||
|
|
2413
|
+
sendNext < 0 ||
|
|
2414
|
+
sendLimit < 0 ||
|
|
2415
|
+
maxData < 0 ||
|
|
2416
|
+
total < 0 ||
|
|
2417
|
+
at < 0 ||
|
|
2418
|
+
end < 0 ||
|
|
2419
|
+
end < at
|
|
2420
|
+
) {
|
|
2421
|
+
return at
|
|
2422
|
+
}
|
|
2423
|
+
const unsent: i64 = sendEnd - sendNext
|
|
2424
|
+
const streamCredit: i64 = sendLimit - sendNext
|
|
2425
|
+
const connectionCredit: i64 = maxData - total
|
|
2135
2426
|
let allowed: i64 = quicStreamMin(unsent, quicStreamMin(streamCredit, connectionCredit))
|
|
2136
2427
|
if (allowed < 0) {
|
|
2137
2428
|
allowed = 0
|
|
@@ -2150,10 +2441,13 @@ export class QuicStreams {
|
|
|
2150
2441
|
if (want === 0 && !finNow) {
|
|
2151
2442
|
return at
|
|
2152
2443
|
}
|
|
2153
|
-
const
|
|
2154
|
-
|
|
2444
|
+
const space: i32 = end - at
|
|
2445
|
+
const over: i32 = quicStreamOverhead(stream.id, sendNext, want)
|
|
2446
|
+
// No room past the frame's header, or none for a byte it must carry: `over` and `space` are never negative.
|
|
2447
|
+
if (over < 0 || space < 0 || space < over || (over === space && want > 0)) {
|
|
2155
2448
|
return at
|
|
2156
2449
|
}
|
|
2450
|
+
const room: i32 = space - over
|
|
2157
2451
|
const n: i32 = room < want ? room : want
|
|
2158
2452
|
const fin: boolean = finNow && n === want
|
|
2159
2453
|
const next: i32 = quicPutStream(
|
|
@@ -2171,8 +2465,8 @@ export class QuicStreams {
|
|
|
2171
2465
|
return at
|
|
2172
2466
|
}
|
|
2173
2467
|
this.noteChunk(stream.id, stream.sendNext, n, fin)
|
|
2174
|
-
stream.sendNext = stream.sendNext
|
|
2175
|
-
this.sendTotal = this.sendTotal
|
|
2468
|
+
stream.sendNext = quicStreamSum(stream.sendNext, toI64(n))
|
|
2469
|
+
this.sendTotal = quicStreamSum(this.sendTotal, toI64(n))
|
|
2176
2470
|
if (fin) {
|
|
2177
2471
|
stream.finSent = true
|
|
2178
2472
|
stream.sendState = QUIC_SEND_DATA_SENT
|
|
@@ -2344,10 +2638,12 @@ export class QuicStreams {
|
|
|
2344
2638
|
const before: i64 = stream.sendBase
|
|
2345
2639
|
const size: i32 = stream.size
|
|
2346
2640
|
if (size > 0) {
|
|
2347
|
-
const
|
|
2641
|
+
const reach: i64 = quicStreamSum(offset, toI64(length))
|
|
2642
|
+
const top: i64 = reach < stream.sendEnd ? reach : stream.sendEnd
|
|
2348
2643
|
const low: i64 = offset < stream.sendBase ? stream.sendBase : offset
|
|
2349
2644
|
// The bytes in flight are fewer than a buffer, so a chunk is too.
|
|
2350
|
-
|
|
2645
|
+
// Stream offsets are never negative, so the first two tests always hold.
|
|
2646
|
+
if (low >= 0 && top >= 0 && low < top) {
|
|
2351
2647
|
quicStreamMarkRing(
|
|
2352
2648
|
stream.sendAcked,
|
|
2353
2649
|
size,
|
|
@@ -2356,12 +2652,15 @@ export class QuicStreams {
|
|
|
2356
2652
|
true
|
|
2357
2653
|
)
|
|
2358
2654
|
}
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2655
|
+
const base: i64 = stream.sendBase
|
|
2656
|
+
const next: i64 = stream.sendNext
|
|
2657
|
+
// Stream offsets are never negative, so the first two tests always hold.
|
|
2658
|
+
if (base >= 0 && next >= 0 && base < next) {
|
|
2659
|
+
const slot: i32 = stream.at(base)
|
|
2660
|
+
const count: i32 = toI32(quicStreamMin(next - base, toI64(size)))
|
|
2362
2661
|
const done: i32 = quicStreamRingRun(stream.sendAcked, size, slot, count, true)
|
|
2363
2662
|
quicStreamMarkRing(stream.sendAcked, size, slot, done, false)
|
|
2364
|
-
stream.sendBase =
|
|
2663
|
+
stream.sendBase = quicStreamSum(base, toI64(done))
|
|
2365
2664
|
}
|
|
2366
2665
|
}
|
|
2367
2666
|
if (fin) {
|