Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ module.exports = class Router extends EventEmitter {
}
routes.push(route);
// normal routes optimization
if(canBeOptimized(route.path) && route.pattern !== '/*' && !this.parent && this.get('case sensitive routing') && this.uwsApp) {
if(!route.complex && canBeOptimized(route.path) && !this.parent && this.get('case sensitive routing') && this.uwsApp) {
if(supportedUwsMethods.has(method)) {
const optimizedPath = this._optimizeRoute(route, this._routes);
if(optimizedPath) {
Expand Down
27 changes: 19 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,25 @@ function patternToRegex(pattern, isPrefix = false) {
}

let wildcardIndex = 0;
let regexPattern = pattern
.replaceAll('.', '\\.')
.replaceAll('-', '\\-')
.replaceAll(/(\*|\(.*?\))/g, (match) => `(?<_wc${wildcardIndex++}>${match.startsWith('(') ? match.slice(1, -1) : match.replaceAll('*', '.*')})`) // Convert * to .* and stuff in parentheses to capture group
.replace(/\/:(\w+)(\(.+?\))?\??/g, (match, param, regex) => {
const optional = match.endsWith('?');
return `\\/${optional ? '?' : ''}(?<${param}>${regex ? regex + '($|\\/)' : '[^/]+'})${optional ? '?' : ''}`;
}); // Convert :param to capture group
let regexPattern = '';
const captureGroupTest = /(\/|[.-]+):(\w+)(?:\((.+?)\))?\??/g;
let offset = 0;
while (true) {
const result = captureGroupTest.exec(pattern);
// Process last preceding part if matched, or final part if match ended
regexPattern += pattern.substring(offset, result?.index ?? pattern.length)
.replaceAll('.', '\\.')
.replaceAll('-', '\\-')
.replaceAll(/(\*|\(.*?\))/g, (match) => // Convert * to .* and stuff in parentheses to capture group
`(?<_wc${wildcardIndex++}>${match.startsWith('(') ? match.slice(1, -1) : match.replaceAll('*', '.*')})`
);
if (!result) break;
const [match, prefix, param, regex] = result;
const optional = match.endsWith('?');
// Convert :param to capture group
regexPattern += `${optional ? '(' : ''}${prefix}(?<${param}>${regex ? regex + '(?=$|\/)' : '[^/]+'})${optional ? ')?' : ''}`;
offset = result.index + match.length;
}

return new RegExp(`^${regexPattern}${isPrefix ? '(?=$|\/)' : '$'}`);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/tests/routers/multi-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const express = require("express");

const app = express();

app.get("/api/1.0/info/:subdir(ab|cd)?/:item([0-9A-Za-z-_]{6}.[^.])/:empty(.{0})?/details", (req, res) => {
res.json({path: req.path, params: req.params});
});

app.get("/api/1.0/projects/:project_id/user/:user_id", (req, res) => {
res.json({path: req.path, params: req.params});
});
Expand All @@ -12,6 +16,10 @@ app.get("/api/1.0/projects/:project_id/users", (req, res) => {
res.json({path: req.path, params: req.params});
});

app.use((req, res, next) => {
res.send('404');
});

app.listen(13333, async () => {
console.log("Server is running at http://localhost:13333");

Expand All @@ -22,6 +30,21 @@ app.listen(13333, async () => {
fetch("http://localhost:13333/api/1.0/projects/456/user/789").then((res) =>
res.json()
),
fetch("http://localhost:13333/api/1.0/info/AAAAAA.2//details").then((res) =>
res.json()
),
fetch("http://localhost:13333/api/1.0/info/zzzzzz+2//details").then((res) =>
res.json()
),
fetch("http://localhost:13333/api/1.0/info/ab/Ab_23-+$/details").then((res) =>
res.json()
),
fetch("http://localhost:13333/api/1.0/info/cd//AAAAAA.a///details").then((res) =>
res.json()
),
fetch("http://localhost:13333/api/1.0/info/Ab%20c.+//details").then((res) =>
res.json()
),
]);

console.log(responses);
Expand Down
7 changes: 7 additions & 0 deletions tests/tests/routing/special-characters.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ app.get('/hi-bye*a', (req, res) => {
res.send('hi-bye');
});

app.get('/test/:from--:to', (req, res) => {
res.send(`from: ${req.params.from}, to: ${req.params.to}`);
});

app.listen(13333, async () => {
console.log('Server is running on port 13333');

Expand All @@ -23,6 +27,9 @@ app.listen(13333, async () => {

res = await fetch('http://localhost:13333/test/hiAbyeaa');
console.log(await res.text());

res = await fetch('http://localhost:13333/test/123--xyz');
console.log(await res.text());

process.exit(0);
})
Loading