@@ -16,6 +16,7 @@ import {
16
16
} from './utils.js' ;
17
17
18
18
class RouteHandlerRegistry {
19
+ readonly #regexRoutes: Map < string , DynamicRoute > = new Map ( ) ;
19
20
readonly #staticRoutes: Map < string , Route > = new Map ( ) ;
20
21
readonly #dynamicRoutesSet: Set < string > = new Set ( ) ;
21
22
readonly #dynamicRoutes: DynamicRoute [ ] = [ ] ;
@@ -103,6 +104,18 @@ class RouteHandlerRegistry {
103
104
104
105
const compiled = compilePath ( route . path ) ;
105
106
107
+ if ( / [ ^ a - z A - Z 0 - 9 / - : ] / . test ( compiled . path ) ) {
108
+ if ( this . #regexRoutes. has ( route . id ) ) {
109
+ this . #logger. warn (
110
+ `Handler for method: ${ route . method } and path: ${ route . path } already exists. The previous handler will be replaced.`
111
+ ) ;
112
+ }
113
+ this . #regexRoutes. set ( route . id , {
114
+ ...route ,
115
+ ...compiled ,
116
+ } ) ;
117
+ return ;
118
+ }
106
119
if ( compiled . isDynamic ) {
107
120
const dynamicRoute = {
108
121
...route ,
@@ -171,28 +184,10 @@ class RouteHandlerRegistry {
171
184
} ;
172
185
}
173
186
174
- for ( const route of this . #dynamicRoutes) {
175
- if ( route . method !== method ) continue ;
176
-
177
- const match = route . regex . exec ( path ) ;
178
- if ( match ?. groups ) {
179
- const params = match . groups ;
180
-
181
- const processedParams = this . #processParams( params ) ;
182
-
183
- const validation = this . #validateParams( processedParams ) ;
184
-
185
- if ( ! validation . isValid ) {
186
- throw new ParameterValidationError ( validation . issues ) ;
187
- }
188
-
189
- return {
190
- handler : route . handler ,
191
- params : processedParams ,
192
- rawParams : params ,
193
- middleware : route . middleware ,
194
- } ;
195
- }
187
+ const routes = [ ...this . #dynamicRoutes, ...this . #regexRoutes. values ( ) ] ;
188
+ for ( const route of routes ) {
189
+ const result = this . #processRoute( route , method , path ) ;
190
+ if ( result ) return result ;
196
191
}
197
192
198
193
return null ;
@@ -227,6 +222,28 @@ class RouteHandlerRegistry {
227
222
) ;
228
223
}
229
224
}
225
+
226
+ #processRoute( route : DynamicRoute , method : HttpMethod , path : Path ) {
227
+ if ( route . method !== method ) return ;
228
+
229
+ const match = route . regex . exec ( path ) ;
230
+ if ( ! match ) return ;
231
+
232
+ const params = match . groups || { } ;
233
+ const processedParams = this . #processParams( params ) ;
234
+ const validation = this . #validateParams( processedParams ) ;
235
+
236
+ if ( ! validation . isValid ) {
237
+ throw new ParameterValidationError ( validation . issues ) ;
238
+ }
239
+
240
+ return {
241
+ handler : route . handler ,
242
+ params : processedParams ,
243
+ rawParams : params ,
244
+ middleware : route . middleware ,
245
+ } ;
246
+ }
230
247
}
231
248
232
249
export { RouteHandlerRegistry } ;
0 commit comments