Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkumardusad committed Dec 20, 2023
1 parent 502265d commit 8069c69
Show file tree
Hide file tree
Showing 13 changed files with 1,006 additions and 450 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Routejs is a fast and lightweight http routing engine for [Node.js](http://nodej
## Features

- Fast and lightweight
- Group routing
- Group based routing
- Host based routing
- Named routing
- Middleware support
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@routejs/router",
"version": "3.0.0",
"version": "3.0.1",
"description": "Fast and lightweight http routing engine for nodejs",
"main": "index.mjs",
"type": "module",
Expand Down
31 changes: 20 additions & 11 deletions src/host-regex.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function hostRegex(host, options = { caseSensitive: false }) {
let param = {};
let isEscape = false;
let caseSensitive = options?.caseSensitive ?? false;
for (let i in host) {
for (let i = 0; i < host.length; i++) {
let char = host[i];
// Match escape character
if (host[i] === "\\" && isEscape === false) {
Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = function hostRegex(host, options = { caseSensitive: false }) {
}
param = {};
}
param.nameStart = +i;
param.nameStart = i;
param.hasParamName = true;
} else if (param.hasParamRegex !== true && host[i].match(/^[\.]$/i)) {
if (param.hasParamName === true) {
Expand All @@ -54,21 +54,29 @@ module.exports = function hostRegex(host, options = { caseSensitive: false }) {
param = {};
} else if (host[i].match(/^[^A-Za-z0-9_]+$/i)) {
if (host[i] === "(") {
if (host[i + 1] === "?") {
if (host[i + 2] == ":") {
throw new TypeError(
`non-capturing groups are not allowed at ${i}`
);
}
throw new TypeError(`pattern cannot start with "?" at ${i}`);
}
if (param.hasParamRegex === true) {
throw new TypeError(`Capturing groups are not allowed at ${i}`);
throw new TypeError(`capturing groups are not allowed at ${i}`);
}
if (param.hasParamName === true && param.nameStart !== i - 1) {
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
param.nameEnd = i - 1;
} else {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
}
} else if (param.hasParamRegex === true && host[i] === ")") {
param.regexEnd = +i;
param.regexEnd = i;
if (param.regexStart !== param.regexEnd) {
params.push(param);
}
Expand All @@ -77,8 +85,8 @@ module.exports = function hostRegex(host, options = { caseSensitive: false }) {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexEnd = +i;
param.regexStart = i;
param.regexEnd = i;
params.push(param);
param = {};
} else if (param.hasParamRegex !== true) {
Expand All @@ -101,10 +109,10 @@ module.exports = function hostRegex(host, options = { caseSensitive: false }) {
param.hasParamRegex === true &&
typeof param.regexEnd === "undefined"
) {
throw new TypeError(`Unterminated group at ${i}`);
throw new TypeError(`unterminated group at ${i}`);
}
if (param.hasParamName === true) {
param.nameEnd = +i;
param.nameEnd = i;
param.hasParamRegex = param.hasParamRegex === true;
if (param.nameStart !== param.nameEnd) {
params.push(param);
Expand Down Expand Up @@ -160,6 +168,7 @@ module.exports = function hostRegex(host, options = { caseSensitive: false }) {
let regex =
caseSensitive === true ? new RegExp(pathRegex, "i") : new RegExp(pathRegex);

// Compiler regex to path
function compile(params = {}, options = {}) {
let tmpPath = regex.source;
let validate = options?.validate ?? false;
Expand All @@ -185,7 +194,7 @@ module.exports = function hostRegex(host, options = { caseSensitive: false }) {

let isEscape = false;
let compiledPath = "";
for (let i in tmpPath) {
for (let i = 0; i < tmpPath.length; i++) {
let char = tmpPath[i];
// Match escape character
if (tmpPath[i] === "\\" && isEscape === false) {
Expand Down
31 changes: 20 additions & 11 deletions src/host-regex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default function hostRegex(host, options = { caseSensitive: false }) {
let param = {};
let isEscape = false;
let caseSensitive = options?.caseSensitive ?? false;
for (let i in host) {
for (let i = 0; i < host.length; i++) {
let char = host[i];
// Match escape character
if (host[i] === "\\" && isEscape === false) {
Expand Down Expand Up @@ -38,7 +38,7 @@ export default function hostRegex(host, options = { caseSensitive: false }) {
}
param = {};
}
param.nameStart = +i;
param.nameStart = i;
param.hasParamName = true;
} else if (param.hasParamRegex !== true && host[i].match(/^[\.]$/i)) {
if (param.hasParamName === true) {
Expand All @@ -54,21 +54,29 @@ export default function hostRegex(host, options = { caseSensitive: false }) {
param = {};
} else if (host[i].match(/^[^A-Za-z0-9_]+$/i)) {
if (host[i] === "(") {
if (host[i + 1] === "?") {
if (host[i + 2] == ":") {
throw new TypeError(
`non-capturing groups are not allowed at ${i}`
);
}
throw new TypeError(`pattern cannot start with "?" at ${i}`);
}
if (param.hasParamRegex === true) {
throw new TypeError(`Capturing groups are not allowed at ${i}`);
throw new TypeError(`capturing groups are not allowed at ${i}`);
}
if (param.hasParamName === true && param.nameStart !== i - 1) {
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
param.nameEnd = i - 1;
} else {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
}
} else if (param.hasParamRegex === true && host[i] === ")") {
param.regexEnd = +i;
param.regexEnd = i;
if (param.regexStart !== param.regexEnd) {
params.push(param);
}
Expand All @@ -77,8 +85,8 @@ export default function hostRegex(host, options = { caseSensitive: false }) {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexEnd = +i;
param.regexStart = i;
param.regexEnd = i;
params.push(param);
param = {};
} else if (param.hasParamRegex !== true) {
Expand All @@ -101,10 +109,10 @@ export default function hostRegex(host, options = { caseSensitive: false }) {
param.hasParamRegex === true &&
typeof param.regexEnd === "undefined"
) {
throw new TypeError(`Unterminated group at ${i}`);
throw new TypeError(`unterminated group at ${i}`);
}
if (param.hasParamName === true) {
param.nameEnd = +i;
param.nameEnd = i;
param.hasParamRegex = param.hasParamRegex === true;
if (param.nameStart !== param.nameEnd) {
params.push(param);
Expand Down Expand Up @@ -160,6 +168,7 @@ export default function hostRegex(host, options = { caseSensitive: false }) {
let regex =
caseSensitive === true ? new RegExp(pathRegex, "i") : new RegExp(pathRegex);

// Compiler regex to path
function compile(params = {}, options = {}) {
let tmpPath = regex.source;
let validate = options?.validate ?? false;
Expand All @@ -185,7 +194,7 @@ export default function hostRegex(host, options = { caseSensitive: false }) {

let isEscape = false;
let compiledPath = "";
for (let i in tmpPath) {
for (let i = 0; i < tmpPath.length; i++) {
let char = tmpPath[i];
// Match escape character
if (tmpPath[i] === "\\" && isEscape === false) {
Expand Down
31 changes: 20 additions & 11 deletions src/path-regex.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function pathRegex(path, options = { caseSensitive: false }) {
let param = {};
let isEscape = false;
let caseSensitive = options?.caseSensitive ?? false;
for (let i in path) {
for (let i = 0; i < path.length; i++) {
let char = path[i];
// Match escape character
if (path[i] === "\\" && isEscape === false) {
Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = function pathRegex(path, options = { caseSensitive: false }) {
}
param = {};
}
param.nameStart = +i;
param.nameStart = i;
param.hasParamName = true;
} else if (param.hasParamRegex !== true && path[i].match(/^[\/]$/i)) {
if (param.hasParamName === true) {
Expand All @@ -51,21 +51,29 @@ module.exports = function pathRegex(path, options = { caseSensitive: false }) {
param = {};
} else if (path[i].match(/^[^A-Za-z0-9_]+$/i)) {
if (path[i] === "(") {
if (path[i + 1] === "?") {
if (path[i + 2] == ":") {
throw new TypeError(
`non-capturing groups are not allowed at ${i}`
);
}
throw new TypeError(`pattern cannot start with "?" at ${i}`);
}
if (param.hasParamRegex === true) {
throw new TypeError(`Capturing groups are not allowed at ${i}`);
throw new TypeError(`capturing groups are not allowed at ${i}`);
}
if (param.hasParamName === true && param.nameStart !== i - 1) {
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
param.nameEnd = i - 1;
} else {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
}
} else if (param.hasParamRegex === true && path[i] === ")") {
param.regexEnd = +i;
param.regexEnd = i;
if (param.regexStart !== param.regexEnd) {
params.push(param);
}
Expand All @@ -74,8 +82,8 @@ module.exports = function pathRegex(path, options = { caseSensitive: false }) {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexEnd = +i;
param.regexStart = i;
param.regexEnd = i;
params.push(param);
param = {};
} else if (param.hasParamRegex !== true) {
Expand All @@ -98,10 +106,10 @@ module.exports = function pathRegex(path, options = { caseSensitive: false }) {
param.hasParamRegex === true &&
typeof param.regexEnd === "undefined"
) {
throw new TypeError(`Unterminated group at ${i}`);
throw new TypeError(`unterminated group at ${i}`);
}
if (param.hasParamName === true) {
param.nameEnd = +i;
param.nameEnd = i;
param.hasParamRegex = param.hasParamRegex === true;
if (param.nameStart !== param.nameEnd) {
params.push(param);
Expand Down Expand Up @@ -150,6 +158,7 @@ module.exports = function pathRegex(path, options = { caseSensitive: false }) {
let regex =
caseSensitive === true ? new RegExp(pathRegex, "i") : new RegExp(pathRegex);

// Compile regex to path
function compile(params = {}, options = {}) {
let tmpPath = regex.source;
let validate = options?.validate ?? false;
Expand All @@ -175,7 +184,7 @@ module.exports = function pathRegex(path, options = { caseSensitive: false }) {

let isEscape = false;
let compiledPath = "";
for (let i in tmpPath) {
for (let i = 0; i < tmpPath.length; i++) {
let char = tmpPath[i];
// Match escape character
if (tmpPath[i] === "\\" && isEscape === false) {
Expand Down
31 changes: 20 additions & 11 deletions src/path-regex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default function pathRegex(path, options = { caseSensitive: false }) {
let param = {};
let isEscape = false;
let caseSensitive = options?.caseSensitive ?? false;
for (let i in path) {
for (let i = 0; i < path.length; i++) {
let char = path[i];
// Match escape character
if (path[i] === "\\" && isEscape === false) {
Expand Down Expand Up @@ -38,7 +38,7 @@ export default function pathRegex(path, options = { caseSensitive: false }) {
}
param = {};
}
param.nameStart = +i;
param.nameStart = i;
param.hasParamName = true;
} else if (param.hasParamRegex !== true && path[i].match(/^[\/]$/i)) {
if (param.hasParamName === true) {
Expand All @@ -51,21 +51,29 @@ export default function pathRegex(path, options = { caseSensitive: false }) {
param = {};
} else if (path[i].match(/^[^A-Za-z0-9_]+$/i)) {
if (path[i] === "(") {
if (path[i + 1] === "?") {
if (path[i + 2] == ":") {
throw new TypeError(
`non-capturing groups are not allowed at ${i}`
);
}
throw new TypeError(`pattern cannot start with "?" at ${i}`);
}
if (param.hasParamRegex === true) {
throw new TypeError(`Capturing groups are not allowed at ${i}`);
throw new TypeError(`capturing groups are not allowed at ${i}`);
}
if (param.hasParamName === true && param.nameStart !== i - 1) {
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
param.nameEnd = i - 1;
} else {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexStart = i;
}
} else if (param.hasParamRegex === true && path[i] === ")") {
param.regexEnd = +i;
param.regexEnd = i;
if (param.regexStart !== param.regexEnd) {
params.push(param);
}
Expand All @@ -74,8 +82,8 @@ export default function pathRegex(path, options = { caseSensitive: false }) {
param = {};
param.hasParamName = false;
param.hasParamRegex = true;
param.regexStart = +i;
param.regexEnd = +i;
param.regexStart = i;
param.regexEnd = i;
params.push(param);
param = {};
} else if (param.hasParamRegex !== true) {
Expand All @@ -98,10 +106,10 @@ export default function pathRegex(path, options = { caseSensitive: false }) {
param.hasParamRegex === true &&
typeof param.regexEnd === "undefined"
) {
throw new TypeError(`Unterminated group at ${i}`);
throw new TypeError(`unterminated group at ${i}`);
}
if (param.hasParamName === true) {
param.nameEnd = +i;
param.nameEnd = i;
param.hasParamRegex = param.hasParamRegex === true;
if (param.nameStart !== param.nameEnd) {
params.push(param);
Expand Down Expand Up @@ -150,6 +158,7 @@ export default function pathRegex(path, options = { caseSensitive: false }) {
let regex =
caseSensitive === true ? new RegExp(pathRegex, "i") : new RegExp(pathRegex);

// Compile regex to path
function compile(params = {}, options = {}) {
let tmpPath = regex.source;
let validate = options?.validate ?? false;
Expand All @@ -175,7 +184,7 @@ export default function pathRegex(path, options = { caseSensitive: false }) {

let isEscape = false;
let compiledPath = "";
for (let i in tmpPath) {
for (let i = 0; i < tmpPath.length; i++) {
let char = tmpPath[i];
// Match escape character
if (tmpPath[i] === "\\" && isEscape === false) {
Expand Down
Loading

0 comments on commit 8069c69

Please sign in to comment.