Skip to content

Commit 3fcc019

Browse files
authored
fix(vercel): valid regex for o11y routes (#3500)
1 parent 8315203 commit 3fcc019

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/presets/vercel/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,31 @@ function normalizeRouteSrc(route: string): string {
359359
.split("/")
360360
.map((segment) => {
361361
if (segment.startsWith("**")) {
362-
return segment === "**" ? "?(?<_>.*)" : `?(?<${segment.slice(3)}>.+)`;
362+
return segment === "**"
363+
? "(?:.*)"
364+
: `?(?<${namedGroup(segment.slice(3))}>.+)`;
363365
}
364366
if (segment === "*") {
365367
return `(?<_${idCtr++}>[^/]*)`;
366368
}
367369
if (segment.includes(":")) {
368370
return segment
369-
.replace(/:(\w+)/g, (_, id) => `(?<${id}>[^/]+)`)
371+
.replace(/:(\w+)/g, (_, id) => `(?<${namedGroup(id)}>[^/]+)`)
370372
.replace(/\./g, String.raw`\.`);
371373
}
372374
return segment;
373375
})
374376
.join("/");
375377
}
376378

379+
// Valid PCRE capture group name
380+
function namedGroup(input = "") {
381+
if (/\d/.test(input[0])) {
382+
input = `_${input}`;
383+
}
384+
return input.replace(/[^a-zA-Z0-9_]/g, "") || "_";
385+
}
386+
377387
// Output is a destination pathname to function name
378388
function normalizeRouteDest(route: string) {
379389
return (

test/presets/vercel.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ describe("nitro:preset:vercel", async () => {
422422
},
423423
{
424424
"dest": "/api/typed/todos/[...]",
425-
"src": "/api/typed/todos/?(?<_>.*)",
425+
"src": "/api/typed/todos/(?:.*)",
426426
},
427427
{
428428
"dest": "/api/typed/todos/[todoId]/comments/[...commentId]",

0 commit comments

Comments
 (0)