2022-07-02 08:09:45 +02:00
|
|
|
import type { Result } from '../../router.ts'
|
|
|
|
import { METHOD_NAME_ALL } from '../../router.ts'
|
|
|
|
import type { Pattern } from '../../utils/url.ts'
|
2023-01-08 10:45:46 +01:00
|
|
|
import { splitPath, splitRoutingPath, getPattern } from '../../utils/url.ts'
|
2022-07-02 08:09:45 +02:00
|
|
|
|
|
|
|
type HandlerSet<T> = {
|
|
|
|
handler: T
|
|
|
|
score: number
|
|
|
|
name: string // For debug
|
|
|
|
}
|
|
|
|
|
|
|
|
function findParam<T>(node: Node<T>, name: string): boolean {
|
|
|
|
for (let i = 0, len = node.patterns.length; i < len; i++) {
|
|
|
|
if (typeof node.patterns[i] === 'object' && node.patterns[i][1] === name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const nodes = Object.values(node.children)
|
|
|
|
for (let i = 0, len = nodes.length; i < len; i++) {
|
|
|
|
if (findParam(nodes[i], name)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Node<T> {
|
|
|
|
methods: Record<string, HandlerSet<T>>[]
|
|
|
|
|
|
|
|
children: Record<string, Node<T>>
|
|
|
|
patterns: Pattern[]
|
|
|
|
order: number = 0
|
|
|
|
name: string
|
|
|
|
handlerSetCache: Record<string, HandlerSet<T>[]>
|
|
|
|
|
|
|
|
constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {
|
|
|
|
this.children = children || {}
|
|
|
|
this.methods = []
|
|
|
|
this.name = ''
|
|
|
|
if (method && handler) {
|
|
|
|
const m: Record<string, HandlerSet<T>> = {}
|
|
|
|
m[method] = { handler: handler, score: 0, name: this.name }
|
|
|
|
this.methods = [m]
|
|
|
|
}
|
|
|
|
this.patterns = []
|
|
|
|
this.handlerSetCache = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
insert(method: string, path: string, handler: T): Node<T> {
|
|
|
|
this.name = `${method} ${path}`
|
|
|
|
this.order = ++this.order
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
|
|
let curNode: Node<T> = this
|
2023-01-08 10:45:46 +01:00
|
|
|
const parts = splitRoutingPath(path)
|
2022-07-02 08:09:45 +02:00
|
|
|
|
|
|
|
const parentPatterns: Pattern[] = []
|
|
|
|
const errorMessage = (name: string): string => {
|
|
|
|
return `Duplicate param name, use another name instead of '${name}' - ${method} ${path} <--- '${name}'`
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0, len = parts.length; i < len; i++) {
|
|
|
|
const p: string = parts[i]
|
|
|
|
|
|
|
|
if (Object.keys(curNode.children).includes(p)) {
|
|
|
|
parentPatterns.push(...curNode.patterns)
|
|
|
|
curNode = curNode.children[p]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
curNode.children[p] = new Node()
|
|
|
|
|
|
|
|
const pattern = getPattern(p)
|
|
|
|
if (pattern) {
|
|
|
|
if (typeof pattern === 'object') {
|
|
|
|
for (let j = 0, len = parentPatterns.length; j < len; j++) {
|
|
|
|
if (typeof parentPatterns[j] === 'object' && parentPatterns[j][1] === pattern[1]) {
|
|
|
|
throw new Error(errorMessage(pattern[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Object.values(curNode.children).some((n) => findParam(n, pattern[1]))) {
|
|
|
|
throw new Error(errorMessage(pattern[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curNode.patterns.push(pattern)
|
|
|
|
parentPatterns.push(...curNode.patterns)
|
|
|
|
}
|
|
|
|
parentPatterns.push(...curNode.patterns)
|
|
|
|
curNode = curNode.children[p]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!curNode.methods.length) {
|
|
|
|
curNode.methods = []
|
|
|
|
}
|
|
|
|
|
|
|
|
const m: Record<string, HandlerSet<T>> = {}
|
|
|
|
|
|
|
|
const handlerSet: HandlerSet<T> = { handler: handler, name: this.name, score: this.order }
|
|
|
|
|
|
|
|
m[method] = handlerSet
|
|
|
|
curNode.methods.push(m)
|
|
|
|
|
|
|
|
return curNode
|
|
|
|
}
|
|
|
|
|
2023-04-30 14:07:00 +02:00
|
|
|
// getHandlerSets
|
|
|
|
private gHSets(node: Node<T>, method: string, wildcard?: boolean): HandlerSet<T>[] {
|
2022-07-02 08:09:45 +02:00
|
|
|
return (node.handlerSetCache[`${method}:${wildcard ? '1' : '0'}`] ||= (() => {
|
|
|
|
const handlerSets: HandlerSet<T>[] = []
|
2022-08-21 03:11:53 +02:00
|
|
|
for (let i = 0, len = node.methods.length; i < len; i++) {
|
|
|
|
const m = node.methods[i]
|
2022-07-02 08:09:45 +02:00
|
|
|
const handlerSet = m[method] || m[METHOD_NAME_ALL]
|
|
|
|
if (handlerSet !== undefined) {
|
2022-08-21 03:11:53 +02:00
|
|
|
handlerSets.push(handlerSet)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
2022-08-21 03:11:53 +02:00
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
return handlerSets
|
|
|
|
})())
|
|
|
|
}
|
|
|
|
|
|
|
|
search(method: string, path: string): Result<T> | null {
|
|
|
|
const handlerSets: HandlerSet<T>[] = []
|
|
|
|
const params: Record<string, string> = {}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
|
|
const curNode: Node<T> = this
|
|
|
|
let curNodes = [curNode]
|
|
|
|
const parts = splitPath(path)
|
|
|
|
|
|
|
|
for (let i = 0, len = parts.length; i < len; i++) {
|
|
|
|
const part: string = parts[i]
|
|
|
|
const isLast = i === len - 1
|
|
|
|
const tempNodes: Node<T>[] = []
|
2022-07-24 04:03:04 +02:00
|
|
|
let matched = false
|
2022-07-02 08:09:45 +02:00
|
|
|
|
|
|
|
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
|
|
|
|
const node = curNodes[j]
|
2022-07-24 04:03:04 +02:00
|
|
|
const nextNode = node.children[part]
|
|
|
|
|
|
|
|
if (nextNode) {
|
|
|
|
if (isLast === true) {
|
|
|
|
// '/hello/*' => match '/hello'
|
|
|
|
if (nextNode.children['*']) {
|
2023-04-30 14:07:00 +02:00
|
|
|
handlerSets.push(...this.gHSets(nextNode.children['*'], method, true))
|
2022-07-24 04:03:04 +02:00
|
|
|
}
|
2023-04-30 14:07:00 +02:00
|
|
|
handlerSets.push(...this.gHSets(nextNode, method))
|
2022-07-24 04:03:04 +02:00
|
|
|
matched = true
|
2022-08-21 03:11:53 +02:00
|
|
|
} else {
|
|
|
|
tempNodes.push(nextNode)
|
2022-07-24 04:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
|
|
|
|
for (let k = 0, len3 = node.patterns.length; k < len3; k++) {
|
|
|
|
const pattern = node.patterns[k]
|
|
|
|
|
|
|
|
// Wildcard
|
|
|
|
// '/hello/*/foo' => match /hello/bar/foo
|
|
|
|
if (pattern === '*') {
|
|
|
|
const astNode = node.children['*']
|
|
|
|
if (astNode) {
|
2023-04-30 14:07:00 +02:00
|
|
|
handlerSets.push(...this.gHSets(astNode, method))
|
2022-07-02 08:09:45 +02:00
|
|
|
tempNodes.push(astNode)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part === '') continue
|
|
|
|
|
|
|
|
// Named match
|
|
|
|
// `/posts/:id` => match /posts/123
|
|
|
|
const [key, name, matcher] = pattern
|
2023-01-08 09:22:44 +01:00
|
|
|
|
2023-07-20 09:48:38 +02:00
|
|
|
const child = node.children[key]
|
|
|
|
|
2023-01-08 09:22:44 +01:00
|
|
|
// `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js
|
2023-01-08 10:45:46 +01:00
|
|
|
const restPathString = parts.slice(i).join('/')
|
2023-01-08 09:22:44 +01:00
|
|
|
if (matcher instanceof RegExp && matcher.test(restPathString)) {
|
2023-07-20 09:48:38 +02:00
|
|
|
handlerSets.push(...this.gHSets(child, method))
|
2023-01-08 09:22:44 +01:00
|
|
|
params[name] = restPathString
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:45 +02:00
|
|
|
if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {
|
|
|
|
if (typeof key === 'string') {
|
|
|
|
if (isLast === true) {
|
2023-07-20 09:48:38 +02:00
|
|
|
handlerSets.push(...this.gHSets(child, method))
|
|
|
|
// `/:id/*` => match `/foo`
|
|
|
|
if (child.children['*']) {
|
|
|
|
handlerSets.push(...this.gHSets(child.children['*'], method))
|
|
|
|
}
|
2022-08-21 03:11:53 +02:00
|
|
|
} else {
|
2023-07-20 09:48:38 +02:00
|
|
|
tempNodes.push(child)
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 16:54:55 +01:00
|
|
|
// `/book/a` => no-slug
|
|
|
|
// `/book/:slug` => slug
|
|
|
|
// `/book/b` => no-slug-b
|
2022-07-24 04:03:04 +02:00
|
|
|
// GET /book/a ~> no-slug, param['slug'] => undefined
|
|
|
|
// GET /book/foo ~> slug, param['slug'] => foo
|
2022-12-07 16:54:55 +01:00
|
|
|
// GET /book/b ~> no-slug-b, param['slug'] => b
|
2022-07-24 04:03:04 +02:00
|
|
|
if (typeof name === 'string' && !matched) {
|
|
|
|
params[name] = part
|
2022-12-07 16:54:55 +01:00
|
|
|
} else {
|
2023-06-03 02:08:01 +02:00
|
|
|
if (node.children[part]) {
|
2022-12-07 16:54:55 +01:00
|
|
|
params[name] = part
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curNodes = tempNodes
|
|
|
|
}
|
|
|
|
|
2022-08-21 03:11:53 +02:00
|
|
|
const len = handlerSets.length
|
|
|
|
if (len === 0) return null
|
|
|
|
if (len === 1) return { handlers: [handlerSets[0].handler], params }
|
2022-07-02 08:09:45 +02:00
|
|
|
|
|
|
|
const handlers = handlerSets
|
|
|
|
.sort((a, b) => {
|
|
|
|
return a.score - b.score
|
|
|
|
})
|
|
|
|
.map((s) => {
|
|
|
|
return s.handler
|
|
|
|
})
|
|
|
|
|
|
|
|
return { handlers, params }
|
|
|
|
}
|
|
|
|
}
|