| 264 | * let one hang an index. |
| 265 | */ |
| 266 | export function compileMatcher(source: string): RegExp | null { |
| 267 | if (!source.startsWith('/') || source.length > MAX_MATCHER_LENGTH) return null; |
| 268 | let out = ''; |
| 269 | for (let i = 0; i < source.length; ) { |
| 270 | const param = /^\/:[A-Za-z_]\w*/.exec(source.slice(i)); |
| 271 | if (param) { |
| 272 | i += param[0].length; |
| 273 | let body = '[^/]+'; |
| 274 | if (source[i] === '(') { |
| 275 | const close = closingParen(source, i); |
| 276 | const group = close < 0 ? null : readGroup(source.slice(i + 1, close)); |
| 277 | if (group === null) return null; |
| 278 | body = group; |
| 279 | i = close + 1; |
| 280 | } |
| 281 | const modifier = /[?*+]/.test(source[i] ?? '') ? source[i++] : ''; |
| 282 | out += modifier ? `(?:/${body})${modifier}` : `/${body}`; |
| 283 | continue; |
| 284 | } |
| 285 | const ch = source[i]!; |
| 286 | if (ch === '(') { |
| 287 | const close = closingParen(source, i); |
| 288 | const group = close < 0 ? null : readGroup(source.slice(i + 1, close)); |
| 289 | if (group === null || /[?*+]/.test(source[close + 1] ?? '')) return null; |
| 290 | out += group; |
| 291 | i = close + 1; |
| 292 | continue; |
| 293 | } |
| 294 | if (!/[\w./~-]/.test(ch)) return null; |
| 295 | out += escapeRegex(ch); |
| 296 | i++; |
| 297 | } |
| 298 | if ((out.split(FILE_WITH_EXTENSION).join('').match(/\.[*+]|\)[*+?]/g)?.length ?? 0) > MAX_REPEATS) return null; |
| 299 | // `/?`: path-to-regexp's trailing delimiter, which is how `/:path*` takes `/`. |
| 300 | return new RegExp(`^${out}/?$`); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Addresses a route serves, one of each shape: a parameter is a segment no |