Skip to content

Commit 03a4d88

Browse files
committed
feat: bump path-to-regexp to v8
1 parent 9ed9af4 commit 03a4d88

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

package-lock.json

Lines changed: 12 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"mustache": "4.2.0",
5151
"otpauth": "9.4.0",
5252
"parse": "7.0.1",
53-
"path-to-regexp": "6.3.0",
53+
"path-to-regexp": "8.3.0",
5454
"pg-monitor": "3.0.0",
5555
"pg-promise": "12.2.0",
5656
"pluralize": "8.0.0",

src/cloud-code/Parse.Cloud.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ const getRoute = parseClass => {
8282
'@Config' : 'config',
8383
}[parseClass] || 'classes';
8484
if (parseClass === '@File') {
85-
return `/${route}/:id?(.*)`;
85+
return `/${route}{/*id}`;
8686
}
8787
if (parseClass === '@Config') {
8888
return `/${route}`;
8989
}
90-
return `/${route}/${parseClass}/:id?(.*)`;
90+
return `/${route}/${parseClass}{/*id}`;
9191
};
9292
/** @namespace
9393
* @name Parse

src/middlewares.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ const handleRateLimit = async (req, res, next) => {
321321
try {
322322
await Promise.all(
323323
rateLimits.map(async limit => {
324-
const pathExp = new RegExp(limit.path);
324+
const pathExp = limit.path.regexp || limit.path;
325325
if (pathExp.test(req.url)) {
326326
await limit.handler(req, res, err => {
327327
if (err) {
@@ -561,10 +561,33 @@ export const addRateLimit = (route, config, cloud) => {
561561
},
562562
});
563563
}
564-
let transformPath = route.requestPath.split('/*').join('/(.*)');
565-
if (transformPath === '*') {
566-
transformPath = '(.*)';
567-
}
564+
// Transform wildcards to named parameters for path-to-regexp v8
565+
// Only transform standalone * (not those already in named params like :id* or *id)
566+
let transformPath = route.requestPath;
567+
// Replace * that are not part of named parameter syntax
568+
// Use a function to check if * is part of a named parameter
569+
transformPath = transformPath.replace(/\*/g, (match, offset, string) => {
570+
// Check if this * is part of a named parameter (e.g., :id*)
571+
// Look backwards to find if there's a : before this * (without a / in between)
572+
let isNamedParam = false;
573+
for (let i = offset - 1; i >= 0; i--) {
574+
if (string[i] === '/') {
575+
break; // Found a /, so this * is not part of a named param
576+
}
577+
if (string[i] === ':') {
578+
isNamedParam = true; // Found :, so this * is part of a named param
579+
break;
580+
}
581+
}
582+
// Check if * is followed by an identifier (like *id) - this is already a named wildcard
583+
const nextChar = offset + 1 < string.length ? string[offset + 1] : null;
584+
const isNamedWildcard = nextChar && /[a-zA-Z_$]/.test(nextChar);
585+
// Only transform if it's not part of a named parameter and not already a named wildcard
586+
if (!isNamedParam && !isNamedWildcard) {
587+
return '*path';
588+
}
589+
return match; // Keep * if it's part of :param* or *id
590+
});
568591
config.rateLimits.push({
569592
path: pathToRegexp(transformPath),
570593
handler: rateLimit({

0 commit comments

Comments
 (0)