Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"@japa/file-system": "^3.0.0",
"@japa/runner": "^5.3.0",
"@japa/snapshot": "^2.0.10",
"@poppinss/matchit": "^3.2.0",
"@poppinss/ts-exec": "^1.4.4",
"@release-it/conventional-changelog": "^11.0.0",
"@types/accepts": "^1.3.7",
Expand Down Expand Up @@ -96,7 +97,6 @@
},
"dependencies": {
"@poppinss/macroable": "^1.1.2",
"@poppinss/matchit": "^3.2.0",
"@poppinss/middleware": "^3.2.7",
"@poppinss/qs": "^6.15.0",
"@poppinss/types": "^1.2.1",
Expand Down
19 changes: 8 additions & 11 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/

import { serialize } from 'cookie-es'
// @ts-expect-error
import matchit from '@poppinss/matchit'
import string from '@poppinss/utils/string'
import { type Encryption } from '@boringnode/encryption'
import { parseBindingReference } from '@adonisjs/fold'
Expand All @@ -21,6 +19,8 @@ import { createURL } from './client/helpers.ts'
import { type CookieOptions } from './types/response.ts'
import { type SignedURLOptions } from './types/url_builder.ts'
import type { RouteMatchers, RouteJSON, MatchItRouteToken } from './types/route.ts'
import { matchRouteTokens } from './router/route_table.ts'
import { parseRoutePattern } from './router/route_parser.ts'
import {
type MiddlewareFn,
type RouteHandlerInfo,
Expand Down Expand Up @@ -159,8 +159,7 @@ export { default as mime } from 'mime-types'
* @returns {MatchItRouteToken[]} Array of parsed route tokens
*/
export function parseRoute(pattern: string, matchers?: RouteMatchers): MatchItRouteToken[] {
const tokens = matchit.parse(pattern, matchers)
return tokens
return parseRoutePattern(pattern, matchers)
}

/**
Expand Down Expand Up @@ -215,13 +214,11 @@ export function createSignedURL(
* @returns {null | Record<string, string>} Extracted parameters or null if no match
*/
export function matchRoute(url: string, patterns: string[]): null | Record<string, string> {
const tokensBucket = patterns.map((pattern) => parseRoute(pattern))
const match = matchit.match(url, tokensBucket)
if (!match.length) {
return null
}

return matchit.exec(url, match)
return matchRouteTokens(
url,
patterns.map((pattern) => parseRoute(pattern)),
false
)
}

/**
Expand Down
113 changes: 113 additions & 0 deletions src/router/route_parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* @adonisjs/http-server
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import type { MatchItRouteToken, RouteMatchers } from '../types/route.ts'

export type ParsedRouteToken = MatchItRouteToken & {
matcher?: RegExp
}

export function stripRouteSeparators(value: string): string {
if (value === '/') {
return value
}
if (value.charCodeAt(0) === 47) {
value = value.substring(1)
}

const lastIndex = value.length - 1
return value.charCodeAt(lastIndex) === 47 ? value.substring(0, lastIndex) : value
}

/**
* Parses a route pattern into the token format shared by route matching and
* URL generation. Single leading/trailing separator stripping is preserved
* for backwards compatibility.
*/
export function parseRoutePattern(
pattern: string,
matchers: RouteMatchers = {}
): MatchItRouteToken[] {
if (pattern === '/') {
return [{ old: pattern, type: 0, val: pattern, end: '' }]
}

if (typeof matchers !== 'object') {
matchers = {}
}

let remaining = stripRouteSeparators(pattern)
let index = -1
let parameterNameEnd = 0
let segmentStart = 0
let remainingLength = remaining.length
const tokens: MatchItRouteToken[] = []

while (++index < remainingLength) {
let character = remaining.charCodeAt(index)

if (character === 58) {
segmentStart = index + 1
let type: 1 | 3 = 1
parameterNameEnd = 0
let suffix = ''

while (index < remainingLength && remaining.charCodeAt(index) !== 47) {
character = remaining.charCodeAt(index)
if (character === 63) {
parameterNameEnd = index
type = 3
} else if (character === 46 && suffix.length === 0) {
parameterNameEnd = index
suffix = remaining.substring(index)
}
index++
}

const value = remaining.substring(segmentStart, parameterNameEnd || index)
const matcher = matchers[value]
tokens.push({
old: pattern,
type,
val: value,
end: suffix,
matcher: matcher?.match,
cast: matcher?.cast,
} as ParsedRouteToken)

remaining = remaining.substring(index)
remainingLength -= index
index = 0
continue
}

if (character === 42) {
tokens.push({
old: pattern,
type: 2,
val: remaining.substring(index),
end: '',
})
continue
}

segmentStart = index
while (index < remainingLength && remaining.charCodeAt(index) !== 47) {
index++
}

const value = remaining.substring(segmentStart, index)
tokens.push({ old: pattern, type: 0, val: value, end: '' })
remaining = remaining.substring(index)
remainingLength -= index
index = segmentStart = 0
}

return tokens
}
Loading
Loading