Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,51 @@ interface CompilerContext {
variantRank?: boolean;
}

function getSingleWildcardRoute<T>(router: RouterContext<T>): MethodData<T> | undefined {
if (router.static && Object.keys(router.static).length > 0) {
return undefined;
}
const root = router.root;
if (!root || !root.wildcard || root.wildcard.key !== "**") {
return undefined;
}
if (
(root.static && Object.keys(root.static).length > 0) ||
(root.methods && Object.keys(root.methods).length > 0) ||
root.param ||
root.suffix
) {
return undefined;
}
const wc = root.wildcard;
if ((wc.static && Object.keys(wc.static).length > 0) || wc.param || wc.suffix || wc.wildcard) {
return undefined;
}
if (!wc.methods) {
return undefined;
}
const methodKeys = Object.keys(wc.methods);
if (methodKeys.length !== 1 || methodKeys[0] !== "") {
return undefined;
}
const entries = wc.methods[""];
if (!entries || entries.length !== 1 || entries[0].route !== "/**") {
return undefined;
}
return entries[0];
}

function compileRouteMatch(ctx: CompilerContext): string {
const singleWildcard = getSingleWildcardRoute(ctx.router);
if (singleWildcard) {
const dataSlot = serializeData(ctx, singleWildcard);
const normalizePathHelper = ctx.opts?.normalize
? `if(p.includes("/.")){let _r=[],_v;for(_v of p.split("/")){if(_v===".")continue;if(_v==="..")_r.length>1&&_r.pop();else _r.push(_v)}if(_v==="."||_v==="..")_r.push("");p=_r.join("/")||"/"}`
: "";
const ret = `{data:${dataSlot},params:p?{"0":w,_:w}:{}}`;
return `${normalizePathHelper}if(p.charCodeAt(p.length-1)===47)p=p.slice(0,-1);const w=p.slice(1);return ${ctx.opts?.matchAll ? `[${ret}]` : ret};`;
}

const matchAll = ctx.opts?.matchAll;
ctx.rank = hasSuffixTrie(ctx.router.root);
let code = compileStaticMatch(ctx);
Expand Down
44 changes: 44 additions & 0 deletions test/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,47 @@ describe("compiled params (interpreter parity)", () => {
);
});
});

describe("compileRouter: single wildcard /** optimization", () => {
it("emits a minimal matcher for a single /** route (AOT and JIT)", () => {
const router = createRouter<string>();
addRoute(router, undefined, "/**", "all");

const aotCode = compileRouterToString(router);
expect(aotCode).toContain("const w=p.slice(1)");
expect(aotCode).not.toContain("p.split");

const aotMatchAllCode = compileRouterToString(router, { matchAll: true });
expect(aotMatchAllCode).toContain("const w=p.slice(1)");
expect(aotMatchAllCode).not.toContain("p.split");

const aot = evalAOT(aotCode);
const aotAll = evalAOT(aotMatchAllCode);
const jit = compileRouter(router);
const jitAll = compileRouter(router, { matchAll: true });

const paths = ["/", "", "//", "/foo", "/foo/", "/foo/bar", "/foo/bar/"];
for (const p of paths) {
const expected = findRoute(router, "GET", p);
expect(aot("GET", p)).toEqual(expected);
expect(jit("GET", p)).toEqual(expected);

const expectedAll = findAllRoutes(router, "GET", p);
expect(aotAll("GET", p)).toEqual(expectedAll);
expect(jitAll("GET", p)).toEqual(expectedAll);
}
});

it("falls back to standard compiler when additional routes or methods exist", () => {
const routerWithMethod = createRouter<string>();
addRoute(routerWithMethod, "GET", "/**", "all");
const codeMethod = compileRouterToString(routerWithMethod);
expect(codeMethod).toContain("p.split");

const routerWithMultiple = createRouter<string>();
addRoute(routerWithMultiple, undefined, "/**", "all");
addRoute(routerWithMultiple, undefined, "/api", "api");
const codeMultiple = compileRouterToString(routerWithMultiple);
expect(codeMultiple).toContain("p.split");
});
});