Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4de9f32e31 | ||
|
|
5e0ae94ba2 |
No files matched your search
+47
-11
@@ -543,35 +543,71 @@ function offer(search: Search, terminals: Terminal[]): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pending walk frames, shared by every lookup. A frame is a node and its depth; `POST` marks the
|
||||||
|
* frame that offers a node's catch-alls once its children are done. The walk is synchronous and
|
||||||
|
* calls no user code, and each call pops only what it pushed, so sharing is safe.
|
||||||
|
*/
|
||||||
|
const stackNodes: RadixNode[] = [];
|
||||||
|
const stackDepths: number[] = [];
|
||||||
|
const POST = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Depth-first walk with backtracking: a static branch that dead-ends deeper falls back to its param,
|
* Depth-first walk with backtracking: a static branch that dead-ends deeper falls back to its param,
|
||||||
* constrained and catch-all siblings. Params are decoded after splitting, so `%2F` stays inside one
|
* constrained and catch-all siblings. Params are decoded after splitting, so `%2F` stays inside one
|
||||||
* segment. Every branch that could still hold a better rank is visited; `min` prunes the rest.
|
* segment. Every branch that could still hold a better rank is visited; `min` prunes the rest.
|
||||||
*
|
*
|
||||||
|
* It runs on an explicit stack, not recursion, so a deep path that the linear matcher accepts cannot
|
||||||
|
* overflow the call stack. Frames pop in the order the recursive walk visited them: static child,
|
||||||
|
* then params in insertion order, then the node's catch-alls.
|
||||||
|
*
|
||||||
* The indexed loops and the empty-collection guards are measured, not style: at 10 routes the old
|
* The indexed loops and the empty-collection guards are measured, not style: at 10 routes the old
|
||||||
* linear scan is a handful of string compares, and these keep the walk within D4 of it (FPRD-001).
|
* linear scan is a handful of string compares, and these keep the walk within D4 of it (FPRD-001).
|
||||||
*/
|
*/
|
||||||
function walk(node: RadixNode, search: Search, segs: string[], depth: number): void {
|
function walk(root: RadixNode, search: Search, segs: string[]): void {
|
||||||
if (search.best && node.min >= search.best.rank) return;
|
const base = stackNodes.length;
|
||||||
|
stackNodes.push(root);
|
||||||
|
stackDepths.push(0);
|
||||||
|
|
||||||
|
while (stackNodes.length > base) {
|
||||||
|
const node = stackNodes.pop()!;
|
||||||
|
const depth = stackDepths.pop()!;
|
||||||
|
|
||||||
|
if (depth === POST) {
|
||||||
|
offer(search, node.catchAll);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search.best && node.min >= search.best.rank) continue;
|
||||||
|
|
||||||
|
if (node.catchAll.length) {
|
||||||
|
stackNodes.push(node);
|
||||||
|
stackDepths.push(POST);
|
||||||
|
}
|
||||||
|
|
||||||
if (depth === segs.length) {
|
if (depth === segs.length) {
|
||||||
if (node.exact.length) offer(search, node.exact);
|
if (node.exact.length) offer(search, node.exact);
|
||||||
} else {
|
continue;
|
||||||
const segment = segs[depth];
|
}
|
||||||
const next = node.statics.size ? node.statics.get(segment) : undefined;
|
|
||||||
if (next) walk(next, search, segs, depth + 1);
|
|
||||||
|
|
||||||
|
const segment = segs[depth];
|
||||||
const params = node.params;
|
const params = node.params;
|
||||||
|
|
||||||
for (let i = 0; i < params.length; i++) {
|
for (let i = params.length - 1; i >= 0; i--) {
|
||||||
const edge = params[i];
|
const edge = params[i];
|
||||||
const value = (search.decoded[depth] ??= decodeSegment(segment));
|
const value = (search.decoded[depth] ??= decodeSegment(segment));
|
||||||
if (edge.constraint && !edge.constraint.test(value)) continue;
|
if (edge.constraint && !edge.constraint.test(value)) continue;
|
||||||
walk(edge.node, search, segs, depth + 1);
|
stackNodes.push(edge.node);
|
||||||
}
|
stackDepths.push(depth + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.catchAll.length) offer(search, node.catchAll);
|
const next = node.statics.size ? node.statics.get(segment) : undefined;
|
||||||
|
|
||||||
|
if (next) {
|
||||||
|
stackNodes.push(next);
|
||||||
|
stackDepths.push(depth + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function searchTree(
|
function searchTree(
|
||||||
@@ -585,7 +621,7 @@ function searchTree(
|
|||||||
|
|
||||||
const exact = tree.exactPaths.get(search.path);
|
const exact = tree.exactPaths.get(search.path);
|
||||||
if (exact) offer(search, exact);
|
if (exact) offer(search, exact);
|
||||||
if (!search.best) walk(tree.root, search, segmentsOf(search), 0);
|
if (!search.best) walk(tree.root, search, segmentsOf(search));
|
||||||
return search.best;
|
return search.best;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
import { describe, it, expect } from 'vitest';
|
import { describe, it, expect } from 'vitest';
|
||||||
import * as live from '../src/http/router';
|
import * as live from '../src/http/router';
|
||||||
import type { RouteDef } from '../src/http/types';
|
import type { RouteDef } from '../src/http/types';
|
||||||
|
import { buildFetch } from '../src/http/web';
|
||||||
import * as oracle from './router-parity/oracle';
|
import * as oracle from './router-parity/oracle';
|
||||||
import { runScenario, type MatcherImpl, type Scenario } from './router-parity/matcher';
|
import { runScenario, type MatcherImpl, type Scenario } from './router-parity/matcher';
|
||||||
import { HAND_SCENARIOS, OWNER_SCENARIOS } from './router-parity/cases';
|
import { HAND_SCENARIOS, OWNER_SCENARIOS } from './router-parity/cases';
|
||||||
@@ -108,6 +109,23 @@ describe('radix index: ranking across branches', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('radix index: deep paths', () => {
|
||||||
|
it('matches a param under 4,999 static segments without overflowing the call stack', async () => {
|
||||||
|
// Every static segment is one walk frame, so a recursive walk throws RangeError here.
|
||||||
|
const prefix = '/s'.repeat(4999);
|
||||||
|
const routes = [route('GET', `${prefix}/:x`)];
|
||||||
|
const path = `${prefix}/a`;
|
||||||
|
|
||||||
|
const expected = oracle.matchRoute(routes, 'GET', path);
|
||||||
|
expect(expected?.params).toEqual({ x: 'a' });
|
||||||
|
expect(live.matchRoute(routes, 'GET', path)).toEqual(expected);
|
||||||
|
|
||||||
|
const res = await buildFetch(routes, undefined)(new Request(`http://x${path}`));
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(await res.text()).toBe(`${prefix}/:x`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('radix index: lifetime', () => {
|
describe('radix index: lifetime', () => {
|
||||||
it('rebuilds when the array it was built from changes length', () => {
|
it('rebuilds when the array it was built from changes length', () => {
|
||||||
const routes = [route('GET', '/a')];
|
const routes = [route('GET', '/a')];
|
||||||
|
|||||||
@@ -53,9 +53,14 @@ export const MUTANTS: Mutant[] = [
|
|||||||
edits: [
|
edits: [
|
||||||
{
|
{
|
||||||
file: ROUTER,
|
file: ROUTER,
|
||||||
from: ` if (next) walk(next, search, segs, depth + 1);`,
|
from: ` const segment = segs[depth];\n`,
|
||||||
to: ` if (next && depth === 0) return walk(next, search, segs, depth + 1);
|
to: ` const segment = segs[depth];
|
||||||
if (next) walk(next, search, segs, depth + 1);`,
|
const first = depth === 0 ? node.statics.get(segment) : undefined;
|
||||||
|
if (first) {
|
||||||
|
stackNodes.push(first);
|
||||||
|
stackDepths.push(1);
|
||||||
|
continue;
|
||||||
|
}\n`,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -116,8 +121,8 @@ export const MUTANTS: Mutant[] = [
|
|||||||
edits: [
|
edits: [
|
||||||
{
|
{
|
||||||
file: ROUTER,
|
file: ROUTER,
|
||||||
from: ` if (node.catchAll.length) offer(search, node.catchAll);`,
|
from: ` if (node.catchAll.length) {`,
|
||||||
to: ` if (node.catchAll.length && depth < segs.length) offer(search, node.catchAll);`,
|
to: ` if (node.catchAll.length && depth < segs.length) {`,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -242,8 +247,8 @@ export const MUTANTS: Mutant[] = [
|
|||||||
edits: [
|
edits: [
|
||||||
{
|
{
|
||||||
file: ROUTER,
|
file: ROUTER,
|
||||||
from: ` if (search.best && node.min >= search.best.rank) return;`,
|
from: ` if (search.best && node.min >= search.best.rank) continue;`,
|
||||||
to: ` if (search.best && node.min + 1 >= search.best.rank) return;`,
|
to: ` if (search.best && node.min + 1 >= search.best.rank) continue;`,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -311,8 +316,8 @@ export const MUTANTS: Mutant[] = [
|
|||||||
edits: [
|
edits: [
|
||||||
{
|
{
|
||||||
file: ROUTER,
|
file: ROUTER,
|
||||||
from: ` if (node.exact.length) offer(search, node.exact);\n } else {`,
|
from: ` if (node.catchAll.length) {`,
|
||||||
to: ` if (node.exact.length) offer(search, node.exact);\n if (node.statics.size) return;\n } else {`,
|
to: ` if (node.catchAll.length && !(depth === segs.length && node.statics.size)) {`,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in new issue
Block a user