UNPKG

@nestjs/core

Version:

The core of NestJS, a TypeScript framework for Node.js backends: modules, dependency injection and the request pipeline, on Express, Fastify or node:http.

119 lines (118 loc) • 5.56 kB
import { Logger, type RouteConflictPolicy, type VersioningOptions } from '@nestjs/common'; import { ResolvedRoute } from './interfaces/resolved-route.interface.js'; import { RouteConflict } from './interfaces/route-conflict.interface.js'; type SegmentKind = 'literal' | 'mixed' | 'param' | 'wildcard'; export interface PathSegment { kind: SegmentKind; value: string; } /** * Static utility class that detects overlapping HTTP routes and reports * them according to a per-kind policy. Stateless — every method takes * everything it needs as parameters. */ export declare class RouteConflictDetector { /** * Tags each segment of a path as a literal, a named param, a named * wildcard, or a mix of text and params (`v:version`, `:name.:ext`). * Optional groups are read as present (`/users{/:id}` as `/users/:id`). * Paths path-to-regexp cannot match (such as `/files/*` or * `/files/:name:ext` on Fastify) are split on `/` and tagged by the * first character of each segment. */ static tokenizePath(rawPath: string): PathSegment[]; /** * Tells whether two paths only differ by the names of their parameters and * wildcards (`/users/:id` and `/users/:username`, but not `/files/:id` and * `/files/:name.:ext`). */ static pathsHaveSameShape(leftPath: string, rightPath: string): boolean; /** * Decides whether two paths can match the same incoming request, given * only their declared patterns (no host/method/version considered). */ static pathsCanOverlap(leftPath: string, rightPath: string): boolean; private static comparablePathsCanOverlap; private static segmentListsCanOverlap; /** * Walks every unique pair of resolved routes and produces a conflict * record for each pair whose (method, host, version, path) tuples can * collide at runtime. * * A router that matches paths in any case (Express, unless it is told * otherwise) serves "/Users" with "/users", so their paths are compared * lower-cased. */ static detect(routes: ResolvedRoute[], versioningOptions: VersioningOptions | undefined, caseSensitive?: boolean): RouteConflict[]; /** * Applies the per-kind policy to a set of conflicts: silences `'off'`, * logs `'warn'` once per conflict, and aggregates every `'error'`-level * conflict into a single `RouteConflictException`. */ static handle(conflicts: RouteConflict[], policy: RouteConflictPolicy | undefined, logger: Logger): void; /** * Removes shadow conflicts that specificity sorting has already resolved. * * When `routeResolutionStrategy: 'specificity'` is active, the sort * promotes more-specific routes ahead of less-specific ones. A shadow * where the sort promoted the winner (it was declared *later* but sorted * *first*) is handled correctly at runtime — the more-specific route is * registered first and handles its requests while the less-specific route * handles the rest. Retaining such a conflict would cause `shadow: 'error'` * to abort an application whose routes actually work as intended. * * Shadows where the winner was already first in declaration order (the * sort did not swap them) are genuine and are kept unchanged. Duplicate * conflicts are always kept. * * @param conflicts Conflicts detected on the sorted route list. * @param declarationOrder Routes in their original declaration order * (i.e. before specificity sorting was applied). */ static filterSortResolvedShadows(conflicts: RouteConflict[], declarationOrder: ResolvedRoute[]): RouteConflict[]; private static segmentsCanOverlap; /** * Paths path-to-regexp cannot match, or with more than * `MAX_EXPANDED_GROUPS` groups, are split on `/` instead. */ private static toComparablePath; private static toComparableSegment; private static parseTokens; private static countGroups; private static expandGroups; private static flattenGroups; private static toSegments; private static classifySegment; /** * Tags each `/`-separated segment by its first character, for the paths * that are not parsed with path-to-regexp. */ private static splitPath; private static methodsCanOverlap; private static versionsCanOverlap; private static hostsCanOverlap; private static hostValuesCanMatchSameRequest; /** * Whether a RegExp and a host with a param share a host cannot be told * without intersecting them. The RegExp is tested against the host as * written and against each of its variants (lower-cased, as the router * matches a string host in any case), so a pair that no request matches * both of is not reported, which `shadow: 'error'` would turn into a failed * bootstrap. */ private static regExpCanMatchHost; /** * A string host is a path-to-regexp pattern, which the router matches * case-insensitively (`:tenant.example.com`, `{www.}example.com`). It is * compared as one segment per combination of its optional groups, as * paths are, so a group does not hide the text around it. */ private static toComparableHost; private static routesAreIdentical; private static hostsAreIdentical; private static hostValuesAreIdentical; private static versionsAreIdentical; private static forEachUniquePair; private static describeConflict; } export {};