@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.
510 lines (509 loc) • 24.7 kB
JavaScript
import { RequestMethod, VERSION_NEUTRAL, VersioningType, } from '@nestjs/common';
import { parse, pathToRegexp, stringify, TokenData, } from 'path-to-regexp';
import { RouteConflictException } from '../errors/exceptions/route-conflict.exception.js';
import { DUPLICATE_ROUTE_MESSAGE, SHADOWED_ROUTE_MESSAGE, } from '../errors/messages.js';
// Each optional group doubles the number of path variants to compare
const MAX_EXPANDED_GROUPS = 6;
const joinText = (tokens) => tokens.map(token => (token.type === 'text' ? token.value : '')).join('');
/**
* 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 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) {
const tokens = RouteConflictDetector.parseTokens(rawPath);
if (!tokens) {
return RouteConflictDetector.splitPath(rawPath);
}
return RouteConflictDetector.toSegments(RouteConflictDetector.flattenGroups(tokens, false));
}
/**
* 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, rightPath) {
const shapeOf = (path) => JSON.stringify(parse(path).tokens, (key, value) => key === 'name' ? undefined : value);
try {
return shapeOf(leftPath) === shapeOf(rightPath);
}
catch {
return false;
}
}
/**
* Decides whether two paths can match the same incoming request, given
* only their declared patterns (no host/method/version considered).
*/
static pathsCanOverlap(leftPath, rightPath) {
return RouteConflictDetector.comparablePathsCanOverlap(RouteConflictDetector.toComparablePath(leftPath), RouteConflictDetector.toComparablePath(rightPath));
}
static comparablePathsCanOverlap(leftPath, rightPath) {
// Rules out most pairs before comparing every pair of variants
if (!leftPath.leadingText.startsWith(rightPath.leadingText) &&
!rightPath.leadingText.startsWith(leftPath.leadingText)) {
return false;
}
return leftPath.variants.some(leftSegments => rightPath.variants.some(rightSegments => RouteConflictDetector.segmentListsCanOverlap(leftSegments, rightSegments)));
}
static segmentListsCanOverlap(leftSegments, rightSegments) {
const leftEndsInWildcard = leftSegments[leftSegments.length - 1]?.kind === 'wildcard';
const rightEndsInWildcard = rightSegments[rightSegments.length - 1]?.kind === 'wildcard';
// A named wildcard like `*path` requires at least one matched segment,
// so only the *shorter* side's trailing wildcard can absorb the
// difference. If the longer side has the wildcard, the other side
// simply does not have enough segments to ever reach that position.
if (leftSegments.length !== rightSegments.length) {
const shorterEndsInWildcard = leftSegments.length < rightSegments.length
? leftEndsInWildcard
: rightEndsInWildcard;
if (!shorterEndsInWildcard) {
return false;
}
}
const sharedLength = Math.min(leftSegments.length, rightSegments.length);
let canOverlap = true;
leftSegments.slice(0, sharedLength).forEach((leftSegment, segmentIndex) => {
if (!canOverlap)
return;
if (!RouteConflictDetector.segmentsCanOverlap(leftSegment, rightSegments[segmentIndex])) {
canOverlap = false;
}
});
return canOverlap;
}
/**
* 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, versioningOptions, caseSensitive = true) {
const conflicts = [];
// Every route is compared with every other one: read each path once
const comparablePaths = new Map(routes.map(route => [
route.path,
RouteConflictDetector.toComparablePath(route.path, caseSensitive),
]));
// ... and each string host, the first time a pair of routes needs it
const comparableHosts = new Map();
const comparableHostOf = (host) => {
let comparableHost = comparableHosts.get(host);
if (!comparableHost) {
comparableHost = RouteConflictDetector.toComparableHost(host);
comparableHosts.set(host, comparableHost);
}
return comparableHost;
};
RouteConflictDetector.forEachUniquePair(routes, (earlierRoute, laterRoute) => {
if (!RouteConflictDetector.methodsCanOverlap(earlierRoute.method, laterRoute.method)) {
return;
}
if (!RouteConflictDetector.versionsCanOverlap(earlierRoute.version, laterRoute.version, versioningOptions)) {
return;
}
if (!RouteConflictDetector.hostsCanOverlap(earlierRoute.host, laterRoute.host, comparableHostOf)) {
return;
}
if (!RouteConflictDetector.comparablePathsCanOverlap(comparablePaths.get(earlierRoute.path), comparablePaths.get(laterRoute.path))) {
return;
}
const isIdentical = RouteConflictDetector.routesAreIdentical(earlierRoute, laterRoute, versioningOptions, caseSensitive);
conflicts.push({
winner: earlierRoute,
shadowed: laterRoute,
kind: isIdentical ? 'duplicate' : 'shadow',
});
});
return conflicts;
}
/**
* 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, policy, logger) {
if (conflicts.length === 0 || policy === undefined)
return;
const errorMessages = [];
conflicts.forEach(conflict => {
const policyForKind = policy[conflict.kind] ?? 'off';
if (policyForKind === 'off')
return;
const message = RouteConflictDetector.describeConflict(conflict);
if (policyForKind === 'warn') {
logger.warn(message);
return;
}
errorMessages.push(message);
});
if (errorMessages.length > 0) {
throw new RouteConflictException(errorMessages);
}
}
/**
* 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, declarationOrder) {
const declarationIndex = new Map(declarationOrder.map((route, idx) => [route, idx]));
return conflicts.filter(conflict => {
if (conflict.kind !== 'shadow')
return true;
const winnerDeclIdx = declarationIndex.get(conflict.winner) ?? -1;
const shadowedDeclIdx = declarationIndex.get(conflict.shadowed) ?? -1;
// The sort promoted the winner (declared later, but sorted to the
// front because it is more specific). The shadow is resolved at
// runtime — drop it. Keep only genuine shadows where the winner was
// already first in declaration order.
return winnerDeclIdx < shadowedDeclIdx;
});
}
static segmentsCanOverlap(leftSegment, rightSegment) {
if (leftSegment.kind === 'wildcard' || rightSegment.kind === 'wildcard') {
return true;
}
if (leftSegment.kind === 'param' || rightSegment.kind === 'param') {
return true;
}
if (leftSegment.kind === 'mixed' && rightSegment.kind === 'mixed') {
return ((leftSegment.start.startsWith(rightSegment.start) ||
rightSegment.start.startsWith(leftSegment.start)) &&
(leftSegment.end.endsWith(rightSegment.end) ||
rightSegment.end.endsWith(leftSegment.end)));
}
if (leftSegment.kind === 'mixed') {
return leftSegment.matcher.test(rightSegment.value);
}
if (rightSegment.kind === 'mixed') {
return rightSegment.matcher.test(leftSegment.value);
}
return leftSegment.value === rightSegment.value;
}
/**
* Paths path-to-regexp cannot match, or with more than
* `MAX_EXPANDED_GROUPS` groups, are split on `/` instead.
*/
static toComparablePath(rawPath, caseSensitive = true) {
const path = caseSensitive ? rawPath : rawPath.toLowerCase();
const tokens = RouteConflictDetector.parseTokens(path);
if (!tokens ||
RouteConflictDetector.countGroups(tokens) > MAX_EXPANDED_GROUPS) {
return {
variants: [
RouteConflictDetector.splitPath(path).map(RouteConflictDetector.toComparableSegment),
],
leadingText: '',
};
}
const firstNonText = tokens.findIndex(token => token.type !== 'text');
return {
variants: RouteConflictDetector.expandGroups(tokens).map(variant => RouteConflictDetector.toSegments(variant.map(token => ({ token, optional: false }))).map(RouteConflictDetector.toComparableSegment)),
// Slashes collapsed, as the segments ignore empty ones
leadingText: `/${joinText(firstNonText === -1 ? tokens : tokens.slice(0, firstNonText))}`.replace(/\/{2,}/g, '/'),
};
}
static toComparableSegment(segment) {
if (segment.kind !== 'mixed') {
return { kind: segment.kind, value: segment.value };
}
// A segment matching this one starts with the text before its first
// param and ends with the text after its last one
const tokens = parse(segment.value).tokens;
return {
kind: 'mixed',
value: segment.value,
matcher: pathToRegexp(segment.value, { sensitive: true }).regexp,
start: joinText(tokens.slice(0, tokens.findIndex(token => token.type !== 'text'))),
end: joinText(tokens.slice(tokens.findLastIndex(token => token.type !== 'text') + 1)),
};
}
static parseTokens(rawPath) {
try {
// Also rejects what parse() accepts but cannot be matched, such as
// `:name:ext`, which Fastify takes as a single param
pathToRegexp(rawPath);
return parse(rawPath).tokens;
}
catch {
return undefined;
}
}
static countGroups(tokens) {
return tokens.reduce((count, token) => token.type === 'group'
? count + 1 + RouteConflictDetector.countGroups(token.tokens)
: count, 0);
}
static expandGroups(tokens) {
return tokens.reduce((variants, token) => {
if (token.type !== 'group') {
return variants.map(variant => [...variant, token]);
}
const groupVariants = RouteConflictDetector.expandGroups(token.tokens);
return variants.flatMap(variant => [
variant,
...groupVariants.map(groupVariant => [...variant, ...groupVariant]),
]);
}, [[]]);
}
static flattenGroups(tokens, optional) {
return tokens.flatMap(token => token.type === 'group'
? RouteConflictDetector.flattenGroups(token.tokens, true)
: [{ token, optional }]);
}
static toSegments(parts) {
const segments = [[]];
parts.forEach(part => {
if (part.token.type !== 'text') {
segments[segments.length - 1].push(part);
return;
}
part.token.value.split('/').forEach((text, index) => {
if (index > 0) {
segments.push([]);
}
if (text.length > 0) {
segments[segments.length - 1].push({
token: { type: 'text', value: text },
optional: part.optional,
});
}
});
});
return segments
.filter(segmentParts => segmentParts.length > 0)
.map(segmentParts => RouteConflictDetector.classifySegment(segmentParts));
}
static classifySegment(parts) {
const tokens = parts.map(part => part.token);
// After text, a wildcard is compared as a mixed segment, which keeps
// `static*path` from overlapping `/users`
if (tokens[0].type === 'wildcard') {
return { kind: 'wildcard', value: tokens[0].name };
}
if (tokens.every(token => token.type === 'text')) {
return {
kind: 'literal',
value: tokens.map(token => token.value).join(''),
};
}
// An optional suffix does not narrow a param down: `:id{.:format}`
// matches the same segments as `:id`
const requiredParts = parts.filter(part => !part.optional);
const [onlyPart, ...otherParts] = requiredParts.length > 0 ? requiredParts : parts;
if (otherParts.length === 0 && onlyPart.token.type === 'param') {
return { kind: 'param', value: onlyPart.token.name };
}
return { kind: 'mixed', value: stringify(new TokenData(tokens)) };
}
/**
* Tags each `/`-separated segment by its first character, for the paths
* that are not parsed with path-to-regexp.
*/
static splitPath(rawPath) {
return rawPath
.split('/')
.filter(rawSegment => rawSegment.length > 0)
.map(rawSegment => {
if (rawSegment.startsWith('*')) {
return { kind: 'wildcard', value: rawSegment.slice(1) };
}
if (rawSegment.startsWith('{*') && rawSegment.endsWith('}')) {
return { kind: 'wildcard', value: rawSegment.slice(2, -1) };
}
if (rawSegment.startsWith(':')) {
return { kind: 'param', value: rawSegment.slice(1) };
}
return { kind: 'literal', value: rawSegment };
});
}
static methodsCanOverlap(leftMethod, rightMethod) {
if (leftMethod === RequestMethod.ALL || rightMethod === RequestMethod.ALL) {
return true;
}
return leftMethod === rightMethod;
}
static versionsCanOverlap(leftVersion, rightVersion, versioningOptions) {
if (!versioningOptions)
return true;
if (versioningOptions.type === VersioningType.URI)
return true;
const leftMatchesAnyVersion = leftVersion === undefined || leftVersion === VERSION_NEUTRAL;
const rightMatchesAnyVersion = rightVersion === undefined || rightVersion === VERSION_NEUTRAL;
if (leftMatchesAnyVersion || rightMatchesAnyVersion)
return true;
const leftValues = Array.isArray(leftVersion) ? leftVersion : [leftVersion];
const rightValues = Array.isArray(rightVersion)
? rightVersion
: [rightVersion];
return leftValues.some(versionValue => rightValues.includes(versionValue));
}
static hostsCanOverlap(leftHost, rightHost, comparableHostOf) {
if (leftHost === undefined || rightHost === undefined)
return true;
const leftHosts = Array.isArray(leftHost) ? leftHost : [leftHost];
const rightHosts = Array.isArray(rightHost) ? rightHost : [rightHost];
return leftHosts.some(leftValue => rightHosts.some(rightValue => RouteConflictDetector.hostValuesCanMatchSameRequest(leftValue, rightValue, comparableHostOf)));
}
static hostValuesCanMatchSameRequest(leftValue, rightValue, comparableHostOf) {
if (leftValue instanceof RegExp && rightValue instanceof RegExp) {
return true;
}
if (leftValue instanceof RegExp) {
return RouteConflictDetector.regExpCanMatchHost(leftValue, rightValue, comparableHostOf(rightValue).variants);
}
if (rightValue instanceof RegExp) {
return RouteConflictDetector.regExpCanMatchHost(rightValue, leftValue, comparableHostOf(leftValue).variants);
}
if (leftValue === rightValue) {
return true;
}
const leftHost = comparableHostOf(leftValue);
const rightHost = comparableHostOf(rightValue);
// Rules out most pairs before comparing every pair of variants
if (!leftHost.trailingText.endsWith(rightHost.trailingText) &&
!rightHost.trailingText.endsWith(leftHost.trailingText)) {
return false;
}
return leftHost.variants.some(leftVariant => rightHost.variants.some(rightVariant => RouteConflictDetector.segmentsCanOverlap(leftVariant, rightVariant)));
}
/**
* 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.
*/
static regExpCanMatchHost(regExp, host, hostVariants) {
const test = (value) => {
// Reset lastIndex before calling test() to guard against RegExps with
// the `g` or `y` flags: those are stateful and would produce
// inconsistent results (false negatives) when the same instance is
// reused across the multiple pair comparisons that a single detect()
// run performs.
regExp.lastIndex = 0;
return regExp.test(value);
};
return test(host) || hostVariants.some(variant => test(variant.value));
}
/**
* 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.
*/
static toComparableHost(host) {
const tokens = RouteConflictDetector.parseTokens(host);
if (!tokens) {
const value = host.toLowerCase();
return { variants: [{ kind: 'literal', value }], trailingText: value };
}
if (RouteConflictDetector.countGroups(tokens) > MAX_EXPANDED_GROUPS) {
// Too many variants to compare: taken to match any host
return {
variants: [{ kind: 'wildcard', value: host }],
trailingText: '',
};
}
return {
variants: RouteConflictDetector.expandGroups(tokens).map(variant => variant.every(token => token.type === 'text')
? { kind: 'literal', value: joinText(variant).toLowerCase() }
: RouteConflictDetector.toComparableSegment({
kind: 'mixed',
value: stringify(new TokenData(variant)).toLowerCase(),
})),
trailingText: joinText(tokens.slice(tokens.findLastIndex(token => token.type !== 'text') + 1)).toLowerCase(),
};
}
static routesAreIdentical(leftRoute, rightRoute, versioningOptions, caseSensitive = true) {
const samePath = caseSensitive
? leftRoute.path === rightRoute.path
: leftRoute.path.toLowerCase() === rightRoute.path.toLowerCase();
return (leftRoute.method === rightRoute.method &&
samePath &&
RouteConflictDetector.hostsAreIdentical(leftRoute.host, rightRoute.host) &&
RouteConflictDetector.versionsAreIdentical(leftRoute.version, rightRoute.version, versioningOptions));
}
static hostsAreIdentical(leftHost, rightHost) {
if (leftHost === undefined && rightHost === undefined)
return true;
if (leftHost === undefined || rightHost === undefined)
return false;
const leftHosts = Array.isArray(leftHost) ? leftHost : [leftHost];
const rightHosts = Array.isArray(rightHost) ? rightHost : [rightHost];
// Set comparison, in both directions: ['a', 'b'] and ['b', 'a'] allow
// the same hosts, and so do ['a', 'a'] and ['a'], while ['a', 'a'] and
// ['a', 'b'] do not, although they have the same length.
const isSubset = (hosts, otherHosts) => hosts.every(value => otherHosts.some(otherValue => RouteConflictDetector.hostValuesAreIdentical(value, otherValue)));
return isSubset(leftHosts, rightHosts) && isSubset(rightHosts, leftHosts);
}
static hostValuesAreIdentical(leftValue, rightValue) {
if (leftValue instanceof RegExp && rightValue instanceof RegExp) {
return (leftValue.source === rightValue.source &&
leftValue.flags === rightValue.flags);
}
return leftValue === rightValue;
}
static versionsAreIdentical(leftVersion, rightVersion, versioningOptions) {
// When versioning is not configured (or URI-based, where the
// version is encoded in the path), version metadata does not
// gate request matching at runtime, so two routes that differ
// only in their declared `version` are runtime duplicates.
if (!versioningOptions || versioningOptions.type === VersioningType.URI) {
return true;
}
if (leftVersion === rightVersion)
return true;
const leftValues = Array.isArray(leftVersion) ? leftVersion : [leftVersion];
const rightValues = Array.isArray(rightVersion)
? rightVersion
: [rightVersion];
if (leftValues.length !== rightValues.length)
return false;
return leftValues.every(value => rightValues.includes(value));
}
static forEachUniquePair(items, visit) {
items.forEach((leftItem, leftIndex) => {
items.slice(leftIndex + 1).forEach(rightItem => {
visit(leftItem, rightItem);
});
});
}
static describeConflict(conflict) {
const method = RequestMethod[conflict.winner.method];
const winnerLabel = `${conflict.winner.instanceWrapper.name}#${conflict.winner.methodName}`;
const shadowedLabel = `${conflict.shadowed.instanceWrapper.name}#${conflict.shadowed.methodName}`;
if (conflict.kind === 'duplicate') {
return DUPLICATE_ROUTE_MESSAGE(method, conflict.winner.path, winnerLabel, shadowedLabel);
}
return SHADOWED_ROUTE_MESSAGE(method, conflict.shadowed.path, shadowedLabel, conflict.winner.path, winnerLabel);
}
}