@amritk/nish-aarch64-darwin 0.34.0 → 0.35.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.
@@ -444,11 +444,16 @@ export class Http3Server {
444
444
  index: Http3CidIndex
445
445
  wheel: Http3Wheel
446
446
  /**
447
- * Each slot's client address, as its first datagram came from. Not
447
+ * Where each slot sends now: its client's address as the first datagram
448
+ * came from, then wherever the connection follows the client to
449
+ * (`QuicConnection.sendAddress`, copied here by `flushSlot`). Not
448
450
  * validated until `quic(slot).addressValidated` is set (RFC 9000 §8.1): a
449
451
  * Retry token the listener checked, with `retry` on, or a Handshake packet
450
452
  * from the client. Before then it is the source of one UDP datagram, which
451
453
  * anyone can forge, so a cap or an allow-list keyed on it waits for that.
454
+ * After the handshake it changes when the client's address does (a NAT
455
+ * rebinding, §9.3), first to an address not yet validated, so a cap keyed
456
+ * on the address a session started from keeps its own copy.
452
457
  */
453
458
  addresses: u8[][]
454
459
  /** What `receive` reads into, and where `flush` writes a flight. */
@@ -743,7 +748,7 @@ export class Http3Server {
743
748
  }
744
749
  const slot: i32 = this.index.find(buf, dcidAt, dcidLength)
745
750
  if (slot >= 0) {
746
- this.serve(slot, buf, at, len, now, key)
751
+ this.serve(slot, buf, at, len, address, now, key)
747
752
  return
748
753
  }
749
754
  this.unowned(buf, at, len, address, now, key)
@@ -793,7 +798,7 @@ export class Http3Server {
793
798
  this.state[slot] = H3_SERVER_BUSY
794
799
  this.signatures[slot] = H3_SERVER_NONE
795
800
  this.accepted = h3Count(this.accepted)
796
- this.serve(slot, buf, at, len, now, key)
801
+ this.serve(slot, buf, at, len, address, now, key)
797
802
  }
798
803
 
799
804
  /**
@@ -869,11 +874,16 @@ export class Http3Server {
869
874
  this.micros = micros
870
875
  }
871
876
 
872
- /** Hands `slot`'s connection a datagram, signs when the handshake asks, and refiles its IDs and timer. */
873
- serve(slot: i32, buf: u8[], at: i32, len: i32, now: i64, key: Secret<u8[]>): void {
877
+ /**
878
+ * Hands `slot`'s connection a datagram from `address`, signs when the
879
+ * handshake asks, and refiles its IDs and timer. The connection decides
880
+ * from the address whether the client moved (RFC 9000 §9.3); an address
881
+ * that is not 18 bytes long leaves it where it is.
882
+ */
883
+ serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
874
884
  const quic: QuicConnection = this.quics[slot]
875
885
  quic.recovery.micros = this.micros
876
- quic.receiveWindow(buf, at, len, now)
886
+ quic.receiveWindowFrom(buf, at, len, address, now)
877
887
  const input: u8[] | null = quic.signatureInput()
878
888
  if (input !== null) {
879
889
  const signature: u8[] | null = this.signer.sign(key, input)
@@ -989,7 +999,21 @@ export class Http3Server {
989
999
  if (this.conns[slot].isDone() && !quic.closed()) {
990
1000
  quic.close(H3_NO_ERROR)
991
1001
  }
1002
+ // A PATH_RESPONSE owed on a path the connection did not move to goes
1003
+ // there, alone (RFC 9000 §9.1); asking for it runs the connection's
1004
+ // timers first, so a validation that ran out has moved it back before
1005
+ // the flight below picks where it goes.
1006
+ const probe: i32 = quic.takeProbeInto(this.tx, H3_SERVER_ZERO, now)
1007
+ if (probe > 0) {
1008
+ this.send(this.tx, H3_SERVER_ZERO, probe, quic.probeAddress, H3_SERVER_ZERO, H3_SERVER_ZERO)
1009
+ }
992
1010
  const to: u8[] = this.addresses[slot]
1011
+ if (quic.pathKnown) {
1012
+ const current: u8[] = quic.sendAddress()
1013
+ for (let k: i32 = 0; k < toI32(to.length) && k < toI32(current.length); k++) {
1014
+ to[k] = current[k]
1015
+ }
1016
+ }
993
1017
  let retried: boolean = false
994
1018
  for (let guard: i32 = 0; guard < 64; guard++) {
995
1019
  const n: i32 = quicListenerTakeFlight(
@@ -157,7 +157,11 @@
157
157
  * routing each row by its ordinal (checked first: a server's ordinal is
158
158
  * refused before it routes anything), and `next(run)` answers
159
159
  * `PG_BATCH_ROW`, then `PG_BATCH_DONE`, `PG_BATCH_MISSING` or
160
- * `PG_BATCH_FAILED`, for each request. It reads no clock, and the batcher
160
+ * `PG_BATCH_FAILED`, for each request. The statement orders its rows by
161
+ * ordinal, so a key's last events come as soon as a later key's row does;
162
+ * with `sorted` false it does not (no Sort node, when `orderBy` is ""), rows
163
+ * are answered in whatever order they come, and every last event waits for
164
+ * the execution's end. It reads no clock, and the batcher
161
165
  * itself allocates only in its constructor (the connection's output buffer a
162
166
  * take writes into may still grow, up to its `maxOutput`); when to take is
163
167
  * the program's.
@@ -445,13 +449,56 @@ const pgBytesOf = (text: string): u8[] => {
445
449
 
446
450
  /** How many bytes `value` takes in decimal, its sign included. */
447
451
  const pgDecimalLength = (value: i64): i32 => {
452
+ // Counted on the negative side, where the most negative i64 fits, two digits a step.
448
453
  let n: i32 = value < 0 ? 2 : 1
449
454
  let v: i64 = value < 0 ? value : -value
450
- while (v <= -10) {
451
- v = v / 10
452
- n += 1
455
+ while (v <= -100) {
456
+ v = v / 100
457
+ n += 2
458
+ }
459
+ return v <= -10 ? n + 1 : n
460
+ }
461
+
462
+ /**
463
+ * Writes `value` in decimal into `out[at, at + n)`, where `n` is
464
+ * `pgDecimalLength(value)`, and answers `at + n`. The digits go once, into
465
+ * their places, from the last, two a step from the magnitude as a `u64`
466
+ * (which holds the most negative i64's, and whose `/` and `%` are never
467
+ * checked). Panics unless the window is inside `out`; each step's guard
468
+ * restates it in the shape that proves its two stores.
469
+ */
470
+ const pgPutDecimal = (out: u8[], at: i32, value: i64, n: i32): i32 => {
471
+ if (!(at >= 0 && at < 2147483620 && n >= 1 && n <= 20 && at + n <= toI32(out.length))) {
472
+ panic("nish/net/postgres: a decimal outside its room")
473
+ }
474
+ const first: i32 = value < 0 ? at + 1 : at
475
+ let m: u64 = value < 0 ? toU64(0) - toU64(value) : toU64(value)
476
+ // Where the last two digits start.
477
+ let j: i32 = at + n - 2
478
+ while (m >= 100 && j >= first && j >= 0 && j < 2147483640 && j + 2 <= toI32(out.length)) {
479
+ const pair: u32 = toU32(m % 100)
480
+ const tens: u32 = pair / 10
481
+ out[j] = toU8(48 + tens)
482
+ out[j + 1] = toU8(48 + pair - tens * 10)
483
+ m = m / 100
484
+ j -= 2
485
+ }
486
+ // One or two digits are left, ending at `j + 2`.
487
+ const pair: u32 = toU32(m)
488
+ if (pair >= 10 && j >= first && j >= 0 && j < 2147483640 && j + 2 <= toI32(out.length)) {
489
+ const tens: u32 = pair / 10
490
+ out[j] = toU8(48 + tens)
491
+ out[j + 1] = toU8(48 + pair - tens * 10)
492
+ } else {
493
+ const last: i32 = j + 1
494
+ if (last >= first && last >= 0 && last < toI32(out.length)) {
495
+ out[last] = toU8(48 + pair)
496
+ }
453
497
  }
454
- return n
498
+ if (value < 0 && at < toI32(out.length)) {
499
+ out[at] = toU8(45)
500
+ }
501
+ return at + n
455
502
  }
456
503
 
457
504
  /** Whether `value` is an `int4`, -2^31..2^31-1. */
@@ -2274,32 +2321,9 @@ export class PgConnection {
2274
2321
 
2275
2322
  /** `value` in decimal, `pgDecimalLength(value)` bytes. */
2276
2323
  private putDecimal(value: i64): void {
2277
- this.reserve(this.outEnd + 20)
2278
- // The digits are written from the least significant, then reversed; the
2279
- // value is made negative first so that the most negative i64 has its digits.
2280
- let v: i64 = value < 0 ? value : -value
2281
- if (value < 0) {
2282
- this.out[this.outEnd] = toU8(45)
2283
- this.outEnd += 1
2284
- }
2285
- const first: i32 = this.outEnd
2286
- while (true) {
2287
- this.out[this.outEnd] = toU8(48 - toI32(v % 10))
2288
- this.outEnd += 1
2289
- v = v / 10
2290
- if (v === 0) {
2291
- break
2292
- }
2293
- }
2294
- let lo: i32 = first
2295
- let hi: i32 = this.outEnd - 1
2296
- while (lo < hi) {
2297
- const t: u8 = this.out[lo]
2298
- this.out[lo] = this.out[hi]
2299
- this.out[hi] = t
2300
- lo++
2301
- hi--
2302
- }
2324
+ const n: i32 = pgDecimalLength(value)
2325
+ this.reserve(this.outEnd + n)
2326
+ this.outEnd = pgPutDecimal(this.out, this.outEnd, value, n)
2303
2327
  }
2304
2328
 
2305
2329
  /**
@@ -3329,6 +3353,14 @@ export class PgBatchConfig {
3329
3353
  runs: i32 = 16
3330
3354
  /** Whether every row of a batch comes in binary format, read with the binary decoders and its ordinal by `binaryInt`; false for text. */
3331
3355
  binary: boolean = false
3356
+ /**
3357
+ * Whether the statement orders its rows by ordinal, so each key's last
3358
+ * event comes as soon as a later key's row does. False drops `q.ord` from
3359
+ * the `order by` (and the Sort node it costs, when `orderBy` is ""): rows
3360
+ * route in whatever order they come, and every request's last event waits
3361
+ * for the execution's end.
3362
+ */
3363
+ sorted: boolean = true
3332
3364
  }
3333
3365
 
3334
3366
  /** One execution's requests, grouped by key, and how far its rows have been routed. */
@@ -3336,6 +3368,8 @@ class PgBatchRun {
3336
3368
  /** Key `k`'s requests' tags are `tags[first[k], first[k + 1])`, in the order they were added; grown by `take` to the batch. */
3337
3369
  tags: i32[]
3338
3370
  first: i32[]
3371
+ /** Under `sorted` false, whether key `k` has had a row; grown by `take` with `tags`, and empty when sorted. */
3372
+ had: boolean[]
3339
3373
  keys: i32 = 0
3340
3374
  /** The key rows are being read for, whether it has had one, and the next of its requests to answer. */
3341
3375
  cur: i32 = 0
@@ -3353,6 +3387,7 @@ class PgBatchRun {
3353
3387
  constructor() {
3354
3388
  this.tags = []
3355
3389
  this.first = []
3390
+ this.had = []
3356
3391
  }
3357
3392
 
3358
3393
  /** Opens the run for `keys` keys, none of whose rows have been read. */
@@ -3365,6 +3400,9 @@ class PgBatchRun {
3365
3400
  this.target = -1
3366
3401
  this.ended = false
3367
3402
  this.failed = false
3403
+ for (let k: i32 = 0; k < keys && k < toI32(this.had.length); k++) {
3404
+ this.had[k] = false
3405
+ }
3368
3406
  }
3369
3407
  }
3370
3408
 
@@ -3381,7 +3419,11 @@ class PgBatchRun {
3381
3419
  * connection to `fail(run)`; after each, `next(run)` answers events until
3382
3420
  * `PG_BATCH_NONE`, `tag` naming the request each is about. Every row goes to
3383
3421
  * every request for its key, and every request gets one last event:
3384
- * `PG_BATCH_DONE`, `PG_BATCH_MISSING` or `PG_BATCH_FAILED`. A batch with
3422
+ * `PG_BATCH_DONE`, `PG_BATCH_MISSING` or `PG_BATCH_FAILED`. Sorted (the
3423
+ * default), rows come in key order and a key's last events come as soon as
3424
+ * a later key's row does; with `sorted` false, rows come in any order, each
3425
+ * answered at once, and every last event waits for `end` or `fail`, then
3426
+ * comes in key order. A batch with
3385
3427
  * `binary` set comes in binary format: its rows are read with the binary
3386
3428
  * decoders, `binaryInt` for each ordinal, and the caller's `binary.failed`
3387
3429
  * is left as it was.
@@ -3425,7 +3467,7 @@ export class PgBatcher {
3425
3467
  private readonly runs: PgBatchRun[]
3426
3468
  /** The request the last `next(run)` answered about; -1 for none. */
3427
3469
  tag: i32 = -1
3428
- /** The number of runs failed by a bad ordinal (missing, NULL, not an integer, or naming no key still open), stopping at 2^31 - 1: each counts its first refused row, and the rows after it, dropped with the failed run, are not counted. */
3470
+ /** The number of runs failed by a bad ordinal (missing, NULL, not an integer, or naming no key still open; with `sorted` false, any key of the run is open), stopping at 2^31 - 1: each counts its first refused row, and the rows after it, dropped with the failed run, are not counted. */
3429
3471
  refused: i32 = 0
3430
3472
  /** The number of `row`, `end` and `fail` calls on a spent handle, each ignored, stopping at 2^31 - 1. */
3431
3473
  stray: i32 = 0
@@ -3434,6 +3476,7 @@ export class PgBatcher {
3434
3476
  private readonly keyType: i32
3435
3477
  private readonly maxBytes: i32
3436
3478
  private readonly binary: boolean
3479
+ private readonly sorted: boolean
3437
3480
  private storeEnd: i32 = 0
3438
3481
  private head: i32 = 0
3439
3482
  private count: i32 = 0
@@ -3472,10 +3515,15 @@ export class PgBatcher {
3472
3515
  this.maxRequests = maxRequests
3473
3516
  this.maxBytes = config.maxBytes
3474
3517
  this.binary = config.binary
3518
+ this.sorted = config.sorted
3475
3519
  this.name = config.name
3476
3520
  const type: string = keyType === PG_BATCH_INT ? "int8" : "text"
3477
3521
  const order: string = config.orderBy.length > 0 ? `, ${config.orderBy}` : ""
3478
- this.sql = `select t.*, q.ord from unnest($1::${type}[]) with ordinality as q(key, ord) cross join lateral (${config.select}) as t order by q.ord${order}`
3522
+ let tail: string = ` order by q.ord${order}`
3523
+ if (!config.sorted) {
3524
+ tail = config.orderBy.length > 0 ? ` order by ${config.orderBy}` : ""
3525
+ }
3526
+ this.sql = `select t.*, q.ord from unnest($1::${type}[]) with ordinality as q(key, ord) cross join lateral (${config.select}) as t${tail}`
3479
3527
  const queueSize: i32 = config.queueSize
3480
3528
  this.qTags = new Array<i32>(queueSize)
3481
3529
  this.qHashes = new Array<i32>(queueSize)
@@ -3745,6 +3793,9 @@ export class PgBatcher {
3745
3793
  // Grown together: `first` holds one more than `tags`, which bounds the keys too.
3746
3794
  run.tags = new Array<i32>(toI32(this.takenKeys.length))
3747
3795
  run.first = new Array<i32>(toI32(this.takenKeys.length) + 1)
3796
+ if (!this.sorted) {
3797
+ run.had = new Array<boolean>(toI32(this.takenKeys.length))
3798
+ }
3748
3799
  }
3749
3800
  run.first[0] = 0
3750
3801
  for (let k: i32 = 0; k < keys && k < toI32(this.keyCounts.length); k++) {
@@ -3834,11 +3885,12 @@ export class PgBatcher {
3834
3885
  /**
3835
3886
  * Routes `conn`'s current row, of run `r`'s execution, by the ordinal in
3836
3887
  * its last column: `next(r)` then answers `PG_BATCH_ROW` for each request
3837
- * of its key, after the last events of the keys before it. False, and the
3888
+ * of its key, after the last events of the keys before it (with `sorted`
3889
+ * false, at once: no last event comes before `end`). False, and the
3838
3890
  * row dropped, when the run has failed; and when the ordinal is NULL, not
3839
- * an integer, or names no key still open (one out of range, or one before
3840
- * the key the last row was for): that counts in `refused` and fails the
3841
- * run; and on a spent handle, which counts in `stray`. Panics on a handle
3891
+ * an integer, or names no key still open (one out of range, or, sorted,
3892
+ * one before the key the last row was for): that counts in `refused` and
3893
+ * fails the run; and on a spent handle, which counts in `stray`. Panics on a handle
3842
3894
  * `take` cannot give, or when the run's events are not all answered.
3843
3895
  */
3844
3896
  row(r: i32, conn: PgConnection): boolean {
@@ -3867,8 +3919,9 @@ export class PgBatcher {
3867
3919
  bad = conn.text.failed
3868
3920
  conn.text.failed = failed
3869
3921
  }
3870
- // Ordinals count from 1 and come in order: a row may name the current key again, never one before it.
3871
- if (bad || ordinal < toI64(run.cur) + 1 || ordinal > toI64(run.keys)) {
3922
+ // Ordinals count from 1 and, sorted, come in order: a row may name the current key again, never one before it.
3923
+ const least: i64 = this.sorted ? toI64(run.cur) + 1 : toI64(1)
3924
+ if (bad || ordinal < least || ordinal > toI64(run.keys)) {
3872
3925
  if (this.refused < 2147483647) {
3873
3926
  this.refused += 1
3874
3927
  }
@@ -3876,6 +3929,9 @@ export class PgBatcher {
3876
3929
  return false
3877
3930
  }
3878
3931
  run.target = toI32(ordinal) - 1
3932
+ if (!this.sorted && run.target < toI32(run.had.length)) {
3933
+ run.had[run.target] = true
3934
+ }
3879
3935
  return true
3880
3936
  }
3881
3937
 
@@ -3921,6 +3977,9 @@ export class PgBatcher {
3921
3977
  return PG_BATCH_NONE
3922
3978
  }
3923
3979
  const run: PgBatchRun = found
3980
+ if (!this.sorted) {
3981
+ return this.nextUnsorted(run)
3982
+ }
3924
3983
  // The keys before the row's, or every key once the execution is over, have had all their rows.
3925
3984
  let limit: i32 = run.cur
3926
3985
  if (run.target >= 0) {
@@ -3952,6 +4011,36 @@ export class PgBatcher {
3952
4011
  }
3953
4012
  return PG_BATCH_NONE
3954
4013
  }
4014
+
4015
+ // `next` with `sorted` false: the row's `PG_BATCH_ROW` events at once, and
4016
+ // every last event only once the execution is over, `cur` walking the keys.
4017
+ private nextUnsorted(run: PgBatchRun): i32 {
4018
+ if (run.target >= 0 && run.target < toI32(run.tags.length)) {
4019
+ const at: i32 = run.first[run.target] + run.at
4020
+ if (at < run.first[run.target + 1]) {
4021
+ run.at += 1
4022
+ this.tag = run.tags[at]
4023
+ return PG_BATCH_ROW
4024
+ }
4025
+ run.target = -1
4026
+ run.at = 0
4027
+ }
4028
+ if (!run.ended) {
4029
+ return PG_BATCH_NONE
4030
+ }
4031
+ while (run.cur < run.keys && run.cur < toI32(run.had.length)) {
4032
+ if (this.emit(run)) {
4033
+ if (run.failed) {
4034
+ return PG_BATCH_FAILED
4035
+ }
4036
+ return run.had[run.cur] ? PG_BATCH_DONE : PG_BATCH_MISSING
4037
+ }
4038
+ run.cur += 1
4039
+ run.at = 0
4040
+ }
4041
+ run.open = false
4042
+ return PG_BATCH_NONE
4043
+ }
3955
4044
  }
3956
4045
 
3957
4046
  // --- Batch timing --------------------------------------------------------------
@@ -436,29 +436,57 @@ export class QuicRecovery {
436
436
  for (const sp of this.spaces) {
437
437
  sp.reset()
438
438
  }
439
+ this.resetPath()
440
+ this.maxAckDelay = QUIC_RECOVERY_DEFAULT_MAX_ACK_DELAY
441
+ this.micros = -1
442
+ this.ptoCount = 0
443
+ this.timeoutSpace = -1
444
+ this.congestionEvents = 0
445
+ this.persistentCongestions = 0
446
+ this.ecnCongestionEvents = 0
447
+ this.restartEcn()
448
+ this.handshakeConfirmed = false
449
+ }
450
+
451
+ /**
452
+ * Starts the RTT estimate, the congestion window and the pacer again from
453
+ * their initial values, for a connection whose peer moved to a new
454
+ * address it has proved it holds (RFC 9000 §9.4): what was learnt of the
455
+ * old path says nothing about the new one. The packets in flight, the
456
+ * peer's `max_ack_delay` and the counters are kept.
457
+ */
458
+ resetPath(): void {
439
459
  this.latestRtt = 0
440
460
  this.smoothedRtt = QUIC_RECOVERY_INITIAL_RTT
441
461
  this.rttVar = QUIC_RECOVERY_INITIAL_RTT / 2
442
462
  this.minRtt = 0
443
463
  this.firstSampleTime = -1
444
- this.maxAckDelay = QUIC_RECOVERY_DEFAULT_MAX_ACK_DELAY
445
464
  this.congestionWindow = QUIC_RECOVERY_INITIAL_WINDOW
446
465
  this.ssthresh = QUIC_MAX_VARINT
447
466
  this.recoveryStart = -1
448
467
  this.pacerBudget = QUIC_RECOVERY_INITIAL_WINDOW
449
468
  this.pacerTime = -1
450
469
  this.pacerRtt = QUIC_RECOVERY_INITIAL_RTT * QUIC_RECOVERY_MICROS
451
- this.micros = -1
452
- this.ptoCount = 0
453
- this.timeoutSpace = -1
454
- this.congestionEvents = 0
455
- this.persistentCongestions = 0
456
- this.ecnCongestionEvents = 0
470
+ }
471
+
472
+ /**
473
+ * Tests ECN again from the start, as on a new path (RFC 9000 §13.4.2).
474
+ * Every packet still in flight loses its mark: those sent on the old
475
+ * path, and those sent to the new one before it was validated, which
476
+ * went out under the old test. Their loss, or an ACK of them, says
477
+ * nothing of the new test, and only packets marked from here on count
478
+ * toward it.
479
+ */
480
+ restartEcn(): void {
481
+ for (const sp of this.spaces) {
482
+ for (let s: i32 = 0; s < toI32(sp.state.length); s += 1) {
483
+ sp.setMarked(s, false)
484
+ }
485
+ }
457
486
  this.ecnState = QUIC_RECOVERY_ECN_TESTING
458
487
  this.ecnTestSent = 0
459
488
  this.ecnTestRecorded = 0
460
489
  this.ecnTestLost = 0
461
- this.handshakeConfirmed = false
462
490
  }
463
491
 
464
492
  /** Space `space`'s packets, or `null` for an index that names no space. */