2022-07-02 06:09:45 +00:00
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2023-10-15 23:39:37 +00:00
|
|
|
import type { Router, Result, ParamIndexMap } from '../../router.ts'
|
2022-09-13 23:01:14 +00:00
|
|
|
import { METHOD_NAME_ALL, METHODS, UnsupportedPathError } from '../../router.ts'
|
2022-09-20 11:57:08 +00:00
|
|
|
import { checkOptionalParameter } from '../../utils/url.ts'
|
2023-10-15 23:39:37 +00:00
|
|
|
import { PATH_ERROR, type ParamAssocArray } from './node.ts'
|
2022-07-02 06:09:45 +00:00
|
|
|
import { Trie } from './trie.ts'
|
|
|
|
|
2022-11-07 18:17:34 +00:00
|
|
|
const methodNames = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase())
|
2022-09-13 23:01:14 +00:00
|
|
|
|
2023-10-15 23:39:37 +00:00
|
|
|
type HandlerData<T> = [T, ParamIndexMap][]
|
2023-02-07 21:47:19 +00:00
|
|
|
type StaticMap<T> = Record<string, Result<T>>
|
2023-01-29 04:55:27 +00:00
|
|
|
type Matcher<T> = [RegExp, HandlerData<T>[], StaticMap<T>]
|
2023-10-15 23:39:37 +00:00
|
|
|
type HandlerWithMetadata<T> = [T, number] // [handler, paramCount]
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2023-10-15 23:39:37 +00:00
|
|
|
const emptyParam: string[] = []
|
2022-12-02 02:28:49 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-01-29 04:55:27 +00:00
|
|
|
const nullMatcher: Matcher<any> = [/^$/, [], {}]
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-12-08 11:11:08 +00:00
|
|
|
let wildcardRegExpCache: Record<string, RegExp> = {}
|
2022-09-13 23:01:14 +00:00
|
|
|
function buildWildcardRegExp(path: string): RegExp {
|
2022-12-08 11:11:08 +00:00
|
|
|
return (wildcardRegExpCache[path] ??= new RegExp(
|
|
|
|
path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearWildcardRegExpCache() {
|
|
|
|
wildcardRegExpCache = {}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 23:39:37 +00:00
|
|
|
function buildMatcherFromPreprocessedRoutes<T>(
|
|
|
|
routes: [string, HandlerWithMetadata<T>[]][]
|
|
|
|
): Matcher<T> {
|
2022-09-13 23:01:14 +00:00
|
|
|
const trie = new Trie()
|
2023-06-01 08:37:19 +00:00
|
|
|
const handlerData: HandlerData<T>[] = []
|
2022-07-02 06:09:45 +00:00
|
|
|
if (routes.length === 0) {
|
|
|
|
return nullMatcher
|
|
|
|
}
|
|
|
|
|
2023-06-01 08:37:19 +00:00
|
|
|
const routesWithStaticPathFlag = routes
|
2023-10-15 23:39:37 +00:00
|
|
|
.map(
|
|
|
|
(route) => [!/\*|\/:/.test(route[0]), ...route] as [boolean, string, HandlerWithMetadata<T>[]]
|
|
|
|
)
|
2023-06-01 08:37:19 +00:00
|
|
|
.sort(([isStaticA, pathA], [isStaticB, pathB]) =>
|
|
|
|
isStaticA ? 1 : isStaticB ? -1 : pathA.length - pathB.length
|
|
|
|
)
|
2022-11-28 13:20:40 +00:00
|
|
|
|
2023-01-29 04:55:27 +00:00
|
|
|
const staticMap: StaticMap<T> = {}
|
2023-06-01 08:37:19 +00:00
|
|
|
for (let i = 0, j = -1, len = routesWithStaticPathFlag.length; i < len; i++) {
|
|
|
|
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i]
|
|
|
|
if (pathErrorCheckOnly) {
|
2023-10-15 23:39:37 +00:00
|
|
|
staticMap[path] = [handlers.map(([h]) => [h, {}]), emptyParam]
|
2023-01-29 04:55:27 +00:00
|
|
|
} else {
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
2023-10-15 23:39:37 +00:00
|
|
|
let paramAssoc: ParamAssocArray
|
2022-09-13 23:01:14 +00:00
|
|
|
try {
|
2023-10-15 23:39:37 +00:00
|
|
|
paramAssoc = trie.insert(path, j, pathErrorCheckOnly)
|
2022-09-13 23:01:14 +00:00
|
|
|
} catch (e) {
|
2023-01-29 04:55:27 +00:00
|
|
|
throw e === PATH_ERROR ? new UnsupportedPathError(path) : e
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pathErrorCheckOnly) {
|
|
|
|
continue
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
2022-09-13 23:01:14 +00:00
|
|
|
|
2023-10-15 23:39:37 +00:00
|
|
|
handlerData[j] = handlers.map(([h, paramCount]) => {
|
|
|
|
const paramIndexMap: ParamIndexMap = {}
|
|
|
|
paramCount -= 1
|
|
|
|
for (; paramCount >= 0; paramCount--) {
|
|
|
|
const [key, value] = paramAssoc[paramCount]
|
|
|
|
paramIndexMap[key] = value
|
|
|
|
}
|
|
|
|
return [h, paramIndexMap]
|
|
|
|
})
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp()
|
2023-06-01 08:37:19 +00:00
|
|
|
for (let i = 0, len = handlerData.length; i < len; i++) {
|
2023-10-15 23:39:37 +00:00
|
|
|
for (let j = 0, len = handlerData[i].length; j < len; j++) {
|
|
|
|
const map = handlerData[i][j]?.[1]
|
|
|
|
if (!map) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
const keys = Object.keys(map)
|
|
|
|
for (let k = 0, len = keys.length; k < len; k++) {
|
|
|
|
map[keys[k]] = paramReplacementMap[map[keys[k]]]
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
const handlerMap: HandlerData<T>[] = []
|
2022-07-02 06:09:45 +00:00
|
|
|
// using `in` because indexReplacementMap is a sparse array
|
|
|
|
for (const i in indexReplacementMap) {
|
2023-06-01 08:37:19 +00:00
|
|
|
handlerMap[i] = handlerData[indexReplacementMap[i]]
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 04:55:27 +00:00
|
|
|
return [regexp, handlerMap, staticMap] as Matcher<T>
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
function findMiddleware<T>(
|
|
|
|
middleware: Record<string, T[]> | undefined,
|
|
|
|
path: string
|
|
|
|
): T[] | undefined {
|
|
|
|
if (!middleware) {
|
|
|
|
return undefined
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
for (const k of Object.keys(middleware).sort((a, b) => b.length - a.length)) {
|
|
|
|
if (buildWildcardRegExp(k).test(path)) {
|
|
|
|
return [...middleware[k]]
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
return undefined
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class RegExpRouter<T> implements Router<T> {
|
2023-05-17 15:05:28 +00:00
|
|
|
name: string = 'RegExpRouter'
|
2023-10-15 23:39:37 +00:00
|
|
|
middleware?: Record<string, Record<string, HandlerWithMetadata<T>[]>>
|
|
|
|
routes?: Record<string, Record<string, HandlerWithMetadata<T>[]>>
|
2022-09-13 23:01:14 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.middleware = { [METHOD_NAME_ALL]: {} }
|
|
|
|
this.routes = { [METHOD_NAME_ALL]: {} }
|
2022-09-12 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 06:09:45 +00:00
|
|
|
add(method: string, path: string, handler: T) {
|
2022-09-13 23:01:14 +00:00
|
|
|
const { middleware, routes } = this
|
|
|
|
|
|
|
|
if (!middleware || !routes) {
|
2022-07-02 06:09:45 +00:00
|
|
|
throw new Error('Can not add a route since the matcher is already built.')
|
|
|
|
}
|
|
|
|
|
2023-06-04 00:16:29 +00:00
|
|
|
if (methodNames.indexOf(method) === -1) methodNames.push(method)
|
2022-11-30 01:08:28 +00:00
|
|
|
if (!middleware[method]) {
|
|
|
|
;[middleware, routes].forEach((handlerMap) => {
|
|
|
|
handlerMap[method] = {}
|
|
|
|
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
|
|
|
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-11-07 18:17:34 +00:00
|
|
|
|
2022-07-02 06:09:45 +00:00
|
|
|
if (path === '/*') {
|
|
|
|
path = '*'
|
|
|
|
}
|
|
|
|
|
2023-10-15 23:39:37 +00:00
|
|
|
const paramCount = (path.match(/\/:/g) || []).length
|
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
if (/\*$/.test(path)) {
|
|
|
|
const re = buildWildcardRegExp(path)
|
2022-12-08 11:11:08 +00:00
|
|
|
if (method === METHOD_NAME_ALL) {
|
|
|
|
Object.keys(middleware).forEach((m) => {
|
|
|
|
middleware[m][path] ||=
|
|
|
|
findMiddleware(middleware[m], path) ||
|
|
|
|
findMiddleware(middleware[METHOD_NAME_ALL], path) ||
|
|
|
|
[]
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
middleware[method][path] ||=
|
|
|
|
findMiddleware(middleware[method], path) ||
|
|
|
|
findMiddleware(middleware[METHOD_NAME_ALL], path) ||
|
|
|
|
[]
|
|
|
|
}
|
2022-09-13 23:01:14 +00:00
|
|
|
Object.keys(middleware).forEach((m) => {
|
|
|
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
|
|
Object.keys(middleware[m]).forEach((p) => {
|
2023-10-15 23:39:37 +00:00
|
|
|
re.test(p) && middleware[m][p].push([handler, paramCount])
|
2022-09-13 23:01:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-11-28 13:20:40 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
Object.keys(routes).forEach((m) => {
|
|
|
|
if (method === METHOD_NAME_ALL || method === m) {
|
2023-10-15 23:39:37 +00:00
|
|
|
Object.keys(routes[m]).forEach(
|
|
|
|
(p) => re.test(p) && routes[m][p].push([handler, paramCount])
|
|
|
|
)
|
2022-09-13 23:01:14 +00:00
|
|
|
}
|
|
|
|
})
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
return
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 11:57:08 +00:00
|
|
|
const paths = checkOptionalParameter(path) || [path]
|
|
|
|
for (let i = 0, len = paths.length; i < len; i++) {
|
|
|
|
const path = paths[i]
|
|
|
|
|
|
|
|
Object.keys(routes).forEach((m) => {
|
2022-10-02 22:50:57 +00:00
|
|
|
if (method === METHOD_NAME_ALL || method === m) {
|
|
|
|
routes[m][path] ||= [
|
2022-11-30 01:08:28 +00:00
|
|
|
...(findMiddleware(middleware[m], path) ||
|
2022-10-02 22:50:57 +00:00
|
|
|
findMiddleware(middleware[METHOD_NAME_ALL], path) ||
|
|
|
|
[]),
|
|
|
|
]
|
2023-10-15 23:39:37 +00:00
|
|
|
routes[m][path].push([
|
|
|
|
handler,
|
|
|
|
paths.length === 2 && i === 0 ? paramCount - 1 : paramCount,
|
|
|
|
])
|
2022-10-02 22:50:57 +00:00
|
|
|
}
|
2022-09-20 11:57:08 +00:00
|
|
|
})
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 23:39:37 +00:00
|
|
|
match(method: string, path: string): Result<T> {
|
2022-12-08 11:11:08 +00:00
|
|
|
clearWildcardRegExpCache() // no longer used.
|
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
const matchers = this.buildAllMatchers()
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
this.match = (method, path) => {
|
|
|
|
const matcher = matchers[method]
|
2023-01-29 04:55:27 +00:00
|
|
|
|
|
|
|
const staticMatch = matcher[2][path]
|
|
|
|
if (staticMatch) {
|
2023-02-07 21:47:19 +00:00
|
|
|
return staticMatch
|
2023-01-29 04:55:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
const match = path.match(matcher[0])
|
|
|
|
if (!match) {
|
2023-10-15 23:39:37 +00:00
|
|
|
return [[], emptyParam]
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
2022-09-13 23:01:14 +00:00
|
|
|
|
|
|
|
const index = match.indexOf('', 1)
|
2023-10-15 23:39:37 +00:00
|
|
|
return [matcher[1][index], match]
|
2022-09-05 10:17:11 +00:00
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
return this.match(method, path)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
private buildAllMatchers(): Record<string, Matcher<T>> {
|
|
|
|
const matchers: Record<string, Matcher<T>> = {}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-11-07 18:17:34 +00:00
|
|
|
methodNames.forEach((method) => {
|
2022-09-13 23:01:14 +00:00
|
|
|
matchers[method] = this.buildMatcher(method) || matchers[METHOD_NAME_ALL]
|
|
|
|
})
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
// Release cache
|
|
|
|
this.middleware = this.routes = undefined
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
return matchers
|
|
|
|
}
|
2022-07-02 06:09:45 +00:00
|
|
|
|
2022-09-13 23:01:14 +00:00
|
|
|
private buildMatcher(method: string): Matcher<T> | null {
|
2023-10-15 23:39:37 +00:00
|
|
|
const routes: [string, HandlerWithMetadata<T>[]][] = []
|
2022-09-13 23:01:14 +00:00
|
|
|
|
|
|
|
let hasOwnRoute = method === METHOD_NAME_ALL
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
;[this.middleware!, this.routes!].forEach((r) => {
|
|
|
|
const ownRoute = r[method]
|
|
|
|
? Object.keys(r[method]).map((path) => [path, r[method][path]])
|
|
|
|
: []
|
|
|
|
if (ownRoute.length !== 0) {
|
|
|
|
hasOwnRoute ||= true
|
2023-10-15 23:39:37 +00:00
|
|
|
routes.push(...(ownRoute as [string, HandlerWithMetadata<T>[]][]))
|
2022-09-13 23:01:14 +00:00
|
|
|
} else if (method !== METHOD_NAME_ALL) {
|
|
|
|
routes.push(
|
|
|
|
...(Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]]) as [
|
|
|
|
string,
|
2023-10-15 23:39:37 +00:00
|
|
|
HandlerWithMetadata<T>[]
|
2022-09-13 23:01:14 +00:00
|
|
|
][])
|
|
|
|
)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
2022-09-13 23:01:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (!hasOwnRoute) {
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
return buildMatcherFromPreprocessedRoutes(routes)
|
2022-07-02 06:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|