@amritk/nish-aarch64-linux 0.49.1 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
@@ -172,7 +172,7 @@ import {
172
172
  } from "nish:net"
173
173
  import { monotonicNanos } from "nish:process"
174
174
  import { Secret } from "nish:secret"
175
- import { getI32BE, getU16BE, tryCopyBytes } from "nish/bytes"
175
+ import { tryCopyBytes } from "nish/bytes"
176
176
  import {
177
177
  PG_ERR_CLOSED,
178
178
  PG_ERR_ENCODING,
@@ -263,25 +263,31 @@ const PG_ASYNC_SEQ_MASK: i32 = PG_ASYNC_SEQ_WRAP - 1
263
263
  const PG_ASYNC_QUEUE_MASK: i32 = PG_ASYNC_QUEUE_WRAP - 1
264
264
 
265
265
  /**
266
- * `n` plus one, wrapped below `mask` plus one, a power of two: for `n` below
267
- * that wrap it is `(n + 1) % wrap`. The sum is `u32`, which wraps by
268
- * definition, so it needs no overflow check.
266
+ * `n` plus one, wrapped below `mask` plus one, a power of two: `(n + 1) %
267
+ * wrap` for `n` below that wrap. A count at or past the mask, or below 0,
268
+ * which no step makes, starts again at 0.
269
269
  */
270
- const pgAsyncStep = (n: i32, mask: i32): i32 => toI32((toU32(n) + toU32(1)) & toU32(mask))
270
+ const pgAsyncStep = (n: i32, mask: i32): i32 => (n >= 0 && n < mask ? n + 1 : 0)
271
271
 
272
272
  /**
273
273
  * How far `from` is behind `to`, two counts that wrap below `mask` plus one,
274
- * a power of two: `(to - from + wrap) % wrap` for counts below the wrap,
275
- * taken in `u32`, which wraps by definition.
274
+ * a power of two: `(to - from + wrap) % wrap` for counts below the wrap. Both
275
+ * are at least 0, so their difference is exact before the mask takes it; a
276
+ * negative count, which no step makes, is no distance.
276
277
  */
277
- const pgAsyncGap = (to: i32, from: i32, mask: i32): i32 => toI32((toU32(to) - toU32(from)) & toU32(mask))
278
+ const pgAsyncGap = (to: i32, from: i32, mask: i32): i32 => {
279
+ if (to < 0 || from < 0) {
280
+ return 0
281
+ }
282
+ return (to - from) & mask
283
+ }
278
284
 
279
285
  /**
280
- * Where wake `w`'s socket pair starts in `wakes`, `2w`: in `u32` the double
281
- * wraps rather than checks, and a wake past 2^30 comes out negative, which
282
- * every caller checks against `wakes` before it reads.
286
+ * Where wake `w`'s socket pair starts in `wakes`, `2w`: -1 for a wake below 0
287
+ * or at 2^30 or past it, which every caller checks against `wakes` before it
288
+ * reads.
283
289
  */
284
- const pgAsyncPair = (w: i32): i32 => toI32(toU32(w) * toU32(2))
290
+ const pgAsyncPair = (w: i32): i32 => (w >= 0 && w < 1073741824 ? w * 2 : -1)
285
291
 
286
292
  /** `n` plus one, held at the `i32` ceiling: a statistic that counts on, never back. */
287
293
  const pgAsyncCount = (n: i32): i32 => (n < 2147483647 ? n + 1 : n)
@@ -888,8 +894,8 @@ const pgAsyncStartup = async (conn: PgConnection, fd: i32, began: i64, waitMs: i
888
894
  if (start < 0 || end <= start) {
889
895
  break
890
896
  }
891
- // Both are at least 0 and `end` is past `start`, so the `u32` difference is exact.
892
- const n: i32 = netWrite(fd, conn.outputBuffer(), start, toI32(toU32(end) - toU32(start)))
897
+ // Both are at least 0 and `end` is past `start`, so the difference is exact.
898
+ const n: i32 = netWrite(fd, conn.outputBuffer(), start, end - start)
893
899
  if (n === PG_ASYNC_AGAIN) {
894
900
  const waited: boolean = await pgAsyncWaitUntil(fd, true, began, waitMs)
895
901
  if (!waited) {
@@ -957,9 +963,10 @@ const pgAsyncWaitUntil = async (fd: i32, write: boolean, began: i64, waitMs: i32
957
963
 
958
964
  /** Milliseconds since `began`, a `monotonicNanos` reading. */
959
965
  const pgAsyncSince = (began: i64): i32 => {
960
- // `u64` arithmetic wraps rather than checks; a clock read after `began`
961
- // is never behind it, so the difference is the real one.
962
- const ms: i64 = toI64((toU64(monotonicNanos()) - toU64(began)) / toU64(1000000))
966
+ // A clock read after `began` is never behind it; a reading that is, or a
967
+ // negative one, counts as no time.
968
+ const now: i64 = monotonicNanos()
969
+ const ms: i64 = began >= 0 && now >= 0 && now >= began ? (now - began) / toI64(1000000) : toI64(0)
963
970
  return ms > toI64(1073741823) ? 1073741823 : toI32(ms)
964
971
  }
965
972
 
@@ -1060,8 +1067,9 @@ const pgAsyncWakePool = (pool: PgAsyncPool): void => {
1060
1067
  wake = wake > PG_ASYNC_LANES ? PG_ASYNC_LANES : wake
1061
1068
  for (let k: i32 = 0; k < wake; k++) {
1062
1069
  // The queue lane of the `k`th task waiting, after the pool's own wake.
1063
- const lane: u32 = (toU32(pool.seatHead) + toU32(k)) & toU32(PG_ASYNC_LANES - 1)
1064
- pgAsyncSignal(pool, toI32(toU32(PG_ASYNC_POOL_WAKE + 1) + lane))
1070
+ const lane: i32 =
1071
+ ((pool.seatHead & (PG_ASYNC_LANES - 1)) + (k & (PG_ASYNC_LANES - 1))) & (PG_ASYNC_LANES - 1)
1072
+ pgAsyncSignal(pool, PG_ASYNC_POOL_WAKE + 1 + lane)
1065
1073
  }
1066
1074
  }
1067
1075
 
@@ -1181,16 +1189,16 @@ export const pgAsyncClaim = async (pool: PgAsyncPool): Promise<i32> => {
1181
1189
  }
1182
1190
  live = true
1183
1191
  if (toI32(pool.claimed[s]) === 0 && pool.seated[s] === 0 && pool.doomAt[s] < 0) {
1184
- // The claim count wraps below 2^24, so in `u32` the ticket, the count
1185
- // times 64 slots plus the slot, is exact and below 2^30.
1186
- const claim: u32 = (toU32(pool.claims[s]) + toU32(1)) & toU32(PG_ASYNC_CLAIM_MASK)
1192
+ // The claim count wraps below 2^24, so the ticket, the count times 64
1193
+ // slots plus the slot, is below 2^30.
1194
+ const claim: i32 = pgAsyncStep(pool.claims[s], PG_ASYNC_CLAIM_MASK) & PG_ASYNC_CLAIM_MASK
1187
1195
  pool.claimed[s] = 1
1188
- pool.claims[s] = toI32(claim)
1196
+ pool.claims[s] = claim
1189
1197
  if (waited) {
1190
1198
  // Another waiter may find room this one did not take.
1191
1199
  pgAsyncWakePool(pool)
1192
1200
  }
1193
- return toI32(claim * toU32(PG_ASYNC_MAX_SIZE) + toU32(s))
1201
+ return claim * PG_ASYNC_MAX_SIZE + (s & (PG_ASYNC_MAX_SIZE - 1))
1194
1202
  }
1195
1203
  }
1196
1204
  if (!live) {
@@ -1374,9 +1382,11 @@ export const pgAsyncSeat = async (pool: PgAsyncPool, statement: i32): Promise<i3
1374
1382
  ? pgAsyncGap(queued, pool.seatHead, PG_ASYNC_QUEUE_MASK) < free
1375
1383
  : pool.seatHead === pool.seatTail
1376
1384
  if (best >= 0 && turn) {
1377
- // `best` is below 64 and `statement` below 64, so in `u32` these
1378
- // offsets are exact; each is then checked against its arrays.
1379
- const known: i32 = toI32(toU32(best) * toU32(PG_ASYNC_MAX_STATEMENTS) + toU32(statement))
1385
+ // `best` is below 64 and `statement` below 64, so the offset is exact;
1386
+ // it is then checked against its array.
1387
+ const known: i32 =
1388
+ (best & (PG_ASYNC_MAX_SIZE - 1)) * PG_ASYNC_MAX_STATEMENTS +
1389
+ (statement & (PG_ASYNC_MAX_STATEMENTS - 1))
1380
1390
  for (let k: i32 = 0; k < PG_ASYNC_MAX_DEPTH; k++) {
1381
1391
  const seat: i32 = pgAsyncSeatAt(best, k)
1382
1392
  if (
@@ -1400,8 +1410,8 @@ export const pgAsyncSeat = async (pool: PgAsyncPool, statement: i32): Promise<i3
1400
1410
  continue
1401
1411
  }
1402
1412
  // The seat claim count wraps below 2^20, so the ticket, the count
1403
- // times 1024 seats plus the seat, is exact in `u32` and below 2^30.
1404
- const claim: u32 = (toU32(pool.seatClaims[seat]) + toU32(1)) & toU32(PG_ASYNC_SEAT_MASK)
1413
+ // times 1024 seats plus the seat, is below 2^30.
1414
+ const claim: i32 = pgAsyncStep(pool.seatClaims[seat], PG_ASYNC_SEAT_MASK) & PG_ASYNC_SEAT_MASK
1405
1415
  pool.seatUsed[seat] = 1
1406
1416
  pool.seatWaiting[seat] = 0
1407
1417
  pool.seatSeq[seat] = pool.nextSeq[best]
@@ -1411,10 +1421,10 @@ export const pgAsyncSeat = async (pool: PgAsyncPool, statement: i32): Promise<i3
1411
1421
  pool.nextSeq[best] = pgAsyncStep(pool.nextSeq[best], PG_ASYNC_SEQ_MASK)
1412
1422
  pool.seated[best] = pgAsyncCount(pool.seated[best])
1413
1423
  pool.writing[best] = 1
1414
- pool.seatClaims[seat] = toI32(claim)
1424
+ pool.seatClaims[seat] = claim
1415
1425
  // The next in the queue may find a seat this one did not take.
1416
1426
  pgAsyncUnqueue(pool, queued)
1417
- return toI32(claim * toU32(PG_ASYNC_SEATS) + toU32(seat))
1427
+ return claim * PG_ASYNC_SEATS + (seat & (PG_ASYNC_SEATS - 1))
1418
1428
  }
1419
1429
  }
1420
1430
  if (!live) {
@@ -1437,10 +1447,7 @@ export const pgAsyncSeat = async (pool: PgAsyncPool, statement: i32): Promise<i3
1437
1447
  continue
1438
1448
  }
1439
1449
  // A queued task waits on its queue lane, after the pool's own wake.
1440
- const w: i32 =
1441
- queued >= 0
1442
- ? toI32(toU32(PG_ASYNC_POOL_WAKE + 1) + (toU32(queued) & toU32(PG_ASYNC_LANES - 1)))
1443
- : PG_ASYNC_POOL_WAKE
1450
+ const w: i32 = queued >= 0 ? PG_ASYNC_POOL_WAKE + 1 + (queued & (PG_ASYNC_LANES - 1)) : PG_ASYNC_POOL_WAKE
1444
1451
  pool.waits = pgAsyncCount(pool.waits)
1445
1452
  await pgAsyncPark(pool, w, left)
1446
1453
  pgAsyncDrain(pool, w)
@@ -1509,8 +1516,13 @@ export const pgAsyncSend = async (pool: PgAsyncPool, ticket: i32, params: PgAsyn
1509
1516
  }
1510
1517
  let sent: i32 = 0
1511
1518
  while (sent >= 0 && sent < toI32(out.length)) {
1512
- // Both are at least 0 and `sent` is short of the end, so the `u32` difference is exact.
1513
- const n: i32 = netWrite(fd, out, sent, toI32(toU32(toI32(out.length)) - toU32(sent)))
1519
+ // Both are at least 0 and `sent` is short of the end, so the difference is exact.
1520
+ const total: i32 = toI32(out.length)
1521
+ let rest: i32 = 0
1522
+ if (sent >= 0 && total >= 0) {
1523
+ rest = total - sent
1524
+ }
1525
+ const n: i32 = netWrite(fd, out, sent, rest)
1514
1526
  if (n === PG_ASYNC_AGAIN) {
1515
1527
  const waited: boolean = await writable(fd)
1516
1528
  const failed: i32 = pgAsyncWaitFailure(waited)
@@ -1549,10 +1561,15 @@ const pgAsyncAhead = (served: i32, seq: i32): i32 => pgAsyncGap(seq, served, PG_
1549
1561
 
1550
1562
  /**
1551
1563
  * Where slot `s`'s `k`th seat is in the seat arrays, `s * PG_ASYNC_MAX_DEPTH
1552
- * + k`: in `u32` the sum wraps rather than checks, and every caller checks
1553
- * the seat against the arrays it reads.
1564
+ * + k`: -1 for a slot or depth out of range, and every caller checks the
1565
+ * seat against the arrays it reads.
1554
1566
  */
1555
- const pgAsyncSeatAt = (s: i32, k: i32): i32 => toI32(toU32(s) * toU32(PG_ASYNC_MAX_DEPTH) + toU32(k))
1567
+ const pgAsyncSeatAt = (s: i32, k: i32): i32 => {
1568
+ if (s < 0 || s >= PG_ASYNC_MAX_SIZE || k < 0 || k >= PG_ASYNC_MAX_DEPTH) {
1569
+ return -1
1570
+ }
1571
+ return (s & (PG_ASYNC_MAX_SIZE - 1)) * PG_ASYNC_MAX_DEPTH + (k & (PG_ASYNC_MAX_DEPTH - 1))
1572
+ }
1556
1573
 
1557
1574
  /**
1558
1575
  * Whether seat `seat`'s answer is lost: its connection failed at or before
@@ -1801,8 +1818,8 @@ const pgAsyncRoom = (pool: PgAsyncPool, s: i32, input: u8[]): i32 => {
1801
1818
  }
1802
1819
  let room: i32 = end
1803
1820
  if (start > 0) {
1804
- // In `u32`: `end` is at or past `start`, so the difference is the bytes kept.
1805
- room = toI32(toU32(end) - toU32(start))
1821
+ // `end` is at or past `start`, so the difference is the bytes kept.
1822
+ room = end - start
1806
1823
  if (!tryCopyBytes(input, 0, input, start, room)) {
1807
1824
  return -1
1808
1825
  }
@@ -1995,8 +2012,8 @@ const pgAsyncPlain = (pool: PgAsyncPool, s: i32): boolean => {
1995
2012
  if (size < 4 || size >= left) {
1996
2013
  return false
1997
2014
  }
1998
- // In `u32`: the frame ends at or before `stop`, so the sum cannot wrap.
1999
- const next: i32 = toI32(toU32(at) + toU32(1) + toU32(size))
2015
+ // In i64: the frame ends before `stop`, so the sum fits an i32.
2016
+ const next: i32 = toI32(toI64(at) + toI64(1) + toI64(size))
2000
2017
  if (type === 90) {
2001
2018
  return next === stop && size === 5 && toI32(input[at + 5]) === PG_ASYNC_IDLE
2002
2019
  }
@@ -2119,8 +2136,8 @@ const pgAsyncScan = (pool: PgAsyncPool, s: i32): boolean => {
2119
2136
  return false
2120
2137
  }
2121
2138
  const type: i32 = toI32(input[at])
2122
- // In `u32`: the frame ends at or before `end`, so the sum cannot wrap.
2123
- at = toI32(toU32(at) + toU32(1) + toU32(size))
2139
+ // In i64: the frame ends at or before `end`, so the sum fits an i32.
2140
+ at = toI32(toI64(at) + toI64(1) + toI64(size))
2124
2141
  if (type === 49) {
2125
2142
  pool.inParsed[s] = 1
2126
2143
  }
@@ -2308,8 +2325,11 @@ export const pgAsyncAnswer = async (pool: PgAsyncPool, ticket: i32, turn: i32):
2308
2325
  const fd: i32 = pool.fds[s]
2309
2326
  const from: i32 = pool.inStart[s]
2310
2327
  const to: i32 = pool.inAnswer[s] >= 0 ? pool.inAnswer[s] : pool.inEnd[s]
2311
- // In `u32`: `to` is at or past `from` here, so the difference is the bytes.
2312
- const count: i32 = from >= 0 && from <= to && to <= toI32(input.length) ? toI32(toU32(to) - toU32(from)) : 0
2328
+ // `to` is at or past `from` here, so the difference is the bytes.
2329
+ let count: i32 = 0
2330
+ if (from >= 0 && from <= to && to <= toI32(input.length)) {
2331
+ count = to - from
2332
+ }
2313
2333
  if (count < 0 || count > 65535) {
2314
2334
  // Not reached: an input is `PG_ASYNC_INPUT_SIZE` bytes. `pgAsyncZeroed`
2315
2335
  // would build a longer one by `push`, which the spawn rules read as
@@ -2451,8 +2471,13 @@ export const pgAsyncQuery = async (
2451
2471
  }
2452
2472
  let sent: i32 = 0
2453
2473
  while (sent >= 0 && sent < toI32(out.length)) {
2454
- // In `u32`: `sent` is below the `length` here, so the difference is the rest.
2455
- const n: i32 = netWrite(fd, out, sent, toI32(toU32(out.length) - toU32(sent)))
2474
+ // `sent` is at least 0 and below the `length` here, so the difference is the rest.
2475
+ const total: i32 = toI32(out.length)
2476
+ let rest: i32 = 0
2477
+ if (sent >= 0 && total >= 0) {
2478
+ rest = total - sent
2479
+ }
2480
+ const n: i32 = netWrite(fd, out, sent, rest)
2456
2481
  if (n === PG_ASYNC_AGAIN) {
2457
2482
  const waited: boolean = await writable(fd)
2458
2483
  const failed: i32 = pgAsyncWaitFailure(waited)
@@ -2627,19 +2652,14 @@ const pgAsyncLabel = (out: u8[], name: i32): void => {
2627
2652
  }
2628
2653
 
2629
2654
  /**
2630
- * A zeroed byte array of `n` bytes, or an empty one for a negative `n`. Up to
2631
- * 64 KiB it is one `new Array` of a `u16` length, which needs no check; a
2632
- * longer one, the rare buffer for a message past that, is built by `push`.
2655
+ * A zeroed byte array of `n` bytes, or an empty one for a negative `n`: one
2656
+ * allocation, its length proven non-negative by the guard.
2633
2657
  */
2634
2658
  const pgAsyncZeroed = (n: i32): u8[] => {
2635
- if (n >= 0 && n <= 65535) {
2636
- return new Array<u8>(toU16(n))
2637
- }
2638
- const out: u8[] = []
2639
- while (toI32(out.length) < n) {
2640
- out.push(0)
2659
+ if (n < 0) {
2660
+ return []
2641
2661
  }
2642
- return out
2662
+ return new Array<u8>(n)
2643
2663
  }
2644
2664
 
2645
2665
  /** `s`'s UTF-8 bytes, or null when one is zero, which a C string in the protocol cannot hold. */
@@ -2740,7 +2760,13 @@ const pgAsyncRead = async (
2740
2760
  return pgAsyncLost(result, PG_ERR_FRAME, 0)
2741
2761
  }
2742
2762
  const type: i32 = toI32(buf[start])
2743
- const length: i32 = getI32BE(buf, start + 1)
2763
+ // Read here rather than with `getI32BE`, whose window the prover would
2764
+ // need tested again after the calls before it.
2765
+ const length: i32 =
2766
+ (toI32(buf[start + 1]) << 24) |
2767
+ (toI32(buf[start + 2]) << 16) |
2768
+ (toI32(buf[start + 3]) << 8) |
2769
+ toI32(buf[start + 4])
2744
2770
  if (length < 4) {
2745
2771
  return pgAsyncLost(result, PG_ERR_FRAME, 0)
2746
2772
  }
@@ -2772,10 +2798,12 @@ const pgAsyncRead = async (
2772
2798
  }
2773
2799
  if (type === 68) {
2774
2800
  // 'D': DataRow, a count then each column's length (-1 for NULL) and bytes.
2775
- if (size < 2) {
2801
+ // The body lies in `buf`, so the second test is never taken: it is
2802
+ // the one the count's two bytes are read under.
2803
+ if (size < 2 || body < 0 || body > toI32(buf.length) - 2) {
2776
2804
  return pgAsyncLost(result, PG_ERR_FRAME, 0)
2777
2805
  }
2778
- const columns: i32 = toI32(getU16BE(buf, body))
2806
+ const columns: i32 = toI32((toU16(buf[body]) << 8) | toU16(buf[body + 1]))
2779
2807
  if (columns > maxColumns) {
2780
2808
  return pgAsyncLost(result, PG_ERR_TOO_BIG, 0)
2781
2809
  }
@@ -2870,7 +2898,12 @@ const pgAsyncRead = async (
2870
2898
  const held: i32 = end - start
2871
2899
  want = toI32(5) - held
2872
2900
  if (held >= 5 && start < toI32(buf.length) - 5) {
2873
- const length: i32 = getI32BE(buf, start + 1)
2901
+ // Read here rather than with `getI32BE`, as above.
2902
+ const length: i32 =
2903
+ (toI32(buf[start + 1]) << 24) |
2904
+ (toI32(buf[start + 2]) << 16) |
2905
+ (toI32(buf[start + 3]) << 8) |
2906
+ toI32(buf[start + 4])
2874
2907
  // A length past `maxMessage` was failed before this read.
2875
2908
  if (length < 4) {
2876
2909
  return pgAsyncLost(result, PG_ERR_FRAME, 0)
@@ -182,6 +182,29 @@ const PG_CARRIER_TIMED_OUT: i32 = -110
182
182
  const PG_CARRIER_MAX_BATCHERS: i32 = 8
183
183
  /** The most queries or executions one carrier holds. */
184
184
  const PG_CARRIER_MAX_QUERIES: i32 = 65536
185
+ /** 2^62 - 1 nanoseconds, past any monotonic clock reading; a connect deadline stops there. */
186
+ const PG_CARRIER_CLOCK_MOST: i64 = 1073741824 * 4294967296 - 1
187
+
188
+ /**
189
+ * The monotonic clock plus `span` nanoseconds, held at `PG_CARRIER_CLOCK_MOST`
190
+ * instead of passing it.
191
+ *
192
+ * @param span - The nanoseconds to add, at least 0.
193
+ * @returns The deadline, or the clock itself when either reading is negative.
194
+ */
195
+ const pgCarrierDeadline = (span: i64): i64 => {
196
+ const now: i64 = monotonicNanos()
197
+ if (now < 0 || span < 0) {
198
+ return now
199
+ }
200
+ const room: i64 = PG_CARRIER_CLOCK_MOST - now
201
+ if (room < 0) {
202
+ return PG_CARRIER_CLOCK_MOST
203
+ }
204
+ const left: i64 = room - span
205
+ return left > 0 ? PG_CARRIER_CLOCK_MOST - left : PG_CARRIER_CLOCK_MOST
206
+ }
207
+
185
208
  /** Where `next()` is in its pass, so that a call resumes where the last stopped. */
186
209
  const PG_CARRIER_POLL: i32 = 0
187
210
  const PG_CARRIER_SLOTS: i32 = 1
@@ -466,7 +489,10 @@ export class PgCarrier {
466
489
  const runs: PgCarrierExecution[] = []
467
490
  const eFree: PgCarrierExecution[] = []
468
491
  for (let e: i32 = 0; e < executions; e++) {
469
- runs.push(new PgCarrierExecution(toI32(toU32(queries) + toU32(e))))
492
+ // Summed in i64; both counts are at most PG_CARRIER_MAX_QUERIES, checked
493
+ // above, so the id fits an i32.
494
+ const id: i64 = toI64(queries) + toI64(e)
495
+ runs.push(new PgCarrierExecution(toI32(id)))
470
496
  }
471
497
  k = toI32(runs.length)
472
498
  while (k > 0) {
@@ -1272,9 +1298,7 @@ export class PgCarrier {
1272
1298
  const sockets: PgCarrierSlot[] = this.sockets
1273
1299
  if (connect) {
1274
1300
  let want: i32 = this.pool.wanted()
1275
- // In `u64`, which wraps by definition: a monotonic clock is centuries
1276
- // short of the top.
1277
- const deadline: i64 = want >= 0 ? toI64(toU64(monotonicNanos()) + toU64(this.timeoutNs)) : toI64(0)
1301
+ const deadline: i64 = want >= 0 ? pgCarrierDeadline(this.timeoutNs) : toI64(0)
1278
1302
  while (want >= 0 && want < toI32(sockets.length)) {
1279
1303
  const slot: PgCarrierSlot = sockets[want]
1280
1304
  crypto.getRandomValues(this.nonce)