@amritk/nish-aarch64-linux 0.49.1 → 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.
- package/INSTALL.md +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +233 -159
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +12 -9
- package/std/README.md +8 -5
- package/std/base64.ts +74 -1
- package/std/bytes.ts +201 -93
- package/std/compress/deflate.ts +20 -15
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/hmac.ts +1 -1
- package/std/crypto/jwt.ts +32 -19
- package/std/crypto/p256.ts +9 -12
- package/std/crypto/pki-roots.ts +7 -4
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1632 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +135 -68
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +12 -6
- package/std/json-value.ts +222 -104
- package/std/json.ts +81 -46
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +120 -38
- package/std/net/handler.ts +178 -66
- package/std/net/hpack.ts +192 -98
- package/std/net/http-compress.ts +83 -30
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +317 -20
- package/std/net/http1-client-codec.ts +137 -40
- package/std/net/http1-codec.ts +391 -202
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +12 -15
- package/std/net/http3.ts +41 -29
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +92 -54
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +66 -25
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +19 -12
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +7 -5
- package/std/net/quic.ts +36 -31
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +81 -33
- package/std/net/tls/record.ts +26 -13
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +152 -38
- package/std/net/upstream.ts +65 -19
- package/std/net/url.ts +97 -78
- package/std/net/websocket.ts +16 -9
- package/std/net/webtransport.ts +28 -12
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +158 -22
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +33 -11
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- 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 {
|
|
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 =
|
|
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
|
-
//
|
|
879
|
-
//
|
|
880
|
-
this.delivered =
|
|
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
|
-
//
|
|
1294
|
-
const end: i64 =
|
|
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
|
|
1481
|
-
*
|
|
1482
|
-
*
|
|
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 =
|
|
1487
|
+
const sum: i64 = wrappingAdd(a, b)
|
|
1486
1488
|
if (a >= 0 && b >= 0 && sum < 0) {
|
|
1487
|
-
return toI64(
|
|
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
|
|
1515
|
-
*
|
|
1516
|
-
*
|
|
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 =>
|
|
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
|
|
3747
|
-
//
|
|
3748
|
-
const
|
|
3749
|
-
|
|
3750
|
-
|
|
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
|
|
4494
|
-
//
|
|
4495
|
-
|
|
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
|
|
5041
|
-
|
|
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
|
|
5255
|
-
const want: i32 = toI32(
|
|
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
|
-
|
|
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
|
-
//
|
|
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`
|
|
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
|
|
823
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
1287
|
-
*
|
|
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
|
|
1290
|
-
|
|
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 `
|
|
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
|
-
/**
|
|
1302
|
-
const
|
|
1303
|
-
|
|
1304
|
-
|
|
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
|
-
/** `
|
|
803
|
-
const
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
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
|
+
}
|