| import { |
| NEXT_INTERCEPTION_MARKER_PREFIX, |
| NEXT_QUERY_PARAM_PREFIX, |
| } from '../../../../lib/constants' |
| import { INTERCEPTION_ROUTE_MARKERS } from './interception-routes' |
| import { escapeStringRegexp } from '../../escape-regexp' |
| import { removeTrailingSlash } from './remove-trailing-slash' |
|
|
| export interface Group { |
| pos: number |
| repeat: boolean |
| optional: boolean |
| } |
|
|
| export interface RouteRegex { |
| groups: { [groupName: string]: Group } |
| re: RegExp |
| } |
|
|
| type GetNamedRouteRegexOptions = { |
| |
| |
| |
| |
| |
| prefixRouteKeys: boolean |
|
|
| |
| |
| |
| |
| |
| includeSuffix?: boolean |
|
|
| |
| |
| |
| |
| |
| |
| |
| includePrefix?: boolean |
|
|
| |
| |
| |
| excludeOptionalTrailingSlash?: boolean |
|
|
| |
| |
| |
| |
| backreferenceDuplicateKeys?: boolean |
| } |
|
|
| type GetRouteRegexOptions = { |
| |
| |
| |
| |
| |
| includeSuffix?: boolean |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| includePrefix?: boolean |
|
|
| |
| |
| |
| excludeOptionalTrailingSlash?: boolean |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const PARAMETER_PATTERN = /^([^[]*)\[((?:\[[^\]]*\])|[^\]]+)\](.*)$/ |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function parseParameter(param: string) { |
| const match = param.match(PARAMETER_PATTERN) |
|
|
| if (!match) { |
| return parseMatchedParameter(param) |
| } |
|
|
| return parseMatchedParameter(match[2]) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function parseMatchedParameter(param: string) { |
| const optional = param.startsWith('[') && param.endsWith(']') |
| if (optional) { |
| param = param.slice(1, -1) |
| } |
| const repeat = param.startsWith('...') |
| if (repeat) { |
| param = param.slice(3) |
| } |
| return { key: param, repeat, optional } |
| } |
|
|
| function getParametrizedRoute( |
| route: string, |
| includeSuffix: boolean, |
| includePrefix: boolean |
| ) { |
| const groups: { [groupName: string]: Group } = {} |
| let groupIndex = 1 |
|
|
| const segments: string[] = [] |
| for (const segment of removeTrailingSlash(route).slice(1).split('/')) { |
| const markerMatch = INTERCEPTION_ROUTE_MARKERS.find((m) => |
| segment.startsWith(m) |
| ) |
| const paramMatches = segment.match(PARAMETER_PATTERN) |
|
|
| if (markerMatch && paramMatches && paramMatches[2]) { |
| const { key, optional, repeat } = parseMatchedParameter(paramMatches[2]) |
| groups[key] = { pos: groupIndex++, repeat, optional } |
| segments.push(`/${escapeStringRegexp(markerMatch)}([^/]+?)`) |
| } else if (paramMatches && paramMatches[2]) { |
| const { key, repeat, optional } = parseMatchedParameter(paramMatches[2]) |
| groups[key] = { pos: groupIndex++, repeat, optional } |
|
|
| if (includePrefix && paramMatches[1]) { |
| segments.push(`/${escapeStringRegexp(paramMatches[1])}`) |
| } |
|
|
| let s = repeat ? (optional ? '(?:/(.+?))?' : '/(.+?)') : '/([^/]+?)' |
|
|
| |
| if (includePrefix && paramMatches[1]) { |
| s = s.substring(1) |
| } |
|
|
| segments.push(s) |
| } else { |
| segments.push(`/${escapeStringRegexp(segment)}`) |
| } |
|
|
| |
| if (includeSuffix && paramMatches && paramMatches[3]) { |
| segments.push(escapeStringRegexp(paramMatches[3])) |
| } |
| } |
|
|
| return { |
| parameterizedRoute: segments.join(''), |
| groups, |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| export function getRouteRegex( |
| normalizedRoute: string, |
| { |
| includeSuffix = false, |
| includePrefix = false, |
| excludeOptionalTrailingSlash = false, |
| }: GetRouteRegexOptions = {} |
| ): RouteRegex { |
| const { parameterizedRoute, groups } = getParametrizedRoute( |
| normalizedRoute, |
| includeSuffix, |
| includePrefix |
| ) |
|
|
| let re = parameterizedRoute |
| if (!excludeOptionalTrailingSlash) { |
| re += '(?:/)?' |
| } |
|
|
| return { |
| re: new RegExp(`^${re}$`), |
| groups: groups, |
| } |
| } |
|
|
| |
| |
| |
| |
| function buildGetSafeRouteKey() { |
| let i = 0 |
|
|
| return () => { |
| let routeKey = '' |
| let j = ++i |
| while (j > 0) { |
| routeKey += String.fromCharCode(97 + ((j - 1) % 26)) |
| j = Math.floor((j - 1) / 26) |
| } |
| return routeKey |
| } |
| } |
|
|
| function getSafeKeyFromSegment({ |
| interceptionMarker, |
| getSafeRouteKey, |
| segment, |
| routeKeys, |
| keyPrefix, |
| backreferenceDuplicateKeys, |
| }: { |
| interceptionMarker?: string |
| getSafeRouteKey: () => string |
| segment: string |
| routeKeys: Record<string, string> |
| keyPrefix?: string |
| backreferenceDuplicateKeys: boolean |
| }) { |
| const { key, optional, repeat } = parseMatchedParameter(segment) |
|
|
| |
| |
| let cleanedKey = key.replace(/\W/g, '') |
|
|
| if (keyPrefix) { |
| cleanedKey = `${keyPrefix}${cleanedKey}` |
| } |
| let invalidKey = false |
|
|
| |
| |
| if (cleanedKey.length === 0 || cleanedKey.length > 30) { |
| invalidKey = true |
| } |
| if (!isNaN(parseInt(cleanedKey.slice(0, 1)))) { |
| invalidKey = true |
| } |
|
|
| if (invalidKey) { |
| cleanedKey = getSafeRouteKey() |
| } |
|
|
| const duplicateKey = cleanedKey in routeKeys |
|
|
| if (keyPrefix) { |
| routeKeys[cleanedKey] = `${keyPrefix}${key}` |
| } else { |
| routeKeys[cleanedKey] = key |
| } |
|
|
| |
| |
| |
| const interceptionPrefix = interceptionMarker |
| ? escapeStringRegexp(interceptionMarker) |
| : '' |
|
|
| let pattern: string |
| if (duplicateKey && backreferenceDuplicateKeys) { |
| |
| |
| pattern = `\\k<${cleanedKey}>` |
| } else if (repeat) { |
| pattern = `(?<${cleanedKey}>.+?)` |
| } else { |
| pattern = `(?<${cleanedKey}>[^/]+?)` |
| } |
|
|
| return optional |
| ? `(?:/${interceptionPrefix}${pattern})?` |
| : `/${interceptionPrefix}${pattern}` |
| } |
|
|
| function getNamedParametrizedRoute( |
| route: string, |
| prefixRouteKeys: boolean, |
| includeSuffix: boolean, |
| includePrefix: boolean, |
| backreferenceDuplicateKeys: boolean |
| ) { |
| const getSafeRouteKey = buildGetSafeRouteKey() |
| const routeKeys: { [named: string]: string } = {} |
|
|
| const segments: string[] = [] |
| for (const segment of removeTrailingSlash(route).slice(1).split('/')) { |
| const hasInterceptionMarker = INTERCEPTION_ROUTE_MARKERS.some((m) => |
| segment.startsWith(m) |
| ) |
|
|
| const paramMatches = segment.match(PARAMETER_PATTERN) |
|
|
| if (hasInterceptionMarker && paramMatches && paramMatches[2]) { |
| |
| segments.push( |
| getSafeKeyFromSegment({ |
| getSafeRouteKey, |
| interceptionMarker: paramMatches[1], |
| segment: paramMatches[2], |
| routeKeys, |
| keyPrefix: prefixRouteKeys |
| ? NEXT_INTERCEPTION_MARKER_PREFIX |
| : undefined, |
| backreferenceDuplicateKeys, |
| }) |
| ) |
| } else if (paramMatches && paramMatches[2]) { |
| |
| if (includePrefix && paramMatches[1]) { |
| segments.push(`/${escapeStringRegexp(paramMatches[1])}`) |
| } |
|
|
| let s = getSafeKeyFromSegment({ |
| getSafeRouteKey, |
| segment: paramMatches[2], |
| routeKeys, |
| keyPrefix: prefixRouteKeys ? NEXT_QUERY_PARAM_PREFIX : undefined, |
| backreferenceDuplicateKeys, |
| }) |
|
|
| |
| if (includePrefix && paramMatches[1]) { |
| s = s.substring(1) |
| } |
|
|
| segments.push(s) |
| } else { |
| segments.push(`/${escapeStringRegexp(segment)}`) |
| } |
|
|
| |
| if (includeSuffix && paramMatches && paramMatches[3]) { |
| segments.push(escapeStringRegexp(paramMatches[3])) |
| } |
| } |
|
|
| return { |
| namedParameterizedRoute: segments.join(''), |
| routeKeys, |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function getNamedRouteRegex( |
| normalizedRoute: string, |
| options: GetNamedRouteRegexOptions |
| ) { |
| const result = getNamedParametrizedRoute( |
| normalizedRoute, |
| options.prefixRouteKeys, |
| options.includeSuffix ?? false, |
| options.includePrefix ?? false, |
| options.backreferenceDuplicateKeys ?? false |
| ) |
|
|
| let namedRegex = result.namedParameterizedRoute |
| if (!options.excludeOptionalTrailingSlash) { |
| namedRegex += '(?:/)?' |
| } |
|
|
| return { |
| ...getRouteRegex(normalizedRoute, options), |
| namedRegex: `^${namedRegex}$`, |
| routeKeys: result.routeKeys, |
| } |
| } |
|
|
| |
| |
| |
| |
| export function getNamedMiddlewareRegex( |
| normalizedRoute: string, |
| options: { |
| catchAll?: boolean |
| } |
| ) { |
| const { parameterizedRoute } = getParametrizedRoute( |
| normalizedRoute, |
| false, |
| false |
| ) |
| const { catchAll = true } = options |
| if (parameterizedRoute === '/') { |
| let catchAllRegex = catchAll ? '.*' : '' |
| return { |
| namedRegex: `^/${catchAllRegex}$`, |
| } |
| } |
|
|
| const { namedParameterizedRoute } = getNamedParametrizedRoute( |
| normalizedRoute, |
| false, |
| false, |
| false, |
| false |
| ) |
| let catchAllGroupedRegex = catchAll ? '(?:(/.*)?)' : '' |
| return { |
| namedRegex: `^${namedParameterizedRoute}${catchAllGroupedRegex}$`, |
| } |
| } |
|
|