fix(router): walk the radix index without recursion

The lookup walk recursed once per path segment, so a valid deep route
such as 4,999 static segments followed by /:x threw RangeError where the
linear matcher matched. Walk on an explicit stack instead, visiting
frames in the same order, and cover the case through buildFetch.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-11 22:06:49 -05:00
1 parent 996fb3f7a5
commit 5e0ae94ba2
2 files changed
+67 -13

No files matched your search

+18
View File
@@ -10,6 +10,7 @@
import { describe, it, expect } from 'vitest';
import * as live from '../src/http/router';
import type { RouteDef } from '../src/http/types';
import { buildFetch } from '../src/http/web';
import * as oracle from './router-parity/oracle';
import { runScenario, type MatcherImpl, type Scenario } from './router-parity/matcher';
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', () => {
it('rebuilds when the array it was built from changes length', () => {
const routes = [route('GET', '/a')];