@amritk/nish-x86_64-linux 0.38.0 → 0.40.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
package/std/net/router.ts CHANGED
@@ -25,6 +25,16 @@
25
25
  * nothing per request. A `Router` is therefore not reentrant: one match at a
26
26
  * time, which is what a connection loop does.
27
27
  *
28
+ * **A route with no parameter and no `*` is answered without the tree.** `add`
29
+ * also files such a route under a hash of its method and path, in one more
30
+ * flat array, and `match` looks there first, for a target no longer than the
31
+ * longest such route: one pass over the path, at most `ROUTER_PROBES` slots,
32
+ * and a byte comparison for a slot whose method and length agree. The
33
+ * tree stays the definition: a literal is tried first at every segment, so
34
+ * the route the exact table holds for a method and a path is the one the tree
35
+ * would have reached, and anything the table does not hold falls through to
36
+ * the tree unchanged. `matchTree` is the tree alone, for a test of exactly that.
37
+ *
28
38
  * **The rules** (`docs/wp43-http-app-layer.md` §2):
29
39
  *
30
40
  * - The path is the target up to its first `?` (the query is left alone) and
@@ -42,9 +52,20 @@
42
52
  * - A path that matches only routes of other methods answers
43
53
  * `ROUTER_METHOD_NOT_ALLOWED`, so a server can tell 405 from 404.
44
54
  *
55
+ * **What is unchecked.** `routerSameBytes` reads with `nish:unsafe`'s
56
+ * `uncheckedGet`, after one check that both ranges are inside their arrays;
57
+ * a range that is not compares unequal, as a byte that differs does. It is
58
+ * the one byte compare a lookup makes: a method of more than seven bytes
59
+ * against each known one (a shorter one is one packed word, `routerMethodKey`),
60
+ * a path against a static route's, a segment against a label.
61
+ * `routerMethodKey` reads its at most seven bytes the same way, after the same
62
+ * one range check; a range outside the array packs to 0, which no method is.
63
+ * Every other read is checked.
64
+ *
45
65
  * Private helpers share the importing program's flat symbol namespace
46
66
  * (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
47
67
  */
68
+ import { uncheckedGet, wrappingAdd } from "nish:unsafe"
48
69
 
49
70
  /** `add`: the pattern is empty, does not start with `/`, or misuses `:`, `*`, `%`, `?` or a byte outside `!` to `~`. */
50
71
  export const ROUTER_BAD_PATTERN: i32 = -1
@@ -73,6 +94,12 @@ export const ROUTER_NO_ROOM: i32 = -11
73
94
  const ROUTER_MAX_METHOD: i32 = 32
74
95
  /** No node or route: an `i32` spelled once, so that `--number-mode f64` does not read a bare `-1` as a double. */
75
96
  const ROUTER_NONE: i32 = -1
97
+ /**
98
+ * The most slots a lookup in the exact-path table reads, and so the furthest
99
+ * from its home slot `add` will file a route. A static route with no free slot
100
+ * that near is left to the tree, which answers it as it always did.
101
+ */
102
+ const ROUTER_PROBES: i32 = 8
76
103
  /** A pattern segment's kind, in `Router.add`'s scratch. */
77
104
  const ROUTER_LITERAL: i32 = 0
78
105
  const ROUTER_PARAM: i32 = 1
@@ -121,6 +148,18 @@ export class Router {
121
148
  private routeMethod: i32[]
122
149
  private endpointNext: i32[]
123
150
  private routeParams: i32[]
151
+ /**
152
+ * The exact-path table: per slot a static route, or -1. Its length is a
153
+ * power of two, at least four slots for each static route, and a route sits
154
+ * within `ROUTER_PROBES` slots of the one its hash names.
155
+ */
156
+ private staticSlots: i32[]
157
+ /** Per route: the hash of its method and path, and its path `staticPaths[routePathStart .. routePathEnd)`; zeros unless it is static. */
158
+ private routeHash: u32[]
159
+ private routePathStart: i32[]
160
+ private routePathEnd: i32[]
161
+ /** Every static route's path, back to back. */
162
+ private staticPaths: u8[]
124
163
  /**
125
164
  * The methods the table knows, `methodBytes[methodStart[m] .. methodEnd[m])`, compared exactly (RFC 9110 §9.1).
126
165
  * @internal Read by std/net/handler.ts, which matches a path against each known method to build a 405's `Allow` value.
@@ -136,6 +175,12 @@ export class Router {
136
175
  * @internal Read by std/net/handler.ts beside `methodStart`, for the same matches.
137
176
  */
138
177
  methodEnd: i32[]
178
+ /**
179
+ * Per method: its bytes and length packed by `routerMethodKey`, or 0 for one
180
+ * too long to pack. Private and written only by `add`, beside the public
181
+ * arrays it is made from, so a lookup compares one word for a method.
182
+ */
183
+ private methodKey: u64[]
139
184
  /** `match`'s scratch, sized by the constructor: each segment's span, then the pairs found so far. */
140
185
  private segStart: i32[]
141
186
  private segEnd: i32[]
@@ -154,16 +199,25 @@ export class Router {
154
199
  maxParams: i32 = 0
155
200
  private maxRoutes: i32 = 0
156
201
  private maxMethods: i32 = 0
202
+ /** How many routes have no parameter and no `*`, and how many of them `staticSlots` holds. */
203
+ private statics: i32 = 0
204
+ private staticHeld: i32 = 0
205
+ /** The longest static route's path, in bytes: a longer target is not hashed at all. */
206
+ private staticLongest: i32 = 0
157
207
  /** The most segments any route has: a path is split no further than one past it. */
158
208
  private deepest: i32 = 0
159
- /** During a match: how many segments the path has, where it ends, the method's index, and the pairs in `found`. */
209
+ /** During a match: how many segments the path has, the method's index, and the pairs in `found`. */
160
210
  private segments: i32 = 0
161
- private pathEnd: i32 = 0
211
+ /**
212
+ * Where the last `match`'s path ends in its target: the first `?`, or the target's end.
213
+ * @internal Read by std/net/handler.ts, so a request's path is not scanned twice.
214
+ */
215
+ pathEnd: i32 = 0
162
216
  private wanted: i32 = 0
163
217
  private foundCount: i32 = 0
164
218
  /** During a match: whether a route of another method ended at the path's end. */
165
219
  private otherMethod: boolean = false
166
- /** The nodes the last `match` visited: never more than `nodeCount()`, which the tests pin. */
220
+ /** The nodes the last `match` visited: never more than `nodeCount()`, which the tests pin, and none when the exact-path table answered. */
167
221
  private visited: i32 = 0
168
222
 
169
223
  /**
@@ -192,9 +246,15 @@ export class Router {
192
246
  this.routeMethod = []
193
247
  this.endpointNext = []
194
248
  this.routeParams = []
249
+ this.staticSlots = [-1, -1, -1, -1, -1, -1, -1, -1]
250
+ this.routeHash = []
251
+ this.routePathStart = []
252
+ this.routePathEnd = []
253
+ this.staticPaths = []
195
254
  this.methodBytes = []
196
255
  this.methodStart = []
197
256
  this.methodEnd = []
257
+ this.methodKey = []
198
258
  this.segStart = new Array<i32>(limits.maxSegments + 1)
199
259
  this.segEnd = new Array<i32>(limits.maxSegments + 1)
200
260
  this.found = new Array<i32>(2 * limits.maxParams)
@@ -223,14 +283,24 @@ export class Router {
223
283
  */
224
284
  paramCount(route: i32): i32 {
225
285
  if (route < 0 || route >= this.routes()) {
226
- panic(`Router.paramCount: route ${route} is not in a table of ${this.routes()}`)
286
+ routerNoRoute(route, this.routes())
227
287
  }
228
288
  return this.routeParams[route]
229
289
  }
230
290
 
231
291
  /**
232
- * How many nodes the last `match` visited.
233
- * @returns the visit count, never more than `nodeCount()`, which the tests use to pin a lookup's cost
292
+ * How many routes the exact-path table holds: the routes `match` answers
293
+ * without the tree. Every route with no parameter and no `*` is one, but for
294
+ * a route whose slots were all taken, which the tree answers instead.
295
+ * @returns the count, never more than the routes that have no parameter and no `*`
296
+ */
297
+ staticCount(): i32 {
298
+ return this.staticHeld
299
+ }
300
+
301
+ /**
302
+ * How many nodes of the tree the last `match` visited.
303
+ * @returns the visit count, never more than `nodeCount()`, and 0 when the exact-path table answered, which the tests use to pin a lookup's cost
234
304
  */
235
305
  lastVisited(): i32 {
236
306
  return this.visited
@@ -301,6 +371,7 @@ export class Router {
301
371
  this.methodBytes.push(toU8(toI32(method.charCodeAt(k))))
302
372
  }
303
373
  this.methodEnd.push(toI32(this.methodBytes.length))
374
+ this.methodKey.push(routerMethodKey(this.methodBytes, this.methodStart[methodId], methodLength))
304
375
  }
305
376
  node = 0
306
377
  for (let k: i32 = 0; k < count; k++) {
@@ -315,9 +386,64 @@ export class Router {
315
386
  if (count > this.deepest) {
316
387
  this.deepest = count
317
388
  }
389
+ this.routePathStart.push(toI32(this.staticPaths.length))
390
+ let hash: u32 = routerHashOf(methodId)
391
+ if (params === 0) {
392
+ for (let k: i32 = 0; k < length; k++) {
393
+ const c: i32 = toI32(pattern.charCodeAt(k))
394
+ this.staticPaths.push(toU8(c))
395
+ hash = routerHashWith(hash, c)
396
+ }
397
+ }
398
+ this.routePathEnd.push(toI32(this.staticPaths.length))
399
+ this.routeHash.push(hash)
400
+ if (params === 0) {
401
+ this.statics += 1
402
+ if (length > this.staticLongest) {
403
+ this.staticLongest = length
404
+ }
405
+ if (4 * this.statics > toI32(this.staticSlots.length)) {
406
+ this.growStatic()
407
+ } else {
408
+ this.fileStatic(route)
409
+ }
410
+ }
318
411
  return route
319
412
  }
320
413
 
414
+ /** Doubles the exact-path table and files every static route again, in the order they were added. */
415
+ private growStatic(): void {
416
+ const slots: i32 = 2 * toI32(this.staticSlots.length)
417
+ this.staticSlots = new Array<i32>(slots)
418
+ for (let k: i32 = 0; k < slots; k++) {
419
+ this.staticSlots[k] = ROUTER_NONE
420
+ }
421
+ this.staticHeld = 0
422
+ for (let r: i32 = 0; r < this.routes(); r++) {
423
+ if (this.routeParams[r] === 0) {
424
+ this.fileStatic(r)
425
+ }
426
+ }
427
+ }
428
+
429
+ /**
430
+ * Files the static route `route` in the first free slot of the
431
+ * `ROUTER_PROBES` from its hash's, or nowhere when all are taken: the tree
432
+ * holds every route, so one the table lacks is still answered.
433
+ */
434
+ private fileStatic(route: i32): void {
435
+ const mask: i32 = toI32(this.staticSlots.length) - 1
436
+ let slot: i32 = routerSlot(this.routeHash[route], mask)
437
+ for (let probe: i32 = 0; probe < ROUTER_PROBES; probe++) {
438
+ if (this.staticSlots[slot] < 0) {
439
+ this.staticSlots[slot] = route
440
+ this.staticHeld += 1
441
+ return
442
+ }
443
+ slot = (slot + 1) & mask
444
+ }
445
+ }
446
+
321
447
  /**
322
448
  * Splits `pattern`, which starts with `/`, into `patKind`/`patStart`/`patEnd`
323
449
  * and answers how many segments it has, or the code that refuses it.
@@ -441,49 +567,136 @@ export class Router {
441
567
  * @returns the matched route's index, or `ROUTER_NOT_FOUND`, `ROUTER_METHOD_NOT_ALLOWED` or `ROUTER_TOO_MANY_PARAMS`
442
568
  */
443
569
  match(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
570
+ if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
571
+ return ROUTER_NOT_FOUND
572
+ }
573
+ // A static route's path is never longer than the longest one: a target
574
+ // past that, query and all, is the tree's without a byte of it read here.
575
+ if (tEnd - tStart <= this.staticLongest) {
576
+ const route: i32 = this.staticRoute(target, tStart, tEnd)
577
+ if (route !== ROUTER_NONE) {
578
+ return route
579
+ }
580
+ // The pass that hashed the path split it too, so the tree reads no byte again.
581
+ return this.answer(target, params)
582
+ }
583
+ return this.descend(target, tStart, tEnd, params)
584
+ }
585
+
586
+ /**
587
+ * What `match` answers, found by the tree alone: the exact-path table is not
588
+ * read. The two agree on every method and target, in the route or code they
589
+ * answer, in `params` and in where the path ends; this one is the definition
590
+ * and exists so that a test can hold `match` to it.
591
+ * @param method the buffer holding the request's method
592
+ * @param mStart where the method starts in `method`
593
+ * @param mEnd where the method ends in `method`
594
+ * @param target the buffer holding the request target, which may be the same buffer as `method`
595
+ * @param tStart where the target starts in `target`
596
+ * @param tEnd where the target ends in `target`, the query included
597
+ * @param params the caller's scratch for the parameter pairs, two slots for each pair
598
+ * @returns the matched route's index, or `ROUTER_NOT_FOUND`, `ROUTER_METHOD_NOT_ALLOWED` or `ROUTER_TOO_MANY_PARAMS`
599
+ */
600
+ matchTree(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
601
+ if (!this.enter(method, mStart, mEnd, target, tStart, tEnd)) {
602
+ return ROUTER_NOT_FOUND
603
+ }
604
+ return this.descend(target, tStart, tEnd, params)
605
+ }
606
+
607
+ /**
608
+ * The start of every lookup: the range checks, and `wanted` set to the
609
+ * method's index, or -1 for one the table has never seen. False for a target
610
+ * that is not `/`-rooted, which is `ROUTER_NOT_FOUND` with `pathEnd` set.
611
+ */
612
+ private enter(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32): boolean {
444
613
  if (mStart < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
445
- panic(`Router.match: the method range ${mStart} to ${mEnd} is outside its ${method.length} bytes`)
614
+ routerOutside("method", mStart, mEnd, toI32(method.length))
446
615
  }
447
616
  if (tStart < 0 || tEnd < tStart || tEnd > toI32(target.length)) {
448
- panic(`Router.match: the target range ${tStart} to ${tEnd} is outside its ${target.length} bytes`)
617
+ routerOutside("target", tStart, tEnd, toI32(target.length))
618
+ // Not reached: the helper panics. The return is what tells the prover
619
+ // that `tStart .. tEnd` is inside `target` from here on.
620
+ return false
449
621
  }
450
622
  this.visited = 0
451
623
  if (tStart === tEnd || toI32(target[tStart]) !== 47) {
452
- return ROUTER_NOT_FOUND
453
- }
454
- // One pass over the path: where it ends, and each segment's span up to one
455
- // past the deepest route, which is as far as any edge can reach.
456
- let end: i32 = tStart
457
- while (end < tEnd && toI32(target[end]) !== 63) {
458
- end += 1
459
- }
460
- const keep: i32 = this.deepest + 1
461
- let segments: i32 = 0
462
- let start: i32 = tStart + 1
463
- for (let i: i32 = tStart + 1; i <= end; i++) {
464
- if (i >= 0 && i < end && i < toI32(target.length) && toI32(target[i]) !== 47) {
465
- continue
466
- }
467
- if (segments < keep) {
468
- this.segStart[segments] = start
469
- this.segEnd[segments] = i
624
+ let end: i32 = tStart
625
+ while (end < tEnd && toI32(target[end]) !== 63) {
626
+ end += 1
470
627
  }
471
- segments += 1
472
- start = i + 1
628
+ this.pathEnd = end
629
+ return false
473
630
  }
474
- this.segments = segments
475
- this.pathEnd = end
476
- this.wanted = -1
477
631
  const methodLength: i32 = mEnd - mStart
478
- for (let m: i32 = 0; m < toI32(this.methodStart.length) && this.wanted < 0; m++) {
632
+ const key: u64 = routerMethodKey(method, mStart, methodLength)
633
+ if (key !== toU64(0)) {
634
+ for (let m: i32 = 0; m < toI32(this.methodKey.length); m++) {
635
+ if (this.methodKey[m] === key) {
636
+ this.wanted = m
637
+ return true
638
+ }
639
+ }
640
+ this.wanted = -1
641
+ return true
642
+ }
643
+ // A method too long for a key is compared byte by byte with each known one.
644
+ for (let m: i32 = 0; m < toI32(this.methodStart.length); m++) {
479
645
  const at: i32 = this.methodStart[m]
646
+ // Checked, the compare's ranges included: `methodBytes`, `methodStart`
647
+ // and `methodEnd` are public, so nothing here can rely on `add` being
648
+ // the only writer.
480
649
  if (
481
650
  this.methodEnd[m] - at === methodLength &&
482
651
  routerSameBytes(this.methodBytes, at, method, mStart, methodLength)
483
652
  ) {
484
653
  this.wanted = m
654
+ return true
655
+ }
656
+ }
657
+ this.wanted = -1
658
+ return true
659
+ }
660
+
661
+ /** The tree's answer for a target `enter` took and no scan has split: the split, then `answer`. */
662
+ private descend(target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
663
+ // `enter` took this range, so this is never true; it is here because a
664
+ // proof does not cross a call, and the reads below need this one.
665
+ if (tStart < 0 || tEnd > toI32(target.length)) {
666
+ return ROUTER_NOT_FOUND
667
+ }
668
+ // One pass over the path: where it ends, and each segment's span up to one
669
+ // past the deepest route, which is as far as any edge can reach.
670
+ const keep: i32 = this.deepest + 1
671
+ let segments: i32 = 0
672
+ let start: i32 = tStart + 1
673
+ let end: i32 = start
674
+ while (end < tEnd) {
675
+ const c: i32 = toI32(target[end])
676
+ if (c === 63) {
677
+ break
678
+ }
679
+ if (c === 47) {
680
+ if (segments < keep) {
681
+ this.segStart[segments] = start
682
+ this.segEnd[segments] = end
683
+ }
684
+ segments += 1
685
+ start = end + 1
485
686
  }
687
+ end += 1
688
+ }
689
+ if (segments < keep) {
690
+ this.segStart[segments] = start
691
+ this.segEnd[segments] = end
486
692
  }
693
+ this.segments = segments + 1
694
+ this.pathEnd = end
695
+ return this.answer(target, params)
696
+ }
697
+
698
+ /** The tree's answer for a path already split into `segStart`/`segEnd`: the walk and the pairs. */
699
+ private answer(target: u8[], params: i32[]): i32 {
487
700
  this.foundCount = 0
488
701
  this.otherMethod = false
489
702
  const route: i32 = this.walk(target, toI32(0), toI32(0))
@@ -500,6 +713,77 @@ export class Router {
500
713
  return route
501
714
  }
502
715
 
716
+ /**
717
+ * The static route of the wanted method whose path is `target` from `tStart`
718
+ * to its first `?` or `tEnd`, or -1 when the exact-path table holds none. On
719
+ * a route `pathEnd` is set; it has no pairs, so `params` is not needed.
720
+ * Either way the path is left split, as `descend` splits it, for `answer`;
721
+ * a range outside `target`, which `enter` never lets through, is
722
+ * `ROUTER_NOT_FOUND` before any split.
723
+ *
724
+ * `match` calls this only for a target no longer than the longest static
725
+ * route. One pass hashes and splits the path; then at most `ROUTER_PROBES` slots are
726
+ * read, and a slot's path is compared only when its method and length
727
+ * agree. The hash picks the slot and decides nothing else: a route is
728
+ * answered for its method and its bytes. The cost is bounded by the longest static route's length,
729
+ * whatever the target and however many routes the table holds.
730
+ */
731
+ private staticRoute(target: u8[], tStart: i32, tEnd: i32): i32 {
732
+ // Never true, as in `descend`: the guard is the proof the loop's reads
733
+ // need. It answers as `descend`'s does, so no split is left for `answer`.
734
+ if (tStart < 0 || tEnd > toI32(target.length)) {
735
+ return ROUTER_NOT_FOUND
736
+ }
737
+ // The first byte is the `/` `enter` found; each later one is hashed, and
738
+ // a `/` among them also closes a segment, as in `descend`.
739
+ let hash: u32 = routerHashWith(routerHashOf(this.wanted), 47)
740
+ const keep: i32 = this.deepest + 1
741
+ let segments: i32 = 0
742
+ let start: i32 = tStart + 1
743
+ let end: i32 = start
744
+ while (end < tEnd) {
745
+ const c: i32 = toI32(target[end])
746
+ if (c === 63) {
747
+ break
748
+ }
749
+ if (c === 47) {
750
+ if (segments < keep) {
751
+ this.segStart[segments] = start
752
+ this.segEnd[segments] = end
753
+ }
754
+ segments += 1
755
+ start = end + 1
756
+ }
757
+ hash = routerHashWith(hash, c)
758
+ end += 1
759
+ }
760
+ if (segments < keep) {
761
+ this.segStart[segments] = start
762
+ this.segEnd[segments] = end
763
+ }
764
+ this.segments = segments + 1
765
+ this.pathEnd = end
766
+ const length: i32 = end - tStart
767
+ const mask: i32 = toI32(this.staticSlots.length) - 1
768
+ let slot: i32 = routerSlot(hash, mask)
769
+ for (let probe: i32 = 0; probe < ROUTER_PROBES; probe++) {
770
+ const route: i32 = this.staticSlots[slot]
771
+ if (route < 0) {
772
+ return ROUTER_NONE
773
+ }
774
+ const at: i32 = this.routePathStart[route]
775
+ if (
776
+ this.routeMethod[route] === this.wanted &&
777
+ this.routePathEnd[route] - at === length &&
778
+ routerSameBytes(this.staticPaths, at, target, tStart, length)
779
+ ) {
780
+ return route
781
+ }
782
+ slot = (slot + 1) & mask
783
+ }
784
+ return ROUTER_NONE
785
+ }
786
+
503
787
  /**
504
788
  * The route below `node`, which has consumed `depth` segments, for the
505
789
  * wanted method, or -1. Literal, then parameter, then `*`: the first branch
@@ -507,7 +791,8 @@ export class Router {
507
791
  * pairs it pushed popped.
508
792
  */
509
793
  private walk(target: u8[], node: i32, depth: i32): i32 {
510
- this.visited += 1
794
+ // Never more than the nodes, so the count needs no overflow check.
795
+ this.visited = wrappingAdd(this.visited, toI32(1))
511
796
  if (depth === this.segments) {
512
797
  return this.endpointFor(node)
513
798
  }
@@ -539,7 +824,7 @@ export class Router {
539
824
  }
540
825
  const wild: i32 = this.wildChild[node]
541
826
  if (wild >= 0) {
542
- this.visited += 1
827
+ this.visited = wrappingAdd(this.visited, toI32(1))
543
828
  const route: i32 = this.endpointFor(wild)
544
829
  if (route >= 0) {
545
830
  const top: i32 = this.foundCount
@@ -616,6 +901,71 @@ export const routerDecode = (target: u8[], start: i32, end: i32, out: u8[]): i32
616
901
  return length
617
902
  }
618
903
 
904
+ /**
905
+ * Where the hash the exact-path table files a static route under begins: at
906
+ * the method's index, so that two methods of one path do not share a slot.
907
+ */
908
+ const routerHashOf = (method: i32): u32 => toU32(method + 1)
909
+
910
+ /**
911
+ * `bytes[at .. at + length)` packed into one word, its first byte lowest and
912
+ * its length in the top byte, so two methods of up to 7 bytes have the same
913
+ * key exactly when they have the same bytes. 0, which no packed method is,
914
+ * for a length outside 1 to 7 or a range outside `bytes`.
915
+ */
916
+ const routerMethodKey = (bytes: u8[], at: i32, length: i32): u64 => {
917
+ if (length < 1 || length > 7 || at < 0 || length > toI32(bytes.length) - at) {
918
+ return toU64(0)
919
+ }
920
+ // The test above puts `[at, at + length)` inside `bytes`, a range the
921
+ // prover cannot follow through `stop`, so each read skips its check and
922
+ // the cursor its overflow test: `stop` is at most `bytes.length`, an i32.
923
+ let key: u64 = toU64(length) << toU64(56)
924
+ let shift: u64 = toU64(0)
925
+ const stop: i32 = wrappingAdd(at, length)
926
+ for (let i: i32 = at; i < stop; i = wrappingAdd(i, toI32(1))) {
927
+ key = key | (toU64(uncheckedGet(bytes, i)) << shift)
928
+ shift = shift + toU64(8)
929
+ }
930
+ return key
931
+ }
932
+
933
+ /**
934
+ * `hash` with the path byte `byte` taken in, as FNV-1a takes one: an exclusive
935
+ * or and a product with its 32-bit prime. `u32` arithmetic wraps.
936
+ */
937
+ const routerHashWith = (hash: u32, byte: i32): u32 => {
938
+ const prime: u32 = 16777619
939
+ return (hash ^ toU32(byte)) * prime
940
+ }
941
+
942
+ /**
943
+ * The slot of a table of `mask + 1`, a power of two, that `hash` names: its
944
+ * product with 2^32 over the golden ratio, the high bits folded into the low
945
+ * ones the mask keeps.
946
+ */
947
+ const routerSlot = (hash: u32, mask: i32): i32 => {
948
+ const spread: u32 = 2654435769
949
+ const mixed: u32 = hash * spread
950
+ return toI32((mixed ^ (mixed >>> 15)) & toU32(mask))
951
+ }
952
+
953
+ /**
954
+ * Panics for a `what` range `start` to `end` outside its `length` bytes. Apart
955
+ * from `enter`, so that a lookup holds no string of its own.
956
+ */
957
+ const routerOutside = (what: string, start: i32, end: i32, length: i32): void => {
958
+ panic(`Router.match: the ${what} range ${start} to ${end} is outside its ${length} bytes`)
959
+ }
960
+
961
+ /**
962
+ * Panics for a `route` that is not one of the table's `routes`. Apart from
963
+ * `paramCount`, which a server calls per request, for the reason `routerOutside` is.
964
+ */
965
+ const routerNoRoute = (route: i32, routes: i32): void => {
966
+ panic(`Router.paramCount: route ${route} is not in a table of ${routes}`)
967
+ }
968
+
619
969
  /** Panics, naming `RouterLimits.<field>`, unless `value` is in `low .. high`. */
620
970
  const routerLimit = (field: string, value: i32, low: i32, high: i32): void => {
621
971
  if (value < low || value > high) {
@@ -698,14 +1048,33 @@ const routerLabelIsText = (bytes: u8[], at: i32, text: string, from: i32, length
698
1048
  return true
699
1049
  }
700
1050
 
701
- /** Whether `a[at .. at + length)` and `b[from .. from + length)` hold the same bytes. */
1051
+ /**
1052
+ * Whether `a[at .. at + length)` and `b[from .. from + length)` hold the same
1053
+ * bytes; a negative length, or a range outside its array, does not. The
1054
+ * ranges are checked once, here, and the bytes are then read without a check
1055
+ * per byte.
1056
+ */
702
1057
  const routerSameBytes = (a: u8[], at: i32, b: u8[], from: i32, length: i32): boolean => {
703
- for (let k: i32 = 0; k < length; k++) {
704
- if (a[at + k] !== b[from + k]) {
1058
+ if (at < 0 || from < 0 || length > toI32(a.length) - at || length > toI32(b.length) - from) {
1059
+ return false
1060
+ }
1061
+ // Two cursors, each stepped without an overflow check: `at` and `from` are
1062
+ // not negative and `length` is at most what is left of each array after
1063
+ // them, so neither passes its array's length, an i32. A negative `length`
1064
+ // puts `stop` below `at` (no overflow, `at` being at least 0), so the loop
1065
+ // never runs and the closing test answers false: a separate `length < 0`
1066
+ // test up front measured 5 to 47 Ir more per match.
1067
+ const stop: i32 = wrappingAdd(at, length)
1068
+ let j: i32 = from
1069
+ let i: i32 = at
1070
+ while (i < stop) {
1071
+ if (uncheckedGet(a, i) !== uncheckedGet(b, j)) {
705
1072
  return false
706
1073
  }
1074
+ i = wrappingAdd(i, toI32(1))
1075
+ j = wrappingAdd(j, toI32(1))
707
1076
  }
708
- return true
1077
+ return i === stop
709
1078
  }
710
1079
 
711
1080
  /** The value of the hex digit `c`, in either case, or -1. */