nish 0.44.0 → 0.46.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/README.md +1 -1
- package/docs/AI.md +18 -16
- package/docs/INSTALL.md +3 -1
- package/package.json +5 -5
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +84 -61
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +3 -4
- package/std/crypto/sha1.ts +107 -65
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1315 -222
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +50 -18
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +225 -124
- package/std/net/quic-listener.ts +197 -89
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +171 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/net/router.ts
CHANGED
|
@@ -62,6 +62,14 @@
|
|
|
62
62
|
* one range check; a range outside the array packs to 0, which no method is.
|
|
63
63
|
* Every other read is checked.
|
|
64
64
|
*
|
|
65
|
+
* **Steps run before and after route bodies.** `step` names a step, 0 for the
|
|
66
|
+
* first, and `use` binds it to a route — or to the requests no route answers,
|
|
67
|
+
* `ROUTER_NOT_FOUND` and `ROUTER_METHOD_NOT_ALLOWED` — while the table is
|
|
68
|
+
* built; `steps` answers a route's set as one bit per step, which
|
|
69
|
+
* `nish/net/handler` and `nish/net/http-server` read to hand the program each
|
|
70
|
+
* step's id (`docs/wp43-http-app-layer.md` §4, stage 5). A route bound to no
|
|
71
|
+
* step answers 0, and nothing else is looked up for it.
|
|
72
|
+
*
|
|
65
73
|
* Private helpers share the importing program's flat symbol namespace
|
|
66
74
|
* (`docs/wp26-stdlib.md` §3e), which is why each one carries the module's name.
|
|
67
75
|
*/
|
|
@@ -90,6 +98,9 @@ export const ROUTER_BAD_ESCAPE: i32 = -10
|
|
|
90
98
|
/** `routerDecode`: the decoded bytes do not fit `out`; nothing was written. */
|
|
91
99
|
export const ROUTER_NO_ROOM: i32 = -11
|
|
92
100
|
|
|
101
|
+
/** The most steps one table names: one bit each of an `i32`, its sign bit aside. */
|
|
102
|
+
export const ROUTER_MAX_STEPS: i32 = 31
|
|
103
|
+
|
|
93
104
|
/** The longest method `add` takes, as `nish/net/http1-codec`'s `HTTP1_MAX_METHOD`. */
|
|
94
105
|
const ROUTER_MAX_METHOD: i32 = 32
|
|
95
106
|
/** No node or route: an `i32` spelled once, so that `--number-mode f64` does not read a bare `-1` as a double. */
|
|
@@ -148,6 +159,8 @@ export class Router {
|
|
|
148
159
|
private routeMethod: i32[]
|
|
149
160
|
private endpointNext: i32[]
|
|
150
161
|
private routeParams: i32[]
|
|
162
|
+
/** Per route: the steps `use` bound to it, bit `s` for step `s`. */
|
|
163
|
+
private routeSteps: i32[]
|
|
151
164
|
/**
|
|
152
165
|
* The exact-path table: per slot a static route, or -1. Its length is a
|
|
153
166
|
* power of two, at least four slots for each static route, and a route sits
|
|
@@ -199,6 +212,12 @@ export class Router {
|
|
|
199
212
|
maxParams: i32 = 0
|
|
200
213
|
private maxRoutes: i32 = 0
|
|
201
214
|
private maxMethods: i32 = 0
|
|
215
|
+
/** The steps bound to `ROUTER_NOT_FOUND`: what `match` answers with any negative code but `ROUTER_METHOD_NOT_ALLOWED` runs them. */
|
|
216
|
+
private missSteps: i32 = 0
|
|
217
|
+
/** The steps bound to the requests `match` answers `ROUTER_METHOD_NOT_ALLOWED`. */
|
|
218
|
+
private refusedSteps: i32 = 0
|
|
219
|
+
/** How many steps `step` has named; the next answers this id. */
|
|
220
|
+
private stepCount: i32 = 0
|
|
202
221
|
/** How many routes have no parameter and no `*`, and how many of them `staticSlots` holds. */
|
|
203
222
|
private statics: i32 = 0
|
|
204
223
|
private staticHeld: i32 = 0
|
|
@@ -246,6 +265,7 @@ export class Router {
|
|
|
246
265
|
this.routeMethod = []
|
|
247
266
|
this.endpointNext = []
|
|
248
267
|
this.routeParams = []
|
|
268
|
+
this.routeSteps = []
|
|
249
269
|
this.staticSlots = [-1, -1, -1, -1, -1, -1, -1, -1]
|
|
250
270
|
this.routeHash = []
|
|
251
271
|
this.routePathStart = []
|
|
@@ -255,12 +275,24 @@ export class Router {
|
|
|
255
275
|
this.methodStart = []
|
|
256
276
|
this.methodEnd = []
|
|
257
277
|
this.methodKey = []
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
278
|
+
// routerLimit holds both to at most 256; the clamps restate that where the
|
|
279
|
+
// prover reads it, so each length fits a `u16` and its sum cannot overflow.
|
|
280
|
+
let maxSegments: i32 = limits.maxSegments
|
|
281
|
+
if (maxSegments > 256) {
|
|
282
|
+
maxSegments = 256
|
|
283
|
+
}
|
|
284
|
+
let maxParams: i32 = limits.maxParams
|
|
285
|
+
if (maxParams < 0 || maxParams > 256) {
|
|
286
|
+
maxParams = 0
|
|
287
|
+
}
|
|
288
|
+
const keep: u16 = toU16(maxSegments + 1)
|
|
289
|
+
const segments: u16 = toU16(maxSegments)
|
|
290
|
+
this.segStart = new Array<i32>(keep)
|
|
291
|
+
this.segEnd = new Array<i32>(keep)
|
|
292
|
+
this.found = new Array<i32>(toU16(maxParams + maxParams))
|
|
293
|
+
this.patKind = new Array<i32>(segments)
|
|
294
|
+
this.patStart = new Array<i32>(segments)
|
|
295
|
+
this.patEnd = new Array<i32>(segments)
|
|
264
296
|
}
|
|
265
297
|
|
|
266
298
|
/** How many routes the table holds; the next `add` answers this index. */
|
|
@@ -282,10 +314,12 @@ export class Router {
|
|
|
282
314
|
* @returns how many pairs `match` writes to `params` for that route, so half the slots it needs
|
|
283
315
|
*/
|
|
284
316
|
paramCount(route: i32): i32 {
|
|
285
|
-
if (route
|
|
286
|
-
|
|
317
|
+
if (route >= 0 && route < this.routes() && route < toI32(this.routeParams.length)) {
|
|
318
|
+
return this.routeParams[route]
|
|
287
319
|
}
|
|
288
|
-
|
|
320
|
+
routerNoRoute(route, this.routes())
|
|
321
|
+
// Not reached: the helper panics.
|
|
322
|
+
return 0
|
|
289
323
|
}
|
|
290
324
|
|
|
291
325
|
/**
|
|
@@ -306,6 +340,59 @@ export class Router {
|
|
|
306
340
|
return this.visited
|
|
307
341
|
}
|
|
308
342
|
|
|
343
|
+
/**
|
|
344
|
+
* Names a new step and answers its id, counting from 0, or `ROUTER_FULL`
|
|
345
|
+
* once the table names `ROUTER_MAX_STEPS`. A step runs for the routes
|
|
346
|
+
* `use` binds it to, in the order of the ids.
|
|
347
|
+
* @returns the new step's id, or `ROUTER_FULL`
|
|
348
|
+
*/
|
|
349
|
+
step(): i32 {
|
|
350
|
+
const id: i32 = this.stepCount
|
|
351
|
+
if (id >= ROUTER_MAX_STEPS) {
|
|
352
|
+
return ROUTER_FULL
|
|
353
|
+
}
|
|
354
|
+
this.stepCount = id + 1
|
|
355
|
+
return id
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Binds step `step` to `route`: every request `match` answers that route
|
|
360
|
+
* runs it. `ROUTER_NOT_FOUND` binds it to the requests no route matches, and
|
|
361
|
+
* `ROUTER_METHOD_NOT_ALLOWED` to those whose path only other methods'
|
|
362
|
+
* routes match. Binding a step twice binds it once. Panics on a route or a
|
|
363
|
+
* step the table does not hold.
|
|
364
|
+
* @param route a route index `add` answered, `ROUTER_NOT_FOUND` or `ROUTER_METHOD_NOT_ALLOWED`
|
|
365
|
+
* @param step a step id `step` answered
|
|
366
|
+
*/
|
|
367
|
+
use(route: i32, step: i32): void {
|
|
368
|
+
if (step < 0 || step >= this.stepCount) {
|
|
369
|
+
panic(`Router.use: step ${step} is not one of the ${this.stepCount} the table names`)
|
|
370
|
+
}
|
|
371
|
+
const bit: i32 = 1 << step
|
|
372
|
+
if (route === ROUTER_NOT_FOUND) {
|
|
373
|
+
this.missSteps = this.missSteps | bit
|
|
374
|
+
} else if (route === ROUTER_METHOD_NOT_ALLOWED) {
|
|
375
|
+
this.refusedSteps = this.refusedSteps | bit
|
|
376
|
+
} else {
|
|
377
|
+
if (route < 0 || route >= this.routes() || route >= toI32(this.routeSteps.length)) {
|
|
378
|
+
panic(`Router.use: route ${route} is not in a table of ${this.routes()}`)
|
|
379
|
+
}
|
|
380
|
+
this.routeSteps[route] = this.routeSteps[route] | bit
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* The steps bound to what `match` answered: bit `s` is set for step `s`.
|
|
386
|
+
* @param route what `match` answered: a route index, or a negative `ROUTER_*` code
|
|
387
|
+
* @returns the route's steps; for `ROUTER_METHOD_NOT_ALLOWED` those bound to it, for any other negative code those bound to `ROUTER_NOT_FOUND`, and 0 for a route bound to none
|
|
388
|
+
*/
|
|
389
|
+
steps(route: i32): i32 {
|
|
390
|
+
if (route >= 0) {
|
|
391
|
+
return route < toI32(this.routeSteps.length) ? this.routeSteps[route] : 0
|
|
392
|
+
}
|
|
393
|
+
return route === ROUTER_METHOD_NOT_ALLOWED ? this.refusedSteps : this.missSteps
|
|
394
|
+
}
|
|
395
|
+
|
|
309
396
|
/**
|
|
310
397
|
* Adds the route `method pattern` and answers its index, counting from 0, or
|
|
311
398
|
* a negative `ROUTER_*` code; a refused pattern adds nothing. Whether a
|
|
@@ -328,7 +415,7 @@ export class Router {
|
|
|
328
415
|
if (length > this.maxPatternBytes) {
|
|
329
416
|
return ROUTER_TOO_LONG
|
|
330
417
|
}
|
|
331
|
-
if (length
|
|
418
|
+
if (toI32(pattern.length) < 1 || toI32(pattern.charCodeAt(0)) !== 47) {
|
|
332
419
|
return ROUTER_BAD_PATTERN
|
|
333
420
|
}
|
|
334
421
|
const count: i32 = this.splitPattern(pattern)
|
|
@@ -336,9 +423,14 @@ export class Router {
|
|
|
336
423
|
return count
|
|
337
424
|
}
|
|
338
425
|
let params: i32 = 0
|
|
339
|
-
for (let k: i32 = 0; k < count; k++) {
|
|
426
|
+
for (let k: i32 = 0; k < count && k < toI32(this.patKind.length); k++) {
|
|
340
427
|
if (this.patKind[k] !== ROUTER_LITERAL) {
|
|
341
|
-
params
|
|
428
|
+
// Never true, `params` counting at most one per segment; a proof
|
|
429
|
+
// does not follow a loop, and the count needs this bound.
|
|
430
|
+
if (params < 0 || params > 1073741822) {
|
|
431
|
+
return ROUTER_TOO_LONG
|
|
432
|
+
}
|
|
433
|
+
params = params + 1
|
|
342
434
|
}
|
|
343
435
|
}
|
|
344
436
|
if (params > this.maxParams) {
|
|
@@ -353,36 +445,54 @@ export class Router {
|
|
|
353
445
|
depth += 1
|
|
354
446
|
}
|
|
355
447
|
const known: i32 = this.methodIndex(method)
|
|
356
|
-
if (node >= 0 && known >= 0) {
|
|
357
|
-
|
|
448
|
+
if (node >= 0 && node < toI32(this.firstEndpoint.length) && known >= 0) {
|
|
449
|
+
let e: i32 = this.firstEndpoint[node]
|
|
450
|
+
while (e >= 0 && e < toI32(this.endpointNext.length) && e < toI32(this.routeMethod.length)) {
|
|
358
451
|
if (this.routeMethod[e] === known) {
|
|
359
452
|
return ROUTER_DUPLICATE
|
|
360
453
|
}
|
|
454
|
+
e = this.endpointNext[e]
|
|
361
455
|
}
|
|
362
456
|
}
|
|
363
457
|
if (this.routes() >= this.maxRoutes || (known < 0 && toI32(this.methodStart.length) >= this.maxMethods)) {
|
|
364
458
|
return ROUTER_FULL
|
|
365
459
|
}
|
|
460
|
+
// The statics are among the routes, which the constructor caps at 1048576
|
|
461
|
+
// and the test above keeps below `maxRoutes`, so this is never true; it is
|
|
462
|
+
// the bound the count below needs, read before anything is added.
|
|
463
|
+
const statics: i32 = this.statics
|
|
464
|
+
if (statics < 0 || statics > 1048575) {
|
|
465
|
+
return ROUTER_FULL
|
|
466
|
+
}
|
|
366
467
|
let methodId: i32 = known
|
|
367
468
|
if (methodId < 0) {
|
|
368
469
|
methodId = toI32(this.methodStart.length)
|
|
369
|
-
|
|
470
|
+
const methodAt: i32 = toI32(this.methodBytes.length)
|
|
471
|
+
this.methodStart.push(methodAt)
|
|
370
472
|
for (let k: i32 = 0; k < methodLength; k++) {
|
|
371
473
|
this.methodBytes.push(toU8(toI32(method.charCodeAt(k))))
|
|
372
474
|
}
|
|
373
475
|
this.methodEnd.push(toI32(this.methodBytes.length))
|
|
374
|
-
this.methodKey.push(routerMethodKey(this.methodBytes,
|
|
476
|
+
this.methodKey.push(routerMethodKey(this.methodBytes, methodAt, methodLength))
|
|
375
477
|
}
|
|
376
478
|
node = 0
|
|
377
479
|
for (let k: i32 = 0; k < count; k++) {
|
|
378
480
|
const next: i32 = this.childFor(node, pattern, k)
|
|
379
481
|
node = next >= 0 ? next : this.addChild(node, pattern, k)
|
|
380
482
|
}
|
|
483
|
+
// Never true: `childFor` and `addChild` answer a node the tree holds. The
|
|
484
|
+
// test is what proves the endpoint reads below.
|
|
485
|
+
if (node < 0 || node >= toI32(this.firstEndpoint.length)) {
|
|
486
|
+
return ROUTER_FULL
|
|
487
|
+
}
|
|
381
488
|
const route: i32 = this.routes()
|
|
489
|
+
// The new route heads its node's endpoints; its next is the old head.
|
|
490
|
+
const head: i32 = this.firstEndpoint[node]
|
|
491
|
+
this.firstEndpoint[node] = route
|
|
382
492
|
this.routeMethod.push(methodId)
|
|
383
493
|
this.routeParams.push(params)
|
|
384
|
-
this.
|
|
385
|
-
this.
|
|
494
|
+
this.routeSteps.push(toI32(0))
|
|
495
|
+
this.endpointNext.push(head)
|
|
386
496
|
if (count > this.deepest) {
|
|
387
497
|
this.deepest = count
|
|
388
498
|
}
|
|
@@ -398,11 +508,12 @@ export class Router {
|
|
|
398
508
|
this.routePathEnd.push(toI32(this.staticPaths.length))
|
|
399
509
|
this.routeHash.push(hash)
|
|
400
510
|
if (params === 0) {
|
|
401
|
-
|
|
511
|
+
const held: i32 = statics + 1
|
|
512
|
+
this.statics = held
|
|
402
513
|
if (length > this.staticLongest) {
|
|
403
514
|
this.staticLongest = length
|
|
404
515
|
}
|
|
405
|
-
if (4 *
|
|
516
|
+
if (4 * held > toI32(this.staticSlots.length)) {
|
|
406
517
|
this.growStatic()
|
|
407
518
|
} else {
|
|
408
519
|
this.fileStatic(route)
|
|
@@ -413,13 +524,21 @@ export class Router {
|
|
|
413
524
|
|
|
414
525
|
/** Doubles the exact-path table and files every static route again, in the order they were added. */
|
|
415
526
|
private growStatic(): void {
|
|
416
|
-
const
|
|
417
|
-
|
|
527
|
+
const now: i32 = toI32(this.staticSlots.length)
|
|
528
|
+
// The table grows at a quarter full and holds at most maxRoutes (2^20), so
|
|
529
|
+
// it never nears 2^30; past it, it stays as it is rather than overflow.
|
|
530
|
+
if (now > 1073741823) {
|
|
531
|
+
return
|
|
532
|
+
}
|
|
533
|
+
const slots: i32 = 2 * now
|
|
534
|
+
// Built by pushes: the length can pass what a `u16` holds.
|
|
535
|
+
const table: i32[] = []
|
|
418
536
|
for (let k: i32 = 0; k < slots; k++) {
|
|
419
|
-
|
|
537
|
+
table.push(ROUTER_NONE)
|
|
420
538
|
}
|
|
539
|
+
this.staticSlots = table
|
|
421
540
|
this.staticHeld = 0
|
|
422
|
-
for (let r: i32 = 0; r < this.routes(); r++) {
|
|
541
|
+
for (let r: i32 = 0; r < this.routes() && r < toI32(this.routeParams.length); r++) {
|
|
423
542
|
if (this.routeParams[r] === 0) {
|
|
424
543
|
this.fileStatic(r)
|
|
425
544
|
}
|
|
@@ -432,12 +551,26 @@ export class Router {
|
|
|
432
551
|
* holds every route, so one the table lacks is still answered.
|
|
433
552
|
*/
|
|
434
553
|
private fileStatic(route: i32): void {
|
|
554
|
+
if (route < 0 || route >= toI32(this.routeHash.length)) {
|
|
555
|
+
return
|
|
556
|
+
}
|
|
435
557
|
const mask: i32 = toI32(this.staticSlots.length) - 1
|
|
436
558
|
let slot: i32 = routerSlot(this.routeHash[route], mask)
|
|
437
559
|
for (let probe: i32 = 0; probe < ROUTER_PROBES; probe++) {
|
|
560
|
+
// Never true, the mask keeping the slot inside the table; the test is the
|
|
561
|
+
// proof the reads need.
|
|
562
|
+
if (slot < 0 || slot >= toI32(this.staticSlots.length)) {
|
|
563
|
+
return
|
|
564
|
+
}
|
|
438
565
|
if (this.staticSlots[slot] < 0) {
|
|
566
|
+
// Never true: a free slot means fewer are held than the table has.
|
|
567
|
+
// The test bounds the count below, and on it the route goes unfiled.
|
|
568
|
+
const held: i32 = this.staticHeld
|
|
569
|
+
if (held < 0 || held >= toI32(this.staticSlots.length)) {
|
|
570
|
+
return
|
|
571
|
+
}
|
|
439
572
|
this.staticSlots[slot] = route
|
|
440
|
-
this.staticHeld
|
|
573
|
+
this.staticHeld = held + 1
|
|
441
574
|
return
|
|
442
575
|
}
|
|
443
576
|
slot = (slot + 1) & mask
|
|
@@ -450,6 +583,11 @@ export class Router {
|
|
|
450
583
|
*/
|
|
451
584
|
private splitPattern(pattern: string): i32 {
|
|
452
585
|
const length: i32 = toI32(pattern.length)
|
|
586
|
+
// `add` refuses a pattern past `maxPatternBytes`, at most 65536, before
|
|
587
|
+
// this; the bound is what keeps the cursor below from overflowing.
|
|
588
|
+
if (length > 65536) {
|
|
589
|
+
return ROUTER_TOO_LONG
|
|
590
|
+
}
|
|
453
591
|
let count: i32 = 0
|
|
454
592
|
let start: i32 = 1
|
|
455
593
|
for (let i: i32 = 1; i <= length; i++) {
|
|
@@ -460,7 +598,14 @@ export class Router {
|
|
|
460
598
|
if (c !== 47) {
|
|
461
599
|
continue
|
|
462
600
|
}
|
|
463
|
-
|
|
601
|
+
// The segment arrays are `maxSegments` long, so the last three tests
|
|
602
|
+
// never hold before the first does; they are what proves the writes.
|
|
603
|
+
if (
|
|
604
|
+
count >= this.maxSegments ||
|
|
605
|
+
count >= toI32(this.patKind.length) ||
|
|
606
|
+
count >= toI32(this.patStart.length) ||
|
|
607
|
+
count >= toI32(this.patEnd.length)
|
|
608
|
+
) {
|
|
464
609
|
return ROUTER_TOO_LONG
|
|
465
610
|
}
|
|
466
611
|
if (count > 0 && this.patKind[count - 1] === ROUTER_WILD) {
|
|
@@ -471,13 +616,18 @@ export class Router {
|
|
|
471
616
|
return kind
|
|
472
617
|
}
|
|
473
618
|
this.patKind[count] = kind
|
|
474
|
-
|
|
619
|
+
// Never true, `start` lying inside the pattern; the count needs the bound.
|
|
620
|
+
if (start < 0 || start > 1073741822) {
|
|
621
|
+
return ROUTER_BAD_PATTERN
|
|
622
|
+
}
|
|
623
|
+
const name: i32 = start + 1
|
|
624
|
+
this.patStart[count] = kind === ROUTER_PARAM ? name : start
|
|
475
625
|
this.patEnd[count] = i
|
|
476
626
|
if (kind === ROUTER_PARAM) {
|
|
477
627
|
for (let k: i32 = 0; k < count; k++) {
|
|
478
628
|
if (
|
|
479
629
|
this.patKind[k] === ROUTER_PARAM &&
|
|
480
|
-
routerSameName(pattern, this.patStart[k], this.patEnd[k],
|
|
630
|
+
routerSameName(pattern, this.patStart[k], this.patEnd[k], name, i)
|
|
481
631
|
) {
|
|
482
632
|
return ROUTER_BAD_PATTERN
|
|
483
633
|
}
|
|
@@ -491,6 +641,19 @@ export class Router {
|
|
|
491
641
|
|
|
492
642
|
/** The child of `node` that pattern segment `k` leads to, or -1. */
|
|
493
643
|
private childFor(node: i32, pattern: string, k: i32): i32 {
|
|
644
|
+
// Never true for what `add` passes; the tests are the proof the reads need.
|
|
645
|
+
if (
|
|
646
|
+
k < 0 ||
|
|
647
|
+
k >= toI32(this.patKind.length) ||
|
|
648
|
+
k >= toI32(this.patStart.length) ||
|
|
649
|
+
k >= toI32(this.patEnd.length) ||
|
|
650
|
+
node < 0 ||
|
|
651
|
+
node >= toI32(this.firstChild.length) ||
|
|
652
|
+
node >= toI32(this.paramChild.length) ||
|
|
653
|
+
node >= toI32(this.wildChild.length)
|
|
654
|
+
) {
|
|
655
|
+
return -1
|
|
656
|
+
}
|
|
494
657
|
const kind: i32 = this.patKind[k]
|
|
495
658
|
if (kind === ROUTER_PARAM) {
|
|
496
659
|
return this.paramChild[node]
|
|
@@ -499,38 +662,77 @@ export class Router {
|
|
|
499
662
|
return this.wildChild[node]
|
|
500
663
|
}
|
|
501
664
|
const start: i32 = this.patStart[k]
|
|
502
|
-
const
|
|
503
|
-
|
|
665
|
+
const end: i32 = this.patEnd[k]
|
|
666
|
+
// `splitPattern` writes ranges inside the pattern, so this never holds.
|
|
667
|
+
if (start < 0 || end < 0) {
|
|
668
|
+
return -1
|
|
669
|
+
}
|
|
670
|
+
const length: i32 = end - start
|
|
671
|
+
let c: i32 = this.firstChild[node]
|
|
672
|
+
while (
|
|
673
|
+
c >= 0 &&
|
|
674
|
+
c < toI32(this.nextSibling.length) &&
|
|
675
|
+
c < toI32(this.labelStart.length) &&
|
|
676
|
+
c < toI32(this.labelEnd.length)
|
|
677
|
+
) {
|
|
504
678
|
const at: i32 = this.labelStart[c]
|
|
505
|
-
|
|
679
|
+
const last: i32 = this.labelEnd[c]
|
|
680
|
+
if (
|
|
681
|
+
at >= 0 &&
|
|
682
|
+
last >= 0 &&
|
|
683
|
+
last - at === length &&
|
|
684
|
+
routerLabelIsText(this.labels, at, pattern, start, length)
|
|
685
|
+
) {
|
|
506
686
|
return c
|
|
507
687
|
}
|
|
688
|
+
c = this.nextSibling[c]
|
|
508
689
|
}
|
|
509
690
|
return -1
|
|
510
691
|
}
|
|
511
692
|
|
|
512
693
|
/** Adds the child of `node` that pattern segment `k` leads to, and answers it. */
|
|
513
694
|
private addChild(node: i32, pattern: string, k: i32): i32 {
|
|
695
|
+
// Never true for what `add` passes, as in `childFor`; -1 adds nothing.
|
|
696
|
+
if (
|
|
697
|
+
k < 0 ||
|
|
698
|
+
k >= toI32(this.patKind.length) ||
|
|
699
|
+
k >= toI32(this.patStart.length) ||
|
|
700
|
+
k >= toI32(this.patEnd.length) ||
|
|
701
|
+
node < 0 ||
|
|
702
|
+
node >= toI32(this.firstChild.length) ||
|
|
703
|
+
node >= toI32(this.paramChild.length) ||
|
|
704
|
+
node >= toI32(this.wildChild.length)
|
|
705
|
+
) {
|
|
706
|
+
return -1
|
|
707
|
+
}
|
|
514
708
|
const child: i32 = this.nodeCount()
|
|
515
709
|
const kind: i32 = this.patKind[k]
|
|
710
|
+
const from: i32 = this.patStart[k]
|
|
711
|
+
const end: i32 = this.patEnd[k]
|
|
516
712
|
const at: i32 = toI32(this.labels.length)
|
|
713
|
+
// The parent's link is written first, while `node` is known to be inside
|
|
714
|
+
// its arrays; a literal child goes first among its siblings, its next the
|
|
715
|
+
// old first.
|
|
716
|
+
let sibling: i32 = ROUTER_NONE
|
|
717
|
+
if (kind === ROUTER_LITERAL) {
|
|
718
|
+
sibling = this.firstChild[node]
|
|
719
|
+
this.firstChild[node] = child
|
|
720
|
+
} else if (kind === ROUTER_PARAM) {
|
|
721
|
+
this.paramChild[node] = child
|
|
722
|
+
} else {
|
|
723
|
+
this.wildChild[node] = child
|
|
724
|
+
}
|
|
517
725
|
this.firstChild.push(ROUTER_NONE)
|
|
518
|
-
this.nextSibling.push(
|
|
726
|
+
this.nextSibling.push(sibling)
|
|
519
727
|
this.paramChild.push(ROUTER_NONE)
|
|
520
728
|
this.wildChild.push(ROUTER_NONE)
|
|
521
729
|
this.firstEndpoint.push(ROUTER_NONE)
|
|
522
730
|
this.labelStart.push(at)
|
|
523
731
|
if (kind === ROUTER_LITERAL) {
|
|
524
732
|
const n: i32 = toI32(pattern.length)
|
|
525
|
-
for (let i: i32 =
|
|
733
|
+
for (let i: i32 = from; i >= 0 && i < end && i < n; i++) {
|
|
526
734
|
this.labels.push(toU8(toI32(pattern.charCodeAt(i))))
|
|
527
735
|
}
|
|
528
|
-
this.nextSibling[child] = this.firstChild[node]
|
|
529
|
-
this.firstChild[node] = child
|
|
530
|
-
} else if (kind === ROUTER_PARAM) {
|
|
531
|
-
this.paramChild[node] = child
|
|
532
|
-
} else {
|
|
533
|
-
this.wildChild[node] = child
|
|
534
736
|
}
|
|
535
737
|
this.labelEnd.push(toI32(this.labels.length))
|
|
536
738
|
return child
|
|
@@ -542,9 +744,15 @@ export class Router {
|
|
|
542
744
|
*/
|
|
543
745
|
methodIndex(method: string): i32 {
|
|
544
746
|
const length: i32 = toI32(method.length)
|
|
545
|
-
for (let m: i32 = 0; m < toI32(this.methodStart.length); m++) {
|
|
747
|
+
for (let m: i32 = 0; m < toI32(this.methodStart.length) && m < toI32(this.methodEnd.length); m++) {
|
|
546
748
|
const at: i32 = this.methodStart[m]
|
|
547
|
-
|
|
749
|
+
const last: i32 = this.methodEnd[m]
|
|
750
|
+
if (
|
|
751
|
+
at >= 0 &&
|
|
752
|
+
last >= 0 &&
|
|
753
|
+
last - at === length &&
|
|
754
|
+
routerLabelIsText(this.methodBytes, at, method, 0, length)
|
|
755
|
+
) {
|
|
548
756
|
return m
|
|
549
757
|
}
|
|
550
758
|
}
|
|
@@ -572,6 +780,11 @@ export class Router {
|
|
|
572
780
|
}
|
|
573
781
|
// A static route's path is never longer than the longest one: a target
|
|
574
782
|
// 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
|
|
784
|
+
// does not cross a call.
|
|
785
|
+
if (tStart < 0 || tEnd < 0) {
|
|
786
|
+
return ROUTER_NOT_FOUND
|
|
787
|
+
}
|
|
575
788
|
if (tEnd - tStart <= this.staticLongest) {
|
|
576
789
|
const route: i32 = this.staticRoute(target, tStart, tEnd)
|
|
577
790
|
if (route !== ROUTER_NONE) {
|
|
@@ -610,8 +823,10 @@ export class Router {
|
|
|
610
823
|
* that is not `/`-rooted, which is `ROUTER_NOT_FOUND` with `pathEnd` set.
|
|
611
824
|
*/
|
|
612
825
|
private enter(method: u8[], mStart: i32, mEnd: i32, target: u8[], tStart: i32, tEnd: i32): boolean {
|
|
613
|
-
if (mStart < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
|
|
826
|
+
if (mStart < 0 || mEnd < 0 || mEnd < mStart || mEnd > toI32(method.length)) {
|
|
614
827
|
routerOutside("method", mStart, mEnd, toI32(method.length))
|
|
828
|
+
// Not reached, as below.
|
|
829
|
+
return false
|
|
615
830
|
}
|
|
616
831
|
if (tStart < 0 || tEnd < tStart || tEnd > toI32(target.length)) {
|
|
617
832
|
routerOutside("target", tStart, tEnd, toI32(target.length))
|
|
@@ -620,9 +835,9 @@ export class Router {
|
|
|
620
835
|
return false
|
|
621
836
|
}
|
|
622
837
|
this.visited = 0
|
|
623
|
-
if (tStart === tEnd || toI32(target[tStart]) !== 47) {
|
|
838
|
+
if (tStart === tEnd || tStart >= toI32(target.length) || toI32(target[tStart]) !== 47) {
|
|
624
839
|
let end: i32 = tStart
|
|
625
|
-
while (end < tEnd && toI32(target[end]) !== 63) {
|
|
840
|
+
while (end < tEnd && end < toI32(target.length) && toI32(target[end]) !== 63) {
|
|
626
841
|
end += 1
|
|
627
842
|
}
|
|
628
843
|
this.pathEnd = end
|
|
@@ -641,13 +856,16 @@ export class Router {
|
|
|
641
856
|
return true
|
|
642
857
|
}
|
|
643
858
|
// 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++) {
|
|
859
|
+
for (let m: i32 = 0; m < toI32(this.methodStart.length) && m < toI32(this.methodEnd.length); m++) {
|
|
645
860
|
const at: i32 = this.methodStart[m]
|
|
646
861
|
// Checked, the compare's ranges included: `methodBytes`, `methodStart`
|
|
647
862
|
// and `methodEnd` are public, so nothing here can rely on `add` being
|
|
648
863
|
// the only writer.
|
|
864
|
+
const last: i32 = this.methodEnd[m]
|
|
649
865
|
if (
|
|
650
|
-
|
|
866
|
+
at >= 0 &&
|
|
867
|
+
last >= 0 &&
|
|
868
|
+
last - at === methodLength &&
|
|
651
869
|
routerSameBytes(this.methodBytes, at, method, mStart, methodLength)
|
|
652
870
|
) {
|
|
653
871
|
this.wanted = m
|
|
@@ -662,12 +880,18 @@ export class Router {
|
|
|
662
880
|
private descend(target: u8[], tStart: i32, tEnd: i32, params: i32[]): i32 {
|
|
663
881
|
// `enter` took this range, so this is never true; it is here because a
|
|
664
882
|
// proof does not cross a call, and the reads below need this one.
|
|
665
|
-
if (tStart < 0 || tEnd > toI32(target.length)) {
|
|
883
|
+
if (tStart < 0 || tStart >= tEnd || tEnd > toI32(target.length)) {
|
|
666
884
|
return ROUTER_NOT_FOUND
|
|
667
885
|
}
|
|
668
886
|
// One pass over the path: where it ends, and each segment's span up to one
|
|
669
887
|
// past the deepest route, which is as far as any edge can reach.
|
|
670
|
-
|
|
888
|
+
// `deepest` counts a route's segments, which the constructor caps at 512,
|
|
889
|
+
// so this is never true; it is the bound the count below needs.
|
|
890
|
+
const deepest: i32 = this.deepest
|
|
891
|
+
if (deepest < 0 || deepest > 1073741822) {
|
|
892
|
+
return ROUTER_NOT_FOUND
|
|
893
|
+
}
|
|
894
|
+
const keep: i32 = deepest + 1
|
|
671
895
|
let segments: i32 = 0
|
|
672
896
|
let start: i32 = tStart + 1
|
|
673
897
|
let end: i32 = start
|
|
@@ -677,24 +901,40 @@ export class Router {
|
|
|
677
901
|
break
|
|
678
902
|
}
|
|
679
903
|
if (c === 47) {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
904
|
+
this.span(segments, keep, start, end)
|
|
905
|
+
// Never true, a path holding fewer slashes than bytes; the count
|
|
906
|
+
// needs the bound, and a proof does not follow a loop.
|
|
907
|
+
if (segments < 0 || segments > 1073741821) {
|
|
908
|
+
return ROUTER_NOT_FOUND
|
|
683
909
|
}
|
|
684
|
-
segments
|
|
910
|
+
segments = segments + 1
|
|
685
911
|
start = end + 1
|
|
686
912
|
}
|
|
687
913
|
end += 1
|
|
688
914
|
}
|
|
689
|
-
if (segments <
|
|
690
|
-
|
|
691
|
-
this.segEnd[segments] = end
|
|
915
|
+
if (segments < 0 || segments > 1073741822) {
|
|
916
|
+
return ROUTER_NOT_FOUND
|
|
692
917
|
}
|
|
918
|
+
this.span(segments, keep, start, end)
|
|
693
919
|
this.segments = segments + 1
|
|
694
920
|
this.pathEnd = end
|
|
695
921
|
return this.answer(target, params)
|
|
696
922
|
}
|
|
697
923
|
|
|
924
|
+
/**
|
|
925
|
+
* Records `[start, end)` as segment `k`'s span when `k` is below `keep`, one
|
|
926
|
+
* past the deepest route: no edge reads a span deeper than that.
|
|
927
|
+
*/
|
|
928
|
+
private span(k: i32, keep: i32, start: i32, end: i32): void {
|
|
929
|
+
// `keep` is at most `maxSegments + 1`, the arrays' length, so the last
|
|
930
|
+
// two tests never decide; they are what proves the writes.
|
|
931
|
+
if (k < 0 || k >= keep || k >= toI32(this.segStart.length) || k >= toI32(this.segEnd.length)) {
|
|
932
|
+
return
|
|
933
|
+
}
|
|
934
|
+
this.segStart[k] = start
|
|
935
|
+
this.segEnd[k] = end
|
|
936
|
+
}
|
|
937
|
+
|
|
698
938
|
/** The tree's answer for a path already split into `segStart`/`segEnd`: the walk and the pairs. */
|
|
699
939
|
private answer(target: u8[], params: i32[]): i32 {
|
|
700
940
|
this.foundCount = 0
|
|
@@ -703,11 +943,16 @@ export class Router {
|
|
|
703
943
|
if (route < 0) {
|
|
704
944
|
return this.otherMethod ? ROUTER_METHOD_NOT_ALLOWED : ROUTER_NOT_FOUND
|
|
705
945
|
}
|
|
706
|
-
const
|
|
707
|
-
|
|
946
|
+
const count: i32 = this.foundCount
|
|
947
|
+
// `pushPair` keeps the count below `found`'s length, so this never holds.
|
|
948
|
+
if (count < 0 || count > 1073741823) {
|
|
708
949
|
return ROUTER_TOO_MANY_PARAMS
|
|
709
950
|
}
|
|
710
|
-
|
|
951
|
+
const slots: i32 = count + count
|
|
952
|
+
if (slots > toI32(params.length)) {
|
|
953
|
+
return ROUTER_TOO_MANY_PARAMS
|
|
954
|
+
}
|
|
955
|
+
for (let k: i32 = 0; k < slots && k < toI32(params.length) && k < toI32(this.found.length); k++) {
|
|
711
956
|
params[k] = this.found[k]
|
|
712
957
|
}
|
|
713
958
|
return route
|
|
@@ -731,13 +976,19 @@ export class Router {
|
|
|
731
976
|
private staticRoute(target: u8[], tStart: i32, tEnd: i32): i32 {
|
|
732
977
|
// Never true, as in `descend`: the guard is the proof the loop's reads
|
|
733
978
|
// need. It answers as `descend`'s does, so no split is left for `answer`.
|
|
734
|
-
if (tStart < 0 || tEnd > toI32(target.length)) {
|
|
979
|
+
if (tStart < 0 || tStart >= tEnd || tEnd > toI32(target.length)) {
|
|
735
980
|
return ROUTER_NOT_FOUND
|
|
736
981
|
}
|
|
737
982
|
// The first byte is the `/` `enter` found; each later one is hashed, and
|
|
738
983
|
// a `/` among them also closes a segment, as in `descend`.
|
|
739
984
|
let hash: u32 = routerHashWith(routerHashOf(this.wanted), 47)
|
|
740
|
-
|
|
985
|
+
// `deepest` counts a route's segments, which the constructor caps at 512,
|
|
986
|
+
// so this is never true; it is the bound the count below needs.
|
|
987
|
+
const deepest: i32 = this.deepest
|
|
988
|
+
if (deepest < 0 || deepest > 1073741822) {
|
|
989
|
+
return ROUTER_NONE
|
|
990
|
+
}
|
|
991
|
+
const keep: i32 = deepest + 1
|
|
741
992
|
let segments: i32 = 0
|
|
742
993
|
let start: i32 = tStart + 1
|
|
743
994
|
let end: i32 = start
|
|
@@ -747,34 +998,52 @@ export class Router {
|
|
|
747
998
|
break
|
|
748
999
|
}
|
|
749
1000
|
if (c === 47) {
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
1001
|
+
this.span(segments, keep, start, end)
|
|
1002
|
+
// Never true, a path holding fewer slashes than bytes; the count
|
|
1003
|
+
// needs the bound, and a proof does not follow a loop.
|
|
1004
|
+
if (segments < 0 || segments > 1073741821) {
|
|
1005
|
+
return ROUTER_NONE
|
|
753
1006
|
}
|
|
754
|
-
segments
|
|
1007
|
+
segments = segments + 1
|
|
755
1008
|
start = end + 1
|
|
756
1009
|
}
|
|
757
1010
|
hash = routerHashWith(hash, c)
|
|
758
1011
|
end += 1
|
|
759
1012
|
}
|
|
760
|
-
if (segments <
|
|
761
|
-
|
|
762
|
-
this.segEnd[segments] = end
|
|
1013
|
+
if (segments < 0 || segments > 1073741822) {
|
|
1014
|
+
return ROUTER_NONE
|
|
763
1015
|
}
|
|
1016
|
+
this.span(segments, keep, start, end)
|
|
764
1017
|
this.segments = segments + 1
|
|
765
1018
|
this.pathEnd = end
|
|
1019
|
+
// `end` only grows from `tStart + 1`; a proof does not follow a loop.
|
|
1020
|
+
if (end < 0) {
|
|
1021
|
+
return ROUTER_NONE
|
|
1022
|
+
}
|
|
766
1023
|
const length: i32 = end - tStart
|
|
767
1024
|
const mask: i32 = toI32(this.staticSlots.length) - 1
|
|
768
1025
|
let slot: i32 = routerSlot(hash, mask)
|
|
769
1026
|
for (let probe: i32 = 0; probe < ROUTER_PROBES; probe++) {
|
|
1027
|
+
// Never true, as in `fileStatic`; the tests are the proof the reads need.
|
|
1028
|
+
if (slot < 0 || slot >= toI32(this.staticSlots.length)) {
|
|
1029
|
+
return ROUTER_NONE
|
|
1030
|
+
}
|
|
770
1031
|
const route: i32 = this.staticSlots[slot]
|
|
771
|
-
if (
|
|
1032
|
+
if (
|
|
1033
|
+
route < 0 ||
|
|
1034
|
+
route >= toI32(this.routePathStart.length) ||
|
|
1035
|
+
route >= toI32(this.routePathEnd.length) ||
|
|
1036
|
+
route >= toI32(this.routeMethod.length)
|
|
1037
|
+
) {
|
|
772
1038
|
return ROUTER_NONE
|
|
773
1039
|
}
|
|
774
1040
|
const at: i32 = this.routePathStart[route]
|
|
1041
|
+
const last: i32 = this.routePathEnd[route]
|
|
775
1042
|
if (
|
|
776
1043
|
this.routeMethod[route] === this.wanted &&
|
|
777
|
-
|
|
1044
|
+
at >= 0 &&
|
|
1045
|
+
last >= 0 &&
|
|
1046
|
+
last - at === length &&
|
|
778
1047
|
routerSameBytes(this.staticPaths, at, target, tStart, length)
|
|
779
1048
|
) {
|
|
780
1049
|
return route
|
|
@@ -796,12 +1065,41 @@ export class Router {
|
|
|
796
1065
|
if (depth === this.segments) {
|
|
797
1066
|
return this.endpointFor(node)
|
|
798
1067
|
}
|
|
1068
|
+
// Never true: the split keeps a span for every depth up to the deepest
|
|
1069
|
+
// route, and every node is the tree's. The tests are the proof the reads need.
|
|
1070
|
+
if (
|
|
1071
|
+
depth < 0 ||
|
|
1072
|
+
depth >= toI32(this.segStart.length) ||
|
|
1073
|
+
depth >= toI32(this.segEnd.length) ||
|
|
1074
|
+
node < 0 ||
|
|
1075
|
+
node >= toI32(this.firstChild.length) ||
|
|
1076
|
+
node >= toI32(this.paramChild.length) ||
|
|
1077
|
+
node >= toI32(this.wildChild.length)
|
|
1078
|
+
) {
|
|
1079
|
+
return -1
|
|
1080
|
+
}
|
|
799
1081
|
const start: i32 = this.segStart[depth]
|
|
800
1082
|
const end: i32 = this.segEnd[depth]
|
|
1083
|
+
// The split writes spans inside the target, so this never holds.
|
|
1084
|
+
if (start < 0 || end < 0) {
|
|
1085
|
+
return -1
|
|
1086
|
+
}
|
|
801
1087
|
const length: i32 = end - start
|
|
802
|
-
|
|
1088
|
+
let c: i32 = this.firstChild[node]
|
|
1089
|
+
while (
|
|
1090
|
+
c >= 0 &&
|
|
1091
|
+
c < toI32(this.nextSibling.length) &&
|
|
1092
|
+
c < toI32(this.labelStart.length) &&
|
|
1093
|
+
c < toI32(this.labelEnd.length)
|
|
1094
|
+
) {
|
|
803
1095
|
const at: i32 = this.labelStart[c]
|
|
804
|
-
|
|
1096
|
+
const last: i32 = this.labelEnd[c]
|
|
1097
|
+
if (
|
|
1098
|
+
at >= 0 &&
|
|
1099
|
+
last >= 0 &&
|
|
1100
|
+
last - at === length &&
|
|
1101
|
+
routerSameBytes(this.labels, at, target, start, length)
|
|
1102
|
+
) {
|
|
805
1103
|
const route: i32 = this.walk(target, c, depth + 1)
|
|
806
1104
|
if (route >= 0) {
|
|
807
1105
|
return route
|
|
@@ -809,41 +1107,64 @@ export class Router {
|
|
|
809
1107
|
// Labels under one node are distinct, so no other literal child can match.
|
|
810
1108
|
break
|
|
811
1109
|
}
|
|
1110
|
+
c = this.nextSibling[c]
|
|
812
1111
|
}
|
|
813
1112
|
const param: i32 = this.paramChild[node]
|
|
814
1113
|
if (param >= 0 && length > 0) {
|
|
815
1114
|
const top: i32 = this.foundCount
|
|
816
|
-
this.
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
1115
|
+
if (this.pushPair(start, end)) {
|
|
1116
|
+
const route: i32 = this.walk(target, param, depth + 1)
|
|
1117
|
+
if (route >= 0) {
|
|
1118
|
+
return route
|
|
1119
|
+
}
|
|
1120
|
+
this.foundCount = top
|
|
822
1121
|
}
|
|
823
|
-
this.foundCount = top
|
|
824
1122
|
}
|
|
825
1123
|
const wild: i32 = this.wildChild[node]
|
|
826
1124
|
if (wild >= 0) {
|
|
827
1125
|
this.visited = wrappingAdd(this.visited, toI32(1))
|
|
828
1126
|
const route: i32 = this.endpointFor(wild)
|
|
829
|
-
if (route >= 0) {
|
|
830
|
-
const top: i32 = this.foundCount
|
|
831
|
-
this.found[2 * top] = start
|
|
832
|
-
this.found[2 * top + 1] = this.pathEnd
|
|
833
|
-
this.foundCount = top + 1
|
|
1127
|
+
if (route >= 0 && this.pushPair(start, this.pathEnd)) {
|
|
834
1128
|
return route
|
|
835
1129
|
}
|
|
836
1130
|
}
|
|
837
1131
|
return -1
|
|
838
1132
|
}
|
|
839
1133
|
|
|
1134
|
+
/**
|
|
1135
|
+
* Pushes the pair `[start, end)` onto `found`, or answers false when it has
|
|
1136
|
+
* no room. A route's pattern holds at most `maxParams` parameters and `*`s,
|
|
1137
|
+
* and `found` has two slots for each, so a walk always has room.
|
|
1138
|
+
*/
|
|
1139
|
+
private pushPair(start: i32, end: i32): boolean {
|
|
1140
|
+
const top: i32 = this.foundCount
|
|
1141
|
+
// `found` holds fewer than 1025 slots, so this is no room by a wider margin.
|
|
1142
|
+
if (top < 0 || top > 1073741822) {
|
|
1143
|
+
return false
|
|
1144
|
+
}
|
|
1145
|
+
const first: i32 = top + top
|
|
1146
|
+
const second: i32 = first + 1
|
|
1147
|
+
if (first < 0 || first >= toI32(this.found.length) || second < 0 || second >= toI32(this.found.length)) {
|
|
1148
|
+
return false
|
|
1149
|
+
}
|
|
1150
|
+
this.found[first] = start
|
|
1151
|
+
this.found[second] = end
|
|
1152
|
+
this.foundCount = top + 1
|
|
1153
|
+
return true
|
|
1154
|
+
}
|
|
1155
|
+
|
|
840
1156
|
/** The route of the wanted method ending at `node`, or -1, noting a route of another method there. */
|
|
841
1157
|
private endpointFor(node: i32): i32 {
|
|
842
|
-
|
|
1158
|
+
if (node < 0 || node >= toI32(this.firstEndpoint.length)) {
|
|
1159
|
+
return -1
|
|
1160
|
+
}
|
|
1161
|
+
let e: i32 = this.firstEndpoint[node]
|
|
1162
|
+
while (e >= 0 && e < toI32(this.endpointNext.length) && e < toI32(this.routeMethod.length)) {
|
|
843
1163
|
if (this.routeMethod[e] === this.wanted) {
|
|
844
1164
|
return e
|
|
845
1165
|
}
|
|
846
1166
|
this.otherMethod = true
|
|
1167
|
+
e = this.endpointNext[e]
|
|
847
1168
|
}
|
|
848
1169
|
return -1
|
|
849
1170
|
}
|
|
@@ -864,21 +1185,33 @@ export class Router {
|
|
|
864
1185
|
* @returns how many decoded bytes were written, or `ROUTER_BAD_ESCAPE` or `ROUTER_NO_ROOM`
|
|
865
1186
|
*/
|
|
866
1187
|
export const routerDecode = (target: u8[], start: i32, end: i32, out: u8[]): i32 => {
|
|
867
|
-
if (start < 0 || end < start || end > toI32(target.length)) {
|
|
1188
|
+
if (start < 0 || end < 0 || end < start || end > toI32(target.length)) {
|
|
868
1189
|
panic(`routerDecode: the range ${start} to ${end} is outside its ${target.length} bytes`)
|
|
869
1190
|
}
|
|
1191
|
+
const n: i32 = toI32(target.length)
|
|
870
1192
|
let length: i32 = 0
|
|
871
1193
|
let i: i32 = start
|
|
872
1194
|
while (i < end) {
|
|
873
1195
|
if (toI32(target[i]) === 37) {
|
|
874
|
-
|
|
1196
|
+
// `end` is at most `n`, so the second test never decides; it is what
|
|
1197
|
+
// proves the two reads.
|
|
1198
|
+
if (
|
|
1199
|
+
i >= end - 2 ||
|
|
1200
|
+
i >= n - 2 ||
|
|
1201
|
+
routerHex(toI32(target[i + 1])) < 0 ||
|
|
1202
|
+
routerHex(toI32(target[i + 2])) < 0
|
|
1203
|
+
) {
|
|
875
1204
|
return ROUTER_BAD_ESCAPE
|
|
876
1205
|
}
|
|
877
1206
|
i += 3
|
|
878
1207
|
} else {
|
|
879
1208
|
i += 1
|
|
880
1209
|
}
|
|
881
|
-
|
|
1210
|
+
// One decoded byte per raw byte at most, so this never holds.
|
|
1211
|
+
if (length >= n) {
|
|
1212
|
+
return ROUTER_NO_ROOM
|
|
1213
|
+
}
|
|
1214
|
+
length = length + 1
|
|
882
1215
|
}
|
|
883
1216
|
if (length > toI32(out.length)) {
|
|
884
1217
|
return ROUTER_NO_ROOM
|
|
@@ -886,10 +1219,9 @@ export const routerDecode = (target: u8[], start: i32, end: i32, out: u8[]): i32
|
|
|
886
1219
|
let at: i32 = 0
|
|
887
1220
|
i = start
|
|
888
1221
|
const room: i32 = toI32(out.length)
|
|
889
|
-
const n: i32 = toI32(target.length)
|
|
890
1222
|
while (i >= 0 && i < end && i < n && at >= 0 && at < room) {
|
|
891
1223
|
const c: i32 = toI32(target[i])
|
|
892
|
-
if (c === 37 && i
|
|
1224
|
+
if (c === 37 && i < n - 2) {
|
|
893
1225
|
out[at] = toU8(routerHex(toI32(target[i + 1])) * 16 + routerHex(toI32(target[i + 2])))
|
|
894
1226
|
i += 3
|
|
895
1227
|
} else {
|
|
@@ -905,7 +1237,7 @@ export const routerDecode = (target: u8[], start: i32, end: i32, out: u8[]): i32
|
|
|
905
1237
|
* Where the hash the exact-path table files a static route under begins: at
|
|
906
1238
|
* the method's index, so that two methods of one path do not share a slot.
|
|
907
1239
|
*/
|
|
908
|
-
const routerHashOf = (method: i32): u32 => toU32(method + 1)
|
|
1240
|
+
const routerHashOf = (method: i32): u32 => toU32(method) + toU32(1)
|
|
909
1241
|
|
|
910
1242
|
/**
|
|
911
1243
|
* `bytes[at .. at + length)` packed into one word, its first byte lowest and
|
|
@@ -972,7 +1304,6 @@ const routerLimit = (field: string, value: i32, low: i32, high: i32): void => {
|
|
|
972
1304
|
panic(`RouterLimits.${field}: ${value} is outside ${low} to ${high}`)
|
|
973
1305
|
}
|
|
974
1306
|
}
|
|
975
|
-
|
|
976
1307
|
/** Whether `c` is a token character (RFC 9110 §5.6.2), what a method is made of. */
|
|
977
1308
|
const routerTchar = (c: i32): boolean => {
|
|
978
1309
|
if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
|
|
@@ -1003,14 +1334,20 @@ const routerTchar = (c: i32): boolean => {
|
|
|
1003
1334
|
* `_`; `*` is the whole segment or nowhere in it; anything else is a literal.
|
|
1004
1335
|
*/
|
|
1005
1336
|
const routerSegmentKind = (pattern: string, start: i32, end: i32): i32 => {
|
|
1006
|
-
|
|
1337
|
+
const n: i32 = toI32(pattern.length)
|
|
1338
|
+
// `splitPattern` passes a range inside `pattern`, so this never holds.
|
|
1339
|
+
if (start < 0 || end < 0 || end < start || end > n) {
|
|
1340
|
+
return ROUTER_BAD_PATTERN
|
|
1341
|
+
}
|
|
1342
|
+
const width: i32 = end - start
|
|
1343
|
+
const first: i32 = start < n ? toI32(pattern.charCodeAt(start)) : -1
|
|
1344
|
+
if (width === 1 && first === 42) {
|
|
1007
1345
|
return ROUTER_WILD
|
|
1008
1346
|
}
|
|
1009
|
-
const param: boolean =
|
|
1010
|
-
if (param &&
|
|
1347
|
+
const param: boolean = width > 0 && first === 58
|
|
1348
|
+
if (param && width === 1) {
|
|
1011
1349
|
return ROUTER_BAD_PATTERN
|
|
1012
1350
|
}
|
|
1013
|
-
const n: i32 = toI32(pattern.length)
|
|
1014
1351
|
for (let i: i32 = start; i >= 0 && i < end && i < n; i++) {
|
|
1015
1352
|
const c: i32 = toI32(pattern.charCodeAt(i))
|
|
1016
1353
|
if (c === 42) {
|
|
@@ -1027,23 +1364,37 @@ const routerSegmentKind = (pattern: string, start: i32, end: i32): i32 => {
|
|
|
1027
1364
|
|
|
1028
1365
|
/** Whether the names `pattern[a .. aEnd)` and `pattern[b .. bEnd)` are the same. */
|
|
1029
1366
|
const routerSameName = (pattern: string, a: i32, aEnd: i32, b: i32, bEnd: i32): boolean => {
|
|
1030
|
-
if (aEnd - a !== bEnd - b) {
|
|
1367
|
+
if (a < 0 || aEnd < 0 || b < 0 || bEnd < 0 || aEnd - a !== bEnd - b) {
|
|
1031
1368
|
return false
|
|
1032
1369
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1370
|
+
// Two cursors, each held inside `pattern`: `splitPattern` passes ranges
|
|
1371
|
+
// inside it, so the bound never ends the walk early.
|
|
1372
|
+
const n: i32 = toI32(pattern.length)
|
|
1373
|
+
let i: i32 = a
|
|
1374
|
+
let j: i32 = b
|
|
1375
|
+
while (i < aEnd) {
|
|
1376
|
+
if (i < 0 || i >= n || j < 0 || j >= n || pattern.charCodeAt(i) !== pattern.charCodeAt(j)) {
|
|
1035
1377
|
return false
|
|
1036
1378
|
}
|
|
1379
|
+
i += 1
|
|
1380
|
+
j += 1
|
|
1037
1381
|
}
|
|
1038
1382
|
return true
|
|
1039
1383
|
}
|
|
1040
1384
|
|
|
1041
1385
|
/** Whether `bytes[at .. at + length)` spells `text[from .. from + length)`. */
|
|
1042
1386
|
const routerLabelIsText = (bytes: u8[], at: i32, text: string, from: i32, length: i32): boolean => {
|
|
1387
|
+
// A range outside either one, which no caller passes, does not spell it.
|
|
1388
|
+
const nb: i32 = toI32(bytes.length)
|
|
1389
|
+
const nt: i32 = toI32(text.length)
|
|
1390
|
+
let i: i32 = at
|
|
1391
|
+
let j: i32 = from
|
|
1043
1392
|
for (let k: i32 = 0; k < length; k++) {
|
|
1044
|
-
if (toI32(bytes[
|
|
1393
|
+
if (i < 0 || i >= nb || j < 0 || j >= nt || toI32(bytes[i]) !== toI32(text.charCodeAt(j))) {
|
|
1045
1394
|
return false
|
|
1046
1395
|
}
|
|
1396
|
+
i += 1
|
|
1397
|
+
j += 1
|
|
1047
1398
|
}
|
|
1048
1399
|
return true
|
|
1049
1400
|
}
|