feat(housekeeping): dispatch canonical domain routes
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import type { HousekeepingDomainId } from "../../migration/types";
|
||||
import type { CanonicalHousekeepingHref } from "../contracts";
|
||||
import type { HousekeepingRegistry } from "../registry";
|
||||
|
||||
export interface HousekeepingRouteMatch {
|
||||
routeId: string;
|
||||
domain: HousekeepingDomainId;
|
||||
params: Readonly<Record<string, string>>;
|
||||
canonicalHref: CanonicalHousekeepingHref;
|
||||
}
|
||||
|
||||
interface ParsedCanonicalPath {
|
||||
rawSegments: readonly string[];
|
||||
decodedSegments: readonly string[];
|
||||
}
|
||||
|
||||
interface RouteCandidate {
|
||||
routeId: string;
|
||||
domain: HousekeepingDomainId;
|
||||
patternSegments: readonly string[];
|
||||
dynamicSegments: number;
|
||||
}
|
||||
|
||||
export function matchHousekeepingRoute(
|
||||
registry: HousekeepingRegistry,
|
||||
canonicalPath: string,
|
||||
): HousekeepingRouteMatch | null {
|
||||
const path = parseCanonicalPath(canonicalPath);
|
||||
if (!path) return null;
|
||||
|
||||
const candidates = registry.domains
|
||||
.flatMap((domain) =>
|
||||
domain.routes
|
||||
.filter((route) =>
|
||||
routeBelongsToDomain(route.href, domain.canonicalHref),
|
||||
)
|
||||
.map((route) => {
|
||||
const patternSegments = route.href.slice(1).split("/");
|
||||
|
||||
return {
|
||||
routeId: route.id,
|
||||
domain: domain.id,
|
||||
patternSegments,
|
||||
dynamicSegments: patternSegments.filter((segment) =>
|
||||
segment.startsWith(":"),
|
||||
).length,
|
||||
};
|
||||
}),
|
||||
)
|
||||
.sort((left, right) => left.dynamicSegments - right.dynamicSegments);
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const params = matchSegments(candidate, path);
|
||||
if (!params) continue;
|
||||
|
||||
return {
|
||||
routeId: candidate.routeId,
|
||||
domain: candidate.domain,
|
||||
params: Object.freeze(params),
|
||||
canonicalHref: canonicalPath as CanonicalHousekeepingHref,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseCanonicalPath(canonicalPath: string): ParsedCanonicalPath | null {
|
||||
if (
|
||||
canonicalPath !== canonicalPath.trim() ||
|
||||
!canonicalPath.startsWith("/ase") ||
|
||||
canonicalPath.includes("?") ||
|
||||
canonicalPath.includes("#") ||
|
||||
canonicalPath.includes("\\") ||
|
||||
canonicalPath.endsWith("/")
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawSegments = canonicalPath.slice(1).split("/");
|
||||
if (rawSegments[0] !== "ase" || rawSegments.some((segment) => !segment)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const decodedSegments: string[] = [];
|
||||
for (const segment of rawSegments) {
|
||||
let decoded: string;
|
||||
try {
|
||||
decoded = decodeURIComponent(segment);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
!decoded ||
|
||||
decoded === "." ||
|
||||
decoded === ".." ||
|
||||
decoded.includes("/") ||
|
||||
decoded.includes("\\")
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
decodedSegments.push(decoded);
|
||||
}
|
||||
|
||||
return { rawSegments, decodedSegments };
|
||||
}
|
||||
|
||||
function routeBelongsToDomain(
|
||||
routeHref: CanonicalHousekeepingHref,
|
||||
domainHref: CanonicalHousekeepingHref,
|
||||
): boolean {
|
||||
return (
|
||||
routeHref === domainHref ||
|
||||
(domainHref !== "/ase" && routeHref.startsWith(`${domainHref}/`)) ||
|
||||
(domainHref === "/ase" && routeHref === "/ase")
|
||||
);
|
||||
}
|
||||
|
||||
function matchSegments(
|
||||
candidate: RouteCandidate,
|
||||
path: ParsedCanonicalPath,
|
||||
): Record<string, string> | null {
|
||||
if (candidate.patternSegments.length !== path.rawSegments.length) return null;
|
||||
|
||||
const params: Record<string, string> = {};
|
||||
for (const [index, patternSegment] of candidate.patternSegments.entries()) {
|
||||
if (!patternSegment.startsWith(":")) {
|
||||
if (patternSegment !== path.rawSegments[index]) return null;
|
||||
continue;
|
||||
}
|
||||
|
||||
const parameterName = patternSegment.slice(1);
|
||||
if (!parameterName || parameterName in params) return null;
|
||||
params[parameterName] = path.decodedSegments[index] ?? "";
|
||||
}
|
||||
|
||||
return params;
|
||||
}
|
||||
Reference in New Issue
Block a user