@amritk/nish-x86_64-linux 0.49.0 → 0.50.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 (92) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
package/std/net/quic.ts CHANGED
@@ -7,13 +7,14 @@
7
7
  * Sans-IO, like the rest of `nish/net`.
8
8
  *
9
9
  * import { QuicConnection, QuicServerConfig } from "nish/net/quic";
10
- * import { tlsSignEcdsaP256 } from "nish/net/tls";
10
+ * import { TlsKeySigner } from "nish/net/tls";
11
11
  *
12
+ * const signer = new TlsKeySigner(config.signatureScheme); // config: QuicServerConfig; P-256 or RSA-PSS
12
13
  * const conn = new QuicConnection(config, entropy); // entropy: QUIC_CONN_ENTROPY_SIZE random bytes
13
14
  * conn.receiveWindow(buf, off, len, now); // every datagram the client sends; now in ms
14
15
  * const input: u8[] | null = conn.signatureInput();
15
16
  * if (input !== null) {
16
- * const signature: u8[] | null = tlsSignEcdsaP256(key, input); // key: Secret<u8[]>
17
+ * const signature: u8[] | null = signer.sign(key, input); // key: Secret<u8[]>, the leaf's own key
17
18
  * if (signature !== null) { conn.sign(signature); }
18
19
  * }
19
20
  * let n: i32 = conn.takeDatagramInto(out, 0, now); // out: at least QUIC_CONN_DATAGRAM_SIZE bytes
@@ -361,6 +362,7 @@ import {
361
362
  } from "nish/net/tls"
362
363
  import { TLS_AES_256_GCM_SHA384, TLS_CHACHA20_POLY1305_SHA256 } from "nish/net/tls/schedule"
363
364
  import { Secret, expose, exposeWith, secret, wipe } from "nish:secret"
365
+ import { wrappingAdd } from "nish:unsafe"
364
366
 
365
367
  // ---- Sizes ----------------------------------------------------------------------
366
368
 
@@ -875,9 +877,9 @@ class QuicConnReassembly {
875
877
  for (let s: i32 = start; s < capacity && s - start < n; s += 1) {
876
878
  this.mark(s, false)
877
879
  }
878
- // Exact: `delivered` only moves past bytes that arrived, below 2^62 + the ring, and `n` is at
879
- // most the ring's size, so the sum is far below 2^63 and the u64 add cannot wrap.
880
- this.delivered = toI64(toU64(this.delivered) + toU64(n))
880
+ // `delivered` only moves past bytes that arrived, below 2^62 + the ring, and `n` is at most the
881
+ // ring's size, so the sum is far below 2^63 and `quicConnSum` never holds it at the cap.
882
+ this.delivered = quicConnSum(this.delivered, toI64(n))
881
883
  }
882
884
  }
883
885
 
@@ -1290,8 +1292,8 @@ class QuicConnSpace {
1290
1292
  // Never taken: a CRYPTO offset is a varint.
1291
1293
  return
1292
1294
  }
1293
- // Exact: `offset` is a varint, below 2^62, and `length` an `i32`, so the u64 sum cannot wrap.
1294
- const end: i64 = toI64(toU64(offset) + toU64(length))
1295
+ // `offset` is a varint, below 2^62, and `length` an `i32`, so the sum is exact.
1296
+ const end: i64 = quicConnSum(offset, toI64(length))
1295
1297
  if (this.cryptoResendLow < 0) {
1296
1298
  this.cryptoResendLow = offset
1297
1299
  this.cryptoResendHigh = end
@@ -1477,14 +1479,14 @@ const quicConnBytes = (count: i64): u8[] => {
1477
1479
  * `a + b`, stopping at the largest or smallest `i64` instead of wrapping. Its
1478
1480
  * callers add a span to a time in milliseconds or a datagram's size to a byte
1479
1481
  * count, which are never near either end, so the sum is exact on every path a
1480
- * connection takes. The add is done in `u64`, where it wraps to the same bits
1481
- * as an exact two's-complement sum, and a wrap shows as a sign the operands
1482
- * did not have.
1482
+ * connection takes. The add is a `wrappingAdd` because the wrap is how an
1483
+ * overflow is seen, as a sign the operands did not have; the answer is then
1484
+ * the cap, never the wrapped value.
1483
1485
  */
1484
1486
  const quicConnSum = (a: i64, b: i64): i64 => {
1485
- const sum: i64 = toI64(toU64(a) + toU64(b))
1487
+ const sum: i64 = wrappingAdd(a, b)
1486
1488
  if (a >= 0 && b >= 0 && sum < 0) {
1487
- return toI64((toU64(1) << toU64(63)) - toU64(1))
1489
+ return toI64(-1) >>> toI64(1)
1488
1490
  }
1489
1491
  if (a < 0 && b < 0 && sum >= 0) {
1490
1492
  return toI64(toU64(1) << toU64(63))
@@ -1511,11 +1513,14 @@ const quicConnRingAt = (index: i64, entries: i32): i32 => {
1511
1513
 
1512
1514
  /**
1513
1515
  * `received * 3 - sent`, what the anti-amplification limit (RFC 9000 §8.1)
1514
- * leaves a path. Byte counts are never near the ends of `i64`, so the
1515
- * arithmetic, done in `u64` where it wraps to the bits of the exact result,
1516
- * is exact.
1516
+ * leaves a path, each step held at the ends of `i64` by `quicConnSum`. Byte
1517
+ * counts are never near those ends, so the answer is exact on every path a
1518
+ * connection takes; a `sent` below 0, which no count holds, subtracts nothing.
1517
1519
  */
1518
- const quicConnThrice = (received: i64, sent: i64): i64 => toI64(toU64(received) * toU64(3) - toU64(sent))
1520
+ const quicConnThrice = (received: i64, sent: i64): i64 => {
1521
+ const tripled: i64 = quicConnSum(quicConnSum(received, received), received)
1522
+ return sent > 0 ? quicConnSum(tripled, 0 - sent) : tripled
1523
+ }
1519
1524
 
1520
1525
  /** Puts `from[at .. at + length)` in `to`, emptied first, in the room `to` already has. */
1521
1526
  const quicConnRefill = (to: u8[], from: u8[], at: i32, length: i32): void => {
@@ -3743,12 +3748,11 @@ export class QuicConnection {
3743
3748
  }
3744
3749
  let matched: boolean = false
3745
3750
  for (let m: i32 = 0; m < this.challengeCount; m += 1) {
3746
- // At most `challengesDrawn` challenges are kept, so the index, taken
3747
- // in `u64` where it is exact, is never negative.
3748
- const base: i32 = quicConnRingAt(
3749
- toI64(toU64(this.challengesDrawn) - toU64(1) - toU64(m)),
3750
- QUIC_CONN_CHALLENGES
3751
- )
3751
+ // At most `challengesDrawn` challenges are kept, so the index is never
3752
+ // negative; were it, `quicConnRingAt` answers -1 and the slot is skipped.
3753
+ const drawn: i64 = this.challengesDrawn
3754
+ const last: i64 = drawn >= 1 ? drawn - 1 : -1
3755
+ const base: i32 = quicConnRingAt(last >= 0 ? last - toI64(m) : -1, QUIC_CONN_CHALLENGES)
3752
3756
  if (base < 0) {
3753
3757
  continue
3754
3758
  }
@@ -4490,9 +4494,11 @@ export class QuicConnection {
4490
4494
  using _scope = arena()
4491
4495
  n = this.buildProbe(out, at, size)
4492
4496
  }
4493
- // Both are byte counts far from the ends of `i64`, so the `u64`
4494
- // difference is exact.
4495
- this.probeBudget = toI64(toU64(this.probeBudget) - toU64(n))
4497
+ // Both are byte counts far from the ends of `i64`, so the difference is
4498
+ // exact; a negative `n`, no bytes built, spends nothing.
4499
+ if (n > 0) {
4500
+ this.probeBudget = quicConnSum(this.probeBudget, toI64(0 - n))
4501
+ }
4496
4502
  return n
4497
4503
  }
4498
4504
 
@@ -5037,9 +5043,8 @@ export class QuicConnection {
5037
5043
  */
5038
5044
  private beforeDatagram(): void {
5039
5045
  const limit: i64 = this.confidentialityLimit
5040
- // The limit is a positive count far from the ends of `i64`, so the
5041
- // `u64` difference is exact.
5042
- if (this.sealedUnderKey >= toI64(toU64(limit) - toU64(limit / 4))) {
5046
+ // The limit is a positive count, so three quarters of it is a proven difference.
5047
+ if (limit >= 0 && this.sealedUnderKey >= limit - limit / 4) {
5043
5048
  this.updateKeys()
5044
5049
  }
5045
5050
  const pn: i64 = this.application.nextPn
@@ -5248,11 +5253,11 @@ export class QuicConnection {
5248
5253
  if (low >= 0) {
5249
5254
  const high: i64 = space.cryptoResendHigh
5250
5255
  // A resend range is never empty while it is kept, so this is never taken.
5251
- if (high < low) {
5256
+ if (high < low || high < 0) {
5252
5257
  return at
5253
5258
  }
5254
- // `high` is at least `low`, so the `u64` difference is exact.
5255
- const want: i32 = toI32(toI64(toU64(high) - toU64(low)))
5259
+ // `high` is at least `low`, which is 0 or more, so the difference is exact.
5260
+ const want: i32 = toI32(high - low)
5256
5261
  const left: i32 = toI32(toI64(end) - toI64(at) - toI64(quicCryptoOverhead(low, want)))
5257
5262
  const n: i32 = left < want ? left : want
5258
5263
  if (n <= 0) {
package/std/net/router.ts CHANGED
@@ -118,7 +118,8 @@ const ROUTER_WILD: i32 = 2
118
118
 
119
119
  /**
120
120
  * What a `Router` holds and refuses past. Each field has a default a server
121
- * can use as it is; the constructor panics on a value no table could work with.
121
+ * can use as it is; the constructor panics on a value no table could work with, and
122
+ * `routerChecked` refuses one.
122
123
  */
123
124
  export class RouterLimits {
124
125
  /** The longest pattern, in bytes (1 to 65536). */
@@ -236,6 +237,13 @@ export class Router {
236
237
  private foundCount: i32 = 0
237
238
  /** During a match: whether a route of another method ended at the path's end. */
238
239
  private otherMethod: boolean = false
240
+ /**
241
+ * Set by `enter` on a range outside its array, and cleared by the `match`
242
+ * that reads it: only then does `match` test the ranges again, to panic.
243
+ * One left set by `tryMatch` costs the next `match` that answers
244
+ * `ROUTER_NOT_FOUND` a test that passes.
245
+ */
246
+ private outside: boolean = false
239
247
  /** The nodes the last `match` visited: never more than `nodeCount()`, which the tests pin, and none when the exact-path table answered. */
240
248
  private visited: i32 = 0
241
249
 
@@ -244,11 +252,7 @@ export class Router {
244
252
  * @param limits the caps, copied out here, so changing them later has no effect on this table
245
253
  */
246
254
  constructor(limits: RouterLimits) {
247
- routerLimit("maxPatternBytes", limits.maxPatternBytes, 1, 65536)
248
- routerLimit("maxSegments", limits.maxSegments, 1, 256)
249
- routerLimit("maxParams", limits.maxParams, 0, 256)
250
- routerLimit("maxRoutes", limits.maxRoutes, 1, 1048576)
251
- routerLimit("maxMethods", limits.maxMethods, 1, 64)
255
+ routerCheckLimits(limits)
252
256
  this.maxPatternBytes = limits.maxPatternBytes
253
257
  this.maxSegments = limits.maxSegments
254
258
  this.maxParams = limits.maxParams
@@ -275,7 +279,7 @@ export class Router {
275
279
  this.methodStart = []
276
280
  this.methodEnd = []
277
281
  this.methodKey = []
278
- // routerLimit holds both to at most 256; the clamps restate that where the
282
+ // routerCheckLimits holds both to at most 256; the clamps restate that where the
279
283
  // prover reads it, so each length fits a `u16` and its sum cannot overflow.
280
284
  let maxSegments: i32 = limits.maxSegments
281
285
  if (maxSegments > 256) {
@@ -322,6 +326,19 @@ export class Router {
322
326
  return 0
323
327
  }
324
328
 
329
+ /**
330
+ * `paramCount` without its panic: a route the table does not hold answers
331
+ * -1, for a caller whose route came from somewhere it does not trust.
332
+ * @param route a route index `add` answered
333
+ * @returns how many pairs `match` writes to `params` for that route, or -1 for a route the table does not hold
334
+ */
335
+ tryParamCount(route: i32): i32 {
336
+ if (route >= 0 && route < this.routes() && route < toI32(this.routeParams.length)) {
337
+ return this.routeParams[route]
338
+ }
339
+ return -1
340
+ }
341
+
325
342
  /**
326
343
  * How many routes the exact-path table holds: the routes `match` answers
327
344
  * without the tree. Every route with no parameter and no `*` is one, but for
@@ -775,12 +792,53 @@ export class Router {
775
792
  * @returns the matched route's index, or `ROUTER_NOT_FOUND`, `ROUTER_METHOD_NOT_ALLOWED` or `ROUTER_TOO_MANY_PARAMS`
776
793
  */
777
794
  match(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
795
+ const route: i32 = this.lookup(method, mStart, mEnd, target, tStart, tEnd, params)
796
+ if (route === ROUTER_NOT_FOUND && this.outside) {
797
+ this.outside = false
798
+ routerCheckRanges(method, mStart, mEnd, target, tStart, tEnd)
799
+ }
800
+ return route
801
+ }
802
+
803
+ /**
804
+ * `match` without its panic: a method or target range outside its array
805
+ * answers `ROUTER_NOT_FOUND`, `params` untouched, as a target that is not
806
+ * `/`-rooted does. For a server whose ranges come from a parser it does not
807
+ * want a mistake in to stop the process.
808
+ * @param method the buffer holding the request's method
809
+ * @param mStart where the method starts in `method`
810
+ * @param mEnd where the method ends in `method`
811
+ * @param target the buffer holding the request target, which may be the same buffer as `method`
812
+ * @param tStart where the target starts in `target`
813
+ * @param tEnd where the target ends in `target`, the query included
814
+ * @param params the caller's scratch for the parameter pairs, two slots for each pair
815
+ * @returns what `match` answers, or `ROUTER_NOT_FOUND` for a range outside its array
816
+ */
817
+ tryMatch(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
818
+ return this.lookup(method, mStart, mEnd, target, tStart, tEnd, params)
819
+ }
820
+
821
+ /**
822
+ * `tryMatch`, and `match` before its panic: a range outside its array is
823
+ * `ROUTER_NOT_FOUND`, tested once, in `enter`. `match` tests the ranges
824
+ * again only when `enter` found one outside, so the found and not-found
825
+ * answers pay for one test.
826
+ */
827
+ private lookup(
828
+ method: u8[],
829
+ mStart: i32,
830
+ mEnd: i32,
831
+ target: u8[],
832
+ tStart: i32,
833
+ tEnd: i32,
834
+ params: i32[]
835
+ ): i32 {
778
836
  if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
779
837
  return ROUTER_NOT_FOUND
780
838
  }
781
839
  // A static route's path is never longer than the longest one: a target
782
840
  // past that, query and all, is the tree's without a byte of it read here.
783
- // `enter` panics on a range outside `target`, so this never holds; a proof
841
+ // `enter` refuses a range outside `target`, so this never holds; a proof
784
842
  // does not cross a call.
785
843
  if (tStart < 0 || tEnd < 0) {
786
844
  return ROUTER_NOT_FOUND
@@ -812,6 +870,7 @@ export class Router {
812
870
  */
813
871
  matchTree(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
814
872
  if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
873
+ routerCheckRanges(method, mStart, mEnd, target, tStart, tEnd)
815
874
  return ROUTER_NOT_FOUND
816
875
  }
817
876
  return this.descend(target, tStart, tEnd, params)
@@ -819,19 +878,19 @@ export class Router {
819
878
 
820
879
  /**
821
880
  * The start of every lookup: the range checks, and `wanted` set to the
822
- * method's index, or -1 for one the table has never seen. False for a target
823
- * that is not `/`-rooted, which is `ROUTER_NOT_FOUND` with `pathEnd` set.
881
+ * method's index, or -1 for one the table has never seen. False for a range
882
+ * outside its array, which `match` then panics for, and for a target that
883
+ * is not `/`-rooted, which is `ROUTER_NOT_FOUND` with `pathEnd` set.
824
884
  */
825
885
  private enter(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32): boolean {
886
+ // The returns are what tell the prover that both ranges are inside their
887
+ // arrays from here on.
826
888
  if (mStart < 0 || mEnd < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
827
- routerOutside("method", mStart, mEnd, toI32(method.length))
828
- // Not reached, as below.
889
+ this.outside = true
829
890
  return false
830
891
  }
831
892
  if (tStart < 0 || tEnd < tStart || tEnd > toI32(target.length)) {
832
- routerOutside("target", tStart, tEnd, toI32(target.length))
833
- // Not reached: the helper panics. The return is what tells the prover
834
- // that `tStart .. tEnd` is inside `target` from here on.
893
+ this.outside = true
835
894
  return false
836
895
  }
837
896
  this.visited = 0
@@ -1283,27 +1342,80 @@ const routerSlot = (hash: u32, mask: i32): i32 => {
1283
1342
  }
1284
1343
 
1285
1344
  /**
1286
- * Panics for a `what` range `start` to `end` outside its `length` bytes. Apart
1287
- * from `enter`, so that a lookup holds no string of its own.
1345
+ * Panics for a method or target range outside its array, naming it; returns
1346
+ * for ranges inside. `match` calls it only when `enter` found a range
1347
+ * outside, so no other answer is tested twice. Apart
1348
+ * from the lookup, so that it holds no string of its own.
1288
1349
  */
1289
- const routerOutside = (what: string, start: i32, end: i32, length: i32): void => {
1290
- panic(`Router.match: the ${what} range ${start} to ${end} is outside its ${length} bytes`)
1350
+ const routerCheckRanges = (
1351
+ method: u8[],
1352
+ mStart: i32,
1353
+ mEnd: i32,
1354
+ target: u8[],
1355
+ tStart: i32,
1356
+ tEnd: i32
1357
+ ): void => {
1358
+ if (mStart < 0 || mEnd < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
1359
+ panic(`Router.match: the method range ${mStart} to ${mEnd} is outside its ${toI32(method.length)} bytes`)
1360
+ }
1361
+ if (tStart < 0 || tEnd < tStart || tEnd > toI32(target.length)) {
1362
+ panic(`Router.match: the target range ${tStart} to ${tEnd} is outside its ${toI32(target.length)} bytes`)
1363
+ }
1291
1364
  }
1292
1365
 
1293
1366
  /**
1294
1367
  * Panics for a `route` that is not one of the table's `routes`. Apart from
1295
- * `paramCount`, which a server calls per request, for the reason `routerOutside` is.
1368
+ * `paramCount`, which a server calls per request, for the reason `routerCheckRanges` is.
1296
1369
  */
1297
1370
  const routerNoRoute = (route: i32, routes: i32): void => {
1298
1371
  panic(`Router.paramCount: route ${route} is not in a table of ${routes}`)
1299
1372
  }
1300
1373
 
1301
- /** Panics, naming `RouterLimits.<field>`, unless `value` is in `low .. high`. */
1302
- const routerLimit = (field: string, value: i32, low: i32, high: i32): void => {
1303
- if (value < low || value > high) {
1304
- panic(`RouterLimits.${field}: ${value} is outside ${low} to ${high}`)
1374
+ /** Why `value` is no `RouterLimits.<field>`, naming it, or "" when it is in `low .. high`. */
1375
+ const routerLimitFault = (field: string, value: i32, low: i32, high: i32): string =>
1376
+ value < low || value > high ? `RouterLimits.${field}: ${value} is outside ${low} to ${high}` : ""
1377
+
1378
+ /** Why `limits` holds a field no table could work with, naming the first, or "" when every one is in range. */
1379
+ const routerLimitsFault = (limits: RouterLimits): string => {
1380
+ let fault: string = routerLimitFault("maxPatternBytes", limits.maxPatternBytes, 1, 65536)
1381
+ if (fault.length === 0) {
1382
+ fault = routerLimitFault("maxSegments", limits.maxSegments, 1, 256)
1383
+ }
1384
+ if (fault.length === 0) {
1385
+ fault = routerLimitFault("maxParams", limits.maxParams, 0, 256)
1386
+ }
1387
+ if (fault.length === 0) {
1388
+ fault = routerLimitFault("maxRoutes", limits.maxRoutes, 1, 1048576)
1305
1389
  }
1390
+ if (fault.length === 0) {
1391
+ fault = routerLimitFault("maxMethods", limits.maxMethods, 1, 64)
1392
+ }
1393
+ return fault
1394
+ }
1395
+
1396
+ /** Panics unless every field of `limits` is one a table can work with: a limit outside is the program's mistake. */
1397
+ const routerCheckLimits = (limits: RouterLimits): void => {
1398
+ const fault: string = routerLimitsFault(limits)
1399
+ if (fault.length > 0) {
1400
+ panic(fault)
1401
+ }
1402
+ }
1403
+
1404
+ /**
1405
+ * An empty table, the twin of `Router`'s constructor that refuses a limit
1406
+ * outside the range `RouterLimits` gives it as an `Err` rather than
1407
+ * panicking: for a program that reads its limits from a file or flags.
1408
+ * @param limits the caps, copied out by the table, so changing them later has no effect on it
1409
+ * @returns the table, or the first limit outside its range, named with the range it must lie in
1410
+ */
1411
+ export const routerChecked = (limits: RouterLimits): Result<Router, string> => {
1412
+ const fault: string = routerLimitsFault(limits)
1413
+ if (fault.length > 0) {
1414
+ return Err(fault)
1415
+ }
1416
+ return Ok(new Router(limits))
1306
1417
  }
1418
+
1307
1419
  /** Whether `c` is a token character (RFC 9110 §5.6.2), what a method is made of. */
1308
1420
  const routerTchar = (c: i32): boolean => {
1309
1421
  if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
package/std/net/serve.ts CHANGED
@@ -799,11 +799,11 @@ const serveParamSlots = (maxParams: i32): i32 => {
799
799
  return 2 * maxParams + 2
800
800
  }
801
801
 
802
- /** `a + b` on a clock reading, wrapping as the `u64` sum does rather than trapping. */
803
- const serveLater = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
804
-
805
- /** How long from `from` to `to` on a clock reading: 0 when `to` is not after it. */
806
- const serveSince = (from: i64, to: i64): i64 => (to > from ? toI64(toU64(to) - toU64(from)) : toI64(0))
802
+ /** `x`, or 2^63 - 1 where `x` is larger: the most an `i64` holds. */
803
+ const serveBelow2p63 = (x: u64): u64 => {
804
+ const most: u64 = (toU64(1) << toU64(63)) - toU64(1)
805
+ return x > most ? most : x
806
+ }
807
807
 
808
808
  /** A zeroed array of `n` bytes, empty when `n` is not positive. */
809
809
  const serveU8s = (n: i32): u8[] => {
@@ -2094,3 +2094,57 @@ export const serve = async (
2094
2094
  netClose(listener)
2095
2095
  return done
2096
2096
  }
2097
+
2098
+ /**
2099
+ * `a + b` on a clock reading or a count, held at the `i64` range's ends where
2100
+ * the sum would leave it, which a clock centuries short of 2^63 never makes
2101
+ * it do. Two values of opposite signs add in `i64`, as they cannot leave the
2102
+ * range. Two of one sign add as magnitudes in `u64`, where they are below
2103
+ * 2^64 and so exact: a sum of values at least 0 is the `u64` sum, and a sum
2104
+ * of values below 0 is `-1 - ((-1 - a) + (-1 - b) + 1)`. Each magnitude is
2105
+ * held below 2^63 before it is read back as an `i64`, so the read is exact
2106
+ * too, and the bounds prover sees no sum that can overflow.
2107
+ */
2108
+ const serveLater = (a: i64, b: i64): i64 => {
2109
+ if (a >= 0) {
2110
+ if (b < 0) {
2111
+ return a + b
2112
+ }
2113
+ const sum: u64 = toU64(a) + toU64(b)
2114
+ return toI64(serveBelow2p63(sum))
2115
+ }
2116
+ if (b >= 0) {
2117
+ return a + b
2118
+ }
2119
+ const past: u64 = toU64(-1 - a) + toU64(-1 - b) + toU64(1)
2120
+ const k: i64 = toI64(serveBelow2p63(past))
2121
+ if (k < 0) {
2122
+ // Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
2123
+ return -1
2124
+ }
2125
+ return -1 - k
2126
+ }
2127
+
2128
+ /**
2129
+ * How long from `from` to `to` on a clock reading: 0 when `to` is not after
2130
+ * it. Where the two have one sign the difference fits an `i64`; where `from`
2131
+ * is below 0 and `to` is not, it is the magnitudes' sum, taken in `u64` and
2132
+ * held below 2^63 as `serveLater` holds one.
2133
+ */
2134
+ const serveSince = (from: i64, to: i64): i64 => {
2135
+ if (to <= from) {
2136
+ return 0
2137
+ }
2138
+ if (from >= 0) {
2139
+ if (to >= 0) {
2140
+ return to - from
2141
+ }
2142
+ // Not taken: `to` is after `from`, which is at least 0.
2143
+ return 0
2144
+ }
2145
+ if (to < 0) {
2146
+ return to - from
2147
+ }
2148
+ const sum: u64 = toU64(to) + toU64(-1 - from) + toU64(1)
2149
+ return toI64(serveBelow2p63(sum))
2150
+ }